Skip to content

Commit 4612c71

Browse files
committed
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
1 parent 04b590d commit 4612c71

File tree

2 files changed

+290
-1
lines changed

2 files changed

+290
-1
lines changed

secp256k1-zkp-sys/build.rs

+3
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

+287-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::{fmt, hash};
2-
use {types::*, Context, NonceFn, PublicKey, Signature};
2+
use {types::*, Context, KeyPair, NonceFn, PublicKey, Signature, XOnlyPublicKey};
3+
use {secp256k1_xonly_pubkey_from_pubkey};
34

45
/// Rangeproof maximum length
56
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
@@ -349,6 +350,19 @@ extern "C" {
349350
input_len: size_t,
350351
) -> c_int;
351352

353+
#[cfg_attr(
354+
not(feature = "external-symbols"),
355+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubkey_agg"
356+
)]
357+
pub fn secp256k1_musig_pubkey_agg(
358+
cx: *const Context,
359+
scratch: *mut ScratchSpace,
360+
combined_pk: *mut XOnlyPublicKey,
361+
pre_session: *mut MusigKeyaggCache,
362+
pubkeys: *const *const XOnlyPublicKey,
363+
n_pubkeys: size_t,
364+
) -> c_int;
365+
352366
#[cfg_attr(
353367
not(feature = "external-symbols"),
354368
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_signature_serialize"
@@ -360,6 +374,17 @@ extern "C" {
360374
sig: *const WhitelistSignature,
361375
) -> c_int;
362376

377+
#[cfg_attr(
378+
not(feature = "external-symbols"),
379+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubkey_tweak_add"
380+
)]
381+
pub fn secp256k1_musig_pubkey_tweak_add(
382+
cx: *const Context,
383+
output_pubkey: *mut PublicKey,
384+
tweak32: *const c_uchar,
385+
keyagg_cache: *mut MusigKeyaggCache,
386+
) -> c_int;
387+
363388
#[cfg_attr(
364389
not(feature = "external-symbols"),
365390
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_sign"
@@ -378,6 +403,21 @@ extern "C" {
378403
noncedata: *mut c_void,
379404
) -> c_int;
380405

