Skip to content

Commit

Permalink
Update rust-secp256k1 to add ecdsa-adaptor/schnorr
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Oct 22, 2020
1 parent fd173b3 commit 987c5dd
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "secp256k1"
version = "0.19.0"
version = "0.19.0-adaptor.0"
authors = [ "Dawid Ciężarkiewicz <[email protected]>",
"Andrew Poelstra <[email protected]>" ]
license = "CC0-1.0"
Expand Down
74 changes: 70 additions & 4 deletions src/bip340.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ impl Signature {
_ => Err(InvalidSignature),
}
}

/// Decompose a bip340 signature into a nonce and a secret key
pub fn decompose(&self) -> Result<(PublicKey, SecretKey), Error> {
Ok((
PublicKey::from_slice(&self.0[0..32])?,
SecretKey::from_slice(&self.0[32..64])?,
))
}
}

impl PublicKey {
Expand Down Expand Up @@ -192,6 +200,7 @@ impl<C: Signing> Secp256k1<C> {
&self,
msg: &Message,
sk: &SecretKey,
nonce_fn: ffi::SchnorrNonceFn,
nonce_data: *const ffi::types::c_void,
) -> Result<Signature, Error> {
unsafe {
Expand All @@ -209,7 +218,7 @@ impl<C: Signing> Secp256k1<C> {
sig.as_mut_c_ptr(),
msg.as_c_ptr(),
&keypair,
ffi::secp256k1_nonce_function_bip340,
nonce_fn,
nonce_data
)
);
Expand All @@ -224,7 +233,9 @@ impl<C: Signing> Secp256k1<C> {
msg: &Message,
sk: &SecretKey,
) -> Result<Signature, Error> {
self.bip340_sign_helper(msg, sk, ptr::null())
unsafe {
self.bip340_sign_helper(msg, sk, ffi::secp256k1_nonce_function_bip340, ptr::null())
}
}

/// Create a BIP340 signature using the given auxiliary random data.
Expand All @@ -234,7 +245,14 @@ impl<C: Signing> Secp256k1<C> {
sk: &SecretKey,
aux_rand: &[u8; 32],
) -> Result<Signature, Error> {
self.bip340_sign_helper(msg, sk, aux_rand.as_c_ptr() as *const ffi::types::c_void)
unsafe {
self.bip340_sign_helper(
msg,
sk,
ffi::secp256k1_nonce_function_bip340,
aux_rand.as_c_ptr() as *const ffi::types::c_void,
)
}
}

/// Create a bip340 signature using the given random number generator to
Expand All @@ -249,7 +267,55 @@ impl<C: Signing> Secp256k1<C> {
) -> Result<Signature, Error> {
let mut aux = [0u8; 32];
rng.fill_bytes(&mut aux);
self.bip340_sign_helper(msg, sk, aux.as_c_ptr() as *const ffi::types::c_void)
unsafe {
self.bip340_sign_helper(
msg,
sk,
ffi::secp256k1_nonce_function_bip340,
aux.as_c_ptr() as *const ffi::types::c_void,
)
}
}

/// Create a bip340 signature using the provided nonce.
pub fn bip340_sign_with_nonce(
&self,
msg: &Message,
sk: &SecretKey,
nonce: &SecretKey,
) -> Result<Signature, Error> {
self.bip340_sign_helper(
msg,
sk,
ffi::constant_nonce_fn,
nonce.as_c_ptr() as *const ffi::types::c_void,
)
}

/// Computes a point fo a Schnorr signature.
pub fn schnorr_compute_sig_point(
&self,
msg: &Message,
nonce: &PublicKey,
pubkey: &PublicKey,
) -> Result<::key::PublicKey, Error> {
unsafe {
let mut sigpoint = ffi::PublicKey::new();

let ret = ffi::secp256k1_schnorrsig_compute_sigpoint(
self.ctx,
&mut sigpoint,
msg.as_c_ptr(),
nonce.as_c_ptr(),
pubkey.as_c_ptr(),
);

if ret == 0 {
return Err(Error::InvalidPublicKey);
}

Ok(::key::PublicKey::from(sigpoint))
}
}

/// Verify a BIP340 signature.
Expand Down
6 changes: 6 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub const BIP340_SIGNATURE_SIZE: usize = 64;
/// Size of a x-only public key
pub const BIP340_PUBLIC_KEY_SIZE: usize = 32;

/// Size of an adaptor signature
pub const ADAPTOR_SIGNATURE_SIZE: usize = 65;

/// Size of an adaptor proof
pub const ADAPTOR_PROOF_SIZE: usize = 97;

/// Size of a key pair
pub const KEY_PAIR_SIZE: usize = 96;

Expand Down
Loading

0 comments on commit 987c5dd

Please sign in to comment.