Skip to content

Commit 9caabee

Browse files
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
Co-authored-by: sanket1729 <sanket1729@gmail.com>
1 parent c61a982 commit 9caabee

File tree

2 files changed

+293
-1
lines changed

2 files changed

+293
-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: 290 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,29 @@ 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+
398+
363399
#[cfg_attr(
364400
not(feature = "external-symbols"),
365401
link_name = "rustsecp256k1zkp_v0_6_0_whitelist_sign"
@@ -378,6 +414,21 @@ extern "C" {
378414
noncedata: *mut c_void,
379415
) -> c_int;
380416

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

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

0 commit comments

Comments
 (0)