Jelajahi Sumber

alloc as lisp hashmap

ada 5 tahun lalu
induk
melakukan
1db67b4d82
3 mengubah file dengan 54 tambahan dan 20 penghapusan
  1. 8 2
      lisp/core.rs
  2. 41 12
      lisp/lisp.rs
  3. 5 6
      lisp/types.rs

+ 8 - 2
lisp/core.rs

@@ -307,11 +307,17 @@ fn range(a: MalArgs) -> MalRet {
 }
 
 fn scalar_zero(a: MalArgs) -> MalRet {
-    Ok(vector![vec![ZKScalar(bls12_381::Scalar::zero()), a[0].clone()]])
+    Ok(vector![vec![
+        ZKScalar(bls12_381::Scalar::zero()),
+        a[0].clone()
+    ]])
 }
 
 fn scalar_one(a: MalArgs) -> MalRet {
-    Ok(vector![vec![ZKScalar(bls12_381::Scalar::one()), a[0].clone()]])
+    Ok(vector![vec![
+        ZKScalar(bls12_381::Scalar::one()),
+        a[0].clone()
+    ]])
 }
 
 fn cs_one(a: MalArgs) -> MalRet {

+ 41 - 12
lisp/lisp.rs

@@ -1,9 +1,9 @@
 #![allow(non_snake_case)]
 
-use bellman::groth16::PreparedVerifyingKey;
 use crate::groth16::VerifyingKey;
 use crate::types::LispCircuit;
 use crate::MalVal::Zk;
+use bellman::groth16::PreparedVerifyingKey;
 use sapvi::bls_extensions::BlsStringConversion;
 use sapvi::{ZKVMCircuit, ZKVirtualMachine};
 
@@ -14,9 +14,12 @@ use bls12_381::Bls12;
 use bls12_381::Scalar;
 use ff::{Field, PrimeField};
 use rand::rngs::OsRng;
-use std::{cell::RefCell, ops::{AddAssign, MulAssign, SubAssign}};
 use std::rc::Rc;
 use std::time::Instant;
+use std::{
+    cell::RefCell,
+    ops::{AddAssign, MulAssign, SubAssign},
+};
 
 //use std::collections::HashMap;
 use fnv::FnvHashMap;
@@ -35,7 +38,7 @@ extern crate regex;
 mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{Bool, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector};
-use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal, Allocation};
+use crate::types::{error, format_error, Allocation, MalArgs, MalErr, MalRet, MalVal};
 mod env;
 mod printer;
 mod reader;
@@ -315,7 +318,18 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         let value = eval(l[2].clone(), env.clone())?;
                         let result = eval(value.clone(), env.clone())?;
                         let symbol = MalVal::Sym(a1.pr_str(false));
-                        let allocs = get_allocations(&env);
+                        if let Hash(allocs, _) = get_allocations(&env)? {
+                            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);
+                            env_set(
+                                &env,
+                                Sym("Allocations".to_string()),
+                                Hash(Rc::new(new_hm), Rc::new(Nil)),
+                            );
+                        };
                         Ok(Nil)
                     }
                     //Sym(ref a0sym) if a0sym == "verify" => {
@@ -327,7 +341,10 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         let left_eval = eval(left.clone(), env.clone())?;
                         let right_eval = eval(right.clone(), env.clone())?;
                         let out_eval = eval(out.clone(), env.clone())?;
-                        println!("enforce \n {:?} \n {:?} \n {:?}", left_eval, right_eval, out_eval);
+                        println!(
+                            "enforce \n {:?} \n {:?} \n {:?}",
+                            left_eval, right_eval, out_eval
+                        );
                         Ok(vector![vec![left_eval, right_eval, out_eval]])
                     }
                     _ => match eval_ast(&ast, &env)? {
@@ -368,11 +385,24 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
 }
 
 pub fn get_allocations(env: &Env) -> MalRet {
-    let alloc_symbol = Sym("Allocations".to_string());
     let mut alloc_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
-    //alloc_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
+    match env_find(env, "Allocations") {
+        Some(e) => match env_get(&e, &Sym("Allocations".to_string())) {
+            Ok(f) => Ok(f),
+            _ => env_set(
+                &env,
+                Sym("Allocations".to_string()),
+                Hash(Rc::new(alloc_hm), Rc::new(Nil)),
+            ),
+        },
+        _ => env_set(
+            &env,
+            Sym("Allocations".to_string()),
+            Hash(Rc::new(alloc_hm), Rc::new(Nil)),
+        ),
+    }
+
     //// TODO check if there is the alloc on the env already
-    env_set(&env, alloc_symbol, Hash(Rc::new(alloc_hm), Rc::new(Nil)))
 }
 
 pub fn setup(ast: MalVal, mut env: Env) -> Result<PreparedVerifyingKey<Bls12>, MalErr> {
@@ -380,7 +410,7 @@ pub fn setup(ast: MalVal, mut env: Env) -> Result<PreparedVerifyingKey<Bls12>, M
     // Create parameters for our circuit. In a production deployment these would
     // be generated securely using a multiparty computation.
 
-    // get all allocs from env 
+    // get all allocs from env
 
     let mut c = LispCircuit {
         params: vec![],
@@ -399,13 +429,12 @@ pub fn setup(ast: MalVal, mut env: Env) -> Result<PreparedVerifyingKey<Bls12>, M
 }
 
 pub fn prove(mut ast: MalVal, mut env: Env) -> MalRet {
-   
     // TODO remove it
     let quantity = bls12_381::Scalar::from(3);
 
     // Create an instance of our circuit (with the preimage as a witness).
     let params = {
-        let  c = LispCircuit {
+        let c = LispCircuit {
             params: vec![],
             allocs: vec![],
             alloc_inputs: vec![],
@@ -415,7 +444,7 @@ pub fn prove(mut ast: MalVal, mut env: Env) -> MalRet {
         groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
     };
 
-    let  circuit= LispCircuit {
+    let circuit = LispCircuit {
         params: vec![],
         allocs: vec![],
         alloc_inputs: vec![],

+ 5 - 6
lisp/types.rs

@@ -1,8 +1,8 @@
 use bellman::groth16::PreparedVerifyingKey;
-use bls12_381::Bls12;
 use bellman::Circuit;
 use bellman::ConstraintSystem;
 use bellman::SynthesisError;
+use bls12_381::Bls12;
 use std::cell::RefCell;
 use std::rc::Rc;
 //use std::collections::HashMap;
@@ -19,7 +19,7 @@ use sapvi::{BlsStringConversion, ConstraintInstruction};
 #[derive(Debug, Clone)]
 pub struct Allocation {
     pub symbol: String,
-    pub value: Scalar
+    pub value: Scalar,
 }
 
 #[derive(Debug, Clone)]
@@ -37,10 +37,9 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         self,
         cs: &mut CS,
     ) -> Result<(), SynthesisError> {
-        
         for alloc_value in &self.allocs {
-//            let var = cs.alloc(|| "private alloc", ||)?;
-            // TODO use env  
+            //            let var = cs.alloc(|| "private alloc", ||)?;
+            // TODO use env
             println!("{:?}", alloc_value);
         }
 
@@ -70,7 +69,7 @@ pub enum MalVal {
     Atom(Rc<RefCell<MalVal>>),
     Zk(Rc<LispCircuit>), // TODO remote it
     Enforce(Rc<Vec<MalVal>>),
-    ZKScalar(bls12_381::Scalar)
+    ZKScalar(bls12_381::Scalar),
 }
 
 #[derive(Debug)]