Skip to content

Commit

Permalink
Replace sha3 crate with sp-core-hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cem Eliguzel committed Aug 12, 2023
1 parent 59189de commit 9dd5823
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 40 deletions.
32 changes: 5 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ scale-codec = { package = "parity-scale-codec", version = "3.6.4", default-featu
scale-info = { version = "2.9.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = "1.0"
sha3 = { version = "0.10", default-features = false }
similar-asserts = "1.1.0"
sqlx = { version = "0.7.1", default-features = false, features = ["macros"] }
thiserror = "1.0"
Expand Down Expand Up @@ -104,6 +103,8 @@ sp-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/su
sp-consensus-aura = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-consensus-grandpa = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { version = "21.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core-hashing = { version = "9.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core-hashing-proc-macro = { version = "9.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-database = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
sp-inherents = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { version = "23.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down
1 change: 0 additions & 1 deletion precompiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ log = { workspace = true }
num_enum = { workspace = true }
scale-info = { workspace = true, optional = true, features = ["derive"] }
serde = { workspace = true, optional = true }
sha3 = { workspace = true }
similar-asserts = { workspace = true, optional = true }

# Moonbeam
Expand Down
5 changes: 3 additions & 2 deletions precompiles/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ path = "tests/tests.rs"

[dependencies]
case = "1.0"
num_enum = { version = "0.5.3", default-features = false }
num_enum = { workspace = true }
prettyplease = "0.1.18"
proc-macro2 = "1.0"
quote = "1.0"
sha3 = "0.10"
sp-core-hashing = { workspace = true }
syn = { version = "1.0", features = ["extra-traits", "fold", "full", "visit"] }

[dev-dependencies]
Expand All @@ -29,5 +30,5 @@ precompile-utils = { path = "../", features = ["testing"] }

fp-evm = { workspace = true }
frame-support = { workspace = true }
sp-core = { workspace = true }
sp-core-hashing = { workspace = true }
sp-std = { workspace = true }
4 changes: 2 additions & 2 deletions precompiles/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use quote::{quote, quote_spanned};
use sha3::{Digest, Keccak256};
use sp_core_hashing::keccak_256;
use syn::{parse_macro_input, spanned::Spanned, Expr, Ident, ItemType, Lit, LitStr};

mod derive_codec;
Expand Down Expand Up @@ -47,7 +47,7 @@ impl ::std::fmt::Debug for Bytes {
pub fn keccak256(input: TokenStream) -> TokenStream {
let lit_str = parse_macro_input!(input as LitStr);

let hash = Keccak256::digest(lit_str.value().as_bytes());
let hash = keccak_256(lit_str.value().as_bytes());

let bytes = Bytes(hash.to_vec());
let eval_str = format!("{:?}", bytes);
Expand Down
2 changes: 1 addition & 1 deletion precompiles/macro/src/precompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use sha3::{Digest, Keccak256};
use sp_core_hashing::keccak_256;
use std::collections::BTreeMap;
use syn::{parse_macro_input, spanned::Spanned};

Expand Down
2 changes: 1 addition & 1 deletion precompiles/macro/src/precompile/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl Precompile {
}

// Compute the 4-bytes selector.
let digest = Keccak256::digest(signature.as_bytes());
let digest = keccak_256(signature.as_bytes());
let selector = u32::from_be_bytes([digest[0], digest[1], digest[2], digest[3]]);

if let Some(previous) = self
Expand Down
8 changes: 4 additions & 4 deletions precompiles/macro/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use sha3::{Digest, Keccak256};
use sp_core_hashing::keccak_256;

#[test]
fn test_keccak256() {
assert_eq!(
&precompile_utils_macro::keccak256!(""),
Keccak256::digest(b"").as_slice(),
keccak_256(b"").as_slice(),
);
assert_eq!(
&precompile_utils_macro::keccak256!("toto()"),
Keccak256::digest(b"toto()").as_slice(),
keccak_256(b"toto()").as_slice(),
);
assert_ne!(
&precompile_utils_macro::keccak256!("toto()"),
Keccak256::digest(b"tata()").as_slice(),
keccak_256(b"tata()").as_slice(),
);
}

Expand Down
1 change: 0 additions & 1 deletion precompiles/tests-external/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ derive_more = { workspace = true }
hex-literal = { workspace = true }
precompile-utils = { workspace = true, features = ["testing"] }
serde = { workspace = true }
sha3 = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
Expand Down

0 comments on commit 9dd5823

Please sign in to comment.