Selaa lähdekoodia

eval zk pub params into circuit

ada 5 vuotta sitten
vanhempi
sitoutus
c080a208a4
4 muutettua tiedostoa jossa 41 lisäystä ja 9 poistoa
  1. 11 3
      lisp/core.rs
  2. 8 2
      lisp/jubjub.lisp
  3. 16 3
      lisp/lisp.rs
  4. 6 1
      lisp/types.rs

+ 11 - 3
lisp/core.rs

@@ -8,7 +8,8 @@ use crate::printer::pr_seq;
 use crate::reader::read_str;
 use crate::types::MalErr::ErrMalVal;
 use crate::types::MalVal::{
-    Add, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Str, Sub, Sym, Vector, Public, Private, AddOne
+    Add, AddOne, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Params, Private, Public,
+    Str, Sub, Sym, Vector,
 };
 use crate::types::{MalArgs, MalRet, MalVal, _assoc, _dissoc, atom, error, func, hash_map};
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
@@ -283,12 +284,18 @@ fn div_scalar(a: MalArgs) -> MalRet {
         (Str(a0), Str(a1)) => {
             let (mut s0, mut s1) = (Scalar::from_string(&a0), Scalar::from_string(&a1));
             let ret = s1.invert().map(|other| *&s0 * other);
-            Ok(Str(std::string::ToString::to_string(&ret.unwrap())[2..].to_string()))
+            Ok(Str(
+                std::string::ToString::to_string(&ret.unwrap())[2..].to_string()
+            ))
         }
         _ => error("expected (scalar, scalar"),
     }
 }
 
+fn cs_params(a: MalArgs) -> MalRet {
+    Ok(Params(Rc::new(a[0].clone())))
+}
+
 fn cs_public(a: MalArgs) -> MalRet {
     Ok(Public(Rc::new(a[0].clone()).clone()))
 }
@@ -434,6 +441,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("lc2", func(|a| Ok(MalVal::Lc2))),
         ("enforce", func(|a| Ok(MalVal::Enforce))),
         ("public", func(cs_public)),
-        ("private", func(cs_private))
+        ("private", func(cs_private)),
+        ("params", func(cs_params)),
     ]
 }

+ 8 - 2
lisp/jubjub.lisp

@@ -1,3 +1,4 @@
+;; public params
 (def! a_u "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e")
 (def! a_v "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891")
 (def! b_u "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e")
@@ -12,7 +13,6 @@
 (def! C (fn* [x1 y1 x2 y2] (* d (A x1 y2) (B y1 x2))))
 (def! P.x (fn* [x1 y1 x2 y2] (/ (+ (A x1 y2) (B y1 x2)) (+ one (C x1 y1 x2 y2)))))
 (def! P.y (fn* [x1 y1 x2 y2] (/ (- (U x1 y1 x2 y2) (A x1 y2) (B y1 x2)) (+ one (C x1 y1 x2 y2)))))
-;; *cs* is the symbol that create a zkvm with a constraint system
 (def! jubjub-add (fn* [x1 y1 x2 y2] (cs! circuit (
                     (add lc0 x1)
                     (add lc0 y1)
@@ -37,7 +37,7 @@
                     enforce 
                     ))))
 (def! circuit (jubjub-add a_u a_v b_u b_v))
-(println circuit)
+;;(println circuit)
 (def! circuit (cs! circuit (
                     (public (P.x a_u a_v b_u b_v)) 
                     (public (P.y a_u a_v b_u b_v)) 
@@ -50,4 +50,10 @@
                     (add lc2 (P.y a_u a_v b_u b_v))
                     enforce
                   )))
+;;(println circuit)
+;; contract exection
+(def! circuit (cs! circuit (
+                            (params [a_u a_v b_u b_v])
+                            )))
+
 (println circuit)

+ 16 - 3
lisp/lisp.rs

@@ -26,8 +26,8 @@ extern crate regex;
 mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{
-    Add, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Private, Public, Str, Sub, Sym,
-    Vector, Zk, AddOne
+    Add, AddOne, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Params, Private, Public, Str,
+    Sub, Sym, Vector, Zk,
 };
 use crate::types::ZKCircuit;
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
@@ -357,6 +357,8 @@ fn zk_circuit_create(a1: &MalVal, env: &Env) -> ZKCircuit {
         constraints: Vec::new(),
         private: Vec::new(),
         public: Vec::new(),
+        params: Vec::new(),
+        verifying_key: Vec::new(),
     };
     zk_circuit
 }
@@ -418,6 +420,18 @@ fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
                                 zk.public
                                     .push(Scalar::from_string(&a.pr_str(false).to_string()));
                             }
+                            Params(a) => {
+                                match a.as_ref() {
+                                    Vector(v, _) => {
+                                        for i in v.iter() {
+                                            zk.params.push(Scalar::from_string(
+                                                &i.pr_str(false).to_string(),
+                                            ));
+                                        }
+                                    }
+                                    _ => println!("params called with a non-seq"),
+                                };
+                            }
                             Enforce => {
                                 zk.constraints.push(ConstraintInstruction::Enforce);
                             }
@@ -485,7 +499,6 @@ fn repl_load(file: String) -> Result<(), ()> {
     for (k, v) in core::ns() {
         env_sets(&repl_env, k, v);
     }
-    let _ = rep("(def! *host-language* \"rust\")", &repl_env);
     let _ = rep("(def! not (fn* (a) (if a false true)))", &repl_env);
     let _ = rep(
         "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))",

+ 6 - 1
lisp/types.rs

@@ -6,7 +6,9 @@ use itertools::Itertools;
 
 use crate::env::{env_bind, Env};
 use crate::types::MalErr::{ErrMalVal, ErrString};
-use crate::types::MalVal::{Atom, Bool, Func, Hash, Int, List, MalFunc, Nil, Str, Sym, Vector, Public, Private };
+use crate::types::MalVal::{
+    Atom, Bool, Func, Hash, Int, List, MalFunc, Nil, Private, Public, Str, Sym, Vector,
+};
 
 use bls12_381::Scalar;
 use sapvi::{
@@ -53,6 +55,7 @@ pub enum MalVal {
     Sub(Rc<MalVal>, Rc<MalVal>),
     Public(Rc<MalVal>),
     Private(Rc<MalVal>),
+    Params(Rc<MalVal>),
 }
 
 #[derive(Debug, Clone)]
@@ -61,6 +64,8 @@ pub struct ZKCircuit {
     pub constraints: Vec<ConstraintInstruction>,
     pub private: Vec<Scalar>,
     pub public: Vec<Scalar>,
+    pub params: Vec<Scalar>,
+    pub verifying_key: Vec<Scalar>,
 }
 
 #[derive(Debug)]