Skip to content

Commit

Permalink
Add zero-knowledge inequality predicate
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 10, 2023
1 parent 94102ac commit e20fc4d
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 36 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ 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.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 }
bbs_plus = { version = "0.18.0", default-features = false }
vb_accumulator = { version = "0.19.0", default-features = false }
schnorr_pok = { version = "0.16.0", default-features = false }
proof_system = { version = "0.24.0", default-features = false }
coconut-crypto = { version = "0.7.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}

secret_sharing_and_dkg = { version = "0.9.0", default-features = false }
oblivious_transfer_protocols = { version = "0.5.0", default-features = false}
bulletproofs_plus_plus = { version = "0.2.0", default-features = false}
smc_range_proof = { version = "0.2.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.22.0",
"version": "0.23.0",
"author": "Dock.io",
"license": "Apache-2.0",
"private": false,
Expand Down
31 changes: 31 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ark_ec::{CurveGroup, VariableBaseMSM};
use ark_serialize::CanonicalSerialize;
use blake2::Blake2b512;
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;

use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -145,6 +146,36 @@ pub fn pedersen_commitment_g2(
g2_affine_to_uint8_array(&comm)
}

#[wasm_bindgen(js_name = generatePedersenCommKeyG1)]
pub fn generate_pedersen_comm_key_g1(
label: Vec<u8>,
return_uncompressed: bool
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let comm_key = schnorr_pok::inequality::CommitmentKey::<G1Affine>::new::<Blake2b512>(&label);
Ok(if return_uncompressed {
obj_to_uint8array_uncompressed!(&comm_key, "CommitmentKey")
} else {
obj_to_uint8array!(&comm_key, false, "CommitmentKey")
})
}

#[wasm_bindgen(js_name = decompressPedersenCommKeyG1)]
pub fn decompress_pedersen_comm_key_g1(
comm_key: js_sys::Uint8Array
) -> Result<js_sys::Uint8Array, JsValue> {
let comm_key = obj_from_uint8array!(
schnorr_pok::inequality::CommitmentKey::<G1Affine>,
comm_key,
false,
"CommitmentKey"
);
Ok(obj_to_uint8array_uncompressed!(
&comm_key,
"SmcParamsAndCommitmentKeyAndSecretKey"
))
}

fn fr_uin8_array_from_bytes_hash(bytes: &[u8]) -> js_sys::Uint8Array {
let f = dock_crypto_utils::hashing_utils::field_elem_from_try_and_incr::<Fr, Blake2b512>(bytes);
fr_to_uint8_array(&f).unwrap()
Expand Down
8 changes: 8 additions & 0 deletions src/composite_proof_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ pub fn generate_bound_check_smc_with_kv_witness(message: Uint8Array) -> Result<J
serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)
}

#[wasm_bindgen(js_name = generatePublicInequalityWitness)]
pub fn generate_public_inequality_witness(message: Uint8Array) -> Result<JsValue, JsValue> {
set_panic_hook();
let message = fr_from_uint8_array(message, true)?;
let witness = Witness::PublicInequality(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
19 changes: 19 additions & 0 deletions src/composite_proof_system/setup_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,22 @@ pub fn generate_setup_param_for_smc_params_and_sk(
params
)))
}

#[wasm_bindgen(js_name = generateSetupParamForCommitmentKey)]
pub fn generate_setup_param_for_commitment_key(
comm_key: js_sys::Uint8Array,
uncompressed: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let comm_key = if uncompressed {
obj_from_uint8array_uncompressed!(schnorr_pok::inequality::CommitmentKey<G1Affine>, comm_key, "CommitmentKey")
} else {
obj_from_uint8array!(schnorr_pok::inequality::CommitmentKey<G1Affine>, comm_key, false, "CommitmentKey")
};
Ok(obj_to_uint8array_uncompressed!(&SetupParams::<
Bls12_381,
G1Affine,
>::CommitmentKey(
comm_key
)))
}
39 changes: 35 additions & 4 deletions src/composite_proof_system/statements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::utils::{
g1_affine_from_uint8_array, g2_affine_from_uint8_array, js_array_to_g1_affine_vec,
js_array_to_g2_affine_vec, set_panic_hook,
};
use crate::utils::{fr_from_uint8_array, g1_affine_from_uint8_array, g2_affine_from_uint8_array, js_array_to_g1_affine_vec, js_array_to_g2_affine_vec, set_panic_hook};
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use js_sys::Uint8Array;
use proof_system::{
meta_statement::{EqualWitnesses, MetaStatement},
prelude,
};
use crate::G1Affine;
use std::collections::BTreeSet;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use zeroize::Zeroize;

// All `Statement`s are returned in their uncompressed form as they are generated by the same party using
// them unlike signature params, public keys, proofs, etc
Expand All @@ -20,6 +19,8 @@ pub(crate) type PedCommG1Stmt =
pub(crate) type PedCommG2Stmt =
prelude::ped_comm::PedersenCommitment<<Bls12_381 as Pairing>::G2Affine>;

pub(crate) type InequalityG1Stmt = prelude::inequality::PublicInequality<<Bls12_381 as Pairing>::G1Affine>;

