Selaa lähdekoodia

mint contract first version

plato 5 vuotta sitten
vanhempi
sitoutus
16b6a1cc05
3 muutettua tiedostoa jossa 46 lisäystä ja 36 poistoa
  1. 30 19
      lisp/examples/macro-test.lisp
  2. 16 16
      lisp/lisp.rs
  3. 0 1
      lisp/types.rs

+ 30 - 19
lisp/examples/macro-test.lisp

@@ -186,12 +186,6 @@
 )
 ))
 
-;; cs.enforce(
-;;     || "boolean constraint",
-;;     |lc| lc + CS::one() - var,
-;;     |lc| lc + var,
-;;     |lc| lc,
-;; );
 (defmacro! zk-boolean (fn* [val] (
         (let* [var (gensym)] (
             `(alloc ~var ~val)
@@ -219,15 +213,10 @@
         (def! u-add (get add-result "u3"))
         (def! v-add (get add-result "v3"))        
         (def! val (last (last (zk-double u-add v-add))))             
-        ;; debug
-        (println 'first-double val)
-        (println 'r r)
-        (println 'cond cond-result)
-        (println 'add add-result)
-        (println 'double acc val)        
         (def! acc (i+ acc 1))        
     ))
-    (println 'out val)
+    (val)
+    ;; { "u3" (get val "u3"), "v3" (get val "v3") }    
 )))
 
 (load-file "mimc-constants.lisp")
@@ -276,20 +265,42 @@
     ))
 )))
 
+(def! mint-contract (fn* [public-u public-v] (
+    (def! randomness (rnd-scalar))    
+    (def! witness-result (zk-witness public-u public-v))    
+    (def! not-small-order (zk-not-small-order? public-u public-v))    
+    (def! g-vcr-u (alloc "g-vcr-u" public-u))
+    (def! g-vcr-v (alloc "g-vcr-v" public-v))
+    (def! mul-result (last (last (jj-mul g-vcr-u g-vcr-v randomness))))
+    (println 'mul-result mul-result)
+    (def! rcvu (alloc-input "rcvu" (get mul-result "u3")))
+    (def! rcvr (alloc-input "rcvr" (get mul-result "v3")))
+    (enforce
+        (scalar::one rcvu)
+        (scalar::one cs::one)
+        (scalar::one rcvu)
+    )
+    (enforce
+        (scalar::one rcvr)
+        (scalar::one cs::one)
+        (scalar::one rcvr)
+    )    
+)))
 
 (prove 
-  (        
-    ;; (def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
-    ;; (def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
-    ;; (mimc left right)    
-    (def! param3 (rnd-scalar))
+  (            
     (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
     (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
-    (jj-mul param-u param-v param3)
+    (mint-contract param-u param-v)    
   )
 )
 
 ;; following some examples 
+;; (def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
+;; (def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
+;; (mimc left right)    
+;; (def! param3 (rnd-scalar))
+;; (jj-mul param-u param-v param3)    
 ;; (def! param3 (rnd-scalar))
 ;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
 ;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))

+ 16 - 16
lisp/lisp.rs

@@ -346,7 +346,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         prove(a1.clone(), env.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc-const" => {
-                        let start = Instant::now();
+                        // let start = Instant::now();
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
@@ -370,11 +370,11 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                                 Alloc(allocs),
                             )?;
                         }
-                        println!("Alloc Const: {:?}", start.elapsed());
+                        // println!("Alloc Const: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc-input" => {
-                        let start = Instant::now();
+                        // let start = Instant::now();
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
@@ -398,11 +398,11 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                                 Alloc(allocs),
                             )?;
                         }
-                        println!("Alloc Input: {:?}", start.elapsed());
+                        // println!("Alloc Input: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc" => {
-                        let start = Instant::now();
+                        // let start = Instant::now();
                         let a1 = l[1].clone();
                         let mut value = eval(l[2].clone(), env.clone())?;
                         if let Func(_, _) = value {
@@ -429,7 +429,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                                 Alloc(allocs),
                             )?;
                         }
-                        println!("Alloc: {:?}", start.elapsed());
+                        // println!("Alloc: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
@@ -494,29 +494,29 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                             }
                             _ => {}
                         };
-                        let enforce_vec = get_enforce_allocs(&env);
+                        let mut enforce_vec = get_enforce_allocs(&env).clone();
                         let enforce = EnforceAllocation {
                             idx: enforce_vec.len() + 1,
                             left: left_vec,
                             right: right_vec,
                             output: out_vec,
                         };
-                        let mut new_vec: Vec<EnforceAllocation> = vec![enforce];
-                        for value in enforce_vec.iter() {
-                            new_vec.push(value.clone());
-                        }
-                        // TODO change it
+                        enforce_vec.push(enforce);
+                        // let mut new_vec: Vec<EnforceAllocation> = vec![enforce];
+                        // for value in enforce_vec.iter() {
+                        //     new_vec.push(value.clone());
+                        // }
                         if let Some(e) = &env.outer {
                             env_set(
                                 &e,
                                 Sym("AllocationsEnforce".to_string()),
-                                vector![vec![Enforce(Rc::new(new_vec.clone()))]],
+                                vector![vec![Enforce(Rc::new(enforce_vec))]],
                             )?;
                         } else {
                             env_set(
                                 &env,
                                 Sym("AllocationsEnforce".to_string()),
-                                vector![vec![Enforce(Rc::new(new_vec.clone()))]],
+                                vector![vec![Enforce(Rc::new(enforce_vec))]],
                             )?;
                         }
 
@@ -639,7 +639,7 @@ pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
 }
 
 pub fn prove(_ast: MalVal, env: Env) -> MalRet {
-    // let start = Instant::now();
+    let start = Instant::now();
     let allocs_input = get_allocations(&env, "AllocationsInput");
     let allocs = get_allocations(&env, "Allocations");
     let enforce_allocs = get_enforce_allocs(&env);
@@ -679,7 +679,7 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
     let result = groth16::verify_proof(verifying_key.as_ref().unwrap(), &proof, &vec_input);
     println!("vec public {:?}", vec_input);
     println!("result {:?}", result);
-
+    println!("Elapsed time: {:?}", start.elapsed());
     Ok(MalVal::Nil)
 }
 

+ 0 - 1
lisp/types.rs

@@ -37,7 +37,6 @@ pub struct LispCircuit {
     pub params: HashMap<String, MalVal>,
     pub allocs: HashMap<String, MalVal>,
     pub alloc_inputs: HashMap<String, MalVal>,
-    //    todo change this for a ordered data structure so enforce
     pub constraints: Vec<EnforceAllocation>,
 }