|
|
@@ -11,6 +11,7 @@ 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::Bls12;
|
|
|
use bls12_381::Scalar;
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
@@ -26,12 +27,42 @@ pub struct EnforceAllocation {
|
|
|
pub output: Vec<(String, String)>,
|
|
|
}
|
|
|
|
|
|
+pub struct VerifyKeyParams {
|
|
|
+ pub random_params: groth16::Parameters<Bls12>,
|
|
|
+ pub verifying_key: groth16::PreparedVerifyingKey<Bls12>,
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Debug, Clone)]
|
|
|
pub struct LispCircuit {
|
|
|
- pub params: Option<FnvHashMap<String, MalVal>>,
|
|
|
- pub allocs: Option<FnvHashMap<String, MalVal>>,
|
|
|
- pub alloc_inputs: Option<FnvHashMap<String, MalVal>>,
|
|
|
- pub constraints: Option<Vec<EnforceAllocation>>,
|
|
|
+ pub params: FnvHashMap<String, MalVal>,
|
|
|
+ pub allocs: FnvHashMap<String, MalVal>,
|
|
|
+ pub alloc_inputs: FnvHashMap<String, MalVal>,
|
|
|
+ pub constraints: Vec<EnforceAllocation>,
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Debug, Clone)]
|
|
|
+pub enum MalVal {
|
|
|
+ Nil,
|
|
|
+ Bool(bool),
|
|
|
+ Int(i64),
|
|
|
+ Str(String),
|
|
|
+ Sym(String),
|
|
|
+ List(Rc<Vec<MalVal>>, Rc<MalVal>),
|
|
|
+ Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
|
|
|
+ Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
|
|
|
+ Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
|
|
|
+ MalFunc {
|
|
|
+ eval: fn(ast: MalVal, env: Env) -> MalRet,
|
|
|
+ ast: Rc<MalVal>,
|
|
|
+ env: Env,
|
|
|
+ params: Rc<MalVal>,
|
|
|
+ is_macro: bool,
|
|
|
+ meta: Rc<MalVal>,
|
|
|
+ },
|
|
|
+ Atom(Rc<RefCell<MalVal>>),
|
|
|
+ Zk(Rc<LispCircuit>), // TODO remote it
|
|
|
+ Enforce(Rc<Vec<EnforceAllocation>>),
|
|
|
+ ZKScalar(bls12_381::Scalar),
|
|
|
}
|
|
|
|
|
|
impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
@@ -40,10 +71,10 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
cs: &mut CS,
|
|
|
) -> Result<(), SynthesisError> {
|
|
|
let mut variables: FnvHashMap<String, Variable> = FnvHashMap::default();
|
|
|
- let mut params_const = self.params.unwrap_or(FnvHashMap::default());
|
|
|
+ let mut params_const = self.params;
|
|
|
|
|
|
println!("Allocations\n");
|
|
|
- for (k, v) in &self.allocs.unwrap_or(FnvHashMap::default()) {
|
|
|
+ for (k, v) in &self.allocs {
|
|
|
println!("k {:?} v {:?}", k, v);
|
|
|
match v {
|
|
|
MalVal::ZKScalar(val) => {
|
|
|
@@ -62,7 +93,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
}
|
|
|
|
|
|
println!("Allocations Input\n");
|
|
|
- for (k, v) in &self.alloc_inputs.unwrap_or(FnvHashMap::default()) {
|
|
|
+ for (k, v) in &self.alloc_inputs {
|
|
|
println!("k {:?} v {:?}", k, v);
|
|
|
match v {
|
|
|
MalVal::ZKScalar(val) => {
|
|
|
@@ -81,15 +112,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
}
|
|
|
|
|
|
println!("Enforce Allocations\n");
|
|
|
- // we need to keep order
|
|
|
- for alloc_value in self.constraints.unwrap_or(Vec::<EnforceAllocation>::new()).iter() {
|
|
|
+ // we need to keep order
|
|
|
+ for alloc_value in self.constraints.iter() {
|
|
|
let coeff = bls12_381::Scalar::one();
|
|
|
let mut left = bellman::LinearCombination::<Scalar>::zero();
|
|
|
let mut right = bellman::LinearCombination::<Scalar>::zero();
|
|
|
let mut output = bellman::LinearCombination::<Scalar>::zero();
|
|
|
for values in alloc_value.left.iter() {
|
|
|
let (a, b) = values;
|
|
|
- println!("a {:?} b {:?}", a, b);
|
|
|
+ println!("left: a {:?} b {:?}", a, b);
|
|
|
let mut val_b = CS::one();
|
|
|
if b != "cs::one" {
|
|
|
val_b = *variables.get(b).unwrap();
|
|
|
@@ -103,12 +134,14 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
if let MalVal::ZKScalar(val) = value {
|
|
|
left = left + (*val, val_b);
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
+ println!("here i am");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for values in alloc_value.right.iter() {
|
|
|
let (a, b) = values;
|
|
|
+ println!("right: a {:?} b {:?}", a, b);
|
|
|
let mut val_b = CS::one();
|
|
|
if b != "cs::one" {
|
|
|
val_b = *variables.get(b).unwrap();
|
|
|
@@ -117,19 +150,24 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
right = right + (coeff, val_b);
|
|
|
} else if a == "scalar::one::neg" {
|
|
|
right = right + (coeff.neg(), val_b);
|
|
|
+ } else {
|
|
|
+ println!("here i am");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for values in alloc_value.output.iter() {
|
|
|
let (a, b) = values;
|
|
|
+ println!("output: a {:?} b {:?}", a, b);
|
|
|
let mut val_b = CS::one();
|
|
|
if b != "cs::one" {
|
|
|
val_b = *variables.get(b).unwrap();
|
|
|
}
|
|
|
if a == "scalar::one" {
|
|
|
- output = output + (coeff, val_b);
|
|
|
+ output = output + (Scalar::one(), val_b);
|
|
|
} else if a == "scalar::one::neg" {
|
|
|
- output = output + (coeff.neg(), val_b);
|
|
|
+ output = output + (Scalar::one().neg(), val_b);
|
|
|
+ } else {
|
|
|
+ println!("here i am");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -139,44 +177,24 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
|
|
|_| right.clone(),
|
|
|
|_| output.clone(),
|
|
|
);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[derive(Debug, Clone)]
|
|
|
-pub enum MalVal {
|
|
|
- Nil,
|
|
|
- Bool(bool),
|
|
|
- Int(i64),
|
|
|
- Str(String),
|
|
|
- Sym(String),
|
|
|
- List(Rc<Vec<MalVal>>, Rc<MalVal>),
|
|
|
- Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
|
|
|
- Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
|
|
|
- Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
|
|
|
- MalFunc {
|
|
|
- eval: fn(ast: MalVal, env: Env) -> MalRet,
|
|
|
- ast: Rc<MalVal>,
|
|
|
- env: Env,
|
|
|
- params: Rc<MalVal>,
|
|
|
- is_macro: bool,
|
|
|
- meta: Rc<MalVal>,
|
|
|
- },
|
|
|
- Atom(Rc<RefCell<MalVal>>),
|
|
|
- Zk(Rc<LispCircuit>), // TODO remote it
|
|
|
- Enforce(Rc<Vec<EnforceAllocation>>),
|
|
|
- ZKScalar(bls12_381::Scalar),
|
|
|
-}
|
|
|
-
|
|
|
#[derive(Debug)]
|
|
|
pub enum MalErr {
|
|
|
ErrString(String),
|
|
|
ErrMalVal(MalVal),
|
|
|
}
|
|
|
|
|
|
+impl From<SynthesisError> for MalErr {
|
|
|
+ fn from(err: SynthesisError) -> MalErr {
|
|
|
+ ErrString("SynthesisError".to_string())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
pub type MalArgs = Vec<MalVal>;
|
|
|
pub type MalRet = Result<MalVal, MalErr>;
|
|
|
|