1 use bitcoin::hashes::{Hash, HashEngine};
2 use bitcoin::hashes::hmac::{Hmac, HmacEngine};
3 use bitcoin::hashes::sha256::Hash as Sha256;
4 use bitcoin::secp256k1::{Message, Secp256k1, SecretKey, Signature, Signing};
6 macro_rules! hkdf_extract_expand {
7 ($salt: expr, $ikm: expr) => {{
8 let mut hmac = HmacEngine::<Sha256>::new($salt);
10 let prk = Hmac::from_engine(hmac).into_inner();
11 let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
13 let t1 = Hmac::from_engine(hmac).into_inner();
14 let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
17 (t1, Hmac::from_engine(hmac).into_inner(), prk)
19 ($salt: expr, $ikm: expr, 2) => {{
20 let (k1, k2, _) = hkdf_extract_expand!($salt, $ikm);
23 ($salt: expr, $ikm: expr, 3) => {{
24 let (k1, k2, prk) = hkdf_extract_expand!($salt, $ikm);
26 let mut hmac = HmacEngine::<Sha256>::new(&prk[..]);
29 (k1, k2, Hmac::from_engine(hmac).into_inner())
33 pub fn hkdf_extract_expand_twice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32]) {
34 hkdf_extract_expand!(salt, ikm, 2)
37 pub fn hkdf_extract_expand_thrice(salt: &[u8], ikm: &[u8]) -> ([u8; 32], [u8; 32], [u8; 32]) {
38 hkdf_extract_expand!(salt, ikm, 3)
42 pub fn sign<C: Signing>(ctx: &Secp256k1<C>, msg: &Message, sk: &SecretKey) -> Signature {
43 #[cfg(feature = "grind_signatures")]
44 let sig = ctx.sign_low_r(msg, sk);
45 #[cfg(not(feature = "grind_signatures"))]
46 let sig = ctx.sign(msg, sk);