Skip to content

Commit

Permalink
Migrate to secp256k1 latest
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRubin committed Jan 16, 2024
1 parent 04b4d24 commit ce077d2
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 29 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sapio-bitcoin"
version = "0.28.1"
version = "0.28.2"

authors = ["Jeremy Rubin <[email protected]>", "Andrew Poelstra <[email protected]>"]
license = "CC0-1.0"
Expand Down Expand Up @@ -39,7 +39,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, package= "sapio-secp256k1" }
secp256k1 = { version = "^0.28.1", default-features = false, package= "sapio-secp256k1" }
core2 = { version = "0.3.0", optional = true, default-features = false }
base64-compat = { version = "1.0.0", optional = true }
bitcoinconsensus = { version = "0.19.0-3", optional = true }
Expand All @@ -51,10 +51,10 @@ version = "0.8.0"
optional = true

[dev-dependencies]
serde_derive = "<1.0.99, >= 1.0.0"
serde_json = "<1.0.45, >= 1.0.0"
serde_derive = ">= 1.0.0"
serde_json = ">= 1.0.0"
serde_test = "1"
secp256k1 = { version = "^0.22.0", features = [ "recovery", "rand-std" ], package= "sapio-secp256k1" }
secp256k1 = { version = "^0.28.1", features = [ "recovery", "rand-std" ], package= "sapio-secp256k1" }
bincode = "1.3.1"
jsonschema-valid = "0.2.0"
# We need to pin ryu (transitive dep from serde_json) to stay compatible with Rust 1.22.0
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl Transaction {
// will result in the data written to the writer being hashed, however the correct
// handling of the SIGHASH_SINGLE bug is to return the 'one array' - either implement
// this behaviour manually or use `signature_hash()`.
writer.write(b"[not a transaction] SIGHASH_SINGLE bug")?;
writer.write_all(b"[not a transaction] SIGHASH_SINGLE bug")?;
return Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ mod tests {

data.clear();
data64.clear();
let len = thread_rng().gen_range(1, 256);
let len = thread_rng().gen_range(1..256);
data.resize(len, 0u8);
data64.resize(len, 0u64);
let mut arr33 = [0u8; 33];
Expand Down
6 changes: 2 additions & 4 deletions src/network/stream_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,15 @@ mod test {
// 2. Spawning thread that will be writing our messages to the TCP Stream at the server side
// in async mode
let handle = thread::spawn(move || {
for ostream in listener.incoming() {
if let Some( ostream) = listener.incoming().next() {
let mut ostream = ostream.unwrap();

for piece in pieces {
ostream.write(&piece[..]).unwrap();
ostream.write_all(&piece[..]).unwrap();
ostream.flush().unwrap();
thread::sleep(Duration::from_secs(1));
}

ostream.shutdown(Shutdown::Both).unwrap();
break;
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/util/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::ops::Index;

use hash_types::XpubIdentifier;
use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine, hex};
use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use secp256k1::{self, Secp256k1, XOnlyPublicKey, Scalar};

use network::constants::Network;
use util::{base58, endian, key};
Expand Down Expand Up @@ -593,7 +593,7 @@ 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[..])?;
sk = sk.add_tweak(&Scalar::from(self.private_key))?;

Ok(ExtendedPrivKey {
network: self.network,
Expand Down Expand Up @@ -734,7 +734,7 @@ impl ExtendedPubKey {
) -> Result<ExtendedPubKey, Error> {
let (sk, chain_code) = self.ckd_pub_tweak(i)?;
let mut pk = self.public_key;
pk.add_exp_assign(secp, &sk[..])?;
pk = pk.add_exp_tweak(secp, &Scalar::from(sk))?;

Ok(ExtendedPubKey {
network: self.network,
Expand Down
2 changes: 1 addition & 1 deletion src/util/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! This module provides keys used in Bitcoin that can be roundtrip
//! (de)serialized.

pub use secp256k1::{XOnlyPublicKey, KeyPair};
pub use secp256k1::{XOnlyPublicKey, Keypair as KeyPair};

use prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/util/merkleblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ mod tests {
impl PartialMerkleTree {
/// Flip one bit in one of the hashes - this should break the authentication
fn damage(&mut self, rng: &mut ThreadRng) {
let n = rng.gen_range(0, self.hashes.len());
let n = rng.gen_range(0.. self.hashes.len());
let bit = rng.gen::<u8>();
let hashes = &mut self.hashes;
let mut hash = hashes[n].into_inner();
Expand Down
13 changes: 6 additions & 7 deletions src/util/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
use core::fmt;
use prelude::*;

use secp256k1::{XOnlyPublicKey as _XOnlyPublicKey, KeyPair as _KeyPair};
use secp256k1::{XOnlyPublicKey as _XOnlyPublicKey, Keypair as _KeyPair};

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

Expand Down Expand Up @@ -110,9 +109,9 @@ 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.clone();
let parity = output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
let tweak_value = TapTweakHash::from_key_and_tweak(self, merkle_root).to_scalar();
let output_key = self.clone();
let (output_key, parity) = output_key.add_tweak(&secp, &tweak_value).expect("Tap tweak failed");

debug_assert!(self.tweak_add_check(&secp, &output_key, parity, tweak_value));
(TweakedPublicKey(output_key), parity)
Expand Down Expand Up @@ -141,8 +140,8 @@ impl TapTweak for UntweakedKeyPair {
/// The tweaked key and its parity.
fn tap_tweak<C: Verification>(mut self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
let pubkey = ::XOnlyPublicKey::from_keypair(&self);
let tweak_value = TapTweakHash::from_key_and_tweak(pubkey.0, merkle_root).into_inner();
self.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
let tweak_value = TapTweakHash::from_key_and_tweak(pubkey.0, merkle_root).to_scalar();
self = self.add_xonly_tweak(&secp, &tweak_value).expect("Tap tweak failed");
TweakedKeyPair(self)
}

Expand Down
8 changes: 4 additions & 4 deletions src/util/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,11 +1103,11 @@ mod tests {
};

// tests
let keypair = secp256k1::KeyPair::from_secret_key(&secp, internal_priv_key);
let keypair = secp256k1::Keypair::from_secret_key(&secp, &internal_priv_key);
let internal_key = XOnlyPublicKey::from_keypair(&keypair);
let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root);
let tweak = TapTweakHash::from_key_and_tweak(internal_key.0, merkle_root);
let mut tweaked_keypair = keypair;
tweaked_keypair.tweak_add_assign(&secp, &tweak).unwrap();
tweaked_keypair = tweaked_keypair.add_xonly_tweak(&secp, &tweak.to_scalar()).unwrap();
let mut sig_msg = Vec::new();
cache.taproot_encode_signing_data_to(
&mut sig_msg,
Expand All @@ -1128,7 +1128,7 @@ mod tests {
let msg = secp256k1::Message::from_slice(&sighash).unwrap();
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);

assert_eq!(expected_internal_pk, internal_key);
assert_eq!(expected_internal_pk, internal_key.0);
assert_eq!(expected_tweak, tweak);
assert_eq!(expected_sig_msg, sig_msg);
assert_eq!(expected_sighash, sighash);
Expand Down
9 changes: 7 additions & 2 deletions src/util/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

use core::fmt;
use core::cmp::Reverse;
Expand Down Expand Up @@ -115,6 +115,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 {
// This is statistically extremely unlikely to panic.
Scalar::from_be_bytes(self.into_inner()).expect("hash value greater than curve order")
}
}

impl TapLeafHash {
Expand Down Expand Up @@ -844,7 +849,7 @@ impl ControlBlock {
secp,
&output_key,
self.output_key_parity,
tweak.into_inner(),
tweak.to_scalar(),
)
}
}
Expand Down

0 comments on commit ce077d2

Please sign in to comment.