Browse Source

added zk-double

plato 5 years ago
parent
commit
43dd0ec21c
3 changed files with 63 additions and 7 deletions
  1. 17 0
      lisp/core.rs
  2. 46 6
      lisp/examples/macro-test.lisp
  3. 0 1
      lisp/types.rs

+ 17 - 0
lisp/core.rs

@@ -423,6 +423,22 @@ fn scalar_square(a: MalArgs) -> MalRet {
     }
 }
 
+fn scalar_double(a: MalArgs) -> MalRet {
+    match a[0].clone() {
+        ZKScalar(a0) => {
+            let z0 = a0.clone();
+            Ok(ZKScalar(z0.double()))
+        }
+        Str(a0) => {
+            let s0 = bls12_381::Scalar::from_string(&a0);
+            Ok(ZKScalar(s0.double()))
+        }
+        _ => error(
+            &format!("scalar double expect (zkscalar or string) found \n {:?}", a).to_string(),
+        ),
+    }
+}
+
 fn add_scalar(a: MalArgs) -> MalRet {
     match (a[0].clone(), a[1].clone()) {
         (Func(_, _), ZKScalar(a1)) => {
@@ -578,5 +594,6 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("cs::one", func(cs_one)),
         ("second", func(second)),
         ("genrand", func(gen_rand)),
+        ("double", func(scalar_double)),
     ]
 }

+ 46 - 6
lisp/examples/macro-test.lisp

@@ -40,21 +40,60 @@
         `(def! ~EDWARDS_D (alloc-const ~EDWARDS_D (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
         `(def! ~u2 (alloc ~u2 (get (nth (nth (zk-square ~val1) 0) 3) "v2")))
         `(def! ~v2 (alloc ~v2 (get (nth (nth (zk-square ~val2) 0) 3) "v2")))
-        ;; `(def! result (alloc-input ~u2v2 (get (last (last (zk-mul ~u2 ~v2))) "result")))        
-        ;; `(def! ~u2 (alloc ~u2 (square ~val1)))
-        ;; `(def! ~v2 (alloc ~v2 (square ~val2)))
-        `(def! result (alloc-input ~u2v2 (* ~u2 ~v2)))        
+        `(def! result (alloc-input ~u2v2 (get (last (last (zk-mul ~u2 ~v2))) "result")))        
         `(enforce  
             ((scalar::one::neg ~u2) (scalar::one ~v2))
             (scalar::one cs::one)
             ((scalar::one cs::one) (~EDWARDS_D ~u2v2))
-            ;; (scalar::one cs::one)
          )
         `{ "result" result }
         )
     ))
 ))
 
+(defmacro! zk-double (fn* [val1 val2] (
+        (let* [u (gensym)
+               v (gensym)
+               u3 (gensym)
+               v3 (gensym)
+               T (gensym)
+               A (gensym)
+               C (gensym)
+               EDWARDS_D (gensym)] (
+        `(def! ~EDWARDS_D (alloc-const ~EDWARDS_D (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
+        `(def! ~u (alloc ~u ~val1))
+        `(def! ~v (alloc ~v ~val2))
+        `(def! ~T (alloc ~T (* (+ ~val1 ~val2) (+ ~val1 ~val2))))
+        `(def! ~A (alloc ~A (* ~u ~v)))
+        `(def! ~C (alloc ~C (* (square ~A) ~EDWARDS_D)))
+        `(def! ~u3 (alloc-input ~u3 (/ (double ~A) (+ scalar::one ~C))))
+        ;; double check why t1.invert is needed on ecc.rs code 
+        `(def! ~v3 (alloc-input ~v3 (/ (- ~T (double ~A)) (- scalar::one ~C))))
+        `(enforce  
+            ((scalar::one ~u) (scalar::one ~v))
+            ((scalar::one ~u) (scalar::one ~v))
+            (scalar::one ~T)
+         )
+         `(enforce  
+            (~EDWARDS_D ~A)
+            (scalar::one ~A)
+            (scalar::one ~C)
+         )
+         `(enforce  
+            ((scalar::one cs::one) (scalar::one ~C))
+            (scalar::one ~u3)
+            ((scalar::one ~A) (scalar::one ~A))    
+         )
+         `(enforce  
+            ((scalar::one cs::one) (scalar::one::neg ~C))
+            (scalar::one ~v3)
+            ((scalar::one ~T) (scalar::one::neg ~A) (scalar::one::neg ~A))    
+         )    
+        ;; `{ "u3" ~u3, "v3" ~v3 }
+        )
+    ))
+))
+
 (def! param1 (scalar 3))
 (def! param2 (scalar 9))
 (def! param-u (scalar "273f910d9ecc1615d8618ed1d15fef4e9472c89ac043042d36183b2cb4d7ef51"))
@@ -63,6 +102,7 @@
   (
     ;; (println (zk-square param1))
     ;; (println (zk-mul param1 param2))
-    (println 'witness (zk-witness param-u param-v))
+    ;; (println 'witness (zk-witness param-u param-v))
+    (println 'double (zk-double param-u param-v))
   )
 )

+ 0 - 1
lisp/types.rs

@@ -97,7 +97,6 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
 
         println!("Allocations Input\n");
         for (k, v) in &self.alloc_inputs {
-            // println!("k {:?} v {:?}", k, v);
             match v {
                 MalVal::ZKScalar(val) => {
                     let var = cs.alloc_input(|| k, || Ok(*val))?;