diff --git a/zkevm-circuits/src/bytecode_circuit/circuit.rs b/zkevm-circuits/src/bytecode_circuit/circuit.rs index 7da4bd14d2..27ade1057f 100644 --- a/zkevm-circuits/src/bytecode_circuit/circuit.rs +++ b/zkevm-circuits/src/bytecode_circuit/circuit.rs @@ -4,8 +4,8 @@ use crate::{ util::{get_push_size, Challenges, Expr, SubCircuit, SubCircuitConfig}, witness, }; -use bus_mapping::state_db::EMPTY_CODE_HASH_LE; -use eth_types::{Field, ToLittleEndian}; +use bus_mapping::{state_db::EMPTY_CODE_HASH_LE, util::POSEIDON_CODE_HASH_ZERO}; +use eth_types::{Field, ToLittleEndian, ToScalar, ToWord}; use gadgets::is_zero::{IsZeroChip, IsZeroConfig, IsZeroInstruction}; use halo2_proofs::{ circuit::{Layouter, Region, Value}, @@ -241,10 +241,14 @@ impl SubCircuitConfig for BytecodeCircuitConfig { meta.query_advice(length, Rotation::cur()), ); - let empty_hash = rlc::expr( - &EMPTY_CODE_HASH_LE.map(|v| Expression::Constant(F::from(v as u64))), - challenges.evm_word(), - ); + let empty_hash = if cfg!(feature = "poseidon-codehash") { + Expression::Constant(POSEIDON_CODE_HASH_ZERO.to_word().to_scalar().unwrap()) + } else { + rlc::expr( + &EMPTY_CODE_HASH_LE.map(|v| Expression::Constant(F::from(v as u64))), + challenges.evm_word(), + ) + }; cb.require_equal( "assert cur.hash == EMPTY_HASH", @@ -457,9 +461,13 @@ impl BytecodeCircuitConfig { last_row_offset ); - let empty_hash = challenges - .evm_word() - .map(|challenge| rlc::value(EMPTY_CODE_HASH_LE.as_ref(), challenge)); + let empty_hash = challenges.evm_word().map(|challenge| { + if cfg!(feature = "poseidon-codehash") { + POSEIDON_CODE_HASH_ZERO.to_word().to_scalar().unwrap() + } else { + rlc::value(EMPTY_CODE_HASH_LE.as_ref(), challenge) + } + }); let mut is_first_time = true; layouter.assign_region( @@ -529,9 +537,13 @@ impl BytecodeCircuitConfig { // Code hash with challenge is calculated only using the first row of the // bytecode (header row), the rest of the code_hash in other rows are ignored. - let code_hash = challenges - .evm_word() - .map(|challenge| rlc::value(&bytecode.rows[0].code_hash.to_le_bytes(), challenge)); + let code_hash = challenges.evm_word().map(|challenge| { + if cfg!(feature = "poseidon-codehash") { + bytecode.rows[0].code_hash.to_scalar().unwrap() + } else { + rlc::value(&bytecode.rows[0].code_hash.to_le_bytes(), challenge) + } + }); for (idx, row) in bytecode.rows.iter().enumerate() { if fail_fast && *offset > last_row_offset { @@ -891,11 +903,9 @@ impl Circuit for BytecodeCircuit { &challenges, )?; #[cfg(feature = "poseidon-codehash")] - config.poseidon_table.dev_load( - &mut layouter, - self.bytecodes.iter().map(|b| &b.bytes), - &challenges, - )?; + config + .poseidon_table + .dev_load(&mut layouter, self.bytecodes.iter().map(|b| &b.bytes))?; self.synthesize_sub(&config, &challenges, &mut layouter)?; Ok(()) } @@ -1044,7 +1054,6 @@ mod tests { } /// Test invalid code_hash data - #[cfg(feature = "fixme")] #[test] fn bytecode_invalid_hash_data() { let k = 9; @@ -1088,8 +1097,6 @@ mod tests { } /// Test invalid byte data - - #[cfg(feature = "fixme")] #[test] fn bytecode_invalid_byte_data() { let k = 9; diff --git a/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs b/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs index 79fe2313bb..6359f76dbd 100644 --- a/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs +++ b/zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs @@ -1,17 +1,18 @@ use crate::{ - evm_circuit::util::{and, constraint_builder::BaseConstraintBuilder, not, or, rlc, select}, + evm_circuit::util::{and, constraint_builder::BaseConstraintBuilder, not, or, select}, table::{BytecodeFieldTag, KeccakTable, PoseidonTable}, util::{Challenges, Expr, SubCircuitConfig}, }; -use bus_mapping::state_db::EMPTY_CODE_HASH_LE; -use eth_types::Field; +use bus_mapping::util::POSEIDON_CODE_HASH_ZERO; +use eth_types::{Field, ToScalar, ToWord}; use gadgets::is_zero::IsZeroChip; use halo2_proofs::{ circuit::{Layouter, Region, Value}, - plonk::{Advice, Column, ConstraintSystem, Error, VirtualCells}, + plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells}, poly::Rotation, }; use log::trace; +use mpt_zktrie::hash::HASHABLE_DOMAIN_SPEC; use std::vec; use super::{ @@ -52,8 +53,7 @@ impl ToHashBlockCircuitConfig Self { let base_conf_cl = base_conf.clone(); let bytecode_table = base_conf.bytecode_table; - // TODO: does this col still used for storing poseidon hash? - // let code_hash = bytecode_table.code_hash; + let code_hash = bytecode_table.code_hash; let q_enable = base_conf.q_enable; // from 0 to last avaliable row @@ -287,11 +287,10 @@ impl ToHashBlockCircuitConfig>::advice_columns(&poseidon_table); - [/* cols[0], */ cols[inp_i + 1], cols[cols.len() - 2]] + [cols[0], cols[inp_i + 1], cols[cols.len() - 2]] }; // we use a special selection exp for only 2 indexs @@ -300,11 +299,12 @@ impl ToHashBlockCircuitConfig ToHashBlockCircuitConfig ToHashBlockCircuitConfig ToHashBlockCircuitConfig ToHashBlockCircuitConfig(v: &NumberOrHash, challenge: Value) -> Value { match v { NumberOrHash::Number(n) => Value::known(F::from(*n as u64)), @@ -52,7 +53,15 @@ pub fn number_or_hash_to_field(v: &NumberOrHash, challenge: Value) b.reverse(); b }; - challenge.map(|challenge| rlc::value(&le_bytes, challenge)) + #[cfg(feature = "scroll")] + { + // use poseidon codehash fr + return challenge.map(|_challenge| rlc::value(&le_bytes, 0x100u64.into())); + } + #[cfg(not(feature = "scroll"))] + { + return challenge.map(|challenge| rlc::value(&le_bytes, challenge)); + } } } } diff --git a/zkevm-circuits/src/evm_circuit/execution/balance.rs b/zkevm-circuits/src/evm_circuit/execution/balance.rs index a806dd3833..e410177fdb 100644 --- a/zkevm-circuits/src/evm_circuit/execution/balance.rs +++ b/zkevm-circuits/src/evm_circuit/execution/balance.rs @@ -128,9 +128,9 @@ impl ExecutionGadget for BalanceGadget { let code_hash = block.rws[step.rw_indices[5]].account_value_pair().0; self.code_hash - .assign(region, offset, region.word_rlc(code_hash))?; + .assign(region, offset, region.code_hash(code_hash))?; self.not_exists - .assign_value(region, offset, region.word_rlc(code_hash))?; + .assign_value(region, offset, region.code_hash(code_hash))?; let balance = if code_hash.is_zero() { eth_types::Word::zero() } else { diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index c467ed5596..0a2d4e14d1 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -648,7 +648,7 @@ impl ExecutionGadget for BeginTxGadget { tx_fee, )?; self.phase2_code_hash - .assign(region, offset, region.word_rlc(callee_code_hash))?; + .assign(region, offset, region.code_hash(callee_code_hash))?; let untrimmed_contract_addr = { let mut stream = RlpStream::new(); stream.begin_list(2); @@ -668,11 +668,11 @@ impl ExecutionGadget for BeginTxGadget { self.is_empty_code_hash.assign_value( region, offset, - region.word_rlc(callee_code_hash), + region.code_hash(callee_code_hash), region.empty_code_hash_rlc(), )?; self.callee_not_exists - .assign_value(region, offset, region.word_rlc(callee_code_hash))?; + .assign_value(region, offset, region.code_hash(callee_code_hash))?; let untrimmed_contract_addr = { let mut stream = ethers_core::utils::rlp::RlpStream::new(); diff --git a/zkevm-circuits/src/evm_circuit/execution/callop.rs b/zkevm-circuits/src/evm_circuit/execution/callop.rs index 93f4c40e73..97f60f6859 100644 --- a/zkevm-circuits/src/evm_circuit/execution/callop.rs +++ b/zkevm-circuits/src/evm_circuit/execution/callop.rs @@ -694,7 +694,7 @@ impl ExecutionGadget for CallOpGadget { rd_offset, rd_length, step.memory_word_size(), - region.word_rlc(callee_code_hash), + region.code_hash(callee_code_hash), )?; self.is_warm .assign(region, offset, Value::known(F::from(is_warm as u64)))?; diff --git a/zkevm-circuits/src/evm_circuit/execution/create.rs b/zkevm-circuits/src/evm_circuit/execution/create.rs index 566f1ec3ed..d70fe9af65 100644 --- a/zkevm-circuits/src/evm_circuit/execution/create.rs +++ b/zkevm-circuits/src/evm_circuit/execution/create.rs @@ -534,7 +534,7 @@ impl ExecutionGadget< let code_hash_previous = block.rws [step.rw_indices[13 + usize::from(is_create2) + copy_rw_increase]] .account_codehash_pair(); - let code_hash_previous_rlc = region.word_rlc(code_hash_previous.0); + let code_hash_previous_rlc = region.code_hash(code_hash_previous.0); self.code_hash_previous .assign(region, offset, code_hash_previous_rlc)?; self.not_address_collision diff --git a/zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs b/zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs index 7e1cdbc934..8d152d1d04 100644 --- a/zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs +++ b/zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs @@ -171,7 +171,7 @@ impl ExecutionGadget for ErrorOOGCallGadget { rd_offset, rd_length, step.memory_word_size(), - region.word_rlc(callee_code_hash), + region.code_hash(callee_code_hash), )?; self.opcode diff --git a/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs b/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs index 4fc6633912..72d325067d 100644 --- a/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs +++ b/zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs @@ -191,7 +191,7 @@ impl ExecutionGadget for ExtcodecopyGadget { let code_hash = block.rws[step.rw_indices[8]].account_value_pair().0; self.code_hash - .assign(region, offset, region.word_rlc(code_hash))?; + .assign(region, offset, region.code_hash(code_hash))?; let code_size = if code_hash.is_zero() { 0 diff --git a/zkevm-circuits/src/evm_circuit/execution/extcodesize.rs b/zkevm-circuits/src/evm_circuit/execution/extcodesize.rs index 4c3c2b300e..3dcf9e9202 100644 --- a/zkevm-circuits/src/evm_circuit/execution/extcodesize.rs +++ b/zkevm-circuits/src/evm_circuit/execution/extcodesize.rs @@ -141,9 +141,9 @@ impl ExecutionGadget for ExtcodesizeGadget { let code_hash = block.rws[step.rw_indices[5]].account_value_pair().0; self.code_hash - .assign(region, offset, region.word_rlc(code_hash))?; + .assign(region, offset, region.code_hash(code_hash))?; self.not_exists - .assign_value(region, offset, region.word_rlc(code_hash))?; + .assign_value(region, offset, region.code_hash(code_hash))?; let rw_offset = 6; #[cfg(feature = "scroll")] diff --git a/zkevm-circuits/src/evm_circuit/execution/return_revert.rs b/zkevm-circuits/src/evm_circuit/execution/return_revert.rs index 328330f5f4..39c287edcd 100644 --- a/zkevm-circuits/src/evm_circuit/execution/return_revert.rs +++ b/zkevm-circuits/src/evm_circuit/execution/return_revert.rs @@ -355,7 +355,7 @@ impl ExecutionGadget for ReturnRevertGadget { self.code_hash.assign( region, offset, - region.word_rlc(U256::from_little_endian(&code_hash)), + region.code_hash(U256::from_little_endian(&code_hash)), )?; // code size. diff --git a/zkevm-circuits/src/evm_circuit/step.rs b/zkevm-circuits/src/evm_circuit/step.rs index a250d364a6..95d87b1221 100644 --- a/zkevm-circuits/src/evm_circuit/step.rs +++ b/zkevm-circuits/src/evm_circuit/step.rs @@ -588,7 +588,7 @@ impl Step { .assign(region, offset, Value::known(F::from(step.block_num)))?; self.state .code_hash - .assign(region, offset, region.word_rlc(call.code_hash))?; + .assign(region, offset, region.code_hash(call.code_hash))?; self.state.program_counter.assign( region, offset, diff --git a/zkevm-circuits/src/evm_circuit/util.rs b/zkevm-circuits/src/evm_circuit/util.rs index 2491f8972d..2dae41a852 100644 --- a/zkevm-circuits/src/evm_circuit/util.rs +++ b/zkevm-circuits/src/evm_circuit/util.rs @@ -196,6 +196,17 @@ impl<'r, 'b, F: FieldExt> CachedRegion<'r, 'b, F> { .map(|r| rlc::value(&n.to_le_bytes(), r)) } + pub fn code_hash(&self, n: U256) -> Value { + self.challenges.evm_word().map(|r| { + if cfg!(feature = "poseidon-codehash") { + // only FieldExt is not enough for ToScalar trait so we have to make workaround + rlc::value(&n.to_le_bytes(), F::from(256u64)) + } else { + rlc::value(&n.to_le_bytes(), r) + } + }) + } + pub fn keccak_rlc(&self, le_bytes: &[u8]) -> Value { self.challenges .keccak_input() @@ -203,7 +214,7 @@ impl<'r, 'b, F: FieldExt> CachedRegion<'r, 'b, F> { } pub fn empty_code_hash_rlc(&self) -> Value { - self.word_rlc(CodeDB::empty_code_hash().to_word()) + self.code_hash(CodeDB::empty_code_hash().to_word()) } /// Constrains a cell to have a constant value. diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs index 5b289536ab..56474b4fa5 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs @@ -257,7 +257,7 @@ impl RestoreContextGadget { } self.caller_code_hash - .assign(region, offset, region.word_rlc(caller_code_hash))?; + .assign(region, offset, region.code_hash(caller_code_hash))?; Ok(()) } diff --git a/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs b/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs index 990e264bb7..4542a99e43 100644 --- a/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs +++ b/zkevm-circuits/src/evm_circuit/util/constraint_builder.rs @@ -11,8 +11,11 @@ use crate::{ }, util::{build_tx_log_expression, Challenges, Expr}, }; -use bus_mapping::{state_db::EMPTY_CODE_HASH_LE, util::KECCAK_CODE_HASH_ZERO}; -use eth_types::{Field, ToLittleEndian, ToWord}; +use bus_mapping::{ + state_db::EMPTY_CODE_HASH_LE, + util::{KECCAK_CODE_HASH_ZERO, POSEIDON_CODE_HASH_ZERO}, +}; +use eth_types::{Field, ToLittleEndian, ToScalar, ToWord}; use gadgets::util::{and, not}; use halo2_proofs::{ circuit::Value, @@ -470,7 +473,11 @@ impl<'a, F: Field> ConstraintBuilder<'a, F> { } pub(crate) fn empty_code_hash_rlc(&self) -> Expression { - self.word_rlc((*EMPTY_CODE_HASH_LE).map(|byte| byte.expr())) + if cfg!(feature = "poseidon-codehash") { + Expression::Constant(POSEIDON_CODE_HASH_ZERO.to_word().to_scalar().unwrap()) + } else { + self.word_rlc((*EMPTY_CODE_HASH_LE).map(|byte| byte.expr())) + } } // Common diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs index c812a70095..e3e3520c85 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs @@ -269,7 +269,7 @@ impl ContractCreateGadget { self.code_hash_rlc.assign( region, offset, - region.word_rlc(code_hash.unwrap_or_default()), + region.code_hash(code_hash.unwrap_or_default()), )?; for (c, v) in self .salt diff --git a/zkevm-circuits/src/poseidon_circuit.rs b/zkevm-circuits/src/poseidon_circuit.rs index a3bd81cdd3..479f017f8c 100644 --- a/zkevm-circuits/src/poseidon_circuit.rs +++ b/zkevm-circuits/src/poseidon_circuit.rs @@ -116,21 +116,21 @@ impl SubCircuit for PoseidonCircuit { fn synthesize_sub( &self, config: &Self::Config, - challenges: &Challenges>, + _challenges: &Challenges>, layouter: &mut impl Layouter, ) -> Result<(), Error> { // for single codehash we sitll use keccak256(nil) - use crate::evm_circuit::util::rlc; - let empty_hash = challenges - .evm_word() - .map(|challenge| rlc::value(CodeDB::empty_code_hash().as_ref(), challenge)); + use eth_types::{ToScalar, ToWord}; + // Note the Option(nil_hash) in construct has different meanings as the returning of + // `to_scalar` so we should not use the returning option here + let empty_hash = CodeDB::empty_code_hash().to_word().to_scalar().unwrap(); let chip = PoseidonHashChip::<_, HASH_BLOCK_STEP_SIZE>::construct( config.0.clone(), &self.0, self.1, false, - crate::test_util::escape_value(empty_hash), + Some(empty_hash), ); chip.load(layouter) diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index ca2179f190..c50d054d8c 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -13,7 +13,7 @@ use crate::{ }; use bus_mapping::circuit_input_builder::{CopyDataType, CopyEvent, CopyStep, ExpEvent}; use core::iter::once; -use eth_types::{Field, ToLittleEndian, ToScalar, Word, U256}; +use eth_types::{Field, ToLittleEndian, ToScalar, ToWord, Word, U256}; use gadgets::{ binary_number::{BinaryNumberChip, BinaryNumberConfig}, util::{split_u256, split_u256_limb64}, @@ -792,11 +792,12 @@ impl PoseidonTable { &self, layouter: &mut impl Layouter, inputs: impl IntoIterator> + Clone, - challenges: &Challenges>, ) -> Result<(), Error> { use crate::bytecode_circuit::bytecode_unroller::{ unroll_to_hash_input_default, HASHBLOCK_BYTES_IN_FIELD, }; + use bus_mapping::state_db::CodeDB; + use mpt_zktrie::hash::HASHABLE_DOMAIN_SPEC; layouter.assign_region( || "poseidon table", @@ -813,7 +814,8 @@ impl PoseidonTable { )?; } offset += 1; - let nil_hash = KeccakTable::assignments(&[], challenges)[0][3]; + let nil_hash = + Value::known(CodeDB::empty_code_hash().to_word().to_scalar().unwrap()); for (column, value) in poseidon_table_columns .iter() .copied() @@ -831,7 +833,12 @@ impl PoseidonTable { for input in inputs.clone() { let mut control_len = input.len(); let mut first_row = true; - let ref_hash = KeccakTable::assignments(input, challenges)[0][3]; + let ref_hash = Value::known( + CodeDB::hash(input.as_slice()) + .to_word() + .to_scalar() + .unwrap(), + ); for row in unroll_to_hash_input_default::(input.iter().copied()) { assert_ne!( control_len, @@ -840,11 +847,13 @@ impl PoseidonTable { input.len() ); let block_size = HASHBLOCK_BYTES_IN_FIELD * row.len(); + let control_len_as_flag = + F::from_u128(HASHABLE_DOMAIN_SPEC * control_len as u128); for (column, value) in poseidon_table_columns.iter().zip_eq( once(ref_hash) .chain(row.map(Value::known)) - .chain(once(Value::known(F::from(control_len as u64)))) + .chain(once(Value::known(control_len_as_flag))) .chain(once(Value::known(if first_row { F::one() } else { diff --git a/zkevm-circuits/src/witness/bytecode.rs b/zkevm-circuits/src/witness/bytecode.rs index a94581745a..d9a1fc3e0d 100644 --- a/zkevm-circuits/src/witness/bytecode.rs +++ b/zkevm-circuits/src/witness/bytecode.rs @@ -21,9 +21,16 @@ impl Bytecode { ) -> Vec<[Value; 5]> { let n = 1 + self.bytes.len(); let mut rows = Vec::with_capacity(n); - let hash = challenges - .evm_word() - .map(|challenge| rlc::value(&self.hash.to_le_bytes(), challenge)); + let hash = if cfg!(feature = "poseidon-codehash") { + challenges + .evm_word() + .map(|_challenge| rlc::value(&self.hash.to_le_bytes(), F::from(256u64))) + //Value::known(rlc::value(&self.hash.to_le_bytes(), F::from(256u64))) + } else { + challenges + .evm_word() + .map(|challenge| rlc::value(&self.hash.to_le_bytes(), challenge)) + }; rows.push([ hash, diff --git a/zkevm-circuits/src/witness/mpt.rs b/zkevm-circuits/src/witness/mpt.rs index f188c371dd..4572746312 100644 --- a/zkevm-circuits/src/witness/mpt.rs +++ b/zkevm-circuits/src/witness/mpt.rs @@ -191,6 +191,16 @@ impl MptUpdates { impl MptUpdate { pub(crate) fn value_assignments(&self, word_randomness: F) -> (F, F) { let assign = |x: Word| match self.key { + Key::Account { + field_tag: AccountFieldTag::CodeHash, + .. + } => { + if cfg!(feature = "poseidon-codehash") { + x.to_scalar().unwrap() + } else { + rlc::value(&x.to_le_bytes(), word_randomness) + } + } Key::Account { field_tag: AccountFieldTag::Nonce | AccountFieldTag::NonExisting | AccountFieldTag::CodeSize, diff --git a/zkevm-circuits/src/witness/rw.rs b/zkevm-circuits/src/witness/rw.rs index afe79db318..c7d0b1943d 100644 --- a/zkevm-circuits/src/witness/rw.rs +++ b/zkevm-circuits/src/witness/rw.rs @@ -593,19 +593,31 @@ impl Rw { } => { match field_tag { // Only these two tags have values that may not fit into a scalar, so we need to - // RLC. - CallContextFieldTag::CodeHash | CallContextFieldTag::Value => { - rlc::value(&value.to_le_bytes(), randomness) + // RLC. (for poseidon hash feature, CodeHash not need rlc) + CallContextFieldTag::CodeHash => { + if cfg!(feature = "poseidon-codehash") { + value.to_scalar().unwrap() + } else { + rlc::value(&value.to_le_bytes(), randomness) + } } + CallContextFieldTag::Value => rlc::value(&value.to_le_bytes(), randomness), _ => value.to_scalar().unwrap(), } } Self::Account { value, field_tag, .. } => match field_tag { - AccountFieldTag::KeccakCodeHash - | AccountFieldTag::Balance - | AccountFieldTag::CodeHash => rlc::value(&value.to_le_bytes(), randomness), + AccountFieldTag::KeccakCodeHash | AccountFieldTag::Balance => { + rlc::value(&value.to_le_bytes(), randomness) + } + AccountFieldTag::CodeHash => { + if cfg!(feature = "poseidon-codehash") { + value.to_scalar().unwrap() + } else { + rlc::value(&value.to_le_bytes(), randomness) + } + } AccountFieldTag::Nonce | AccountFieldTag::NonExisting | AccountFieldTag::CodeSize => value.to_scalar().unwrap(), @@ -635,9 +647,16 @@ impl Rw { field_tag, .. } => Some(match field_tag { - AccountFieldTag::KeccakCodeHash - | AccountFieldTag::Balance - | AccountFieldTag::CodeHash => rlc::value(&value_prev.to_le_bytes(), randomness), + AccountFieldTag::KeccakCodeHash | AccountFieldTag::Balance => { + rlc::value(&value_prev.to_le_bytes(), randomness) + } + AccountFieldTag::CodeHash => { + if cfg!(feature = "poseidon-codehash") { + value_prev.to_scalar().unwrap() + } else { + rlc::value(&value_prev.to_le_bytes(), randomness) + } + } AccountFieldTag::Nonce | AccountFieldTag::NonExisting | AccountFieldTag::CodeSize => value_prev.to_scalar().unwrap(),