Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MuSig related bindings #46

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 5 additions & 3 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 All @@ -40,6 +40,8 @@ emcc -O$OPTIMIZATION_LEVEL \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-I${SECP256K1_SOURCE_DIR}/include \
${SECP256K1_SOURCE_DIR}/src/*.o \
${SECP256K1_SOURCE_DIR}/src/libsecp256k1_la-secp256k1.o \
${SECP256K1_SOURCE_DIR}/src/libsecp256k1_precomputed_la-precomputed_ecmult.o \
${SECP256K1_SOURCE_DIR}/src/libsecp256k1_precomputed_la-precomputed_ecmult_gen.o \
./main.c \
-o ./dist/secp256k1-zkp.js
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: 1 addition & 1 deletion secp256k1-zkp
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
46 changes: 46 additions & 0 deletions src/lib/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,55 @@ export interface SurjectionProof {
) => boolean;
}

export interface Musig {
pubkeyAgg(pubKeys: Array<Uint8Array>): {
aggPubkey: Uint8Array;
keyaggCache: Uint8Array;
};
nonceGen(
sessionId: Uint8Array,
pubKey: 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
): {
pubkey: Uint8Array;
keyaggCache: 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
Loading