-
Notifications
You must be signed in to change notification settings - Fork 286
Add Musig2 module #716
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
base: master
Are you sure you want to change the base?
Add Musig2 module #716
Changes from all commits
a21d6ca
f384a2d
e90015e
dd26c30
2f0f9d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
extern crate secp256k1; | ||
|
||
use secp256k1::musig::{ | ||
new_nonce_pair, AggregatedNonce, KeyAggCache, PartialSignature, PublicNonce, | ||
Session, SessionSecretRand, | ||
}; | ||
use secp256k1::{Keypair, Message, PublicKey, Scalar, Secp256k1, SecretKey, pubkey_sort}; | ||
|
||
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 mut pubkeys_ref: Vec<&PublicKey> = pubkeys.iter().collect(); | ||
let pubkeys_ref = pubkeys_ref.as_mut_slice(); | ||
|
||
pubkey_sort(&secp, pubkeys_ref); | ||
|
||
let mut musig_key_agg_cache = KeyAggCache::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_sec_rand1 = SessionSecretRand::from_rng(&mut rng); | ||
|
||
let nonce_pair1 = new_nonce_pair( | ||
&secp, | ||
musig_session_sec_rand1, | ||
Some(&musig_key_agg_cache), | ||
Some(seckey1), | ||
pubkey1, | ||
Some(msg), | ||
None, | ||
); | ||
|
||
let musig_session_sec_rand2 = SessionSecretRand::from_rng(&mut rng); | ||
|
||
let nonce_pair2 = new_nonce_pair( | ||
&secp, | ||
musig_session_sec_rand2, | ||
Some(&musig_key_agg_cache), | ||
Some(seckey2), | ||
pubkey2, | ||
Some(msg), | ||
None, | ||
); | ||
|
||
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<&PublicNonce> = nonces.iter().collect(); | ||
let nonces_ref = nonces_ref.as_slice(); | ||
|
||
let agg_nonce = AggregatedNonce::new(&secp, nonces_ref); | ||
|
||
let session = Session::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); | ||
|
||
let keypair2 = Keypair::from_secret_key(&secp, &seckey2); | ||
let partial_sign2 = | ||
session.partial_sign(&secp, sec_nonce2, &keypair2, &musig_key_agg_cache); | ||
|
||
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<&PartialSignature> = partial_sigs.iter().collect(); | ||
let partial_sigs_ref = partial_sigs_ref.as_slice(); | ||
|
||
let aggregated_signature = session.partial_sig_agg(partial_sigs_ref); | ||
|
||
assert!(aggregated_signature.verify(&secp, &agg_pk, &msg_bytes).is_ok()); | ||
} | ||
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -672,6 +672,147 @@ extern "C" { | |||||||
hashfp: EllswiftEcdhHashFn, | ||||||||
data: *mut c_void) | ||||||||
-> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_pubnonce_parse")] | ||||||||
pub fn secp256k1_musig_pubnonce_parse( | ||||||||
cx: *const Context, | ||||||||
nonce: *mut MusigPubNonce, | ||||||||
in66: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_pubnonce_serialize")] | ||||||||
pub fn secp256k1_musig_pubnonce_serialize( | ||||||||
cx: *const Context, | ||||||||
out66: *mut c_uchar, | ||||||||
nonce: *const MusigPubNonce, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_aggnonce_parse")] | ||||||||
pub fn secp256k1_musig_aggnonce_parse( | ||||||||
cx: *const Context, | ||||||||
nonce: *mut MusigAggNonce, | ||||||||
in66: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_aggnonce_serialize")] | ||||||||
pub fn secp256k1_musig_aggnonce_serialize( | ||||||||
cx: *const Context, | ||||||||
out66: *mut c_uchar, | ||||||||
nonce: *const MusigAggNonce, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_partial_sig_parse")] | ||||||||
pub fn secp256k1_musig_partial_sig_parse( | ||||||||
cx: *const Context, | ||||||||
sig: *mut MusigPartialSignature, | ||||||||
in32: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_partial_sig_serialize")] | ||||||||
pub fn secp256k1_musig_partial_sig_serialize( | ||||||||
cx: *const Context, | ||||||||
out32: *mut c_uchar, | ||||||||
sig: *const MusigPartialSignature, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_pubkey_agg")] | ||||||||
pub fn secp256k1_musig_pubkey_agg( | ||||||||
cx: *const Context, | ||||||||
agg_pk: *mut XOnlyPublicKey, | ||||||||
keyagg_cache: *mut MusigKeyAggCache, | ||||||||
pubkeys: *const *const PublicKey, | ||||||||
n_pubkeys: size_t, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming),link_name = "rustsecp256k1_v0_11_musig_pubkey_get")] | ||||||||
pub fn secp256k1_musig_pubkey_get( | ||||||||
cx: *const Context, | ||||||||
agg_pk: *mut PublicKey, | ||||||||
keyagg_cache: *const MusigKeyAggCache, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_pubkey_ec_tweak_add")] | ||||||||
pub fn secp256k1_musig_pubkey_ec_tweak_add( | ||||||||
cx: *const Context, | ||||||||
output_pubkey: *mut PublicKey, | ||||||||
keyagg_cache: *mut MusigKeyAggCache, | ||||||||
tweak32: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_pubkey_xonly_tweak_add")] | ||||||||
pub fn secp256k1_musig_pubkey_xonly_tweak_add( | ||||||||
cx: *const Context, | ||||||||
output_pubkey: *mut PublicKey, | ||||||||
keyagg_cache: *mut MusigKeyAggCache, | ||||||||
tweak32: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_nonce_gen")] | ||||||||
pub fn secp256k1_musig_nonce_gen( | ||||||||
cx: *const Context, | ||||||||
secnonce: *mut MusigSecNonce, | ||||||||
pubnonce: *mut MusigPubNonce, | ||||||||
session_secrand32: *const c_uchar, | ||||||||
seckey: *const c_uchar, | ||||||||
pubkey: *const PublicKey, | ||||||||
msg32: *const c_uchar, | ||||||||
keyagg_cache: *const MusigKeyAggCache, | ||||||||
extra_input32: *const c_uchar, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_nonce_agg")] | ||||||||
pub fn secp256k1_musig_nonce_agg( | ||||||||
cx: *const Context, | ||||||||
aggnonce: *mut MusigAggNonce, | ||||||||
pubnonces: *const *const MusigPubNonce, | ||||||||
n_pubnonces: size_t, | ||||||||
) -> c_int; | ||||||||
|
||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_nonce_process")] | ||||||||
pub fn secp256k1_musig_nonce_process( | ||||||||
cx: *const Context, | ||||||||
session: *mut MusigSession, | ||||||||
aggnonce: *const MusigAggNonce, | ||||||||
msg32: *const c_uchar, | ||||||||
keyagg_cache: *const MusigKeyAggCache, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_partial_sign")] | ||||||||
pub fn secp256k1_musig_partial_sign( | ||||||||
cx: *const Context, | ||||||||
partial_sig: *mut MusigPartialSignature, | ||||||||
secnonce: *mut MusigSecNonce, | ||||||||
keypair: *const Keypair, | ||||||||
keyagg_cache: *const MusigKeyAggCache, | ||||||||
session: *const MusigSession, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_partial_sig_verify")] | ||||||||
pub fn secp256k1_musig_partial_sig_verify( | ||||||||
cx: *const Context, | ||||||||
partial_sig: *const MusigPartialSignature, | ||||||||
pubnonce: *const MusigPubNonce, | ||||||||
pubkey: *const PublicKey, | ||||||||
keyagg_cache: *const MusigKeyAggCache, | ||||||||
session: *const MusigSession, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_musig_partial_sig_agg")] | ||||||||
pub fn secp256k1_musig_partial_sig_agg( | ||||||||
cx: *const Context, | ||||||||
sig64: *mut c_uchar, | ||||||||
session: *const MusigSession, | ||||||||
partial_sigs: *const *const MusigPartialSignature, | ||||||||
n_sigs: size_t, | ||||||||
) -> c_int; | ||||||||
|
||||||||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_11_ec_pubkey_sort")] | ||||||||
pub fn secp256k1_ec_pubkey_sort( | ||||||||
ctx: *const Context, | ||||||||
pubkeys: *mut *const PublicKey, | ||||||||
n_pubkeys: size_t | ||||||||
) -> c_int; | ||||||||
} | ||||||||
|
||||||||
#[cfg(not(secp256k1_fuzz))] | ||||||||
|
@@ -1097,6 +1238,94 @@ impl <T: CPtr> CPtr for Option<T> { | |||||||
} | ||||||||
} | ||||||||
} | ||||||||
/// Total length (in bytes) of the key aggregation context. | ||||||||
/// This structure packs all metadata needed for aggregating individual public keys | ||||||||
/// into a single MuSig2 aggregated key (including coefficients and any extra metadata). | ||||||||
pub const MUSIG_KEYAGG_LEN: usize = 197; | ||||||||
|
||||||||
/// Length (in bytes) of the secret nonce structure used in a MuSig2 session. | ||||||||
/// It holds the secret (ephemeral) nonces used internally for nonce derivation | ||||||||
/// before the corresponding public nonces are computed. | ||||||||
pub const MUSIG_SECNONCE_LEN: usize = 132; | ||||||||
|
||||||||
/// Length (in bytes) of the public nonce structure. | ||||||||
/// This is derived from the secret nonce and shared among participants to build | ||||||||
/// nonce commitments in the MuSig2 protocol. | ||||||||
pub const MUSIG_PUBNONCE_LEN: usize = 132; | ||||||||
|
||||||||
/// Length (in bytes) of the aggregated nonce structure. | ||||||||
/// Represents the combined nonce obtained by aggregating the individual public nonces | ||||||||
/// from all participants for the final signature computation. | ||||||||
pub const MUSIG_AGGNONCE_LEN: usize = 132; | ||||||||
|
||||||||
/// Serialized length (in bytes) of the aggregated nonce. | ||||||||
/// This is the compact form (typically using a compressed representation) used for | ||||||||
/// transmitting or storing the aggregated nonce. | ||||||||
pub const MUSIG_AGGNONCE_SERIALIZED_LEN: usize = 66; | ||||||||
|
||||||||
/// Serialized length (in bytes) of an individual public nonce. | ||||||||
/// This compact serialized form is what gets exchanged between signers. | ||||||||
pub const MUSIG_PUBNONCE_SERIALIZED_LEN: usize = 66; | ||||||||
|
||||||||
/// Length (in bytes) of the session structure. | ||||||||
/// The session object holds all state needed for a MuSig2 signing session (e.g. aggregated nonce, | ||||||||
/// key aggregation info, and other state necessary for computing partial signatures). | ||||||||
pub const MUSIG_SESSION_LEN: usize = 133; | ||||||||
|
||||||||
/// Length (in bytes) of the internal representation of a partial signature. | ||||||||
/// This structure include magic bytes ([0xeb, 0xfb, 0x1a, 0x32]) alongside the actual signature scalar. | ||||||||
pub const MUSIG_PART_SIG_LEN: usize = 36; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
FTR, I previously suggested changing MUSIG_PART_SIG_LEN to 32 but that is wrong. It really holds 36 bytes in memory, however, it is 32 bytes in serialized form. Therefore need for another constant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments on the constants explaining this would be great too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Fixed the serialized size and also added comments explaining the constants. |
||||||||
|
||||||||
/// Serialized length (in bytes) of a partial signature. | ||||||||
/// This is the compact form (typically just the 32-byte scalar) that is used when communicating | ||||||||
/// partial signatures to be combined into the final signature. | ||||||||
pub const MUSIG_PART_SIG_SERIALIZED_LEN: usize = 32; | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||||||||
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_LEN]); | ||||||||
impl_array_newtype!(MusigKeyAggCache, c_uchar, MUSIG_KEYAGG_LEN); | ||||||||
impl_raw_debug!(MusigKeyAggCache); | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq)] | ||||||||
pub struct MusigSecNonce([c_uchar; MUSIG_SECNONCE_LEN]); | ||||||||
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_LEN); | ||||||||
impl_raw_debug!(MusigSecNonce); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This leaks the nonce. We need to hide it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Done in 2ea5674 |
||||||||
|
||||||||
impl MusigSecNonce { | ||||||||
pub fn dangerous_from_bytes(bytes: [c_uchar; MUSIG_SECNONCE_LEN]) -> Self { | ||||||||
MusigSecNonce(bytes) | ||||||||
} | ||||||||
|
||||||||
pub fn dangerous_into_bytes(self) -> [c_uchar; MUSIG_SECNONCE_LEN] { | ||||||||
self.0 | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||||||||
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]); | ||||||||
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_LEN); | ||||||||
impl_raw_debug!(MusigPubNonce); | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||||||||
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]); | ||||||||
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_LEN); | ||||||||
impl_raw_debug!(MusigAggNonce); | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||||||||
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]); | ||||||||
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_LEN); | ||||||||
impl_raw_debug!(MusigSession); | ||||||||
|
||||||||
#[repr(C)] | ||||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||||||||
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FTR these struct declarations looked wrong but are indeed correct based on the current API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think they should be changed? |
||||||||
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_LEN); | ||||||||
impl_raw_debug!(MusigPartialSignature); | ||||||||
|
||||||||
#[cfg(secp256k1_fuzz)] | ||||||||
mod fuzz_dummy { | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way this example is written makes it difficult to understand how one would use the API in a real-world application where the signers are not in a single binary but distributed and need to exchange messages. I suggest restructuring the code such that operations of each signer are separated. Maybe it'd be even better to use threads and channels to simulate message transmission unless it makes the code too complicated (I think it shouldn't since it should be just a few additional lines.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Looking into this.