فهرست منبع

improvement on zklisp

ada 5 سال پیش
والد
کامیت
2aa8e13e8e
3فایلهای تغییر یافته به همراه32 افزوده شده و 4 حذف شده
  1. 19 3
      lisp/core.rs
  2. 12 1
      lisp/jubjub.lisp
  3. 1 0
      lisp/lisp.rs

+ 19 - 3
lisp/core.rs

@@ -8,8 +8,8 @@ use crate::printer::pr_seq;
 use crate::reader::read_str;
 use crate::types::MalErr::ErrMalVal;
 use crate::types::MalVal::{
-    Add, AddOne, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Params, Private, Public,
-    Str, Sub, Sym, Vector,
+    Add, AddOne, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Private, Public, Str, Sub,
+    Sym, Vector, Params
 };
 use crate::types::{MalArgs, MalRet, MalVal, _assoc, _dissoc, atom, error, func, hash_map};
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
@@ -288,7 +288,7 @@ fn div_scalar(a: MalArgs) -> MalRet {
                 std::string::ToString::to_string(&ret.unwrap())[2..].to_string()
             ))
         }
-        _ => error("expected (scalar, scalar"),
+        _ => error("expected (scalar, scalar)"),
     }
 }
 
@@ -303,6 +303,21 @@ fn cs_public(a: MalArgs) -> MalRet {
 fn cs_private(a: MalArgs) -> MalRet {
     Ok(Private(Rc::new(a[0].clone()).clone()))
 }
+fn range(a: MalArgs) -> MalRet {
+    let mut result = vec![];
+    match (a[0].clone(), a[1].clone()) {
+        (Int(a0), Int(a1)) => {
+            for n in a0..a1 {
+               result.push(n);
+            };
+            Ok(list!(result
+                .iter()
+                .map(|a| Nil) 
+                .collect::<Vec<MalVal>>()))
+        },
+        _ => error("expected int int")
+    }
+}
 
 fn add_scalar(a: MalArgs) -> MalRet {
     match (a[0].clone(), a[1].clone()) {
@@ -443,5 +458,6 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("public", func(cs_public)),
         ("private", func(cs_private)),
         ("params", func(cs_params)),
+        ("range", func(range)),
     ]
 }

+ 12 - 1
lisp/jubjub.lisp

@@ -7,12 +7,23 @@
 (def! d "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")
 (def! one "0000000000000000000000000000000000000000000000000000000000000001")
 (defzk! circuit ())
-(def! U (fn* [x1 y1 x2 y2] (+ (+ x1 y1) (+ x2 y2))))
+;; 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)

+ 1 - 0
lisp/lisp.rs

@@ -421,6 +421,7 @@ fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
                                     .push(Scalar::from_string(&a.pr_str(false).to_string()));
                             }
                             Params(a) => {
+                                // todo add multiple matchs
                                 match a.as_ref() {
                                     Vector(v, _) => {
                                         for i in v.iter() {