diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a8669b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +yarn.lock +node_modules diff --git a/musig-bindings/musig-bindings.d.ts b/musig-bindings/musig-bindings.d.ts index 2723184..bc546c2 100644 --- a/musig-bindings/musig-bindings.d.ts +++ b/musig-bindings/musig-bindings.d.ts @@ -1,10 +1,15 @@ /* tslint:disable */ /* eslint-disable */ /** -* @param {Uint32Array} seed +*/ +export class MusigBN256WasmAggregatedPubkey { + free(): void; +/** +* @param {Uint8Array} encoded_pubkeys * @returns {Uint8Array} */ -export function generate_keypair(seed: Uint32Array): Uint8Array; + static compute(encoded_pubkeys: Uint8Array): Uint8Array; +} /** */ export class MusigBN256WasmSigner { @@ -50,8 +55,7 @@ export class MusigBN256WasmVerifier { * @param {Uint8Array} message * @param {Uint8Array} encoded_pubkeys * @param {Uint8Array} encoded_signature -* @param {number} position * @returns {boolean} */ - static verify(message: Uint8Array, encoded_pubkeys: Uint8Array, encoded_signature: Uint8Array, position: number): boolean; + static verify(message: Uint8Array, encoded_pubkeys: Uint8Array, encoded_signature: Uint8Array): boolean; } diff --git a/musig-bindings/musig-bindings.js b/musig-bindings/musig-bindings.js index 45615f9..74a660e 100644 --- a/musig-bindings/musig-bindings.js +++ b/musig-bindings/musig-bindings.js @@ -57,6 +57,18 @@ function passArray8ToWasm0(arg, malloc) { return ptr; } +let cachegetInt32Memory0 = null; +function getInt32Memory0() { + if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { + cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory0; +} + +function getArrayU8FromWasm0(ptr, len) { + return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); +} + let cachegetUint32Memory0 = null; function getUint32Memory0() { if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) { @@ -71,33 +83,32 @@ function passArray32ToWasm0(arg, malloc) { WASM_VECTOR_LEN = arg.length; return ptr; } - -let cachegetInt32Memory0 = null; -function getInt32Memory0() { - if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { - cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachegetInt32Memory0; -} - -function getArrayU8FromWasm0(ptr, len) { - return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); -} /** -* @param {Uint32Array} seed -* @returns {Uint8Array} */ -module.exports.generate_keypair = function(seed) { - var ptr0 = passArray32ToWasm0(seed, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - wasm.generate_keypair(8, ptr0, len0); - var r0 = getInt32Memory0()[8 / 4 + 0]; - var r1 = getInt32Memory0()[8 / 4 + 1]; - var v1 = getArrayU8FromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 1); - return v1; -}; +class MusigBN256WasmAggregatedPubkey { + free() { + const ptr = this.ptr; + this.ptr = 0; + + wasm.__wbg_musigbn256wasmaggregatedpubkey_free(ptr); + } + /** + * @param {Uint8Array} encoded_pubkeys + * @returns {Uint8Array} + */ + static compute(encoded_pubkeys) { + var ptr0 = passArray8ToWasm0(encoded_pubkeys, wasm.__wbindgen_malloc); + var len0 = WASM_VECTOR_LEN; + wasm.musigbn256wasmaggregatedpubkey_compute(8, ptr0, len0); + var r0 = getInt32Memory0()[8 / 4 + 0]; + var r1 = getInt32Memory0()[8 / 4 + 1]; + var v1 = getArrayU8FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 1); + return v1; + } +} +module.exports.MusigBN256WasmAggregatedPubkey = MusigBN256WasmAggregatedPubkey; /** */ class MusigBN256WasmSigner { @@ -215,17 +226,16 @@ class MusigBN256WasmVerifier { * @param {Uint8Array} message * @param {Uint8Array} encoded_pubkeys * @param {Uint8Array} encoded_signature - * @param {number} position * @returns {boolean} */ - static verify(message, encoded_pubkeys, encoded_signature, position) { + static verify(message, encoded_pubkeys, encoded_signature) { var ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc); var len0 = WASM_VECTOR_LEN; var ptr1 = passArray8ToWasm0(encoded_pubkeys, wasm.__wbindgen_malloc); var len1 = WASM_VECTOR_LEN; var ptr2 = passArray8ToWasm0(encoded_signature, wasm.__wbindgen_malloc); var len2 = WASM_VECTOR_LEN; - var ret = wasm.musigbn256wasmverifier_verify(ptr0, len0, ptr1, len1, ptr2, len2, position); + var ret = wasm.musigbn256wasmverifier_verify(ptr0, len0, ptr1, len1, ptr2, len2); return ret !== 0; } } diff --git a/musig-bindings/musig-bindings_bg.d.ts b/musig-bindings/musig-bindings_bg.d.ts index a509e6f..b46113c 100644 --- a/musig-bindings/musig-bindings_bg.d.ts +++ b/musig-bindings/musig-bindings_bg.d.ts @@ -1,9 +1,8 @@ /* tslint:disable */ /* eslint-disable */ export const memory: WebAssembly.Memory; -export function __wbg_musigbn256wasmverifier_free(a: number): void; -export function musigbn256wasmverifier_verify(a: number, b: number, c: number, d: number, e: number, f: number, g: number): number; -export function generate_keypair(a: number, b: number, c: number): void; +export function __wbg_musigbn256wasmaggregatedpubkey_free(a: number): void; +export function musigbn256wasmaggregatedpubkey_compute(a: number, b: number, c: number): void; export function __wbg_musigbn256wasmsigner_free(a: number): void; export function musigbn256wasmsigner_new(a: number, b: number, c: number): number; export function musigbn256wasmsigner_compute_precommitment(a: number, b: number, c: number, d: number): void; @@ -11,5 +10,7 @@ export function musigbn256wasmsigner_receive_precommitments(a: number, b: number export function musigbn256wasmsigner_receive_commitments(a: number, b: number, c: number, d: number): void; export function musigbn256wasmsigner_sign(a: number, b: number, c: number, d: number, e: number, f: number): void; export function musigbn256wasmsigner_receive_signature_shares(a: number, b: number, c: number, d: number): void; +export function __wbg_musigbn256wasmverifier_free(a: number): void; +export function musigbn256wasmverifier_verify(a: number, b: number, c: number, d: number, e: number, f: number): number; export function __wbindgen_malloc(a: number): number; export function __wbindgen_free(a: number, b: number): void; diff --git a/musig-bindings/musig-bindings_bg.wasm b/musig-bindings/musig-bindings_bg.wasm index c0e053f..78bb688 100644 Binary files a/musig-bindings/musig-bindings_bg.wasm and b/musig-bindings/musig-bindings_bg.wasm differ diff --git a/typescript-example/test/example.test.ts b/typescript-example/test/example.test.ts index 14930b7..6ec31ba 100644 --- a/typescript-example/test/example.test.ts +++ b/typescript-example/test/example.test.ts @@ -2,7 +2,7 @@ import * as chai from "chai"; const expect = chai.expect; import * as crypto from "crypto"; -import { MusigBN256WasmSigner, MusigBN256WasmVerifier } from "musig-bindings"; +import { MusigBN256WasmSigner, MusigBN256WasmVerifier, MusigBN256WasmAggregatedPubkey } from "musig-bindings"; import { privateKeyFromSeed, private_key_to_pubkey } from "zksync-crypto"; describe("Schnorr-MuSig", function () { @@ -47,6 +47,14 @@ describe("Schnorr-MuSig", function () { } }); + + it("should compute aggregated pubkey from pubkey list", function(){ + let all_pubkeys = merge_array(pubkeys); + + let agg_pubkey = MusigBN256WasmAggregatedPubkey.compute(all_pubkeys); + }) + + it("should compute pre commitments", function () { // each party should compute his own commitment and send hash of it to other parties for (let i = 0; i < number_of_participants; i++) { @@ -92,7 +100,7 @@ describe("Schnorr-MuSig", function () { // loop is redundant let all_pubkeys = merge_array(pubkeys); for (let i = 0; i < number_of_participants; i++) { - let is_valid = MusigBN256WasmVerifier.verify(message, all_pubkeys, aggregated_signatures[i], i); + let is_valid = MusigBN256WasmVerifier.verify(message, all_pubkeys, aggregated_signatures[i]); expect(is_valid).eq(true); } });