Skip to content

Commit

Permalink
Add range proof protocols
Browse files Browse the repository at this point in the history
Signed-off-by: lovesh <lovesh.bond@gmail.com>
  • Loading branch information
lovesh committed Oct 2, 2023
1 parent 56f4723 commit 94102ac
Show file tree
Hide file tree
Showing 25 changed files with 1,700 additions and 896 deletions.
159 changes: 98 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "dock_crypto_wasm"
repository = "https://github.com/docknetwork/crypto-wasm"
version = "0.16.0"
version = "0.17.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -25,16 +25,19 @@ wasm-bindgen = "= 0.2.86"
dlmalloc = { version = "0.2.4", features = ["global"], optional = true }
serde_with = { version = "1.10.0", default-features = false, features = ["macros"] }

bbs_plus = { version = "0.16.0", default-features = false }
vb_accumulator = { version = "0.17.0", default-features = false }
schnorr_pok = { version = "0.14.0", default-features = false }
proof_system = { version = "0.22.0", default-features = false }
coconut-crypto = { version = "0.5.0", default-features = false }
dock_crypto_utils = { version = "0.15.0", default-features = false }
saver = { version = "0.13.0", default-features = false }
legogroth16 = { version = "0.10.0", default-features = false, features = ["circom", "wasmer-js"] }
secret_sharing_and_dkg = { version = "0.7.0", default-features = false }
oblivious_transfer_protocols = { version = "0.3.0", default-features = false}
bbs_plus = { version = "0.17.0", default-features = false }
vb_accumulator = { version = "0.18.0", default-features = false }
schnorr_pok = { version = "0.15.0", default-features = false }
proof_system = { version = "0.23.0", default-features = false }
coconut-crypto = { version = "0.6.0", default-features = false }
dock_crypto_utils = { version = "0.16.0", default-features = false }
saver = { version = "0.14.0", default-features = false }
legogroth16 = { version = "0.11.0", default-features = false, features = ["circom", "wasmer-js"] }
secret_sharing_and_dkg = { version = "0.8.0", default-features = false }
oblivious_transfer_protocols = { version = "0.4.0", default-features = false}
bulletproofs_plus_plus = { version = "0.1.0", default-features = false}
smc_range_proof = { version = "0.1.0", default-features = false}


ark-ec = { version = "^0.4.0", default-features = false }
ark-ff = { version = "^0.4.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/crypto-wasm",
"version": "0.21.0",
"version": "0.22.0",
"author": "Dock.io",
"license": "Apache-2.0",
"private": false,
Expand Down
137 changes: 133 additions & 4 deletions src/bound_check.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
use ark_bls12_381::Bls12_381;
use proof_system::prelude::generate_snark_srs_bound_check;
use blake2::Blake2b512;
use bulletproofs_plus_plus::prelude::SetupParams;
use js_sys::Uint8Array;
use proof_system::prelude::{
bound_check_smc::SmcParamsAndCommitmentKey,
bound_check_smc_with_kv::SmcParamsAndCommitmentKeyAndSecretKey, generate_snark_srs_bound_check,
};

use crate::utils::{get_seeded_rng, set_panic_hook};
use crate::{
utils::{get_seeded_rng, set_panic_hook},
G1Affine,
};
use wasm_bindgen::prelude::*;
use zeroize::Zeroize;

/// Setup snark and generate compressed or uncompressed SNARK proving key
pub(crate) type BppSetupParams = SetupParams<G1Affine>;
pub(crate) type SmcParams = SmcParamsAndCommitmentKey<Bls12_381>;
pub(crate) type SmcParamsAndSk = SmcParamsAndCommitmentKeyAndSecretKey<Bls12_381>;

/// Setup snark for proving bounds and and generate compressed or uncompressed SNARK proving key
#[wasm_bindgen(js_name = boundCheckSnarkSetup)]
pub fn bound_check_snark_setup(return_uncompressed: bool) -> Result<js_sys::Uint8Array, JsValue> {
pub fn bound_check_snark_setup(return_uncompressed: bool) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let mut rng = get_seeded_rng();
let snark_pk = generate_snark_srs_bound_check::<Bls12_381, _>(&mut rng).map_err(|e| {
Expand All @@ -22,3 +35,119 @@ pub fn bound_check_snark_setup(return_uncompressed: bool) -> Result<js_sys::Uint
obj_to_uint8array!(&snark_pk, false, "LegoProvingKey")
})
}

/// Create setup params for Bulletproofs++ range proofs
#[wasm_bindgen(js_name = boundCheckBppSetup)]
pub fn bound_check_bpp_setup(
label: Vec<u8>,
base: u16,
value_bit_size: u16,
return_uncompressed: bool,
) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let bpp_setup_params = BppSetupParams::new_for_arbitrary_range_proof::<Blake2b512>(
&label,
base,
value_bit_size,
1,
);
Ok(if return_uncompressed {
obj_to_uint8array_uncompressed!(&bpp_setup_params, "Bulletproofs++SetupParams")
} else {
obj_to_uint8array!(&bpp_setup_params, false, "Bulletproofs++SetupParams")
})
}

