Explorar el Código

added public and private symbols

ada hace 5 años
padre
commit
61d5caa090
Se han modificado 6 ficheros con 48 adiciones y 15 borrados
  1. 5 5
      lisp/TODO.md
  2. 12 1
      lisp/core.rs
  3. 17 2
      lisp/jubjub.lisp
  4. 10 5
      lisp/lisp.rs
  5. 1 1
      lisp/new.lisp
  6. 3 1
      lisp/types.rs

+ 5 - 5
lisp/TODO.md

@@ -1,7 +1,7 @@
 ## TODO
 
-[] - Document the language
-[] - Integrate with zkvm command line
-[x] - Integrate with ZKVMCircuit: allocs and constraints
-[x] - Added CryptoOperation such double and square to core.rs
-[] - Adapt ZKContract to use lisp to read contract and execute
+- Document the language
+- Integrate with zkvm command line
+- Integrate with ZKVMCircuit: allocs and constraints
+- Added CryptoOperation such double and square to core.rs
+- Adapt ZKContract to use lisp to read contract and execute

+ 12 - 1
lisp/core.rs

@@ -8,7 +8,7 @@ use crate::printer::pr_seq;
 use crate::reader::read_str;
 use crate::types::MalErr::ErrMalVal;
 use crate::types::MalVal::{
-    Add, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Str, Sub, Sym, Vector,
+    Add, Atom, Bool, Func, Hash, Int, Lc0, List, MalFunc, Nil, Str, Sub, Sym, Vector, Public, Private
 };
 use crate::types::{MalArgs, MalRet, MalVal, _assoc, _dissoc, atom, error, func, hash_map};
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
@@ -288,6 +288,15 @@ fn div_scalar(a: MalArgs) -> MalRet {
         _ => error("expected (scalar, scalar"),
     }
 }
+
+fn cs_public(a: MalArgs) -> MalRet {
+    Ok(Public(Rc::new(a[0].clone()).clone()))
+}
+
+fn cs_private(a: MalArgs) -> MalRet {
+    Ok(Private(Rc::new(a[0].clone()).clone()))
+}
+
 fn add_scalar(a: MalArgs) -> MalRet {
     match (a[0].clone(), a[1].clone()) {
         (Str(a0), Str(a1)) => {
@@ -419,5 +428,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("lc1", func(|a| Ok(MalVal::Lc1))),
         ("lc2", func(|a| Ok(MalVal::Lc2))),
         ("enforce", func(|a| Ok(MalVal::Enforce))),
+        ("public", func(cs_public)),
+        ("private", func(cs_private))
     ]
 }

+ 17 - 2
lisp/jubjub.lisp

@@ -12,7 +12,8 @@
 (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)))))
-(def! jubjub-add (fn* [x1 y1 x2 y2] (zkcons! circuit (
+;; *cs* is the symbol that create a zkvm with a constraint system
+(def! jubjub-add (fn* [x1 y1 x2 y2] (cs! circuit (
                     (add lc0 x1)
                     (add lc0 y1)
                     (add lc1 x2)
@@ -35,4 +36,18 @@
                     (sub lc2 (B y1 x2))
                     enforce 
                     ))))
-(println (jubjub-add a_u a_v b_u b_v))
+(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 lc1 one)
+                    (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 lc1 one)
+                    (add lc2 (P.y a_u a_v b_u b_v))
+                    enforce
+                  )))
+(println circuit)

+ 10 - 5
lisp/lisp.rs

@@ -26,7 +26,8 @@ extern crate regex;
 mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{
-    Add, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Str, Sub, Sym, Vector, Zk,
+    Add, Bool, Func, Hash, Lc0, Lc1, Lc2, List, MalFunc, Nil, Private, Public, Str, Sub, Sym,
+    Vector, Zk,
 };
 use crate::types::ZKCircuit;
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
@@ -272,7 +273,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                             _ => Ok(Nil),
                         }
                     }
-                    Sym(ref a0sym) if a0sym == "zkcons!" => {
+                    Sym(ref a0sym) if a0sym == "cs!" => {
                         let (a1, a2) = (l[1].clone(), l[2].clone());
                         let value = eval_ast(&a2, &env)?;
                         match value {
@@ -282,8 +283,8 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                             }
                             _ => println!("invalid format"),
                         }
-//                        println!("3 {:?}", eval(a1.clone(), env.clone()));
-                        env_set(&env, a1.clone(), 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())
                     }
                     Sym(ref a0sym) if a0sym == "defzk!" => {
@@ -396,7 +397,11 @@ 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()));
-                            },
+                            }
+                            Public(a) => {
+                                zk.public
+                                    .push(Scalar::from_string(&a.pr_str(false).to_string()));
+                            }
                             Enforce => {
                                 zk.constraints.push(ConstraintInstruction::Enforce);
                             }

+ 1 - 1
lisp/new.lisp

@@ -14,4 +14,4 @@
                  (add lc1 one)
                  'enforce)))
 (println "bit-dec")
-(zkcons! circuit cs)
+(cs! circuit cs)

+ 3 - 1
lisp/types.rs

@@ -6,7 +6,7 @@ use itertools::Itertools;
 
 use crate::env::{env_bind, Env};
 use crate::types::MalErr::{ErrMalVal, ErrString};
-use crate::types::MalVal::{Atom, Bool, Func, Hash, Int, List, MalFunc, Nil, Str, Sym, Vector};
+use crate::types::MalVal::{Atom, Bool, Func, Hash, Int, List, MalFunc, Nil, Str, Sym, Vector, Public, Private };
 
 use bls12_381::Scalar;
 use sapvi::{
@@ -50,6 +50,8 @@ pub enum MalVal {
     Enforce,
     Add(Rc<MalVal>, Rc<MalVal>),
     Sub(Rc<MalVal>, Rc<MalVal>),
+    Public(Rc<MalVal>),
+    Private(Rc<MalVal>),
 }
 
 #[derive(Debug, Clone)]