Skip to content

Commit f735f14

Browse files
committed
secp256k1-zkp-sys: Add Rust FFI for Musig2 module
1 parent 7becbb1 commit f735f14

File tree

2 files changed

+254
-1
lines changed

2 files changed

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

+251-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,157 @@ 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_combine"
342+
)]
343+
pub fn secp256k1_musig_pubkey_combine(
344+
cx: *const Context,
345+
scratch: *mut ScratchSpace,
346+
combined_pk: *mut XOnlyPublicKey,
347+
pre_session: *mut MusigPreSession,
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+
pre_session: *mut MusigPreSession,
359+
output_pubkey: *mut PublicKey,
360+
internal_pubkey: *const XOnlyPublicKey,
361+
tweak32: *const c_uchar,
362+
) -> c_int;
363+
364+
#[cfg_attr(
365+
not(feature = "external-symbols"),
366+
link_name = "rustsecp256k1zkp_v0_4_0_musig_session_init"
367+
)]
368+
pub fn secp256k1_musig_session_init(
369+
cx: *const Context,
370+
secnonce: *mut MusigSecNonce,
371+
pubnonce: *mut c_uchar,
372+
session_id32: *const c_uchar,
373+
seckey: *const c_uchar,
374+
msg32: *const c_uchar,
375+
combined_pk: *const XOnlyPublicKey,
376+
extra_intput32: *const c_uchar,
377+
) -> c_int;
378+
379+
#[cfg_attr(
380+
not(feature = "external-symbols"),
381+
link_name = "rustsecp256k1zkp_v0_4_0_musig_nonces_combine"
382+
)]
383+
pub fn secp256k1_musig_nonces_combine(
384+
cx: *const Context,
385+
combined_pubnonce: *const c_uchar,
386+
pubnonces: *const *const c_uchar,
387+
n_pubnonces: size_t,
388+
) -> c_int;
389+
390+
#[cfg_attr(
391+
not(feature = "external-symbols"),
392+
link_name = "rustsecp256k1zkp_v0_4_0_musig_process_nonces"
393+
)]
394+
pub fn secp256k1_musig_process_nonces(
395+
cx: *const Context,
396+
session_cache: *mut MusigSessionCache,
397+
sig_template: *mut MusigTemplate,
398+
nonce_parity: *mut c_int,
399+
pubnonces: *const *const c_uchar,
400+
n_pubnonces: size_t,
401+
msg32: *const c_uchar,
402+
combined_pk: *const XOnlyPublicKey,
403+
pre_session: *const MusigPreSession,
404+
adaptor: *const PublicKey,
405+
) -> c_int;
406+
407+
#[cfg_attr(
408+
not(feature = "external-symbols"),
409+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_signature_serialize"
410+
)]
411+
pub fn secp256k1_musig_partial_signature_serialize(
412+
cx: *const Context,
413+
out32: *mut c_uchar,
414+
sig: *const MusigPartialSignature,
415+
) -> c_int;
416+
417+
#[cfg_attr(
418+
not(feature = "external-symbols"),
419+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_signature_parse"
420+
)]
421+
pub fn secp256k1_musig_partial_signature_parse(
422+
cx: *const Context,
423+
sig: *mut MusigPartialSignature,
424+
in32: *const c_uchar,
425+
) -> c_int;
426+
427+
#[cfg_attr(
428+
not(feature = "external-symbols"),
429+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sign"
430+
)]
431+
pub fn secp256k1_musig_partial_sign(
432+
cx: *const Context,
433+
partial_sig: *mut MusigPartialSignature,
434+
secnonce: *mut MusigSecNonce,
435+
keypair: *const KeyPair,
436+
pre_session: *const MusigPreSession,
437+
session_cache: *const MusigSessionCache,
438+
) -> c_int;
439+
440+
#[cfg_attr(
441+
not(feature = "external-symbols"),
442+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_verify"
443+
)]
444+
pub fn secp256k1_musig_partial_sig_verify(
445+
cx: *const Context,
446+
partial_sig: *const MusigPartialSignature,
447+
pubnonce: *const c_uchar,
448+
pubkey: *const XOnlyPublicKey,
449+
pre_session: *const MusigPreSession,
450+
session_cache: *const MusigSessionCache,
451+
) -> c_int;
452+
453+
#[cfg_attr(
454+
not(feature = "external-symbols"),
455+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_combine"
456+
)]
457+
pub fn secp256k1_musig_partial_sig_combine(
458+
cx: *const Context,
459+
sig64: *mut c_uchar,
460+
sig_template: *const MusigTemplate,
461+
partial_sigs: *const *const MusigPartialSignature,
462+
n_sigs: size_t,
463+
) -> c_int;
464+
465+
#[cfg_attr(
466+
not(feature = "external-symbols"),
467+
link_name = "rustsecp256k1zkp_v0_4_0_musig_partial_sig_adapt"
468+
)]
469+
pub fn secp256k1_musig_partial_sig_adapt(
470+
cx: *const Context,
471+
adaptor_sig: *mut MusigPartialSignature,
472+
partial_sig: *const MusigPartialSignature,
473+
sec_adaptor32: *const c_uchar,
474+
nonce_parity: c_int,
475+
) -> c_int;
476+
477+
#[cfg_attr(
478+
not(feature = "external-symbols"),
479+
link_name = "rustsecp256k1zkp_v0_4_0_musig_extract_secret_adaptor"
480+
)]
481+
pub fn secp256k1_musig_extract_secret_adaptor(
482+
cx: *const Context,
483+
sec_adaptor32: *mut c_uchar,
484+
sig64: *const c_uchar,
485+
partial_sigs: *const MusigPartialSignature,
486+
n_partial_sigs: size_t,
487+
nonce_parity: c_int,
488+
) -> c_int;
337489
}
338490

