Skip to content

Commit

Permalink
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
Browse files Browse the repository at this point in the history
  • Loading branch information
GeneFerneau authored and sanket1729 committed Mar 21, 2022
1 parent c61a982 commit 865e8ca
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 1 deletion.
3 changes: 3 additions & 0 deletions secp256k1-zkp-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ fn main() {
.define("ENABLE_MODULE_RANGEPROOF", Some("1"))
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
.define("ENABLE_MODULE_WHITELIST", Some("1"))
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
.define("ENABLE_MODULE_MUSIG", Some("1"))
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
.define("ECMULT_GEN_PREC_BITS", Some("4"))
// TODO these three should be changed to use libgmp, at least until secp PR 290 is merged
.define("USE_NUM_NONE", Some("1"))
Expand Down
291 changes: 290 additions & 1 deletion secp256k1-zkp-sys/src/zkp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::{fmt, hash};
use {types::*, Context, NonceFn, PublicKey, Signature};
use {types::*, Context, KeyPair, NonceFn, PublicKey, Signature, XOnlyPublicKey};

/// Rangeproof maximum length
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
Expand Down Expand Up @@ -349,6 +349,19 @@ extern "C" {
input_len: size_t,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubkey_agg"
)]
pub fn secp256k1_musig_pubkey_agg(
cx: *const Context,
scratch: *mut ScratchSpace,
combined_pk: *mut XOnlyPublicKey,
pre_session: *mut MusigKeyaggCache,
pubkeys: *const *const XOnlyPublicKey,
n_pubkeys: size_t,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_signature_serialize"
Expand All @@ -360,6 +373,29 @@ extern "C" {
sig: *const WhitelistSignature,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubkey_xonly_tweak_add"
)]
pub fn secp256k1_musig_pubkey_xonly_tweak_add(
cx: *const Context,
output_pubkey: *mut XOnlyPublicKey,
keyagg_cache: *mut MusigKeyaggCache,
tweak32: *const c_uchar,
) -> c_int;


#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_sign"
Expand All @@ -378,6 +414,21 @@ extern "C" {
noncedata: *mut c_void,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_gen"
)]
pub fn secp256k1_musig_nonce_gen(
cx: *const Context,
secnonce: *mut MusigSecNonce,
pubnonce: *mut MusigPubNonce,
session_id32: *const c_uchar,
seckey: *const c_uchar,
msg32: *const c_uchar,
keyagg_cache: *const MusigKeyaggCache,
extra_input32: *const c_uchar,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_verify"
Expand All @@ -390,6 +441,162 @@ extern "C" {
n_keys: size_t,
sub_pubkey: *const PublicKey,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_agg"
)]
pub fn secp256k1_musig_nonce_agg(
cx: *const Context,
aggnonce: *const MusigAggNonce,
pubnonces: *const *const MusigPubNonce,
n_pubnonces: size_t,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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,
adaptor: *const PublicKey,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubnonce_serialize"
)]
pub fn secp256k1_musig_pubnonce_serialize(
cx: *const Context,
out32: *mut c_uchar,
nonce: *const MusigPubNonce,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubnonce_parse"
)]
pub fn secp256k1_musig_pubnonce_parse(
cx: *const Context,
nonce: *mut MusigPubNonce,
in32: *const c_uchar,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_aggnonce_serialize"
)]
pub fn secp256k1_musig_aggnonce_serialize(
cx: *const Context,
out32: *mut c_uchar,
nonce: *const MusigAggNonce,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_aggnonce_parse"
)]
pub fn secp256k1_musig_aggnonce_parse(
cx: *const Context,
nonce: *mut MusigAggNonce,
in32: *const c_uchar,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sig_verify"
)]
pub fn secp256k1_musig_partial_sig_verify(
cx: *const Context,
partial_sig: *const MusigPartialSignature,
pubnonce: *const MusigPubNonce,
pubkey: *const XOnlyPublicKey,
keyagg_cache: *const MusigKeyaggCache,
session: *const MusigSession,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_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(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_parity"
)]
pub fn secp256k1_musig_nonce_parity(
cx: *const Context,
nonce_parity: *mut c_int,
session: *mut MusigSession,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_adapt"
)]
pub fn secp256k1_musig_adapt(
cx: *const Context,
sig64: *mut c_uchar,
pre_sig64: *const c_uchar,
sec_adaptor32: *const c_uchar,
nonce_parity: c_int,
) -> c_int;

#[cfg_attr(
not(feature = "external-symbols"),
link_name = "rustsecp256k1zkp_v0_6_0_musig_extract_adaptor"
)]
pub fn secp256k1_musig_extract_adaptor(
cx: *const Context,
sec_adaptor32: *mut c_uchar,
sig64: *const c_uchar,
pre_sig64: *const c_uchar,
nonce_parity: c_int,
) -> c_int;
}

#[repr(C)]
Expand Down Expand Up @@ -587,3 +794,85 @@ impl EcdsaAdaptorSignature {
&self.0
}
}

#[repr(C)]
pub struct ScratchSpace(c_int);

pub const MUSIG_KEYAGG_LEN: usize = 165;
pub const MUSIG_SECNONCE_LEN: usize = 68;
pub const MUSIG_PUBNONCE_LEN: usize = 132;
pub const MUSIG_AGGNONCE_LEN: usize = 132;
pub const MUSIG_SESSION_LEN: usize = 133;
pub const MUSIG_PART_SIG_LEN: usize = 36;

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigKeyaggCache {
pub data: [c_uchar; MUSIG_KEYAGG_LEN],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigSecNonce {
pub data: [c_uchar; MUSIG_SECNONCE_LEN],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigPubNonce {
pub data: [c_uchar; MUSIG_PUBNONCE_LEN],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigAggNonce {
pub data: [c_uchar; MUSIG_AGGNONCE_LEN],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigSession {
pub data: [c_uchar; MUSIG_SESSION_LEN],
}

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

#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MusigPartialSignature {
pub data: [c_uchar; MUSIG_PART_SIG_LEN],
}

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

0 comments on commit 865e8ca

Please sign in to comment.