/// Create setup params for set-membership check based range proofs
#[wasm_bindgen(js_name = boundCheckSmcSetup)]
pub fn bound_check_smc_setup(
label: Vec<u8>,
base: u16,
return_uncompressed: bool,
) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let mut rng = get_seeded_rng();
let (smc_setup_params, _) = SmcParams::new::<_, Blake2b512>(&mut rng, &label, base);
smc_setup_params
.verify()
.map_err(|e| JsValue::from(&format!("Param validation failed with error: {:?}", e)))?;
Ok(if return_uncompressed {
obj_to_uint8array_uncompressed!(&smc_setup_params, "SmcParamsAndCommitmentKey")
} else {
obj_to_uint8array!(&smc_setup_params, false, "SmcParamsAndCommitmentKey")
})
}

/// Create setup params for set-membership check based range proofs supporting keyed-verification
#[wasm_bindgen(js_name = boundCheckSmcWithKVSetup)]
pub fn bound_check_smc_with_kv_setup(
label: Vec<u8>,
base: u16,
return_uncompressed: bool,
) -> Result<js_sys::Array, JsValue> {
set_panic_hook();
let mut rng = get_seeded_rng();
let (smc_setup_params, sk) = SmcParams::new::<_, Blake2b512>(&mut rng, &label, base);
smc_setup_params
.verify()
.map_err(|e| JsValue::from(&format!("Param validation failed with error: {:?}", e)))?;
let setup = js_sys::Array::new();
let smc_params = if return_uncompressed {
obj_to_uint8array_uncompressed!(&smc_setup_params, "SmcParamsAndCommitmentKey")
} else {
obj_to_uint8array!(&smc_setup_params, false, "SmcParamsAndCommitmentKey")
};
let smc_setup_params_with_sk = SmcParamsAndSk {
params_and_comm_key: smc_setup_params,
sk,
};
let smc_params_with_sk = if return_uncompressed {
obj_to_uint8array_uncompressed!(
&smc_setup_params_with_sk,
"SmcParamsAndCommitmentKeyAndSecretKey"
)
} else {
obj_to_uint8array!(
&smc_setup_params_with_sk,
false,
"SmcParamsAndCommitmentKeyAndSecretKey"
)
};
setup.push(&smc_params);
setup.push(&smc_params_with_sk);
Ok(setup)
}

/// Decompress EC points in Bulletproofs++ setup
#[wasm_bindgen(js_name = decompressBppParams)]
pub fn decompress_bpp_params(params: Uint8Array) -> Result<Uint8Array, JsValue> {
let params = obj_from_uint8array!(BppSetupParams, params, false, "Bulletproofs++SetupParams");
Ok(obj_to_uint8array_uncompressed!(
&params,
"Bulletproofs++SetupParams"
))
}

/// Decompress EC points in set-membership based range proof
#[wasm_bindgen(js_name = decompressSmcParams)]
pub fn decompress_smc_params(params: Uint8Array) -> Result<Uint8Array, JsValue> {
let params = obj_from_uint8array!(SmcParams, params, false, "SmcParamsAndCommitmentKey");
Ok(obj_to_uint8array_uncompressed!(
&params,
"SmcParamsAndCommitmentKey"
))
}

#[wasm_bindgen(js_name = decompressSmcParamsAndSk)]
pub fn decompress_smc_params_and_sk(params: Uint8Array) -> Result<Uint8Array, JsValue> {
let params = obj_from_uint8array!(
SmcParamsAndSk,
params,
false,
"SmcParamsAndCommitmentKeyAndSecretKey"
);
Ok(obj_to_uint8array_uncompressed!(
&params,
"SmcParamsAndCommitmentKeyAndSecretKey"
))
}
26 changes: 25 additions & 1 deletion src/composite_proof_system/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod setup_params;
pub mod statement;
pub mod statements;

