-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bindings to the ElligatorSwift implementation #627
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,15 @@ pub type SchnorrNonceFn = Option<unsafe extern "C" fn( | |
data: *mut c_void, | ||
) -> c_int>; | ||
|
||
/// A hash function used by `ellswift_ecdh` to hash the final ECDH shared secret. | ||
pub type EllswiftEcdhHashFn = Option<unsafe extern "C" fn( | ||
output: *mut c_uchar, | ||
x32: *const c_uchar, | ||
ell_a64: *const c_uchar, | ||
ell_b64: *const c_uchar, | ||
data: *mut c_void, | ||
) -> c_int>; | ||
|
||
/// Data structure that contains additional arguments for schnorrsig_sign_custom. | ||
#[repr(C)] | ||
pub struct SchnorrSigExtraParams { | ||
|
@@ -517,11 +526,32 @@ impl core::hash::Hash for Keypair { | |
} | ||
} | ||
|
||
/// Library-internal representation of a ElligatorSwift encoded group element. | ||
#[repr(C)] | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct ElligatorSwift([u8; 64]); | ||
Comment on lines
+531
to
+532
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few comments/questions on this type:
|
||
|
||
impl ElligatorSwift { | ||
pub fn from_array(arr: [u8; 64]) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is public maybe we should document that the input array should typically be the array that was created using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory, it could be any 64-bytes array. Ofc we assume that someone knows the secret key associated with the encode point, but this array could come over wire, for example. Not sure if it is restricted to things we explicitly created with |
||
ElligatorSwift(arr) | ||
} | ||
pub fn to_array(self) -> [u8; 64] { | ||
self.0 | ||
} | ||
} | ||
|
||
impl_array_newtype!(ElligatorSwift, u8, 64); | ||
impl_raw_debug!(ElligatorSwift); | ||
|
||
extern "C" { | ||
/// Default ECDH hash function | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ecdh_hash_function_default")] | ||
pub static secp256k1_ecdh_hash_function_default: EcdhHashFn; | ||
|
||
/// Default ECDH hash function for BIP324 key establishment | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ellswift_xdh_hash_function_bip324")] | ||
pub static secp256k1_ellswift_xdh_hash_function_bip324: EllswiftEcdhHashFn; | ||
|
||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_nonce_function_rfc6979")] | ||
pub static secp256k1_nonce_function_rfc6979: NonceFn; | ||
|
||
|
@@ -600,6 +630,34 @@ extern "C" { | |
output_pubkey: *mut PublicKey, | ||
keypair: *const Keypair) | ||
-> c_int; | ||
// Elligator Swift | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ellswift_encode")] | ||
pub fn secp256k1_ellswift_encode(ctx: *const Context, | ||
ell64: *mut c_uchar, | ||
pubkey: *const PublicKey, | ||
rnd32: *const c_uchar) | ||
-> c_int; | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ellswift_decode")] | ||
pub fn secp256k1_ellswift_decode(ctx: *const Context, | ||
pubkey: *mut u8, | ||
ell64: *const c_uchar) | ||
-> c_int; | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ellswift_create")] | ||
pub fn secp256k1_ellswift_create(ctx: *const Context, | ||
ell64: *mut c_uchar, | ||
seckey32: *const c_uchar, | ||
aux_rand32: *const c_uchar) | ||
-> c_int; | ||
#[cfg_attr(not(rust_secp_no_symbol_renaming), link_name = "rustsecp256k1_v0_9_0_ellswift_xdh")] | ||
pub fn secp256k1_ellswift_xdh(ctx: *const Context, | ||
output: *mut c_uchar, | ||
ell_a64: *const c_uchar, | ||
ell_b64: *const c_uchar, | ||
seckey32: *const c_uchar, | ||
party: c_int, | ||
hashfp: EllswiftEcdhHashFn, | ||
data: *mut c_void) | ||
-> c_int; | ||
} | ||
|
||
#[cfg(not(secp256k1_fuzz))] | ||
|
@@ -979,6 +1037,53 @@ impl<T> CPtr for [T] { | |
} | ||
} | ||
|
||
impl<T> CPtr for &[T] { | ||
type Target = T; | ||
fn as_c_ptr(&self) -> *const Self::Target { | ||
if self.is_empty() { | ||
ptr::null() | ||
} else { | ||
self.as_ptr() | ||
} | ||
} | ||
|
||
fn as_mut_c_ptr(&mut self) -> *mut Self::Target { | ||
if self.is_empty() { | ||
ptr::null_mut() | ||
} else { | ||
self.as_ptr() as *mut Self::Target | ||
} | ||
} | ||
|
||
} | ||
|
||
impl CPtr for [u8; 32] { | ||
type Target = u8; | ||
fn as_c_ptr(&self) -> *const Self::Target { | ||
self.as_ptr() | ||
} | ||
|
||
fn as_mut_c_ptr(&mut self) -> *mut Self::Target { | ||
self.as_mut_ptr() | ||
} | ||
} | ||
|
||
impl <T: CPtr> CPtr for Option<T> { | ||
type Target = T::Target; | ||
fn as_mut_c_ptr(&mut self) -> *mut Self::Target { | ||
match self { | ||
Some(contents) => contents.as_mut_c_ptr(), | ||
None => ptr::null_mut(), | ||
} | ||
} | ||
fn as_c_ptr(&self) -> *const Self::Target { | ||
match self { | ||
Some(content) => content.as_c_ptr(), | ||
None => ptr::null(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(secp256k1_fuzz)] | ||
mod fuzz_dummy { | ||
use super::*; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Legend, thanks for implementing the review suggestions!