update zklisp with cs methods

This commit is contained in:
ada
2020-10-25 21:15:44 +01:00
parent eec38d5c3f
commit be8378d44d
5 changed files with 89 additions and 47 deletions

View File

@@ -1,16 +0,0 @@
[package]
name = "zklisp"
version = "0.1.0"
authors = ["mileschet"]
[dependencies]
lazy_static = "1.4.0"
regex = "1.3.1"
itertools = "0.8.0"
fnv = "1.0.6"
bellman = { version = "0.8", default-features = false, features = ["groth16"] }
sapvi = { path = "../" }
[[bin]]
name = "lisp"
path = "lisp.rs"

View File

@@ -1,9 +1,9 @@
(def! bit-dec (fn* [x] (
(def! bits (unpack-bits x 256))
(def! enforce-step-1 (fn* [b] (enforce (add-one-lc0 (sub-lc0 b) (add-lc1 b))))
(map enforce-step-1 bits)
;; this can be improved like declaring the function before using like the example above
(map (fn* [b] ((add-lc0 x) double-coeff-lc) bits)
(enforce reset-coeff-lc sub-lc0 add-one-lc1)
)))))
(def! bit-dec
(fn* [x] (
(def! bits (unpack-bits x 256))
(def! enforce-step-1 (fn* [b] (enforce (add-one-lc0 (sub-lc0 b) (add-lc1 b))))
(map enforce-step-1 bits)
(map (fn* [b] ((add-lc0 b) double-coeff-lc) bits)
(enforce reset-coeff-lc sub-lc0 add-one-lc1)
)))))

View File

@@ -4,6 +4,25 @@ use std::rc::Rc;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use sapvi::bls_extensions::BlsStringConversion;
use sapvi::error::{Error, Result};
use sapvi::serial::{Decodable, Encodable};
use sapvi::vm::{
AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVMCircuit,
ZKVirtualMachine,
};
use bellman::{
gadgets::{
Assignment,
},
groth16, Circuit, ConstraintSystem, SynthesisError,
};
use bls12_381::Bls12;
use bls12_381::Scalar;
use ff::{Field, PrimeField};
use rand::rngs::OsRng;
use std::ops::{AddAssign, MulAssign, SubAssign};
use std::time::Instant;
use crate::printer::pr_seq;
use crate::reader::read_str;
use crate::types::MalErr::ErrMalVal;
@@ -153,17 +172,20 @@ fn nth(a: MalArgs) -> MalRet {
}
}
// (unpack-bits x 256 it produces a Vec
fn unpack_bits(a: MalArgs) -> MalRet {
// Scalar::from_string(
let mut result = vec![];
match (a[0].clone(), a[1].clone()) {
(List(seq, _), Int(idx)) | (Vector(seq, _), Int(idx)) => {
if seq.len() <= idx as usize {
return error("nth: index out of range");
}
Ok(seq[idx as usize].clone())
(Str(ref s), Int(size)) => {
let value = Scalar::from_string(s);
for (_, bit) in value.to_le_bits().into_iter().cloned().enumerate() {
match bit {
true => result.push(Scalar::one()),
false => result.push(Scalar::zero()),
}
}
Ok(list!(result.iter().map(|a| Str(std::string::ToString::to_string(&a))).collect::<Vec<MalVal>>()))
}
_ => error("invalid args to nth"),
_ => error("invalid args to unpack-bits"),
}
}
@@ -243,6 +265,22 @@ fn seq(a: MalArgs) -> MalRet {
}
}
fn add_one_lc0(a: MalArgs) -> MalRet {
Ok(Sym("add-one-lc0".to_string()))
}
fn enforce(a: MalArgs) -> MalRet {
Ok(Sym("enforce".to_string()))
}
fn double_coeff_lc(a: MalArgs) -> MalRet {
Ok(Sym("double-coeff-lc".to_string()))
}
fn reset_coeff_lc(a: MalArgs) -> MalRet {
Ok(Sym("reset-coeff-lc".to_string()))
}
pub fn ns() -> Vec<(&'static str, MalVal)> {
vec![
("=", func(|a| Ok(Bool(a[0] == a[1])))),
@@ -329,6 +367,30 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
("deref", func(|a| a[0].deref())),
("reset!", func(|a| a[0].reset_bang(&a[1]))),
("swap!", func(|a| a[0].swap_bang(&a[1..].to_vec()))),
("unpack_bits", func(unpack_bits)),
("unpack-bits", func(unpack_bits)),
("add-lc0",
func(|a| {
println!("add-lc0 {}", pr_seq(&a, false, "", "", " "));
Ok(Nil)
})),
("sub-lc0",
func(|a| {
println!("sub-lc0 {}", pr_seq(&a, false, "", "", " "));
Ok(Nil)
})),
("enforce", func(enforce)),
("double-coeff-lc", func(double_coeff_lc)),
("reset-coeff-lc", func(double_coeff_lc)),
("sub-lc0",
func(|a| {
println!("sub-lc0 {}", pr_seq(&a, false, "", "", " "));
Ok(Nil)
})),
("add-one-lc0", func(add_one_lc0)),
("add-lc1",
func(|a| {
println!("add-lc1 {}", pr_seq(&a, false, "", "", " "));
Ok(Nil)
})),
]
}

View File

@@ -1,12 +0,0 @@
use bellman::groth16;
use bls12_381::{Bls12, Scalar};
use std::collections::{HashMap, HashSet};
pub use crate::bls_extensions::BlsStringConversion;
pub use crate::error::{Error, Result};
pub use crate::serial::{Decodable, Encodable};
pub use crate::vm::{
AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVMCircuit,
ZKVirtualMachine,
};

View File

@@ -1,2 +1,10 @@
(def! C_D "0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")
(println C_D)
(def! dec "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000")
(def! bits (unpack-bits dec 256))
(println (count bits))
(map (fn* [b] (println (add-one-lc0))) bits)
;; (map (fn* [b] ((add-lc0 b) (double-coeff-lc))) bits)
;; (reset-coeff-lc)
;; (sub-lc0 x)
;; (add-one-lc1)