From f9428bc8156c9f919ef011fb3d158ec871d35640 Mon Sep 17 00:00:00 2001 From: Cayle Sharrock Date: Mon, 5 Jul 2021 09:06:09 +0200 Subject: [PATCH] Migrate to digest 0.9 (#49) * Migrate to digest 0.9 This PR moves all dependencies to use the digest 0.9 traits and APIs. This is a breaking change, so the minor version is incremented. Clients of this generally only need to update the `result` method to `finalize`; and obviously make use of the v0.9 `digest::Digest` trait where necessary. As a result, the deprecated k12, sha3 and Blake3 objects can be removed. Methods and functins that need a hasher are all generic over `Digest`. We retain the convenience wrapper over `VarBlake2B` to produce 256 bit hashes and implement the necessary sub-traits to support `digest::Digest`. This update also fixes https://github.com/tari-project/tari-crypto/issues/35 * Update src/ristretto/ristretto_keys.rs Co-authored-by: Stan Bondi Co-authored-by: Stan Bondi --- Cargo.toml | 11 ++- src/hash/blake2.rs | 52 +++++-------- src/hash/blake3.rs | 120 ----------------------------- src/hash/k12.rs | 115 --------------------------- src/hash/mod.rs | 3 - src/hash/sha3.rs | 112 --------------------------- src/musig.rs | 4 +- src/ristretto/musig.rs | 8 +- src/ristretto/ristretto_com_sig.rs | 4 +- src/ristretto/ristretto_keys.rs | 6 +- src/ristretto/ristretto_sig.rs | 4 +- src/ristretto/script_commitment.rs | 2 +- src/ristretto/utils.rs | 6 +- src/script/tari_script.rs | 16 ++-- 14 files changed, 46 insertions(+), 417 deletions(-) delete mode 100644 src/hash/blake3.rs delete mode 100644 src/hash/k12.rs delete mode 100644 src/hash/sha3.rs diff --git a/Cargo.toml b/Cargo.toml index 86634b34..97ce2de3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,24 +7,22 @@ categories = ["cryptography"] homepage = "https://tari.com" readme = "README.md" license = "BSD-3-Clause" -version = "0.10.0" +version = "0.11.0" edition = "2018" [dependencies] tari_utilities = "^0.3" base64 = "0.10.1" -digest = "0.8.0" +digest = "0.9.0" rand = { version = "0.8", default-features = false } clear_on_drop = "=0.2.4" curve25519-dalek = { package = "curve25519-dalek-ng", version = "4", default-features = false, features = ["u64_backend", "serde", "alloc"] } bulletproofs = {version = "4.0.0", package="tari_bulletproofs"} merlin = { version = "3", default-features = false } -sha2 = "0.8.0" +sha2 = "0.9.5" sha3 = "0.9" thiserror = "1.0.20" -blake2 = "0.8.1" -blake3 = "0.3" -k12 = "0.1" +blake2 = "0.9.1" rmp-serde = "0.13.7" serde = "1.0.89" serde_json = "1.0" @@ -35,6 +33,7 @@ wasm-bindgen = { version = "^0.2", features = ["serde-serialize"], optional = tr [dev-dependencies] criterion = "0.3.4" bincode = "1.1.4" +blake3 = "0.3" [build-dependencies] cbindgen = "0.17.0" diff --git a/src/hash/blake2.rs b/src/hash/blake2.rs index 93c61d91..76bbf73d 100644 --- a/src/hash/blake2.rs +++ b/src/hash/blake2.rs @@ -20,51 +20,29 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use blake2::VarBlake2b; -use digest::{ - generic_array::{typenum::U32, GenericArray}, - FixedOutput, - Input, - Reset, - VariableOutput, -}; +use blake2::{digest::VariableOutput, VarBlake2b}; +use digest::{consts::U32, generic_array::GenericArray, FixedOutput, Reset, Update}; /// A convenience wrapper produce 256 bit hashes from Blake2b #[derive(Clone, Debug)] pub struct Blake256(VarBlake2b); -impl Blake256 { - pub fn new() -> Self { - let h = VarBlake2b::new(32).unwrap(); - Blake256(h) - } - - pub fn result(self) -> GenericArray { - self.fixed_result() - } -} - impl Default for Blake256 { fn default() -> Self { - let h = VarBlake2b::new(32).unwrap(); + let h = VariableOutput::new(32).unwrap(); Blake256(h) } } -impl Input for Blake256 { - fn input>(&mut self, data: B) { - (self.0).input(data); - } -} - impl FixedOutput for Blake256 { type OutputSize = U32; - fn fixed_result(self) -> GenericArray { - let mut arr = GenericArray::default(); - // ..32 range index is always safe because VarBlake2b is initialized with 32 elements - self.0.variable_result(|res| arr.copy_from_slice(&res[..32])); - arr + fn finalize_into(self, out: &mut GenericArray) { + self.0.finalize_variable(|res| out.copy_from_slice(res)); + } + + fn finalize_into_reset(&mut self, out: &mut GenericArray) { + self.0.finalize_variable_reset(|res| out.copy_from_slice(res)); } } @@ -74,15 +52,21 @@ impl Reset for Blake256 { } } +impl Update for Blake256 { + fn update(&mut self, data: impl AsRef<[u8]>) { + self.0.update(data); + } +} + #[cfg(test)] mod test { use crate::common::Blake256; - use digest::{Input, Reset}; + use digest::Digest; use tari_utilities::hex; #[test] fn blake256() { - let e = Blake256::new().chain(b"one").chain(b"two").result().to_vec(); + let e = Blake256::new().chain(b"one").chain(b"two").finalize().to_vec(); let h = hex::to_hex(&e); assert_eq!( h, @@ -94,7 +78,7 @@ mod test { fn reset() { let mut e = Blake256::default().chain(b"foobar"); e.reset(); - let e = e.chain(b"onetwo").result().to_vec(); + let e = e.chain(b"onetwo").finalize().to_vec(); let h = hex::to_hex(&e); assert_eq!( h, diff --git a/src/hash/blake3.rs b/src/hash/blake3.rs deleted file mode 100644 index 3b4c2281..00000000 --- a/src/hash/blake3.rs +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright 2020 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -use blake3::Hasher; -use digest::{ - generic_array::{typenum::U32, GenericArray}, - FixedOutput, - Input, - Reset, -}; - -/// A convenience wrapper produce 256 bit hashes from Blake3 -#[deprecated( - note = "This wrapper becomes obsolete once tari_crypto updates to digest v0.9, which is dependent on Dalek \ - libraries updating to digest 0.9. When that happens, you can use the underlying blake3 hasher directly \ - and this wrapper will be removed." -)] -#[derive(Clone, Debug)] -pub struct Blake3(Hasher); - -#[allow(deprecated)] -impl Blake3 { - pub fn new() -> Self { - let h = Hasher::new(); - Blake3(h) - } - - pub fn result(self) -> GenericArray { - self.fixed_result() - } -} - -#[allow(deprecated)] -impl Default for Blake3 { - fn default() -> Self { - let h = Hasher::new(); - Blake3(h) - } -} - -#[allow(deprecated)] -impl Input for Blake3 { - fn input>(&mut self, data: B) { - (self.0).update(data.as_ref()); - } -} - -#[allow(deprecated)] -impl FixedOutput for Blake3 { - type OutputSize = U32; - - fn fixed_result(self) -> GenericArray { - let v = (self.0).finalize(); - GenericArray::clone_from_slice(v.as_bytes()) - } -} - -#[allow(deprecated)] -impl Reset for Blake3 { - fn reset(&mut self) { - (self.0).reset(); - } -} - -#[cfg(test)] -#[allow(deprecated)] -mod test { - use crate::hash::blake3::Blake3; - use digest::{Input, Reset}; - use tari_utilities::hex; - - #[test] - fn blake3_test() { - let e = Blake3::new() - .chain(b"The quick brown fox jumps over ") - .chain(b"the lazy dog") - .result() - .to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "2f1514181aadccd913abd94cfa592701a5686ab23f8df1dff1b74710febc6d4a".to_string() - ); - } - - #[test] - fn reset() { - let mut e = Blake3::default().chain(b"foobar"); - e.reset(); - let e = e - .chain(b"The quick brown fox jumps over ") - .chain(b"the lazy dog") - .result() - .to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "2f1514181aadccd913abd94cfa592701a5686ab23f8df1dff1b74710febc6d4a".to_string() - ); - } -} diff --git a/src/hash/k12.rs b/src/hash/k12.rs deleted file mode 100644 index b458cdf1..00000000 --- a/src/hash/k12.rs +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2020 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -use digest::{ - generic_array::{typenum::U32, GenericArray}, - FixedOutput, - Input, -}; -use k12::{ - digest::{ExtendableOutput, Reset, Update}, - KangarooTwelve, -}; - -/// A convenience wrapper produce 256 bit hashes from Kangaroo12 -#[deprecated( - note = "This wrapper becomes obsolete once tari_crypto updates to digest v0.9, which is dependent on Dalek \ - libraries updating to digest 0.9. When that happens, you can use the underlying KangarooTwelve hasher \ - directly and this wrapper will be removed." -)] -#[derive(Debug)] -pub struct K12(KangarooTwelve); - -#[allow(deprecated)] -impl K12 { - pub fn new() -> Self { - let h = KangarooTwelve::new(); - K12(h) - } - - pub fn result(self) -> GenericArray { - self.fixed_result() - } -} - -#[allow(deprecated)] -impl Default for K12 { - fn default() -> Self { - let h = KangarooTwelve::new(); - K12(h) - } -} - -#[allow(deprecated)] -impl Input for K12 { - fn input>(&mut self, data: B) { - (self.0).update(data.as_ref()); - } -} - -#[allow(deprecated)] -impl FixedOutput for K12 { - type OutputSize = U32; - - fn fixed_result(self) -> GenericArray { - let v = (self.0).finalize_boxed(32); - GenericArray::clone_from_slice(&v) - } -} - -#[allow(deprecated)] -impl Reset for K12 { - fn reset(&mut self) { - (self.0).reset(); - } -} - -#[allow(deprecated)] -#[cfg(test)] -mod test { - use crate::hash::k12::K12; - use digest::Input; - use k12::digest::Reset; - use tari_utilities::hex; - - #[test] - fn k12_test() { - let e = K12::new().chain(b"").result().to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "1ac2d450fc3b4205d19da7bfca1b37513c0803577ac7167f06fe2ce1f0ef39e5".to_string() - ); - } - - #[test] - fn reset() { - let mut e = K12::default().chain(b"foobar"); - e.reset(); - let e = e.result().to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "1ac2d450fc3b4205d19da7bfca1b37513c0803577ac7167f06fe2ce1f0ef39e5".to_string() - ); - } -} diff --git a/src/hash/mod.rs b/src/hash/mod.rs index 5fe58c7e..a4e8cc32 100644 --- a/src/hash/mod.rs +++ b/src/hash/mod.rs @@ -21,6 +21,3 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pub mod blake2; -pub mod blake3; -pub mod k12; -pub mod sha3; diff --git a/src/hash/sha3.rs b/src/hash/sha3.rs deleted file mode 100644 index 69d0b423..00000000 --- a/src/hash/sha3.rs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2020 The Tari Project -// -// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -// following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -// disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the -// following disclaimer in the documentation and/or other materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote -// products derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -use digest::{ - generic_array::{typenum::U32, GenericArray}, - FixedOutput, - Input, - Reset, -}; -use sha3::{Digest, Sha3_256}; - -/// A convenience wrapper produce 256 bit hashes from Blake2b -#[deprecated( - note = "This wrapper becomes obsolete once tari_crypto updates to digest v0.9, which is dependent on Dalek \ - libraries updating to digest 0.9. When that happens, you can use the underlying Sha3_256 hasher directly \ - and this wrapper will be removed." -)] -#[derive(Clone, Debug)] -pub struct Sha3(Sha3_256); - -#[allow(deprecated)] -impl Sha3 { - pub fn new() -> Self { - let h = Sha3_256::new(); - Sha3(h) - } - - pub fn result(self) -> GenericArray { - self.fixed_result() - } -} - -#[allow(deprecated)] -impl Default for Sha3 { - fn default() -> Self { - let h = Sha3_256::new(); - Sha3(h) - } -} - -#[allow(deprecated)] -impl Input for Sha3 { - fn input>(&mut self, data: B) { - (self.0).update(data); - } -} - -#[allow(deprecated)] -impl FixedOutput for Sha3 { - type OutputSize = U32; - - fn fixed_result(self) -> GenericArray { - let v = (self.0).finalize(); - GenericArray::clone_from_slice(&v) - } -} - -#[allow(deprecated)] -impl Reset for Sha3 { - fn reset(&mut self) { - (self.0).reset() - } -} - -#[allow(deprecated)] -#[cfg(test)] -mod test { - use crate::hash::sha3::Sha3; - use digest::{Input, Reset}; - use tari_utilities::hex; - - #[test] - fn sha_test() { - let e = Sha3::new().chain(b"a").chain(b"bc").result().to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532".to_string() - ); - } - - #[test] - fn reset() { - let mut e = Sha3::default().chain("fubar"); - e.reset(); - let e = e.chain(b"abc").result().to_vec(); - let h = hex::to_hex(&e); - assert_eq!( - h, - "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532".to_string() - ); - } -} diff --git a/src/musig.rs b/src/musig.rs index 7f5c3185..f02714d6 100644 --- a/src/musig.rs +++ b/src/musig.rs @@ -180,7 +180,7 @@ where for k in self.pub_keys.iter() { common = common.chain(k.as_bytes()); } - K::from_bytes(&common.result()) + K::from_bytes(&common.finalize()) .expect("Could not calculate Scalar from hash value. Your crypto/hash combination might be inconsistent") } @@ -190,7 +190,7 @@ where /// You should ensure that the SecretKey constructor protects against failures and that the hash digest given /// produces a byte array of the correct length. fn calculate_partial_key(common: &[u8], pubkey: &P) -> K { - let k = D::new().chain(common).chain(pubkey.as_bytes()).result(); + let k = D::new().chain(common).chain(pubkey.as_bytes()).finalize(); K::from_bytes(&k) .expect("Could not calculate Scalar from hash value. Your crypto/hash combination might be inconsistent") } diff --git a/src/ristretto/musig.rs b/src/ristretto/musig.rs index bf4bade7..1f365ca3 100644 --- a/src/ristretto/musig.rs +++ b/src/ristretto/musig.rs @@ -548,7 +548,7 @@ impl SignatureCollection { .chain(r_agg.as_bytes()) .chain(p_agg.as_bytes()) .chain(m) - .result(); + .finalize(); RistrettoSecretKey::from_bytes(&e).expect("Found a u256 that does not map to a valid Ristretto scalar") } @@ -916,7 +916,7 @@ mod test { .chain(data.r_agg.as_bytes()) .chain(p_agg.as_bytes()) .chain(&m_hash) - .result(); + .finalize(); assert!(sig.verify_challenge(p_agg, &challenge)); assert_eq!(&s_agg, sig); } @@ -1042,7 +1042,7 @@ mod test_joint_key { .chain(p2.as_bytes()) .chain(p1.as_bytes()) .chain(p3.as_bytes()) - .result() + .finalize() .to_vec(); // Check Ell let ell = RistrettoSecretKey::from_vec(&ell).unwrap(); @@ -1052,7 +1052,7 @@ mod test_joint_key { let h = Sha256::new() .chain(ell.as_bytes()) .chain(p.as_bytes()) - .result() + .finalize() .to_vec(); RistrettoSecretKey::from_vec(&h).unwrap() }; diff --git a/src/ristretto/ristretto_com_sig.rs b/src/ristretto/ristretto_com_sig.rs index 3ae31aa1..8b6cc13c 100644 --- a/src/ristretto/ristretto_com_sig.rs +++ b/src/ristretto/ristretto_com_sig.rs @@ -143,7 +143,7 @@ mod test { .chain(commitment.as_bytes()) .chain(nonce_commitment.as_bytes()) .chain(b"Small Gods") - .result(); + .finalize(); let e_key = RistrettoSecretKey::from_bytes(&challenge).unwrap(); let u_value = &k_1 + e_key.clone() * &x_value; let v_value = &k_2 + e_key * &a_value; @@ -188,7 +188,7 @@ mod test { .chain(nonce_commitment_alice.as_bytes()) .chain(nonce_commitment_bob.as_bytes()) .chain(b"Moving Pictures") - .result(); + .finalize(); // Calculate Alice's signature let sig_alice = RistrettoComSig::sign(a_value_alice, x_value_alice, k_2_alice, k_1_alice, &challenge, &factory).unwrap(); diff --git a/src/ristretto/ristretto_keys.rs b/src/ristretto/ristretto_keys.rs index 03e81972..51d0536b 100644 --- a/src/ristretto/ristretto_keys.rs +++ b/src/ristretto/ristretto_keys.rs @@ -41,8 +41,6 @@ use std::{ }; use tari_utilities::{hex::Hex, ByteArray, ByteArrayError, ExtendBytes, Hashable}; -type HashDigest = Blake2b; - /// The [SecretKey](trait.SecretKey.html) implementation for [Ristretto](https://ristretto.group) is a thin wrapper /// around the Dalek [Scalar](struct.Scalar.html) type, representing a 256-bit integer (mod the group order). /// @@ -256,9 +254,7 @@ impl DiffieHellmanSharedSecret for RistrettoPublicKey { // Requires custom Hashable implementation for RistrettoPublicKey as CompressedRistretto doesnt implement this trait impl Hashable for RistrettoPublicKey { fn hash(&self) -> Vec { - let mut hasher = HashDigest::new(); - hasher.input(&self.to_vec()); - hasher.result().to_vec() + Blake2b::digest(self.as_bytes()).to_vec() } } diff --git a/src/ristretto/ristretto_sig.rs b/src/ristretto/ristretto_sig.rs index c5ad8013..4a93a9b2 100644 --- a/src/ristretto/ristretto_sig.rs +++ b/src/ristretto/ristretto_sig.rs @@ -126,7 +126,7 @@ mod test { .chain(P.as_bytes()) .chain(R.as_bytes()) .chain(b"Small Gods") - .result(); + .finalize(); let e_key = RistrettoSecretKey::from_bytes(&e).unwrap(); let s = &r + &e_key * &k; let sig = RistrettoSchnorr::sign(k, r, &e).unwrap(); @@ -159,7 +159,7 @@ mod test { .chain(P1.as_bytes()) .chain(P2.as_bytes()) .chain(b"Moving Pictures") - .result(); + .finalize(); // Calculate Alice's signature let s1 = RistrettoSchnorr::sign(k1, r1, &e).unwrap(); // Calculate Bob's signature diff --git a/src/ristretto/script_commitment.rs b/src/ristretto/script_commitment.rs index e6ff60e2..be8f5957 100644 --- a/src/ristretto/script_commitment.rs +++ b/src/ristretto/script_commitment.rs @@ -181,7 +181,7 @@ impl ScriptCommitmentFactory { let script_hash = s .as_hash::() .map_err(|_| ScriptCommitmentError::InvalidDigestLength)?; - let h = D::new().chain(c.as_bytes()).chain(&script_hash[..]).result(); + let h = D::new().chain(c.as_bytes()).chain(&script_hash[..]).finalize(); let hash = RistrettoSecretKey::from_bytes(&h[..]).map_err(ScriptCommitmentError::from)?; Ok(key + &hash) } diff --git a/src/ristretto/utils.rs b/src/ristretto/utils.rs index 5fc548c7..a5356e57 100644 --- a/src/ristretto/utils.rs +++ b/src/ristretto/utils.rs @@ -44,7 +44,11 @@ pub fn sign( ) -> Result { let mut rng = rand::thread_rng(); let (nonce, public_nonce) = RistrettoPublicKey::random_keypair(&mut rng); - let message = D::new().chain(public_nonce.as_bytes()).chain(message).result().to_vec(); + let message = D::new() + .chain(public_nonce.as_bytes()) + .chain(message) + .finalize() + .to_vec(); let e = RistrettoSecretKey::from_bytes(&message).map_err(|_| SchnorrSignatureError::InvalidChallenge)?; let s = RistrettoSchnorr::sign(private_key.clone(), nonce.clone(), e.as_bytes())?; Ok(SignatureSet { diff --git a/src/script/tari_script.rs b/src/script/tari_script.rs index 572a0036..ebc3f5b6 100644 --- a/src/script/tari_script.rs +++ b/src/script/tari_script.rs @@ -16,10 +16,8 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // pending updates to Dalek/Digest -#[allow(deprecated)] use crate::{ common::Blake256, - hash::sha3::Sha3, ristretto::{RistrettoPublicKey, RistrettoSecretKey}, script::{ error::ScriptError, @@ -30,8 +28,9 @@ use crate::{ StackItem, }, }; -use blake2::Digest; +use digest::Digest; use sha2::Sha256; +use sha3::Sha3_256; use std::{cmp::Ordering, convert::TryFrom, fmt, ops::Deref}; use tari_utilities::{ hex::{from_hex, to_hex, Hex, HexError}, @@ -173,12 +172,11 @@ impl TariScript { let b = Blake256::new() .chain(pub_key.as_bytes()) .chain(&self.as_bytes()) - .result(); + .finalize(); RistrettoSecretKey::from_bytes(b.as_slice()).map_err(|_| ScriptError::InvalidSignature) } // pending updates to Dalek/Digest - #[allow(deprecated)] fn execute_opcode( &self, opcode: &Opcode, @@ -220,7 +218,7 @@ impl TariScript { OrVerify(n) => TariScript::handle_or_verify(stack, *n), HashBlake256 => TariScript::handle_hash::(stack), HashSha256 => TariScript::handle_hash::(stack), - HashSha3 => TariScript::handle_hash::(stack), + HashSha3 => TariScript::handle_hash::(stack), CheckSig(msg) => match self.check_sig(stack, *msg.deref())? { true => stack.push(Number(1)), false => stack.push(Number(0)), @@ -477,11 +475,8 @@ impl Default for ExecutionState { #[cfg(test)] mod test { - use crate::script::StackItem; - #[allow(deprecated)] use crate::{ common::Blake256, - hash::sha3::Sha3, inputs, keys::{PublicKey, SecretKey}, ristretto::{pedersen::PedersenCommitment, RistrettoPublicKey, RistrettoSchnorr, RistrettoSecretKey}, @@ -490,6 +485,7 @@ mod test { op_codes::{slice_to_boxed_hash, slice_to_boxed_message, HashValue}, ExecutionStack, ScriptContext, + StackItem, StackItem::{Commitment, Hash, Number}, TariScript, DEFAULT_SCRIPT_HASH, @@ -497,6 +493,7 @@ mod test { }; use blake2::Digest; use sha2::Sha256; + use sha3::Sha3_256 as Sha3; use tari_utilities::{hex::Hex, ByteArray}; fn context_with_height(height: u64) -> ScriptContext { @@ -736,7 +733,6 @@ mod test { } #[test] - #[allow(deprecated)] fn op_hash() { let mut rng = rand::thread_rng(); let (_, p) = RistrettoPublicKey::random_keypair(&mut rng);