Skip to content

Commit b77f47f

Browse files
committed
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
1 parent d5d1f80 commit b77f47f

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
@@ -43,6 +43,9 @@ fn main() {
4343
.define("ENABLE_MODULE_GENERATOR", Some("1"))
4444
.define("ENABLE_MODULE_RANGEPROOF", Some("1"))
4545
.define("ENABLE_MODULE_ECDSA_ADAPTOR", Some("1"))
46+
.define("ENABLE_MODULE_EXTRAKEYS", Some("1"))
47+
.define("ENABLE_MODULE_MUSIG", Some("1"))
48+
.define("ENABLE_MODULE_SCHNORRSIG", Some("1"))
4649
.define("ECMULT_GEN_PREC_BITS", Some("4"))
4750
// TODO these three should be changed to use libgmp, at least until secp PR 290 is merged
4851
.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, PublicKey, Signature};
2+
use {types::*, Context, KeyPair, PublicKey, Signature, XOnlyPublicKey};
3+
use {secp256k1_xonly_pubkey_from_pubkey};
34

45
/// Rangeproof maximum length
56
pub const RANGEPROOF_MAX_LENGTH: size_t = 5134;
@@ -334,6 +335,200 @@ extern "C" {
334335
adaptor_sig162: *const EcdsaAdaptorSignature,
335336
enckey: *const PublicKey,
336337
) -> c_int;
338+
339+
#[cfg_attr(
340+
not(feature = "external-symbols"),
341+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubkey_agg"
342+
)]
343+
pub fn secp256k1_musig_pubkey_agg(
344+
cx: *const Context,
345+
scratch: *mut ScratchSpace,
346+
combined_pk: *mut XOnlyPublicKey,
347+
pre_session: *mut MusigKeyaggCache,
348+
pubkeys: *const *const XOnlyPublicKey,
349+
n_pubkeys: size_t,
350+
) -> c_int;
351+
352+
#[cfg_attr(
353+
not(feature = "external-symbols"),
354+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubkey_tweak_add"
355+
)]
356+
pub fn secp256k1_musig_pubkey_tweak_add(
357+
cx: *const Context,
358+
output_pubkey: *mut PublicKey,
359+
tweak32: *const c_uchar,
360+
keyagg_cache: *mut MusigKeyaggCache,
361+
) -> c_int;
362+
363+
#[cfg_attr(
364+
not(feature = "external-symbols"),
365+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_gen"
366+
)]
367+
pub fn secp256k1_musig_nonce_gen(
368+
cx: *const Context,
369+
secnonce: *mut MusigSecNonce,
370+
pubnonce: *mut MusigPubNonce,
371+
session_id32: *const c_uchar,
372+
seckey: *const c_uchar,
373+
msg32: *const c_uchar,
374+
keyagg_cache: *const MusigKeyaggCache,
375+
extra_input32: *const c_uchar,
376+
) -> c_int;
377+
378+
#[cfg_attr(
379+
not(feature = "external-symbols"),
380+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_agg"
381+
)]
382+
pub fn secp256k1_musig_nonce_agg(
383+
cx: *const Context,
384+
aggnonce: *const MusigAggNonce,
385+
pubnonces: *const *const MusigPubNonce,
386+
n_pubnonces: size_t,
387+
) -> c_int;
388+
389+
#[cfg_attr(
390+
not(feature = "external-symbols"),
391+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_process"
392+
)]
393+
pub fn secp256k1_musig_nonce_process(
394+
cx: *const Context,
395+
session: *mut MusigSession,
396+
aggnonce: *const MusigAggNonce,
397+
msg32: *const c_uchar,
398+
keyagg_cache: *const MusigKeyaggCache,
399+
adaptor: *const PublicKey,
400+
) -> c_int;
401+
402+
#[cfg_attr(
403+
not(feature = "external-symbols"),
404+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubnonce_serialize"
405+
)]
406+
pub fn secp256k1_musig_pubnonce_serialize(
407+
cx: *const Context,
408+
out32: *mut c_uchar,
409+
nonce: *const MusigPubNonce,
410+
) -> c_int;
411+
412+
#[cfg_attr(
413+
not(feature = "external-symbols"),
414+
link_name = "rustsecp256k1zkp_v0_4_0_musig_pubnonce_parse"
415+
)]
416+
pub fn secp256k1_musig_pubnonce_parse(
417+
cx: *const Context,
418+
nonce: *mut MusigPubNonce,
419+
in32: *const c_uchar,
420+
) -> c_int;
421+
422+
#[cfg_attr(
423+
not(feature = "external-symbols"),
424+
link_name = "rustsecp256k1zkp_v0_4_0_musig_aggnonce_serialize"
425+
)]
426+
pub fn secp256k1_musig_aggnonce_serialize(
427+
cx: *const Context,
428+
out32: *mut c_uchar,
429+
nonce: *const MusigAggNonce,
430+
) -> c_int;
431+
432+
#[cfg_attr(
433+
not(feature = "external-symbols"),
434+
link_name = "rustsecp256k1zkp_v0_4_0_musig_aggnonce_parse"
435+
)]
436+
pub fn secp256k1_musig_aggnonce_parse(
437+
cx: *const Context,
438+
nonce: *mut MusigAggNonce,
439+
in32: *const c_uchar,
440+
) -> c_int;
441+
442+
#[cfg_attr(
443+
not(feature = "external-symbols"),
444+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_serialize"
445+
)]
446+
pub fn secp256k1_musig_partial_sig_serialize(
447+
cx: *const Context,
448+
out32: *mut c_uchar,
449+
sig: *const MusigPartialSignature,
450+
) -> c_int;
451+
452+
#[cfg_attr(
453+
not(feature = "external-symbols"),
454+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_parse"
455+
)]
456+
pub fn secp256k1_musig_partial_sig_parse(
457+
cx: *const Context,
458+
sig: *mut MusigPartialSignature,
459+
in32: *const c_uchar,
460+
) -> c_int;
461+
462+
#[cfg_attr(
463+
not(feature = "external-symbols"),
464+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sign"
465+
)]
466+
pub fn secp256k1_musig_partial_sign(
467+
cx: *const Context,
468+
partial_sig: *mut MusigPartialSignature,
469+
secnonce: *mut MusigSecNonce,
470+
keypair: *const KeyPair,
471+
keyagg_cache: *const MusigKeyaggCache,
472+
session: *const MusigSession,
473+
) -> c_int;
474+
475+
#[cfg_attr(
476+
not(feature = "external-symbols"),
477+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_verify"
478+
)]
479+
pub fn secp256k1_musig_partial_sig_verify(
480+
cx: *const Context,
481+
partial_sig: *const MusigPartialSignature,
482+
pubnonce: *const MusigPubNonce,
483+
pubkey: *const XOnlyPublicKey,
484+
keyagg_cache: *const MusigKeyaggCache,
485+
session: *const MusigSession,
486+
) -> c_int;
487+
488+
#[cfg_attr(
489+
not(feature = "external-symbols"),
490+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_agg"
491+
)]
492+
pub fn secp256k1_musig_partial_sig_agg(
493+
cx: *const Context,
494+
sig64: *mut c_uchar,
495+
session: *const MusigSession,
496+
partial_sigs: *const *const MusigPartialSignature,
497+
n_sigs: size_t,
498+
) -> c_int;
499+
500+
#[cfg_attr(
501+
not(feature = "external-symbols"),
502+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonce_parity"
503+
)]
504+
pub fn secp256k1_musig_nonce_parity(
505+
cx: *const Context,
506+
nonce_parity: *mut c_int,
507+
session: *mut MusigSession,
508+
) -> c_int;
509+
510+
#[cfg_attr(
511+
not(feature = "external-symbols"),
512+
link_name = "rustsecp256k1zkp_v0_4_0_musig_adapt"
513+
)]
514+
pub fn secp256k1_musig_adapt(
515+
cx: *const Context,
516+
sig64: *mut c_uchar,
517+
sec_adaptor32: *const c_uchar,
518+
nonce_parity: c_int,
519+
) -> c_int;
520+
521+
#[cfg_attr(
522+
not(feature = "external-symbols"),
523+
link_name = "rustsecp256k1zkp_v0_4_0_musig_extract_adaptor"
524+
)]
525+
pub fn secp256k1_musig_extract_adaptor(
526+
cx: *const Context,
527+
sec_adaptor32: *mut c_uchar,
528+
sig64: *const c_uchar,
529+
pre_sig64: *const c_uchar,
530+
nonce_parity: c_int,
531+
) -> c_int;
337532
}
338533