339491
#[repr(C)]
@@ -510,3 +662,101 @@ impl EcdsaAdaptorSignature {
510662
&self.0
511663
}
512664
}
665+
666+
#[repr(C)]
667+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
668+
pub struct MusigPreSession {
669+
pub magic: u64,
670+
pub pk_hash: [c_uchar; 32],
671+
pub second_pk: [c_uchar; 32],
672+
pub pk_parity: c_int,
673+
pub is_tweaked: c_int,
674+
pub tweak: [c_uchar; 32],
675+
pub internal_key_parity: c_int,
676+
}
677+
678+
impl MusigPreSession {
679+
pub fn new() -> Self {
680+
Self {
681+
magic: 0xf4ad_bbdf_7c7d_d304,
682+
pk_hash: [0; 32],
683+
second_pk: [0; 32],
684+
pk_parity: 0,
685+
is_tweaked: 0,
686+
tweak: [0; 32],
687+
internal_key_parity: 0,
688+
}
689+
}
690+
}
691+
692+
#[repr(C)]
693+
pub struct ScratchSpace(c_int);
694+
695+
#[repr(C)]
696+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
697+
pub struct MusigPartialSignature {
698+
pub data: [c_uchar; 32],
699+
}
700+
701+
impl MusigPartialSignature {
702+
pub fn new() -> Self {
703+
Self { data: [0; 32] }
704+
}
705+
}
706+
707+
#[repr(C)]
708+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
709+
pub struct MusigTemplate {
710+
pub data: [c_uchar; 64],
711+
}
712+
713+
impl MusigTemplate {
714+
pub fn new() -> Self {
715+
Self { data: [0; 64] }
716+
}
717+
}
718+
719+
#[repr(C)]
720+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
721+
pub struct MusigSessionCache {
722+
pub data: [c_uchar; 65],
723+
}
724+
725+
impl MusigSessionCache {
726+
pub fn new() -> Self {
727+
Self { data: [0; 65] }
728+
}
729+
}
730+
731+
#[repr(C)]
732+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
733+
pub struct MusigSecNonce {
734+
pub data: [c_uchar; 64],
735+
}
736+
737+
impl MusigSecNonce {
738+
pub fn new() -> Self {
739+
Self { data: [0; 64] }
740+
}
741+
}
742+
743+
#[repr(C)]
744+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
745+
pub struct MusigPubNonce {
746+
pub data: [c_uchar; 66],
747+
}
748+
749+
impl MusigPubNonce {
750+
pub fn new() -> Self {
751+
Self { data: [0; 66] }
752+
}
753+
}
754+
755+
pub fn xonly_from_pubkey(cx: *const Context, pubkey: *const PublicKey) -> (XOnlyPublicKey, c_int) {
756+
unsafe {
757+
let mut xonly = XOnlyPublicKey::new();
758+
let mut parity = 0;
759+
secp256k1_xonly_pubkey_from_pubkey(cx, &mut xonly, &mut parity, pubkey);
760+
(xonly, parity)
761+
}
762+
}

0 commit comments

Comments
 (0)