Skip to content

Commit 7c9dfff

Browse files
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
Co-authored-by: sanket1729 <[email protected]>
1 parent c61a982 commit 7c9dfff

File tree

2 files changed

+286
-1
lines changed

2 files changed

+286
-1
lines changed

secp256k1-zkp-sys/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ fn main() {
4444
.define("ENABLE_MODULE_RANGEPROOF", Some("1"))
4545
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
4646
.define("ENABLE_MODULE_WHITELIST", Some("1"))
47+
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
48+
.define("ENABLE_MODULE_MUSIG", Some("1"))
49+
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
4750
.define("ECMULT_GEN_PREC_BITS", Some("4"))
4851
// TODO these three should be changed to use libgmp, at least until secp PR 290 is merged
4952
.define("USE_NUM_NONE", Some("1"))

secp256k1-zkp-sys/src/zkp.rs

Lines changed: 283 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use core::{fmt, hash};
2-
use {types::*, Context, NonceFn, PublicKey, Signature};
2+
use {types::*, Context, KeyPair, NonceFn, PublicKey, Signature, XOnlyPublicKey};
33

44
/// Rangeproof maximum length
55
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
@@ -349,6 +349,19 @@ extern "C" {
349349
input_len: size_t,
350350
) -> c_int;
351351

352+
#[cfg_attr(
353+
not(feature = "external-symbols"),
354+
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubkey_agg"
355+
)]
356+
pub fn secp256k1_musig_pubkey_agg(
357+
cx: *const Context,
358+
scratch: *mut ScratchSpace,
359+
combined_pk: *mut XOnlyPublicKey,
360+
pre_session: *mut MusigKeyaggCache,
361+
pubkeys: *const *const XOnlyPublicKey,
362+
n_pubkeys: size_t,
363+
) -> c_int;
364+
352365
#[cfg_attr(
353366
not(feature = "external-symbols"),
354367
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_signature_serialize"
@@ -360,6 +373,28 @@ extern "C" {
360373
sig: *const WhitelistSignature,
361374
) -> c_int;
362375

376+
#[cfg_attr(
377+
not(feature = "external-symbols"),
378+
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubkey_ec_tweak_add"
379+
)]
380+
pub fn secp256k1_musig_pubkey_ec_tweak_add(
381+
cx: *const Context,
382+
output_pubkey: *mut PublicKey,
383+
keyagg_cache: *mut MusigKeyaggCache,
384+
tweak32: *const c_uchar,
385+
) -> c_int;
386+
387+
#[cfg_attr(
388+
not(feature = "external-symbols"),
389+
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubkey_xonly_tweak_add"
390+
)]
391+
pub fn secp256k1_musig_pubkey_xonly_tweak_add(
392+
cx: *const Context,
393+
output_pubkey: *mut XOnlyPublicKey,
394+
keyagg_cache: *mut MusigKeyaggCache,
395+
tweak32: *const c_uchar,
396+
) -> c_int;
397+
363398
#[cfg_attr(
364399
not(feature = "external-symbols"),
365400
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_sign"
@@ -378,6 +413,21 @@ extern "C" {
378413
noncedata: *mut c_void,
379414
) -> c_int;
380415

