Skip to content

Commit 047b85d

Browse files
committed
Add Rust FFI for Musig2 module
1 parent 685112b commit 047b85d

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,147 @@ extern "C" {
658658
hashfp: EllswiftEcdhHashFn,
659659
data: *mut c_void)
660660
-> c_int;
661+
662+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_pubnonce_parse")]
663+
pub fn secp256k1_musig_pubnonce_parse(
664+
cx: *const Context,
665+
nonce: *mut MusigPubNonce,
666+
in66: *const c_uchar,
667+
) -> c_int;
668+
669+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_pubnonce_serialize")]
670+
pub fn secp256k1_musig_pubnonce_serialize(
671+
cx: *const Context,
672+
out66: *mut c_uchar,
673+
nonce: *const MusigPubNonce,
674+
) -> c_int;
675+
676+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_aggnonce_parse")]
677+
pub fn secp256k1_musig_aggnonce_parse(
678+
cx: *const Context,
679+
nonce: *mut MusigAggNonce,
680+
in66: *const c_uchar,
681+
) -> c_int;
682+
683+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_aggnonce_serialize")]
684+
pub fn secp256k1_musig_aggnonce_serialize(
685+
cx: *const Context,
686+
out66: *mut c_uchar,
687+
nonce: *const MusigAggNonce,
688+
) -> c_int;
689+
690+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_partial_sig_parse")]
691+
pub fn secp256k1_musig_partial_sig_parse(
692+
cx: *const Context,
693+
sig: *mut MusigPartialSignature,
694+
in32: *const c_uchar,
695+
) -> c_int;
696+
697+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_partial_sig_serialize")]
698+
pub fn secp256k1_musig_partial_sig_serialize(
699+
cx: *const Context,
700+
out32: *mut c_uchar,
701+
sig: *const MusigPartialSignature,
702+
) -> c_int;
703+
704+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_pubkey_agg")]
705+
pub fn secp256k1_musig_pubkey_agg(
706+
cx: *const Context,
707+
agg_pk: *mut XOnlyPublicKey,
708+
keyagg_cache: *mut MusigKeyAggCache,
709+
pubkeys: *const *const PublicKey,
710+
n_pubkeys: size_t,
711+
) -> c_int;
712+
713+
#[cfg_attr(not(rust_secp_no_symbol_renaming),link_name = "rustsecp256k1_v0_10_0_musig_pubkey_get")]
714+
pub fn secp256k1_musig_pubkey_get(
715+
cx: *const Context,
716+
agg_pk: *mut PublicKey,
717+
keyagg_cache: *const MusigKeyAggCache,
718+
) -> c_int;
719+
720+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_pubkey_ec_tweak_add")]
721+
pub fn secp256k1_musig_pubkey_ec_tweak_add(
722+
cx: *const Context,
723+
output_pubkey: *mut PublicKey,
724+
keyagg_cache: *mut MusigKeyAggCache,
725+
tweak32: *const c_uchar,
726+
) -> c_int;
727+
728+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_pubkey_xonly_tweak_add")]
729+
pub fn secp256k1_musig_pubkey_xonly_tweak_add(
730+
cx: *const Context,
731+
output_pubkey: *mut PublicKey,
732+
keyagg_cache: *mut MusigKeyAggCache,
733+
tweak32: *const c_uchar,
734+
) -> c_int;
735+
736+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_nonce_gen")]
737+
pub fn secp256k1_musig_nonce_gen(
738+
cx: *const Context,
739+
secnonce: *mut MusigSecNonce,
740+
pubnonce: *mut MusigPubNonce,
741+
session_secrand32: *const c_uchar,
742+
seckey: *const c_uchar,
743+
pubkey: *const PublicKey,
744+
msg32: *const c_uchar,
745+
keyagg_cache: *const MusigKeyAggCache,
746+
extra_input32: *const c_uchar,
747+
) -> c_int;
748+
749+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_nonce_agg")]
750+
pub fn secp256k1_musig_nonce_agg(
751+
cx: *const Context,
752+
aggnonce: *mut MusigAggNonce,
753+
pubnonces: *const *const MusigPubNonce,
754+
n_pubnonces: size_t,
755+
) -> c_int;
756+
757+
758+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_nonce_process")]
759+
pub fn secp256k1_musig_nonce_process(
760+
cx: *const Context,
761+
session: *mut MusigSession,
762+
aggnonce: *const MusigAggNonce,
763+
msg32: *const c_uchar,
764+
keyagg_cache: *const MusigKeyAggCache,
765+
) -> c_int;
766+
767+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_partial_sign")]
768+
pub fn secp256k1_musig_partial_sign(
769+
cx: *const Context,
770+
partial_sig: *mut MusigPartialSignature,
771+
secnonce: *mut MusigSecNonce,
772+
keypair: *const Keypair,
773+
keyagg_cache: *const MusigKeyAggCache,
774+
session: *const MusigSession,
775+
) -> c_int;
776+
777+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_partial_sig_verify")]
778+
pub fn secp256k1_musig_partial_sig_verify(
779+
cx: *const Context,
780+
partial_sig: *const MusigPartialSignature,
781+
pubnonce: *const MusigPubNonce,
782+
pubkey: *const PublicKey,
783+
keyagg_cache: *const MusigKeyAggCache,
784+
session: *const MusigSession,
785+
) -> c_int;
786+
787+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_musig_partial_sig_agg")]
788+
pub fn secp256k1_musig_partial_sig_agg(
789+
cx: *const Context,
790+
sig64: *mut c_uchar,
791+
session: *const MusigSession,
792+
partial_sigs: *const *const MusigPartialSignature,
793+
n_sigs: size_t,
794+
) -> c_int;
795+
796+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_10_0_ec_pubkey_sort")]
797+
pub fn secp256k1_ec_pubkey_sort(
798+
ctx: *const Context,
799+
pubkeys: *const *const PublicKey,
800+
n_pubkeys: size_t
801+
) -> c_int;
661802
}
662803

