Skip to content

Commit

Permalink
Merging the develop branch into the main branch (rs v1.4.0, node v1.1…
Browse files Browse the repository at this point in the history
….0, wasm v1.4.0)

This merge contains the following set of changes:
- Pick up recent update of fawkes-crypto to add ability to preparse gates(#60)
- Bump versions `libzkbob-rs` to `v1.4.0`, `libzkbob-rs-node` to `v1.1.0`, `libzkbob-rs-wasm` to `v1.4.0` (#65)
  • Loading branch information
akolotov authored May 26, 2023
2 parents 921841b + ab27fd0 commit e6a3438
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 38 deletions.
6 changes: 3 additions & 3 deletions libzkbob-rs-node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libzkbob-rs-node"
version = "1.0.0"
version = "1.1.0"
authors = ["Dmitry Vdovin <[email protected]>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -11,7 +11,7 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libzkbob-rs = { version = "1.0.0", features = ["native"] }
libzkbob-rs = { version = "1.4.0", features = ["native"] }
#libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
neon = { version = "0.10.0", default-features = false, features = ["channel-api", "napi-6", "promise-api", "task-api"] }
# FIXME: Using a random fork for now
Expand All @@ -23,5 +23,5 @@ hex = "0.4.3"
git = "https://github.com/zkbob/fawkes-crypto"
branch = "master"
package = "fawkes-crypto-zkbob"
version = "4.5.0"
version = "4.6.0"
features = ["multicore"]
4 changes: 2 additions & 2 deletions libzkbob-rs-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export interface VK {
}

declare class Params {
static fromBinary(data: Buffer): Params;
static fromFile(path: string): Params;
static fromBinary(data: Buffer, precompute: boolean): Params;
static fromFile(path: string, precompute: boolean): Params;
}

declare class Proof {
Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libzkbob-rs-node",
"version": "1.0.0",
"version": "1.1.0",
"description": "Neon version of libzkbob-rs",
"main": "index.js",
"types": "index.d.ts",
Expand Down
15 changes: 9 additions & 6 deletions libzkbob-rs-node/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::path::PathBuf;
use std::sync::Arc;

use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::Parameters;
use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::{Parameters, PrecomputedData};
use neon::{prelude::*, types::buffer::TypedArray};

use crate::Engine;
use crate::{Engine, Fr};

pub type BoxedParams = JsBox<Arc<Params>>;
pub struct Params {
pub inner: Parameters<Engine>,
pub precomputed: Option<PrecomputedData<Fr>>,
}

pub fn from_binary(mut cx: FunctionContext) -> JsResult<BoxedParams> {
let input = cx.argument::<JsBuffer>(0)?;

let mut data = input.as_slice(&cx);
let inner = Parameters::read(&mut data, true, true).unwrap();

Ok(cx.boxed(Arc::new(Params { inner })))
let precompute = cx.argument::<JsBoolean>(1)?.value(&mut cx);
let precomputed = precompute.then(|| inner.precompute());
Ok(cx.boxed(Arc::new(Params { inner, precomputed })))
}

pub fn from_file(mut cx: FunctionContext) -> JsResult<BoxedParams> {
Expand All @@ -28,8 +30,9 @@ pub fn from_file(mut cx: FunctionContext) -> JsResult<BoxedParams> {

let data = std::fs::read(path).unwrap();
let inner = Parameters::read(&mut data.as_slice(), true, true).unwrap();

Ok(cx.boxed(Arc::new(Params { inner })))
let precompute = cx.argument::<JsBoolean>(1)?.value(&mut cx);
let precomputed = precompute.then(|| inner.precompute());
Ok(cx.boxed(Arc::new(Params { inner, precomputed })))
}

impl Finalize for Params {}
53 changes: 44 additions & 9 deletions libzkbob-rs-node/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::verifier:
use libzkbob_rs::libzeropool::fawkes_crypto::ff_uint::Num;
use libzkbob_rs::libzeropool::POOL_PARAMS;
use libzkbob_rs::proof::{
prove_delegated_deposit as prove_delegated_deposit_native, prove_tree as prove_tree_native,
prove_delegated_deposit as prove_delegated_deposit_native,
prove_tree as prove_tree_native,
prove_tx as prove_tx_native,
};
use neon::prelude::*;
Expand All @@ -31,7 +32,13 @@ pub fn prove_tx_async(mut cx: FunctionContext) -> JsResult<JsPromise> {

let promise = cx
.task(move || {
let (inputs, proof) = prove_tx_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let (inputs, proof) = prove_tx_native(
&params.inner,
&*POOL_PARAMS,
tr_pub,
tr_sec,
&params.precomputed,
);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
Expand All @@ -50,7 +57,13 @@ pub fn prove_tree_async(mut cx: FunctionContext) -> JsResult<JsPromise> {

let promise = cx
.task(move || {
let (inputs, proof) = prove_tree_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let (inputs, proof) = prove_tree_native(
&params.inner,
&*POOL_PARAMS,
tr_pub,
tr_sec,
&params.precomputed,
);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
Expand All @@ -69,8 +82,13 @@ pub fn prove_delegated_deposit_async(mut cx: FunctionContext) -> JsResult<JsProm

let promise = cx
.task(move || {
let (inputs, proof) =
prove_delegated_deposit_native(&params.inner, &*POOL_PARAMS, d_pub, d_sec);
let (inputs, proof) = prove_delegated_deposit_native(
&params.inner,
&*POOL_PARAMS,
d_pub,
d_sec,
&params.precomputed,
);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
Expand All @@ -88,7 +106,13 @@ pub fn prove_tx(mut cx: FunctionContext) -> JsResult<JsValue> {
let tr_pub = neon_serde::from_value(&mut cx, tr_pub_js).unwrap();
let tr_sec = neon_serde::from_value(&mut cx, tr_sec_js).unwrap();

let pair = prove_tx_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let pair = prove_tx_native(
&params.inner,
&*POOL_PARAMS,
tr_pub,
tr_sec,
&params.precomputed,
);

let proof = SnarkProof {
inputs: pair.0,
Expand All @@ -108,7 +132,13 @@ pub fn prove_tree(mut cx: FunctionContext) -> JsResult<JsValue> {
let tr_pub = neon_serde::from_value(&mut cx, tr_pub_js).unwrap();
let tr_sec = neon_serde::from_value(&mut cx, tr_sec_js).unwrap();

let pair = prove_tree_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let pair = prove_tree_native(
&params.inner,
&*POOL_PARAMS,
tr_pub,
tr_sec,
&params.precomputed,
);

let proof = SnarkProof {
inputs: pair.0,
Expand All @@ -128,8 +158,13 @@ pub fn prove_delegated_deposit(mut cx: FunctionContext) -> JsResult<JsValue> {
let d_pub = neon_serde::from_value(&mut cx, d_pub_js).unwrap();
let d_sec = neon_serde::from_value(&mut cx, d_sec_js).unwrap();

let (inputs, proof) =
prove_delegated_deposit_native(&params.inner, &*POOL_PARAMS, d_pub, d_sec);
let (inputs, proof) = prove_delegated_deposit_native(
&params.inner,
&*POOL_PARAMS,
d_pub,
d_sec,
&params.precomputed,
);

let proof = SnarkProof { inputs, proof };

Expand Down
4 changes: 2 additions & 2 deletions libzkbob-rs-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs-wasm"
description = "A higher level zkBob API for Wasm"
version = "1.3.0"
version = "1.4.0"
authors = ["Dmitry Vdovin <[email protected]>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -57,7 +57,7 @@ web-sys = "0.3.61"
git = "https://github.com/zkBob/fawkes-crypto"
branch = "master"
package = "fawkes-crypto-zkbob"
version = "4.5.0"
version = "4.6.0"
features = ["wasm", "serde_support"]


Expand Down
28 changes: 23 additions & 5 deletions libzkbob-rs-wasm/src/params.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use fawkes_crypto::backend::bellman_groth16::PrecomputedData;
use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::Parameters;
use wasm_bindgen::prelude::*;

use crate::Engine;
use crate::{Engine, Fr};

#[wasm_bindgen]
pub struct Params {
#[wasm_bindgen(skip)]
pub inner: Parameters<Engine>,
#[wasm_bindgen(skip)]
pub precomputed: Option<PrecomputedData<Fr>>
}

impl From<Parameters<Engine>> for Params {
fn from(params: Parameters<Engine>) -> Self {
Params { inner: params }
Params { inner: params, precomputed: None }
}
}

Expand All @@ -25,14 +28,29 @@ impl From<Params> for Parameters<Engine> {
impl Params {
#[wasm_bindgen(js_name = "fromBinary")]
pub fn from_binary(input: &[u8]) -> Result<Params, JsValue> {
Self::from_binary_ext(input, true, true)
Self::from_binary_ext(input, true, true, false)
}

#[wasm_bindgen(js_name = "fromBinaryExtended")]
pub fn from_binary_ext(input: &[u8], disallow_points_at_infinity: bool, checked: bool) -> Result<Params, JsValue> {
pub fn from_binary_ext(input: &[u8], disallow_points_at_infinity: bool, checked: bool, precompute: bool) -> Result<Params, JsValue> {
let mut input = input;
let inner = Parameters::read(&mut input, disallow_points_at_infinity, checked).map_err(|err| js_err!("{}", err))?;
let mut precomputed = None;

if precompute {
if let Ok(precompute_memory_size) = inner.precompute_memory_size() {
{
// WebAssembly.Memory.grow(..) is extremely slow on iOS
// so it's much better to allocate necessary memory with one call
// than to do it multiple times in precompute.
let mut v: Vec<u8> = Vec::new();
v.reserve(precompute_memory_size);
v.shrink_to_fit()
}
precomputed = Some(inner.precompute());
}
}

Ok(Params { inner })
Ok(Params { inner, precomputed })
}
}
7 changes: 6 additions & 1 deletion libzkbob-rs-wasm/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fawkes_crypto::backend::bellman_groth16::prover::prove_precomputed;
use libzkbob_rs::libzeropool::{
circuit::tree::tree_update,
circuit::tx::c_transfer,
Expand Down Expand Up @@ -45,6 +46,7 @@ impl Proof {
transfer_pub: ts_types::TransferPub,
transfer_sec: ts_types::TransferSec,
) -> Result<crate::ts_types::Proof, JsValue> {
let precomputed = &params.precomputed;
let params = &params.inner;

let public: NativeTransferPub<_> =
Expand All @@ -56,7 +58,10 @@ impl Proof {
c_transfer(&public, &secret, &*POOL_PARAMS);
};

let (inputs, snark_proof) = prove(params, &public, &secret, circuit);
let (inputs, snark_proof) = match precomputed {
Some(precomputed) => prove_precomputed(params, &public, &secret, circuit, precomputed),
None => prove(params, &public, &secret, circuit)
};

let proof = Proof {
inputs,
Expand Down
4 changes: 2 additions & 2 deletions libzkbob-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs"
description = "A higher level zkBob API"
version = "1.3.0"
version = "1.4.0"
authors = ["Dmitry Vdovin <[email protected]>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -26,7 +26,7 @@ hex = { version = "0.4.3", features = ["serde"] }
git = "https://github.com/zkbob/libzeropool-zkbob"
branch = "master"
package = "libzeropool-zkbob"
version = "1.2.0"
version = "1.3.0"
default-features = false
features = ["in3out127"]

Expand Down
31 changes: 24 additions & 7 deletions libzkbob-rs/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use libzeropool::{
},
fawkes_crypto::{
backend::bellman_groth16::engines::Engine,
backend::bellman_groth16::prover::{prove, Proof},
backend::bellman_groth16::Parameters,
backend::bellman_groth16::{prover::{prove, Proof}, PrecomputedData},
backend::bellman_groth16::{Parameters, prover::prove_precomputed},
ff_uint::Num,
},
native::{
Expand All @@ -21,6 +21,7 @@ pub fn prove_tx<P, E>(
pool_params: &P,
transfer_pub: TransferPub<E::Fr>,
transfer_sec: TransferSec<E::Fr>,
precomputed: &Option<PrecomputedData<E::Fr>>,
) -> (Vec<Num<E::Fr>>, Proof<E>)
where
P: PoolParams<Fr = E::Fr>,
Expand All @@ -30,14 +31,20 @@ where
c_transfer(&public, &secret, pool_params);
};

prove(params, &transfer_pub, &transfer_sec, circuit)
match precomputed {
Some(precomputed) => {
prove_precomputed(params, &transfer_pub, &transfer_sec, circuit, precomputed)
},
None => prove(params, &transfer_pub, &transfer_sec, circuit)
}
}

pub fn prove_tree<P, E>(
params: &Parameters<E>,
pool_params: &P,
tree_pub: TreePub<E::Fr>,
tree_sec: TreeSec<E::Fr>,
precomputed: &Option<PrecomputedData<E::Fr>>,
) -> (Vec<Num<E::Fr>>, Proof<E>)
where
P: PoolParams<Fr = E::Fr>,
Expand All @@ -47,14 +54,20 @@ where
tree_update(&public, &secret, pool_params);
};

prove(params, &tree_pub, &tree_sec, circuit)
match precomputed {
Some(precomputed) => {
prove_precomputed(params, &tree_pub, &tree_sec, circuit, precomputed)
},
None => prove(params, &tree_pub, &tree_sec, circuit)
}
}

pub fn prove_delegated_deposit<P, E>(
params: &Parameters<E>,
pool_params: &P,
d_pub: DelegatedDepositBatchPub<E::Fr>,
d_sec: DelegatedDepositBatchSec<E::Fr>,
precomputed: &Option<PrecomputedData<E::Fr>>,
) -> (Vec<Num<E::Fr>>, Proof<E>)
where
P: PoolParams<Fr = E::Fr>,
Expand All @@ -64,6 +77,10 @@ where
check_delegated_deposit_batch(&public, &secret, pool_params);
};

prove(params, &d_pub, &d_sec, circuit)
}

match precomputed {
Some(precomputed) => {
prove_precomputed(params, &d_pub, &d_sec, circuit, precomputed)
},
None => prove(params, &d_pub, &d_sec, circuit)
}
}

0 comments on commit e6a3438

Please sign in to comment.