From d1379ac1e90a6d12adfe86233fd3f1548b5169be Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Fri, 31 Mar 2023 13:38:27 -0400 Subject: [PATCH] Fix hash output writing to memory --- evm/src/witness/util.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/evm/src/witness/util.rs b/evm/src/witness/util.rs index a5ebf2ac..afd8bad1 100644 --- a/evm/src/witness/util.rs +++ b/evm/src/witness/util.rs @@ -1,4 +1,4 @@ -use ethereum_types::U256; +use ethereum_types::{BigEndianHash, H256, U256}; use plonky2::field::types::Field; use crate::cpu::columns::CpuColumnsView; @@ -135,9 +135,18 @@ pub(crate) fn mem_write_gp_log_and_fill( channel.addr_context = F::from_canonical_usize(address.context); channel.addr_segment = F::from_canonical_usize(address.segment); channel.addr_virtual = F::from_canonical_usize(address.virt); - for (i, limb) in val_limbs.into_iter().enumerate() { - channel.value[2 * i] = F::from_canonical_u32(limb as u32); - channel.value[2 * i + 1] = F::from_canonical_u32((limb >> 32) as u32); + if row.is_keccak_sponge.is_one() { + // Retrieve hash value as it was computed and store the values in memory the same way the kernel hash is stored. + let val_h256 = H256::from_uint(&val).0; + let val_u32s: [u32; 8] = core::array::from_fn(|i| { + u32::from_le_bytes(core::array::from_fn(|j| val_h256[i * 4 + j])) + }); + channel.value = val_u32s.map(F::from_canonical_u32); + } else { + for (i, limb) in val_limbs.into_iter().enumerate() { + channel.value[2 * i] = F::from_canonical_u32(limb as u32); + channel.value[2 * i + 1] = F::from_canonical_u32((limb >> 32) as u32); + } } op