From 51e70321e439a90cff6b9c5e5c500b73587927de Mon Sep 17 00:00:00 2001 From: lovesh Date: Thu, 21 Mar 2024 16:46:21 +0530 Subject: [PATCH] kb universal accum Signed-off-by: lovesh --- Cargo.lock | 8 +- Cargo.toml | 4 +- package.json | 2 +- src/accumulator/common.rs | 322 ++++++ src/accumulator/kb_accumulator.rs | 542 +++++++++ src/accumulator/mod.rs | 3 + src/{ => accumulator}/vb_accumulator.rs | 348 +----- src/bddt16_kvac.rs | 30 +- src/composite_proof_system/mod.rs | 32 +- src/composite_proof_system/setup_params.rs | 5 +- .../statements/accumulator.rs | 183 ++- src/delegated_proof.rs | 60 +- src/js/composite_proof_system_wasm.js | 60 + src/js/delegated_proof_wasm.js | 10 + src/js/kb_accumulator_wasm.js | 138 +++ .../composite_proof_system.d.ts | 60 + src/js/type_declarations/delegated_proof.d.ts | 6 +- src/js/type_declarations/index.d.ts | 2 + src/js/type_declarations/kb_accumulator.d.ts | 194 ++++ src/js/types/IKBUniversalAccumulator.ts | 7 + src/js/types/INonMembershipWitness.ts | 3 + src/js/types/IUniversalAccumulator.ts | 3 + src/js/types/index.ts | 1 + src/js/wasm_module.js | 1 + src/lib.rs | 2 +- tests/js/accumulator.spec.ts | 206 ++-- tests/js/kb-universal-accumulator.spec.ts | 1023 +++++++++++++++++ tests/js/proofSystem.spec.ts | 278 ++++- 28 files changed, 3056 insertions(+), 477 deletions(-) create mode 100644 src/accumulator/common.rs create mode 100644 src/accumulator/kb_accumulator.rs create mode 100644 src/accumulator/mod.rs rename src/{ => accumulator}/vb_accumulator.rs (68%) create mode 100644 src/js/kb_accumulator_wasm.js create mode 100644 src/js/type_declarations/kb_accumulator.d.ts create mode 100644 src/js/types/IKBUniversalAccumulator.ts create mode 100644 tests/js/kb-universal-accumulator.spec.ts diff --git a/Cargo.lock b/Cargo.lock index 3b54f2b..14ebd5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1246,9 +1246,9 @@ dependencies = [ [[package]] name = "proof_system" -version = "0.25.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca4eb6c89bb45e12362ee53919110d8577acd0f18c41ffbd9a02a6946111a527" +checksum = "3683ff20a514049abf66b4a773ff3d3045b76f7c6180452d85483ced6b0302e6" dependencies = [ "aead", "ark-ec", @@ -1841,9 +1841,9 @@ checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" [[package]] name = "vb_accumulator" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2835edd3b7878ced8b25b39b1862dc3a96f5177339e336e3b1b0cb73064522b" +checksum = "502486b9f6ce41edd9a4797f31448ede0341b97723e6032d92f36dba97952e29" dependencies = [ "ark-ec", "ark-ff", diff --git a/Cargo.toml b/Cargo.toml index 2e85485..85fabea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,9 @@ dlmalloc = { version = "0.2.5", features = ["global"], optional = true } serde_with = { version = "1.10.0", default-features = false, features = ["macros"] } bbs_plus = { version = "0.19.0", default-features = false } -vb_accumulator = { version = "0.20.0", default-features = false } +vb_accumulator = { version = "0.21.0", default-features = false } schnorr_pok = { version = "0.17.0", default-features = false } -proof_system = { version = "0.25.0", default-features = false } +proof_system = { version = "0.26.0", default-features = false } coconut-crypto = { version = "0.8.0", default-features = false } dock_crypto_utils = { version = "0.17.0", default-features = false } saver = { version = "0.15.0", default-features = false } diff --git a/package.json b/package.json index 48029bd..68e41e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/crypto-wasm", - "version": "0.25.0", + "version": "0.26.0", "author": "Dock.io", "license": "Apache-2.0", "private": false, diff --git a/src/accumulator/common.rs b/src/accumulator/common.rs new file mode 100644 index 0000000..bd3b44f --- /dev/null +++ b/src/accumulator/common.rs @@ -0,0 +1,322 @@ +use crate::{ + utils::{fr_to_jsvalue, random_bytes, set_panic_hook}, + Fr, +}; +use ark_bls12_381::Bls12_381; +use ark_serialize::CanonicalDeserialize; +use blake2::Blake2b512; +use dock_crypto_utils::concat_slices; +use vb_accumulator::prelude::{Keypair, PublicKey, SecretKey, SetupParams}; +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; +use zeroize::Zeroize; + +// Trying to keep types at one place so changing the curve is easier +pub(crate) type AccumSk = SecretKey; +pub type AccumPk = PublicKey; +pub type AccumSetupParams = SetupParams; +pub(crate) type AccumKeypair = Keypair; + +/// Generate accumulator parameters. They are needed to generate public key and initialize the accumulator. +/// Pass the `label` argument to generate parameters deterministically. +#[wasm_bindgen(js_name = generateAccumulatorParams)] +pub fn generate_accumulator_params(label: Option>) -> Result { + set_panic_hook(); + let label = label.unwrap_or_else(random_bytes); + let params = AccumSetupParams::new::(&label); + Ok(obj_to_uint8array!(¶ms, false, "SetupParams")) +} + +/// Check if parameters are valid. Before verifying witness or using for proof verification, +/// make sure the params are valid. +#[wasm_bindgen(js_name = isAccumulatorParamsValid)] +pub fn accumulator_is_params_valid(params: js_sys::Uint8Array) -> Result { + set_panic_hook(); + let params = deserialize_params(params)?; + Ok(params.is_valid()) +} + +/// Generate secret key for the accumulator manager who updates the accumulator and creates witnesses. +/// Pass the `seed` argument to generate key deterministically. +#[wasm_bindgen(js_name = generateAccumulatorSecretKey)] +pub fn accumulator_generate_secret_key(seed: Option>) -> Result { + set_panic_hook(); + let mut seed = seed.unwrap_or_else(random_bytes); + let sk = AccumSk::generate_using_seed::(&seed); + seed.zeroize(); + + serde_wasm_bindgen::to_value(&sk).map_err(JsValue::from) +} + +/// Generate public key from given params and secret key. +#[wasm_bindgen(js_name = generateAccumulatorPublicKey)] +pub fn accumulator_generate_public_key( + secret_key: JsValue, + params: js_sys::Uint8Array, +) -> Result { + set_panic_hook(); + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let params = deserialize_params(params)?; + let pk = AccumKeypair::public_key_from_secret_key(&sk, ¶ms); + Ok(obj_to_uint8array!(&pk, false, "PublicKeyG2")) +} + +/// Check if public key is valid. Before verifying witness or using for proof verification, +/// make sure the public key is valid. +#[wasm_bindgen(js_name = isAccumulatorPublicKeyValid)] +pub fn accumulator_is_pubkey_valid(public_key: js_sys::Uint8Array) -> Result { + set_panic_hook(); + let pk = deserialize_public_key(public_key)?; + Ok(pk.is_valid()) +} + +/// Generate private and public key from given params and optional `seed`. +/// Pass the `seed` argument to generate keys deterministically. +#[wasm_bindgen(js_name = generateAccumulatorKeyPair)] +pub fn accumulator_generate_keypair( + params: js_sys::Uint8Array, + seed: Option>, +) -> Result { + set_panic_hook(); + let params = deserialize_params(params)?; + let seed = seed.unwrap_or(random_bytes()); + let keypair = AccumKeypair::generate_using_seed::(&seed, ¶ms); + serde_wasm_bindgen::to_value(&keypair).map_err(JsValue::from) +} + +/// To add arbitrary bytes as an accumulator member, they should be first converted to +/// a field element. This function will prefix the given bytes with a constant string as +/// domain separator and then generate a field element using IETF standard. +#[wasm_bindgen(js_name = accumulatorGetElementFromBytes)] +pub fn accumulator_get_element_from_bytes(bytes: Vec) -> Result { + set_panic_hook(); + let f = fr_to_jsvalue(&encode_bytes_as_accumulator_member(&bytes))?; + Ok(f) +} + +pub(crate) fn deserialize_params(bytes: js_sys::Uint8Array) -> Result { + CanonicalDeserialize::deserialize_compressed(&bytes.to_vec()[..]).map_err(|e| { + JsValue::from(&format!( + "Failed to deserialize accumulator params from bytes due to error: {:?}", + e + )) + }) +} + +pub(crate) fn deserialize_public_key(bytes: js_sys::Uint8Array) -> Result { + CanonicalDeserialize::deserialize_compressed(&bytes.to_vec()[..]).map_err(|e| { + JsValue::from(&format!( + "Failed to deserialize accumulator public key from bytes due to error: {:?}", + e + )) + }) +} + +pub fn encode_bytes_as_accumulator_member(bytes: &[u8]) -> Fr { + dock_crypto_utils::hashing_utils::field_elem_from_try_and_incr::( + &concat_slices!(bytes, b"Accumulator element"), + ) +} + +#[macro_use] +mod macros { + #[macro_export] + macro_rules! get_membership_witness { + ($accum: ident, $element: ident, $sk: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let sk: AccumSk = serde_wasm_bindgen::from_value($sk)?; + let new_value = $accum.compute_membership_witness(&element, &sk); + serde_wasm_bindgen::to_value(&new_value).map_err(JsValue::from) + }}; + } + + #[macro_export] + macro_rules! get_membership_witnesses_for_batch { + ($accum: ident, $elements: ident, $sk: ident) => {{ + let elems = js_array_to_fr_vec(&$elements)?; + let sk: AccumSk = serde_wasm_bindgen::from_value($sk)?; + let witnesses = $accum.compute_membership_witnesses_for_batch(&elems, &sk); + + let result = js_sys::Array::new(); + for witness in witnesses { + result.push(&serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)?); + } + Ok(result) + }}; + } + + #[macro_export] + macro_rules! verify_membership { + ($accum: ident, $element: ident, $witness_type: ident, $witness: ident, $pk: ident, $params: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let witness: $witness_type = serde_wasm_bindgen::from_value($witness)?; + let pk = deserialize_public_key($pk)?; + let params = deserialize_params($params)?; + Ok($accum.verify_membership(&element, &witness, &pk, ¶ms)) + }}; + } + + #[macro_export] + macro_rules! update_witness_post_add { + ($witness:expr, $element: ident, $addition: ident, $old_accumulated: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let addition = fr_from_uint8_array($addition, true)?; + let old_accumulated = g1_affine_from_uint8_array($old_accumulated)?; + serde_wasm_bindgen::to_value(&$witness.update_after_addition( + &element, + &addition, + &old_accumulated, + )) + .map_err(JsValue::from) + }}; + } + + #[macro_export] + macro_rules! update_witness_post_remove { + ($witness:expr, $element: ident, $removal: ident, $new_accumulated: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let removal = fr_from_uint8_array($removal, true)?; + let new_accumulated = g1_affine_from_uint8_array($new_accumulated)?; + let new_wit = $witness + .update_after_removal(&element, &removal, &new_accumulated) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_after_removal returned error: {:?}", + e + )) + })?; + serde_wasm_bindgen::to_value(&new_wit).map_err(JsValue::from) + }}; + } + + #[macro_export] + macro_rules! update_witness_single_batch { + ($witness:expr, $element: ident, $additions: ident, $removals: ident, $public_info: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let additions = js_array_to_fr_vec(&$additions)?; + let removals = js_array_to_fr_vec(&$removals)?; + let public_info: Omega = ark_serialize::CanonicalDeserialize::deserialize_compressed(&$public_info.to_vec()[..]).map_err(|e| { + JsValue::from(&format!( + "Failed to deserialize public info from bytes due to error: {:?}", + e + )) + })?; + let new_witness = $witness + .update_using_public_info_after_batch_updates(&additions, &removals, &public_info, &element) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_using_public_info_after_batch_updates returned error: {:?}", + e + )) + })?; + serde_wasm_bindgen::to_value(&new_witness).map_err(JsValue::from) + }} + } + + #[macro_export] + macro_rules! update_witness_multiple_batches { + ($witness:expr, $element: ident, $additions: ident, $removals: ident, $public_info: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + if $additions.length() == $removals.length() && $removals.length() == $public_info.length() { + let size = $additions.length(); + let mut updates_and_public_info = Vec::with_capacity(size as usize); + for i in 0..size { + let adds = js_array_to_fr_vec(&js_sys::Array::from(&$additions.get(i)))?; + let rems = js_array_to_fr_vec(&js_sys::Array::from(&$removals.get(i)))?; + let bytes: Vec = serde_wasm_bindgen::from_value($public_info.get(i))?; + let p: Omega = ark_serialize::CanonicalDeserialize::deserialize_compressed(&bytes[..]).map_err(|e| JsValue::from(&format!( + "Failed to deserialize public info from bytes due to error: {:?}", + e + )))?; + updates_and_public_info.push((adds, rems, p)); + } + let new_witness = $witness.update_using_public_info_after_multiple_batch_updates(updates_and_public_info.iter().map(|(a, r, p)| (a.as_slice(), r.as_slice(), p)).collect::>(), &element) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_using_public_info_after_multiple_batch_updates returned error: {:?}", + e + )) + })?; + let w = serde_wasm_bindgen::to_value(&new_witness).map_err(JsValue::from)?; + Ok(w) + } else { + Err(JsValue::from(&format!( + "Expected same but found different lengths for additions, removals and public info: {} {} {}", + $additions.length(), $removals.length(), $public_info.length() + ))) + } + }} + } + + #[macro_export] + macro_rules! init_proof_protocol { + ($protocol:ident, $witness:ident, $element: ident, $blinding: ident, $public_key: ident, $params: ident, $prk: ident) => {{ + let element = fr_from_uint8_array($element, true)?; + let blinding = fr_from_uint8_array($blinding, true)?; + let pk = deserialize_public_key($public_key)?; + let params = deserialize_params($params)?; + + let mut rng = get_seeded_rng(); + let protocol = $protocol::init( + &mut rng, + element, + Some(blinding), + &$witness, + &pk, + ¶ms, + &$prk, + ); + serde_wasm_bindgen::to_value(&protocol).map_err(JsValue::from) + }}; + } + + #[macro_export] + macro_rules! verify_proof { + ($proof: ident, $accumulated:ident, $challenge: ident, $public_key: ident, $params: ident, $prk: ident) => {{ + let accumulated = g1_affine_from_uint8_array($accumulated)?; + let challenge = fr_from_uint8_array($challenge, false)?; + let pk = deserialize_public_key($public_key)?; + let params = deserialize_params($params)?; + to_verify_response!($proof.verify( + &accumulated, + &challenge, + pk.clone(), + params.clone(), + &$prk + )) + }}; + } + + #[macro_export] + macro_rules! update_using_secret_key_after_batch_updates { + ($witnesses: ident, $elements:ident, $additions: ident, $removals: ident, $old_accumulated: ident, $secret_key: ident, $wit_type: ident) => {{ + let elements = js_array_to_fr_vec(&$elements)?; + let additions = js_array_to_fr_vec(&$additions)?; + let removals = js_array_to_fr_vec(&$removals)?; + let old_accumulated = g1_affine_from_uint8_array($old_accumulated)?; + let sk: AccumSk = serde_wasm_bindgen::from_value($secret_key)?; + let mut wits = Vec::with_capacity($witnesses.length() as usize); + for w in $witnesses.values() { + wits.push(serde_wasm_bindgen::from_value::<$wit_type>(w.unwrap())?); + } + let new_wits = $wit_type::update_using_secret_key_after_batch_updates( + &additions, + &removals, + &elements, + &wits, + &old_accumulated, + &sk, + ) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_using_secret_key_after_batch_updates returned error: {:?}", + e + )) + })?; + let result = js_sys::Array::new(); + for w in new_wits { + result.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); + } + Ok(result) + }} + } +} diff --git a/src/accumulator/kb_accumulator.rs b/src/accumulator/kb_accumulator.rs new file mode 100644 index 0000000..8601081 --- /dev/null +++ b/src/accumulator/kb_accumulator.rs @@ -0,0 +1,542 @@ +use crate::{ + accumulator::{ + common::{deserialize_params, deserialize_public_key, AccumSk}, + vb_accumulator::Omega, + }, + utils::{fr_from_uint8_array, js_array_to_fr_vec, set_panic_hook}, +}; +use ark_bls12_381::Bls12_381; +use ark_ec::pairing::Pairing; +use js_sys::Uint8Array; +use vb_accumulator::kb_universal_accumulator::{ + witness::{ + KBUniversalAccumulatorMembershipWitness, KBUniversalAccumulatorNonMembershipWitness, + }, + KBUniversalAccumulator, +}; +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; +use zeroize::Zeroize; + +pub(crate) type KBUniversalAccum = KBUniversalAccumulator; +pub(crate) type KBUniMembershipWit = + KBUniversalAccumulatorMembershipWitness<::G1Affine>; +pub(crate) type KBUniNonMembershipWit = + KBUniversalAccumulatorNonMembershipWitness<::G1Affine>; + +#[wasm_bindgen(js_name = kbUniversalAccumulatorInitialise)] +pub fn kb_universal_accumulator_initialize( + domain: js_sys::Array, + secret_key: JsValue, + params: Uint8Array, +) -> Result { + let domain = js_array_to_fr_vec(&domain)?; + let secret_key: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let params = deserialize_params(params)?; + let accum = KBUniversalAccum::initialize_empty(¶ms); + let (mem, non_mem) = accum.compute_extended(&domain, &secret_key); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorComputeExtended)] +pub fn kb_universal_accumulator_compute_extended( + old_accum: JsValue, + new_elements: js_sys::Array, + secret_key: JsValue, +) -> Result { + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let new_elements = js_array_to_fr_vec(&new_elements)?; + let secret_key: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_extended(&new_elements, &secret_key); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorAdd)] +pub fn kb_universal_accumulator_add( + old_accum: JsValue, + element: Uint8Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let element = fr_from_uint8_array(element, true)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_new_post_add(&element, &sk); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorRemove)] +pub fn kb_universal_accumulator_remove( + old_accum: JsValue, + element: Uint8Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let element = fr_from_uint8_array(element, true)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_new_post_remove(&element, &sk); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorMembershipWitness)] +pub fn kb_universal_accumulator_membership_witness( + accum: JsValue, + element: Uint8Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + crate::get_membership_witness!(accum, element, secret_key) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorVerifyMembership)] +pub fn kb_universal_accumulator_verify_membership( + accum: JsValue, + member: Uint8Array, + witness: JsValue, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + crate::verify_membership!( + accum, + member, + KBUniMembershipWit, + witness, + public_key, + params + ) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorNonMembershipWitness)] +pub fn kb_universal_accumulator_non_membership_witness( + accum: JsValue, + non_member: Uint8Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + let element = fr_from_uint8_array(non_member, true)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let new_value = accum.compute_non_membership_witness(&element, &sk); + serde_wasm_bindgen::to_value(&new_value).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorVerifyNonMembership)] +pub fn universal_accumulator_verify_non_membership( + accum: JsValue, + non_member: Uint8Array, + witness: JsValue, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + let non_member = fr_from_uint8_array(non_member, true)?; + let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?; + let pk = deserialize_public_key(public_key)?; + let params = deserialize_params(params)?; + Ok(accum.verify_non_membership(&non_member, &witness, &pk, ¶ms)) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorAddBatch)] +pub fn kb_universal_accumulator_add_batch( + existing_accum: JsValue, + elements: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(existing_accum)?; + let elems = js_array_to_fr_vec(&elements)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_new_post_add_batch(&elems, &sk); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorRemoveBatch)] +pub fn kb_universal_accumulator_remove_batch( + existing_accum: JsValue, + elements: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(existing_accum)?; + let elems = js_array_to_fr_vec(&elements)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_new_post_remove_batch(&elems, &sk); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorBatchUpdates)] +pub fn kb_universal_accumulator_batch_updates( + existing_accum: JsValue, + additions: js_sys::Array, + removals: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(existing_accum)?; + let adds = js_array_to_fr_vec(&additions)?; + let removes = js_array_to_fr_vec(&removals)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (mem, non_mem) = accum.compute_new_post_batch_updates(&adds, &removes, &sk); + let new = KBUniversalAccum::from_accumulated(mem, non_mem); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorMembershipWitnessesForBatch)] +pub fn kb_universal_accumulator_membership_witnesses_for_batch( + accum: JsValue, + elements: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + crate::get_membership_witnesses_for_batch!(accum, elements, secret_key) +} + +#[wasm_bindgen(js_name = kbUniversalAccumulatorNonMembershipWitnessesForBatch)] +pub fn kb_universal_accumulator_non_membership_witnesses_for_batch( + accum: JsValue, + non_members: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(accum)?; + let non_members = js_array_to_fr_vec(&non_members)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let witnesses = accum.compute_non_membership_witnesses_for_batch(&non_members, &sk); + let result = js_sys::Array::new(); + for witness in witnesses { + result.push(&serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)?); + } + Ok(result) +} + +#[wasm_bindgen(js_name = kbUniversalUpdateMembershipWitnessPostAdd)] +pub fn kb_universal_update_membership_witness_post_add( + witness: JsValue, + member: Uint8Array, + addition: Uint8Array, + old_accum: JsValue, +) -> Result { + set_panic_hook(); + let witness: KBUniMembershipWit = serde_wasm_bindgen::from_value(witness)?; + let member = fr_from_uint8_array(member, true)?; + let addition = fr_from_uint8_array(addition, true)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let new = accum.update_mem_wit_on_addition(&witness, &member, &addition); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalUpdateMembershipWitnessPostRemove)] +pub fn kb_universal_update_membership_witness_post_remove( + witness: JsValue, + member: Uint8Array, + removal: Uint8Array, + new_accum: JsValue, +) -> Result { + set_panic_hook(); + let witness: KBUniMembershipWit = serde_wasm_bindgen::from_value(witness)?; + let member = fr_from_uint8_array(member, true)?; + let removal = fr_from_uint8_array(removal, true)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(new_accum)?; + let new = accum + .update_mem_wit_on_removal(&witness, &member, &removal) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_mem_wit_on_removal returned error: {:?}", + e + )) + })?; + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalUpdateNonMembershipWitnessPostAdd)] +pub fn kb_universal_update_non_membership_witness_post_add( + witness: JsValue, + non_member: Uint8Array, + addition: Uint8Array, + new_accum: JsValue, +) -> Result { + set_panic_hook(); + let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?; + let non_member = fr_from_uint8_array(non_member, true)?; + let addition = fr_from_uint8_array(addition, true)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(new_accum)?; + let new = accum + .update_non_mem_wit_on_addition(&witness, &non_member, &addition) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_non_mem_wit_on_addition returned error: {:?}", + e + )) + })?; + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUniversalUpdateNonMembershipWitnessPostRemove)] +pub fn kb_universal_update_non_membership_witness_post_remove( + witness: JsValue, + non_member: Uint8Array, + removal: Uint8Array, + old_accum: JsValue, +) -> Result { + set_panic_hook(); + let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?; + let non_member = fr_from_uint8_array(non_member, true)?; + let removal = fr_from_uint8_array(removal, true)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let new = accum.update_non_mem_wit_on_removal(&witness, &non_member, &removal); + serde_wasm_bindgen::to_value(&new).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = kbUpdateMembershipWitnessesPostBatchUpdates)] +pub fn kb_universal_update_membership_witnesses_post_batch_updates( + witnesses: js_sys::Array, + members: js_sys::Array, + additions: js_sys::Array, + removals: js_sys::Array, + old_accum: JsValue, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let members = js_array_to_fr_vec(&members)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let mut wits = Vec::with_capacity(witnesses.length() as usize); + for w in witnesses.values() { + wits.push(serde_wasm_bindgen::from_value::( + w.unwrap(), + )?); + } + let new_wits = accum + .update_mem_wit_using_secret_key_on_batch_updates( + &additions, &removals, &members, &wits, &sk, + ) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_mem_wit_using_secret_key_on_batch_updates returned error: {:?}", + e + )) + })?; + let result = js_sys::Array::new(); + for w in new_wits { + result.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); + } + Ok(result) +} + +#[wasm_bindgen(js_name = kbUpdateNonMembershipWitnessesPostBatchUpdates)] +pub fn kb_universal_update_non_membership_witnesses_post_batch_updates( + witnesses: js_sys::Array, + non_members: js_sys::Array, + additions: js_sys::Array, + removals: js_sys::Array, + old_accum: JsValue, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let non_members = js_array_to_fr_vec(&non_members)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let mut wits = Vec::with_capacity(witnesses.length() as usize); + for w in witnesses.values() { + wits.push(serde_wasm_bindgen::from_value::( + w.unwrap(), + )?); + } + let new_wits = accum.update_non_mem_wit_using_secret_key_on_batch_updates( + &additions, + &removals, + &non_members, + &wits, + &sk, + ) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_non_mem_wit_using_secret_key_on_batch_updates returned error: {:?}", + e + )) + })?; + let result = js_sys::Array::new(); + for w in new_wits { + result.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); + } + Ok(result) +} + +#[wasm_bindgen(js_name = kbUpdateBothWitnessesPostBatchUpdates)] +pub fn kb_universal_update_both_witnesses_post_batch_updates( + mem_witnesses: js_sys::Array, + members: js_sys::Array, + non_mem_witnesses: js_sys::Array, + non_members: js_sys::Array, + additions: js_sys::Array, + removals: js_sys::Array, + old_accum: JsValue, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let members = js_array_to_fr_vec(&members)?; + let non_members = js_array_to_fr_vec(&non_members)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let mut mem_wits = Vec::with_capacity(mem_witnesses.length() as usize); + for w in mem_witnesses.values() { + mem_wits.push(serde_wasm_bindgen::from_value::( + w.unwrap(), + )?); + } + let mut non_mem_wits = Vec::with_capacity(non_mem_witnesses.length() as usize); + for w in non_mem_witnesses.values() { + non_mem_wits.push(serde_wasm_bindgen::from_value::( + w.unwrap(), + )?); + } + let (new_mem_wits, new_non_mem_wits) = accum + .update_both_wit_using_secret_key_on_batch_updates( + &additions, + &removals, + &members, + &mem_wits, + &non_members, + &non_mem_wits, + &sk, + ) + .map_err(|e| { + JsValue::from(&format!( + "Evaluating update_both_wit_using_secret_key_on_batch_updates returned error: {:?}", + e + )) + })?; + let result = js_sys::Array::new(); + let mem_wit_arr = js_sys::Array::new(); + let non_mem_wit_arr = js_sys::Array::new(); + for w in new_mem_wits { + mem_wit_arr.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); + } + for w in new_non_mem_wits { + non_mem_wit_arr.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); + } + result.push(&mem_wit_arr); + result.push(&non_mem_wit_arr); + Ok(result) +} + +#[wasm_bindgen(js_name = publicInfoForKBUniversalMemWitnessUpdate)] +pub fn public_info_for_kb_universal_mem_witness_update( + old_accum: JsValue, + additions: js_sys::Array, + removals: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let omega = accum.generate_omega_for_membership_witnesses(&additions, &removals, &sk); + Ok(obj_to_uint8array!(&omega, false, "Omega")) +} + +#[wasm_bindgen(js_name = publicInfoForKBUniversalNonMemWitnessUpdate)] +pub fn public_info_for_kb_universal_non_mem_witness_update( + old_accum: JsValue, + additions: js_sys::Array, + removals: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let omega = accum.generate_omega_for_non_membership_witnesses(&additions, &removals, &sk); + Ok(obj_to_uint8array!(&omega, false, "Omega")) +} + +#[wasm_bindgen(js_name = publicInfoForBothKBUniversalWitnessUpdate)] +pub fn public_info_for_both_kb_universal_witness_update( + old_accum: JsValue, + additions: js_sys::Array, + removals: js_sys::Array, + secret_key: JsValue, +) -> Result { + set_panic_hook(); + let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?; + let additions = js_array_to_fr_vec(&additions)?; + let removals = js_array_to_fr_vec(&removals)?; + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let (omega_mem, omega_non_mem) = + accum.generate_omega_for_both_witnesses(&additions, &removals, &sk); + let result = js_sys::Array::new(); + result.push(&obj_to_uint8array!(&omega_mem, false, "Omega").into()); + result.push(&obj_to_uint8array!(&omega_non_mem, false, "Omega").into()); + Ok(result) +} + +#[wasm_bindgen(js_name = updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate)] +pub fn update_kb_universal_membership_witness_using_public_info_after_batch_update( + witness: JsValue, + member: Uint8Array, + additions: js_sys::Array, + removals: js_sys::Array, + public_info: Uint8Array, +) -> Result { + set_panic_hook(); + let witness: KBUniMembershipWit = serde_wasm_bindgen::from_value(witness)?; + crate::update_witness_single_batch!(witness, member, additions, removals, public_info) +} + +#[wasm_bindgen(js_name = updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates)] +pub fn update_kb_universal_membership_witness_using_public_info_after_multiple_batch_updates( + witness: JsValue, + member: Uint8Array, + additions: js_sys::Array, + removals: js_sys::Array, + public_info: js_sys::Array, +) -> Result { + set_panic_hook(); + let witness: KBUniMembershipWit = serde_wasm_bindgen::from_value(witness)?; + crate::update_witness_multiple_batches!(witness, member, additions, removals, public_info) +} + +#[wasm_bindgen(js_name = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate)] +pub fn update_kb_universal_non_membership_witness_using_public_info_after_batch_update( + witness: JsValue, + non_member: Uint8Array, + additions: js_sys::Array, + removals: js_sys::Array, + public_info: Uint8Array, +) -> Result { + set_panic_hook(); + let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?; + crate::update_witness_single_batch!(witness, non_member, additions, removals, public_info) +} + +#[wasm_bindgen(js_name = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates)] +pub fn update_kb_universal_non_membership_witness_using_public_info_after_multiple_batch_updates( + witness: JsValue, + non_member: Uint8Array, + additions: js_sys::Array, + removals: js_sys::Array, + public_info: js_sys::Array, +) -> Result { + set_panic_hook(); + let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?; + crate::update_witness_multiple_batches!(witness, non_member, additions, removals, public_info) +} diff --git a/src/accumulator/mod.rs b/src/accumulator/mod.rs new file mode 100644 index 0000000..214a3b1 --- /dev/null +++ b/src/accumulator/mod.rs @@ -0,0 +1,3 @@ +pub mod common; +pub mod kb_accumulator; +pub mod vb_accumulator; diff --git a/src/vb_accumulator.rs b/src/accumulator/vb_accumulator.rs similarity index 68% rename from src/vb_accumulator.rs rename to src/accumulator/vb_accumulator.rs index 1ff6b8f..a432e29 100644 --- a/src/vb_accumulator.rs +++ b/src/accumulator/vb_accumulator.rs @@ -1,35 +1,40 @@ use crate::utils::{ - fr_from_jsvalue, fr_from_uint8_array, fr_to_jsvalue, fr_to_uint8_array, - g1_affine_from_uint8_array, g1_affine_to_uint8_array, get_seeded_rng, js_array_from_frs, - js_array_to_fr_vec, random_bytes, set_panic_hook, + fr_from_jsvalue, fr_from_uint8_array, fr_to_uint8_array, g1_affine_from_uint8_array, + g1_affine_to_uint8_array, get_seeded_rng, js_array_from_frs, js_array_to_fr_vec, random_bytes, + set_panic_hook, }; use ark_bls12_381::Bls12_381; use ark_ec::pairing::Pairing; -use dock_crypto_utils::concat_slices; use wasm_bindgen::prelude::*; use ark_ff::One; use blake2::Blake2b512; -use vb_accumulator::prelude::{ - Accumulator, Keypair, MembershipProof, MembershipProofProtocol, MembershipProvingKey, - MembershipWitness, NonMembershipProof, NonMembershipProofProtocol, NonMembershipProvingKey, - NonMembershipWitness, Omega as Omega_, PositiveAccumulator, PublicKey, SecretKey, SetupParams, - UniversalAccumulator, +use vb_accumulator::{ + kb_universal_accumulator::witness::{ + KBUniversalAccumulatorMembershipWitness, KBUniversalAccumulatorNonMembershipWitness, + }, + prelude::{ + Accumulator, MembershipProof, MembershipProofProtocol, MembershipProvingKey, + MembershipWitness, NonMembershipProof, NonMembershipProofProtocol, NonMembershipProvingKey, + NonMembershipWitness, Omega as Omega_, PositiveAccumulator, UniversalAccumulator, + }, }; use zeroize::Zeroize; -use crate::{to_verify_response, Fr}; +use crate::{ + accumulator::common::{deserialize_params, deserialize_public_key, AccumSk}, + to_verify_response, Fr, +}; -// Trying to keep types at one place so changing the curve is easier -pub(crate) type AccumSk = SecretKey; -pub type AccumPk = PublicKey; -pub type AccumSetupParams = SetupParams; -pub(crate) type AccumKeypair = Keypair; pub(crate) type PositiveAccum = PositiveAccumulator; pub(crate) type UniversalAccum = UniversalAccumulator; pub(crate) type MembershipWit = MembershipWitness<::G1Affine>; pub(crate) type NonMembershipWit = NonMembershipWitness<::G1Affine>; +pub(crate) type KbUniMembershipWit = + KBUniversalAccumulatorMembershipWitness<::G1Affine>; +pub(crate) type KbUniNonMembershipWit = + KBUniversalAccumulatorNonMembershipWitness<::G1Affine>; pub(crate) type Omega = Omega_<::G1Affine>; pub type MembershipPrk = MembershipProvingKey<::G1Affine>; pub type NonMembershipPrk = NonMembershipProvingKey<::G1Affine>; @@ -40,83 +45,6 @@ pub(crate) type NonMemProof = NonMembershipProof; use crate::common::VerifyResponse; -/// Generate accumulator parameters. They are needed to generate public key and initialize the accumulator. -/// Pass the `label` argument to generate parameters deterministically. -#[wasm_bindgen(js_name = generateAccumulatorParams)] -pub fn generate_accumulator_params(label: Option>) -> Result { - set_panic_hook(); - let label = label.unwrap_or_else(random_bytes); - let params = AccumSetupParams::new::(&label); - Ok(obj_to_uint8array!(¶ms, false, "SetupParams")) -} - -/// Check if parameters are valid. Before verifying witness or using for proof verification, -/// make sure the params are valid. -#[wasm_bindgen(js_name = isAccumulatorParamsValid)] -pub fn accumulator_is_params_valid(params: js_sys::Uint8Array) -> Result { - set_panic_hook(); - let params = deserialize_params(params)?; - Ok(params.is_valid()) -} - -/// Generate secret key for the accumulator manager who updates the accumulator and creates witnesses. -/// Pass the `seed` argument to generate key deterministically. -#[wasm_bindgen(js_name = generateAccumulatorSecretKey)] -pub fn accumulator_generate_secret_key(seed: Option>) -> Result { - set_panic_hook(); - let mut seed = seed.unwrap_or_else(random_bytes); - let sk = AccumSk::generate_using_seed::(&seed); - seed.zeroize(); - - serde_wasm_bindgen::to_value(&sk).map_err(JsValue::from) -} - -/// Generate public key from given params and secret key. -#[wasm_bindgen(js_name = generateAccumulatorPublicKey)] -pub fn accumulator_generate_public_key( - secret_key: JsValue, - params: js_sys::Uint8Array, -) -> Result { - set_panic_hook(); - let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; - let params = deserialize_params(params)?; - let pk = AccumKeypair::public_key_from_secret_key(&sk, ¶ms); - Ok(obj_to_uint8array!(&pk, false, "PublicKeyG2")) -} - -/// Check if public key is valid. Before verifying witness or using for proof verification, -/// make sure the public key is valid. -#[wasm_bindgen(js_name = isAccumulatorPublicKeyValid)] -pub fn accumulator_is_pubkey_valid(public_key: js_sys::Uint8Array) -> Result { - set_panic_hook(); - let pk = deserialize_public_key(public_key)?; - Ok(pk.is_valid()) -} - -/// Generate private and public key from given params and optional `seed`. -/// Pass the `seed` argument to generate keys deterministically. -#[wasm_bindgen(js_name = generateAccumulatorKeyPair)] -pub fn accumulator_generate_keypair( - params: js_sys::Uint8Array, - seed: Option>, -) -> Result { - set_panic_hook(); - let params = deserialize_params(params)?; - let seed = seed.unwrap_or(random_bytes()); - let keypair = AccumKeypair::generate_using_seed::(&seed, ¶ms); - serde_wasm_bindgen::to_value(&keypair).map_err(JsValue::from) -} - -/// To add arbitrary bytes as an accumulator member, they should be first converted to -/// a field element. This function will prefix the given bytes with a constant string as -/// domain separator and then generate a field element using IETF standard. -#[wasm_bindgen(js_name = accumulatorGetElementFromBytes)] -pub fn accumulator_get_element_from_bytes(bytes: Vec) -> Result { - set_panic_hook(); - let f = fr_to_jsvalue(&encode_bytes_as_accumulator_member(&bytes))?; - Ok(f) -} - /// Initialize a positive accumulator #[wasm_bindgen(js_name = positiveAccumulatorInitialize)] pub fn positive_accumulator_initialize(params: js_sys::Uint8Array) -> Result { @@ -188,7 +116,7 @@ pub fn positive_accumulator_verify_membership( set_panic_hook(); let accumulated = g1_affine_from_uint8_array(accumulated)?; let accum = PositiveAccum::from_accumulated(accumulated); - crate::verify_membership!(accum, element, witness, public_key, params) + crate::verify_membership!(accum, element, MembershipWit, witness, public_key, params) } /// Creates the initial elements that depend on the order of the curve and thus can be considered fixed. @@ -299,7 +227,7 @@ pub fn universal_accumulator_verify_membership( let accumulated = g1_affine_from_uint8_array(accumulated)?; // Only care about accumulated let accum = UniversalAccum::from_accumulated(accumulated); - crate::verify_membership!(accum, member, witness, public_key, params) + crate::verify_membership!(accum, member, MembershipWit, witness, public_key, params) } #[wasm_bindgen(js_name = universalAccumulatorComputeD)] @@ -545,10 +473,10 @@ pub fn universal_accumulator_non_membership_witnesses_for_batch( let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; let params = deserialize_params(params)?; let witnesses = accum - .compute_non_membership_witness_for_batch_given_d(d, &non_members, &sk, ¶ms) + .compute_non_membership_witnesses_for_batch_given_d(d, &non_members, &sk, ¶ms) .map_err(|e| { JsValue::from(&format!( - "Evaluating compute_non_membership_witness_for_batch_given_d returned error: {:?}", + "Evaluating compute_non_membership_witnesses_for_batch_given_d returned error: {:?}", e )) })?; @@ -954,231 +882,3 @@ pub fn accumulator_challenge_contribution_from_non_membership_proof( })?; Ok(js_sys::Uint8Array::from(bytes.as_slice())) } - -pub(crate) fn deserialize_params(bytes: js_sys::Uint8Array) -> Result { - ark_serialize::CanonicalDeserialize::deserialize_compressed(&bytes.to_vec()[..]).map_err(|e| { - JsValue::from(&format!( - "Failed to deserialize accumulator params from bytes due to error: {:?}", - e - )) - }) -} - -pub(crate) fn deserialize_public_key(bytes: js_sys::Uint8Array) -> Result { - ark_serialize::CanonicalDeserialize::deserialize_compressed(&bytes.to_vec()[..]).map_err(|e| { - JsValue::from(&format!( - "Failed to deserialize accumulator public key from bytes due to error: {:?}", - e - )) - }) -} - -#[macro_use] -mod macros { - #[macro_export] - macro_rules! get_membership_witness { - ($accum: ident, $element: ident, $sk: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let sk: AccumSk = serde_wasm_bindgen::from_value($sk)?; - let new_value = $accum.compute_membership_witness(&element, &sk); - serde_wasm_bindgen::to_value(&new_value).map_err(JsValue::from) - }}; - } - - #[macro_export] - macro_rules! get_membership_witnesses_for_batch { - ($accum: ident, $elements: ident, $sk: ident) => {{ - let elems = js_array_to_fr_vec(&$elements)?; - let sk: AccumSk = serde_wasm_bindgen::from_value($sk)?; - let witnesses = $accum.compute_membership_witness_for_batch(&elems, &sk); - - let result = js_sys::Array::new(); - for witness in witnesses { - result.push(&serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from)?); - } - Ok(result) - }}; - } - - #[macro_export] - macro_rules! verify_membership { - ($accum: ident, $element: ident, $witness: ident, $pk: ident, $params: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let witness: MembershipWit = serde_wasm_bindgen::from_value($witness)?; - let pk = deserialize_public_key($pk)?; - let params = deserialize_params($params)?; - Ok($accum.verify_membership(&element, &witness, &pk, ¶ms)) - }}; - } - - #[macro_export] - macro_rules! update_witness_post_add { - ($witness:expr, $element: ident, $addition: ident, $old_accumulated: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let addition = fr_from_uint8_array($addition, true)?; - let old_accumulated = g1_affine_from_uint8_array($old_accumulated)?; - serde_wasm_bindgen::to_value(&$witness.update_after_addition( - &element, - &addition, - &old_accumulated, - )) - .map_err(JsValue::from) - }}; - } - - #[macro_export] - macro_rules! update_witness_post_remove { - ($witness:expr, $element: ident, $removal: ident, $new_accumulated: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let removal = fr_from_uint8_array($removal, true)?; - let new_accumulated = g1_affine_from_uint8_array($new_accumulated)?; - let new_wit = $witness - .update_after_removal(&element, &removal, &new_accumulated) - .map_err(|e| { - JsValue::from(&format!( - "Evaluating update_after_removal returned error: {:?}", - e - )) - })?; - serde_wasm_bindgen::to_value(&new_wit).map_err(JsValue::from) - }}; - } - - #[macro_export] - macro_rules! update_witness_single_batch { - ($witness:expr, $element: ident, $additions: ident, $removals: ident, $public_info: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let additions = js_array_to_fr_vec(&$additions)?; - let removals = js_array_to_fr_vec(&$removals)?; - let public_info: Omega = ark_serialize::CanonicalDeserialize::deserialize_compressed(&$public_info.to_vec()[..]).map_err(|e| { - JsValue::from(&format!( - "Failed to deserialize public info from bytes due to error: {:?}", - e - )) - })?; - let new_witness = $witness - .update_using_public_info_after_batch_updates(&additions, &removals, &public_info, &element) - .map_err(|e| { - JsValue::from(&format!( - "Evaluating update_using_public_info_after_batch_updates returned error: {:?}", - e - )) - })?; - serde_wasm_bindgen::to_value(&new_witness).map_err(JsValue::from) - }} - } - - #[macro_export] - macro_rules! update_witness_multiple_batches { - ($witness:expr, $element: ident, $additions: ident, $removals: ident, $public_info: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - if $additions.length() == $removals.length() && $removals.length() == $public_info.length() { - let size = $additions.length(); - let mut updates_and_public_info = Vec::with_capacity(size as usize); - for i in 0..size { - let adds = js_array_to_fr_vec(&js_sys::Array::from(&$additions.get(i)))?; - let rems = js_array_to_fr_vec(&js_sys::Array::from(&$removals.get(i)))?; - let bytes: Vec = serde_wasm_bindgen::from_value($public_info.get(i))?; - let p: Omega = ark_serialize::CanonicalDeserialize::deserialize_compressed(&bytes[..]).map_err(|e| JsValue::from(&format!( - "Failed to deserialize public info from bytes due to error: {:?}", - e - )))?; - updates_and_public_info.push((adds, rems, p)); - } - let new_witness = $witness.update_using_public_info_after_multiple_batch_updates(updates_and_public_info.iter().map(|(a, r, p)| (a.as_slice(), r.as_slice(), p)).collect::>(), &element) - .map_err(|e| { - JsValue::from(&format!( - "Evaluating update_using_public_info_after_multiple_batch_updates returned error: {:?}", - e - )) - })?; - let w = serde_wasm_bindgen::to_value(&new_witness).map_err(JsValue::from)?; - Ok(w) - } else { - Err(JsValue::from(&format!( - "Expected same but found different lengths for additions, removals and public info: {} {} {}", - $additions.length(), $removals.length(), $public_info.length() - ))) - } - }} - } - - #[macro_export] - macro_rules! init_proof_protocol { - ($protocol:ident, $witness:ident, $element: ident, $blinding: ident, $public_key: ident, $params: ident, $prk: ident) => {{ - let element = fr_from_uint8_array($element, true)?; - let blinding = fr_from_uint8_array($blinding, true)?; - let pk = deserialize_public_key($public_key)?; - let params = deserialize_params($params)?; - - let mut rng = get_seeded_rng(); - let protocol = $protocol::init( - &mut rng, - element, - Some(blinding), - &$witness, - &pk, - ¶ms, - &$prk, - ); - serde_wasm_bindgen::to_value(&protocol).map_err(JsValue::from) - }}; - } - - #[macro_export] - macro_rules! verify_proof { - ($proof: ident, $accumulated:ident, $challenge: ident, $public_key: ident, $params: ident, $prk: ident) => {{ - let accumulated = g1_affine_from_uint8_array($accumulated)?; - let challenge = fr_from_uint8_array($challenge, false)?; - let pk = deserialize_public_key($public_key)?; - let params = deserialize_params($params)?; - to_verify_response!($proof.verify( - &accumulated, - &challenge, - pk.clone(), - params.clone(), - &$prk - )) - }}; - } - - #[macro_export] - macro_rules! update_using_secret_key_after_batch_updates { - ($witnesses: ident, $elements:ident, $additions: ident, $removals: ident, $old_accumulated: ident, $secret_key: ident, $wit_type: ident) => {{ - let elements = js_array_to_fr_vec(&$elements)?; - let additions = js_array_to_fr_vec(&$additions)?; - let removals = js_array_to_fr_vec(&$removals)?; - let old_accumulated = g1_affine_from_uint8_array($old_accumulated)?; - let sk: AccumSk = serde_wasm_bindgen::from_value($secret_key)?; - let mut wits = Vec::with_capacity($witnesses.length() as usize); - for w in $witnesses.values() { - wits.push(serde_wasm_bindgen::from_value::<$wit_type>(w.unwrap())?); - } - let new_wits = $wit_type::update_using_secret_key_after_batch_updates( - &additions, - &removals, - &elements, - &wits, - &old_accumulated, - &sk, - ) - .map_err(|e| { - JsValue::from(&format!( - "Evaluating update_using_secret_key_after_batch_updates returned error: {:?}", - e - )) - })?; - let result = js_sys::Array::new(); - for w in new_wits { - result.push(&serde_wasm_bindgen::to_value(&w).map_err(JsValue::from)?); - } - Ok(result) - }} - } -} - -pub fn encode_bytes_as_accumulator_member(bytes: &[u8]) -> Fr { - dock_crypto_utils::hashing_utils::field_elem_from_try_and_incr::( - &concat_slices!(bytes, b"Accumulator element"), - ) -} diff --git a/src/bddt16_kvac.rs b/src/bddt16_kvac.rs index 7a42a22..e145532 100644 --- a/src/bddt16_kvac.rs +++ b/src/bddt16_kvac.rs @@ -8,18 +8,17 @@ use crate::{ }, Fr, G1Affine, }; +use ark_ec::AffineRepr; use blake2::Blake2b512; use dock_crypto_utils::{ concat_slices, hashing_utils::affine_group_elem_from_try_and_incr, signature::MultiMessageSignatureParams, }; use kvac::bddt_2016::{ - mac::MAC, - setup::{MACParams, SecretKey, PublicKey}, + mac::{ProofOfValidityOfMAC, MAC}, + setup::{MACParams, PublicKey, SecretKey}, }; use std::collections::BTreeMap; -use ark_ec::AffineRepr; -use kvac::bddt_2016::mac::ProofOfValidityOfMAC; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use zeroize::Zeroize; @@ -111,7 +110,12 @@ pub fn bddt16_mac_generate_public_key_g1( #[wasm_bindgen(js_name = bddt16MacIsPublicKeyG1Valid)] pub fn bddt16_mac_is_pubkey_g1_valid(public_key: js_sys::Uint8Array) -> Result { set_panic_hook(); - let pk = obj_from_uint8array!(BDDT16MACPublicKeyG1, public_key, false, "BDDT16MACPublicKeyG1"); + let pk = obj_from_uint8array!( + BDDT16MACPublicKeyG1, + public_key, + false, + "BDDT16MACPublicKeyG1" + ); Ok(!pk.0.is_zero()) } @@ -228,7 +232,12 @@ pub fn bddt16_mac_proof_of_validity( set_panic_hook(); let mac = obj_from_uint8array!(BDDT16MAC, mac, true); let sk = obj_from_uint8array!(BDDT16MACSecretKey, secret_key, true, "BDDT16MACSecretKey"); - let pk = obj_from_uint8array!(BDDT16MACPublicKeyG1, public_key, false, "BDDT16MACPublicKeyG1"); + let pk = obj_from_uint8array!( + BDDT16MACPublicKeyG1, + public_key, + false, + "BDDT16MACPublicKeyG1" + ); let params: BDDT16MACParams = serde_wasm_bindgen::from_value(params)?; let mut rng = get_seeded_rng(); let proof = ProofOfValidityOfMACG1::new::<_, Blake2b512>(&mut rng, &mac, &sk, &pk, ¶ms); @@ -242,12 +251,17 @@ pub fn bddt16_mac_verify_proof_of_validity( messages: js_sys::Array, public_key: js_sys::Uint8Array, params: JsValue, - encode_messages: bool + encode_messages: bool, ) -> Result { let proof = obj_from_uint8array!(ProofOfValidityOfMACG1, proof, false); let mac = obj_from_uint8array!(BDDT16MAC, mac, true); let messages = encode_messages_as_js_array_to_fr_vec(&messages, encode_messages)?; - let pk = obj_from_uint8array!(BDDT16MACPublicKeyG1, public_key, false, "BDDT16MACPublicKeyG1"); + let pk = obj_from_uint8array!( + BDDT16MACPublicKeyG1, + public_key, + false, + "BDDT16MACPublicKeyG1" + ); let params: BDDT16MACParams = serde_wasm_bindgen::from_value(params)?; to_verify_response!(proof.verify::(&mac, messages.as_slice(), &pk, ¶ms)) } diff --git a/src/composite_proof_system/mod.rs b/src/composite_proof_system/mod.rs index 47e1441..8e4ea98 100644 --- a/src/composite_proof_system/mod.rs +++ b/src/composite_proof_system/mod.rs @@ -4,6 +4,9 @@ pub mod statements; use wasm_bindgen::prelude::*; use crate::{ + accumulator::vb_accumulator::{ + KbUniMembershipWit, KbUniNonMembershipWit, MembershipWit, NonMembershipWit, + }, bbs::BBSSignature, bbs_plus::BBSPlusSigG1, bddt16_kvac::BDDT16MAC, @@ -14,7 +17,6 @@ use crate::{ encode_messages_as_js_map_to_fr_btreemap, fr_from_uint8_array, get_seeded_rng, js_array_to_fr_vec, set_panic_hook, }, - vb_accumulator::{MembershipWit, NonMembershipWit}, G1Affine, }; use ark_bls12_381::Bls12_381; @@ -35,6 +37,8 @@ pub(crate) type PoKBBSPlusSigWit = witness::PoKBBSSignatureG1; pub(crate) type PokPSSigWit = witness::PoKPSSignature; pub(crate) type AccumMemWit = witness::Membership; pub(crate) type AccumNonMemWit = witness::NonMembership; +pub(crate) type KbUniAccumMemWit = witness::KBUniMembership; +pub(crate) type KbUniAccumNonMemWit = witness::KBUniNonMembership; pub(crate) type ProofSpec = proof_system::proof_spec::ProofSpec; pub(crate) type Proof = proof::Proof; pub(crate) type StatementProof = proof_system::prelude::StatementProof; @@ -104,6 +108,32 @@ pub fn generate_accumulator_non_membership_witness( serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from) } +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorMembershipWitness)] +pub fn generate_kb_universal_accumulator_membership_witness( + element: Uint8Array, + accum_witness: JsValue, +) -> Result { + set_panic_hook(); + let element = fr_from_uint8_array(element, true)?; + let accum_witness: KbUniMembershipWit = serde_wasm_bindgen::from_value(accum_witness)?; + let witness: witness::Witness = + KbUniAccumMemWit::new_as_witness(element, accum_witness); + serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorNonMembershipWitness)] +pub fn generate_kb_universal_accumulator_non_membership_witness( + element: Uint8Array, + accum_witness: JsValue, +) -> Result { + set_panic_hook(); + let element = fr_from_uint8_array(element, true)?; + let accum_witness: KbUniNonMembershipWit = serde_wasm_bindgen::from_value(accum_witness)?; + let witness: witness::Witness = + KbUniAccumNonMemWit::new_as_witness(element, accum_witness); + serde_wasm_bindgen::to_value(&witness).map_err(JsValue::from) +} + #[wasm_bindgen(js_name = generatePedersenCommitmentWitness)] pub fn generate_pedersen_commitment_witness(elements: js_sys::Array) -> Result { set_panic_hook(); diff --git a/src/composite_proof_system/setup_params.rs b/src/composite_proof_system/setup_params.rs index 5e97321..a87601b 100644 --- a/src/composite_proof_system/setup_params.rs +++ b/src/composite_proof_system/setup_params.rs @@ -5,6 +5,10 @@ use wasm_bindgen::prelude::*; use zeroize::Zeroize; use crate::{ + accumulator::{ + common::{AccumPk, AccumSetupParams}, + vb_accumulator::{MembershipPrk, NonMembershipPrk}, + }, bbs::BBSSigParams, bbs_plus::{BBSPlusPublicKeyG2, BBSPlusSigParamsG1}, bddt16_kvac::BDDT16MACParams, @@ -16,7 +20,6 @@ use crate::{ utils::{ js_array_to_fr_vec, js_array_to_g1_affine_vec, js_array_to_g2_affine_vec, set_panic_hook, }, - vb_accumulator::{AccumPk, AccumSetupParams, MembershipPrk, NonMembershipPrk}, G1Affine, }; diff --git a/src/composite_proof_system/statements/accumulator.rs b/src/composite_proof_system/statements/accumulator.rs index 588d77b..5a6d293 100644 --- a/src/composite_proof_system/statements/accumulator.rs +++ b/src/composite_proof_system/statements/accumulator.rs @@ -1,8 +1,9 @@ use crate::{ - utils::{g1_affine_from_uint8_array, set_panic_hook}, - vb_accumulator::{ - deserialize_params, deserialize_public_key, AccumSk, MembershipPrk, NonMembershipPrk, + accumulator::{ + common::{deserialize_params, deserialize_public_key, AccumSk}, + vb_accumulator::{MembershipPrk, NonMembershipPrk}, }, + utils::{g1_affine_from_uint8_array, set_panic_hook}, G1Affine, }; use ark_bls12_381::Bls12_381; @@ -13,11 +14,35 @@ use zeroize::Zeroize; pub(crate) type AccumMemStmt = prelude::accumulator::VBAccumulatorMembership; pub(crate) type AccumNonMemStmt = prelude::accumulator::VBAccumulatorNonMembership; + +pub(crate) type KBUniAccumMemProverStmt = + prelude::accumulator::cdh::KBUniversalAccumulatorMembershipCDHProver; +pub(crate) type KBUniAccumMemVerifierStmt = + prelude::accumulator::cdh::KBUniversalAccumulatorMembershipCDHVerifier; +pub(crate) type KBUniAccumNonMemProverStmt = + prelude::accumulator::cdh::KBUniversalAccumulatorNonMembershipCDHProver; +pub(crate) type KBUniAccumNonMemVerifierStmt = + prelude::accumulator::cdh::KBUniversalAccumulatorNonMembershipCDHVerifier; + pub(crate) type AccumMemStmtKV = prelude::accumulator::keyed_verification::VBAccumulatorMembershipKV; pub(crate) type AccumMemStmtKVFullVerifier = prelude::accumulator::keyed_verification::VBAccumulatorMembershipKVFullVerifier; +pub(crate) type KBAccumMemStmtKV = + prelude::accumulator::keyed_verification::KBUniversalAccumulatorMembershipKV; +pub(crate) type KBAccumMemStmtKVFullVerifier = + prelude::accumulator::keyed_verification::KBUniversalAccumulatorMembershipKVFullVerifier< + G1Affine, + >; + +pub(crate) type KBAccumNonMemStmtKV = + prelude::accumulator::keyed_verification::KBUniversalAccumulatorNonMembershipKV; +pub(crate) type KBAccumNonMemStmtKVFullVerifier = + prelude::accumulator::keyed_verification::KBUniversalAccumulatorNonMembershipKVFullVerifier< + G1Affine, + >; + #[wasm_bindgen(js_name = generateAccumulatorMembershipStatement)] pub fn generate_accumulator_membership_statement( params: Uint8Array, @@ -121,3 +146,155 @@ pub fn generate_accumulator_kv_full_verifier_membership_statement( "AccumMemStmtKVFullVerifier" )) } + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorKVMembershipStatement)] +pub fn generate_kb_universal_accumulator_kv_membership_statement( + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBAccumMemStmtKV::new::(accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBAccumMemStmtKV" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorKVFullVerifierMembershipStatement)] +pub fn generate_kb_universal_accumulator_kv_full_verifier_membership_statement( + secret_key: Uint8Array, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "AccumSk"); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBAccumMemStmtKVFullVerifier::new::(accumulated, sk); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBAccumMemStmtKVFullVerifier" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorKVNonMembershipStatement)] +pub fn generate_kb_universal_accumulator_kv_non_membership_statement( + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBAccumNonMemStmtKV::new::(accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBAccumNonMemStmtKV" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement)] +pub fn generate_kb_universal_accumulator_kv_full_verifier_non_membership_statement( + secret_key: Uint8Array, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "AccumSk"); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBAccumNonMemStmtKVFullVerifier::new::(accumulated, sk); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBAccumNonMemStmtKVFullVerifier" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorMembershipProverStatement)] +pub fn generate_kb_universal_accumulator_membership_prover_statement( + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBUniAccumMemProverStmt::new(accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumMemProverStmt" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorMembershipVerifierStatement)] +pub fn generate_kb_universal_accumulator_membership_verifier_statement( + params: Uint8Array, + public_key: Uint8Array, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let pk = deserialize_public_key(public_key)?; + let params = deserialize_params(params)?; + let statement = KBUniAccumMemVerifierStmt::new_statement_from_params(params, pk, accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumMemVerifierStmt" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs)] +pub fn generate_kb_universal_accumulator_membership_verifier_statement_from_param_refs( + params: usize, + public_key: usize, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = + KBUniAccumMemVerifierStmt::new_statement_from_params_ref(params, public_key, accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumMemVerifierStmt" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorNonMembershipProverStatement)] +pub fn generate_kb_universal_accumulator_non_membership_prover_statement( + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBUniAccumNonMemProverStmt::new(accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumNonMemProverStmt" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorNonMembershipVerifierStatement)] +pub fn generate_kb_universal_accumulator_non_membership_verifier_statement( + params: Uint8Array, + public_key: Uint8Array, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let pk = deserialize_public_key(public_key)?; + let params = deserialize_params(params)?; + let statement = + KBUniAccumNonMemVerifierStmt::new_statement_from_params(params, pk, accumulated); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumNonMemVerifierStmt" + )) +} + +#[wasm_bindgen(js_name = generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs)] +pub fn generate_kb_universal_accumulator_non_membership_verifier_statement_from_param_refs( + params: usize, + public_key: usize, + accumulated: Uint8Array, +) -> Result { + set_panic_hook(); + let accumulated = g1_affine_from_uint8_array(accumulated)?; + let statement = KBUniAccumNonMemVerifierStmt::new_statement_from_params_ref( + params, + public_key, + accumulated, + ); + Ok(obj_to_uint8array_uncompressed!( + &statement, + "KBUniAccumNonMemVerifierStmt" + )) +} diff --git a/src/delegated_proof.rs b/src/delegated_proof.rs index 2454f18..0820e90 100644 --- a/src/delegated_proof.rs +++ b/src/delegated_proof.rs @@ -1,11 +1,17 @@ use crate::{ - bddt16_kvac::BDDT16MACSecretKey, common::VerifyResponse, composite_proof_system::Proof, - to_verify_response, utils::set_panic_hook, vb_accumulator::AccumSk, G1Affine, + accumulator::common::AccumSk, bddt16_kvac::BDDT16MACSecretKey, common::VerifyResponse, + composite_proof_system::Proof, to_verify_response, utils::set_panic_hook, G1Affine, }; use js_sys::Uint8Array; use kvac::bddt_2016::delegated_proof::DelegatedProof as BDDT16Dp; use proof_system::prelude::StatementProof; -use vb_accumulator::proofs_keyed_verification::DelegatedMembershipProof as VBMemBp; +use vb_accumulator::{ + kb_universal_accumulator::proofs_keyed_verification::{ + KBUniversalAccumulatorDelegatedMembershipProof as KBUniMemDp, + KBUniversalAccumulatorDelegatedNonMembershipProof as KBUniNonMemDp, + }, + proofs_keyed_verification::DelegatedMembershipProof as VBMemDp, +}; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use zeroize::Zeroize; @@ -30,6 +36,20 @@ pub fn get_all_delegated_subproofs_from_proof(proof: Uint8Array) -> Result { + let dp = p.to_delegated_proof(); + let val = js_sys::Array::new(); + val.push(&JsValue::from(2_u32)); + val.push(&JsValue::from(obj_to_uint8array!(&dp, false))); + r.set(&JsValue::from(i as u32), &val); + } + StatementProof::KBUniversalAccumulatorNonMembershipKV(p) => { + let dp = p.to_delegated_proof(); + let val = js_sys::Array::new(); + val.push(&JsValue::from(3_u32)); + val.push(&JsValue::from(obj_to_uint8array!(&dp, false))); + r.set(&JsValue::from(i as u32), &val); + } _ => (), } } @@ -54,7 +74,7 @@ pub fn verify_vb_accum_membership_delegated_proof( ) -> Result { set_panic_hook(); let proof = obj_from_uint8array!( - VBMemBp, + VBMemDp, proof, false, "VBMembershipDelegatedProof" @@ -62,3 +82,35 @@ pub fn verify_vb_accum_membership_delegated_proof( let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); to_verify_response!(proof.verify(&sk)) } + +#[wasm_bindgen(js_name = verifyKBUniAccumMembershipDelegatedProof)] +pub fn verify_kb_uni_accum_membership_delegated_proof( + proof: Uint8Array, + secret_key: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!( + KBUniMemDp, + proof, + false, + "KBUniversalAccumulatorDelegatedMembershipProof" + ); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + to_verify_response!(proof.verify(&sk)) +} + +#[wasm_bindgen(js_name = verifyKBUniAccumNonMembershipDelegatedProof)] +pub fn verify_kb_uni_accum_non_membership_delegated_proof( + proof: Uint8Array, + secret_key: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!( + KBUniNonMemDp, + proof, + false, + "KBUniversalAccumulatorDelegatedNonMembershipProof" + ); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + to_verify_response!(proof.verify(&sk)) +} diff --git a/src/js/composite_proof_system_wasm.js b/src/js/composite_proof_system_wasm.js index 6606ff6..a2720c8 100644 --- a/src/js/composite_proof_system_wasm.js +++ b/src/js/composite_proof_system_wasm.js @@ -72,6 +72,36 @@ module.exports.generateAccumulatorNonMembershipStatementFromParamRefs = (params, return wasm.generateAccumulatorNonMembershipStatementFromParamRefs(params, publicKey, provingKey, accumulated); }; +module.exports.generateKBUniversalAccumulatorMembershipProverStatement = (accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorMembershipProverStatement(accumulated); +}; + +module.exports.generateKBUniversalAccumulatorMembershipVerifierStatement = (params, publicKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorMembershipVerifierStatement(params, publicKey, accumulated); +}; + +module.exports.generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs = (params, publicKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs(params, publicKey, accumulated); +}; + +module.exports.generateKBUniversalAccumulatorNonMembershipProverStatement = (accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorNonMembershipProverStatement(accumulated); +}; + +module.exports.generateKBUniversalAccumulatorNonMembershipVerifierStatement = (params, publicKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorNonMembershipVerifierStatement(params, publicKey, accumulated); +}; + +module.exports.generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs = (params, publicKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs(params, publicKey, accumulated); +}; + module.exports.generatePedersenCommitmentG1Statement = (bases, commitment) => { requireWasmInitialized(); return wasm.generatePedersenCommitmentG1Statement(bases, commitment); @@ -232,6 +262,16 @@ module.exports.generateAccumulatorNonMembershipWitness = (element, witness) => { return wasm.generateAccumulatorNonMembershipWitness(element, witness); }; +module.exports.generateKBUniversalAccumulatorMembershipWitness = (element, witness) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorMembershipWitness(element, witness); +}; + +module.exports.generateKBUniversalAccumulatorNonMembershipWitness = (element, witness) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorNonMembershipWitness(element, witness); +}; + module.exports.generatePedersenCommitmentWitness = (elements) => { requireWasmInitialized(); return wasm.generatePedersenCommitmentWitness(elements); @@ -345,4 +385,24 @@ module.exports.generateAccumulatorKVMembershipStatement = (accumulated) => { module.exports.generateAccumulatorKVFullVerifierMembershipStatement = (secretKey, accumulated) => { requireWasmInitialized(); return wasm.generateAccumulatorKVFullVerifierMembershipStatement(secretKey, accumulated); +}; + +module.exports.generateKBUniversalAccumulatorKVMembershipStatement = (accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorKVMembershipStatement(accumulated); +}; + +module.exports.generateKBUniversalAccumulatorKVFullVerifierMembershipStatement = (secretKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorKVFullVerifierMembershipStatement(secretKey, accumulated); +}; + +module.exports.generateKBUniversalAccumulatorKVNonMembershipStatement = (accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorKVNonMembershipStatement(accumulated); +}; + +module.exports.generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement = (secretKey, accumulated) => { + requireWasmInitialized(); + return wasm.generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement(secretKey, accumulated); }; \ No newline at end of file diff --git a/src/js/delegated_proof_wasm.js b/src/js/delegated_proof_wasm.js index 4f3698f..4da15fc 100644 --- a/src/js/delegated_proof_wasm.js +++ b/src/js/delegated_proof_wasm.js @@ -16,3 +16,13 @@ module.exports.verifyVBAccumMembershipDelegatedProof = (proof, secretKey) => { requireWasmInitialized(); return wasm.verifyVBAccumMembershipDelegatedProof(proof, secretKey); }; + +module.exports.verifyKBUniAccumMembershipDelegatedProof = (proof, secretKey) => { + requireWasmInitialized(); + return wasm.verifyKBUniAccumMembershipDelegatedProof(proof, secretKey); +}; + +module.exports.verifyKBUniAccumNonMembershipDelegatedProof = (proof, secretKey) => { + requireWasmInitialized(); + return wasm.verifyKBUniAccumNonMembershipDelegatedProof(proof, secretKey); +}; \ No newline at end of file diff --git a/src/js/kb_accumulator_wasm.js b/src/js/kb_accumulator_wasm.js new file mode 100644 index 0000000..a9e661f --- /dev/null +++ b/src/js/kb_accumulator_wasm.js @@ -0,0 +1,138 @@ +const { + wasm, requireWasmInitialized +} = require('./init_wasm'); + +module.exports.kbUniversalAccumulatorInitialise = (domain, secretKey, params) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorInitialise(domain, secretKey, params) +}; + +module.exports.kbUniversalAccumulatorComputeExtended = (oldAccum, newElements, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorComputeExtended(oldAccum, newElements, secretKey) +}; + +module.exports.kbUniversalAccumulatorAdd = (accumulator, element, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorAdd(accumulator, element, secretKey) +}; + +module.exports.kbUniversalAccumulatorRemove = (accumulator, element, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorRemove(accumulator, element, secretKey) +}; + +module.exports.kbUniversalAccumulatorMembershipWitness = (accumulator, element, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorMembershipWitness(accumulator, element, secretKey) +}; + +module.exports.kbUniversalAccumulatorVerifyMembership = (accumulator, element, witness, publicKey, params) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorVerifyMembership(accumulator, element, witness, publicKey, params) +}; + +module.exports.kbUniversalAccumulatorNonMembershipWitness = (accumulator, element, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorNonMembershipWitness(accumulator, element, secretKey) +}; + +module.exports.kbUniversalAccumulatorVerifyNonMembership = (accumulator, element, witness, publicKey, params) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorVerifyNonMembership(accumulator, element, witness, publicKey, params) +}; + +module.exports.kbUniversalAccumulatorAddBatch = (accumulator, elements, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorAddBatch(accumulator, elements, secretKey) +}; + +module.exports.kbUniversalAccumulatorRemoveBatch = (accumulator, elements, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorRemoveBatch(accumulator, elements, secretKey) +}; + +module.exports.kbUniversalAccumulatorBatchUpdates = (accumulator, additions, removals, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorBatchUpdates(accumulator, additions, removals, secretKey) +}; + +module.exports.kbUniversalAccumulatorMembershipWitnessesForBatch = (accumulator, elements, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorMembershipWitnessesForBatch(accumulator, elements, secretKey) +}; + +module.exports.kbUniversalAccumulatorNonMembershipWitnessesForBatch = (accumulator, elements, secretKey) => { + requireWasmInitialized(); + return wasm.kbUniversalAccumulatorNonMembershipWitnessesForBatch(accumulator, elements, secretKey) +}; + +module.exports.kbUniversalUpdateMembershipWitnessPostAdd = (witness, member, addition, oldAccumulator) => { + requireWasmInitialized(); + return wasm.kbUniversalUpdateMembershipWitnessPostAdd(witness, member, addition, oldAccumulator) +}; + +module.exports.kbUniversalUpdateMembershipWitnessPostRemove = (witness, member, removal, oldAccumulator) => { + requireWasmInitialized(); + return wasm.kbUniversalUpdateMembershipWitnessPostRemove(witness, member, removal, oldAccumulator) +}; + +module.exports.kbUniversalUpdateNonMembershipWitnessPostAdd = (witness, member, addition, oldAccumulator) => { + requireWasmInitialized(); + return wasm.kbUniversalUpdateNonMembershipWitnessPostAdd(witness, member, addition, oldAccumulator) +}; + +module.exports.kbUniversalUpdateNonMembershipWitnessPostRemove = (witness, member, removal, oldAccumulator) => { + requireWasmInitialized(); + return wasm.kbUniversalUpdateNonMembershipWitnessPostRemove(witness, member, removal, oldAccumulator) +}; + +module.exports.kbUpdateMembershipWitnessesPostBatchUpdates = (witnesses, members, additions, removals, oldAccumulator, secretKey) => { + requireWasmInitialized(); + return wasm.kbUpdateMembershipWitnessesPostBatchUpdates(witnesses, members, additions, removals, oldAccumulator, secretKey) +}; + +module.exports.kbUpdateNonMembershipWitnessesPostBatchUpdates = (witnesses, nonMembers, additions, removals, oldAccumulator, secretKey) => { + requireWasmInitialized(); + return wasm.kbUpdateNonMembershipWitnessesPostBatchUpdates(witnesses, nonMembers, additions, removals, oldAccumulator, secretKey) +}; + +module.exports.kbUpdateBothWitnessesPostBatchUpdates = (memWitnesses, members, nonMemWitnesses, nonMembers, additions, removals, oldAccumulator, secretKey) => { + requireWasmInitialized(); + return wasm.kbUpdateBothWitnessesPostBatchUpdates(memWitnesses, members, nonMemWitnesses, nonMembers, additions, removals, oldAccumulator, secretKey) +}; + +module.exports.publicInfoForKBUniversalMemWitnessUpdate = (oldAccumulator, additions, removals, secretKey) => { + requireWasmInitialized(); + return wasm.publicInfoForKBUniversalMemWitnessUpdate(oldAccumulator, additions, removals, secretKey) +}; + +module.exports.publicInfoForKBUniversalNonMemWitnessUpdate = (oldAccumulator, additions, removals, secretKey) => { + requireWasmInitialized(); + return wasm.publicInfoForKBUniversalNonMemWitnessUpdate(oldAccumulator, additions, removals, secretKey) +}; + +module.exports.publicInfoForBothKBUniversalWitnessUpdate = (oldAccumulator, additions, removals, secretKey) => { + requireWasmInitialized(); + return wasm.publicInfoForBothKBUniversalWitnessUpdate(oldAccumulator, additions, removals, secretKey) +}; + +module.exports.updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate = (witness, member, additions, removals, publicInfo) => { + requireWasmInitialized(); + return wasm.updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate(witness, member, additions, removals, publicInfo) +}; + +module.exports.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate = (witness, nonMember, additions, removals, publicInfo) => { + requireWasmInitialized(); + return wasm.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate(witness, nonMember, additions, removals, publicInfo) +}; + +module.exports.updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates = (witness, member, additions, removals, publicInfo) => { + requireWasmInitialized(); + return wasm.updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(witness, member, additions, removals, publicInfo) +}; + +module.exports.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates = (witness, nonMember, additions, removals, publicInfo) => { + requireWasmInitialized(); + return wasm.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(witness, nonMember, additions, removals, publicInfo) +}; \ No newline at end of file diff --git a/src/js/type_declarations/composite_proof_system.d.ts b/src/js/type_declarations/composite_proof_system.d.ts index 3624ba9..326b5ad 100644 --- a/src/js/type_declarations/composite_proof_system.d.ts +++ b/src/js/type_declarations/composite_proof_system.d.ts @@ -92,6 +92,38 @@ export function generateAccumulatorNonMembershipStatementFromParamRefs( accumulated: Uint8Array ): Uint8Array; +export function generateKBUniversalAccumulatorMembershipProverStatement( + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorMembershipVerifierStatement( + params: Uint8Array, + publicKey: Uint8Array, + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs( + params: number, + publicKey: number, + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorNonMembershipProverStatement( + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorNonMembershipVerifierStatement( + params: Uint8Array, + publicKey: Uint8Array, + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs( + params: number, + publicKey: number, + accumulated: Uint8Array +): Uint8Array; + export function generatePedersenCommitmentG1Statement( bases: Uint8Array[], commitment: Uint8Array @@ -293,6 +325,16 @@ export function generateAccumulatorNonMembershipWitness( accumulatorWitness: INonMembershipWitness ): Uint8Array; +export function generateKBUniversalAccumulatorMembershipWitness( + element: Uint8Array, + accumulatorWitness: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorNonMembershipWitness( + element: Uint8Array, + accumulatorWitness: Uint8Array +): Uint8Array; + export function generatePedersenCommitmentWitness( elements: Uint8Array[] ): Uint8Array; @@ -418,3 +460,21 @@ export function generateAccumulatorKVFullVerifierMembershipStatement( secretKey: Uint8Array, accumulated: Uint8Array ): Uint8Array; + +export function generateKBUniversalAccumulatorKVMembershipStatement( + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorKVFullVerifierMembershipStatement( + secretKey: Uint8Array, + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorKVNonMembershipStatement( + accumulated: Uint8Array +): Uint8Array; + +export function generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement( + secretKey: Uint8Array, + accumulated: Uint8Array +): Uint8Array; diff --git a/src/js/type_declarations/delegated_proof.d.ts b/src/js/type_declarations/delegated_proof.d.ts index 54d66ac..a833546 100644 --- a/src/js/type_declarations/delegated_proof.d.ts +++ b/src/js/type_declarations/delegated_proof.d.ts @@ -4,4 +4,8 @@ export function getAllDelegatedSubproofsFromProof(proof: Uint8Array): Map; -export function verifyVBAccumMembershipDelegatedProof(proof: Uint8Array, secretKey: Uint8Array): Required; \ No newline at end of file +export function verifyVBAccumMembershipDelegatedProof(proof: Uint8Array, secretKey: Uint8Array): Required; + +export function verifyKBUniAccumMembershipDelegatedProof(proof: Uint8Array, secretKey: Uint8Array): Required; + +export function verifyKBUniAccumNonMembershipDelegatedProof(proof: Uint8Array, secretKey: Uint8Array): Required; \ No newline at end of file diff --git a/src/js/type_declarations/index.d.ts b/src/js/type_declarations/index.d.ts index 91e5ac4..0b88969 100644 --- a/src/js/type_declarations/index.d.ts +++ b/src/js/type_declarations/index.d.ts @@ -13,4 +13,6 @@ export * from "./frost_dkg"; export * from "./threshold_sig"; export * from "./bdd16_kvac"; export * from "./delegated_proof"; +export * from "./kb_accumulator"; + diff --git a/src/js/type_declarations/kb_accumulator.d.ts b/src/js/type_declarations/kb_accumulator.d.ts new file mode 100644 index 0000000..d074ffe --- /dev/null +++ b/src/js/type_declarations/kb_accumulator.d.ts @@ -0,0 +1,194 @@ +import {IKBUniversalAccumulator} from "../types"; + +export function kbUniversalAccumulatorInitialise( + domain: Uint8Array[], + secretKey: Uint8Array, + params: Uint8Array +): Required; + +export function kbUniversalAccumulatorComputeExtended( + oldAccum: IKBUniversalAccumulator, + newElements: Uint8Array[], + secretKey: Uint8Array, +): Required; + +export function kbUniversalAccumulatorAdd( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + secretKey: Uint8Array, +): IKBUniversalAccumulator; + +export function kbUniversalAccumulatorRemove( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + secretKey: Uint8Array, +): IKBUniversalAccumulator; + +export function kbUniversalAccumulatorMembershipWitness( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + secretKey: Uint8Array, +): Uint8Array; + +export function kbUniversalAccumulatorVerifyMembership( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + witness: Uint8Array, + publicKey: Uint8Array, + params: Uint8Array, +): boolean; + +export function kbUniversalAccumulatorNonMembershipWitness( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + secretKey: Uint8Array, +): Uint8Array; + +export function kbUniversalAccumulatorVerifyNonMembership( + accumulator: IKBUniversalAccumulator, + element: Uint8Array, + witness: Uint8Array, + publicKey: Uint8Array, + params: Uint8Array, +): boolean; + +export function kbUniversalAccumulatorAddBatch( + accumulator: IKBUniversalAccumulator, + elements: Uint8Array[], + secretKey: Uint8Array, +): IKBUniversalAccumulator; + +export function kbUniversalAccumulatorRemoveBatch( + accumulator: IKBUniversalAccumulator, + elements: Uint8Array[], + secretKey: Uint8Array, +): IKBUniversalAccumulator; + +export function kbUniversalAccumulatorBatchUpdates( + accumulator: IKBUniversalAccumulator, + additions: Uint8Array[], + removals: Uint8Array[], + secretKey: Uint8Array, +): IKBUniversalAccumulator; + +export function kbUniversalAccumulatorMembershipWitnessesForBatch( + accumulator: IKBUniversalAccumulator, + elements: Uint8Array[], + secretKey: Uint8Array, +): Uint8Array[]; + +export function kbUniversalAccumulatorNonMembershipWitnessesForBatch( + accumulator: IKBUniversalAccumulator, + elements: Uint8Array[], + secretKey: Uint8Array, +): Uint8Array[]; + +export function kbUniversalUpdateMembershipWitnessPostAdd( + witness: Uint8Array, + member: Uint8Array, + addition: Uint8Array, + oldAccumulator: IKBUniversalAccumulator, +): Uint8Array; + +export function kbUniversalUpdateMembershipWitnessPostRemove( + witness: Uint8Array, + member: Uint8Array, + removal: Uint8Array, + newAccumulator: IKBUniversalAccumulator, +): Uint8Array; + +export function kbUniversalUpdateNonMembershipWitnessPostAdd( + witness: Uint8Array, + nonMember: Uint8Array, + addition: Uint8Array, + oldAccumulator: IKBUniversalAccumulator, +): Uint8Array; + +export function kbUniversalUpdateNonMembershipWitnessPostRemove( + witness: Uint8Array, + nonMember: Uint8Array, + removal: Uint8Array, + newAccumulator: IKBUniversalAccumulator, +): Uint8Array; + +export function kbUpdateMembershipWitnessesPostBatchUpdates( + witnesses: Uint8Array[], + members: Uint8Array[], + additions: Uint8Array[], + removals: Uint8Array[], + oldAccumulator: IKBUniversalAccumulator, + secretKey: Uint8Array, +): Uint8Array[]; + +export function kbUpdateNonMembershipWitnessesPostBatchUpdates( + witnesses: Uint8Array[], + nonMembers: Uint8Array[], + additions: Uint8Array[], + removals: Uint8Array[], + oldAccumulator: IKBUniversalAccumulator, + secretKey: Uint8Array, +): Uint8Array[]; + +export function kbUpdateBothWitnessesPostBatchUpdates( + memWitnesses: Uint8Array[], + members: Uint8Array[], + nonMemWitnesses: Uint8Array[], + nonMembers: Uint8Array[], + additions: Uint8Array[], + removals: Uint8Array[], + oldAccumulator: IKBUniversalAccumulator, + secretKey: Uint8Array, +): [Uint8Array[], Uint8Array[]]; + +export function publicInfoForKBUniversalMemWitnessUpdate( + oldAccumulator: IKBUniversalAccumulator, + additions: Uint8Array[], + removals: Uint8Array[], + secretKey: Uint8Array, +): Uint8Array; + +export function publicInfoForKBUniversalNonMemWitnessUpdate( + oldAccumulator: IKBUniversalAccumulator, + additions: Uint8Array[], + removals: Uint8Array[], + secretKey: Uint8Array, +): Uint8Array; + +export function publicInfoForBothKBUniversalWitnessUpdate( + oldAccumulator: IKBUniversalAccumulator, + additions: Uint8Array[], + removals: Uint8Array[], + secretKey: Uint8Array, +): [Uint8Array, Uint8Array]; + +export function updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate( + witness: Uint8Array, + member: Uint8Array, + additions: Uint8Array[], + removals: Uint8Array[], + publicInfo: Uint8Array, +): Uint8Array; + +export function updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate( + witness: Uint8Array, + nonMember: Uint8Array, + additions: Uint8Array[], + removals: Uint8Array[], + publicInfo: Uint8Array, +): Uint8Array; + +export function updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates( + witness: Uint8Array, + member: Uint8Array, + additions: Uint8Array[][], + removals: Uint8Array[][], + publicInfo: Uint8Array[], +): Uint8Array; + +export function updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates( + witness: Uint8Array, + nonMember: Uint8Array, + additions: Uint8Array[][], + removals: Uint8Array[][], + publicInfo: Uint8Array[], +): Uint8Array; \ No newline at end of file diff --git a/src/js/types/IKBUniversalAccumulator.ts b/src/js/types/IKBUniversalAccumulator.ts new file mode 100644 index 0000000..15f2924 --- /dev/null +++ b/src/js/types/IKBUniversalAccumulator.ts @@ -0,0 +1,7 @@ +/** + * KB universal accumulator + */ +export interface IKBUniversalAccumulator { + readonly mem: Uint8Array; + readonly non_mem: Uint8Array; +} \ No newline at end of file diff --git a/src/js/types/INonMembershipWitness.ts b/src/js/types/INonMembershipWitness.ts index 34dfb82..80a8264 100644 --- a/src/js/types/INonMembershipWitness.ts +++ b/src/js/types/INonMembershipWitness.ts @@ -1,3 +1,6 @@ +/** + * Non membership witness for VB universal accumulator + */ export interface INonMembershipWitness { readonly d: Uint8Array; readonly C: Uint8Array diff --git a/src/js/types/IUniversalAccumulator.ts b/src/js/types/IUniversalAccumulator.ts index 818def1..f774f57 100644 --- a/src/js/types/IUniversalAccumulator.ts +++ b/src/js/types/IUniversalAccumulator.ts @@ -1,3 +1,6 @@ +/** + * VB universal accumulator + */ export interface IUniversalAccumulator { readonly f_V: Uint8Array; readonly V: Uint8Array; diff --git a/src/js/types/index.ts b/src/js/types/index.ts index 6e0f743..d67a82c 100644 --- a/src/js/types/index.ts +++ b/src/js/types/index.ts @@ -30,3 +30,4 @@ export { type IUniversalAccumulator } from "./IUniversalAccumulator"; export { type INonMembershipWitness } from "./INonMembershipWitness"; export { type LCTerm, type LC, type Constraint, type R1CS } from "./R1CS"; export { type Bddt16MacParams } from "./Bddt16MacParams"; +export { type IKBUniversalAccumulator } from "./IKBUniversalAccumulator"; diff --git a/src/js/wasm_module.js b/src/js/wasm_module.js index bc15e0c..91ec909 100644 --- a/src/js/wasm_module.js +++ b/src/js/wasm_module.js @@ -36,4 +36,5 @@ module.exports = { ...require('./threshold_sig_wasm'), ...require('./bdd16_kvac_wasm'), ...require('./delegated_proof_wasm'), + ...require('./kb_accumulator_wasm'), }; diff --git a/src/lib.rs b/src/lib.rs index 07e8819..f4c505b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ use ark_ec::pairing::Pairing; #[macro_use] pub mod utils; +pub mod accumulator; pub mod bbs; pub mod bbs_plus; mod bddt16_kvac; @@ -33,7 +34,6 @@ pub mod ps; pub mod r1cs; pub mod saver; pub mod threshold_sig; -pub mod vb_accumulator; // Trying to keep types at one place so changing the curve is easier pub(crate) type Fr = ::ScalarField; diff --git a/tests/js/accumulator.spec.ts b/tests/js/accumulator.spec.ts index 9afdbd2..44446f8 100644 --- a/tests/js/accumulator.spec.ts +++ b/tests/js/accumulator.spec.ts @@ -67,7 +67,7 @@ import { import {stringToBytes} from "./util"; -describe("For Positive accumulator", () => { +describe("For VB positive accumulator", () => { let params: Uint8Array, sk: Uint8Array, pk: Uint8Array, @@ -147,7 +147,7 @@ describe("For Positive accumulator", () => { const witness1 = positiveAccumulatorMembershipWitness(accumulator1, e1, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, e1, witness1, pk, @@ -159,7 +159,7 @@ describe("For Positive accumulator", () => { const witness2 = positiveAccumulatorMembershipWitness(accumulator2, e2, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e2, witness2, pk, @@ -168,7 +168,7 @@ describe("For Positive accumulator", () => { ).toBe(true); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e1, witness1, pk, @@ -187,7 +187,7 @@ describe("For Positive accumulator", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator3), + accumulator3, e1, witness11, pk, @@ -197,7 +197,7 @@ describe("For Positive accumulator", () => { expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator3), + accumulator3, e1, witness1, pk, @@ -221,7 +221,7 @@ describe("For Positive accumulator", () => { const witness1 = positiveAccumulatorMembershipWitness(accumulator1, e1, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, e1, witness1, pk, @@ -231,7 +231,7 @@ describe("For Positive accumulator", () => { const witness2 = positiveAccumulatorMembershipWitness(accumulator1, e2, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, e2, witness2, pk, @@ -241,7 +241,7 @@ describe("For Positive accumulator", () => { const witness3 = positiveAccumulatorMembershipWitness(accumulator1, e3, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, e3, witness3, pk, @@ -251,7 +251,7 @@ describe("For Positive accumulator", () => { const witness4 = positiveAccumulatorMembershipWitness(accumulator1, e4, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, e4, witness4, pk, @@ -269,7 +269,7 @@ describe("For Positive accumulator", () => { expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e1, witness1, pk, @@ -278,7 +278,7 @@ describe("For Positive accumulator", () => { ).toBe(false); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e2, witness2, pk, @@ -287,7 +287,7 @@ describe("For Positive accumulator", () => { ).toBe(false); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e3, witness3, pk, @@ -296,7 +296,7 @@ describe("For Positive accumulator", () => { ).toBe(false); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e4, witness4, pk, @@ -311,7 +311,7 @@ describe("For Positive accumulator", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e2, witness22, pk, @@ -325,7 +325,7 @@ describe("For Positive accumulator", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator2), + accumulator2, e4, witness42, pk, @@ -346,7 +346,7 @@ describe("For Positive accumulator", () => { const witness5 = positiveAccumulatorMembershipWitness(accumulator3, e5, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator3), + accumulator3, e5, witness5, pk, @@ -356,7 +356,7 @@ describe("For Positive accumulator", () => { const witness6 = positiveAccumulatorMembershipWitness(accumulator3, e6, sk); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator3), + accumulator3, e6, witness6, pk, @@ -394,7 +394,7 @@ describe("For Positive accumulator", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, batch[0], witnesses[0], pk, @@ -403,7 +403,7 @@ describe("For Positive accumulator", () => { ).toBe(true); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, batch[1], witnesses[1], pk, @@ -412,7 +412,7 @@ describe("For Positive accumulator", () => { ).toBe(true); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(accumulator1), + accumulator1, batch[2], witnesses[2], pk, @@ -422,7 +422,7 @@ describe("For Positive accumulator", () => { }); }); -describe("For Universal accumulator", () => { +describe("For VB universal accumulator", () => { let params: Uint8Array, sk: Uint8Array, pk: Uint8Array, @@ -506,7 +506,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, e1, witness1, pk, @@ -522,7 +522,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e2, witness2, pk, @@ -531,7 +531,7 @@ describe("For Universal accumulator", () => { ).toBe(true); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e1, witness1, pk, @@ -550,7 +550,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, e1, witness11, pk, @@ -560,7 +560,7 @@ describe("For Universal accumulator", () => { expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, e1, witness1, pk, @@ -583,7 +583,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, nonMember, witness, pk, @@ -609,7 +609,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, nonMember, nmWitness, pk, @@ -639,7 +639,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, e1, witness1, pk, @@ -653,7 +653,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, e2, witness2, pk, @@ -667,7 +667,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, e3, witness3, pk, @@ -681,7 +681,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, e4, witness4, pk, @@ -699,7 +699,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, nonMember, nmWitness, pk, @@ -717,7 +717,7 @@ describe("For Universal accumulator", () => { expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e1, witness1, pk, @@ -726,7 +726,7 @@ describe("For Universal accumulator", () => { ).toBe(false); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e2, witness2, pk, @@ -735,7 +735,7 @@ describe("For Universal accumulator", () => { ).toBe(false); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e3, witness3, pk, @@ -744,7 +744,7 @@ describe("For Universal accumulator", () => { ).toBe(false); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e4, witness4, pk, @@ -759,7 +759,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e2, witness22, pk, @@ -773,7 +773,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, e4, witness42, pk, @@ -791,7 +791,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator2), + accumulator2.V, nonMember, nmWitness, pk, @@ -816,7 +816,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, e5, witness5, pk, @@ -830,7 +830,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, e6, witness6, pk, @@ -848,7 +848,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator3), + accumulator3.V, nonMember, nmWitness, pk, @@ -887,7 +887,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, batch[0], witnesses[0], pk, @@ -896,7 +896,7 @@ describe("For Universal accumulator", () => { ).toBe(true); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, batch[1], witnesses[1], pk, @@ -905,7 +905,7 @@ describe("For Universal accumulator", () => { ).toBe(true); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, batch[2], witnesses[2], pk, @@ -928,7 +928,7 @@ describe("For Universal accumulator", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, nonMembers[0], nmWitnesses[0], pk, @@ -937,7 +937,7 @@ describe("For Universal accumulator", () => { ).toBe(true); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, nonMembers[1], nmWitnesses[1], pk, @@ -946,7 +946,7 @@ describe("For Universal accumulator", () => { ).toBe(true); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator1), + accumulator1.V, nonMembers[2], nmWitnesses[2], pk, @@ -961,7 +961,7 @@ describe("For Universal accumulator", () => { }); }); -describe("Witness update", () => { +describe("For VB accumulator witness update", () => { let params: Uint8Array, sk: Uint8Array, pk: Uint8Array, @@ -1017,7 +1017,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, e1, posMemWit, pk, @@ -1032,7 +1032,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, e1, uniMemWit, pk, @@ -1050,7 +1050,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWit, pk, @@ -1067,11 +1067,11 @@ describe("Witness update", () => { posMemWit, e1, e2, - positiveAccumulatorGetAccumulated(posAccumulator) + posAccumulator ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator1), + posAccumulator1, e1, posMemWit, pk, @@ -1083,11 +1083,11 @@ describe("Witness update", () => { uniMemWit, e1, e2, - universalAccumulatorGetAccumulated(uniAccumulator) + uniAccumulator.V ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator1), + uniAccumulator1.V, e1, uniMemWit, pk, @@ -1099,11 +1099,11 @@ describe("Witness update", () => { nonMemWit, nonMember, e2, - universalAccumulatorGetAccumulated(uniAccumulator) + uniAccumulator.V ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator1), + uniAccumulator1.V, nonMember, nonMemWit, pk, @@ -1118,11 +1118,11 @@ describe("Witness update", () => { posMemWit, e1, e2, - positiveAccumulatorGetAccumulated(posAccumulator2) + posAccumulator2 ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator2), + posAccumulator2, e1, posMemWit, pk, @@ -1134,11 +1134,11 @@ describe("Witness update", () => { uniMemWit, e1, e2, - universalAccumulatorGetAccumulated(uniAccumulator2) + uniAccumulator2.V ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator2), + uniAccumulator2.V, e1, uniMemWit, pk, @@ -1150,11 +1150,11 @@ describe("Witness update", () => { nonMemWit, nonMember, e2, - universalAccumulatorGetAccumulated(uniAccumulator2) + uniAccumulator2.V ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator2), + uniAccumulator2.V, nonMember, nonMemWit, pk, @@ -1192,12 +1192,12 @@ describe("Witness update", () => { [member1, member2], [member3, member4], [], - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, sk ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator1), + posAccumulator1, member1, newWits[0], pk, @@ -1206,7 +1206,7 @@ describe("Witness update", () => { ).toBe(true); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator1), + posAccumulator1, member2, newWits[1], pk, @@ -1231,7 +1231,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWitInitial, pk, @@ -1246,7 +1246,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, member, uniMemWitInitial, pk, @@ -1264,7 +1264,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWitInitial, pk, @@ -1278,13 +1278,13 @@ describe("Witness update", () => { ]; const posPublicInfo0 = publicInfoForWitnessUpdate( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, addBatch0, [], sk ); const uniPublicInfo0 = publicInfoForWitnessUpdate( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, addBatch0, [], sk @@ -1306,7 +1306,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWit, pk, @@ -1323,7 +1323,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, member, uniMemWit, pk, @@ -1340,7 +1340,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWit, pk, @@ -1355,13 +1355,13 @@ describe("Witness update", () => { const remBatch1 = addBatch0; const posPublicInfo1 = publicInfoForWitnessUpdate( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, addBatch1, remBatch1, sk ); const uniPublicInfo1 = publicInfoForWitnessUpdate( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, addBatch1, remBatch1, sk @@ -1389,7 +1389,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWit, pk, @@ -1406,7 +1406,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, member, uniMemWit, pk, @@ -1423,7 +1423,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWit, pk, @@ -1438,13 +1438,13 @@ describe("Witness update", () => { const remBatch2 = addBatch1; const posPublicInfo2 = publicInfoForWitnessUpdate( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, addBatch2, remBatch2, sk ); const uniPublicInfo2 = publicInfoForWitnessUpdate( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, addBatch2, remBatch2, sk @@ -1472,7 +1472,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWit, pk, @@ -1489,7 +1489,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, member, uniMemWit, pk, @@ -1506,7 +1506,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWit, pk, @@ -1523,7 +1523,7 @@ describe("Witness update", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWit, pk, @@ -1540,7 +1540,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, member, uniMemWit, pk, @@ -1557,7 +1557,7 @@ describe("Witness update", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(uniAccumulator), + uniAccumulator.V, nonMember, nonMemWit, pk, @@ -1567,7 +1567,7 @@ describe("Witness update", () => { }); }); -describe("Proofs ", () => { +describe("For VB accumulator proofs ", () => { let params: Uint8Array, sk: Uint8Array, pk: Uint8Array, @@ -1621,7 +1621,7 @@ describe("Proofs ", () => { ); expect( positiveAccumulatorVerifyMembership( - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, member, posMemWit, pk, @@ -1636,7 +1636,7 @@ describe("Proofs ", () => { ); expect( universalAccumulatorVerifyMembership( - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, member, uniMemWit, pk, @@ -1656,7 +1656,7 @@ describe("Proofs ", () => { let pBytes = accumulatorChallengeContributionFromMembershipProtocol( protocol, - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, pk, params, prk @@ -1668,7 +1668,7 @@ describe("Proofs ", () => { let vBytes = accumulatorChallengeContributionFromMembershipProof( proof, - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, pk, params, prk @@ -1680,7 +1680,7 @@ describe("Proofs ", () => { let result = accumulatorVerifyMembershipProof( proof, - positiveAccumulatorGetAccumulated(posAccumulator), + posAccumulator, verifierChallenge, pk, params, @@ -1700,7 +1700,7 @@ describe("Proofs ", () => { pBytes = accumulatorChallengeContributionFromMembershipProtocol( protocol, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, pk, params, prk @@ -1712,7 +1712,7 @@ describe("Proofs ", () => { vBytes = accumulatorChallengeContributionFromMembershipProof( proof, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, pk, params, prk @@ -1724,7 +1724,7 @@ describe("Proofs ", () => { result = accumulatorVerifyMembershipProof( proof, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, verifierChallenge, pk, params, @@ -1751,7 +1751,7 @@ describe("Proofs ", () => { ); expect( universalAccumulatorVerifyNonMembership( - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, nonMember, nonMemWit, pk, @@ -1771,7 +1771,7 @@ describe("Proofs ", () => { const pBytes = accumulatorChallengeContributionFromNonMembershipProtocol( protocol, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, pk, params, prk @@ -1783,7 +1783,7 @@ describe("Proofs ", () => { const vBytes = accumulatorChallengeContributionFromNonMembershipProof( proof, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, pk, params, prk @@ -1795,7 +1795,7 @@ describe("Proofs ", () => { const result = accumulatorVerifyNonMembershipProof( proof, - universalAccumulatorGetAccumulated(accumulator), + accumulator.V, verifierChallenge, pk, params, diff --git a/tests/js/kb-universal-accumulator.spec.ts b/tests/js/kb-universal-accumulator.spec.ts new file mode 100644 index 0000000..6c504a4 --- /dev/null +++ b/tests/js/kb-universal-accumulator.spec.ts @@ -0,0 +1,1023 @@ +import { + accumulatorGetElementFromBytes, + generateAccumulatorParams, + generateAccumulatorPublicKey, + generateAccumulatorSecretKey, + generateFieldElementFromNumber, + generateRandomFieldElement, + IKBUniversalAccumulator, + initializeWasm, + kbUniversalAccumulatorAdd, + kbUniversalAccumulatorComputeExtended, + kbUniversalAccumulatorInitialise, + kbUniversalAccumulatorRemove, + kbUniversalAccumulatorAddBatch, + kbUniversalAccumulatorBatchUpdates, + kbUniversalAccumulatorMembershipWitness, + kbUniversalAccumulatorMembershipWitnessesForBatch, + kbUniversalAccumulatorNonMembershipWitness, + kbUniversalAccumulatorNonMembershipWitnessesForBatch, + kbUniversalAccumulatorRemoveBatch, + kbUniversalAccumulatorVerifyMembership, + kbUniversalAccumulatorVerifyNonMembership, + kbUniversalUpdateMembershipWitnessPostAdd, + kbUniversalUpdateMembershipWitnessPostRemove, + kbUniversalUpdateNonMembershipWitnessPostAdd, + kbUniversalUpdateNonMembershipWitnessPostRemove, + kbUpdateMembershipWitnessesPostBatchUpdates, + publicInfoForKBUniversalMemWitnessUpdate, + publicInfoForKBUniversalNonMemWitnessUpdate, + updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate, + updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate, + updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates, + updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates, + publicInfoForBothKBUniversalWitnessUpdate, kbUpdateNonMembershipWitnessesPostBatchUpdates, + kbUpdateBothWitnessesPostBatchUpdates +} from "../../lib"; +import {areUint8ArraysEqual, stringToBytes} from "./util"; + +describe("For KB universal accumulator", () => { + let params: Uint8Array, + sk: Uint8Array, + pk: Uint8Array, + accumulator: IKBUniversalAccumulator; + let domain: Uint8Array[]; + + const seed = new Uint8Array([0, 2, 3, 4, 5]); + const maxSize = 20; + + beforeAll(async () => { + await initializeWasm(); + }); + + it("initialize accumulator", () => { + params = generateAccumulatorParams(); + sk = generateAccumulatorSecretKey(seed); + pk = generateAccumulatorPublicKey(sk, params); + + domain = []; + for (let i = 1; i <= 5; i++) { + domain.push(generateFieldElementFromNumber(100 + i)); + } + + accumulator = kbUniversalAccumulatorInitialise(domain, sk, params); + expect(accumulator).toBeInstanceOf(Object); + + const newElements: Uint8Array[] = []; + for (let i = 106; i <= 120; i++) { + newElements.push(generateFieldElementFromNumber(100 + i)); + } + + accumulator = kbUniversalAccumulatorComputeExtended(accumulator, newElements, sk); + expect(accumulator).toBeInstanceOf(Object); + + domain.push(...newElements); + }); + + it("add an element", () => { + let accumulator1 = kbUniversalAccumulatorAdd(accumulator, domain[0], sk); + expect(accumulator1).toBeInstanceOf(Object); + accumulator1 = kbUniversalAccumulatorAdd(accumulator1, domain[1], sk); + expect(accumulator1).toBeInstanceOf(Object); + }); + + it("membership and non-membership after single element updates", () => { + const nonMember = domain[10]; + + const e1 = domain[0]; + const e2 = domain[1]; + + const accumulator1 = kbUniversalAccumulatorAdd(accumulator, e1, sk); + const witness1 = kbUniversalAccumulatorMembershipWitness( + accumulator1, + e1, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e1, + witness1, + pk, + params + ) + ).toBe(true); + + const accumulator2 = kbUniversalAccumulatorAdd(accumulator1, e2, sk); + const witness2 = kbUniversalAccumulatorMembershipWitness( + accumulator2, + e2, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e2, + witness2, + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e1, + witness1, + pk, + params + ) + ).toBe(false); + + let accumulator3 = kbUniversalAccumulatorRemove(accumulator2, e2, sk); + // e2 was added and removed so the accumulator becomes same as before + expect(accumulator1).toEqual(accumulator3); + + const witness11 = kbUniversalAccumulatorMembershipWitness( + accumulator3, + e1, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator3, + e1, + witness11, + pk, + params + ) + ).toBe(true); + + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator3, + e1, + witness1, + pk, + params + ) + ).toBe(true); + + const e3 = domain[2]; + const e4 = domain[3]; + accumulator3 = kbUniversalAccumulatorAdd(accumulator3, e3, sk); + accumulator3 = kbUniversalAccumulatorAdd(accumulator3, e4, sk); + + const witness = kbUniversalAccumulatorNonMembershipWitness( + accumulator3, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator3, + nonMember, + witness, + pk, + params + ) + ).toBe(true); + }); + + it("membership and non-membership after batch updates", () => { + const nonMember = domain[10]; + let nmWitness = kbUniversalAccumulatorNonMembershipWitness( + accumulator, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nmWitness, + pk, + params + ) + ).toBe(true); + + const e1 = domain[0]; + const e2 = domain[1]; + const e3 = domain[2]; + const e4 = domain[3]; + const e5 = domain[4]; + const e6 = domain[5]; + + // Add a batch to `accumulator` + const addBatch = [e1, e2, e3, e4]; + const accumulator1 = kbUniversalAccumulatorAddBatch( + accumulator, + addBatch, + sk + ); + + const witness1 = kbUniversalAccumulatorMembershipWitness( + accumulator1, + e1, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e1, + witness1, + pk, + params + ) + ).toBe(true); + const witness2 = kbUniversalAccumulatorMembershipWitness( + accumulator1, + e2, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e2, + witness2, + pk, + params + ) + ).toBe(true); + const witness3 = kbUniversalAccumulatorMembershipWitness( + accumulator1, + e3, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e3, + witness3, + pk, + params + ) + ).toBe(true); + const witness4 = kbUniversalAccumulatorMembershipWitness( + accumulator1, + e4, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e4, + witness4, + pk, + params + ) + ).toBe(true); + + nmWitness = kbUniversalAccumulatorNonMembershipWitness( + accumulator1, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMember, + nmWitness, + pk, + params + ) + ).toBe(true); + + // Then remove a batch from new `accumulator1` + const removeBatch = [e1, e3]; + const accumulator2 = kbUniversalAccumulatorRemoveBatch( + accumulator1, + removeBatch, + sk + ); + + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e1, + witness1, + pk, + params + ) + ).toBe(false); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e2, + witness2, + pk, + params + ) + ).toBe(false); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e3, + witness3, + pk, + params + ) + ).toBe(false); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e4, + witness4, + pk, + params + ) + ).toBe(false); + + const witness22 = kbUniversalAccumulatorMembershipWitness( + accumulator2, + e2, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e2, + witness22, + pk, + params + ) + ).toBe(true); + const witness42 = kbUniversalAccumulatorMembershipWitness( + accumulator2, + e4, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e4, + witness42, + pk, + params + ) + ).toBe(true); + + nmWitness = kbUniversalAccumulatorNonMembershipWitness( + accumulator2, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator2, + nonMember, + nmWitness, + pk, + params + ) + ).toBe(true); + + // Then add and remove a batch from new `accumulator2` + const addNewBatch = [e5, e6]; + const removeNewBatch = [e2, e4]; + const accumulator3 = kbUniversalAccumulatorBatchUpdates( + accumulator2, + addNewBatch, + removeNewBatch, + sk + ); + + const witness5 = kbUniversalAccumulatorMembershipWitness( + accumulator3, + e5, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator3, + e5, + witness5, + pk, + params + ) + ).toBe(true); + const witness6 = kbUniversalAccumulatorMembershipWitness( + accumulator3, + e6, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator3, + e6, + witness6, + pk, + params + ) + ).toBe(true); + + nmWitness = kbUniversalAccumulatorNonMembershipWitness( + accumulator3, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator3, + nonMember, + nmWitness, + pk, + params + ) + ).toBe(true); + + // Add a batch to `accumulator`, remove a batch from `accumulator`, then add a batch and then remove + let accumulator4 = kbUniversalAccumulatorAddBatch(accumulator, addBatch, sk); + accumulator4 = kbUniversalAccumulatorRemoveBatch( + accumulator4, + removeBatch, + sk + ); + accumulator4 = kbUniversalAccumulatorAddBatch(accumulator4, addNewBatch, sk); + accumulator4 = kbUniversalAccumulatorRemoveBatch( + accumulator4, + removeNewBatch, + sk + ); + expect(accumulator4).toEqual(accumulator3); + }); + + it("membership and non-membership witnesses for multiple members", () => { + const e1 = domain[0]; + const e2 = domain[1]; + const e3 = domain[2]; + + const batch = [e1, e2, e3]; + const accumulator1 = kbUniversalAccumulatorAddBatch(accumulator, batch, sk); + + const witnesses = kbUniversalAccumulatorMembershipWitnessesForBatch( + accumulator1, + batch, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + batch[0], + witnesses[0], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + batch[1], + witnesses[1], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + batch[2], + witnesses[2], + pk, + params + ) + ).toBe(true); + + let nonMembers = [ + domain[10], + domain[11], + domain[12], + ]; + const nmWitnesses = kbUniversalAccumulatorNonMembershipWitnessesForBatch( + accumulator1, + nonMembers, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMembers[0], + nmWitnesses[0], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMembers[1], + nmWitnesses[1], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMembers[2], + nmWitnesses[2], + pk, + params + ) + ).toBe(true); + }); +}); + +describe("For KB universal accumulator witness update", () => { + let params: Uint8Array, + sk: Uint8Array, + pk: Uint8Array, + accumulator: IKBUniversalAccumulator; + + let domain: Uint8Array[]; + + beforeAll(async () => { + await initializeWasm(); + }); + + beforeEach(() => { + params = generateAccumulatorParams(); + sk = generateAccumulatorSecretKey(); + pk = generateAccumulatorPublicKey(sk, params); + + domain = []; + for (let i = 1; i <= 20; i++) { + domain.push(generateFieldElementFromNumber(100 + i)); + } + + accumulator = kbUniversalAccumulatorInitialise(domain, sk, params); + }); + + it("after single update", () => { + const nonMember = domain[10]; + + const e1 = domain[0]; + accumulator = kbUniversalAccumulatorAdd(accumulator, e1, sk); + + let uniMemWit = kbUniversalAccumulatorMembershipWitness( + accumulator, + e1, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + e1, + uniMemWit, + pk, + params + ) + ).toBe(true); + + let nonMemWit = kbUniversalAccumulatorNonMembershipWitness( + accumulator, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + + const e2 = domain[1]; + + const accumulator1 = kbUniversalAccumulatorAdd(accumulator, e2, sk); + + uniMemWit = kbUniversalUpdateMembershipWitnessPostAdd( + uniMemWit, + e1, + e2, + accumulator + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + e1, + uniMemWit, + pk, + params + ) + ).toBe(true); + + nonMemWit = kbUniversalUpdateNonMembershipWitnessPostAdd( + nonMemWit, + nonMember, + e2, + accumulator1 + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + + const accumulator2 = kbUniversalAccumulatorRemove(accumulator1, e2, sk); + + uniMemWit = kbUniversalUpdateMembershipWitnessPostRemove( + uniMemWit, + e1, + e2, + accumulator2 + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator2, + e1, + uniMemWit, + pk, + params + ) + ).toBe(true); + + nonMemWit = kbUniversalUpdateNonMembershipWitnessPostRemove( + nonMemWit, + nonMember, + e2, + accumulator1 + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator2, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + }); + + it("by manager after batch updates", () => { + const nonMember1 = domain[10]; + const nonMember2 = domain[11]; + const member1 = domain[0]; + const member2 = domain[1]; + + accumulator = kbUniversalAccumulatorAddBatch( + accumulator, + [member1, member2], + sk + ); + const memWits = kbUniversalAccumulatorMembershipWitnessesForBatch( + accumulator, + [member1, member2], + sk + ); + const nonMemWits = kbUniversalAccumulatorNonMembershipWitnessesForBatch( + accumulator, + [nonMember1, nonMember2], + sk + ); + + const member3 = generateRandomFieldElement(); + const member4 = generateRandomFieldElement(); + + const accumulator1 = kbUniversalAccumulatorAddBatch( + accumulator, + [member3, member4], + sk + ); + + const newMemWits = kbUpdateMembershipWitnessesPostBatchUpdates( + memWits, + [member1, member2], + [member3, member4], + [], + accumulator, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + member1, + newMemWits[0], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator1, + member2, + newMemWits[1], + pk, + params + ) + ).toBe(true); + + const newNonMemWits = kbUpdateNonMembershipWitnessesPostBatchUpdates( + nonMemWits, + [nonMember1, nonMember2], + [member3, member4], + [], + accumulator, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMember1, + newNonMemWits[0], + pk, + params + ) + ).toBe(true); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator1, + nonMember2, + newNonMemWits[1], + pk, + params + ) + ).toBe(true); + + const [newMemWits_, newNonMemWits_] = kbUpdateBothWitnessesPostBatchUpdates(memWits, [member1, member2], nonMemWits, [nonMember1, nonMember2], [member3, member4], [], accumulator, sk); + expect(newMemWits.length).toEqual(newMemWits_.length); + expect(newNonMemWits.length).toEqual(newNonMemWits_.length); + for (let i = 0; i < newMemWits.length; i++) { + expect(areUint8ArraysEqual(newMemWits[i], newMemWits_[i])).toEqual(true); + } + for (let i = 0; i < newNonMemWits.length; i++) { + expect(areUint8ArraysEqual(newNonMemWits[i], newNonMemWits_[i])).toEqual(true); + } + + accumulator = accumulator1; + }); + + it("after batch updates", () => { + const member = domain[0]; + const nonMember = domain[10]; + + accumulator = kbUniversalAccumulatorAdd(accumulator, member, sk); + + const uniMemWitInitial = kbUniversalAccumulatorMembershipWitness( + accumulator, + member, + sk + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + member, + uniMemWitInitial, + pk, + params + ) + ).toBe(true); + + const nonMemWitInitial = kbUniversalAccumulatorNonMembershipWitness( + accumulator, + nonMember, + sk + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWitInitial, + pk, + params + ) + ).toBe(true); + + const addBatch0 = [ + domain[1], + domain[2], + ]; + + const memPublicInfo0 = publicInfoForKBUniversalMemWitnessUpdate( + accumulator, + addBatch0, + [], + sk + ); + const nonMemPublicInfo0 = publicInfoForKBUniversalNonMemWitnessUpdate( + accumulator, + addBatch0, + [], + sk + ); + + accumulator = kbUniversalAccumulatorAddBatch( + accumulator, + addBatch0, + sk + ); + + let uniMemWit = updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate( + uniMemWitInitial, + member, + addBatch0, + [], + memPublicInfo0 + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + member, + uniMemWit, + pk, + params + ) + ).toBe(true); + + let nonMemWit = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate( + nonMemWitInitial, + nonMember, + addBatch0, + [], + nonMemPublicInfo0 + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + + // let [memPublicInfo_, nonMemPublicInfo_] = publicInfoForBothKBUniversalWitnessUpdate( + // accumulator, + // addBatch0, + // [], + // sk + // ); + // expect(areUint8ArraysEqual(memPublicInfo0, memPublicInfo_)).toEqual(true); + // expect(areUint8ArraysEqual(nonMemPublicInfo0, nonMemPublicInfo_)).toEqual(true); + + const addBatch1 = [ + domain[3], + domain[4], + ]; + const remBatch1 = addBatch0; + + const memPublicInfo1 = publicInfoForKBUniversalMemWitnessUpdate( + accumulator, + addBatch1, + remBatch1, + sk + ); + const nonMemPublicInfo1 = publicInfoForKBUniversalNonMemWitnessUpdate( + accumulator, + addBatch1, + remBatch1, + sk + ); + + accumulator = kbUniversalAccumulatorBatchUpdates( + accumulator, + addBatch1, + remBatch1, + sk + ); + + uniMemWit = updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate( + uniMemWit, + member, + addBatch1, + remBatch1, + memPublicInfo1 + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + member, + uniMemWit, + pk, + params + ) + ).toBe(true); + + nonMemWit = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate( + nonMemWit, + nonMember, + addBatch1, + remBatch1, + nonMemPublicInfo1 + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + + // [memPublicInfo_, nonMemPublicInfo_] = publicInfoForBothKBUniversalWitnessUpdate( + // accumulator, + // addBatch1, + // remBatch1, + // sk + // ); + // expect(areUint8ArraysEqual(memPublicInfo1, memPublicInfo_)).toEqual(true); + // expect(areUint8ArraysEqual(nonMemPublicInfo1, nonMemPublicInfo_)).toEqual(true); + + const addBatch2 = [ + domain[5], + domain[6], + ]; + const remBatch2 = addBatch1; + + const memPublicInfo2 = publicInfoForKBUniversalMemWitnessUpdate( + accumulator, + addBatch2, + remBatch2, + sk + ); + + const nonMemPublicInfo2 = publicInfoForKBUniversalNonMemWitnessUpdate( + accumulator, + addBatch2, + remBatch2, + sk + ); + + accumulator = kbUniversalAccumulatorBatchUpdates( + accumulator, + addBatch2, + remBatch2, + sk + ); + + uniMemWit = updateKBUniversalMembershipWitnessUsingPublicInfoAfterBatchUpdate( + uniMemWit, + member, + addBatch2, + remBatch2, + memPublicInfo2 + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + member, + uniMemWit, + pk, + params + ) + ).toBe(true); + + nonMemWit = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterBatchUpdate( + nonMemWit, + nonMember, + addBatch2, + remBatch2, + nonMemPublicInfo2 + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + + + uniMemWit = updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates( + uniMemWitInitial, + member, + [addBatch0, addBatch1, addBatch2], + [[], remBatch1, remBatch2], + [memPublicInfo0, memPublicInfo1, memPublicInfo2] + ); + expect( + kbUniversalAccumulatorVerifyMembership( + accumulator, + member, + uniMemWit, + pk, + params + ) + ).toBe(true); + + nonMemWit = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates( + nonMemWitInitial, + nonMember, + [addBatch0, addBatch1, addBatch2], + [[], remBatch1, remBatch2], + [nonMemPublicInfo0, nonMemPublicInfo1, nonMemPublicInfo2] + ); + expect( + kbUniversalAccumulatorVerifyNonMembership( + accumulator, + nonMember, + nonMemWit, + pk, + params + ) + ).toBe(true); + }); +}); \ No newline at end of file diff --git a/tests/js/proofSystem.spec.ts b/tests/js/proofSystem.spec.ts index d121259..11faa97 100644 --- a/tests/js/proofSystem.spec.ts +++ b/tests/js/proofSystem.spec.ts @@ -39,7 +39,6 @@ import { universalAccumulatorAdd, universalAccumulatorComputeD, universalAccumulatorComputeInitialFv, - universalAccumulatorGetAccumulated, universalAccumulatorInitialiseGivenFv, universalAccumulatorMembershipWitness, universalAccumulatorNonMembershipWitness, @@ -99,7 +98,27 @@ import { generatePoKBBSPlusSignatureProverStatementFromParamRefs, generatePoKBBSSignatureProverStatementFromParamRefs, generateMembershipProvingKey, - getAllDelegatedSubproofsFromProof, verifyBDDT16DelegatedProof, verifyVBAccumMembershipDelegatedProof + getAllDelegatedSubproofsFromProof, + verifyBDDT16DelegatedProof, + verifyVBAccumMembershipDelegatedProof, + kbUniversalAccumulatorInitialise, + kbUniversalAccumulatorAdd, + kbUniversalAccumulatorMembershipWitness, + kbUniversalAccumulatorNonMembershipWitness, + generateKBUniversalAccumulatorKVMembershipStatement, + generateKBUniversalAccumulatorKVNonMembershipStatement, + generateKBUniversalAccumulatorMembershipProverStatement, + generateKBUniversalAccumulatorNonMembershipProverStatement, + generateKBUniversalAccumulatorMembershipWitness, + generateKBUniversalAccumulatorNonMembershipWitness, + generateKBUniversalAccumulatorMembershipVerifierStatement, + generateKBUniversalAccumulatorNonMembershipVerifierStatement, + generateKBUniversalAccumulatorKVFullVerifierMembershipStatement, + generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement, + generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs, + generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs, + verifyKBUniAccumMembershipDelegatedProof, + verifyKBUniAccumNonMembershipDelegatedProof } from "../../lib"; import { BbsSigParams, PSSigParams } from "../../lib/types"; import {checkResult, getRevealedUnrevealed, stringToBytes} from "./util"; @@ -573,6 +592,8 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem messages1[5] = member; messages2[5] = member; + const nonMember = messages1[3]; + const sig1 = sign(messages1, sk1, sigParams1, false); const sig2 = sign(messages2, sk2, sigParams2, false); @@ -605,16 +626,24 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem ]; let uniAccumulator = getUniversalAccum(initialElements, sk, params, 100); - const nonMemPrk = generateNonMembershipProvingKey(); + const domain: Uint8Array[] = []; + for (let i = 1; i <= 10; i++) { + domain.push(generateFieldElementFromNumber(100 + i)); + } + // Non-member should be part of the domain + domain.push(nonMember); + + let kbUniAccumulator = kbUniversalAccumulatorInitialise(domain, sk, params); + + const nonMemPrk = generateNonMembershipProvingKey(); const memPrk = accumulatorDeriveMembershipProvingKeyFromNonMembershipKey( nonMemPrk ); posAccumulator = positiveAccumulatorAdd(posAccumulator, member, sk); uniAccumulator = universalAccumulatorAdd(uniAccumulator, member, sk); - - const nonMember = messages1[3]; + kbUniAccumulator = kbUniversalAccumulatorAdd(kbUniAccumulator, member, sk); const posWitness = positiveAccumulatorMembershipWitness( posAccumulator, @@ -626,6 +655,11 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem member, sk ); + const kbUniWitness = kbUniversalAccumulatorMembershipWitness( + kbUniAccumulator, + member, + sk + ); const d = universalAccumulatorComputeD(nonMember, [member]); const nmWitness = universalAccumulatorNonMembershipWitness( @@ -636,8 +670,14 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem params ); - const posAccumulated = positiveAccumulatorGetAccumulated(posAccumulator); - const uniAccumulated = universalAccumulatorGetAccumulated(uniAccumulator); + const kbUniNmWitness = kbUniversalAccumulatorNonMembershipWitness( + kbUniAccumulator, + nonMember, + sk + ); + + const posAccumulated = posAccumulator; + const uniAccumulated = uniAccumulator.V; const statement1 = !isPs ? buildProverStatement( sigParams1, @@ -671,9 +711,15 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem memPrk, uniAccumulated ); - let statement5; + const statement5 = isKvac ? generateKBUniversalAccumulatorKVMembershipStatement(kbUniAccumulator.mem) : generateKBUniversalAccumulatorMembershipProverStatement( + kbUniAccumulator.mem + ); + const statement6 = isKvac ? generateKBUniversalAccumulatorKVNonMembershipStatement(kbUniAccumulator.non_mem) : generateKBUniversalAccumulatorNonMembershipProverStatement( + kbUniAccumulator.non_mem + ); + let statement7; if (!isKvac) { - statement5 = generateAccumulatorNonMembershipStatement( + statement7 = generateAccumulatorNonMembershipStatement( params, pk, nonMemPrk, @@ -688,13 +734,19 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem set1.add([1, 5]); set1.add([2, 0]); set1.add([3, 0]); + set1.add([4, 0]); metaStatements.push(generateWitnessEqualityMetaStatement(set1)); + const set2 = new Set<[number, number]>(); + set2.add([0, 3]); + set2.add([5, 0]); + metaStatements.push(generateWitnessEqualityMetaStatement(set2)); + if (!isKvac) { - const set2 = new Set<[number, number]>(); - set2.add([0, 3]); - set2.add([4, 0]); - metaStatements.push(generateWitnessEqualityMetaStatement(set2)); + const set3 = new Set<[number, number]>(); + set3.add([0, 3]); + set3.add([6, 0]); + metaStatements.push(generateWitnessEqualityMetaStatement(set3)); } const proverStatements: Uint8Array[] = []; @@ -702,8 +754,10 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem proverStatements.push(statement2); proverStatements.push(statement3); proverStatements.push(statement4); + proverStatements.push(statement5); + proverStatements.push(statement6); if (!isKvac) { - proverStatements.push(statement5); + proverStatements.push(statement7); } const proverProofSpec = generateProofSpecG1(proverStatements, metaStatements, []); @@ -713,9 +767,11 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem const witness2 = buildWitness(sig2, unrevealedMsgs2, false); const witness3 = generateAccumulatorMembershipWitness(member, posWitness); const witness4 = generateAccumulatorMembershipWitness(member, uniWitness); - let witness5; + const witness5 = generateKBUniversalAccumulatorMembershipWitness(member, kbUniWitness); + const witness6 = generateKBUniversalAccumulatorNonMembershipWitness(nonMember, kbUniNmWitness); + let witness7; if (!isKvac) { - witness5 = generateAccumulatorNonMembershipWitness( + witness7 = generateAccumulatorNonMembershipWitness( nonMember, nmWitness ); @@ -726,8 +782,10 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem witnesses.push(witness2); witnesses.push(witness3); witnesses.push(witness4); + witnesses.push(witness5); + witnesses.push(witness6); if (!isKvac) { - witnesses.push(witness5); + witnesses.push(witness7); } const proof = generateCompositeProofG1(proverProofSpec, witnesses); @@ -755,8 +813,16 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem )); verifierStatements.push(statement3); verifierStatements.push(statement4); + verifierStatements.push(isKvac ? statement5 : generateKBUniversalAccumulatorMembershipVerifierStatement( + params, + pk, + kbUniAccumulator.mem)); + verifierStatements.push(isKvac ? statement6 : generateKBUniversalAccumulatorNonMembershipVerifierStatement( + params, + pk, + kbUniAccumulator.non_mem)); if (!isKvac) { - verifierStatements.push(statement5); + verifierStatements.push(statement7); } const verifierProofSpec = generateProofSpecG1(verifierStatements, metaStatements, []); expect(isProofSpecG1Valid(verifierProofSpec)).toEqual(true); @@ -783,6 +849,12 @@ describe("Proving knowledge of signatures and accumulator membership and non-mem statements.push( generateAccumulatorKVFullVerifierMembershipStatement(sk, uniAccumulated) ); + statements.push( + generateKBUniversalAccumulatorKVFullVerifierMembershipStatement(sk, kbUniAccumulator.mem) + ); + statements.push( + generateKBUniversalAccumulatorKVFullVerifierNonMembershipStatement(sk, kbUniAccumulator.non_mem) + ); const proofSpec = generateProofSpecG1(statements, metaStatements, []); const res = verifyCompositeProofG1(proof, proofSpec); checkResult(res); @@ -1205,11 +1277,36 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { 100 ); + const domain1: Uint8Array[] = []; + for (let i = 1; i <= 10; i++) { + domain1.push(generateFieldElementFromNumber(100 + i)); + } + domain1.push(messages3[nonMemberIndex]); + let kbUniAccumulator1 = kbUniversalAccumulatorInitialise(domain1, accumSk1, accumParams1); + + const domain2: Uint8Array[] = []; + for (let i = 1; i <= 10; i++) { + domain2.push(generateFieldElementFromNumber(200 + i)); + } + domain2.push(messages4[nonMemberIndex]); + let kbUniAccumulator2 = kbUniversalAccumulatorInitialise(domain2, accumSk2, accumParams2); + const nonMemPrk = generateNonMembershipProvingKey(); const memPrk = accumulatorDeriveMembershipProvingKeyFromNonMembershipKey( nonMemPrk ); + kbUniAccumulator1 = kbUniversalAccumulatorAdd( + kbUniAccumulator1, + messages1[memberIndex], + accumSk1 + ); + kbUniAccumulator2 = kbUniversalAccumulatorAdd( + kbUniAccumulator2, + messages2[memberIndex], + accumSk2 + ); + const [revealedMsgs1, unrevealedMsgs1] = getRevealedUnrevealed( messages1, new Set() @@ -1281,10 +1378,31 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { accumParams2 ); - const posAccumulated1 = positiveAccumulatorGetAccumulated(posAccumulator1); - const posAccumulated2 = positiveAccumulatorGetAccumulated(posAccumulator2); - const uniAccumulated1 = universalAccumulatorGetAccumulated(uniAccumulator1); - const uniAccumulated2 = universalAccumulatorGetAccumulated(uniAccumulator2); + const kbUniMemWitness1 = kbUniversalAccumulatorMembershipWitness( + kbUniAccumulator1, + messages1[memberIndex], + accumSk1 + ); + const kbUniMemWitness2 = kbUniversalAccumulatorMembershipWitness( + kbUniAccumulator2, + messages2[memberIndex], + accumSk2 + ); + const kbUniNonMemWitness1 = kbUniversalAccumulatorNonMembershipWitness( + kbUniAccumulator1, + messages3[nonMemberIndex], + accumSk1 + ); + const kbUniNonMemWitness2 = kbUniversalAccumulatorNonMembershipWitness( + kbUniAccumulator2, + messages4[nonMemberIndex], + accumSk2 + ); + + const posAccumulated1 = posAccumulator1; + const posAccumulated2 = posAccumulator2; + const uniAccumulated1 = uniAccumulator1.V; + const uniAccumulated2 = uniAccumulator2.V; const allSetupParams: Uint8Array[] = []; allSetupParams.push( @@ -1363,6 +1481,18 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { 9 - accumStmtOffset, uniAccumulated2 ); + const statement13 = generateKBUniversalAccumulatorMembershipProverStatement( + kbUniAccumulator1.mem + ); + const statement14 = generateKBUniversalAccumulatorMembershipProverStatement( + kbUniAccumulator2.mem + ); + const statement15 = generateKBUniversalAccumulatorNonMembershipProverStatement( + kbUniAccumulator1.non_mem + ); + const statement16 = generateKBUniversalAccumulatorNonMembershipProverStatement( + kbUniAccumulator2.non_mem + ); const proverStatements: Uint8Array[] = []; proverStatements.push(statement1); @@ -1377,6 +1507,10 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { proverStatements.push(statement10); proverStatements.push(statement11); proverStatements.push(statement12); + proverStatements.push(statement13); + proverStatements.push(statement14); + proverStatements.push(statement15); + proverStatements.push(statement16); const proverProofSpec = generateProofSpecG1(proverStatements, [], allSetupParams); expect(isProofSpecG1Valid(proverProofSpec)).toEqual(true); @@ -1433,6 +1567,22 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { messages4[nonMemberIndex], uniWitness4 ); + const witness13 = generateKBUniversalAccumulatorMembershipWitness( + messages1[memberIndex], + kbUniMemWitness1 + ); + const witness14 = generateKBUniversalAccumulatorMembershipWitness( + messages2[memberIndex], + kbUniMemWitness2 + ); + const witness15 = generateKBUniversalAccumulatorNonMembershipWitness( + messages3[nonMemberIndex], + kbUniNonMemWitness1 + ); + const witness16 = generateKBUniversalAccumulatorNonMembershipWitness( + messages4[nonMemberIndex], + kbUniNonMemWitness2 + ); const witnesses: Uint8Array[] = []; witnesses.push(witness1); @@ -1447,9 +1597,22 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { witnesses.push(witness10); witnesses.push(witness11); witnesses.push(witness12); + witnesses.push(witness13); + witnesses.push(witness14); + witnesses.push(witness15); + witnesses.push(witness16); const proof = generateCompositeProofG1(proverProofSpec, witnesses); + const statement17 = generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs(4 - accumStmtOffset, + 5 - accumStmtOffset, kbUniAccumulator1.mem); + const statement18 = generateKBUniversalAccumulatorMembershipVerifierStatementFromParamRefs(6 - accumStmtOffset, + 7 - accumStmtOffset, kbUniAccumulator2.mem); + const statement19 = generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs(4 - accumStmtOffset, + 5 - accumStmtOffset, kbUniAccumulator1.non_mem); + const statement20 = generateKBUniversalAccumulatorNonMembershipVerifierStatementFromParamRefs(6 - accumStmtOffset, + 7 - accumStmtOffset, kbUniAccumulator2.non_mem); + const verifierStatements: Uint8Array[] = []; verifierStatements.push(isKvac ? sigVerStmtFunc( 0, @@ -1499,6 +1662,10 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { verifierStatements.push(statement10); verifierStatements.push(statement11); verifierStatements.push(statement12); + verifierStatements.push(statement17); + verifierStatements.push(statement18); + verifierStatements.push(statement19); + verifierStatements.push(statement20); const verifierProofSpec = generateProofSpecG1(verifierStatements, [], allSetupParams); expect(isProofSpecG1Valid(verifierProofSpec)).toEqual(true); @@ -1520,6 +1687,10 @@ describe("Reusing setup params of BBS, BBS+ and accumulator", () => { statements.push(statement10); statements.push(statement11); statements.push(statement12); + statements.push(statement17); + statements.push(statement18); + statements.push(statement19); + statements.push(statement20); const proofSpec = generateProofSpecG1(statements, [], allSetupParams); const res = verifyCompositeProofG1(proof, proofSpec); @@ -1715,6 +1886,8 @@ describe("Delegated proofs", () => { messages1[5] = member; messages2[5] = member; + const nonMember = messages1[3]; + const sig1 = bbsSign(messages1, bbsSk, bbsParams, false); const sig2 = bddt16MacGenerate(messages2, bddt16Sk, bddt16Params, false); @@ -1730,7 +1903,26 @@ describe("Delegated proofs", () => { member, sk ); - const accumulated = positiveAccumulatorGetAccumulated(accumulator); + const accumulated = accumulator; + + const domain: Uint8Array[] = []; + for (let i = 1; i <= 10; i++) { + domain.push(generateFieldElementFromNumber(100 + i)); + } + domain.push(nonMember); + let kbUniAccumulator = kbUniversalAccumulatorInitialise(domain, sk, params); + kbUniAccumulator = kbUniversalAccumulatorAdd(kbUniAccumulator, member, sk); + const kbUniMemWitness = kbUniversalAccumulatorMembershipWitness( + kbUniAccumulator, + member, + sk + ); + + const kbUniNonMemWitness = kbUniversalAccumulatorNonMembershipWitness( + kbUniAccumulator, + nonMember, + sk + ); const [revealedMsgs1, unrevealedMsgs1] = getRevealedUnrevealed( messages1, @@ -1744,6 +1936,7 @@ describe("Delegated proofs", () => { const proverStatements: Uint8Array[] = []; proverStatements.push(generatePoKBBSSignatureProverStatement(bbsParams, revealedMsgs1, false)); proverStatements.push(generatePoKBDDT16MacStatement(bddt16Params, revealedMsgs2, false)); + proverStatements.push(generateAccumulatorMembershipStatement( params, pk, @@ -1752,6 +1945,11 @@ describe("Delegated proofs", () => { )); proverStatements.push(generateAccumulatorKVMembershipStatement(accumulated)); + proverStatements.push(generateKBUniversalAccumulatorMembershipProverStatement(kbUniAccumulator.mem)); + proverStatements.push(generateKBUniversalAccumulatorNonMembershipProverStatement(kbUniAccumulator.non_mem)); + proverStatements.push(generateKBUniversalAccumulatorKVMembershipStatement(kbUniAccumulator.mem)); + proverStatements.push(generateKBUniversalAccumulatorKVNonMembershipStatement(kbUniAccumulator.non_mem)); + const metaStatements: Uint8Array[] = []; const set1 = new Set<[number, number]>(); @@ -1759,8 +1957,16 @@ describe("Delegated proofs", () => { set1.add([1, 5]); set1.add([2, 0]); set1.add([3, 0]); + set1.add([4, 0]); + set1.add([6, 0]); metaStatements.push(generateWitnessEqualityMetaStatement(set1)); + const set2 = new Set<[number, number]>(); + set2.add([0, 3]); + set2.add([5, 0]); + set2.add([7, 0]); + metaStatements.push(generateWitnessEqualityMetaStatement(set2)); + const proverProofSpec = generateProofSpecG1(proverStatements, metaStatements, []); expect(isProofSpecG1Valid(proverProofSpec)).toEqual(true); @@ -1768,12 +1974,20 @@ describe("Delegated proofs", () => { const witness2 = generatePoKBDDT16MacWitness(sig2, unrevealedMsgs2, false); const witness3 = generateAccumulatorMembershipWitness(member, witness); const witness4 = generateAccumulatorMembershipWitness(member, witness); + const witness5 = generateKBUniversalAccumulatorMembershipWitness(member, kbUniMemWitness); + const witness6 = generateKBUniversalAccumulatorNonMembershipWitness(nonMember, kbUniNonMemWitness); + const witness7 = generateKBUniversalAccumulatorMembershipWitness(member, kbUniMemWitness); + const witness8 = generateKBUniversalAccumulatorNonMembershipWitness(nonMember, kbUniNonMemWitness); const witnesses: Uint8Array[] = []; witnesses.push(witness1); witnesses.push(witness2); witnesses.push(witness3); witnesses.push(witness4); + witnesses.push(witness5); + witnesses.push(witness6); + witnesses.push(witness7); + witnesses.push(witness8); const proof = generateCompositeProofG1(proverProofSpec, witnesses); @@ -1792,25 +2006,41 @@ describe("Delegated proofs", () => { accumulated )); verifierStatements.push(generateAccumulatorKVMembershipStatement(accumulated)); + verifierStatements.push(generateKBUniversalAccumulatorMembershipVerifierStatement(params, pk, kbUniAccumulator.mem)); + verifierStatements.push(generateKBUniversalAccumulatorNonMembershipVerifierStatement(params, pk, kbUniAccumulator.non_mem)); + verifierStatements.push(generateKBUniversalAccumulatorKVMembershipStatement(kbUniAccumulator.mem)); + verifierStatements.push(generateKBUniversalAccumulatorKVNonMembershipStatement(kbUniAccumulator.non_mem)); const verifierProofSpec = generateProofSpecG1(verifierStatements, metaStatements, []); expect(isProofSpecG1Valid(verifierProofSpec)).toEqual(true); checkResult(verifyCompositeProofG1(proof, verifierProofSpec)); const dps: Map = getAllDelegatedSubproofsFromProof(proof); - expect(dps.size).toEqual(2); + expect(dps.size).toEqual(4); const dp0 = dps.get(1); const dp1 = dps.get(3); + const dp2 = dps.get(6); + const dp3 = dps.get(7); expect(Array.isArray(dp0) && dp0.length).toEqual(2); expect(Array.isArray(dp1) && dp1.length).toEqual(2); + expect(Array.isArray(dp2) && dp2.length).toEqual(2); + expect(Array.isArray(dp3) && dp3.length).toEqual(2); // @ts-ignore expect(dp0[0]).toEqual(0); // @ts-ignore expect(dp1[0]).toEqual(1); + // @ts-ignore + expect(dp2[0]).toEqual(2); + // @ts-ignore + expect(dp3[0]).toEqual(3); // @ts-ignore checkResult(verifyBDDT16DelegatedProof(dp0[1], bddt16Sk)); // @ts-ignore checkResult(verifyVBAccumMembershipDelegatedProof(dp1[1], sk)); + // @ts-ignore + checkResult(verifyKBUniAccumMembershipDelegatedProof(dp2[1], sk)); + // @ts-ignore + checkResult(verifyKBUniAccumNonMembershipDelegatedProof(dp3[1], sk)); }) }) \ No newline at end of file