Jelajahi Sumber

syntethize circuit

ada 5 tahun lalu
induk
melakukan
4a8b4b4b95
2 mengubah file dengan 43 tambahan dan 5 penghapusan
  1. 1 0
      lisp/run.sh
  2. 42 5
      lisp/types.rs

+ 1 - 0
lisp/run.sh

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

+ 42 - 5
lisp/types.rs

@@ -10,6 +10,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 bellman::Variable;
 use bls12_381::Scalar;
 
 #[derive(Debug, Clone)]
@@ -37,21 +38,57 @@ pub struct LispCircuit {
 impl Circuit<bls12_381::Scalar> for LispCircuit {
     fn synthesize<CS: ConstraintSystem<bls12_381::Scalar>>(
         self,
-        _cs: &mut CS,
+        cs: &mut CS,
     ) -> Result<(), SynthesisError> {
+        let mut variables: FnvHashMap<String, Variable> = FnvHashMap::default();
+
         println!("Allocations\n");
-        for alloc_value in &self.allocs {
-            println!("{:?}", alloc_value);
+        for (k, v) in &self.allocs {
+            if let MalVal::ZKScalar(val) = v {
+                println!("val {:?}", val);
+                let var = cs.alloc(|| "alloc", || Ok(*val))?;
+                variables.insert(k.to_string(), var);
+            } else {
+                println!("k {:?} v {:?}", k, v);
+            }
         }
 
         println!("Allocations Input\n");
-        for alloc_value in &self.alloc_inputs {
-            println!("{:?}", alloc_value);
+        for (k, v) in &self.alloc_inputs {
+            if let MalVal::ZKScalar(val) = v {
+                println!("val {:?}", val);
+                let var = cs.alloc(|| "alloc", || Ok(*val))?;
+                variables.insert(k.to_string(), var);
+            } else {
+                println!("k {:?} v {:?}", k, v);
+            }
         }
 
         println!("Enforce Allocations\n");
         for alloc_value in &self.constraints {
             println!("{:?}", alloc_value);
+            let coeff = bls12_381::Scalar::one();
+            let left = bellman::LinearCombination::<Scalar>::zero();
+            let right = bellman::LinearCombination::<Scalar>::zero();
+            let output = bellman::LinearCombination::<Scalar>::zero();
+            for values in alloc_value.left.iter() {
+                let (a, b) = values;
+                println!("{:?} {:?}", a, variables.get(a));
+                println!("{:?} {:?}", b, variables.get(b));
+                let val_a = variables.get(a).unwrap();
+                let val_b = variables.get(b).unwrap();
+                if a == "scalar::one" {
+                //    val_a = coeff;
+                } 
+                left = left + (val_a, val_b);
+            }
+
+            cs.enforce(
+                || "constraint",
+                |_| left.clone(),
+                |_| right.clone(),
+                |_| output.clone(),
+            );
         }
 
         Ok(())