Skip to content

Commit

Permalink
Add Rust FFI for Musig2 module
Browse files Browse the repository at this point in the history
  • Loading branch information
jlest01 committed Jul 31, 2024
1 parent 685112b commit 78e2627
Showing 1 changed file with 297 additions and 0 deletions.
297 changes: 297 additions & 0 deletions secp256k1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,147 @@ extern "C" {
internal_pubkey: *const XOnlyPublicKey,
tweak32: *const c_uchar,
) -> c_int;

#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_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_10_0_ec_pubkey_sort")]
pub fn secp256k1_ec_pubkey_sort(
ctx: *const Context,
pubkeys: *const *const PublicKey,
n_pubkeys: size_t
) -> c_int;
}

/// A reimplementation of the C function `secp256k1_context_create` in rust.
Expand Down Expand Up @@ -1084,6 +1225,162 @@ impl <T: CPtr> CPtr for Option<T> {
}
}

pub const MUSIG_KEYAGG_LEN: usize = 197;
pub const MUSIG_SECNONCE_LEN: usize = 132;
pub const MUSIG_PUBNONCE_LEN: usize = 132;
pub const MUSIG_AGGNONCE_LEN: usize = 132;
pub const MUSIG_AGGNONCE_SERIALIZED_LEN: usize = 66;
pub const MUSIG_PUBNONCE_SERIALIZED_LEN: usize = 66;
pub const MUSIG_SESSION_LEN: usize = 133;
pub const MUSIG_PART_SIG_LEN: usize = 36;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_LEN]);
impl_array_newtype!(MusigKeyAggCache, c_uchar, MUSIG_KEYAGG_LEN);
impl_raw_debug!(MusigKeyAggCache);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigKeyAggCache {
fn eq(&self, other: &Self) -> bool {
self.0[..] == other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigKeyAggCache {}

impl MusigKeyAggCache {
pub fn new() -> Self {
MusigKeyAggCache([0; MUSIG_KEYAGG_LEN])
}
}


#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigSecNonce(pub(crate) [c_uchar; MUSIG_SECNONCE_LEN]);
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_LEN);
impl_raw_debug!(MusigSecNonce);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigSecNonce {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigSecNonce {}

impl MusigSecNonce {
pub fn new() -> Self {
MusigSecNonce([0; MUSIG_SECNONCE_LEN])
}

/// Don't use this. Refer to the documentation of wrapper APIs in the crate.
//
// No need for strong warning here, the user cannot use the ffi types directly in any of the APIs
pub fn dangerous_from_bytes(bytes: [c_uchar; MUSIG_SECNONCE_LEN]) -> Self {
MusigSecNonce(bytes)
}

/// Don't use this. Refer to the documentation of wrapper APIs in the crate.
//
// No need for strong warning here, the user cannot use the ffi types directly in any of the high level APIs
pub fn dangerous_into_bytes(self) -> [c_uchar; MUSIG_SECNONCE_LEN] {
self.0
}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]);
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_LEN);
impl_raw_debug!(MusigPubNonce);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigPubNonce {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigPubNonce {}

impl MusigPubNonce {
pub fn new() -> Self {
MusigPubNonce([0; MUSIG_PUBNONCE_LEN])
}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]);
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_LEN);
impl_raw_debug!(MusigAggNonce);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigAggNonce {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigAggNonce {}

impl MusigAggNonce {
pub fn new() -> Self {
MusigAggNonce([0; MUSIG_AGGNONCE_LEN])
}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]);
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_LEN);
impl_raw_debug!(MusigSession);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigSession {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigSession {}

impl MusigSession {
pub fn new() -> Self {
MusigSession([0; MUSIG_SESSION_LEN])
}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]);
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_LEN);
impl_raw_debug!(MusigPartialSignature);

#[cfg(not(secp256k1_fuzz))]
impl PartialEq for MusigPartialSignature {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(secp256k1_fuzz))]
impl Eq for MusigPartialSignature {}

impl MusigPartialSignature {
pub fn new() -> Self {
MusigPartialSignature([0; MUSIG_PART_SIG_LEN])
}
}

#[cfg(secp256k1_fuzz)]
mod fuzz_dummy {
use super::*;
Expand Down

0 comments on commit 78e2627

Please sign in to comment.