Skip to content

Commit

Permalink
Add proof of validity for KVAC
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 7, 2024
1 parent 1ad8cb0 commit 85cedd7
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 22 deletions.
2 changes: 1 addition & 1 deletion 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 @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "dock_crypto_wasm"
repository = "https://github.com/docknetwork/crypto-wasm"
version = "0.18.0"
version = "0.19.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -15,7 +15,7 @@ console = ["console_error_panic_hook"]
default = ["dlmalloc"]

[dependencies]
console_error_panic_hook = { version = "0.1.1", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
js-sys = "0.3"
rand = { version = "0.7", features = ["wasm-bindgen"] }
serde = { version = "1.0", features = ["derive"] }
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.24.0",
"version": "0.25.0",
"author": "Dock.io",
"license": "Apache-2.0",
"private": false,
Expand Down
6 changes: 3 additions & 3 deletions scripts/build-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ export PATH="$PATH:node_modules/.bin"

if [ -z "$BUILD_MODE" ]
then
echo "BUILD_MODE not specified defaulting to RELEASE"
echo "BUILD_MODE not specified, defaulting to RELEASE"
BUILD_MODE="RELEASE"
fi

# Build based on input parameter
if [ "$BUILD_MODE" = "RELEASE" ];
then
echo "Building WASM Output in RELEASE MODE"
rustup run stable wasm-pack build --release --out-dir lib --target web
rustup run stable wasm-pack build --release --out-dir lib --target web -- --features="console"
elif [ "$BUILD_MODE" = "PROFILING" ];
then
echo "Building WASM Output in PROFILING MODE"
rustup run stable wasm-pack build --profiling --out-dir lib --target web
rustup run stable wasm-pack build --profiling --out-dir lib --target web -- --features="console"
elif [ "$BUILD_MODE" = "DEBUG" ];
then
echo "Building WASM Output in DEBUG MODE"
Expand Down
4 changes: 0 additions & 4 deletions src/bbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ pub fn bbs_verify(
encode_messages: bool,
) -> Result<JsValue, JsValue> {
set_panic_hook();
// let signature: SigG1 = serde_wasm_bindgen::from_value(signature)?;
let signature = obj_from_uint8array!(BBSSignature, signature, true);
let pk = obj_from_uint8array!(BBSPublicKey, public_key, false, "BBSPublicKey");
let params: BBSSigParams = serde_wasm_bindgen::from_value(params)?;
Expand Down Expand Up @@ -252,7 +251,6 @@ pub fn bbs_gen_proof(
let protocol: BBSPoKOfSigProtocol = serde_wasm_bindgen::from_value(protocol)?;
let challenge = fr_from_uint8_array(challenge, false)?;
match protocol.gen_proof(&challenge) {
// Ok(proof) => Ok(serde_wasm_bindgen::to_value(&proof).map_err(|e| JsValue::from(e)).unwrap()),
Ok(proof) => Ok(obj_to_uint8array!(&proof, false, "BBS+ProofG1")),
Err(e) => Err(JsValue::from(&format!("{:?}", e))),
}
Expand All @@ -268,7 +266,6 @@ pub fn bbs_verify_proof(
encode_messages: bool,
) -> Result<JsValue, JsValue> {
set_panic_hook();
// let proof: PoKOfSigProof = serde_wasm_bindgen::from_value(proof)?;
let proof: BBSPoKOfSigProof = obj_from_uint8array!(BBSPoKOfSigProof, proof, false);
let params: BBSSigParams = serde_wasm_bindgen::from_value(params)?;
let public_key = obj_from_uint8array!(BBSPublicKey, public_key, false, "BBSPublicKey");
Expand Down Expand Up @@ -309,7 +306,6 @@ pub fn bbs_challenge_contribution_from_proof(
encode_messages: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
// let proof: PoKOfSigProof = serde_wasm_bindgen::from_value(proof)?;
let proof: BBSPoKOfSigProof = obj_from_uint8array!(BBSPoKOfSigProof, proof, false);
let msgs = encode_messages_as_js_map_to_fr_btreemap(&revealed_msgs, encode_messages)?;
let params: BBSSigParams = serde_wasm_bindgen::from_value(params)?;
Expand Down
3 changes: 0 additions & 3 deletions src/bbs_plus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ pub fn bbs_plus_sign_g2(

let mut rng = get_seeded_rng();
match BBSPlusSigG2::new(&mut rng, &messages, &sk, &params) {
// Ok(sig) => Ok(serde_wasm_bindgen::to_value(&sig).map_err(|e| JsValue::from(e)).unwrap()),
Ok(sig) => Ok(obj_to_uint8array!(&sig, true, "BBSPlusSigG2")),
Err(e) => Err(JsValue::from(&format!("{:?}", e))),
}
Expand All @@ -388,7 +387,6 @@ pub fn bbs_plus_blind_sign_g2(

let mut rng = get_seeded_rng();
match BBSPlusSigG2::new_with_committed_messages(&mut rng, &commitment, msgs_ref, &sk, &params) {
// Ok(sig) => Ok(serde_wasm_bindgen::to_value(&sig).map_err(|e| JsValue::from(e)).unwrap()),
Ok(sig) => Ok(obj_to_uint8array!(&sig, true, "BBSPlusSigG2")),
Err(e) => Err(JsValue::from(&format!("{:?}", e))),
}
Expand Down Expand Up @@ -528,7 +526,6 @@ pub fn bbs_plus_challenge_contribution_from_proof(
encode_messages: bool,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
// let proof: PoKOfSigProof = serde_wasm_bindgen::from_value(proof)?;
let proof: BBSPlusPoKOfSigProof = obj_from_uint8array!(BBSPlusPoKOfSigProof, proof, false);
let msgs = encode_messages_as_js_map_to_fr_btreemap(&revealed_msgs, encode_messages)?;
let params: BBSPlusSigParamsG1 = serde_wasm_bindgen::from_value(params)?;
Expand Down
59 changes: 58 additions & 1 deletion src/bddt16_kvac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ use dock_crypto_utils::{
};
use kvac::bddt_2016::{
mac::MAC,
setup::{MACParams, SecretKey},
setup::{MACParams, SecretKey, PublicKey},
};
use std::collections::BTreeMap;
use ark_ec::AffineRepr;
use kvac::bddt_2016::mac::ProofOfValidityOfMAC;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use zeroize::Zeroize;

pub type BDDT16MACParams = MACParams<G1Affine>;
pub type BDDT16MACSecretKey = SecretKey<Fr>;
pub type BDDT16MACPublicKeyG1 = PublicKey<G1Affine>;
pub type BDDT16MAC = MAC<G1Affine>;
pub type ProofOfValidityOfMACG1 = ProofOfValidityOfMAC<G1Affine>;

#[wasm_bindgen(js_name = bddt16GenerateMacParams)]
pub fn bddt16_generate_mac_params(
Expand Down Expand Up @@ -92,6 +96,25 @@ pub fn bddt16_mac_generate_secret_key(
Ok(obj_to_uint8array!(&sk, true, "BDDT16MACSecretKey"))
}

#[wasm_bindgen(js_name = bddt16MacGeneratePublicKeyG1)]
pub fn bddt16_mac_generate_public_key_g1(
secret_key: js_sys::Uint8Array,
params: JsValue,
) -> Result<js_sys::Uint8Array, JsValue> {
set_panic_hook();
let sk = obj_from_uint8array!(BDDT16MACSecretKey, secret_key, true, "BDDT16MACSecretKey");
let params: BDDT16MACParams = serde_wasm_bindgen::from_value(params)?;
let pk = BDDT16MACPublicKeyG1::new(&sk, &params.g_0);
Ok(obj_to_uint8array!(&pk, false, "BDDT16MACPublicKeyG1"))
}

#[wasm_bindgen(js_name = bddt16MacIsPublicKeyG1Valid)]
pub fn bddt16_mac_is_pubkey_g1_valid(public_key: js_sys::Uint8Array) -> Result<bool, JsValue> {
set_panic_hook();
let pk = obj_from_uint8array!(BDDT16MACPublicKeyG1, public_key, false, "BDDT16MACPublicKeyG1");
Ok(!pk.0.is_zero())
}

#[wasm_bindgen(js_name = bddt16MacGetBasesForCommitment)]
pub fn bddt16_mac_get_bases_for_commitment(
params: JsValue,
Expand Down Expand Up @@ -195,6 +218,40 @@ pub fn bddt16_unblind_mac(
))
}

#[wasm_bindgen(js_name = bddt16MacProofOfValidity)]
pub fn bddt16_mac_proof_of_validity(
mac: js_sys::Uint8Array,
secret_key: js_sys::Uint8Array,
public_key: js_sys::Uint8Array,
params: JsValue,
) -> Result<js_sys::Uint8Array, JsValue> {
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 params: BDDT16MACParams = serde_wasm_bindgen::from_value(params)?;
let mut rng = get_seeded_rng();
let proof = ProofOfValidityOfMACG1::new::<_, Blake2b512>(&mut rng, &mac, &sk, &pk, &params);
Ok(obj_to_uint8array!(&proof, false, "ProofOfValidityOfMACG1"))
}

#[wasm_bindgen(js_name = bddt16MacVerifyProofOfValidity)]
pub fn bddt16_mac_verify_proof_of_validity(
proof: js_sys::Uint8Array,
mac: js_sys::Uint8Array,
messages: js_sys::Array,
public_key: js_sys::Uint8Array,
params: JsValue,
encode_messages: bool
) -> Result<JsValue, JsValue> {
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 params: BDDT16MACParams = serde_wasm_bindgen::from_value(params)?;
to_verify_response!(proof.verify::<Blake2b512>(&mac, messages.as_slice(), &pk, &params))
}

#[wasm_bindgen(js_name = bddt16MacVerify)]
pub fn bddt16_mac_verify(
messages: js_sys::Array,
Expand Down
32 changes: 32 additions & 0 deletions src/js/bdd16_kvac_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ module.exports.bddt16MacAdaptParamsForMsgCount = (params, generating_label, new_
return wasm.bddt16MacAdaptParamsForMsgCount(params, generating_label, new_count);
};

module.exports.bddt16MacGeneratePublicKeyG1 = (secretKey, params) => {
requireWasmInitialized();
return wasm.bddt16MacGeneratePublicKeyG1(secretKey, params);
};

module.exports.bddt16MacIsPublicKeyG1Valid = (publicKey) => {
requireWasmInitialized();
return wasm.bddt16MacIsPublicKeyG1Valid(publicKey);
};

module.exports.bddt16MacGetBasesForCommitment = (params, indicesToCommit) => {
requireWasmInitialized();
return wasm.bddt16MacGetBasesForCommitment(params, indicesToCommit);
Expand All @@ -68,6 +78,28 @@ module.exports.bddt16MacVerify = (
return wasm.bddt16MacVerify(messages, mac, secretKey, params, encodeMessages);
};

module.exports.bddt16MacProofOfValidity = (
mac,
secretKey,
publicKey,
params,
) => {
requireWasmInitialized();
return wasm.bddt16MacProofOfValidity(mac, secretKey, publicKey, params);
};

module.exports.bddt16MacVerifyProofOfValidity = (
proof,
mac,
messages,
publicKey,
params,
encodeMessages
) => {
requireWasmInitialized();
return wasm.bddt16MacVerifyProofOfValidity(proof, mac, messages, publicKey, params, encodeMessages);
};

module.exports.bddt16MacCommitMsgs = (
messages,
blinding,
Expand Down
20 changes: 20 additions & 0 deletions src/js/type_declarations/bdd16_kvac.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function bddt16MacAdaptParamsForMsgCount(
new_count: number
): Bddt16MacParams;

export function bddt16MacGeneratePublicKeyG1(secretKey: Uint8Array, params: Bddt16MacParams): Uint8Array;

export function bddt16MacIsPublicKeyG1Valid(publicKey: Uint8Array): boolean;

export function bddt16MacGetBasesForCommitment(
params: Bddt16MacParams,
indicesToCommit: number[]
Expand All @@ -49,6 +53,22 @@ export function bddt16MacVerify(
encodeMessages: boolean
): Required<VerifyResult>;

export function bddt16MacProofOfValidity(
mac: Uint8Array,
secretKey: Uint8Array,
publicKey: Uint8Array,
params: Bddt16MacParams,
): Uint8Array;

export function bddt16MacVerifyProofOfValidity(
proof: Uint8Array,
mac: Uint8Array,
messages: Uint8Array[],
publicKey: Uint8Array,
params: Bddt16MacParams,
encodeMessages: boolean
): Required<VerifyResult>;

export function bddt16MacCommitMsgs(
messages: Map<number, Uint8Array>,
blinding: Uint8Array,
Expand Down
29 changes: 22 additions & 7 deletions tests/js/bddt16Kvac.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import {
bddt16MacAdaptParamsForMsgCount,
bddt16BlindMacGenerate,
bddt16GenerateMacParams,
bddt16IsMacParamsValid, bddt16MacCommitMsgs,
bddt16IsMacParamsValid,
bddt16MacCommitMsgs,
bddt16MacGenerate,
bddt16MacGenerateSecretKey,
Bddt16MacParams,
bddt16MacParamsFromBytes,
bddt16MacParamsMaxSupportedMsgs,
bddt16MacParamsToBytes,
bddt16MacVerify, bddt16UnblindMac,
bddt16MacVerify,
bddt16UnblindMac,
generateRandomFieldElement,
initializeWasm
initializeWasm,
bddt16MacGeneratePublicKeyG1,
bddt16MacIsPublicKeyG1Valid,
bddt16MacProofOfValidity,
bddt16MacVerifyProofOfValidity
} from "../../lib";
import {stringToBytes} from "./util";
import {checkResult, stringToBytes} from "./util";

describe("For BBS+ signatures", () => {
let macParams: Bddt16MacParams, sk: Uint8Array;
let macParams: Bddt16MacParams, sk: Uint8Array, pkG1: Uint8Array;
const seed = new Uint8Array([0, 2, 3, 4, 5]);
const messages = [
stringToBytes("Message1"),
Expand Down Expand Up @@ -70,10 +76,19 @@ describe("For BBS+ signatures", () => {
macParams = params;
});

it("generate and verify MAC", () => {
it("generate public key in G1 from secret key", () => {
pkG1 = bddt16MacGeneratePublicKeyG1(sk, macParams);
expect(pkG1).toBeInstanceOf(Uint8Array);
expect(bddt16MacIsPublicKeyG1Valid(pkG1)).toBe(true);
});

it("generate and verify MAC and its proof of validity", () => {
const mac = bddt16MacGenerate(messages, sk, macParams, true);
const res = bddt16MacVerify(messages, mac, sk, macParams, true);
expect(res.verified).toBe(true);
checkResult(res);

let proofOfValidity = bddt16MacProofOfValidity(mac, sk, pkG1, macParams);
checkResult(bddt16MacVerifyProofOfValidity(proofOfValidity, mac, messages, pkG1, macParams, true));
});

it("extend MAC params", () => {
Expand Down

0 comments on commit 85cedd7

Please sign in to comment.