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 Musig2 module #716

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ required-features = ["hashes", "std"]
name = "generate_keys"
required-features = ["rand", "std"]

[[example]]
name = "musig"
required-features = ["rand", "std"]

[workspace]
members = ["secp256k1-sys"]
exclude = ["no_std_test"]
103 changes: 103 additions & 0 deletions examples/musig.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
extern crate secp256k1;

use secp256k1::musig::{
new_musig_nonce_pair, MusigAggNonce, MusigKeyAggCache, MusigPartialSignature, MusigPubNonce,
MusigSession, MusigSessionId,
};
use secp256k1::{Keypair, Message, PublicKey, Scalar, Secp256k1, SecretKey};

fn main() {
let secp = Secp256k1::new();
let mut rng = rand::thread_rng();

let (seckey1, pubkey1) = secp.generate_keypair(&mut rng);

let seckey2 = SecretKey::new(&mut rng);
let pubkey2 = PublicKey::from_secret_key(&secp, &seckey2);

let pubkeys = [pubkey1, pubkey2];
let pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect();
let pubkeys_ref = pubkeys_ref.as_slice();

let mut musig_key_agg_cache = MusigKeyAggCache::new(&secp, pubkeys_ref);

let plain_tweak: [u8; 32] = *b"this could be a BIP32 tweak....\0";
let xonly_tweak: [u8; 32] = *b"this could be a Taproot tweak..\0";

let plain_tweak = Scalar::from_be_bytes(plain_tweak).unwrap();
musig_key_agg_cache.pubkey_ec_tweak_add(&secp, &plain_tweak).unwrap();

let xonly_tweak = Scalar::from_be_bytes(xonly_tweak).unwrap();
let tweaked_agg_pk = musig_key_agg_cache.pubkey_xonly_tweak_add(&secp, &xonly_tweak).unwrap();

let agg_pk = musig_key_agg_cache.agg_pk();

assert_eq!(agg_pk, tweaked_agg_pk.x_only_public_key().0);

let msg_bytes: [u8; 32] = *b"this_could_be_the_hash_of_a_msg!";
let msg = Message::from_digest_slice(&msg_bytes).unwrap();

let musig_session_id1 = MusigSessionId::new(&mut rng);

let nonce_pair1 = new_musig_nonce_pair(
&secp,
musig_session_id1,
Some(&musig_key_agg_cache),
Some(seckey1),
pubkey1,
Some(msg),
None,
)
.unwrap();

let musig_session_id2 = MusigSessionId::new(&mut rng);

let nonce_pair2 = new_musig_nonce_pair(
&secp,
musig_session_id2,
Some(&musig_key_agg_cache),
Some(seckey2),
pubkey2,
Some(msg),
None,
)
.unwrap();

let sec_nonce1 = nonce_pair1.0;
let pub_nonce1 = nonce_pair1.1;

let sec_nonce2 = nonce_pair2.0;
let pub_nonce2 = nonce_pair2.1;

let nonces = [pub_nonce1, pub_nonce2];
let nonces_ref: Vec<&MusigPubNonce> = nonces.iter().collect();
let nonces_ref = nonces_ref.as_slice();

let agg_nonce = MusigAggNonce::new(&secp, nonces_ref);

let session = MusigSession::new(&secp, &musig_key_agg_cache, agg_nonce, msg);

let keypair1 = Keypair::from_secret_key(&secp, &seckey1);
let partial_sign1 =
session.partial_sign(&secp, sec_nonce1, &keypair1, &musig_key_agg_cache).unwrap();

let keypair2 = Keypair::from_secret_key(&secp, &seckey2);
let partial_sign2 =
session.partial_sign(&secp, sec_nonce2, &keypair2, &musig_key_agg_cache).unwrap();

let is_partial_signature_valid =
session.partial_verify(&secp, &musig_key_agg_cache, partial_sign1, pub_nonce1, pubkey1);
assert!(is_partial_signature_valid);

let is_partial_signature_valid =
session.partial_verify(&secp, &musig_key_agg_cache, partial_sign2, pub_nonce2, pubkey2);
assert!(is_partial_signature_valid);

let partial_sigs = [partial_sign1, partial_sign2];
let partial_sigs_ref: Vec<&MusigPartialSignature> = partial_sigs.iter().collect();
let partial_sigs_ref = partial_sigs_ref.as_slice();

let sig64 = session.partial_sig_agg(partial_sigs_ref);

assert!(secp.verify_schnorr(&sig64, &msg_bytes, &agg_pk).is_ok());
}
1 change: 1 addition & 0 deletions secp256k1-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn main() {
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
.define("ENABLE_MODULE_ELLSWIFT", Some("1"))
.define("ENABLE_MODULE_MUSIG", Some("1"))
// upstream sometimes introduces calls to printf, which we cannot compile
// with WASM due to its lack of libc. printf is never necessary and we can
// just #define it away.
Expand Down
2 changes: 1 addition & 1 deletion secp256k1-sys/depend/secp256k1-HEAD-revision.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# This file was automatically created by vendor-libsecp.sh
1ad5185cd42c0636104129fcc9f6a4bf9c67cc40
398051cba6052c77f801b0aa57f1b0f161b0b12d
7 changes: 5 additions & 2 deletions secp256k1-sys/depend/secp256k1/.cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ env:
MAKEFLAGS: -j4
BUILD: check
### secp256k1 config
ECMULTWINDOW: auto
ECMULTGENPRECISION: auto
ECMULTWINDOW: 15
ECMULTGENKB: 22
ASM: no
WIDEMUL: auto
WITH_VALGRIND: yes
Expand All @@ -21,6 +21,7 @@ env:
ECDH: no
RECOVERY: no
SCHNORRSIG: no
MUSIG: no
ELLSWIFT: no
### test options
SECP256K1_TEST_ITERS:
Expand Down Expand Up @@ -67,6 +68,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
MUSIG: yes
ELLSWIFT: yes
matrix:
# Currently only gcc-snapshot, the other compilers are tested on GHA with QEMU
Expand All @@ -83,6 +85,7 @@ task:
ECDH: yes
RECOVERY: yes
SCHNORRSIG: yes
MUSIG: yes
ELLSWIFT: yes
WRAPPER_CMD: 'valgrind --error-exitcode=42'
SECP256K1_TEST_ITERS: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ runs:
cat valgrind_fingerprint
shell: bash

- uses: actions/cache@v3
- uses: actions/cache@v4
id: cache
with:
path: ${{ env.CI_HOMEBREW_CELLAR_VALGRIND }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ runs:
load: true
cache-from: type=gha

- # Workaround for https://github.com/google/sanitizers/issues/1614 .
# The underlying issue has been fixed in clang 18.1.3.
run: sudo sysctl -w vm.mmap_rnd_bits=28
shell: bash

- # Tell Docker to pass environment variables in `env` into the container.
run: >
docker run \
Expand Down
Loading
Loading