339534
#[repr(C)]
@@ -510,3 +705,94 @@ impl EcdsaAdaptorSignature {
510705
&self.0
511706
}
512707
}
708+
709+
#[repr(C)]
710+
pub struct ScratchSpace(c_int);
711+
712+
pub const MUSIG_KEYAGG_LEN: usize = 165;
713+
pub const MUSIG_SECNONCE_LEN: usize = 68;
714+
pub const MUSIG_PUBNONCE_LEN: usize = 132;
715+
pub const MUSIG_AGGNONCE_LEN: usize = 132;
716+
pub const MUSIG_SESSION_LEN: usize = 133;
717+
pub const MUSIG_PART_SIG_LEN: usize = 36;
718+
719+
#[repr(C)]
720+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
721+
pub struct MusigKeyaggCache {
722+
pub data: [c_uchar; MUSIG_KEYAGG_LEN],
723+
}
724+
725+
impl MusigKeyaggCache {
726+
pub fn new() -> Self {
727+
Self { data: [0; MUSIG_KEYAGG_LEN] }
728+
}
729+
}
730+
731+
#[repr(C)]
732+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
733+
pub struct MusigSecNonce {
734+
pub data: [c_uchar; MUSIG_SECNONCE_LEN],
735+
}
736+
737+
impl MusigSecNonce {
738+
pub fn new() -> Self {
739+
Self { data: [0; MUSIG_SECNONCE_LEN] }
740+
}
741+
}
742+
743+
#[repr(C)]
744+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
745+
pub struct MusigPubNonce {
746+
pub data: [c_uchar; MUSIG_PUBNONCE_LEN],
747+
}
748+
749+
impl MusigPubNonce {
750+
pub fn new() -> Self {
751+
Self { data: [0; MUSIG_PUBNONCE_LEN] }
752+
}
753+
}
754+
755+
#[repr(C)]
756+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
757+
pub struct MusigAggNonce {
758+
pub data: [c_uchar; MUSIG_AGGNONCE_LEN],
759+
}
760+
761+
impl MusigAggNonce {
762+
pub fn new() -> Self {
763+
Self { data: [0; MUSIG_AGGNONCE_LEN] }
764+
}
765+
}
766+
767+
#[repr(C)]
768+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
769+
pub struct MusigSession {
770+
pub data: [c_uchar; MUSIG_SESSION_LEN],
771+
}
772+
773+
impl MusigSession {
774+
pub fn new() -> Self {
775+
Self { data: [0; MUSIG_SESSION_LEN] }
776+
}
777+
}
778+
779+
#[repr(C)]
780+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
781+
pub struct MusigPartialSignature {
782+
pub data: [c_uchar; MUSIG_PART_SIG_LEN],
783+
}
784+
785+
impl MusigPartialSignature {
786+
pub fn new() -> Self {
787+
Self { data: [0; MUSIG_PART_SIG_LEN] }
788+
}
789+
}
790+
791+
pub fn xonly_from_pubkey(cx: *const Context, pubkey: *const PublicKey) -> (XOnlyPublicKey, c_int) {
792+
unsafe {
793+
let mut xonly = XOnlyPublicKey::new();
794+
let mut parity = 0;
795+
secp256k1_xonly_pubkey_from_pubkey(cx, &mut xonly, &mut parity, pubkey);
796+
(xonly, parity)
797+
}
798+
}

0 commit comments

Comments
 (0)