diff --git a/package.json b/package.json index f0ed1c5..3237c08 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@docknetwork/crypto-wasm", - "version": "0.31.0", + "version": "0.32.0", "author": "Dock.io", "license": "Apache-2.0", "private": false, diff --git a/src/accumulator/common.rs b/src/accumulator/common.rs index bd3b44f..6e26074 100644 --- a/src/accumulator/common.rs +++ b/src/accumulator/common.rs @@ -1,7 +1,4 @@ -use crate::{ - utils::{fr_to_jsvalue, random_bytes, set_panic_hook}, - Fr, -}; +use crate::{utils::{fr_to_jsvalue, random_bytes, set_panic_hook}, Fr, G1Affine}; use ark_bls12_381::Bls12_381; use ark_serialize::CanonicalDeserialize; use blake2::Blake2b512; @@ -15,6 +12,8 @@ pub(crate) type AccumSk = SecretKey; pub type AccumPk = PublicKey; pub type AccumSetupParams = SetupParams; pub(crate) type AccumKeypair = Keypair; +pub type AccumSetupParamsKV = vb_accumulator::setup_keyed_verification::SetupParams; +pub type AccumPkKV = vb_accumulator::setup_keyed_verification::PublicKey; /// Generate accumulator parameters. They are needed to generate public key and initialize the accumulator. /// Pass the `label` argument to generate parameters deterministically. @@ -26,6 +25,15 @@ pub fn generate_accumulator_params(label: Option>) -> Result>) -> Result { + set_panic_hook(); + let label = label.unwrap_or_else(random_bytes); + let params = AccumSetupParamsKV::new::(&label); + Ok(obj_to_uint8array!(¶ms, false, "SetupParamsKV")) +} + /// Check if parameters are valid. Before verifying witness or using for proof verification, /// make sure the params are valid. #[wasm_bindgen(js_name = isAccumulatorParamsValid)] @@ -60,6 +68,19 @@ pub fn accumulator_generate_public_key( Ok(obj_to_uint8array!(&pk, false, "PublicKeyG2")) } +/// Generate public key from given params and secret key. +#[wasm_bindgen(js_name = generateAccumulatorPublicKeyForKeyedVerification)] +pub fn accumulator_generate_public_key_for_keyed_verification( + secret_key: JsValue, + params: js_sys::Uint8Array, +) -> Result { + set_panic_hook(); + let sk: AccumSk = serde_wasm_bindgen::from_value(secret_key)?; + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let pk = AccumPkKV::new_from_secret_key(&sk, ¶ms); + Ok(obj_to_uint8array!(&pk, false, "PublicKeyG1")) +} + /// 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)] diff --git a/src/js/accumulator_wasm.js b/src/js/accumulator_wasm.js index 960e2fc..33bd3dc 100644 --- a/src/js/accumulator_wasm.js +++ b/src/js/accumulator_wasm.js @@ -20,6 +20,11 @@ module.exports.isAccumulatorParamsValid = (params) => { return wasm.isAccumulatorParamsValid(params) }; +module.exports.generateAccumulatorParamsForKeyedVerification = (label) => { + requireWasmInitialized(); + return wasm.generateAccumulatorParamsForKeyedVerification(label) +}; + module.exports.generateAccumulatorPublicKey = (secretKey, params) => { requireWasmInitialized(); return wasm.generateAccumulatorPublicKey(secretKey, params) @@ -35,6 +40,11 @@ module.exports.generateAccumulatorKeyPair = (params, seed) => { return wasm.generateAccumulatorKeyPair(params, seed) }; +module.exports.generateAccumulatorPublicKeyForKeyedVerification = (secretKey, params) => { + requireWasmInitialized(); + return wasm.generateAccumulatorPublicKeyForKeyedVerification(secretKey, params) +}; + module.exports.accumulatorGetElementFromBytes = (bytes) => { requireWasmInitialized(); return wasm.accumulatorGetElementFromBytes(bytes) diff --git a/src/js/keyed_proof_wasm.js b/src/js/keyed_proof_wasm.js index d7e4bb0..ce5c4f6 100644 --- a/src/js/keyed_proof_wasm.js +++ b/src/js/keyed_proof_wasm.js @@ -37,12 +37,72 @@ module.exports.verifyVBAccumMembershipKeyedProof = (proof, secretKey) => { return wasm.verifyVBAccumMembershipKeyedProof(proof, secretKey); }; +module.exports.proofOfValidityOfVBAccumMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfValidityOfVBAccumMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfValidityOfVBAccumMembershipKeyedProof = (proofOfValidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfValidityOfVBAccumMembershipKeyedProof(proofOfValidity, keyedProof, publicKey, params); +}; + +module.exports.proofOfInvalidityOfVBAccumMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfInvalidityOfVBAccumMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfInvalidityOfVBAccumMembershipKeyedProof = (proofOfInvalidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfInvalidityOfVBAccumMembershipKeyedProof(proofOfInvalidity, keyedProof, publicKey, params); +}; + module.exports.verifyKBUniAccumMembershipKeyedProof = (proof, secretKey) => { requireWasmInitialized(); return wasm.verifyKBUniAccumMembershipKeyedProof(proof, secretKey); }; +module.exports.proofOfValidityOfKBUniAccumMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfValidityOfKBUniAccumMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfValidityOfKBUniAccumMembershipKeyedProof = (proofOfValidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfValidityOfKBUniAccumMembershipKeyedProof(proofOfValidity, keyedProof, publicKey, params); +}; + +module.exports.proofOfInvalidityOfKBUniAccumMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfInvalidityOfKBUniAccumMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfInvalidityOfKBUniAccumMembershipKeyedProof = (proofOfInvalidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfInvalidityOfKBUniAccumMembershipKeyedProof(proofOfInvalidity, keyedProof, publicKey, params); +}; + module.exports.verifyKBUniAccumNonMembershipKeyedProof = (proof, secretKey) => { requireWasmInitialized(); return wasm.verifyKBUniAccumNonMembershipKeyedProof(proof, secretKey); -}; \ No newline at end of file +}; + +module.exports.proofOfValidityOfKBUniAccumNonMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfValidityOfKBUniAccumNonMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof = (proofOfValidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof(proofOfValidity, keyedProof, publicKey, params); +}; + +module.exports.proofOfInvalidityOfKBUniAccumNonMembershipKeyedProof = (proof, secretKey, publicKey, params) => { + requireWasmInitialized(); + return wasm.proofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proof, secretKey, publicKey, params); +}; + +module.exports.verifyProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof = (proofOfInvalidity, keyedProof, publicKey, params) => { + requireWasmInitialized(); + return wasm.verifyProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proofOfInvalidity, keyedProof, publicKey, params); +}; diff --git a/src/js/type_declarations/accumulator.d.ts b/src/js/type_declarations/accumulator.d.ts index 8ca4097..e2e3097 100644 --- a/src/js/type_declarations/accumulator.d.ts +++ b/src/js/type_declarations/accumulator.d.ts @@ -8,6 +8,10 @@ export function isAccumulatorParamsValid( params: Uint8Array ): boolean; +export function generateAccumulatorParamsForKeyedVerification( + label?: Uint8Array +): Uint8Array; + export function generateAccumulatorSecretKey(seed?: Uint8Array): Uint8Array; export function generateAccumulatorPublicKey( @@ -24,6 +28,11 @@ export function generateAccumulatorKeyPair( seed?: Uint8Array ): Required; +export function generateAccumulatorPublicKeyForKeyedVerification( + secretKey: Uint8Array, + params: Uint8Array +): Uint8Array; + export function accumulatorGetElementFromBytes( bytes: Uint8Array, ): Uint8Array; diff --git a/src/js/type_declarations/keyed_proof.d.ts b/src/js/type_declarations/keyed_proof.d.ts index 8f1bccc..5012cf4 100644 --- a/src/js/type_declarations/keyed_proof.d.ts +++ b/src/js/type_declarations/keyed_proof.d.ts @@ -14,6 +14,30 @@ export function verifyProofOfInvalidityOfBDDT16KeyedProof(proofOfInvalidity: Uin export function verifyVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required; +export function proofOfValidityOfVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfValidityOfVBAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; + +export function proofOfInvalidityOfVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfInvalidityOfVBAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; + export function verifyKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required; -export function verifyKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required; \ No newline at end of file +export function proofOfValidityOfKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfValidityOfKBUniAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; + +export function proofOfInvalidityOfKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfInvalidityOfKBUniAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; + +export function verifyKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required; + +export function proofOfValidityOfKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; + +export function proofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array; + +export function verifyProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required; diff --git a/src/keyed_proof.rs b/src/keyed_proof.rs index 164ec1e..48a10ef 100644 --- a/src/keyed_proof.rs +++ b/src/keyed_proof.rs @@ -16,13 +16,18 @@ use kvac::bddt_2016::keyed_proof::{ use proof_system::prelude::StatementProof; use vb_accumulator::{ kb_universal_accumulator::proofs_keyed_verification::{ - KBUniversalAccumulatorKeyedMembershipProof as KBUniMemKp, - KBUniversalAccumulatorKeyedNonMembershipProof as KBUniNonMemKp, + KBUniversalAccumulatorKeyedMembershipProof as KBUniMemKp, KBUniversalAccumulatorProofOfValidityOfKeyedMembershipProof as KBUniMemPvkp, + KBUniversalAccumulatorProofOfInvalidityOfKeyedMembershipProof as KBUniMemPivkp, + KBUniversalAccumulatorKeyedNonMembershipProof as KBUniNonMemKp, KBUniversalAccumulatorProofOfValidityOfKeyedNonMembershipProof as KBUniNonMemPvkp, + KBUniversalAccumulatorProofOfInvalidityOfKeyedNonMembershipProof as KBUniNonMemPivkp + }, + proofs_keyed_verification::{ + KeyedMembershipProof as VBMemKp, ProofOfValidityOfKeyedMembershipProof as VbMemPvkp, ProofOfInvalidityOfKeyedMembershipProof as VbMemPivkp }, - proofs_keyed_verification::KeyedMembershipProof as VBMemDp, }; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; use zeroize::Zeroize; +use crate::accumulator::common::{AccumPkKV, AccumSetupParamsKV}; #[wasm_bindgen(js_name = getAllKeyedSubproofsFromProof)] pub fn get_all_keyed_subproofs_from_proof(proof: Uint8Array) -> Result { @@ -188,11 +193,95 @@ pub fn verify_vb_accum_membership_keyed_proof( secret_key: Uint8Array, ) -> Result { set_panic_hook(); - let proof = obj_from_uint8array!(VBMemDp, proof, false, "VBMembershipKeyedProof"); + let proof = obj_from_uint8array!(VBMemKp, proof, false, "VBMembershipKeyedProof"); let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); to_verify_response!(proof.verify(&sk)) } +#[wasm_bindgen(js_name = proofOfValidityOfVBAccumMembershipKeyedProof)] +pub fn proof_of_validity_of_vb_accum_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(VBMemKp, proof, false, "VBMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + let p = proof.create_proof_of_validity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms); + Ok(obj_to_uint8array!( + &p, + false, + "ProofOfValidityOfVBAccumMembershipKeyedProof" + )) +} + +#[wasm_bindgen(js_name = verifyProofOfValidityOfVBAccumMembershipKeyedProof)] +pub fn verify_proof_of_validity_of_vb_accum_membership_keyed_proof( + proof_of_validity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_validity = obj_from_uint8array!( + VbMemPvkp, + proof_of_validity, + false, + "ProofOfValidityOfVBAccumMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(VBMemKp, keyed_proof, false, "VBMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_validity.verify::(&keyed_proof, &pk, ¶ms)) +} + +#[wasm_bindgen(js_name = proofOfInvalidityOfVBAccumMembershipKeyedProof)] +pub fn proof_of_invalidity_of_vb_accum_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(VBMemKp, proof, false, "VBMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + match proof.create_proof_of_invalidity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms) { + Ok(p) => Ok(obj_to_uint8array!( + &p, + false, + "ProofOfInvalidityOfVBAccumMembershipKeyedProof" + )), + Err(e) => Err(JsValue::from(&format!("{:?}", e))), + } +} + +#[wasm_bindgen(js_name = verifyProofOfInvalidityOfVBAccumMembershipKeyedProof)] +pub fn verify_proof_of_invalidity_of_vb_accum_membership_keyed_proof( + proof_of_invalidity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_invalidity = obj_from_uint8array!( + VbMemPivkp, + proof_of_invalidity, + false, + "ProofOfInvalidityOfVBAccumMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(VBMemKp, keyed_proof, false, "VBMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_invalidity.verify::(&keyed_proof, &pk, ¶ms)) +} + #[wasm_bindgen(js_name = verifyKBUniAccumMembershipKeyedProof)] pub fn verify_kb_uni_accum_membership_keyed_proof( proof: Uint8Array, @@ -209,6 +298,90 @@ pub fn verify_kb_uni_accum_membership_keyed_proof( to_verify_response!(proof.verify(&sk)) } +#[wasm_bindgen(js_name = proofOfValidityOfKBUniAccumMembershipKeyedProof)] +pub fn proof_of_validity_of_kb_uni_accum_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(KBUniMemKp, proof, false, "KBUniMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + let p = proof.create_proof_of_validity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms); + Ok(obj_to_uint8array!( + &p, + false, + "ProofOfValidityOfKBUniAccumMembershipKeyedProof" + )) +} + +#[wasm_bindgen(js_name = verifyProofOfValidityOfKBUniAccumMembershipKeyedProof)] +pub fn verify_proof_of_validity_of_kb_uni_accum_membership_keyed_proof( + proof_of_validity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_validity = obj_from_uint8array!( + KBUniMemPvkp, + proof_of_validity, + false, + "ProofOfValidityOfKBUniAccumMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(KBUniMemKp, keyed_proof, false, "KBUniMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_validity.verify::(&keyed_proof, &pk, ¶ms)) +} + +#[wasm_bindgen(js_name = proofOfInvalidityOfKBUniAccumMembershipKeyedProof)] +pub fn proof_of_invalidity_of_kb_uni_accum_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(KBUniMemKp, proof, false, "KBUniMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + match proof.create_proof_of_invalidity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms) { + Ok(p) => Ok(obj_to_uint8array!( + &p, + false, + "ProofOfInvalidityOfKBUniAccumMembershipKeyedProof" + )), + Err(e) => Err(JsValue::from(&format!("{:?}", e))), + } +} + +#[wasm_bindgen(js_name = verifyProofOfInvalidityOfKBUniAccumMembershipKeyedProof)] +pub fn verify_proof_of_invalidity_of_kb_uni_accum_membership_keyed_proof( + proof_of_invalidity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_invalidity = obj_from_uint8array!( + KBUniMemPivkp, + proof_of_invalidity, + false, + "ProofOfInvalidityOfKBUniAccumMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(KBUniMemKp, keyed_proof, false, "KBUniMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_invalidity.verify::(&keyed_proof, &pk, ¶ms)) +} + #[wasm_bindgen(js_name = verifyKBUniAccumNonMembershipKeyedProof)] pub fn verify_kb_uni_accum_non_membership_keyed_proof( proof: Uint8Array, @@ -224,3 +397,87 @@ pub fn verify_kb_uni_accum_non_membership_keyed_proof( let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); to_verify_response!(proof.verify(&sk)) } + +#[wasm_bindgen(js_name = proofOfValidityOfKBUniAccumNonMembershipKeyedProof)] +pub fn proof_of_validity_of_kb_uni_accum_non_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(KBUniNonMemKp, proof, false, "KBUniNonMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + let p = proof.create_proof_of_validity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms); + Ok(obj_to_uint8array!( + &p, + false, + "ProofOfValidityOfKBUniAccumNonMembershipKeyedProof" + )) +} + +#[wasm_bindgen(js_name = verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof)] +pub fn verify_proof_of_validity_of_kb_uni_accum_non_membership_keyed_proof( + proof_of_validity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_validity = obj_from_uint8array!( + KBUniNonMemPvkp, + proof_of_validity, + false, + "ProofOfValidityOfKBUniAccumNonMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(KBUniNonMemKp, keyed_proof, false, "KBUniNonMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_validity.verify::(&keyed_proof, &pk, ¶ms)) +} + +#[wasm_bindgen(js_name = proofOfInvalidityOfKBUniAccumNonMembershipKeyedProof)] +pub fn proof_of_invalidity_of_kb_uni_accum_non_membership_keyed_proof( + proof: Uint8Array, + secret_key: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof = obj_from_uint8array!(KBUniNonMemKp, proof, false, "KBUniNonMembershipKeyedProof"); + let sk = obj_from_uint8array!(AccumSk, secret_key, true, "VBAccumulatorSk"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + let mut rng = get_seeded_rng(); + match proof.create_proof_of_invalidity::<_, Blake2b512>(&mut rng, &sk, &pk, ¶ms) { + Ok(p) => Ok(obj_to_uint8array!( + &p, + false, + "ProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof" + )), + Err(e) => Err(JsValue::from(&format!("{:?}", e))), + } +} + +#[wasm_bindgen(js_name = verifyProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof)] +pub fn verify_proof_of_invalidity_of_kb_uni_accum_non_membership_keyed_proof( + proof_of_invalidity: Uint8Array, + keyed_proof: Uint8Array, + public_key: Uint8Array, + params: Uint8Array, +) -> Result { + set_panic_hook(); + let proof_of_invalidity = obj_from_uint8array!( + KBUniNonMemPivkp, + proof_of_invalidity, + false, + "ProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof" + ); + let keyed_proof = obj_from_uint8array!(KBUniNonMemKp, keyed_proof, false, "KBUniNonMembershipKeyedProof"); + let pk = obj_from_uint8array!(AccumPkKV, public_key, false, "VBAccumulatorPkG1"); + let params = obj_from_uint8array!(AccumSetupParamsKV, params, false); + to_verify_response!(proof_of_invalidity.verify::(&keyed_proof, &pk, ¶ms)) +} diff --git a/src/threshold_sig/signing.rs b/src/threshold_sig/signing.rs index 71189fe..f1cd48f 100644 --- a/src/threshold_sig/signing.rs +++ b/src/threshold_sig/signing.rs @@ -3,7 +3,7 @@ use crate::{ bbs_plus::BBSPlusSigParamsG1, threshold_sig::{KAPPA, SALT_SIZE, STATISTICAL_SECURITY_PARAMETER}, utils::{ - encode_messages_as_js_array_to_fr_vec, fr_from_uint8_array, get_seeded_rng, + encode_messages_as_js_array_to_fr_vec_in_constant_time, fr_from_uint8_array, get_seeded_rng, js_array_to_iter, js_set_to_btree_set, set_panic_hook, }, Fr, @@ -109,7 +109,7 @@ macro_rules! create_signature_share { ($messages: ident, $index_in_output: ident, $phase1_output: ident, $phase2_output: ident, $params: ident, $encode_messages: ident, $params_type: ident, $phase1_output_type: ident, $share_type: ident, $sig_name: expr) => {{ set_panic_hook(); let params: $params_type = serde_wasm_bindgen::from_value($params)?; - let messages = encode_messages_as_js_array_to_fr_vec(&$messages, $encode_messages)?; + let messages = encode_messages_as_js_array_to_fr_vec_in_constant_time(&$messages, $encode_messages)?; let phase1_output = obj_from_uint8array!($phase1_output_type, $phase1_output, true); let phase2_output = obj_from_uint8array!(Phase2Output, $phase2_output, true); let share = $share_type::new( diff --git a/tests/js/bbs.spec.ts b/tests/js/bbs.spec.ts index 8178f70..d3f1bc1 100644 --- a/tests/js/bbs.spec.ts +++ b/tests/js/bbs.spec.ts @@ -32,7 +32,10 @@ import { bbsChallengeContributionFromProofConstantTime, bbsInitializeProofOfKnowledgeOfSignatureNew, bbsVerifyProofOfKnowledgeOfSignatureNew, - bbsChallengeContributionFromProofNew, bbsChallengeContributionFromProtocolNew, bbsGenProofOfKnowledgeOfSignatureNew, + bbsChallengeContributionFromProofNew, + bbsChallengeContributionFromProtocolNew, + bbsGenProofOfKnowledgeOfSignatureNew, + encodeMessageForSigningInConstantTime, } from "../../lib"; @@ -122,6 +125,18 @@ describe("For BBS signatures", () => { expect(res.verified).toBe(true); }); + it("generate and verify signature in G1 with encoded messages", () => { + const encMsgs = messages.map((m) => encodeMessageForSigningInConstantTime(m)); + + const sig1 = bbsSignConstantTime(encMsgs, sk, sigParamsG1, false); + const res1 = bbsVerify(encMsgs, sig1, pkG2, sigParamsG1, false); + expect(res1.verified).toBe(true); + + const sig2 = bbsSign(encMsgs, sk, sigParamsG1, false); + const res2 = bbsVerifyConstantTime(encMsgs, sig2, pkG2, sigParamsG1, false); + expect(res2.verified).toBe(true); + }) + it("extend signature params in G1", () => { const label = stringToBytes("Sig params g1"); const params0 = bbsGenerateSignatureParams(1); diff --git a/tests/js/bbsPlus.spec.ts b/tests/js/bbsPlus.spec.ts index 0792dc6..893c4a7 100644 --- a/tests/js/bbsPlus.spec.ts +++ b/tests/js/bbsPlus.spec.ts @@ -47,7 +47,9 @@ import { bbsPlusCommitMsgsInG2ConstantTime, bbsPlusInitializeProofOfKnowledgeOfSignatureConstantTime, bbsPlusVerifyProofOfKnowledgeOfSignatureConstantTime, - bbsPlusChallengeContributionFromProofConstantTime, bbsPlusChallengeContributionFromProtocolConstantTime, + bbsPlusChallengeContributionFromProofConstantTime, + bbsPlusChallengeContributionFromProtocolConstantTime, + encodeMessageForSigningInConstantTime, } from "../../lib"; @@ -181,6 +183,18 @@ describe("For BBS+ signatures", () => { expect(res.verified).toBe(true); }); + it("generate and verify signature in G1 with encoded messages", () => { + const encMsgs = messages.map((m) => encodeMessageForSigningInConstantTime(m)); + + const sig1 = bbsPlusSignG1ConstantTime(encMsgs, sk, sigParamsG1, false); + const res1 = bbsPlusVerifyG1(encMsgs, sig1, pkG2, sigParamsG1, false); + expect(res1.verified).toBe(true); + + const sig2 = bbsPlusSignG1(encMsgs, sk, sigParamsG1, false); + const res2 = bbsPlusVerifyG1ConstantTime(encMsgs, sig2, pkG2, sigParamsG1, false); + expect(res2.verified).toBe(true); + }) + it("generate and verify signature in G2", () => { const sig = bbsPlusSignG2(messages, sk, sigParamsG2, true); const res = bbsPlusVerifyG2(messages, sig, pkG1, sigParamsG2, true); diff --git a/tests/js/bddt16Kvac.spec.ts b/tests/js/bddt16Kvac.spec.ts index d69c8c0..d7c7fe8 100644 --- a/tests/js/bddt16Kvac.spec.ts +++ b/tests/js/bddt16Kvac.spec.ts @@ -20,7 +20,9 @@ import { bddt16MacVerifyProofOfValidity, bddt16MacGenerateConstantTime, bddt16MacVerifyConstantTime, - bddt16MacCommitMsgsConstantTime, bddt16BlindMacGenerateConstantTime + bddt16MacCommitMsgsConstantTime, + bddt16BlindMacGenerateConstantTime, + encodeMessageForSigningInConstantTime, } from "../../lib"; import {checkResult, stringToBytes} from "./util"; @@ -100,6 +102,18 @@ describe("For BDDT16 MAC", () => { checkResult(bddt16MacVerifyProofOfValidity(proofOfValidity, mac, messages, pkG1, macParams, true)); }); + it("generate and verify signature in G1 with encoded messages", () => { + const encMsgs = messages.map((m) => encodeMessageForSigningInConstantTime(m)); + + const sig1 = bddt16MacGenerateConstantTime(encMsgs, sk, macParams, false); + const res1 = bddt16MacVerify(encMsgs, sig1, sk, macParams, false); + expect(res1.verified).toBe(true); + + const sig2 = bddt16MacGenerate(encMsgs, sk, macParams, false); + const res2 = bddt16MacVerifyConstantTime(encMsgs, sig2, sk, macParams, false); + expect(res2.verified).toBe(true); + }) + it("extend MAC params", () => { const label = stringToBytes("MAC param"); const params0 = bddt16GenerateMacParams(1); diff --git a/tests/js/proofSystem.spec.ts b/tests/js/proofSystem.spec.ts index d97da7b..a02777f 100644 --- a/tests/js/proofSystem.spec.ts +++ b/tests/js/proofSystem.spec.ts @@ -127,7 +127,15 @@ import { verifyProofOfValidityOfBDDT16KeyedProof, generatePoKBBSSignatureProverStatementNew, generatePoKBBSSignatureVerifierStatementNew, - generatePoKBBSSignatureProverStatementFromParamRefsNew, generatePoKBBSSignatureVerifierStatementFromParamRefsNew + generatePoKBBSSignatureProverStatementFromParamRefsNew, + generatePoKBBSSignatureVerifierStatementFromParamRefsNew, + generateAccumulatorPublicKeyForKeyedVerification, + generateAccumulatorParamsForKeyedVerification, + proofOfValidityOfVBAccumMembershipKeyedProof, + verifyProofOfValidityOfVBAccumMembershipKeyedProof, + proofOfValidityOfKBUniAccumMembershipKeyedProof, + verifyProofOfValidityOfKBUniAccumMembershipKeyedProof, + proofOfValidityOfKBUniAccumNonMembershipKeyedProof, verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof } from "../../lib"; import { BbsSigParams, PSSigParams } from "../../lib/types"; import {checkResult, getRevealedUnrevealed, stringToBytes} from "./util"; @@ -2089,10 +2097,27 @@ describe("Keyed proofs", () => { // @ts-ignore checkResult(verifyKBUniAccumNonMembershipKeyedProof(dp3[1], sk)); - const pkG1 = bddt16MacGeneratePublicKeyG1(bddt16Sk, bddt16Params); + const macPkG1 = bddt16MacGeneratePublicKeyG1(bddt16Sk, bddt16Params); // @ts-ignore - const pv = proofOfValidityOfBDDT16KeyedProof(dp0[1], bddt16Sk, pkG1, bddt16Params); + const pv1 = proofOfValidityOfBDDT16KeyedProof(dp0[1], bddt16Sk, macPkG1, bddt16Params); // @ts-ignore - checkResult(verifyProofOfValidityOfBDDT16KeyedProof(pv, dp0[1], pkG1, bddt16Params)); + checkResult(verifyProofOfValidityOfBDDT16KeyedProof(pv1, dp0[1], macPkG1, bddt16Params)); + + const accumParamsKv = generateAccumulatorParamsForKeyedVerification(); + const accumPkG1 = generateAccumulatorPublicKeyForKeyedVerification(sk, accumParamsKv); + // @ts-ignore + const pv2 = proofOfValidityOfVBAccumMembershipKeyedProof(dp1[1], sk, accumPkG1, accumParamsKv); + // @ts-ignore + checkResult(verifyProofOfValidityOfVBAccumMembershipKeyedProof(pv2, dp1[1], accumPkG1, accumParamsKv)); + + // @ts-ignore + const pv3 = proofOfValidityOfKBUniAccumMembershipKeyedProof(dp2[1], sk, accumPkG1, accumParamsKv); + // @ts-ignore + checkResult(verifyProofOfValidityOfKBUniAccumMembershipKeyedProof(pv3, dp2[1], accumPkG1, accumParamsKv)); + + // @ts-ignore + const pv4 = proofOfValidityOfKBUniAccumNonMembershipKeyedProof(dp3[1], sk, accumPkG1, accumParamsKv); + // @ts-ignore + checkResult(verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof(pv4, dp3[1], accumPkG1, accumParamsKv)); }) }) \ No newline at end of file