mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-10 07:08:05 -05:00
Merge branch 'master' of github.com:narodnik/sapvi
This commit is contained in:
@@ -35,7 +35,7 @@ num_enum = "0.5.0"
|
||||
|
||||
lazy_static = "1.4.0"
|
||||
itertools = "0.8.0"
|
||||
fnv = "1.0.6"
|
||||
#fnv = "1.0.6"
|
||||
regex = "1"
|
||||
|
||||
simplelog = "0.7.4"
|
||||
@@ -83,6 +83,10 @@ path = "src/bin/zkvm.rs"
|
||||
name = "dfi"
|
||||
path = "src/bin/dfi.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "mimc"
|
||||
path = "src/old/mimc.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "dfg"
|
||||
path = "src/bin/dfg.rs"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
//use std::collections::HashMap;
|
||||
use fnv::FnvHashMap;
|
||||
use std::collections::HashMap;
|
||||
// use fnv::FnvHashMap;
|
||||
|
||||
use crate::types::MalErr::ErrString;
|
||||
use crate::types::MalVal::{List, Nil, Sym, Vector};
|
||||
@@ -9,7 +9,7 @@ use crate::types::{error, MalErr, MalRet, MalVal};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EnvStruct {
|
||||
data: RefCell<FnvHashMap<String, MalVal>>,
|
||||
data: RefCell<HashMap<String, MalVal>>,
|
||||
pub outer: Option<Env>,
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ pub type Env = Rc<EnvStruct>;
|
||||
|
||||
pub fn env_new(outer: Option<Env>) -> Env {
|
||||
Rc::new(EnvStruct {
|
||||
data: RefCell::new(FnvHashMap::default()),
|
||||
data: RefCell::new(HashMap::default()),
|
||||
outer: outer,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -186,12 +186,6 @@
|
||||
)
|
||||
))
|
||||
|
||||
;; cs.enforce(
|
||||
;; || "boolean constraint",
|
||||
;; |lc| lc + CS::one() - var,
|
||||
;; |lc| lc + var,
|
||||
;; |lc| lc,
|
||||
;; );
|
||||
(defmacro! zk-boolean (fn* [val] (
|
||||
(let* [var (gensym)] (
|
||||
`(alloc ~var ~val)
|
||||
@@ -209,7 +203,6 @@
|
||||
(def! val (last (last (zk-double u v))))
|
||||
(def! acc 0)
|
||||
(dotimes (count result) (
|
||||
(def! acc (i+ acc 1))
|
||||
(def! u3 (get val "u3"))
|
||||
(def! v3 (get val "v3"))
|
||||
(def! r (nth result acc))
|
||||
@@ -220,64 +213,103 @@
|
||||
(def! u-add (get add-result "u3"))
|
||||
(def! v-add (get add-result "v3"))
|
||||
(def! val (last (last (zk-double u-add v-add))))
|
||||
;; debug
|
||||
(println 'first-double val)
|
||||
(println 'r r)
|
||||
(println 'cond cond-result)
|
||||
(println 'add add-result)
|
||||
(println 'double acc val)
|
||||
(def! acc (i+ acc 1))
|
||||
))
|
||||
(println 'out val)
|
||||
(val)
|
||||
;; { "u3" (get val "u3"), "v3" (get val "v3") }
|
||||
)))
|
||||
|
||||
(load-file "mimc-constants.lisp")
|
||||
(defmacro! mimc-macro (fn* [xr xl acc] (
|
||||
(let* [tmp-xl (gensym) xl-new-value (gensym) cur-mimc-const (gensym)] (
|
||||
(defmacro! mimc-macro (fn* [left-value right-value acc] (
|
||||
(let* [tmp-xl (gensym2 'tmp_xl)
|
||||
xl-new-value (gensym2 'xl_new_value)
|
||||
cur-mimc-const (gensym2 'cur_mimc_const)
|
||||
xl (gensym2 'xl)
|
||||
xr (gensym2 'xr)] (
|
||||
`(def! ~xl (alloc ~xl ~left-value))
|
||||
`(def! ~xr (alloc ~xr ~right-value))
|
||||
`(def! ~cur-mimc-const (alloc-const ~cur-mimc-const (nth mimc-constants ~acc)))
|
||||
`(def! ~tmp-xl (alloc ~tmp-xl (square (+ ~cur-mimc-const ~xl))))
|
||||
`(enforce
|
||||
((scalar::one xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
(scalar::one ~tmp-xl)
|
||||
)
|
||||
(if (= acc 321)
|
||||
`(def! ~xl-new-value (alloc ~xl-new-value (+ (* ~tmp-xl (+ ~cur-mimc-const ~xl)) ~xr)))
|
||||
`(def! ~xl-new-value (alloc-input ~xl-new-value (+ (* ~tmp-xl (+ ~cur-mimc-const ~xl)) ~xr)))
|
||||
)
|
||||
`(def! new-value (+ (* ~tmp-xl (+ ~cur-mimc-const ~xl)) ~xr))
|
||||
`(if (= ~acc 321)
|
||||
(def! ~xl-new-value (alloc-input ~xl-new-value new-value))
|
||||
(def! ~xl-new-value (alloc ~xl-new-value new-value))
|
||||
)
|
||||
`(enforce
|
||||
(scalar::one ~tmp-xl)
|
||||
((scalar::one xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl-new-value) (scalar::one::neg xr))
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl-new-value) (scalar::one::neg ~xr))
|
||||
)
|
||||
)))))
|
||||
`{ "left" new-value }
|
||||
)
|
||||
))))
|
||||
|
||||
(def! mimc (fn* [left right] (
|
||||
(def! xl (alloc "xl" left))
|
||||
(def! xr (alloc "xr" right))
|
||||
(def! acc 0)
|
||||
(dotimes 322 (
|
||||
(println (mimc-macro xl xr acc))
|
||||
(def! acc (i+ acc 1))
|
||||
(def! xl left)
|
||||
(def! xr right)
|
||||
(dotimes 322 (
|
||||
(def! result (mimc-macro xl xr acc))
|
||||
(def! result-value (get (last (last result)) "left"))
|
||||
(println acc)
|
||||
(println xl xr)
|
||||
(println result-value)
|
||||
(def! xr xl)
|
||||
(def! xl result-value)
|
||||
(def! acc (i+ acc 1))
|
||||
))
|
||||
)))
|
||||
|
||||
(def! param3 (rnd-scalar))
|
||||
;; (println 'rnd-scalar param3)
|
||||
(def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
(def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
(def! mint-contract (fn* [public-u public-v] (
|
||||
(def! randomness (rnd-scalar))
|
||||
(def! witness-result (zk-witness public-u public-v))
|
||||
(def! not-small-order (zk-not-small-order? public-u public-v))
|
||||
(def! g-vcr-u (alloc-input "g-vcr-u" public-u))
|
||||
(def! g-vcr-v (alloc-input "g-vcr-v" public-v))
|
||||
(def! mul-result (last (last (jj-mul g-vcr-u g-vcr-v randomness))))
|
||||
(def! rcvu (alloc "rcvu" (get mul-result "u3")))
|
||||
(def! rcvr (alloc "rcvr" (get mul-result "v3")))
|
||||
(enforce
|
||||
(scalar::one rcvu)
|
||||
(scalar::one cs::one)
|
||||
(scalar::one rcvu)
|
||||
)
|
||||
(enforce
|
||||
(scalar::one rcvr)
|
||||
(scalar::one cs::one)
|
||||
(scalar::one rcvr)
|
||||
)
|
||||
)))
|
||||
|
||||
(prove
|
||||
(
|
||||
;; (jj-mul param-u param-v param3)
|
||||
(mimc param-u param-v)
|
||||
(
|
||||
(def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
(def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
(mint-contract param-u param-v)
|
||||
)
|
||||
)
|
||||
|
||||
;; following some examples
|
||||
;; (def! alloc-u (alloc "alloc-u" param-u))
|
||||
;; (def! alloc-v (alloc "alloc-v" param-v))
|
||||
;; (def! condition (alloc "condition" param3))
|
||||
;; (println 'conditionally_select
|
||||
;; (conditionally_select alloc-u alloc-v condition))
|
||||
;; (def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
|
||||
;; (def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
|
||||
;; (mimc left right)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (println 'rnd-scalar param3)
|
||||
;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
;; (println (zk-mul param1 param2))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (println (zk-mul param1 param2))
|
||||
;; (def! param1 (scalar 3))
|
||||
;; (def! param2 (scalar 9))
|
||||
|
||||
321
lisp/examples/mint2.lisp
Normal file
321
lisp/examples/mint2.lisp
Normal file
@@ -0,0 +1,321 @@
|
||||
(load-file "util.lisp")
|
||||
|
||||
(def! zk-not-small-order? (fn* [u v] (
|
||||
(def! first-doubling (last (last (zk-double u v))))
|
||||
(def! second-doubling (last (last
|
||||
(zk-double (get first-doubling "u3") (get first-doubling "v3")))))
|
||||
(def! third-doubling (last (last
|
||||
(zk-double (get second-doubling "u3") (get second-doubling "v3")))))
|
||||
(zk-nonzero? (get third-doubling "u3"))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(defmacro! zk-nonzero? (fn* [var] (
|
||||
(let* [inv (gensym)
|
||||
v1 (gensym)] (
|
||||
`(alloc ~inv (invert ~var))
|
||||
`(alloc ~v1 ~var)
|
||||
`(enforce
|
||||
(scalar::one ~v1)
|
||||
(scalar::one ~inv)
|
||||
(scalar::one cs::one)
|
||||
)
|
||||
)
|
||||
))
|
||||
))
|
||||
|
||||
(defmacro! zk-square (fn* [var] (
|
||||
(let* [v1 (gensym)
|
||||
v2 (gensym)] (
|
||||
`(alloc ~v1 ~var)
|
||||
`(def! output (alloc-input ~v2 (square ~var)))
|
||||
`(enforce
|
||||
(scalar::one ~v1)
|
||||
(scalar::one ~v1)
|
||||
(scalar::one ~v2)
|
||||
)
|
||||
`{ "v2" output }
|
||||
)
|
||||
))
|
||||
))
|
||||
|
||||
(defmacro! zk-mul (fn* [val1 val2] (
|
||||
(let* [v1 (gensym)
|
||||
v2 (gensym)
|
||||
var (gensym)] (
|
||||
`(alloc ~v1 ~val1)
|
||||
`(alloc ~v2 ~val2)
|
||||
`(def! result (alloc-input ~var (* ~val1 ~val2)))
|
||||
`(enforce
|
||||
(scalar::one ~v1)
|
||||
(scalar::one ~v2)
|
||||
(scalar::one ~var)
|
||||
)
|
||||
`{ "result" result }
|
||||
)
|
||||
))
|
||||
))
|
||||
|
||||
(defmacro! zk-witness (fn* [val1 val2] (
|
||||
(let* [u2 (gensym)
|
||||
v2 (gensym)
|
||||
u2v2 (gensym)
|
||||
EDWARDS_D (gensym)] (
|
||||
`(def! ~EDWARDS_D (alloc-const ~EDWARDS_D (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
|
||||
`(def! ~u2 (alloc ~u2 (get (nth (nth (zk-square ~val1) 0) 3) "v2")))
|
||||
`(def! ~v2 (alloc ~v2 (get (nth (nth (zk-square ~val2) 0) 3) "v2")))
|
||||
`(def! result (alloc-input ~u2v2 (get (last (last (zk-mul ~u2 ~v2))) "result")))
|
||||
`(enforce
|
||||
((scalar::one::neg ~u2) (scalar::one ~v2))
|
||||
(scalar::one cs::one)
|
||||
((scalar::one cs::one) (~EDWARDS_D ~u2v2))
|
||||
)
|
||||
`{ "result" result }
|
||||
)
|
||||
))
|
||||
))
|
||||
|
||||
(defmacro! zk-double (fn* [val1 val2] (
|
||||
(let* [u (gensym)
|
||||
v (gensym)
|
||||
u3 (gensym)
|
||||
v3 (gensym)
|
||||
T (gensym)
|
||||
A (gensym)
|
||||
C (gensym)
|
||||
EDWARDS_D (gensym)] (
|
||||
`(def! ~EDWARDS_D (alloc-const ~EDWARDS_D (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
|
||||
`(def! ~u (alloc ~u ~val1))
|
||||
`(def! ~v (alloc ~v ~val2))
|
||||
`(def! ~T (alloc ~T (* (+ ~val1 ~val2) (+ ~val1 ~val2))))
|
||||
`(def! ~A (alloc ~A (* ~u ~v)))
|
||||
`(def! ~C (alloc ~C (* (square ~A) ~EDWARDS_D)))
|
||||
`(def! ~u3 (alloc-input ~u3 (/ (double ~A) (+ scalar::one ~C))))
|
||||
`(def! ~v3 (alloc-input ~v3 (/ (- ~T (double ~A)) (- scalar::one ~C))))
|
||||
`(enforce
|
||||
((scalar::one ~u) (scalar::one ~v))
|
||||
((scalar::one ~u) (scalar::one ~v))
|
||||
(scalar::one ~T)
|
||||
)
|
||||
`(enforce
|
||||
(~EDWARDS_D ~A)
|
||||
(scalar::one ~A)
|
||||
(scalar::one ~C)
|
||||
)
|
||||
`(enforce
|
||||
((scalar::one cs::one) (scalar::one ~C))
|
||||
(scalar::one ~u3)
|
||||
((scalar::one ~A) (scalar::one ~A))
|
||||
)
|
||||
`(enforce
|
||||
((scalar::one cs::one) (scalar::one::neg ~C))
|
||||
(scalar::one ~v3)
|
||||
((scalar::one ~T) (scalar::one::neg ~A) (scalar::one::neg ~A))
|
||||
)
|
||||
{ "u3" u3, "v3" v3 }
|
||||
)
|
||||
))
|
||||
))
|
||||
|
||||
(defmacro! conditionally-select (fn* [val1 val2 val3] (
|
||||
(let* [u-prime (gensym)
|
||||
v-prime (gensym)
|
||||
u (gensym)
|
||||
v (gensym)
|
||||
condition (gensym)
|
||||
] (
|
||||
`(def! ~u (alloc ~u ~val1))
|
||||
`(def! ~v (alloc ~v ~val2))
|
||||
`(def! ~condition (alloc ~condition ~val3))
|
||||
`(def! ~u-prime (alloc-input ~u-prime (* ~u ~condition)))
|
||||
`(def! ~v-prime (alloc-input ~v-prime (* ~v ~condition)))
|
||||
`(enforce
|
||||
(scalar::one ~u)
|
||||
(scalar::one ~condition)
|
||||
(scalar::one ~u-prime)
|
||||
)
|
||||
`(enforce
|
||||
(scalar::one ~v)
|
||||
(scalar::one ~condition)
|
||||
(scalar::one ~v-prime)
|
||||
)
|
||||
{ "u-prime" u-prime, "v-prime" v-prime }
|
||||
)
|
||||
))))
|
||||
|
||||
(defmacro! jj-add (fn* [param1 param2 param3 param4]
|
||||
(let* [u1 (gensym) v1 (gensym) u2 (gensym) v2 (gensym)
|
||||
EDWARDS_D (gensym) U (gensym) A (gensym) B (gensym)
|
||||
C (gensym) u3 (gensym) v3 (gensym)] (
|
||||
;; debug
|
||||
;; `(println 'jj-add ~param1 ~param2 ~param3 ~param4)
|
||||
`(def! ~u1 (alloc ~u1 ~param1))
|
||||
`(def! ~v1 (alloc ~v1 ~param2))
|
||||
`(def! ~u2 (alloc ~u2 ~param3))
|
||||
`(def! ~v2 (alloc ~v2 ~param4))
|
||||
`(def! ~EDWARDS_D (alloc-const ~EDWARDS_D (scalar "2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1")))
|
||||
`(def! ~U (alloc ~U (* (+ ~u1 ~v1) (+ ~u2 ~v2))))
|
||||
`(def! ~A (alloc ~A (* ~v2 ~u1)))
|
||||
`(def! ~B (alloc ~B (* ~u2 ~v1)))
|
||||
`(def! ~C (alloc ~C (* ~EDWARDS_D (* ~A ~B))))
|
||||
`(def! ~u3 (alloc-input ~u3 (/ (+ ~A ~B) (+ scalar::one ~C))))
|
||||
`(def! ~v3 (alloc-input ~v3 (/ (- (- ~U ~A) ~B) (- scalar::one ~C))))
|
||||
`(enforce
|
||||
((scalar::one ~u1) (scalar::one ~v1))
|
||||
((scalar::one ~u2) (scalar::one ~v2))
|
||||
(scalar::one ~U)
|
||||
)
|
||||
`(enforce
|
||||
(~EDWARDS_D ~A)
|
||||
(scalar::one ~B)
|
||||
(scalar::one ~C)
|
||||
)
|
||||
`(enforce
|
||||
((scalar::one cs::one)(scalar::one ~C))
|
||||
(scalar::one ~u3)
|
||||
((scalar::one ~A) (scalar::one ~B))
|
||||
)
|
||||
`(enforce
|
||||
((scalar::one cs::one) (scalar::one::neg ~C))
|
||||
(scalar::one ~v3)
|
||||
((scalar::one ~U) (scalar::one::neg ~A) (scalar::one::neg ~B))
|
||||
)
|
||||
`{ "u3" u3, "v3" v3 }
|
||||
)
|
||||
)
|
||||
))
|
||||
|
||||
(defmacro! zk-boolean (fn* [val] (
|
||||
(let* [var (gensym)] (
|
||||
`(alloc ~var ~val)
|
||||
`(enforce
|
||||
(scalar::one cs::one) (scalar::one ~var)
|
||||
(scalar::one ~var)
|
||||
()
|
||||
)
|
||||
)
|
||||
))))
|
||||
|
||||
(def! jj-mul (fn* [u v b] (
|
||||
(def! result (unpack-bits b))
|
||||
(eval (map zk-boolean result))
|
||||
(def! val (last (last (zk-double u v))))
|
||||
(def! acc 0)
|
||||
(dotimes (count result) (
|
||||
(def! u3 (get val "u3"))
|
||||
(def! v3 (get val "v3"))
|
||||
(def! r (nth result acc))
|
||||
(def! cond-result (last (last (conditionally-select u3 v3 r))))
|
||||
(def! u-prime (get cond-result "u-prime"))
|
||||
(def! v-prime (get cond-result "v-prime"))
|
||||
(def! add-result (last (jj-add u3 v3 u-prime v-prime)))
|
||||
(def! u-add (get add-result "u3"))
|
||||
(def! v-add (get add-result "v3"))
|
||||
(def! val (last (last (zk-double u-add v-add))))
|
||||
(def! acc (i+ acc 1))
|
||||
))
|
||||
(val)
|
||||
;; { "u3" (get val "u3"), "v3" (get val "v3") }
|
||||
)))
|
||||
|
||||
(load-file "mimc-constants.lisp")
|
||||
(defmacro! mimc-macro (fn* [left-value right-value acc] (
|
||||
(let* [tmp-xl (gensym2 'tmp_xl)
|
||||
xl-new-value (gensym2 'xl_new_value)
|
||||
cur-mimc-const (gensym2 'cur_mimc_const)
|
||||
xl (gensym2 'xl)
|
||||
xr (gensym2 'xr)] (
|
||||
`(def! ~xl (alloc ~xl ~left-value))
|
||||
`(def! ~xr (alloc ~xr ~right-value))
|
||||
`(def! ~cur-mimc-const (alloc-const ~cur-mimc-const (nth mimc-constants ~acc)))
|
||||
`(def! ~tmp-xl (alloc ~tmp-xl (square (+ ~cur-mimc-const ~xl))))
|
||||
`(enforce
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
(scalar::one ~tmp-xl)
|
||||
)
|
||||
`(def! new-value (+ (* ~tmp-xl (+ ~cur-mimc-const ~xl)) ~xr))
|
||||
`(if (= ~acc 321)
|
||||
(def! ~xl-new-value (alloc-input ~xl-new-value new-value))
|
||||
(def! ~xl-new-value (alloc ~xl-new-value new-value))
|
||||
)
|
||||
`(enforce
|
||||
(scalar::one ~tmp-xl)
|
||||
((scalar::one ~xl) (~cur-mimc-const cs::one))
|
||||
((scalar::one ~xl-new-value) (scalar::one::neg ~xr))
|
||||
)
|
||||
`{ "left" new-value }
|
||||
)
|
||||
))))
|
||||
|
||||
(def! mimc (fn* [left right] (
|
||||
(def! acc 0)
|
||||
(def! xl left)
|
||||
(def! xr right)
|
||||
(dotimes 322 (
|
||||
(def! result (mimc-macro xl xr acc))
|
||||
(def! result-value (get (last (last result)) "left"))
|
||||
(println acc)
|
||||
(println xl xr)
|
||||
(println result-value)
|
||||
(def! xr xl)
|
||||
(def! xl result-value)
|
||||
(def! acc (i+ acc 1))
|
||||
))
|
||||
)))
|
||||
|
||||
(def! mint-contract (fn* [public-u public-v] (
|
||||
(def! randomness (rnd-scalar))
|
||||
(def! witness-result (zk-witness public-u public-v))
|
||||
(def! not-small-order (zk-not-small-order? public-u public-v))
|
||||
(def! g-vcr-u (alloc-input "g-vcr-u" public-u))
|
||||
(def! g-vcr-v (alloc-input "g-vcr-v" public-v))
|
||||
(def! mul-result (last (last (jj-mul g-vcr-u g-vcr-v randomness))))
|
||||
(def! rcvu (alloc "rcvu" (get mul-result "u3")))
|
||||
(def! rcvr (alloc "rcvr" (get mul-result "v3")))
|
||||
(enforce
|
||||
(scalar::one rcvu)
|
||||
(scalar::one cs::one)
|
||||
(scalar::one rcvu)
|
||||
)
|
||||
(enforce
|
||||
(scalar::one rcvr)
|
||||
(scalar::one cs::one)
|
||||
(scalar::one rcvr)
|
||||
)
|
||||
)))
|
||||
|
||||
(prove
|
||||
(
|
||||
(def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
(def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
(mint-contract param-u param-v)
|
||||
)
|
||||
)
|
||||
|
||||
;; following some examples
|
||||
;; (def! left (scalar "15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e"))
|
||||
;; (def! right (scalar "015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891"))
|
||||
;; (mimc left right)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (def! param3 (rnd-scalar))
|
||||
;; (println 'rnd-scalar param3)
|
||||
;; (def! param-u (scalar "6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337"))
|
||||
;; (def! param-v (scalar "6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b"))
|
||||
;; (println (zk-mul param1 param2))
|
||||
;; (jj-mul param-u param-v param3)
|
||||
;; (println (zk-mul param1 param2))
|
||||
;; (def! param1 (scalar 3))
|
||||
;; (def! param2 (scalar 9))
|
||||
;; (println (zk-square param1))
|
||||
;; (println (zk-mul param1 param2))
|
||||
;; (println 'witness (zk-witness param-u param-v))
|
||||
;; (println 'double (last (last (zk-double param-u param-v))))
|
||||
;; (println 'nonzero (zk-nonzero? param3))
|
||||
;; (println 'not-small-order? (zk-not-small-order? param-u param-v))
|
||||
@@ -2,9 +2,12 @@
|
||||
(def! gensym
|
||||
(let* [counter (atom 0)]
|
||||
(fn* []
|
||||
;; (symbol (str "G__" (genrand) (swap! counter inc))))))
|
||||
(symbol (str "G__" (swap! counter inc))))))
|
||||
|
||||
(def! gensym2
|
||||
(let* [counter (atom 0)]
|
||||
(fn* [name]
|
||||
(symbol (str name "__" (swap! counter inc))))))
|
||||
;; Like load-file, but will never load the same path twice.
|
||||
|
||||
;; This file is normally loaded with `load-file`, so it needs a
|
||||
|
||||
113
lisp/lisp.rs
113
lisp/lisp.rs
@@ -8,10 +8,10 @@ use simplelog::*;
|
||||
|
||||
use bellman::groth16;
|
||||
use bls12_381::Bls12;
|
||||
use fnv::FnvHashMap;
|
||||
// use fnv::FnvHashMap;
|
||||
use itertools::Itertools;
|
||||
use rand::rngs::OsRng;
|
||||
use std::fs::File;
|
||||
use std::{collections::HashMap, cell::RefCell};
|
||||
use std::rc::Rc;
|
||||
use std::time::Instant;
|
||||
use std::{
|
||||
@@ -24,14 +24,14 @@ use types::EnforceAllocation;
|
||||
extern crate clap;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate fnv;
|
||||
// extern crate fnv;
|
||||
extern crate itertools;
|
||||
extern crate regex;
|
||||
|
||||
#[macro_use]
|
||||
mod types;
|
||||
use crate::types::MalErr::{ErrMalVal, ErrString};
|
||||
use crate::types::MalVal::{Bool, Enforce, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector};
|
||||
use crate::types::MalVal::{Bool, Enforce, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector, Alloc};
|
||||
use crate::types::VerifyKeyParams;
|
||||
use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
|
||||
mod env;
|
||||
@@ -132,7 +132,7 @@ fn eval_ast(ast: &MalVal, env: &Env) -> MalRet {
|
||||
Ok(vector!(lst))
|
||||
}
|
||||
Hash(hm, _) => {
|
||||
let mut new_hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
|
||||
let mut new_hm: HashMap<String, MalVal> = HashMap::default();
|
||||
for (k, v) in hm.iter() {
|
||||
new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
|
||||
}
|
||||
@@ -291,7 +291,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
Sym(ref a0sym) if a0sym == "dotimes" => {
|
||||
match eval(l[1].clone(), env.clone())? {
|
||||
MalVal::Int(v) => {
|
||||
for _i in 1..v {
|
||||
for _i in 0..v {
|
||||
eval(l[2].clone(), env.clone())?;
|
||||
}
|
||||
Ok(Nil)
|
||||
@@ -346,56 +346,63 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
prove(a1.clone(), env.clone())
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "alloc-const" => {
|
||||
// let start = Instant::now();
|
||||
let a1 = l[1].clone();
|
||||
let value = eval(l[2].clone(), env.clone())?;
|
||||
let result = eval(value.clone(), env.clone())?;
|
||||
let allocs = get_allocations(&env, "AllocationsConst");
|
||||
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.clone());
|
||||
allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
|
||||
// let mut new_hm: HashMap<String, MalVal> = HashMap::default();
|
||||
// for (k, v) in allocs.borrow_mut().iter() {
|
||||
// new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
|
||||
// }
|
||||
// new_hm.insert(a1.pr_str(false), result.clone());
|
||||
if let Some(e) = &env.outer {
|
||||
env_set(
|
||||
&e,
|
||||
Sym("AllocationsConst".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
} else {
|
||||
env_set(
|
||||
&env,
|
||||
Sym("AllocationsConst".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
}
|
||||
// println!("Alloc Const: {:?}", start.elapsed());
|
||||
Ok(result.clone())
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "alloc-input" => {
|
||||
// let start = Instant::now();
|
||||
let a1 = l[1].clone();
|
||||
let value = eval(l[2].clone(), env.clone())?;
|
||||
let result = eval(value.clone(), env.clone())?;
|
||||
let allocs = get_allocations(&env, "AllocationsInput");
|
||||
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.clone());
|
||||
allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
|
||||
// let mut new_hm: HashMap<String, MalVal> = HashMap::default();
|
||||
// for (k, v) in allocs.borrow_mut().iter() {
|
||||
// new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
|
||||
// }
|
||||
// new_hm.insert(a1.pr_str(false), result.clone());
|
||||
if let Some(e) = &env.outer {
|
||||
env_set(
|
||||
&e,
|
||||
Sym("AllocationsInput".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
} else {
|
||||
env_set(
|
||||
&env,
|
||||
Sym("AllocationsInput".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
}
|
||||
// println!("Alloc Input: {:?}", start.elapsed());
|
||||
Ok(result.clone())
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "alloc" => {
|
||||
// let start = Instant::now();
|
||||
let a1 = l[1].clone();
|
||||
let mut value = eval(l[2].clone(), env.clone())?;
|
||||
if let Func(_, _) = value {
|
||||
@@ -403,24 +410,26 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
}
|
||||
let result = eval(value.clone(), env.clone())?;
|
||||
let allocs = get_allocations(&env, "Allocations");
|
||||
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.clone());
|
||||
allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
|
||||
// let mut new_hm: HashMap<String, MalVal> = HashMap::default();
|
||||
// for (k, v) in allocs.borrow_mut().iter() {
|
||||
// new_hm.insert(k.to_string(), eval(v.clone(), env.clone())?);
|
||||
// }
|
||||
// new_hm.insert(a1.pr_str(false), result.clone());
|
||||
if let Some(e) = &env.outer {
|
||||
env_set(
|
||||
&e,
|
||||
Sym("Allocations".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
} else {
|
||||
env_set(
|
||||
&env,
|
||||
Sym("Allocations".to_string()),
|
||||
Hash(Rc::new(new_hm), Rc::new(Nil)),
|
||||
Alloc(allocs),
|
||||
)?;
|
||||
}
|
||||
// println!("Alloc: {:?}", start.elapsed());
|
||||
Ok(result.clone())
|
||||
}
|
||||
//Sym(ref a0sym) if a0sym == "verify" => {
|
||||
@@ -485,29 +494,29 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
let enforce_vec = get_enforce_allocs(&env);
|
||||
let mut enforce_vec = get_enforce_allocs(&env).clone();
|
||||
let enforce = EnforceAllocation {
|
||||
idx: enforce_vec.len() + 1,
|
||||
left: left_vec,
|
||||
right: right_vec,
|
||||
output: out_vec,
|
||||
};
|
||||
let mut new_vec: Vec<EnforceAllocation> = vec![enforce];
|
||||
for value in enforce_vec.iter() {
|
||||
new_vec.push(value.clone());
|
||||
}
|
||||
// TODO change it
|
||||
enforce_vec.push(enforce);
|
||||
// let mut new_vec: Vec<EnforceAllocation> = vec![enforce];
|
||||
// for value in enforce_vec.iter() {
|
||||
// new_vec.push(value.clone());
|
||||
// }
|
||||
if let Some(e) = &env.outer {
|
||||
env_set(
|
||||
&e,
|
||||
Sym("AllocationsEnforce".to_string()),
|
||||
vector![vec![Enforce(Rc::new(new_vec.clone()))]],
|
||||
vector![vec![Enforce(Rc::new(enforce_vec))]],
|
||||
)?;
|
||||
} else {
|
||||
env_set(
|
||||
&env,
|
||||
Sym("AllocationsEnforce".to_string()),
|
||||
vector![vec![Enforce(Rc::new(new_vec.clone()))]],
|
||||
vector![vec![Enforce(Rc::new(enforce_vec))]],
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -517,7 +526,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
// );
|
||||
// println!("enforce here {:?}", get_enforce_allocs_nested(&env));
|
||||
|
||||
Ok(MalVal::Nil)
|
||||
Ok(MalVal::Str("enforce-eof".to_string()))
|
||||
}
|
||||
_ => match eval_ast(&ast, &env)? {
|
||||
List(ref el, _) => {
|
||||
@@ -585,7 +594,7 @@ pub fn get_enforce_allocs_nested(env: &Env) -> Vec<EnforceAllocation> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
|
||||
pub fn get_allocations(env: &Env, key: &str) -> RefCell<HashMap<String, MalVal>> {
|
||||
if let Some(e) = &env.outer {
|
||||
get_allocations_nested(&e, key)
|
||||
} else {
|
||||
@@ -593,12 +602,12 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
|
||||
let alloc_hm: Rc<FnvHashMap<String, MalVal>> = Rc::new(FnvHashMap::default());
|
||||
pub fn get_allocations_nested(env: &Env, key: &str) -> RefCell<HashMap<String, MalVal>> {
|
||||
let alloc_hm: RefCell<HashMap<String, MalVal>> = RefCell::new(HashMap::default());
|
||||
match env_find(env, key) {
|
||||
Some(e) => match env_get(&e, &Sym(key.to_string())) {
|
||||
Ok(f) => {
|
||||
if let Hash(allocs, _) = f {
|
||||
if let MalVal::Alloc(allocs) = f {
|
||||
allocs
|
||||
} else {
|
||||
alloc_hm
|
||||
@@ -613,9 +622,9 @@ pub fn get_allocations_nested(env: &Env, key: &str) -> Rc<FnvHashMap<String, Mal
|
||||
pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
|
||||
let start = Instant::now();
|
||||
let c = LispCircuit {
|
||||
params: FnvHashMap::default(),
|
||||
allocs: FnvHashMap::default(),
|
||||
alloc_inputs: FnvHashMap::default(),
|
||||
params: HashMap::default(),
|
||||
allocs: HashMap::default(),
|
||||
alloc_inputs: HashMap::default(),
|
||||
constraints: Vec::new(),
|
||||
};
|
||||
let random_parameters =
|
||||
@@ -630,7 +639,7 @@ pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
|
||||
}
|
||||
|
||||
pub fn prove(_ast: MalVal, env: Env) -> MalRet {
|
||||
// let start = Instant::now();
|
||||
let start = Instant::now();
|
||||
let allocs_input = get_allocations(&env, "AllocationsInput");
|
||||
let allocs = get_allocations(&env, "Allocations");
|
||||
let enforce_allocs = get_enforce_allocs(&env);
|
||||
@@ -638,9 +647,9 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
|
||||
//setup
|
||||
let params = Some({
|
||||
let circuit = LispCircuit {
|
||||
params: allocs_const.as_ref().clone(),
|
||||
allocs: allocs.as_ref().clone(),
|
||||
alloc_inputs: allocs_input.as_ref().clone(),
|
||||
params: allocs_const.borrow().clone(),
|
||||
allocs: allocs.borrow().clone(),
|
||||
alloc_inputs: allocs_input.borrow().clone(),
|
||||
constraints: enforce_allocs.clone(),
|
||||
};
|
||||
groth16::generate_random_parameters::<Bls12, _, _>(circuit, &mut OsRng)?
|
||||
@@ -648,15 +657,15 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
|
||||
let verifying_key = Some(groth16::prepare_verifying_key(¶ms.as_ref().unwrap().vk));
|
||||
// prove
|
||||
let circuit = LispCircuit {
|
||||
params: allocs_const.as_ref().clone(),
|
||||
allocs: allocs.as_ref().clone(),
|
||||
alloc_inputs: allocs_input.as_ref().clone(),
|
||||
params: allocs_const.borrow().clone(),
|
||||
allocs: allocs.borrow().clone(),
|
||||
alloc_inputs: allocs_input.borrow().clone(),
|
||||
constraints: enforce_allocs.clone(),
|
||||
};
|
||||
|
||||
let proof = groth16::create_random_proof(circuit, params.as_ref().unwrap(), &mut OsRng)?;
|
||||
let mut vec_input = vec![];
|
||||
for (k, val) in allocs_input.iter() {
|
||||
for (k, val) in allocs_input.borrow_mut().iter() {
|
||||
match val {
|
||||
MalVal::Str(v) => {
|
||||
vec_input.push(bls12_381::Scalar::from_string(&v.to_string()));
|
||||
@@ -670,7 +679,7 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
|
||||
let result = groth16::verify_proof(verifying_key.as_ref().unwrap(), &proof, &vec_input);
|
||||
println!("vec public {:?}", vec_input);
|
||||
println!("result {:?}", result);
|
||||
|
||||
println!("Elapsed time: {:?}", start.elapsed());
|
||||
Ok(MalVal::Nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use bellman::{gadgets::Assignment, groth16, Circuit, ConstraintSystem, SynthesisError};
|
||||
use sapvi::bls_extensions::BlsStringConversion;
|
||||
use std::cell::RefCell;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use std::ops::{Add, AddAssign, MulAssign, SubAssign};
|
||||
use std::rc::Rc;
|
||||
//use std::collections::HashMap;
|
||||
use fnv::FnvHashMap;
|
||||
// use fnv::FnvHashMap;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::env::{env_bind, Env};
|
||||
@@ -35,10 +34,9 @@ pub struct VerifyKeyParams {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LispCircuit {
|
||||
pub params: FnvHashMap<String, MalVal>,
|
||||
pub allocs: FnvHashMap<String, MalVal>,
|
||||
pub alloc_inputs: FnvHashMap<String, MalVal>,
|
||||
// todo change this for a ordered data structure so enforce
|
||||
pub params: HashMap<String, MalVal>,
|
||||
pub allocs: HashMap<String, MalVal>,
|
||||
pub alloc_inputs: HashMap<String, MalVal>,
|
||||
pub constraints: Vec<EnforceAllocation>,
|
||||
}
|
||||
|
||||
@@ -51,7 +49,7 @@ pub enum MalVal {
|
||||
Sym(String),
|
||||
List(Rc<Vec<MalVal>>, Rc<MalVal>),
|
||||
Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
|
||||
Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
|
||||
Hash(Rc<HashMap<String, MalVal>>, Rc<MalVal>),
|
||||
Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
|
||||
MalFunc {
|
||||
eval: fn(ast: MalVal, env: Env) -> MalRet,
|
||||
@@ -62,7 +60,7 @@ pub enum MalVal {
|
||||
meta: Rc<MalVal>,
|
||||
},
|
||||
Atom(Rc<RefCell<MalVal>>),
|
||||
Zk(Rc<LispCircuit>), // TODO remote it
|
||||
Alloc(RefCell<HashMap<String, MalVal>>),
|
||||
Enforce(Rc<Vec<EnforceAllocation>>),
|
||||
ZKScalar(bls12_381::Scalar),
|
||||
}
|
||||
@@ -72,22 +70,22 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
self,
|
||||
cs: &mut CS,
|
||||
) -> Result<(), SynthesisError> {
|
||||
let mut variables: FnvHashMap<String, Variable> = FnvHashMap::default();
|
||||
let mut variables: HashMap<String, Variable> = HashMap::default();
|
||||
let mut params_const = self.params;
|
||||
|
||||
println!("Allocations\n");
|
||||
// println!("Allocations\n");
|
||||
for (k, v) in &self.allocs {
|
||||
match v {
|
||||
MalVal::ZKScalar(val) => {
|
||||
let var = cs.alloc(|| k, || Ok(*val))?;
|
||||
variables.insert(k.to_string(), var);
|
||||
println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
// println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
}
|
||||
MalVal::Str(val) => {
|
||||
let val_scalar = bls12_381::Scalar::from_string(&*val);
|
||||
let var = cs.alloc(|| k, || Ok(val_scalar))?;
|
||||
variables.insert(k.to_string(), var);
|
||||
println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
// println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
}
|
||||
_ => {
|
||||
println!("not allocated k {:?} v {:?}", k, v);
|
||||
@@ -95,19 +93,19 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
}
|
||||
}
|
||||
|
||||
println!("Allocations Input\n");
|
||||
// println!("Allocations Input\n");
|
||||
for (k, v) in &self.alloc_inputs {
|
||||
match v {
|
||||
MalVal::ZKScalar(val) => {
|
||||
let var = cs.alloc_input(|| k, || Ok(*val))?;
|
||||
variables.insert(k.to_string(), var);
|
||||
println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
// println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
}
|
||||
MalVal::Str(val) => {
|
||||
let val_scalar = bls12_381::Scalar::from_string(&*val);
|
||||
let var = cs.alloc_input(|| k, || Ok(val_scalar))?;
|
||||
variables.insert(k.to_string(), var);
|
||||
println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
// println!("k {:?} v {:?} var {:?}", k, v, var);
|
||||
}
|
||||
_ => {
|
||||
println!("not allocated k {:?} v {:?}", k, v);
|
||||
@@ -119,7 +117,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
let mut enforce_sorted = self.constraints.clone();
|
||||
enforce_sorted.sort_by(|a, b| a.idx.cmp(&b.idx));
|
||||
for alloc_value in enforce_sorted.iter() {
|
||||
println!("Enforce -> {:?}", alloc_value);
|
||||
// println!("Enforce -> {:?}", alloc_value);
|
||||
let coeff = bls12_381::Scalar::one();
|
||||
let mut left = bellman::LinearCombination::<Scalar>::zero();
|
||||
let mut right = bellman::LinearCombination::<Scalar>::zero();
|
||||
@@ -136,8 +134,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
left = left + (coeff.neg(), val_b);
|
||||
} else {
|
||||
if let Some(value) = params_const.get(a) {
|
||||
if let MalVal::ZKScalar(val) = value {
|
||||
left = left + (*val, val_b);
|
||||
match value {
|
||||
MalVal::ZKScalar(val) => {
|
||||
left = left + (*val, val_b);
|
||||
}
|
||||
MalVal::Str(s) => {
|
||||
let val = bls12_381::Scalar::from_string(&s.to_string());
|
||||
left = left + (val, val_b);
|
||||
}
|
||||
_ => { println!("not a valid param {:?}", value) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,8 +161,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
right = right + (coeff.neg(), val_b);
|
||||
} else {
|
||||
if let Some(value) = params_const.get(a) {
|
||||
if let MalVal::ZKScalar(val) = value {
|
||||
right = right + (*val, val_b);
|
||||
match value {
|
||||
MalVal::ZKScalar(val) => {
|
||||
right = right + (*val, val_b);
|
||||
}
|
||||
MalVal::Str(s) => {
|
||||
let val = bls12_381::Scalar::from_string(&s.to_string());
|
||||
right = right + (val, val_b);
|
||||
}
|
||||
_ => { println!("not a valid param {:?}", value) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,8 +189,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
|
||||
output = output + (coeff.neg(), val_b);
|
||||
} else {
|
||||
if let Some(value) = params_const.get(a) {
|
||||
if let MalVal::ZKScalar(val) = value {
|
||||
output = output + (*val, val_b);
|
||||
match value {
|
||||
MalVal::ZKScalar(val) => {
|
||||
output = output + (*val, val_b);
|
||||
}
|
||||
MalVal::Str(s) => {
|
||||
let val = bls12_381::Scalar::from_string(&s.to_string());
|
||||
output = output + (val, val_b);
|
||||
}
|
||||
_ => { println!("not a valid param {:?}", value) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -380,7 +399,7 @@ pub fn func(f: fn(MalArgs) -> MalRet) -> MalVal {
|
||||
Func(f, Rc::new(Nil))
|
||||
}
|
||||
|
||||
pub fn _assoc(mut hm: FnvHashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
|
||||
pub fn _assoc(mut hm: HashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
|
||||
if kvs.len() % 2 != 0 {
|
||||
return error("odd number of elements");
|
||||
}
|
||||
@@ -395,7 +414,7 @@ pub fn _assoc(mut hm: FnvHashMap<String, MalVal>, kvs: MalArgs) -> MalRet {
|
||||
Ok(Hash(Rc::new(hm), Rc::new(Nil)))
|
||||
}
|
||||
|
||||
pub fn _dissoc(mut hm: FnvHashMap<String, MalVal>, ks: MalArgs) -> MalRet {
|
||||
pub fn _dissoc(mut hm: HashMap<String, MalVal>, ks: MalArgs) -> MalRet {
|
||||
for k in ks.iter() {
|
||||
match k {
|
||||
Str(ref s) => {
|
||||
@@ -408,6 +427,6 @@ pub fn _dissoc(mut hm: FnvHashMap<String, MalVal>, ks: MalArgs) -> MalRet {
|
||||
}
|
||||
|
||||
pub fn hash_map(kvs: MalArgs) -> MalRet {
|
||||
let hm: FnvHashMap<String, MalVal> = FnvHashMap::default();
|
||||
let hm: HashMap<String, MalVal> = HashMap::default();
|
||||
_assoc(hm, kvs)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash -x
|
||||
python scripts/preprocess.py proofs/mimc.psm > /tmp/mimc.psm || exit $?
|
||||
python scripts/compile.py --supervisor /tmp/mimc.psm --output mimc.zcd || exit $?
|
||||
python3 scripts/preprocess.py proofs/mimc.psm > /tmp/mimc.psm || exit $?
|
||||
python3 scripts/compile.py --supervisor /tmp/mimc.psm --output mimc.zcd || exit $?
|
||||
cargo run --release --bin mimc
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash -x
|
||||
python scripts/preprocess.py proofs/mimc.psm > /tmp/mimc.psm || exit $?
|
||||
python scripts/compile.py --supervisor /tmp/mimc.psm --output mimc.zcd || exit $?
|
||||
python3 scripts/preprocess.py proofs/mimc.psm > /tmp/mimc.psm || exit $?
|
||||
python3 scripts/compile.py --supervisor /tmp/mimc.psm --output mimc.zcd || exit $?
|
||||
cargo run --release --bin zkvm -- init mimc.zcd mimc.zts
|
||||
cargo run --release --bin zkvm -- prove mimc.zcd mimc.zts proofs/mimc.params mimc.prf
|
||||
cargo run --release --bin zkvm -- verify mimc.zcd mimc.zts mimc.prf
|
||||
|
||||
@@ -94,6 +94,9 @@ fn main() -> Result<()> {
|
||||
0x015d_8c7f_5b43_fe33,
|
||||
]);
|
||||
|
||||
println!("----> {:?}", left);
|
||||
println!("----> {:?}", right);
|
||||
|
||||
contract.set_param("left_0", left.clone())?;
|
||||
contract.set_param("right", right.clone())?;
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use std::sync::Arc;
|
||||
use crate::net::error::{NetError, NetResult};
|
||||
use crate::net::sessions::{InboundSession, OutboundSession, SeedSession};
|
||||
use crate::net::{Channel, ChannelPtr, Hosts, HostsPtr, Settings, SettingsPtr};
|
||||
use crate::net::messages::Message;
|
||||
use crate::system::{Subscriber, SubscriberPtr, Subscription};
|
||||
|
||||
/// List of channels that are awaiting connection.
|
||||
@@ -17,7 +18,7 @@ pub type ConnectedChannels<T> = Mutex<HashMap<SocketAddr, Arc<T>>>;
|
||||
/// Atomic pointer to p2p interface.
|
||||
pub type P2pPtr = Arc<P2p>;
|
||||
|
||||
/// Top level peer-to-peer networking interface.
|
||||
/// Top level peer-to-peer networking interface.
|
||||
pub struct P2p {
|
||||
pending: PendingChannels,
|
||||
channels: ConnectedChannels<Channel>,
|
||||
@@ -78,6 +79,14 @@ impl P2p {
|
||||
debug!(target: "net", "P2p::run() [BEGIN]");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn broadcast<M: Message + Clone>(&self, message: M) -> NetResult<()> {
|
||||
for channel in self.channels.lock().await.values() {
|
||||
channel.send(message.clone()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add channel address to the list of connected channels.
|
||||
pub async fn store(&self, channel: ChannelPtr) {
|
||||
self.channels
|
||||
@@ -126,7 +135,7 @@ impl P2p {
|
||||
pub async fn subscribe_channel(&self) -> Subscription<NetResult<ChannelPtr>> {
|
||||
self.channel_subscriber.clone().subscribe().await
|
||||
}
|
||||
|
||||
|
||||
/// Stop a subscription.
|
||||
pub async fn subscribe_stop(&self) -> Subscription<NetError> {
|
||||
self.stop_subscriber.clone().subscribe().await
|
||||
|
||||
@@ -4,9 +4,16 @@ use rand::thread_rng;
|
||||
// For benchmarking
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
// from string scalar
|
||||
use sapvi::bls_extensions::BlsStringConversion;
|
||||
|
||||
// Bring in some tools for using finite fiels
|
||||
use ff::{Field, PrimeField};
|
||||
|
||||
// mimc constants
|
||||
mod mimc_constants;
|
||||
use mimc_constants::mimc_constants;
|
||||
|
||||
// We're going to use the BLS12-381 pairing-friendly elliptic curve.
|
||||
use bls12_381::{Bls12, Scalar};
|
||||
|
||||
@@ -49,6 +56,20 @@ fn mimc<Scalar: PrimeField>(mut xl: Scalar, mut xr: Scalar, constants: &[Scalar]
|
||||
xl
|
||||
}
|
||||
|
||||
macro_rules! from_slice {
|
||||
($data:expr, $len:literal) => {{
|
||||
let mut array = [0; $len];
|
||||
// panics if not enough data
|
||||
let bytes = &$data[..array.len()];
|
||||
assert_eq!(bytes.len(), array.len());
|
||||
for (a, b) in array.iter_mut().rev().zip(bytes.iter()) {
|
||||
*a = *b;
|
||||
}
|
||||
//array.copy_from_slice(bytes.iter().rev());
|
||||
array
|
||||
}};
|
||||
}
|
||||
|
||||
/// This is our demo circuit for proving knowledge of the
|
||||
/// preimage of a MiMC hash invocation.
|
||||
struct MiMCDemo<'a, Scalar: PrimeField> {
|
||||
@@ -84,9 +105,13 @@ impl<'a, Scalar: PrimeField> Circuit<Scalar> for MiMCDemo<'a, Scalar> {
|
||||
|
||||
// tmp = (xL + Ci)^2
|
||||
let tmp_value = xl_value.map(|mut e| {
|
||||
println!("{:?}", e);
|
||||
e.add_assign(&self.constants[i]);
|
||||
e.square()
|
||||
});
|
||||
|
||||
// println!("tmp_value {:?} {:?}", self.constants[i], tmp_value);
|
||||
|
||||
let tmp = cs.alloc(
|
||||
|| "tmp",
|
||||
|| tmp_value.ok_or(SynthesisError::AssignmentMissing),
|
||||
@@ -130,6 +155,10 @@ impl<'a, Scalar: PrimeField> Circuit<Scalar> for MiMCDemo<'a, Scalar> {
|
||||
|lc| lc + new_xl - xr,
|
||||
);
|
||||
|
||||
println!("{:?}", i);
|
||||
println!("{:?} {:?}", xl_value, xr_value);
|
||||
println!("{:?}", new_xl_value);
|
||||
|
||||
// xR = xL
|
||||
xr = xl;
|
||||
xr_value = xl_value;
|
||||
@@ -146,10 +175,19 @@ impl<'a, Scalar: PrimeField> Circuit<Scalar> for MiMCDemo<'a, Scalar> {
|
||||
fn main() {
|
||||
use rand::rngs::OsRng;
|
||||
|
||||
// Generate the MiMC round constants
|
||||
let constants = (0..MIMC_ROUNDS)
|
||||
.map(|_| Scalar::random(&mut OsRng))
|
||||
.collect::<Vec<_>>();
|
||||
// // Generate the MiMC round constants
|
||||
// let constants = (0..MIMC_ROUNDS)
|
||||
// .map(|_| Scalar::random(&mut OsRng))
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
let mut constants = Vec::new();
|
||||
for const_str in mimc_constants() {
|
||||
let bytes = from_slice!(&hex::decode(const_str).unwrap(), 32);
|
||||
assert_eq!(bytes.len(), 32);
|
||||
let constant = Scalar::from_bytes(&bytes).unwrap();
|
||||
|
||||
constants.push(constant);
|
||||
}
|
||||
|
||||
println!("Creating parameters...");
|
||||
|
||||
@@ -170,7 +208,7 @@ fn main() {
|
||||
println!("Creating proofs...");
|
||||
|
||||
// Let's benchmark stuff!
|
||||
const SAMPLES: u32 = 10;
|
||||
const SAMPLES: u32 = 1;
|
||||
let mut total_proving = Duration::new(0, 0);
|
||||
let mut total_verifying = Duration::new(0, 0);
|
||||
|
||||
@@ -180,8 +218,10 @@ fn main() {
|
||||
|
||||
for _ in 0..SAMPLES {
|
||||
// Generate a random preimage and compute the image
|
||||
let xl = Scalar::random(&mut OsRng);
|
||||
let xr = Scalar::random(&mut OsRng);
|
||||
// let xl = Scalar::random(&mut OsRng);
|
||||
// let xr = Scalar::random(&mut OsRng);
|
||||
let xl = bls12_381::Scalar::from_string("15a36d1f0f390d8852a35a8c1908dd87a361ee3fd48fdf77b9819dc82d90607e");
|
||||
let xr = bls12_381::Scalar::from_string("015d8c7f5b43fe33f7891142c001d9251f3abeeb98fad3e87b0dc53c4ebf1891");
|
||||
let image = mimc(xl, xr, &constants);
|
||||
|
||||
proof_vec.truncate(0);
|
||||
|
||||
Reference in New Issue
Block a user