Преглед изворни кода

added operations on scalars outside of the constrains system

ada пре 5 година
родитељ
комит
2914446394
4 измењених фајлова са 49 додато и 32 уклоњено
  1. 25 2
      lisp/core.rs
  2. 19 27
      lisp/jubjub.lisp
  3. 4 2
      lisp/lisp.rs
  4. 1 1
      lisp/types.rs

+ 25 - 2
lisp/core.rs

@@ -256,6 +256,22 @@ fn sub(a: MalArgs) -> MalRet {
     Ok(Sub(Rc::new(a[0].clone()), Rc::new(a[1].clone())))
 }
 
+fn add_scalar(a: MalArgs) -> MalRet {
+    println!("{:?}", a);
+    match (a[0].clone(), a[1].clone()) {
+        (Sym(a0), Sym(a1)) => {
+            println!("{:?}", a0);
+            //let (mut s0, mut s1) = (Scalar::from_string(&a0), Scalar::from_string(&a1));
+            //let result = s0.add_assign(&s1);
+            //println!("{:?}", result);
+            Ok(Str(std::string::ToString::to_string(&Scalar::one())[2..].to_string()))
+        }
+        _ => error("expected (scalar, scalar"),
+    };
+
+    Ok(Str(std::string::ToString::to_string(&Scalar::one())[2..].to_string()))
+}
+
 fn add(a: MalArgs) -> MalRet {
     // get next symbol should be lc0 lc1 lc2
     Ok(Add(Rc::new(a[0].clone()), Rc::new(a[1].clone())))
@@ -323,11 +339,16 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("<=", func(fn_t_int_int!(Bool, |i, j| { i <= j }))),
         (">", func(fn_t_int_int!(Bool, |i, j| { i > j }))),
         (">=", func(fn_t_int_int!(Bool, |i, j| { i >= j }))),
-        ("+", func(fn_t_int_int!(Int, |i, j| { i + j }))),
+        ("+", func(add_scalar)),
         ("-", func(fn_t_int_int!(Int, |i, j| { i - j }))),
-        ("*", func(fn_t_int_int!(Int, |i, j| { i * j }))),
+        //        ("*", func(mul_scalar)),
         ("/", func(fn_t_int_int!(Int, |i, j| { i / j }))),
         ("time-ms", func(time_ms)),
+        ("i+", func(fn_t_int_int!(Int, |i, j| { i + j }))),
+        ("i-", func(fn_t_int_int!(Int, |i, j| { i - j }))),
+        ("i*", func(fn_t_int_int!(Int, |i, j| { i * j }))),
+        ("i/", func(fn_t_int_int!(Int, |i, j| { i / j }))),
+        ("time-ms", func(time_ms)),
         ("sequential?", func(fn_is_type!(List(_, _), Vector(_, _)))),
         ("list", func(|a| Ok(list!(a)))),
         ("list?", func(fn_is_type!(List(_, _)))),
@@ -365,5 +386,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("sub", func(sub)),
         ("lc0", func(|a| Ok(MalVal::Lc0))),
         ("lc1", func(|a| Ok(MalVal::Lc1))),
+        ("lc2", func(|a| Ok(MalVal::Lc2))),
+        ("enforce", func(|a| Ok(MalVal::Enforce))),
     ]
 }

+ 19 - 27
lisp/jubjub.lisp

@@ -1,27 +1,19 @@
-(def! D "0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")
-(def! ONE "0x0000000000000000000000000000000000000000000000000000000000000001")
-(def! jj-add (fn* [x1 y1 x2 y2] (
-    (def! U (mul (add (x1 y1)) (add (x2 y2))))
-    (enforce (add-lc0 (x1 y1)) (add-lc1 (x2 y2)) (add-lc2 U))
-    (def! A (mul (x2 x1)))
-    (def! B (mul (x2 y1)))
-    (def! C (mul (ONE A)))
-    (enforce (add-coeff-lc0 (D A)) (add-lc1 B) (add-lc2 C))
-    (def! Px (div (add A B) (add C_ONE C)))
-    (enforce (add-one-lc0) (sub-lc0 C) (add-lc1 Px) (add-lc2 (A B)))
-
-    (def! Py (div (sub U A B) (sub C_ONE C))) 
-    (enforce (add-one-lc0) (sub-lc0 C) (add-lc1 Py) (add-lc2 (U A B)))
-    (Px Py)
-)))
-;;    (def input_spend (contract x1 y1 x2 y2) (
-;;        (def P (jjadd (x1 y1 x2 y2)))
-;;        (enforce 
-;;            (add_lc0 Px)
-;;            (add_lc1_one)
-;;            (add_lc2 Px))
-;;        (enforce 
-;;            (add_lc0 Py)
-;;            (add_lc1_one)
-;;            (add_lc2 Py))
-;;    )
+(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 ())
+(def! U (fn* [x1 y1 x2 y2] (+ (+ x1 y1) (+ x2 y2))))
+(def! jubjub-add (fn* [x1 y1 x2 y2] (zkcons! circuit (
+                    (add lc0 x1)
+                    (add lc0 y1)
+                    (add lc1 x2)
+                    (add lc1 y2)
+                    (add lc2 (U x1 y1 x2 y2))))
+                    enforce
+                    ))
+(println (jubjub-add a_u a_v b_u b_v))
+(println circuit)

+ 4 - 2
lisp/lisp.rs

@@ -282,7 +282,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                             }
                             _ => println!("invalid format"),
                         }
-                        println!("3 {:?}", eval(a1.clone(), env.clone()));
+//                        println!("3 {:?}", eval(a1.clone(), env.clone()));
                         env_set(&env, a1.clone(), eval(a1.clone(), env.clone())?); 
                         eval(a1.clone(), env.clone())
                     }
@@ -368,7 +368,6 @@ fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
                     for b in elements.iter() {
                         match b {
                             Add(b1, b2) => {
-                                // ONE is a const if it's capital case or *VARIABLE NAME*
                                 zk.private
                                     .push(Scalar::from_string(&b2.pr_str(false).to_string()));
                                 let const_a: ConstraintInstruction = match b1.apply(vec![])? {
@@ -397,6 +396,9 @@ fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
                                 };
                                 zk.constraints.push(const_a);
                                 //env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
+                            },
+                            Enforce => {
+                                zk.constraints.push(ConstraintInstruction::Enforce);
                             }
                             val => println!("not mapped"),
                         }

+ 1 - 1
lisp/types.rs

@@ -47,7 +47,7 @@ pub enum MalVal {
     Lc0,
     Lc1,
     Lc2,
-    Enforce(bool),
+    Enforce,
     Add(Rc<MalVal>, Rc<MalVal>),
     Sub(Rc<MalVal>, Rc<MalVal>),
 }