Просмотр исходного кода

added Constraint Instructions and Variables from ZKVM to lisp env

ada 5 лет назад
Родитель
Сommit
fa798a8ba5
2 измененных файлов с 56 добавлено и 4 удалено
  1. 1 0
      lisp/TODO.md
  2. 55 4
      lisp/lisp.rs

+ 1 - 0
lisp/TODO.md

@@ -6,3 +6,4 @@
 [] - Improve output for debug, log and error with ast and env dump
 [] - Integrate with zkvm command line
 [] - Integrate with ZKVMCircuit: allocs and constraints
+[] - Added CryptoOperation such double and square to core.rs

+ 55 - 4
lisp/lisp.rs

@@ -10,6 +10,10 @@ use std::time::Instant;
 //use std::collections::HashMap;
 use fnv::FnvHashMap;
 use itertools::Itertools;
+use sapvi::vm::{
+    AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, 
+    ZKVirtualMachine,
+};
 
 #[macro_use]
 extern crate clap;
@@ -280,13 +284,60 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                                             Sym(_) => {}
                                             Add(b1, b2) => {
                                                 println!("Add {:?} {:?}", b1, b2);
-                                                zk.private.push(Scalar::from_string(&b2.pr_str(false).to_string()));
-                                                env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
+                                                // TODO extract a method
+                                                zk.private.push(Scalar::from_string(
+                                                    &b2.pr_str(false).to_string(),
+                                                ));
+                                                let const_a: ConstraintInstruction = match b1.as_ref() {
+                                                    Lc0 => 
+                                                        ConstraintInstruction::Lc0Add(
+                                                            zk.private.len(),
+                                                        
+                                                    ),
+                                                    Lc1 => 
+                                                        ConstraintInstruction::Lc1Add(
+                                                            zk.private.len(),
+                                                    ),
+                                                    Lc2 => 
+                                                        ConstraintInstruction::Lc2Add(
+                                                            zk.private.len(),
+                                                    ),
+                                                    _ => ConstraintInstruction::Lc0Add(0) 
+                                                };
+                                                zk.constraints.push(const_a);
+                                                env_set(
+                                                    &env,
+                                                    a1.clone(),
+                                                    types::MalVal::Zk(zk.clone()),
+                                                );
                                             }
                                             Sub(b1, b2) => {
                                                 println!("Sub {:?} {:?}", b1, b2);
-                                                zk.private.push(Scalar::from_string(&b2.pr_str(false).to_string()));
-                                                env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
+                                                zk.private.push(Scalar::from_string(
+                                                    &b2.pr_str(false).to_string(),
+                                                ));
+                                                let const_a: ConstraintInstruction = match b1.as_ref() {
+                                                    Lc0 => 
+                                                        ConstraintInstruction::Lc0Sub(
+                                                            zk.private.len(),
+                                                        
+                                                    ),
+                                                    Lc1 => 
+                                                        ConstraintInstruction::Lc1Add(
+                                                            zk.private.len(),
+                                                    ),
+                                                    Lc2 => 
+                                                        ConstraintInstruction::Lc2Add(
+                                                            zk.private.len(),
+                                                    ),
+                                                    _ => ConstraintInstruction::Lc0Add(0) 
+                                                };
+                                                zk.constraints.push(const_a);
+                                                env_set(
+                                                    &env,
+                                                    a1.clone(),
+                                                    types::MalVal::Zk(zk.clone()),
+                                                );
                                             }
                                             _ => {
                                                 println!("not mapped");