From 9e0622ca384a2b55fc08057e9de731471ed61099 Mon Sep 17 00:00:00 2001 From: popcnt <142196625+popcnt1@users.noreply.github.com> Date: Thu, 29 Aug 2024 01:39:55 +0800 Subject: [PATCH] smt: Switch to SHA-256 hashing (#2401) --- Cargo.lock | 2 +- crates/rooch-genesis/released/main | Bin 292641 -> 296075 bytes crates/rooch-genesis/released/test | Bin 273593 -> 274683 bytes crates/rooch-genesis/src/main.rs | 3 +- moveos/smt/Cargo.toml | 11 ++--- moveos/smt/src/jellyfish_merkle/hash.rs | 44 ++++++------------ .../jellyfish_merkle/jellyfish_merkle_test.rs | 26 ----------- moveos/smt/src/smt_object.rs | 8 ++-- 8 files changed, 26 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 453e9bd38a..39fa484d57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11142,6 +11142,7 @@ dependencies = [ "anyhow", "backtrace", "bcs", + "bitcoin 0.31.2", "byteorder", "bytes", "function_name", @@ -11161,7 +11162,6 @@ dependencies = [ "serde 1.0.208", "serde_bytes", "thiserror", - "tiny-keccak", ] [[package]] diff --git a/crates/rooch-genesis/released/main b/crates/rooch-genesis/released/main index 2d5432d76c3b7341a2b3ab18222f452a96dc0d55..8cdbb8e7f98c7ccde6bef1f16942f9f34d44b139 100644 GIT binary patch delta 11210 zcmeI&iGLhr83*uresk^4>~6A~&CxVTvq_pXrAIbxl5H!tDJ_Sk6tF0V8j^I=7TP4_ z04nN+OGQAaDJ-BI7331+C=ig_+EzdX!~+m46_hH-A*agW^UnLU;Pv_V2kbO`Ki_%h zo$GzynauOFn_r2aeOBW9#?Fqm_Kv>xw(hy<-u85FYhR|VZ%(FV?y7XUJ<~ULZnn3r zBeQBwOV7M)rlq^Rr>(WMC7tc)>*?vu%**z)_El}9^86|DF?-{UE?soX?F*Y}&Ukn8 ztk|rc?&0ii(+HLhRh3_~26t?^c=|9=-^D-P~ODlJ9U6mki6aYV5iax9#ZA zd9=uRl){lG=@BxF;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+hFrb5a1!Fs?N6^|qV}jnx zX;mwA3yxSpjln%lR1mCLK|M1ONqfAlX%A1kk`oI%%qzU4&q;tC*^VQQlj1x3XflT* zh*WSS;&kFjJeg|}g-AC}Khk7Ungsa1+4k-5w%jg?n%Sma$g!h2R$Rfe?;o}^6Eev}UX8R% zM9lNHY4N)x!hA5yGxU9VG;Xq_(kY8O$bbwU+$=t4Rc7~j z(g$y{@ZpvylCdyd(Q4k8QeFJ7>KHS>bVMSUwT8OZB?@;*`1ZE36!SDVizVL)a~v-k zj(ux4X1N*@$yodAYv_@wJ)W94+}UkQf3J1@%jLx@7mxnR?z^<#r<+FJH|^KoMjbZc zn-g=}7dOIp{LKeiE9dTDhM=bpZ7o&vvxg6qJ$#T2ojrWuZ!~|evxg7iGGq@Q!ez)F yK7`AVJ$wk4A$#~BzmeI)hwR})_=U(GK4cFc!i|tUe8?U?WDg(yuMQsyrv3xaUZ}AE diff --git a/crates/rooch-genesis/released/test b/crates/rooch-genesis/released/test index 3c57d6d527d9e740c355a767ad29992aa1d1ee6f..57562f181d64026b40df33ebb366167bbbdba78e 100644 GIT binary patch delta 1131 zcmdmaQ{eYQfeC7kNhu~N25APCrY6bB7A6)3#ztlqsc9Bz=0@fwNk#^yW=3X-$;L*e z=1HchCWgkQhUQ7;K!vFWW{D=ICPt|;6B)%PztL2jIA4@;#`e3COf%IPt*2jfU=r6& z$}CCF&&-QYOi3w9EiR5P&n(G^FDOmQ$xM#VPOa2SN-R!&!OQ>#_FygbgtQc-7R4u3 zmZZu-bTL?YZUapkW4c4NMrHLFkS$hG`Y zMoH#oCTSLiMkYyS28n5=Mk%HyCdLM4G7}laCl_cdPMj~wIAQyLX{MR#%>`Q93$z$p jK5`!mU^q3)ngIx)Qj-g`mbS;cF>Q}`W1jPhiGcwCA&V-s diff --git a/crates/rooch-genesis/src/main.rs b/crates/rooch-genesis/src/main.rs index 8a232881ce..54cc660762 100644 --- a/crates/rooch-genesis/src/main.rs +++ b/crates/rooch-genesis/src/main.rs @@ -15,7 +15,8 @@ struct GenesisOpts { chain_id: BuiltinChainID, } -fn main() -> Result<()> { +#[tokio::main] +async fn main() -> Result<()> { let _ = tracing_subscriber::fmt::try_init(); let opts: GenesisOpts = GenesisOpts::parse(); match &opts.chain_id { diff --git a/moveos/smt/Cargo.toml b/moveos/smt/Cargo.toml index 24b4c3ca6e..6aa92055f6 100644 --- a/moveos/smt/Cargo.toml +++ b/moveos/smt/Cargo.toml @@ -16,17 +16,21 @@ rust-version = { workspace = true } [dependencies] anyhow = { workspace = true } +backtrace = { workspace = true } bcs = { workspace = true } bytes = { workspace = true } byteorder = { workspace = true } -backtrace = { workspace = true } +bitcoin = { workspace = true } +function_name = { workspace = true } hex = { workspace = true } log = { workspace = true } +metrics = { workspace = true } more-asserts = { workspace = true } num-derive = { workspace = true } num-traits = { workspace = true } once_cell = { workspace = true } primitive-types = { workspace = true } +prometheus = { workspace = true } proptest = { workspace = true } proptest-derive = { workspace = true } parking_lot = { workspace = true } @@ -34,8 +38,3 @@ rand = { workspace = true } serde = { workspace = true, features = ["derive", "rc"] } serde_bytes = { workspace = true } thiserror = { workspace = true } -tiny-keccak = { workspace = true, features = ["keccak", "sha3"] } -prometheus = { workspace = true } -function_name = { workspace = true } - -metrics = { workspace = true } \ No newline at end of file diff --git a/moveos/smt/src/jellyfish_merkle/hash.rs b/moveos/smt/src/jellyfish_merkle/hash.rs index 1a456de04a..3f0026a68b 100644 --- a/moveos/smt/src/jellyfish_merkle/hash.rs +++ b/moveos/smt/src/jellyfish_merkle/hash.rs @@ -1,6 +1,7 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 +use bitcoin::hashes::{sha256t_hash_newtype, Hash}; use bytes::Bytes; use hex::FromHex; use more_asserts::debug_assert_lt; @@ -14,15 +15,20 @@ use std::{ fmt::{self, Debug}, str::FromStr, }; -use tiny_keccak::{Hasher, Sha3}; + +sha256t_hash_newtype! { + pub struct RoochSmtTag = hash_str("rooch-smt"); + + #[hash_newtype(forward)] + pub struct RoochSmtHash(_); +} pub(crate) fn merkle_hash(left: HashValue, right: HashValue) -> HashValue { let mut value = left.to_vec(); value.extend(right.to_vec()); - HashValue::sha3_256_of(&value) + HashValue::tag_sha256(&value) } -//TODO replace HashValue with H256 /// Output value of our hash function. Intentionally opaque for safety and modularity. #[derive(Clone, Copy, Eq, Hash, PartialEq, PartialOrd, Ord)] #[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))] @@ -73,28 +79,10 @@ impl HashValue { HashValue { hash } } - /// Convenience function that computes a `HashValue` internally equal to - /// the sha3_256 of a byte buffer. It will handle hasher creation, data - /// feeding and finalization. - /// - /// Note this will not result in the `::hash()` for any - /// reasonable struct T, as this computes a sha3 without any ornaments. - pub fn sha3_256_of(buffer: &[u8]) -> Self { - let mut sha3 = Sha3::v256(); - sha3.update(buffer); - HashValue::from_keccak(sha3) - } - - #[cfg(test)] - pub fn from_iter_sha3<'a, I>(buffers: I) -> Self - where - I: IntoIterator, - { - let mut sha3 = Sha3::v256(); - for buffer in buffers { - sha3.update(buffer); - } - HashValue::from_keccak(sha3) + /// Creates a new `HashValue` by tagging the given `data` with `rooch-smt`. + pub fn tag_sha256(data: &[u8]) -> Self { + let digest = RoochSmtHash::hash(data); + HashValue::new(digest.to_byte_array()) } /// Returns the mut reference array @@ -102,12 +90,6 @@ impl HashValue { &mut self.hash[..] } - fn from_keccak(state: Sha3) -> Self { - let mut hash = Self::zero(); - state.finalize(hash.as_ref_mut()); - hash - } - /// Returns the `index`-th bit in the bytes. pub fn bit(&self, index: usize) -> bool { assert!(index < Self::LENGTH_IN_BITS); diff --git a/moveos/smt/src/jellyfish_merkle/jellyfish_merkle_test.rs b/moveos/smt/src/jellyfish_merkle/jellyfish_merkle_test.rs index d1e6af9370..e7e03f6f56 100644 --- a/moveos/smt/src/jellyfish_merkle/jellyfish_merkle_test.rs +++ b/moveos/smt/src/jellyfish_merkle/jellyfish_merkle_test.rs @@ -1015,29 +1015,3 @@ where .next() .map(|(k, _v)| k.clone()) } - -//TODO: add test -// #[test] -// fn blob_crypto_hash_test() -> Result<()> { -// let buf = hex::decode( -// "0xfa000000000000007b161ceeef010000000000000000000000000000000000000000000000000000" -// .strip_prefix("0x") -// .ok_or_else(|| format_err!("strip_prefix error"))?, -// )?; -// let blob = Blob::from(buf); -// let hash = blob.merkle_hash(); - -// let name = starcoin_crypto::_serde_name::trace_name::() -// .expect("The `CryptoHasher` macro only applies to structs and enums"); -// assert_eq!(name, "Blob"); -// let salt_prefix: &[u8] = b"STARCOIN::Blob"; -// let ser = bcs::to_bytes(&blob)?; -// let salt = [ -// HashValue::sha3_256_of(salt_prefix).as_slice(), -// ser.as_slice(), -// ] -// .concat(); -// let hash1 = HashValue::sha3_256_of(&salt[..]); -// assert_eq!(hash, hash1); -// Ok(()) -// } diff --git a/moveos/smt/src/smt_object.rs b/moveos/smt/src/smt_object.rs index 4636b62098..8a38802b4e 100644 --- a/moveos/smt/src/smt_object.rs +++ b/moveos/smt/src/smt_object.rs @@ -4,14 +4,16 @@ // Copyright (c) The Starcoin Core Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::jellyfish_merkle::hash::{HashValue, SMTHash}; +use std::{cell::Cell, fmt}; + use anyhow::Result; use primitive_types::H256; use serde::{ de::{self, DeserializeOwned}, Deserialize, Serialize, }; -use std::{cell::Cell, fmt}; + +use crate::jellyfish_merkle::hash::{HashValue, SMTHash}; pub trait Key: std::cmp::Ord + Copy + Into + From {} @@ -197,7 +199,7 @@ impl SMTHash for SMTObject { match self.cached_hash.get() { Some(hash) => hash, None => { - let hash = HashValue::sha3_256_of(&self.raw); + let hash = HashValue::tag_sha256(&self.raw); self.cached_hash.set(Some(hash)); hash }