Explorar o código

brute force merge env and now it work

plato %!s(int64=5) %!d(string=hai) anos
pai
achega
05881c9380
Modificáronse 5 ficheiros con 87 adicións e 57 borrados
  1. 6 6
      lisp/core.rs
  2. 15 17
      lisp/jubjub-mul.lisp
  3. 64 32
      lisp/lisp.rs
  4. 1 1
      lisp/run.sh
  5. 1 1
      lisp/types.rs

+ 6 - 6
lisp/core.rs

@@ -383,9 +383,7 @@ fn negate_from(a: MalArgs) -> MalRet {
 
 fn scalar_from(a: MalArgs) -> MalRet {
     match a[0].clone() {
-        ZKScalar(s0) => {
-            Ok(ZKScalar(s0))
-        }
+        ZKScalar(s0) => Ok(ZKScalar(s0)),
         Str(a0) => {
             let s0 = bls12_381::Scalar::from_string(&a0.to_string());
             Ok(ZKScalar(s0))
@@ -401,14 +399,16 @@ fn scalar_from(a: MalArgs) -> MalRet {
 fn scalar_square(a: MalArgs) -> MalRet {
     match a[0].clone() {
         ZKScalar(a0) => {
-            let mut z0 = a0.clone();
+            let  z0 = a0.clone();
             Ok(ZKScalar(z0.square()))
         }
         Str(a0) => {
-            let mut s0 = bls12_381::Scalar::from_string(&a0);
+            let  s0 = bls12_381::Scalar::from_string(&a0);
             Ok(ZKScalar(s0.square()))
         }
-        _ => error(&format!("scalar square expect (zkscalar or string) found \n {:?}", a).to_string()),
+        _ => error(
+            &format!("scalar square expect (zkscalar or string) found \n {:?}", a).to_string(),
+        ),
     }
 }
 

+ 15 - 17
lisp/jubjub-mul.lisp

@@ -5,32 +5,31 @@
 (def! param2 (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
 (def! param1 (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
 
-(setup
-    (prove 
-      (
+;; (setup
+    (prove (      
     (def! zk-square (fn* [var] (
-            (def! result (alloc "square-var" (square var)))
+            (def! value (alloc "value" var))
+            (def! result (alloc "result" (square var)))
             (enforce  
-                (scalar::one square-var) 
-                (scalar::one square-var)
+                (scalar::one value) 
+                (scalar::one value)
                 (scalar::one result)
             )
         )
     ))
 
-     (def! u1 (alloc "u1" param1))
-     (def! v1 (alloc "v1" param2))
-     (def! u2 (alloc "u2" param3))
-     (def! v2 (alloc "v2" param4))
-     (def! EDWARDS_D (alloc-const "EDWARDS_D" (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
-     (def! U (alloc "U" (* (+ u1 v1) (+ u2 v2))))
+    (def! u1 (alloc "u1" param1))
+    (def! v1 (alloc "v1" param2))
+    (def! u2 (alloc "u2" param3))
+    (def! v2 (alloc "v2" param4))
+    (def! U (alloc "U" (* (+ u1 v1) (+ u2 v2))))
      (def! A (alloc "A" (* v2 u1)))
      (def! B (alloc "B" (* u2 v1)))
+     (def! EDWARDS_D (alloc-const "EDWARDS_D" (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
      (def! C (alloc "C" (* EDWARDS_D (* A B))))
      (def! u3 (alloc-input "u3" (/ (+ A B) (+ scalar::one C))))
      (def! v3 (alloc-input "v3" (/ (- (- U A) B) (- scalar::one C))))
-     (println 'square (zk-square param1))
-  (
+    (zk-square param1)
   (enforce  
     ((scalar::one u1) (scalar::one v1))
     ((scalar::one u2) (scalar::one v2))
@@ -50,8 +49,7 @@
     ((scalar::one cs::one) (scalar::one::neg C))
     (scalar::one v3)
     ((scalar::one U) (scalar::one::neg A) (scalar::one::neg B))
-  )
-  )
- )
+  )  
 )
 )
+;; )

+ 64 - 32
lisp/lisp.rs

@@ -11,7 +11,7 @@ use bls12_381::Bls12;
 use fnv::FnvHashMap;
 use itertools::Itertools;
 use rand::rngs::OsRng;
-use std::fs;
+use std::{borrow::{Borrow, BorrowMut}, fs};
 use std::fs::File;
 use std::rc::Rc;
 use std::time::Instant;
@@ -366,7 +366,6 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc" => {
-                        println!("{:?}", l);
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
@@ -376,11 +375,20 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                             new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
                         }
                         new_hm.insert(a1.pr_str(false), result.clone());
-                        env_set(
-                            &env,
-                            Sym("Allocations".to_string()),
-                            Hash(Rc::new(new_hm), Rc::new(Nil)),
-                        )?;
+                        // TODO change it 
+                        if let Some(e) = &env.outer {
+                            env_set(
+                                &e,
+                                Sym("Allocations".to_string()),
+                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                            )?;
+                        } else {
+                            env_set(
+                                &env,
+                                Sym("Allocations".to_string()),
+                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                            )?;
+                        }
                         Ok(result.clone())
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
@@ -456,20 +464,22 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         for value in enforce_vec.iter() {
                             new_vec.push(value.clone());
                         }
-                        env_set(
-                            &env,
-                            Sym("AllocationsEnforce".to_string()),
-                            vector![vec![Enforce(Rc::new(new_vec))]],
-                        );
-                        /*
-                                                println!("\n\nallocations {:?}", get_allocations(&env, "Allocations"));
-                                                println!(
-                                                    "\n\nallocations input {:?}",
-                                                    get_allocations(&env, "AllocationsInput")
-                                                );
-                                                println!("\n\nallocations enforce {:?}", get_enforce_allocs(&env));
-                        */
-                        Ok(vector![vec![]])
+                        // TODO change it 
+                        if let Some(e) = &env.outer {
+                            env_set(
+                                &e,
+                                Sym("AllocationsEnforce".to_string()),
+                                vector![vec![Enforce(Rc::new(new_vec.clone()))]],
+                            )?;                     
+                        } else {
+                            env_set(
+                                &env,
+                                Sym("AllocationsEnforce".to_string()),
+                                vector![vec![Enforce(Rc::new(new_vec.clone()))]],
+                            )?;                     
+                        }
+                        
+                        Ok(MalVal::Nil)
                     }
                     _ => match eval_ast(&ast, &env)? {
                         List(ref el, _) => {
@@ -509,8 +519,16 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
     ret
 }
 
-pub fn get_enforce_allocs(env: &Env) -> Vec<EnforceAllocation> {
-    // todo need some cleanup
+pub fn get_enforce_allocs(env: &Env) -> Vec<EnforceAllocation> {    
+    if let Some(e) = &env.outer {
+        get_enforce_allocs_nested(&e)
+    } else {
+        get_enforce_allocs_nested(&env)
+    }
+}
+
+pub fn get_enforce_allocs_nested(env: &Env) -> Vec<EnforceAllocation> {
+    
     match env_find(env, "AllocationsEnforce") {
         Some(e) => match env_get(&e, &Sym("AllocationsEnforce".to_string())) {
             Ok(f) => {
@@ -529,12 +547,25 @@ pub fn get_enforce_allocs(env: &Env) -> Vec<EnforceAllocation> {
         _ => vec![],
     }
 }
+
 pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
+    if let Some(e) = &env.outer {
+        get_allocations_nested(&e, key)
+    } else {
+        get_allocations_nested(&env, key)
+    }
+}
+
+pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
     let alloc_hm: Rc<FnvHashMap<String, MalVal>> = Rc::new(FnvHashMap::default());
+    
+    if let Some(e) = &env.outer {
+    }
+
     match env_find(env, key) {
         Some(e) => match env_get(&e, &Sym(key.to_string())) {
             Ok(f) => {
-                if let Hash(allocs, _) = f {
+                if let Hash(allocs, _) = f {                  
                     allocs
                 } else {
                     alloc_hm
@@ -546,6 +577,7 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
     }
 }
 
+
 pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
     let start = Instant::now();
     let c = LispCircuit {
@@ -566,7 +598,7 @@ pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
 }
 
 pub fn prove(_ast: MalVal, env: Env) -> MalRet {
-    let start = Instant::now();
+    // let start = Instant::now();
     let allocs_input = get_allocations(&env, "AllocationsInput");
     let allocs = get_allocations(&env, "Allocations");
     let enforce_allocs = get_enforce_allocs(&env);
@@ -640,13 +672,13 @@ fn main() -> Result<(), ()> {
     )
     .get_matches();
 
-    CombinedLogger::init(vec![TermLogger::new(
-        LevelFilter::Debug,
-        Config::default(),
-        TerminalMode::Mixed,
-    )
-    .unwrap()])
-    .unwrap();
+    // CombinedLogger::init(vec![TermLogger::new(
+    //     LevelFilter::Debug,
+    //     Config::default(),
+    //     TerminalMode::Mixed,
+    // )
+    // .unwrap()])
+    // .unwrap();
 
     match matches.subcommand() {
         Some(("load", matches)) => {

+ 1 - 1
lisp/run.sh

@@ -1,3 +1,3 @@
-#export RUST_BACKTRACE=full
+export RUST_BACKTRACE=full
 cargo run --bin lisp load jubjub-mul.lisp
 #cargo run --bin lisp load new-cs.lisp

+ 1 - 1
lisp/types.rs

@@ -142,7 +142,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                         }
                     }
                 }
-//                println!("left: a {:?} b {:?} val_b: {:?}", a, b, val_b);
+                //                println!("left: a {:?} b {:?} val_b: {:?}", a, b, val_b);
             }
 
             for values in alloc_value.right.iter() {