416+
#[cfg_attr(
417+
not(feature = "external-symbols"),
418+
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_gen"
419+
)]
420+
pub fn secp256k1_musig_nonce_gen(
421+
cx: *const Context,
422+
secnonce: *mut MusigSecNonce,
423+
pubnonce: *mut MusigPubNonce,
424+
session_id32: *const c_uchar,
425+
seckey: *const c_uchar,
426+
msg32: *const c_uchar,
427+
keyagg_cache: *const MusigKeyaggCache,
428+
extra_input32: *const c_uchar,
429+
) -> c_int;
430+
381431
#[cfg_attr(
382432
not(feature = "external-symbols"),
383433
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_verify"
@@ -390,6 +440,162 @@ extern "C" {
390440
n_keys: size_t,
391441
sub_pubkey: *const PublicKey,
392442
) -> c_int;
443+
444+
#[cfg_attr(
445+
not(feature = "external-symbols"),
446+
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_agg"
447+
)]
448+
pub fn secp256k1_musig_nonce_agg(
449+
cx: *const Context,
450+
aggnonce: *const MusigAggNonce,
451+
pubnonces: *const *const MusigPubNonce,
452+
n_pubnonces: size_t,
453+
) -> c_int;
454+
455+
#[cfg_attr(
456+
not(feature = "external-symbols"),
457+
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_process"
458+
)]
459+
pub fn secp256k1_musig_nonce_process(
460+
cx: *const Context,
461+
session: *mut MusigSession,
462+
aggnonce: *const MusigAggNonce,
463+
msg32: *const c_uchar,
464+
keyagg_cache: *const MusigKeyaggCache,
465+
adaptor: *const PublicKey,
466+
) -> c_int;
467+
468+
#[cfg_attr(
469+
not(feature = "external-symbols"),
470+
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubnonce_serialize"
471+
)]
472+
pub fn secp256k1_musig_pubnonce_serialize(
473+
cx: *const Context,
474+
out32: *mut c_uchar,
475+
nonce: *const MusigPubNonce,
476+
) -> c_int;
477+
478+
#[cfg_attr(
479+
not(feature = "external-symbols"),
480+
link_name = "rustsecp256k1zkp_v0_6_0_musig_pubnonce_parse"
481+
)]
482+
pub fn secp256k1_musig_pubnonce_parse(
483+
cx: *const Context,
484+
nonce: *mut MusigPubNonce,
485+
in32: *const c_uchar,
486+
) -> c_int;
487+
488+
#[cfg_attr(
489+
not(feature = "external-symbols"),
490+
link_name = "rustsecp256k1zkp_v0_6_0_musig_aggnonce_serialize"
491+
)]
492+
pub fn secp256k1_musig_aggnonce_serialize(
493+
cx: *const Context,
494+
out32: *mut c_uchar,
495+
nonce: *const MusigAggNonce,
496+
) -> c_int;
497+
498+
#[cfg_attr(
499+
not(feature = "external-symbols"),
500+
link_name = "rustsecp256k1zkp_v0_6_0_musig_aggnonce_parse"
501+
)]
502+
pub fn secp256k1_musig_aggnonce_parse(
503+
cx: *const Context,
504+
nonce: *mut MusigAggNonce,
505+
in32: *const c_uchar,
506+
) -> c_int;
507+
508+
#[cfg_attr(
509+
not(feature = "external-symbols"),
510+
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sig_serialize"
511+
)]
512+
pub fn secp256k1_musig_partial_sig_serialize(
513+
cx: *const Context,
514+
out32: *mut c_uchar,
515+
sig: *const MusigPartialSignature,
516+
) -> c_int;
517+
518+
#[cfg_attr(
519+
not(feature = "external-symbols"),
520+
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sig_parse"
521+
)]
522+
pub fn secp256k1_musig_partial_sig_parse(
523+
cx: *const Context,
524+
sig: *mut MusigPartialSignature,
525+
in32: *const c_uchar,
526+
) -> c_int;
527+
528+
#[cfg_attr(
529+
not(feature = "external-symbols"),
530+
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sign"
531+
)]
532+
pub fn secp256k1_musig_partial_sign(
533+
cx: *const Context,
534+
partial_sig: *mut MusigPartialSignature,
535+
secnonce: *mut MusigSecNonce,
536+
keypair: *const KeyPair,
537+
keyagg_cache: *const MusigKeyaggCache,
538+
session: *const MusigSession,
539+
) -> c_int;
540+
541+
#[cfg_attr(
542+
not(feature = "external-symbols"),
543+
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sig_verify"
544+
)]
545+
pub fn secp256k1_musig_partial_sig_verify(
546+
cx: *const Context,
547+
partial_sig: *const MusigPartialSignature,
548+
pubnonce: *const MusigPubNonce,
549+
pubkey: *const XOnlyPublicKey,
550+
keyagg_cache: *const MusigKeyaggCache,
551+
session: *const MusigSession,
552+
) -> c_int;
553+
554+
#[cfg_attr(
555+
not(feature = "external-symbols"),
556+
link_name = "rustsecp256k1zkp_v0_6_0_musig_partial_sig_agg"
557+
)]
558+
pub fn secp256k1_musig_partial_sig_agg(
559+
cx: *const Context,
560+
sig64: *mut c_uchar,
561+
session: *const MusigSession,
562+
partial_sigs: *const *const MusigPartialSignature,
563+
n_sigs: size_t,
564+
) -> c_int;
565+
566+
#[cfg_attr(
567+
not(feature = "external-symbols"),
568+
link_name = "rustsecp256k1zkp_v0_6_0_musig_nonce_parity"
569+
)]
570+
pub fn secp256k1_musig_nonce_parity(
571+
cx: *const Context,
572+
nonce_parity: *mut c_int,
573+
session: *const MusigSession,
574+
) -> c_int;
575+
576+
#[cfg_attr(
577+
not(feature = "external-symbols"),
578+
link_name = "rustsecp256k1zkp_v0_6_0_musig_adapt"
579+
)]
580+
pub fn secp256k1_musig_adapt(
581+
cx: *const Context,
582+
sig64: *mut c_uchar,
583+
pre_sig64: *const c_uchar,
584+
sec_adaptor32: *const c_uchar,
585+
nonce_parity: c_int,
586+
) -> c_int;
587+
588+
#[cfg_attr(
589+
not(feature = "external-symbols"),
590+
link_name = "rustsecp256k1zkp_v0_6_0_musig_extract_adaptor"
591+
)]
592+
pub fn secp256k1_musig_extract_adaptor(
593+
cx: *const Context,
594+
sec_adaptor32: *mut c_uchar,
595+
sig64: *const c_uchar,
596+
pre_sig64: *const c_uchar,
597+
nonce_parity: c_int,
598+
) -> c_int;
393599
}
394600

