Selaa lähdekoodia

changed hashmap implementation and make small improvements on allocs

plato 5 vuotta sitten
vanhempi
sitoutus
9d9c9fb09a
6 muutettua tiedostoa jossa 73 lisäystä ja 65 poistoa
  1. 1 1
      Cargo.toml
  2. 4 4
      lisp/env.rs
  3. 8 7
      lisp/examples/macro-test.lisp
  4. 0 1
      lisp/examples/util.lisp
  5. 49 40
      lisp/lisp.rs
  6. 11 12
      lisp/types.rs

+ 1 - 1
Cargo.toml

@@ -35,7 +35,7 @@ num_enum = "0.5.0"
 
 lazy_static = "1.4.0"
 itertools = "0.8.0"
-fnv = "1.0.6"
+#fnv = "1.0.6"
 regex = "1"
 
 simplelog = "0.7.4"

+ 4 - 4
lisp/env.rs

@@ -1,7 +1,7 @@
 use std::cell::RefCell;
 use std::rc::Rc;
-//use std::collections::HashMap;
-use fnv::FnvHashMap;
+use std::collections::HashMap;
+// use fnv::FnvHashMap;
 
 use crate::types::MalErr::ErrString;
 use crate::types::MalVal::{List, Nil, Sym, Vector};
@@ -9,7 +9,7 @@ use crate::types::{error, MalErr, MalRet, MalVal};
 
 #[derive(Debug)]
 pub struct EnvStruct {
-    data: RefCell<FnvHashMap<String, MalVal>>,
+    data: RefCell<HashMap<String, MalVal>>,
     pub outer: Option<Env>,
 }
 
@@ -20,7 +20,7 @@ pub type Env = Rc<EnvStruct>;
 
 pub fn env_new(outer: Option<Env>) -> Env {
     Rc::new(EnvStruct {
-        data: RefCell::new(FnvHashMap::default()),
+        data: RefCell::new(HashMap::default()),
         outer: outer,
     })
 }

+ 8 - 7
lisp/examples/macro-test.lisp

@@ -276,15 +276,16 @@
     ))
 )))
 
