Skip to content

Commit

Permalink
WIP: Upgrade to secp256k1 v0.23.2
Browse files Browse the repository at this point in the history
We recently released a new version of `rust-secp256k1`, upgrade to use
it.

WIP because currently includes an `excect` that needs removing.
  • Loading branch information
tcharding committed Jun 28, 2022
1 parent b645b6b commit 515c4a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -36,7 +36,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
bech32 = { version = "0.8.1", default-features = false }
bitcoin_hashes = { version = "0.10.0", default-features = false }
secp256k1 = { version = "0.22.0", default-features = false }
secp256k1 = { version = "0.23.2", default-features = false }
core2 = { version = "0.3.0", optional = true, default-features = false }

base64 = { version = "0.13.0", optional = true }
Expand All @@ -48,7 +48,7 @@ hashbrown = { version = "0.8", optional = true }
[dev-dependencies]
serde_json = "<1.0.45"
serde_test = "1"
secp256k1 = { version = "0.22.0", features = [ "recovery", "rand-std" ] }
secp256k1 = { version = "0.23.2", features = [ "recovery", "rand-std" ] }
bincode = "1.3.1"

[[example]]
Expand Down
11 changes: 5 additions & 6 deletions src/util/bip32.rs
Expand Up @@ -600,15 +600,15 @@ impl ExtendedPrivKey {

hmac_engine.input(&endian::u32_to_array_be(u32::from(i)));
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
let mut sk = secp256k1::SecretKey::from_slice(&hmac_result[..32])?;
sk.add_assign(&self.private_key[..])?;
let sk = secp256k1::SecretKey::from_slice(&hmac_result[..32])?;
let tweaked = sk.add_tweak(&self.private_key.into())?;

Ok(ExtendedPrivKey {
network: self.network,
depth: self.depth + 1,
parent_fingerprint: self.fingerprint(secp),
child_number: i,
private_key: sk,
private_key: tweaked,
chain_code: ChainCode::from(&hmac_result[32..])
})
}
Expand Down Expand Up @@ -741,15 +741,14 @@ impl ExtendedPubKey {
i: ChildNumber,
) -> Result<ExtendedPubKey, Error> {
let (sk, chain_code) = self.ckd_pub_tweak(i)?;
let mut pk = self.public_key;
pk.add_exp_assign(secp, &sk[..])?;
let tweaked = self.public_key.add_exp_tweak(secp, &sk.into())?;

Ok(ExtendedPubKey {
network: self.network,
depth: self.depth + 1,
parent_fingerprint: self.fingerprint(),
child_number: i,
public_key: pk,
public_key: tweaked,
chain_code,
})
}
Expand Down
18 changes: 8 additions & 10 deletions src/util/schnorr.rs
Expand Up @@ -21,7 +21,6 @@ use core::fmt;
use crate::prelude::*;

use secp256k1::{self, Secp256k1, Verification, constants};
use crate::hashes::Hash;
use crate::util::taproot::{TapBranchHash, TapTweakHash};
use crate::SchnorrSighashType;

Expand Down Expand Up @@ -110,11 +109,10 @@ impl TapTweak for UntweakedPublicKey {
/// # Returns
/// The tweaked key and its parity.
fn tap_tweak<C: Verification>(self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> (TweakedPublicKey, secp256k1::Parity) {
let tweak_value = TapTweakHash::from_key_and_tweak(self, merkle_root).into_inner();
let mut output_key = self;
let parity = output_key.tweak_add_assign(secp, &tweak_value).expect("Tap tweak failed");
let tweak = TapTweakHash::from_key_and_tweak(self, merkle_root).to_scalar();
let (output_key, parity) = self.add_tweak(secp, &tweak).expect("Tap tweak failed");

debug_assert!(self.tweak_add_check(secp, &output_key, parity, tweak_value));
debug_assert!(self.tweak_add_check(secp, &output_key, parity, tweak));
(TweakedPublicKey(output_key), parity)
}

Expand All @@ -139,11 +137,11 @@ impl TapTweak for UntweakedKeyPair {
///
/// # Returns
/// The tweaked key and its parity.
fn tap_tweak<C: Verification>(mut self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
let pubkey = crate::XOnlyPublicKey::from_keypair(&self);
let tweak_value = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).into_inner();
self.tweak_add_assign(secp, &tweak_value).expect("Tap tweak failed");
TweakedKeyPair(self)
fn tap_tweak<C: Verification>(self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
let (pubkey, _parity) = crate::XOnlyPublicKey::from_keypair(&self);
let tweak = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).to_scalar();
let tweaked = self.add_xonly_tweak(secp, &tweak).expect("Tap tweak failed");
TweakedKeyPair(tweaked)
}

fn dangerous_assume_tweaked(self) -> TweakedKeyPair {
Expand Down
11 changes: 8 additions & 3 deletions src/util/taproot.rs
Expand Up @@ -18,7 +18,7 @@

use crate::prelude::*;
use crate::io;
use secp256k1::{self, Secp256k1};
use secp256k1::{self, Secp256k1, Scalar};

use core::fmt;
use core::cmp::Reverse;
Expand Down Expand Up @@ -89,6 +89,11 @@ impl TapTweakHash {
}
TapTweakHash::from_engine(eng)
}

/// Converts a `TapTweakHash` into a `Scalar` ready for use with key tweaking API.
pub fn to_scalar(&self) -> Scalar {
Scalar::from_be_bytes(self.into_inner()).expect("TODO: handle this error")
}
}

impl TapLeafHash {
Expand Down Expand Up @@ -818,12 +823,12 @@ impl ControlBlock {
);
}
// compute the taptweak
let tweak = TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash));
let tweak = TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash)).to_scalar();
self.internal_key.tweak_add_check(
secp,
&output_key,
self.output_key_parity,
tweak.into_inner(),
tweak,
)
}
}
Expand Down

0 comments on commit 515c4a2

Please sign in to comment.