Skip to content

Commit fadc4cf

Browse files
Create rust-bidings
Create bindings for all methods and static types in ellswift.h in secp256k1-sys and their respective safe-rust types. All methods are extensively commented and tested using BIP324's test vectors
1 parent 7922d05 commit fadc4cf

File tree

5 files changed

+744
-0
lines changed

5 files changed

+744
-0
lines changed

secp256k1-sys/src/lib.rs

+72
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ pub type SchnorrNonceFn = Option<unsafe extern "C" fn(
8383
data: *mut c_void,
8484
) -> c_int>;
8585

86+
pub type EllswiftECDHHashFn = Option<
87+
unsafe extern "C" fn(
88+
output: *mut c_uchar,
89+
x32: *const c_uchar,
90+
ell_a64: *const c_uchar,
91+
ell_b64: *const c_uchar,
92+
data: *mut c_void,
93+
) -> c_int,
94+
>;
95+
8696
/// Data structure that contains additional arguments for schnorrsig_sign_custom.
8797
#[repr(C)]
8898
pub struct SchnorrSigExtraParams {
@@ -518,11 +528,41 @@ impl core::hash::Hash for KeyPair {
518528
}
519529
}
520530

531+
pub struct XOnlySharedSecret(pub [u8; 32]);
532+
533+
impl XOnlySharedSecret {
534+
pub fn as_bytes(&self) -> &[u8] {
535+
&self.0
536+
}
537+
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
538+
&mut self.0
539+
}
540+
}
541+
542+
impl_array_newtype!(XOnlySharedSecret, u8, 32);
543+
impl_raw_debug!(XOnlySharedSecret);
544+
545+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
546+
pub struct ElligatorSwift([u8; 64]);
547+
548+
impl ElligatorSwift {
549+
pub fn from_array(arr: [u8; 64]) -> Self {
550+
ElligatorSwift(arr)
551+
}
552+
}
553+
554+
impl_array_newtype!(ElligatorSwift, u8, 64);
555+
impl_raw_debug!(ElligatorSwift);
556+
521557
extern "C" {
522558
/// Default ECDH hash function
523559
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ecdh_hash_function_default")]
524560
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn;
525561

562+
/// Default ECDH hash function for BIP324 key establishment
563+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ellswift_xdh_hash_function_bip324")]
564+
pub static secp256k1_ellswift_xdh_hash_function_bip324: EllswiftECDHHashFn;
565+
526566
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_nonce_function_rfc6979")]
527567
pub static secp256k1_nonce_function_rfc6979: NonceFn;
528568

@@ -601,6 +641,38 @@ extern "C" {
601641
output_pubkey: *mut PublicKey,
602642
keypair: *const KeyPair)
603643
-> c_int;
644+
// Elligator Swift
645+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ellswift_encode")]
646+
pub fn secp256k1_ellswift_encode(
647+
ctx: *const Context,
648+
ell64: *mut c_uchar,
649+
pubkey: *const PublicKey,
650+
rnd32: *const c_uchar,
651+
) -> c_int;
652+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ellswift_decode")]
653+
pub fn secp256k1_ellswift_decode(
654+
ctx: *const Context,
655+
pubkey: *mut u8,
656+
ell64: *const c_uchar,
657+
) -> c_int;
658+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ellswift_create")]
659+
pub fn secp256k1_ellswift_create(
660+
ctx: *const Context,
661+
ell64: *mut c_uchar,
662+
seckey32: *const c_uchar,
663+
aux_rand32: *const c_uchar,
664+
) -> c_int;
665+
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_8_1_ellswift_xdh")]
666+
pub fn secp256k1_ellswift_xdh(
667+
ctx: *const Context,
668+
output: *mut c_uchar,
669+
ell_a64: *const c_uchar,
670+
ell_b64: *const c_uchar,
671+
seckey32: *const c_uchar,
672+
party: c_int,
673+
hashfp: EllswiftECDHHashFn,
674+
data: *mut c_void,
675+
) -> c_int;
604676
}
605677

606678
#[cfg(not(secp256k1_fuzz))]

src/constants.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub const SCHNORR_PUBLIC_KEY_SIZE: usize = 32;
3030
/// The size of a key pair.
3131
pub const KEY_PAIR_SIZE: usize = 96;
3232

33+
/// The size of a full ElligatorSwift encoding.
34+
pub const ELLSWIFT_ENCODING_SIZE: usize = 64;
35+
3336
/// The Prime for the secp256k1 field element.
3437
#[rustfmt::skip]
3538
pub const FIELD_SIZE: [u8; 32] = [

0 commit comments

Comments
 (0)