-(def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
-(def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
+
 (prove 
   (        
-    (mimc left right)
-    ;; (def! param3 (rnd-scalar))
-    ;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
-    ;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
-    ;; (jj-mul param-u param-v param3)
+    ;; (def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
+    ;; (def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
+    ;; (mimc left right)    
+    (def! param3 (rnd-scalar))
+    (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
+    (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
+    (jj-mul param-u param-v param3)
   )
 )
 

+ 0 - 1
lisp/examples/util.lisp

@@ -8,7 +8,6 @@
   (let* [counter (atom 0)]
     (fn* [name]
       (symbol (str name "__" (swap! counter inc))))))
-
 ;; Like load-file, but will never load the same path twice.
 
 ;; This file is normally loaded with `load-file`, so it needs a

+ 49 - 40
lisp/lisp.rs

@@ -8,10 +8,10 @@ use simplelog::*;
 
 use bellman::groth16;
 use bls12_381::Bls12;
-use fnv::FnvHashMap;
+// use fnv::FnvHashMap;
 use itertools::Itertools;
 use rand::rngs::OsRng;
-use std::fs::File;
+use std::{collections::HashMap, cell::RefCell};
 use std::rc::Rc;
 use std::time::Instant;
 use std::{
@@ -24,14 +24,14 @@ use types::EnforceAllocation;
 extern crate clap;
 #[macro_use]
 extern crate lazy_static;
-extern crate fnv;
+// extern crate fnv;
 extern crate itertools;
 extern crate regex;
 
 #[macro_use]
 mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
-use crate::types::MalVal::{Bool, Enforce, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector};
+use crate::types::MalVal::{Bool, Enforce, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector, Alloc};
 use crate::types::VerifyKeyParams;
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
 mod env;
@@ -132,7 +132,7 @@ fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
             Ok(vector!(lst))
         }
         Hash(hm, _) => {
-            let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
+            let mut new_hm: HashMap<String, MalVal> = HashMap::default();
             for (k, v) in hm.iter() {
                 new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
             }
@@ -346,56 +346,63 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         prove(a1.clone(), env.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc-const" => {
+                        let start = Instant::now();
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
                         let allocs = get_allocations(&env, "AllocationsConst");
-                        let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
-                        for (k, v) in allocs.iter() {
-                            new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
-                        }
-                        new_hm.insert(a1.pr_str(false), result.clone());      
+                        allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
+                        // let mut new_hm: HashMap<String, MalVal> = HashMap::default();                        
+                        // for (k, v) in allocs.borrow_mut().iter() {
+                        //     new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
+                        // }
+                        // new_hm.insert(a1.pr_str(false), result.clone());      
                         if let Some(e) = &env.outer {
                             env_set(
                                 &e,
                                 Sym("AllocationsConst".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         } else {
                             env_set(
                                 &env,
                                 Sym("AllocationsConst".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         }
+                        println!("Alloc Const: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc-input" => {
+                        let start = Instant::now();
                         let a1 = l[1].clone();
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
                         let allocs = get_allocations(&env, "AllocationsInput");
-                        let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
-                        for (k, v) in allocs.iter() {
-                            new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
-                        }
-                        new_hm.insert(a1.pr_str(false), result.clone());
+                        allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
+                        // let mut new_hm: HashMap<String, MalVal> = HashMap::default();
+                        // for (k, v) in allocs.borrow_mut().iter() {
+                        //     new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
+                        // }
+                        // new_hm.insert(a1.pr_str(false), result.clone());
                         if let Some(e) = &env.outer {
                             env_set(
                                 &e,
                                 Sym("AllocationsInput".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         } else {
                             env_set(
                                 &env,
                                 Sym("AllocationsInput".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         }
+                        println!("Alloc Input: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     Sym(ref a0sym) if a0sym == "alloc" => {
+                        let start = Instant::now();
                         let a1 = l[1].clone();
                         let mut value = eval(l[2].clone(), env.clone())?;
                         if let Func(_, _) = value {
@@ -403,24 +410,26 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         } 
                         let result = eval(value.clone(), env.clone())?;
                         let allocs = get_allocations(&env, "Allocations");
-                        let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
-                        for (k, v) in allocs.iter() {
-                            new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
-                        }
-                        new_hm.insert(a1.pr_str(false), result.clone());
+                        allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
+                        // let mut new_hm: HashMap<String, MalVal> = HashMap::default();
+                        // for (k, v) in allocs.borrow_mut().iter() {
+                        //     new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
+                        // }                                        
+                        // new_hm.insert(a1.pr_str(false), result.clone());
                         if let Some(e) = &env.outer {
                             env_set(
                                 &e,
                                 Sym("Allocations".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         } else {
                             env_set(
                                 &env,
                                 Sym("Allocations".to_string()),
-                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                                Alloc(allocs),
                             )?;
                         }
+                        println!("Alloc: {:?}", start.elapsed());
                         Ok(result.clone())
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
@@ -585,7 +594,7 @@ pub fn get_enforce_allocs_nested(env: &Env) -> Vec<EnforceAllocation> {
     }
 }
 
-pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
+pub fn get_allocations(env: &Env, key: &str) -> RefCell<HashMap<String, MalVal>> {
     if let Some(e) = &env.outer {
         get_allocations_nested(&e, key)
     } else {
@@ -593,12 +602,12 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
     }
 }
 
-pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
-    let alloc_hm: Rc<FnvHashMap<String, MalVal>> = Rc::new(FnvHashMap::default());
+pub fn get_allocations_nested(env: &Env, key: &str) -> RefCell<HashMap<String, MalVal>> {
+    let alloc_hm: RefCell<HashMap<String, MalVal>> = RefCell::new(HashMap::default());
     match env_find(env, key) {
         Some(e) => match env_get(&e, &Sym(key.to_string())) {
             Ok(f) => {
-                if let Hash(allocs, _) = f {
+                if let MalVal::Alloc(allocs) = f {
                     allocs
                 } else {
                     alloc_hm
@@ -613,9 +622,9 @@ pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, Mal
 pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
     let start = Instant::now();
     let c = LispCircuit {
-        params: FnvHashMap::default(),
-        allocs: FnvHashMap::default(),
-        alloc_inputs: FnvHashMap::default(),
+        params: HashMap::default(),
+        allocs: HashMap::default(),
+        alloc_inputs: HashMap::default(),
         constraints: Vec::new(),
     };
     let random_parameters =
@@ -638,9 +647,9 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
     //setup
     let params = Some({
         let circuit = LispCircuit {
-            params: allocs_const.as_ref().clone(),
-            allocs: allocs.as_ref().clone(),
-            alloc_inputs: allocs_input.as_ref().clone(),
+            params: allocs_const.borrow().clone(),
+            allocs: allocs.borrow().clone(),
+            alloc_inputs: allocs_input.borrow().clone(),
             constraints: enforce_allocs.clone(),
         };
         groth16::generate_random_parameters::<Bls12, _, _>(circuit, &mut OsRng)?
@@ -648,15 +657,15 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
     let verifying_key = Some(groth16::prepare_verifying_key(&params.as_ref().unwrap().vk));
     // prove
     let circuit = LispCircuit {
-        params: allocs_const.as_ref().clone(),
-        allocs: allocs.as_ref().clone(),
-        alloc_inputs: allocs_input.as_ref().clone(),
+        params: allocs_const.borrow().clone(),
+        allocs: allocs.borrow().clone(),
+        alloc_inputs: allocs_input.borrow().clone(),
         constraints: enforce_allocs.clone(),
     };
 
     let proof = groth16::create_random_proof(circuit, params.as_ref().unwrap(), &mut OsRng)?;
     let mut vec_input = vec![];
-    for (k, val) in allocs_input.iter() {
+    for (k, val) in allocs_input.borrow_mut().iter() {
         match val {
             MalVal::Str(v) => {
                 vec_input.push(bls12_381::Scalar::from_string(&v.to_string()));

+ 11 - 12
lisp/types.rs

@@ -1,10 +1,9 @@
 use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
 use sapvi::bls_extensions::BlsStringConversion;
-use std::cell::RefCell;
+use std::{cell::RefCell, collections::HashMap};
 use std::ops::{Add, AddAssign, MulAssign, SubAssign};
 use std::rc::Rc;
-//use std::collections::HashMap;
-use fnv::FnvHashMap;
+// use fnv::FnvHashMap;
 use itertools::Itertools;
 
 use crate::env::{env_bind, Env};
@@ -35,9 +34,9 @@ pub struct VerifyKeyParams {
 
 #[derive(Debug, Clone)]
 pub struct LispCircuit {
-    pub params: FnvHashMap<String, MalVal>,
-    pub allocs: FnvHashMap<String, MalVal>,
-    pub alloc_inputs: FnvHashMap<String, MalVal>,
+    pub params: HashMap<String, MalVal>,
+    pub allocs: HashMap<String, MalVal>,
+    pub alloc_inputs: HashMap<String, MalVal>,
     //    todo change this for a ordered data structure so enforce
     pub constraints: Vec<EnforceAllocation>,
 }
@@ -51,7 +50,7 @@ pub enum MalVal {
     Sym(String),
     List(Rc<Vec<MalVal>>, Rc<MalVal>),
     Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
-    Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
+    Hash(Rc<HashMap<String, MalVal>>, Rc<MalVal>),
     Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
     MalFunc {
         eval: fn(ast: MalVal, env: Env) -> MalRet,
@@ -62,7 +61,7 @@ pub enum MalVal {
         meta: Rc<MalVal>,
     },
     Atom(Rc<RefCell<MalVal>>),
-    Zk(Rc<LispCircuit>), // TODO remote it
+    Alloc(RefCell<HashMap<String, MalVal>>),
     Enforce(Rc<Vec<EnforceAllocation>>),
     ZKScalar(bls12_381::Scalar),
 }
@@ -72,7 +71,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         self,
         cs: &mut CS,
     ) -> Result<(), SynthesisError> {
-        let mut variables: FnvHashMap<String, Variable> = FnvHashMap::default();
+        let mut variables: HashMap<String, Variable> = HashMap::default();
         let mut params_const = self.params;
 
         // println!("Allocations\n");
@@ -401,7 +400,7 @@ pub fn func(f: fn(MalArgs) -> MalRet) -> MalVal {
     Func(f, Rc::new(Nil))
 }
 
-pub fn _assoc(mut hm: FnvHashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
+pub fn _assoc(mut hm: HashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
     if kvs.len() % 2 != 0 {
         return error("odd number of elements");
     }
@@ -416,7 +415,7 @@ pub fn _assoc(mut hm: FnvHashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
     Ok(Hash(Rc::new(hm), Rc::new(Nil)))
 }
 
-pub fn _dissoc(mut hm: FnvHashMap<String, MalVal>, ks: MalArgs) -> MalRet {
+pub fn _dissoc(mut hm: HashMap<String, MalVal>, ks: MalArgs) -> MalRet {
     for k in ks.iter() {
         match k {
             Str(ref s) => {
@@ -429,6 +428,6 @@ pub fn _dissoc(mut hm: FnvHashMap<String, MalVal>, ks: MalArgs) -> MalRet {
 }
 
 pub fn hash_map(kvs: MalArgs) -> MalRet {
-    let hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
+    let hm: HashMap<String, MalVal> = HashMap::default();
     _assoc(hm, kvs)
 }