395601
#[repr(C)]
@@ -587,3 +793,79 @@ impl EcdsaAdaptorSignature {
587793
&self.0
588794
}
589795
}
796+
797+
#[repr(C)]
798+
pub struct ScratchSpace(c_int);
799+
800+
pub const MUSIG_KEYAGG_LEN: usize = 165;
801+
pub const MUSIG_SECNONCE_LEN: usize = 68;
802+
pub const MUSIG_PUBNONCE_LEN: usize = 132;
803+
pub const MUSIG_AGGNONCE_LEN: usize = 132;
804+
pub const MUSIG_SESSION_LEN: usize = 133;
805+
pub const MUSIG_PART_SIG_LEN: usize = 36;
806+
807+
#[repr(C)]
808+
pub struct MusigKeyaggCache([c_uchar; MUSIG_KEYAGG_LEN]);
809+
impl_array_newtype!(MusigKeyaggCache, c_uchar, MUSIG_KEYAGG_LEN);
810+
impl_raw_debug!(MusigKeyaggCache);
811+
812+
impl MusigKeyaggCache {
813+
pub fn new() -> Self {
814+
MusigKeyaggCache([0; MUSIG_KEYAGG_LEN])
815+
}
816+
}
817+
818+
#[repr(C)]
819+
pub struct MusigSecNonce([c_uchar; MUSIG_SECNONCE_LEN]);
820+
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_LEN);
821+
impl_raw_debug!(MusigSecNonce);
822+
823+
impl MusigSecNonce {
824+
pub fn new() -> Self {
825+
MusigSecNonce([0; MUSIG_SECNONCE_LEN])
826+
}
827+
}
828+
829+
#[repr(C)]
830+
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]);
831+
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_LEN);
832+
impl_raw_debug!(MusigPubNonce);
833+
834+
impl MusigPubNonce {
835+
pub fn new() -> Self {
836+
MusigPubNonce([0; MUSIG_PUBNONCE_LEN])
837+
}
838+
}
839+
840+
#[repr(C)]
841+
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]);
842+
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_LEN);
843+
impl_raw_debug!(MusigAggNonce);
844+
845+
impl MusigAggNonce {
846+
pub fn new() -> Self {
847+
MusigAggNonce([0; MUSIG_AGGNONCE_LEN])
848+
}
849+
}
850+
851+
#[repr(C)]
852+
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]);
853+
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_LEN);
854+
impl_raw_debug!(MusigSession);
855+
856+
impl MusigSession {
857+
pub fn new() -> Self {
858+
MusigSession([0; MUSIG_SESSION_LEN])
859+
}
860+
}
861+
862+
#[repr(C)]
863+
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]);
864+
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_LEN);
865+
impl_raw_debug!(MusigPartialSignature);
866+
867+
impl MusigPartialSignature {
868+
pub fn new() -> Self {
869+
MusigPartialSignature([0; MUSIG_PART_SIG_LEN])
870+
}
871+
}

0 commit comments

Comments
 (0)