diff --git a/Cargo.toml b/Cargo.toml index 40e5405d36..f4b4793398 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sapio-bitcoin" -version = "0.28.1" +version = "0.28.2" authors = ["Jeremy Rubin ", "Andrew Poelstra "] license = "CC0-1.0" @@ -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 } @@ -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 diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 60314db76b..bd7f404f8f 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -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(()) } diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 4c24748b86..cdee09a20a 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -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]; diff --git a/src/network/stream_reader.rs b/src/network/stream_reader.rs index 665e3268e6..c8488a468e 100644 --- a/src/network/stream_reader.rs +++ b/src/network/stream_reader.rs @@ -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; } }); diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 611ee005ae..5acec7410a 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -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}; @@ -593,7 +593,7 @@ impl ExtendedPrivKey { hmac_engine.input(&endian::u32_to_array_be(u32::from(i))); let hmac_result: Hmac = 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, @@ -734,7 +734,7 @@ impl ExtendedPubKey { ) -> Result { 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, diff --git a/src/util/key.rs b/src/util/key.rs index a51dbfa1bc..69980f3e1e 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -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::*; diff --git a/src/util/merkleblock.rs b/src/util/merkleblock.rs index cc70f58186..83f4ef7432 100644 --- a/src/util/merkleblock.rs +++ b/src/util/merkleblock.rs @@ -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::(); let hashes = &mut self.hashes; let mut hash = hashes[n].into_inner(); diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index df0b0b0b65..5f40e47c8f 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -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; @@ -110,9 +109,9 @@ impl TapTweak for UntweakedPublicKey { /// # Returns /// The tweaked key and its parity. fn tap_tweak(self, secp: &Secp256k1, merkle_root: Option) -> (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) @@ -141,8 +140,8 @@ impl TapTweak for UntweakedKeyPair { /// The tweaked key and its parity. fn tap_tweak(mut self, secp: &Secp256k1, merkle_root: Option) -> 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) } diff --git a/src/util/sighash.rs b/src/util/sighash.rs index 286e573604..a0000984c1 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -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, @@ -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); diff --git a/src/util/taproot.rs b/src/util/taproot.rs index a50a7d5363..242990820b 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -18,7 +18,7 @@ use prelude::*; use io; -use secp256k1::{self, Secp256k1}; +use secp256k1::{self, Secp256k1, Scalar}; use core::fmt; use core::cmp::Reverse; @@ -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 { @@ -844,7 +849,7 @@ impl ControlBlock { secp, &output_key, self.output_key_parity, - tweak.into_inner(), + tweak.to_scalar(), ) } }