瀏覽代碼

Merge pull request #9 from narodnik/feature/lisp

Feature/lisp
ada 5 年之前
父節點
當前提交
743a5b0701
共有 6 個文件被更改,包括 82 次插入81 次删除
  1. 8 2
      lisp/core.rs
  2. 39 0
      lisp/jubjub-add.lisp
  3. 0 70
      lisp/jubjub.lisp
  4. 25 6
      lisp/lisp.rs
  5. 1 1
      lisp/run.sh
  6. 9 2
      lisp/types.rs

+ 8 - 2
lisp/core.rs

@@ -267,6 +267,7 @@ fn sub_scalar(a: MalArgs) -> MalRet {
 }
 
 fn mul_scalar(a: MalArgs) -> MalRet {
+    println!("{:?}", a);
     match (a[0].clone(), a[1].clone()) {
         (ZKScalar(mut a0), ZKScalar(a1)) => {
             // let (mut s0, s1) = (Scalar::from_string(&a0), Scalar::from_string(&a1));
@@ -357,15 +358,20 @@ fn scalar_from(a: MalArgs) -> MalRet {
 
 fn add_scalar(a: MalArgs) -> MalRet {
     match (a[0].clone(), a[1].clone()) {
+        (ZKScalar(a0), ZKScalar(a1)) => {
+            let (mut z0, z1) = (a0.clone(), a1.clone());
+            z0.add_assign(z1);
+            Ok(ZKScalar(z0))
+        },
         (Str(a0), Str(a1)) => {
             let (mut s0, s1) = (
                 bls12_381::Scalar::from_string(&a0),
                 bls12_381::Scalar::from_string(&a1),
             );
             s0.add_assign(s1);
-            Ok(Str(std::string::ToString::to_string(&s0)[2..].to_string()))
+            Ok(ZKScalar(s0))
         }
-        _ => error("expected (scalar, scalar"),
+        _ => error(&format!("add scalar expected (scalar, scalar)\n {:?}", a).to_string()),
     }
 }
 

+ 39 - 0
lisp/jubjub-add.lisp

@@ -0,0 +1,39 @@
+(println "jubjub-add.lisp")
+;; Compute U = (u1 + v1) * (v2 - EDWARDS_A*u2)
+;;           = (u1 + v1) * (u2 + v2)
+( (let* [
+      EDWARDS_D (alloc-const "EDWARDS_D" (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1"))
+      u1 (alloc "u1" (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
+      v1 (alloc "v1" (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
+      u2 (alloc "u2" (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
+      v2 (alloc "v2" (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
+      U (alloc-input "U" (* (+ u1 u2) (+ v1 v2)))
+      A (alloc-input "A" (* v2 u1))
+      B (alloc-input "B" (* u2 v1))
+      C (alloc-input "C" (* EDWARDS_D (* A B)))
+      ]
+(prove
+ (setup 
+  (
+  (enforce  
+    (
+     (scalar::one u1)
+     (scalar::one v1)
+    )
+    (
+     (scalar::one u2)
+     (scalar::one v2)
+    )
+    (scalar::one U)
+  )
+  (enforce
+    (EDWARDS_D A)
+    (scalar::one B)
+    (scalar::one C)
+  )
+  )
+ )
+)
+)
+ )
+;; (println 'verify  (MyCircuit (scalar 27)))

+ 0 - 70
lisp/jubjub.lisp

@@ -1,70 +0,0 @@
-;; public params
-(def! a_u "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e")
-(def! a_v "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891")
-(def! b_u "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e")
-(def! b_v "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891")
-(def! a "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000")
-(def! d "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")
-(def! one "0000000000000000000000000000000000000000000000000000000000000001")
-(defzk! circuit ())
-;; U should be evaluated just once
-(def! U (fn* [x1 y1 x2 y2] (* (+ x1 y1) (+ x2 y2))))
-(def! A (fn* [x1 y2] (* y2 x1)))
-(def! B (fn* [y1 x2] (* x2 y1)))
-(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)))))
-
-
-
-;; lc0 = bellman::LinearCombination::<Scalar>::zero();
-;; (lc0-args LinearCombination<Scalar>)
-
-;; (cs! circuit (lc0-args) (lc1-args) (lc2-args))
-
-;; (lc-add-coeff 1 1)
-
-(def! jubjub-add (fn* [x1 y1 x2 y2] (cs! circuit (
-                    (add lc0 x1)
-                    (add lc0 y1)
-                    (add lc1 x2)
-                    (add lc1 y2)
-                    (add lc2 (U x1 y1 x2 y2))
-                    enforce
-;; Compute P.x = (A + B) / (1 + C)
-                    (add-one lc0)
-                    (add lc0 (C x1 y1 x2 y2))
-                    (add lc1 (P.x x1 y1 x2 y2))
-                    (add lc1 (A x1 y2))
-                    (add lc1 (B y1 x2))
-                    enforce
-;; Compute P.y = (U - A - B) / (1 - C)                    
-                    (add-one lc0)
-                    (sub lc0 (C x1 y1 x2 y2))
-                    (add lc1 (P.y x1 y1 x2 y2))
-                    (add lc2 (U x1 y1 x2 y2))
-                    (sub lc2 (A x1 y2))
-                    (sub lc2 (B y1 x2))
-                    enforce 
-                    ))))
-(def! circuit (jubjub-add a_u a_v b_u b_v))
-;;(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)) 
-                    (add lc0 (P.x a_u a_v b_u b_v))
-                    (add-one lc1)
-                    (add lc2 (P.x a_u a_v b_u b_v))
-                    enforce
-                    (add lc0 (P.y a_u a_v b_u b_v))
-                    (add-one lc1)
-                    (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)

+ 25 - 6
lisp/lisp.rs

@@ -302,6 +302,23 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         ast = eval(a1.clone(), env.clone())?;
                         prove(a1.clone(), env.clone())
                     }
+                    Sym(ref a0sym) if a0sym == "alloc-const" => {
+                        let a1 = l[1].clone();
+                        let value = eval(l[2].clone(), env.clone())?;
+                        let result = eval(value.clone(), env.clone())?;
+                        let allocs = get_allocations(&env, "AllocationsConst");
+                        let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
+                        for (k, v) in allocs.iter() {
+                            new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
+                        }
+                        new_hm.insert(a1.pr_str(false), result.clone());
+                        env_set(
+                            &env,
+                            Sym("AllocationsConst".to_string()),
+                            Hash(Rc::new(new_hm), Rc::new(Nil)),
+                        )?;
+                        Ok(result.clone())
+                    }
                     Sym(ref a0sym) if a0sym == "alloc-input" => {
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
@@ -311,13 +328,13 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         for (k, v) in allocs.iter() {
                             new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
                         }
-                        new_hm.insert(a1.pr_str(false), result);
+                        new_hm.insert(a1.pr_str(false), result.clone());
                         env_set(
                             &env,
                             Sym("AllocationsInput".to_string()),
                             Hash(Rc::new(new_hm), Rc::new(Nil)),
                         )?;
-                        Ok(Nil)
+                        Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc" => {
                         let a1 = l[1].clone();
@@ -328,13 +345,13 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         for (k, v) in allocs.iter() {
                             new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
                         }
-                        new_hm.insert(a1.pr_str(false), result);
+                        new_hm.insert(a1.pr_str(false), result.clone());
                         env_set(
                             &env,
                             Sym("Allocations".to_string()),
                             Hash(Rc::new(new_hm), Rc::new(Nil)),
                         )?;
-                        Ok(Nil)
+                        Ok(result.clone())
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
                     Sym(ref a0sym) if a0sym == "enforce" => {
@@ -503,10 +520,11 @@ pub fn setup(_ast: MalVal, env: Env) -> Result<PreparedVerifyingKey<Bls12>, MalE
     // be generated securely using a multiparty computation.
     let allocs_input = get_allocations(&env, "AllocationsInput");
     let allocs = get_allocations(&env, "Allocations");
+    let allocs_const = get_allocations(&env, "AllocationsConst");
     let enforce_allocs = get_enforce_allocs(&env);
 
     let c = LispCircuit {
-        params: vec![],
+        params: allocs_const.as_ref().clone(),
         allocs: allocs.as_ref().clone(),
         alloc_inputs: allocs_input.as_ref().clone(),
         constraints: enforce_allocs,
@@ -528,9 +546,10 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
     let allocs_input = get_allocations(&env, "AllocationsInput");
     let allocs = get_allocations(&env, "Allocations");
     let enforce_allocs = get_enforce_allocs(&env);
+    let allocs_const = get_allocations(&env, "AllocationsConst");
 
     let circuit = LispCircuit {
-        params: vec![],
+        params:  allocs_const.as_ref().clone(),
         allocs: allocs.as_ref().clone(),
         alloc_inputs: allocs_input.as_ref().clone(),
         constraints: enforce_allocs,

+ 1 - 1
lisp/run.sh

@@ -1,2 +1,2 @@
 #export RUST_BACKTRACE=full
-cargo run --bin lisp load new-cs.lisp
+cargo run --bin lisp load jubjub-add.lisp

+ 9 - 2
lisp/types.rs

@@ -32,7 +32,7 @@ pub struct EnforceAllocation {
 
 #[derive(Debug, Clone)]
 pub struct LispCircuit {
-    pub params: Vec<Option<Scalar>>,
+    pub params: FnvHashMap<String, MalVal>,
     pub allocs: FnvHashMap<String, MalVal>,
     pub alloc_inputs: FnvHashMap<String, MalVal>,
     pub constraints: Vec<EnforceAllocation>,
@@ -76,6 +76,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
             let mut right = bellman::LinearCombination::<Scalar>::zero();
             let mut output = bellman::LinearCombination::<Scalar>::zero();
             for values in alloc_value.left.iter() {
+                println!("values {:?}", values);
                 let (a, b) = values;
                 let mut val_b = CS::one();
                 if b != "cs::one" {
@@ -85,7 +86,13 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                     left = left + (coeff, val_b);
                 } else if a == "scalar::one::neg" {
                     left = left + (coeff.neg(), val_b);
-                } 
+                } else {
+                  if let Some(value) = self.params.get(a) {
+                    if let MalVal::ZKScalar(val) = value {
+                      left = left + (*val, val_b);
+                    }
+                  }
+                }
             }
 
             for values in alloc_value.right.iter() {