Skip to content

Commit

Permalink
add Musig related bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Dec 6, 2023
1 parent bcd5456 commit 9ba3810
Show file tree
Hide file tree
Showing 12 changed files with 892 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/*
.vscode/
.nyc_output
build
node_modules
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM emscripten/emsdk:latest
FROM emscripten/emsdk:3.1.40

RUN apt-get update
RUN apt-get install dh-autoreconf -y
Expand Down
4 changes: 2 additions & 2 deletions scripts/build_wasm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi
OPTIMIZATION_LEVEL=s
# C functions to export to Javascript
EXPORTED_RUNTIME_METHODS="['getValue', 'setValue', 'ccall']"
EXPORTED_FUNCTIONS="['_secp256k1_ecmult_gen_prec_table', '_secp256k1_pre_g', '_free', '_malloc', '_ecdh', '_generator_generate', '_generator_generate_blinded', '_pedersen_blind_generator_blind_sum', '_pedersen_commitment', '_rangeproof_sign', '_rangeproof_info', '_rangeproof_verify', '_rangeproof_rewind', '_surjectionproof_initialize', '_surjectionproof_generate', '_surjectionproof_verify', '_ec_seckey_negate', '_ec_seckey_tweak_add', '_ec_seckey_tweak_sub', '_ec_seckey_tweak_mul', '_ec_is_point', '_ec_point_compress', '_ec_point_from_scalar', '_ec_x_only_point_tweak_add', '_ec_sign_ecdsa', '_ec_verify_ecdsa', '_ec_sign_schnorr', '_ec_verify_schnorr', '_ec_seckey_verify', '_ec_point_add_scalar']"
EXPORTED_FUNCTIONS="['_secp256k1_ecmult_gen_prec_table', '_secp256k1_pre_g', '_free', '_malloc', '_ecdh', '_generator_generate', '_generator_generate_blinded', '_pedersen_blind_generator_blind_sum', '_pedersen_commitment', '_rangeproof_sign', '_rangeproof_info', '_rangeproof_verify', '_rangeproof_rewind', '_surjectionproof_initialize', '_surjectionproof_generate', '_surjectionproof_verify', '_ec_seckey_negate', '_ec_seckey_tweak_add', '_ec_seckey_tweak_sub', '_ec_seckey_tweak_mul', '_ec_is_point', '_ec_point_compress', '_ec_point_from_scalar', '_ec_x_only_point_tweak_add', '_ec_sign_ecdsa', '_ec_verify_ecdsa', '_ec_sign_schnorr', '_ec_verify_schnorr', '_ec_seckey_verify', '_ec_point_add_scalar', '_musig_pubkey_agg', '_musig_nonce_gen', '_musig_nonce_agg', '_musig_nonce_process', '_musig_partial_sign', '_musig_partial_sig_verify', '_musig_partial_sig_agg', '_musig_pubkey_xonly_tweak_add']"

SECP256K1_SOURCE_DIR=secp256k1-zkp

Expand All @@ -22,7 +22,7 @@ cd ${SECP256K1_SOURCE_DIR}
./autogen.sh

# Compile secp256k1 to bitcode
emconfigure ./configure --enable-tests=no --enable-exhaustive-tests=no --enable-benchmark=no --enable-module-rangeproof=yes --enable-module-surjectionproof=yes --enable-experimental=yes --enable-module-generator=yes --enable-module-schnorrsig=yes --enable-module-extrakeys=yes --enable-module-ecdh=yes
emconfigure ./configure --enable-tests=no --enable-exhaustive-tests=no --enable-benchmark=no --enable-module-rangeproof=yes --enable-module-surjectionproof=yes --enable-experimental=yes --enable-module-generator=yes --enable-module-schnorrsig=yes --enable-module-extrakeys=yes --enable-module-ecdh=yes --enable-module-musig=yes
emmake make -j $num_jobs

# go back to the root folder
Expand Down
4 changes: 2 additions & 2 deletions scripts/compile_wasm_docker
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Build the container
docker build -t secp256k1-wasm .
# Run the container
docker run --name linux-build -d -i secp256k1-wasm
docker run --name linux-build --entrypoint=sh -d -i secp256k1-wasm

# Copy the secp256k1 folder inside the container
docker cp ./secp256k1-zkp/. linux-build:/build/secp256k1-zkp
Expand All @@ -19,6 +19,6 @@ docker exec linux-build bash build_wasm
rm -rf src/lib/secp256k1-zkp.js
docker cp linux-build:/build/dist/secp256k1-zkp.js ./src/lib

docker stop linux-build
docker kill linux-build
docker rm linux-build
#docker rmi secp256k1-wasm
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ecc } from './ecc';
import { ecdh } from './ecdh';
import { generator } from './generator';
import { Secp256k1ZKP } from './interface';
import { musig } from './musig';
import { pedersen } from './pedersen';
import { rangeproof } from './rangeproof';
import { surjectionproof } from './surjectionproof';
Expand All @@ -12,6 +13,7 @@ export const secp256k1Function = async (): Promise<Secp256k1ZKP> => {
return {
ecdh: ecdh(cModule),
ecc: ecc(cModule),
musig: musig(cModule),
pedersen: pedersen(cModule),
generator: generator(cModule),
rangeproof: rangeproof(cModule),
Expand Down
40 changes: 40 additions & 0 deletions src/lib/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,49 @@ export interface SurjectionProof {
) => boolean;
}

export interface Musig {
pubkeyAgg(pubkeys: Array<Uint8Array>): {
aggPubkey: Uint8Array;
keyaggCache: Uint8Array;
};
nonceGen(sessionId: Uint8Array): {
pubNonce: Uint8Array;
secNonce: Uint8Array;
};
nonceAgg(pubNonces: Array<Uint8Array>): Uint8Array;
nonceProcess(
nonceAgg: Uint8Array,
msg: Uint8Array,
keyaggCache: Uint8Array
): Uint8Array;
partialSign(
secNonce: Uint8Array,
secKey: Uint8Array,
keyaggCache: Uint8Array,
session: Uint8Array
): Uint8Array;
partialVerify(
partialSig: Uint8Array,
pubNonce: Uint8Array,
pubKey: Uint8Array,
keyaggCache: Uint8Array,
session: Uint8Array
): boolean;
partialSigAgg(
session: Uint8Array,
partialSigs: Array<Uint8Array>
): Uint8Array;
pubkeyXonlyTweakAdd(
keyaggCache: Uint8Array,
tweak: Uint8Array,
compress?: boolean
): Uint8Array;
}

export interface Secp256k1ZKP {
ecdh: Ecdh;
ecc: Ecc;
musig: Musig;
surjectionproof: SurjectionProof;
rangeproof: RangeProof;
pedersen: Pedersen;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Long from 'long';
import { CModule } from './cmodule';

interface MemoryI {
charStarToUint8(ptr: number, size: number): Uint8Array;
malloc(size: number): number;
charStar(buffer: Uint8Array): number;
charStarArray(buffers: Uint8Array[]): number;
Expand All @@ -15,6 +16,10 @@ export default class Memory implements MemoryI {

constructor(private cModule: CModule) {}

charStarToUint8(ptr: number, size: number): Uint8Array {
return new Uint8Array(this.cModule.HEAPU8.subarray(ptr, ptr + size));
}

malloc(size: number): number {
const ret = this.cModule._malloc(size);
this.toFree.push(ret);
Expand Down
Loading

0 comments on commit 9ba3810

Please sign in to comment.