Skip to content

Commit

Permalink
New function for domain extension
Browse files Browse the repository at this point in the history
Signed-off-by: lovesh <lovesh.bond@gmail.com>
  • Loading branch information
lovesh committed Mar 29, 2024
1 parent 264035e commit 923d3af
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ dlmalloc = { version = "0.2.6", 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.21.0", default-features = false }
vb_accumulator = { version = "0.22.0", default-features = false }
schnorr_pok = { version = "0.17.0", default-features = false }
proof_system = { version = "0.26.0", default-features = false }
proof_system = { version = "0.27.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 }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/crypto-wasm",
"version": "0.27.0",
"version": "0.28.0",
"author": "Dock.io",
"license": "Apache-2.0",
"private": false,
Expand Down
126 changes: 126 additions & 0 deletions src/accumulator/kb_accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use ark_serialize::CanonicalDeserialize;
use js_sys::Uint8Array;
use vb_accumulator::kb_universal_accumulator::{
witness::{
Expand Down Expand Up @@ -540,3 +541,128 @@ pub fn update_kb_universal_non_membership_witness_using_public_info_after_multip
let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?;
crate::update_witness_multiple_batches!(witness, non_member, additions, removals, public_info)
}

#[wasm_bindgen(js_name = kbUpdateNonMembershipWitnessesPostDomainExtension)]
pub fn kb_universal_update_non_membership_witnesses_post_domain_extension(
witnesses: js_sys::Array,
non_members: js_sys::Array,
new_elements: js_sys::Array,
old_accum: JsValue,
secret_key: JsValue,
) -> Result<js_sys::Array, JsValue> {
set_panic_hook();
let non_members = js_array_to_fr_vec(&non_members)?;
let new_elements = js_array_to_fr_vec(&new_elements)?;
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::<KBUniNonMembershipWit>(
w.unwrap(),
)?);
}
let new_wits = accum.update_non_mem_wit_using_secret_key_on_domain_extension(
&new_elements,
&non_members,
&wits,
&sk,
)
.map_err(|e| {
JsValue::from(&format!(
"Evaluating update_non_mem_wit_using_secret_key_on_domain_extension 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 = publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension)]
pub fn public_info_for_kb_universal_non_mem_witness_update_on_domain_extension(
old_accum: JsValue,
new_elements: js_sys::Array,
secret_key: JsValue,
) -> Result<Uint8Array, JsValue> {
set_panic_hook();
let accum: KBUniversalAccum = serde_wasm_bindgen::from_value(old_accum)?;
let new_elements = js_array_to_fr_vec(&new_elements)?;
let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?;
let omega =
accum.generate_omega_for_non_membership_witnesses_on_domain_extension(&new_elements, &sk);
Ok(obj_to_uint8array!(&omega, false, "Omega"))
}

#[wasm_bindgen(js_name = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterDomainExtension)]
pub fn update_kb_universal_non_membership_witness_using_public_info_after_domain_extension(
witness: JsValue,
non_member: Uint8Array,
new_elements: js_sys::Array,
public_info: Uint8Array,
) -> Result<JsValue, JsValue> {
set_panic_hook();
let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?;
let non_member = fr_from_uint8_array(non_member, true)?;
let new_elements = js_array_to_fr_vec(&new_elements)?;
let public_info: Omega =
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_domain_extension(&new_elements, &public_info, &non_member)
.map_err(|e| {
JsValue::from(&format!(
"Evaluating update_using_public_info_after_domain_extension returned error: {:?}",
e
))
})?;
serde_wasm_bindgen::to_value(&new_witness).map_err(JsValue::from)
}

#[wasm_bindgen(js_name = updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleDomainExtensions)]
pub fn update_kb_universal_non_membership_witness_using_public_info_after_multiple_domain_extensions(
witness: JsValue,
non_member: Uint8Array,
new_elements: js_sys::Array,
public_info: js_sys::Array,
) -> Result<JsValue, JsValue> {
set_panic_hook();
let witness: KBUniNonMembershipWit = serde_wasm_bindgen::from_value(witness)?;
let non_member = fr_from_uint8_array(non_member, true)?;
if new_elements.length() == public_info.length() {
let size = new_elements.length();
let mut updates_and_public_info = Vec::with_capacity(size as usize);
for i in 0..size {
let new = js_array_to_fr_vec(&js_sys::Array::from(&new_elements.get(i)))?;
let bytes: Vec<u8> = serde_wasm_bindgen::from_value(public_info.get(i))?;
let p: Omega =
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((new, p));
}
let new_witness = witness.update_using_public_info_after_multiple_domain_extensions(updates_and_public_info.iter().map(|(a, p)| (a.as_slice(), p)).collect::<Vec<_>>(), &non_member)
.map_err(|e| {
JsValue::from(&format!(
"Evaluating update_using_public_info_after_multiple_domain_extensions 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 elements and public info: {} {}",
new_elements.length(),
public_info.length()
)))
}
}
20 changes: 20 additions & 0 deletions src/js/kb_accumulator_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,24 @@ module.exports.updateKBUniversalMembershipWitnessUsingPublicInfoAfterMultipleBat
module.exports.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates = (witness, nonMember, additions, removals, publicInfo) => {
requireWasmInitialized();
return wasm.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleBatchUpdates(witness, nonMember, additions, removals, publicInfo)
};

module.exports.kbUpdateNonMembershipWitnessesPostDomainExtension = (witnesses, nonMembers, newElements, oldAccumulator, secretKey) => {
requireWasmInitialized();
return wasm.kbUpdateNonMembershipWitnessesPostDomainExtension(witnesses, nonMembers, newElements, oldAccumulator, secretKey)
};

module.exports.publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension = (oldAccumulator, newElements, secretKey) => {
requireWasmInitialized();
return wasm.publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension(oldAccumulator, newElements, secretKey)
};

module.exports.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterDomainExtension = (witness, nonMember, newElements, publicInfo) => {
requireWasmInitialized();
return wasm.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterDomainExtension(witness, nonMember, newElements, publicInfo)
};

module.exports.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleDomainExtensions = (witness, nonMember, newElements, publicInfo) => {
requireWasmInitialized();
return wasm.updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleDomainExtensions(witness, nonMember, newElements, publicInfo)
};
30 changes: 29 additions & 1 deletion src/js/type_declarations/kb_accumulator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,32 @@ export function updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipl
additions: Uint8Array[][],
removals: Uint8Array[][],
publicInfo: Uint8Array[],
): Uint8Array;
): Uint8Array;

export function kbUpdateNonMembershipWitnessesPostDomainExtension(
witnesses: Uint8Array[],
nonMembers: Uint8Array[],
newElements: Uint8Array[],
oldAccumulator: IKBUniversalAccumulator,
secretKey: Uint8Array,
): Uint8Array[];

export function publicInfoForKBUniversalNonMemWitnessUpdateOnDomainExtension(
oldAccumulator: IKBUniversalAccumulator,
newElements: Uint8Array[],
secretKey: Uint8Array,
): Uint8Array;

export function updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterDomainExtension(
witness: Uint8Array,
nonMember: Uint8Array,
newElements: Uint8Array[],
publicInfo: Uint8Array,
): Uint8Array;

export function updateKBUniversalNonMembershipWitnessUsingPublicInfoAfterMultipleDomainExtensions(
witness: Uint8Array,
member: Uint8Array,
newElements: Uint8Array[][],
publicInfo: Uint8Array[],
): Uint8Array;

0 comments on commit 923d3af

Please sign in to comment.