use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -324,6 +324,30 @@ pub fn generate_r1cs_circom_witness(
serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)
}

#[wasm_bindgen(js_name = generateBoundCheckBppWitness)]
pub fn generate_bound_check_bpp_witness(message: Uint8Array) -> Result<JsValue, JsValue> {
set_panic_hook();
let message = fr_from_uint8_array(message, true)?;
let witness = Witness::BoundCheckBpp(message);
serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)
}

#[wasm_bindgen(js_name = generateBoundCheckSmcWitness)]
pub fn generate_bound_check_smc_witness(message: Uint8Array) -> Result<JsValue, JsValue> {
set_panic_hook();
let message = fr_from_uint8_array(message, true)?;
let witness = Witness::BoundCheckSmc(message);
serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)
}

#[wasm_bindgen(js_name = generateBoundCheckSmcWithKVWitness)]
pub fn generate_bound_check_smc_with_kv_witness(message: Uint8Array) -> Result<JsValue, JsValue> {
set_panic_hook();
let message = fr_from_uint8_array(message, true)?;
let witness = Witness::BoundCheckSmcWithKV(message);
serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)
}

pub fn parse_statements_meta_statements_and_setup_params<G: AffineRepr>(
statements: js_sys::Array,
meta_statements: js_sys::Array,
Expand Down
67 changes: 67 additions & 0 deletions src/composite_proof_system/setup_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
accumulator::{AccumPk, AccumSetupParams, MembershipPrk, NonMembershipPrk},
bbs::BBSSigParams,
bbs_plus::{BBSPlusPublicKeyG2, BBSPlusSigParamsG1},
bound_check::{BppSetupParams, SmcParams, SmcParamsAndSk},
legosnark::{LegoProvingKey, LegoVerifyingKey},
ps::{PSPublicKey, PSSignatureParams},
r1cs::gen_r1cs,
Expand Down Expand Up @@ -353,3 +354,69 @@ pub fn generate_setup_param_for_field_elem_vec(
js_array_to_fr_vec(&arr)?
)))
}

#[wasm_bindgen(js_name = generateSetupParamForBppParams)]
pub fn generate_setup_param_for_bpp_params(
params: js_sys::Uint8Array,
uncompressed: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let params = if uncompressed {
obj_from_uint8array_uncompressed!(BppSetupParams, params, "Bulletproofs++SetupParams")
} else {
obj_from_uint8array!(BppSetupParams, params, false, "Bulletproofs++SetupParams")
};
Ok(obj_to_uint8array_uncompressed!(&SetupParams::<
Bls12_381,
G1Affine,
>::BppSetupParams(
params
)))
}

#[wasm_bindgen(js_name = generateSetupParamForSmcParams)]
pub fn generate_setup_param_for_smc_params(
params: js_sys::Uint8Array,
uncompressed: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let params = if uncompressed {
obj_from_uint8array_uncompressed!(SmcParams, params, "SmcParamsAndCommitmentKey")
} else {
obj_from_uint8array!(SmcParams, params, false, "SmcParamsAndCommitmentKey")
};
Ok(obj_to_uint8array_uncompressed!(&SetupParams::<
Bls12_381,
G1Affine,
>::SmcParamsAndCommKey(
params
)))
}

#[wasm_bindgen(js_name = generateSetupParamForSmcParamsAndSk)]
pub fn generate_setup_param_for_smc_params_and_sk(
params: js_sys::Uint8Array,
uncompressed: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let params = if uncompressed {
obj_from_uint8array_uncompressed!(
SmcParamsAndSk,
params,
"SmcParamsAndCommitmentKeyAndSecretKey"
)
} else {
obj_from_uint8array!(
SmcParamsAndSk,
params,
false,
"SmcParamsAndCommitmentKeyAndSecretKey"
)
};
Ok(obj_to_uint8array_uncompressed!(&SetupParams::<
Bls12_381,
G1Affine,
>::SmcParamsAndCommKeyAndSk(
params
)))
}
Loading

0 comments on commit 94102ac

Please sign in to comment.