Просмотр исходного кода

added constraint system one on dsl

ada 5 лет назад
Родитель
Сommit
679a072cbd
4 измененных файлов с 28 добавлено и 6 удалено
  1. 6 1
      lisp/core.rs
  2. 4 4
      lisp/jubjub.lisp
  3. 17 1
      lisp/lisp.rs
  4. 1 0
      lisp/types.rs

+ 6 - 1
lisp/core.rs

@@ -8,7 +8,7 @@ use crate::printer::pr_seq;
 use crate::reader::read_str;
 use crate::reader::read_str;
 use crate::types::MalErr::ErrMalVal;
 use crate::types::MalErr::ErrMalVal;
 use crate::types::MalVal::{
 use crate::types::MalVal::{
-    Add, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Str, Sub, Sym, Vector, Public, Private
+    Add, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Str, Sub, Sym, Vector, Public, Private, AddOne
 };
 };
 use crate::types::{MalArgs, MalRet, MalVal, _assoc, _dissoc, atom, error, func, hash_map};
 use crate::types::{MalArgs, MalRet, MalVal, _assoc, _dissoc, atom, error, func, hash_map};
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
@@ -308,6 +308,10 @@ fn add_scalar(a: MalArgs) -> MalRet {
     }
     }
 }
 }
 
 
+fn add_one(a: MalArgs) -> MalRet {
+    Ok(AddOne(Rc::new(a[0].clone())))
+}
+
 fn add(a: MalArgs) -> MalRet {
 fn add(a: MalArgs) -> MalRet {
     // get next symbol should be lc0 lc1 lc2
     // get next symbol should be lc0 lc1 lc2
     Ok(Add(Rc::new(a[0].clone()), Rc::new(a[1].clone())))
     Ok(Add(Rc::new(a[0].clone()), Rc::new(a[1].clone())))
@@ -423,6 +427,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("swap!", func(|a| a[0].swap_bang(&a[1..].to_vec()))),
         ("swap!", func(|a| a[0].swap_bang(&a[1..].to_vec()))),
         ("unpack-bits", func(unpack_bits)),
         ("unpack-bits", func(unpack_bits)),
         ("add", func(add)),
         ("add", func(add)),
+        ("add-one", func(add_one)),
         ("sub", func(sub)),
         ("sub", func(sub)),
         ("lc0", func(|a| Ok(MalVal::Lc0))),
         ("lc0", func(|a| Ok(MalVal::Lc0))),
         ("lc1", func(|a| Ok(MalVal::Lc1))),
         ("lc1", func(|a| Ok(MalVal::Lc1))),

+ 4 - 4
lisp/jubjub.lisp

@@ -21,14 +21,14 @@
                     (add lc2 (U x1 y1 x2 y2))
                     (add lc2 (U x1 y1 x2 y2))
                     enforce
                     enforce
 ;; Compute P.x = (A + B) / (1 + C)
 ;; Compute P.x = (A + B) / (1 + C)
-                    (add lc0 one)
+                    (add-one lc0)
                     (add lc0 (C x1 y1 x2 y2))
                     (add lc0 (C x1 y1 x2 y2))
                     (add lc1 (P.x x1 y1 x2 y2))
                     (add lc1 (P.x x1 y1 x2 y2))
                     (add lc1 (A x1 y2))
                     (add lc1 (A x1 y2))
                     (add lc1 (B y1 x2))
                     (add lc1 (B y1 x2))
                     enforce
                     enforce
 ;; Compute P.y = (U - A - B) / (1 - C)                    
 ;; Compute P.y = (U - A - B) / (1 - C)                    
-                    (add lc0 one)
+                    (add-one lc0)
                     (sub lc0 (C x1 y1 x2 y2))
                     (sub lc0 (C x1 y1 x2 y2))
                     (add lc1 (P.y x1 y1 x2 y2))
                     (add lc1 (P.y x1 y1 x2 y2))
                     (add lc2 (U x1 y1 x2 y2))
                     (add lc2 (U x1 y1 x2 y2))
@@ -42,11 +42,11 @@
                     (public (P.x a_u a_v b_u b_v)) 
                     (public (P.x a_u a_v b_u b_v)) 
                     (public (P.y 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 lc0 (P.x a_u a_v b_u b_v))
-                    (add lc1 one)
+                    (add-one lc1)
                     (add lc2 (P.x a_u a_v b_u b_v))
                     (add lc2 (P.x a_u a_v b_u b_v))
                     enforce
                     enforce
                     (add lc0 (P.y a_u a_v b_u b_v))
                     (add lc0 (P.y a_u a_v b_u b_v))
-                    (add lc1 one)
+                    (add-one lc1)
                     (add lc2 (P.y a_u a_v b_u b_v))
                     (add lc2 (P.y a_u a_v b_u b_v))
                     enforce
                     enforce
                   )))
                   )))

+ 17 - 1
lisp/lisp.rs

@@ -27,7 +27,7 @@ mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{
 use crate::types::MalVal::{
     Add, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Private, Public, Str, Sub, Sym,
     Add, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Private, Public, Str, Sub, Sym,
-    Vector, Zk,
+    Vector, Zk, AddOne
 };
 };
 use crate::types::ZKCircuit;
 use crate::types::ZKCircuit;
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
@@ -398,6 +398,22 @@ fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
                                 zk.constraints.push(const_a);
                                 zk.constraints.push(const_a);
                                 //env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
                                 //env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
                             }
                             }
+                            AddOne(b1) => {
+                                let const_a: ConstraintInstruction = match b1.apply(vec![])? {
+                                    Lc0 => ConstraintInstruction::Lc0AddOne,
+                                    Lc1 => ConstraintInstruction::Lc1AddOne,
+                                    Lc2 => ConstraintInstruction::Lc2AddOne,
+                                    _ => {
+                                        println!("{:?}", b1.apply(vec![]));
+                                        ConstraintInstruction::Lc0AddOne
+                                    }
+                                };
+                                zk.constraints.push(const_a);
+                            }
+                            Private(a) => {
+                                zk.private
+                                    .push(Scalar::from_string(&a.pr_str(false).to_string()));
+                            }
                             Public(a) => {
                             Public(a) => {
                                 zk.public
                                 zk.public
                                     .push(Scalar::from_string(&a.pr_str(false).to_string()));
                                     .push(Scalar::from_string(&a.pr_str(false).to_string()));

+ 1 - 0
lisp/types.rs

@@ -49,6 +49,7 @@ pub enum MalVal {
     Lc2,
     Lc2,
     Enforce,
     Enforce,
     Add(Rc<MalVal>, Rc<MalVal>),
     Add(Rc<MalVal>, Rc<MalVal>),
+    AddOne(Rc<MalVal>),
     Sub(Rc<MalVal>, Rc<MalVal>),
     Sub(Rc<MalVal>, Rc<MalVal>),
     Public(Rc<MalVal>),
     Public(Rc<MalVal>),
     Private(Rc<MalVal>),
     Private(Rc<MalVal>),