663804
#[cfg(not(secp256k1_fuzz))]
@@ -1084,6 +1225,133 @@ impl <T: CPtr> CPtr for Option<T> {
10841225
}
10851226
}
10861227

1228+
pub const MUSIG_KEYAGG_LEN: usize = 197;
1229+
pub const MUSIG_SECNONCE_LEN: usize = 132;
1230+
pub const MUSIG_PUBNONCE_LEN: usize = 132;
1231+
pub const MUSIG_AGGNONCE_LEN: usize = 132;
1232+
pub const MUSIG_AGGNONCE_SERIALIZED_LEN: usize = 66;
1233+
pub const MUSIG_PUBNONCE_SERIALIZED_LEN: usize = 66;
1234+
pub const MUSIG_SESSION_LEN: usize = 133;
1235+
pub const MUSIG_PART_SIG_LEN: usize = 36;
1236+
1237+
#[repr(C)]
1238+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1239+
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_LEN]);
1240+
impl_array_newtype!(MusigKeyAggCache, c_uchar, MUSIG_KEYAGG_LEN);
1241+
impl_raw_debug!(MusigKeyAggCache);
1242+
1243+
impl MusigKeyAggCache {
1244+
pub fn new() -> Self {
1245+
MusigKeyAggCache([0; MUSIG_KEYAGG_LEN])
1246+
}
1247+
}
1248+
1249+
impl Default for MusigKeyAggCache {
1250+
fn default() -> Self {
1251+
Self::new()
1252+
}
1253+
}
1254+
1255+
#[repr(C)]
1256+
#[derive(Copy, Clone, PartialEq, Eq)]
1257+
pub struct MusigSecNonce(pub(crate) [c_uchar; MUSIG_SECNONCE_LEN]);
1258+
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_LEN);
1259+
impl_raw_debug!(MusigSecNonce);
1260+
1261+
impl MusigSecNonce {
1262+
pub fn new() -> Self {
1263+
MusigSecNonce([0; MUSIG_SECNONCE_LEN])
1264+
}
1265+
1266+
// No need for strong warning here, the user cannot use the ffi types directly in any of the APIs
1267+
pub fn dangerous_from_bytes(bytes: [c_uchar; MUSIG_SECNONCE_LEN]) -> Self {
1268+
MusigSecNonce(bytes)
1269+
}
1270+
1271+
// No need for strong warning here, the user cannot use the ffi types directly in any of the high level APIs
1272+
pub fn dangerous_into_bytes(self) -> [c_uchar; MUSIG_SECNONCE_LEN] {
1273+
self.0
1274+
}
1275+
}
1276+
1277+
impl Default for MusigSecNonce {
1278+
fn default() -> Self {
1279+
Self::new()
1280+
}
1281+
}
1282+
1283+
#[repr(C)]
1284+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1285+
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]);
1286+
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_LEN);
1287+
impl_raw_debug!(MusigPubNonce);
1288+
1289+
impl MusigPubNonce {
1290+
pub fn new() -> Self {
1291+
MusigPubNonce([0; MUSIG_PUBNONCE_LEN])
1292+
}
1293+
}
1294+
1295+
impl Default for MusigPubNonce {
1296+
fn default() -> Self {
1297+
Self::new()
1298+
}
1299+
}
1300+
1301+
#[repr(C)]
1302+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1303+
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]);
1304+
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_LEN);
1305+
impl_raw_debug!(MusigAggNonce);
1306+
1307+
impl MusigAggNonce {
1308+
pub fn new() -> Self {
1309+
MusigAggNonce([0; MUSIG_AGGNONCE_LEN])
1310+
}
1311+
}
1312+
1313+
impl Default for MusigAggNonce {
1314+
fn default() -> Self {
1315+
Self::new()
1316+
}
1317+
}
1318+
1319+
#[repr(C)]
1320+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1321+
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]);
1322+
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_LEN);
1323+
impl_raw_debug!(MusigSession);
1324+
1325+
impl MusigSession {
1326+
pub fn new() -> Self {
1327+
MusigSession([0; MUSIG_SESSION_LEN])
1328+
}
1329+
}
1330+
1331+
impl Default for MusigSession {
1332+
fn default() -> Self {
1333+
Self::new()
1334+
}
1335+
}
1336+
1337+
#[repr(C)]
1338+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1339+
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]);
1340+
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_LEN);
1341+
impl_raw_debug!(MusigPartialSignature);
1342+
1343+
impl MusigPartialSignature {
1344+
pub fn new() -> Self {
1345+
MusigPartialSignature([0; MUSIG_PART_SIG_LEN])
1346+
}
1347+
}
1348+
1349+
impl Default for MusigPartialSignature {
1350+
fn default() -> Self {
1351+
Self::new()
1352+
}
1353+
}
1354+
10871355
#[cfg(secp256k1_fuzz)]
10881356
mod fuzz_dummy {
10891357
use super::*;

0 commit comments

Comments
 (0)