mod hashes;
mod key;
mod signature;
pub extern crate bitcoin_hashes;
pub extern crate secp256k1;
pub use crate::hashes::{Pay2ContractHash, Sign2ContractHash, Pay2ContractFullHash, Sign2ContractFullHash,TweakHash, PubkeyTweakHash};
pub use crate::key::{TweakableKey, TweakedKey};
use secp256k1::ffi::CPtr as _;
use secp256k1::{
ecdsa, schnorr, Keypair, Message, PublicKey, Secp256k1, SecretKey, Signing,
Verification, XOnlyPublicKey,
};
use crate::signature::NonceFnData;
impl<H: TweakHash<AllowedKeys = XOnly>> TweakedKey<Keypair, H> {
pub fn from_secret_key(
untweaked_key: &SecretKey,
data: &[u8],
) -> Self {
let secp = Secp256k1::signing_only();
let untweaked = Keypair::from_secret_key(&secp, untweaked_key);
Self::new(&untweaked, data)
}
pub fn to_x_only_public_key(&self) -> TweakedKey<XOnlyPublicKey, H> {
self.map_ref(|kp| kp.x_only_public_key().0)
}
pub fn sign_schnorr<C: Signing + Verification>(
&self,
secp: &Secp256k1<C>,
msg: &[u8],
s2c_data: &[u8],
) -> (schnorr::Signature, XOnlyPublicKey)
where
H: PubkeyTweakHash,
H::SignatureTweakHash: TweakHash<AllowedKeys = XOnly>,
{
let mut data = NonceFnData {
ctx: secp,
data: s2c_data,
original_nonce: None,
};
let ext_params = secp256k1::ffi::SchnorrSigExtraParams::new(
Some(crate::signature::s2c_schnorr_nonce_fn::<C, H::SignatureTweakHash>),
core::ptr::addr_of_mut!(data).cast(),
);
unsafe {
let mut ret = [0u8; secp256k1::constants::SCHNORR_SIGNATURE_SIZE];
assert!(
secp256k1::ffi::secp256k1_schnorrsig_sign_custom(
secp.ctx().as_ptr().cast_const(),
ret.as_mut_ptr(),
msg.as_c_ptr(),
msg.len(),
self.as_inner().as_c_ptr(),
&ext_params,
) == 1
);
(
schnorr::Signature::from_byte_array(ret),
data.original_nonce.unwrap().x_only_public_key().0,
)
}
}
}
impl<H: TweakHash<AllowedKeys = Full>> TweakedKey<(SecretKey, PublicKey), H> {
pub fn to_public_key(&self) -> TweakedKey<PublicKey, H> {
self.map_ref(|kp| kp.1)
}
pub fn sign_ecdsa<C: Signing + Verification>(
&self,
secp: &Secp256k1<C>,
msg: impl Into<Message>,
s2c_data: &[u8],
) -> (ecdsa::Signature, PublicKey)
where
H: PubkeyTweakHash,
H::SignatureTweakHash: TweakHash<AllowedKeys = Full>,
{
let msg = msg.into();
let mut data = NonceFnData {
ctx: secp,
data: s2c_data,
original_nonce: None,
};
unsafe {
let mut ret = secp256k1::ffi::Signature::new();
assert!(
secp256k1::ffi::secp256k1_ecdsa_sign(
secp.ctx().as_ptr().cast_const(),
&mut ret,
msg.as_c_ptr(),
self.as_inner().0.as_c_ptr(),
Some(crate::signature::s2c_ecdsa_nonce_fn::<C, H::SignatureTweakHash>),
core::ptr::addr_of_mut!(data).cast(),
) == 1
);
(ret.into(), data.original_nonce.unwrap())
}
}
}
pub trait AllowedKeysMarker: private::Sealed {
type Serialized: bitcoin_hashes::IsByteArray;
}
pub enum Full {}
pub enum XOnly {}
impl AllowedKeysMarker for Full {
type Serialized = [u8; 33];
}
impl AllowedKeysMarker for XOnly {
type Serialized = [u8; 32];
}
mod private {
pub trait Sealed {}
impl Sealed for super::Full {}
impl Sealed for super::XOnly {}
}
#[cfg(test)]
mod tests_x_only {
use super::*;
fn test_data() -> (
Secp256k1<secp256k1::All>,
TweakedKey<Keypair, Pay2ContractHash>,
TweakedKey<XOnlyPublicKey, Pay2ContractHash>,
) {
let secp = Secp256k1::new();
let sk: SecretKey = "0101010101010101010101010101010101010101010101010101010101010101"
.parse()
.unwrap();
let keypair = sk.keypair(&secp);
let (x_only, _parity) = keypair.x_only_public_key();
let prog = b"this is a thing I'm committing in the pubkey";
let tweaked_keypair_1 = TweakedKey::from_secret_key(&sk, prog);
let tweaked_keypair_2 = TweakedKey::new(&keypair, prog);
assert_eq!(tweaked_keypair_1, tweaked_keypair_2);
let tweaked_pk = tweaked_keypair_1.to_x_only_public_key();
let tweaked_pk_1 = TweakedKey::new(&x_only, prog);
assert_eq!(tweaked_pk, tweaked_pk_1);
assert!(tweaked_pk.verify_key_commitment(&x_only, prog));
let prog2 = b"this is NOT a thing I'm committing in the pubkey";
let tweaked_keypair_3 = TweakedKey::from_secret_key(&sk, prog2);
assert_ne!(tweaked_keypair_1, tweaked_keypair_3);
let tweaked_keypair_3 = TweakedKey::from_secret_key(&sk, prog2);
assert_ne!(tweaked_keypair_1, tweaked_keypair_3);
(secp, tweaked_keypair_1, tweaked_pk)
}
#[test]
fn end_to_end_schnorr() {
let (secp, tweaked_keypair, tweaked_pk) = test_data();
let wit = b"this is a thing I'm committing in the signature";
let msg = b"this is the message I'm actually signing";
let (sig, nonce) = tweaked_keypair.sign_schnorr(&secp, msg, wit);
let xonly = tweaked_pk.as_inner();
secp.verify_schnorr(&sig, msg, &xonly)
.unwrap();
assert!(tweaked_pk.verify_schnorr_commitment(&sig, &nonce, wit));
let wit2 = b"this is NOT a thing I'm committing in the signature";
assert!(!tweaked_pk.verify_schnorr_commitment(&sig, &nonce, wit2));
}
}
#[cfg(test)]
mod tests_ecdsa {
use super::*;
fn test_data() -> (
Secp256k1<secp256k1::All>,
TweakedKey<(SecretKey, PublicKey), Pay2ContractFullHash>,
TweakedKey<PublicKey, Pay2ContractFullHash>,
) {
let secp = Secp256k1::new();
let sk: SecretKey = "0101010101010101010101010101010101010101010101010101010101010101"
.parse()
.unwrap();
let pk = sk.public_key(&secp);
let prog = b"this is a thing I'm committing in the pubkey";
let tweaked_keypair = TweakedKey::new(&(sk, pk), prog);
let tweaked_pk = tweaked_keypair.to_public_key();
let tweaked_pk_1 = TweakedKey::new(&pk, prog);
assert_eq!(tweaked_pk, tweaked_pk_1);
assert!(tweaked_pk.verify_key_commitment(&pk, prog));
let prog2 = b"this is NOT a thing I'm committing in the pubkey";
let tweaked_keypair_2 = TweakedKey::new(&(sk, pk), prog2);
assert_ne!(tweaked_keypair, tweaked_keypair_2);
let tweaked_keypair_2 = TweakedKey::new(&(sk, pk), prog2);
assert_ne!(tweaked_keypair, tweaked_keypair_2);
(secp, tweaked_keypair, tweaked_pk)
}
#[test]
fn end_to_end_ecdsa() {
let (secp, tweaked_keypair, tweaked_pk) = test_data();
let wit = b"this is a thing I'm committing in the signature";
let msg = Message::from_digest(*b"the sentence is a digest I swear");
let (sig, nonce) = tweaked_keypair.sign_ecdsa(&secp, msg, wit);
let pk= tweaked_pk.as_inner();
secp.verify_ecdsa(msg, &sig, &pk)
.unwrap();
assert!(tweaked_pk.verify_ecdsa_commitment(&sig, &nonce, wit));
let wit2 = b"this is NOT a thing I'm committing in the signature";
assert!(!tweaked_pk.verify_ecdsa_commitment(&sig, &nonce, wit2));
}
}