Ver código fonte

major fix on scalar add and alloc returns

ada 5 anos atrás
pai
commit
00ec659416
5 arquivos alterados com 14 adições e 77 exclusões
  1. 8 2
      lisp/core.rs
  2. 0 70
      lisp/jubjub.lisp
  3. 4 4
      lisp/lisp.rs
  4. 1 1
      lisp/run.sh
  5. 1 0
      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()),
     }
 }
 

+ 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)

+ 4 - 4
lisp/lisp.rs

@@ -311,13 +311,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 +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("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" => {

+ 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

+ 1 - 0
lisp/types.rs

@@ -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" {