#[wasm_bindgen(js_name = generatePedersenCommitmentG1Statement)]
pub fn generate_pedersen_commitment_g1_statement(
commitment_key: js_sys::Array,
Expand Down Expand Up @@ -70,6 +71,36 @@ pub fn generate_pedersen_commitment_g2_statement_from_param_refs(
Ok(obj_to_uint8array_uncompressed!(&statement, "PedCommG2Stmt"))
}

#[wasm_bindgen(js_name = generatePublicInequalityG1Statement)]
pub fn generate_public_inequality_g1_statement(
inequal_to: Uint8Array,
commitment_key: Uint8Array,
uncompressed_key: bool,
) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let commitment_key = if uncompressed_key {
obj_from_uint8array_uncompressed!(schnorr_pok::inequality::CommitmentKey<G1Affine>, commitment_key, "CommitmentKey")
} else {
obj_from_uint8array!(schnorr_pok::inequality::CommitmentKey<G1Affine>, commitment_key, false, "CommitmentKey")
};
let inequal_to = fr_from_uint8_array(inequal_to, false)?;
let statement =
InequalityG1Stmt::new_statement_from_params::<Bls12_381>(inequal_to, commitment_key);
Ok(obj_to_uint8array_uncompressed!(&statement, "InequalityG1Stmt"))
}

#[wasm_bindgen(js_name = generatePublicInequalityG1StatementFromParamRefs)]
pub fn generate_public_inequality_g1_statement_from_param_refs(
inequal_to: Uint8Array,
commitment_key: usize,
) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let inequal_to = fr_from_uint8_array(inequal_to, false)?;
let statement =
InequalityG1Stmt::new_statement_from_params_ref::<Bls12_381>(inequal_to, commitment_key);
Ok(obj_to_uint8array_uncompressed!(&statement, "InequalityG1Stmt"))
}

#[wasm_bindgen(js_name = generateWitnessEqualityMetaStatement)]
pub fn generate_witness_equality_meta_statement(equality: js_sys::Set) -> Result<JsValue, JsValue> {
set_panic_hook();
Expand Down
15 changes: 15 additions & 0 deletions src/js/composite_proof_system_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ module.exports.generateBoundCheckSmcWithKVVerifierStatementFromParamRefs = (min,
return wasm.generateBoundCheckSmcWithKVVerifierStatementFromParamRefs(min, max, params);
};

module.exports.generatePublicInequalityG1Statement = (inequalTo, commKey, uncompressedKey) => {
requireWasmInitialized();
return wasm.generatePublicInequalityG1Statement(inequalTo, commKey, uncompressedKey);
};

module.exports.generatePublicInequalityG1StatementFromParamRefs = (inequalTo, commKey) => {
requireWasmInitialized();
return wasm.generatePublicInequalityG1StatementFromParamRefs(inequalTo, commKey);
};

module.exports.generateWitnessEqualityMetaStatement = (equalities) => {
requireWasmInitialized();
return wasm.generateWitnessEqualityMetaStatement(equalities);
Expand Down Expand Up @@ -237,6 +247,11 @@ module.exports.generateBoundCheckSmcWithKVWitness = (message) => {
return wasm.generateBoundCheckSmcWithKVWitness(message);
};

module.exports.generatePublicInequalityWitness = (message) => {
requireWasmInitialized();
return wasm.generatePublicInequalityWitness(message);
};

module.exports.generateProofSpecG1 = (statements, metaStatements, setupParams, context) => {
requireWasmInitialized();
return wasm.generateProofSpecG1(statements, metaStatements, setupParams, context);
Expand Down
5 changes: 5 additions & 0 deletions src/js/setup_params_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ module.exports.generateSetupParamForSmcParams = (params, uncompressed) => {
module.exports.generateSetupParamForSmcParamsAndSk = (params, uncompressed) => {
requireWasmInitialized();
return wasm.generateSetupParamForSmcParamsAndSk(params, uncompressed);
};

module.exports.generateSetupParamForCommitmentKey = (commKey, uncompressed) => {
requireWasmInitialized();
return wasm.generateSetupParamForCommitmentKey(commKey, uncompressed);
};
15 changes: 15 additions & 0 deletions src/js/type_declarations/composite_proof_system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ export function generateBoundCheckSmcWithKVVerifierStatementFromParamRefs(
params: number,
): Uint8Array;

export function generatePublicInequalityG1Statement(
inequalTo: Uint8Array,
commKey: Uint8Array,
uncompressedKey: boolean
): Uint8Array;

export function generatePublicInequalityG1StatementFromParamRefs(
inequalTo: Uint8Array,
commKey: number,
): Uint8Array;

export function generateWitnessEqualityMetaStatement(
equalities: Set<[number, number]>,
): Uint8Array;
Expand Down Expand Up @@ -288,6 +299,10 @@ export function generateBoundCheckSmcWithKVWitness(
message: Uint8Array
): Uint8Array;

export function generatePublicInequalityWitness(
message: Uint8Array
): Uint8Array;

export function generateProofSpecG1(
statements: Uint8Array[],
metaStatements: Uint8Array[],
Expand Down
5 changes: 5 additions & 0 deletions src/js/type_declarations/setup_params.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ export function generateSetupParamForSmcParams(
export function generateSetupParamForSmcParamsAndSk(
params: Uint8Array,
uncompressed: boolean
): Uint8Array;

export function generateSetupParamForCommitmentKey(
commKey: Uint8Array,
uncompressed: boolean
): Uint8Array;
9 changes: 9 additions & 0 deletions src/js/type_declarations/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ export function pedersenCommitmentG2(
bases: Uint8Array[],
messages: Uint8Array[],
): Uint8Array;

export function generatePedersenCommKeyG1(
label: Uint8Array,
returnUncompressed: boolean,
): Uint8Array;

export function decompressPedersenCommKeyG1(
commKey: Uint8Array
): Uint8Array;
Loading

0 comments on commit e20fc4d

Please sign in to comment.