Skip to content

Commit

Permalink
Fix some functions and add some more
Browse files Browse the repository at this point in the history
Signed-off-by: lovesh <lovesh.bond@gmail.com>
  • Loading branch information
lovesh committed Jun 24, 2024
1 parent a9eac81 commit 830e6d4
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 20 deletions.
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.31.0",
"version": "0.32.0",
"author": "Dock.io",
"license": "Apache-2.0",
"private": false,
Expand Down
29 changes: 25 additions & 4 deletions src/accumulator/common.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +12,8 @@ pub(crate) type AccumSk = SecretKey<Fr>;
pub type AccumPk = PublicKey<Bls12_381>;
pub type AccumSetupParams = SetupParams<Bls12_381>;
pub(crate) type AccumKeypair = Keypair<Bls12_381>;
pub type AccumSetupParamsKV = vb_accumulator::setup_keyed_verification::SetupParams<G1Affine>;
pub type AccumPkKV = vb_accumulator::setup_keyed_verification::PublicKey<G1Affine>;

/// Generate accumulator parameters. They are needed to generate public key and initialize the accumulator.
/// Pass the `label` argument to generate parameters deterministically.
Expand All @@ -26,6 +25,15 @@ pub fn generate_accumulator_params(label: Option<Vec<u8>>) -> Result<js_sys::Uin
Ok(obj_to_uint8array!(&params, false, "SetupParams"))
}

/// Generate accumulator parameters for keyed-verification. Pass the `label` argument to generate parameters deterministically.
#[wasm_bindgen(js_name = generateAccumulatorParamsForKeyedVerification)]
pub fn generate_accumulator_params_for_keyed_verification(label: Option<Vec<u8>>) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let label = label.unwrap_or_else(random_bytes);
let params = AccumSetupParamsKV::new::<Blake2b512>(&label);
Ok(obj_to_uint8array!(&params, 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)]
Expand Down Expand Up @@ -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<js_sys::Uint8Array, JsValue> {
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, &params);
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)]
Expand Down
10 changes: 10 additions & 0 deletions src/js/accumulator_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
62 changes: 61 additions & 1 deletion src/js/keyed_proof_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};

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);
};
9 changes: 9 additions & 0 deletions src/js/type_declarations/accumulator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -24,6 +28,11 @@ export function generateAccumulatorKeyPair(
seed?: Uint8Array
): Required<IKeypair>;

export function generateAccumulatorPublicKeyForKeyedVerification(
secretKey: Uint8Array,
params: Uint8Array
): Uint8Array;

export function accumulatorGetElementFromBytes(
bytes: Uint8Array,
): Uint8Array;
Expand Down
26 changes: 25 additions & 1 deletion src/js/type_declarations/keyed_proof.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ export function verifyProofOfInvalidityOfBDDT16KeyedProof(proofOfInvalidity: Uin

export function verifyVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required<VerifyResult>;

export function proofOfValidityOfVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfValidityOfVBAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;

export function proofOfInvalidityOfVBAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfInvalidityOfVBAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;

export function verifyKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required<VerifyResult>;

export function verifyKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required<VerifyResult>;
export function proofOfValidityOfKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfValidityOfKBUniAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;

export function proofOfInvalidityOfKBUniAccumMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfInvalidityOfKBUniAccumMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;

export function verifyKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array): Required<VerifyResult>;

export function proofOfValidityOfKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfValidityOfKBUniAccumNonMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;

export function proofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proof: Uint8Array, secretKey: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Uint8Array;

export function verifyProofOfInvalidityOfKBUniAccumNonMembershipKeyedProof(proofOfValidity: Uint8Array, keyedProof: Uint8Array, publicKey: Uint8Array, params: Uint8Array): Required<VerifyResult>;
Loading

0 comments on commit 830e6d4

Please sign in to comment.