diff --git a/evm/Cargo.toml b/evm/Cargo.toml index e942a9f9..2d1e2850 100644 --- a/evm/Cargo.toml +++ b/evm/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [dependencies] anyhow = "1.0.40" +blake = "2.0.2" +env_logger = "0.9.0" eth_trie_utils = "0.4.0" ethereum-types = "0.14.0" hex = { version = "0.4.3", optional = true } diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 01ba419b..25e25119 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -41,7 +41,6 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/halt.asm"), include_str!("asm/hash/blake/compression.asm"), include_str!("asm/hash/blake/g_functions.asm"), - include_str!("asm/hash/blake/initial_state.asm"), include_str!("asm/hash/blake/iv.asm"), include_str!("asm/hash/blake/ops.asm"), include_str!("asm/hash/blake/permutations.asm"), diff --git a/evm/src/cpu/kernel/asm/hash/blake/compression.asm b/evm/src/cpu/kernel/asm/hash/blake/compression.asm index f06df538..44b10ffb 100644 --- a/evm/src/cpu/kernel/asm/hash/blake/compression.asm +++ b/evm/src/cpu/kernel/asm/hash/blake/compression.asm @@ -1,14 +1,14 @@ %macro blake_initial_state - %blake_iv(7) - %blake_iv(6) - %blake_iv(5) - %blake_iv(4) - %blake_iv(3) - %blake_iv(2) - %blake_iv(1) + %blake_iv_i(7) + %blake_iv_i(6) + %blake_iv_i(5) + %blake_iv_i(4) + %blake_iv_i(3) + %blake_iv_i(2) + %blake_iv_i(1) // stack: IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7 PUSH 0x01010040 // params: key = 00, digest_size = 64 = 0x40 - %blake_iv(0) + %blake_iv_i(0) XOR // stack: IV_0 ^ params, IV_1, IV_2, IV_3, IV_4, IV_5, IV_6, IV_7 %endmacro @@ -19,7 +19,8 @@ %mload_kernel_general // stack: num_blocks %mul_const(128) - // stack: num_bytes + %increment + // stack: num_bytes+1 %endmacro %macro blake_message_addr @@ -30,7 +31,7 @@ global blake_compression: %blake_initial_state // stack: t_0, t_1, h_0, h_1, h_2, h_3, h_4, h_5, h_6, h_7 - %stack: () -> (0, 0, 0) + %stack () -> (0, 0, 0) // stack: cur_block = 0, t_0 = 0, t_1 = 0, h_0, h_1, h_2, h_3, h_4, h_5, h_6, h_7 diff --git a/evm/src/cpu/kernel/asm/hash/blake/permutations.asm b/evm/src/cpu/kernel/asm/hash/blake/permutations.asm index 6f444c3b..da278ec2 100644 --- a/evm/src/cpu/kernel/asm/hash/blake/permutations.asm +++ b/evm/src/cpu/kernel/asm/hash/blake/permutations.asm @@ -1,4 +1,4 @@ -permutation_1_constants: +global permutation_1_constants: BYTES 14 BYTES 10 BYTES 4 @@ -16,7 +16,7 @@ permutation_1_constants: BYTES 5 BYTES 3 -permutation_2_constants: +global permutation_2_constants: BYTES 11 BYTES 8 BYTES 12 @@ -34,7 +34,7 @@ permutation_2_constants: BYTES 9 BYTES 4 -permutation_3_constants: +global permutation_3_constants: BYTES 7 BYTES 9 BYTES 3 @@ -52,7 +52,7 @@ permutation_3_constants: BYTES 15 BYTES 8 -permutation_4_constants: +global permutation_4_constants: BYTES 9 BYTES 0 BYTES 5 @@ -70,7 +70,7 @@ permutation_4_constants: BYTES 3 BYTES 13 -permutation_5_constants: +global permutation_5_constants: BYTES 2 BYTES 12 BYTES 6 @@ -88,7 +88,7 @@ permutation_5_constants: BYTES 1 BYTES 9 -permutation_6_constants: +global permutation_6_constants: BYTES 12 BYTES 5 BYTES 1 @@ -106,7 +106,7 @@ permutation_6_constants: BYTES 8 BYTES 11 -permutation_7_constants: +global permutation_7_constants: BYTES 13 BYTES 11 BYTES 7 @@ -124,7 +124,7 @@ permutation_7_constants: BYTES 2 BYTES 10 -permutation_8_constants: +global permutation_8_constants: BYTES 6 BYTES 15 BYTES 14 @@ -142,7 +142,7 @@ permutation_8_constants: BYTES 10 BYTES 5 -permutation_9_constants: +global permutation_9_constants: BYTES 10 BYTES 2 BYTES 8 diff --git a/evm/src/cpu/kernel/tests/hash.rs b/evm/src/cpu/kernel/tests/hash.rs index b7b2e5c2..52c8d9dd 100644 --- a/evm/src/cpu/kernel/tests/hash.rs +++ b/evm/src/cpu/kernel/tests/hash.rs @@ -1,6 +1,7 @@ use std::str::FromStr; use anyhow::Result; +use blake::{hash as blake_hash}; use ethereum_types::U256; use rand::{thread_rng, Rng}; use ripemd::{Digest, Ripemd160}; @@ -23,6 +24,13 @@ fn ripemd(input: Vec) -> U256 { U256::from(&hasher.finalize()[..]) } +/// Standard Blake implementation. +fn blake(input: Vec) -> U256 { + let mut result = [0; 32]; + blake_hash(256, &input, &mut result).unwrap(); + U256::from(result) +} + fn make_random_input() -> Vec { // Generate a random message, between 0 and 9999 bytes. let mut rng = thread_rng(); @@ -88,3 +96,8 @@ fn test_sha2() -> Result<()> { fn test_ripemd() -> Result<()> { test_hash("ripemd_stack", &ripemd) } + +#[test] +fn test_blake() -> Result<()> { + test_hash("blake", &blake) +}