406+
#[cfg_attr(
407+
not(feature = "external-symbols"),
408+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_gen"
409+
)]
410+
pub fn secp256k1_musig_nonce_gen(
411+
cx: *const Context,
412+
secnonce: *mut MusigSecNonce,
413+
pubnonce: *mut MusigPubNonce,
414+
session_id32: *const c_uchar,
415+
seckey: *const c_uchar,
416+
msg32: *const c_uchar,
417+
keyagg_cache: *const MusigKeyaggCache,
418+
extra_input32: *const c_uchar,
419+
) -> c_int;
420+
381421
#[cfg_attr(
382422
not(feature = "external-symbols"),
383423
link_name = "rustsecp256k1zkp_v0_4_0_whitelist_verify"
@@ -390,6 +430,161 @@ extern "C" {
390430
n_keys: size_t,
391431
sub_pubkey: *const PublicKey,
392432
) -> c_int;
433+
434+
#[cfg_attr(
435+
not(feature = "external-symbols"),
436+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_agg"
437+
)]
438+
pub fn secp256k1_musig_nonce_agg(
439+
cx: *const Context,
440+
aggnonce: *const MusigAggNonce,
441+
pubnonces: *const *const MusigPubNonce,
442+
n_pubnonces: size_t,
443+
) -> c_int;
444+
445+
#[cfg_attr(
446+
not(feature = "external-symbols"),
447+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_process"
448+
)]
449+
pub fn secp256k1_musig_nonce_process(
450+
cx: *const Context,
451+
session: *mut MusigSession,
452+
aggnonce: *const MusigAggNonce,
453+
msg32: *const c_uchar,
454+
keyagg_cache: *const MusigKeyaggCache,
455+
adaptor: *const PublicKey,
456+
) -> c_int;
457+
458+
#[cfg_attr(
459+
not(feature = "external-symbols"),
460+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubnonce_serialize"
461+
)]
462+
pub fn secp256k1_musig_pubnonce_serialize(
463+
cx: *const Context,
464+
out32: *mut c_uchar,
465+
nonce: *const MusigPubNonce,
466+
) -> c_int;
467+
468+
#[cfg_attr(
469+
not(feature = "external-symbols"),
470+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubnonce_parse"
471+
)]
472+
pub fn secp256k1_musig_pubnonce_parse(
473+
cx: *const Context,
474+
nonce: *mut MusigPubNonce,
475+
in32: *const c_uchar,
476+
) -> c_int;
477+
478+
#[cfg_attr(
479+
not(feature = "external-symbols"),
480+
link_name = "rustsecp256k1zkp_v0_4_0_musig_aggnonce_serialize"
481+
)]
482+
pub fn secp256k1_musig_aggnonce_serialize(
483+
cx: *const Context,
484+
out32: *mut c_uchar,
485+
nonce: *const MusigAggNonce,
486+
) -> c_int;
487+
488+
#[cfg_attr(
489+
not(feature = "external-symbols"),
490+
link_name = "rustsecp256k1zkp_v0_4_0_musig_aggnonce_parse"
491+
)]
492+
pub fn secp256k1_musig_aggnonce_parse(
493+
cx: *const Context,
494+
nonce: *mut MusigAggNonce,
495+
in32: *const c_uchar,
496+
) -> c_int;
497+
498+
#[cfg_attr(
499+
not(feature = "external-symbols"),
500+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_serialize"
501+
)]
502+
pub fn secp256k1_musig_partial_sig_serialize(
503+
cx: *const Context,
504+
out32: *mut c_uchar,
505+
sig: *const MusigPartialSignature,
506+
) -> c_int;
507+
508+
#[cfg_attr(
509+
not(feature = "external-symbols"),
510+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_parse"
511+
)]
512+
pub fn secp256k1_musig_partial_sig_parse(
513+
cx: *const Context,
514+
sig: *mut MusigPartialSignature,
515+
in32: *const c_uchar,
516+
) -> c_int;
517+
518+
#[cfg_attr(
519+
not(feature = "external-symbols"),
520+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sign"
521+
)]
522+
pub fn secp256k1_musig_partial_sign(
523+
cx: *const Context,
524+
partial_sig: *mut MusigPartialSignature,
525+
secnonce: *mut MusigSecNonce,
526+
keypair: *const KeyPair,
527+
keyagg_cache: *const MusigKeyaggCache,
528+
session: *const MusigSession,
529+
) -> c_int;
530+
531+
#[cfg_attr(
532+
not(feature = "external-symbols"),
533+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_verify"
534+
)]
535+
pub fn secp256k1_musig_partial_sig_verify(
536+
cx: *const Context,
537+
partial_sig: *const MusigPartialSignature,
538+
pubnonce: *const MusigPubNonce,
539+
pubkey: *const XOnlyPublicKey,
540+
keyagg_cache: *const MusigKeyaggCache,
541+
session: *const MusigSession,
542+
) -> c_int;
543+
544+
#[cfg_attr(
545+
not(feature = "external-symbols"),
546+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_agg"
547+
)]
548+
pub fn secp256k1_musig_partial_sig_agg(
549+
cx: *const Context,
550+
sig64: *mut c_uchar,
551+
session: *const MusigSession,
552+
partial_sigs: *const *const MusigPartialSignature,
553+
n_sigs: size_t,
554+
) -> c_int;
555+
556+
#[cfg_attr(
557+
not(feature = "external-symbols"),
558+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_parity"
559+
)]
560+
pub fn secp256k1_musig_nonce_parity(
561+
cx: *const Context,
562+
nonce_parity: *mut c_int,
563+
session: *mut MusigSession,
564+
) -> c_int;
565+
566+
#[cfg_attr(
567+
not(feature = "external-symbols"),
568+
link_name = "rustsecp256k1zkp_v0_4_0_musig_adapt"
569+
)]
570+
pub fn secp256k1_musig_adapt(
571+
cx: *const Context,
572+
sig64: *mut c_uchar,
573+
sec_adaptor32: *const c_uchar,
574+
nonce_parity: c_int,
575+
) -> c_int;
576+
577+
#[cfg_attr(
578+
not(feature = "external-symbols"),
579+
link_name = "rustsecp256k1zkp_v0_4_0_musig_extract_adaptor"
580+
)]
581+
pub fn secp256k1_musig_extract_adaptor(
582+
cx: *const Context,
583+
sec_adaptor32: *mut c_uchar,
584+
sig64: *const c_uchar,
585+
pre_sig64: *const c_uchar,
586+
nonce_parity: c_int,
587+
) -> c_int;
393588
}
394589

395590
#[repr(C)]
@@ -599,3 +794,94 @@ impl EcdsaAdaptorSignature {
599794
&self.0
600795
}
601796
}
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+
}
879+
880+
pub fn xonly_from_pubkey(cx: *const Context, pubkey: *const PublicKey) -> (XOnlyPublicKey, c_int) {
881+
unsafe {
882+
let mut xonly = XOnlyPublicKey::new();
883+
let mut parity = 0;
884+
secp256k1_xonly_pubkey_from_pubkey(cx, &mut xonly, &mut parity, pubkey);
885+
(xonly, parity)
886+
}
887+
}

0 commit comments

Comments
 (0)