From 75d64e7db592046cac9cc95712049f2c46e9ad25 Mon Sep 17 00:00:00 2001 From: "sm.wu" Date: Fri, 19 May 2023 16:41:09 +0800 Subject: [PATCH] account address optimisation --- .../src/evm_circuit/execution/address.rs | 15 ++++++----- .../src/evm_circuit/execution/caller.rs | 13 ++++----- .../util/math_gadget/is_equal_word.rs | 25 ++++++++++++++--- .../util/math_gadget/is_zero_word.rs | 27 ++++++++++++++++--- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/address.rs b/zkevm-circuits/src/evm_circuit/execution/address.rs index 5a3d1bfb6d..ce0e6552a1 100644 --- a/zkevm-circuits/src/evm_circuit/execution/address.rs +++ b/zkevm-circuits/src/evm_circuit/execution/address.rs @@ -1,17 +1,20 @@ use crate::{ evm_circuit::{ execution::ExecutionGadget, - param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_HALF_WORD}, + param::N_BYTES_ACCOUNT_ADDRESS, step::ExecutionState, util::{ common_gadget::SameContextGadget, constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta}, - AccountAddress, CachedRegion, + CachedRegion, }, witness::{Block, Call, ExecStep, Transaction}, }, table::CallContextFieldTag, - util::{word::WordExpr, Expr}, + util::{ + word::{WordCell, WordExpr}, + Expr, + }, }; use bus_mapping::evm::OpcodeId; use eth_types::{Field, ToAddress, ToLittleEndian}; @@ -20,7 +23,7 @@ use halo2_proofs::plonk::Error; #[derive(Clone, Debug)] pub(crate) struct AddressGadget { same_context: SameContextGadget, - address: AccountAddress, + address: WordCell, } impl ExecutionGadget for AddressGadget { @@ -29,9 +32,7 @@ impl ExecutionGadget for AddressGadget { const EXECUTION_STATE: ExecutionState = ExecutionState::ADDRESS; fn configure(cb: &mut EVMConstraintBuilder) -> Self { - // TODO switch to query_word_unchecked once callcontext -> XXXaddress encoded to Word2 type - // properly refer discussion thread https://github.com/privacy-scaling-explorations/zkevm-circuits/pull/1414/files#r1197845688 - let address = cb.query_account_address(); + let address = cb.query_word_unchecked(); // Lookup callee address in call context. cb.call_context_lookup_read(None, CallContextFieldTag::CalleeAddress, address.to_word()); diff --git a/zkevm-circuits/src/evm_circuit/execution/caller.rs b/zkevm-circuits/src/evm_circuit/execution/caller.rs index 095572c11e..1c4cdc75dd 100644 --- a/zkevm-circuits/src/evm_circuit/execution/caller.rs +++ b/zkevm-circuits/src/evm_circuit/execution/caller.rs @@ -6,12 +6,15 @@ use crate::{ util::{ common_gadget::SameContextGadget, constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta}, - AccountAddress, CachedRegion, + CachedRegion, }, witness::{Block, Call, ExecStep, Transaction}, }, table::CallContextFieldTag, - util::{word::WordExpr, Expr}, + util::{ + word::{WordCell, WordExpr}, + Expr, + }, }; use bus_mapping::evm::OpcodeId; use eth_types::{Field, ToLittleEndian}; @@ -21,7 +24,7 @@ use halo2_proofs::plonk::Error; pub(crate) struct CallerGadget { same_context: SameContextGadget, // Using RLC to match against rw_table->stack_op value - caller_address: AccountAddress, + caller_address: WordCell, } impl ExecutionGadget for CallerGadget { @@ -30,9 +33,7 @@ impl ExecutionGadget for CallerGadget { const EXECUTION_STATE: ExecutionState = ExecutionState::CALLER; fn configure(cb: &mut EVMConstraintBuilder) -> Self { - // TODO switch to query_word_unchecked once callcontext -> XXXaddress encoded to Word2 type - // properly refer discussion thread https://github.com/privacy-scaling-explorations/zkevm-circuits/pull/1414/files#r1197845688 - let caller_address = cb.query_account_address(); + let caller_address = cb.query_word_unchecked(); // Lookup rw_table -> call_context with caller address cb.call_context_lookup_read( diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs index 8d33e232c6..ad28ea537f 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use eth_types::Field; +use eth_types::{Field, ToLittleEndian}; use gadgets::util::and; use halo2_proofs::{ circuit::Value, @@ -8,8 +8,11 @@ use halo2_proofs::{ }; use crate::{ - evm_circuit::util::{ - constraint_builder::EVMConstraintBuilder, transpose_val_ret, CachedRegion, + evm_circuit::{ + param::N_BYTES_HALF_WORD, + util::{ + constraint_builder::EVMConstraintBuilder, from_bytes, transpose_val_ret, CachedRegion, + }, }, util::word::{Word, WordExpr}, }; @@ -68,6 +71,22 @@ impl, T2: WordExpr> IsEqualWordGadget { .map(|(lhs, rhs)| self.assign(region, offset, lhs, rhs)), ) } + + pub(crate) fn assign_u256( + &self, + region: &mut CachedRegion<'_, '_, F>, + offset: usize, + lhs: eth_types::Word, + rhs: eth_types::Word, + ) -> Result { + let lhs_lo = from_bytes::value::(&lhs.to_le_bytes()[0..N_BYTES_HALF_WORD]); + let lhs_hi = from_bytes::value::(&lhs.to_le_bytes()[N_BYTES_HALF_WORD..]); + let rhs_lo = from_bytes::value::(&rhs.to_le_bytes()[0..N_BYTES_HALF_WORD]); + let rhs_hi = from_bytes::value::(&rhs.to_le_bytes()[N_BYTES_HALF_WORD..]); + self.is_zero_lo.assign(region, offset, rhs_lo - lhs_lo)?; + self.is_zero_hi.assign(region, offset, rhs_hi - lhs_hi)?; + Ok(F::from(2)) + } } // TODO add unittest diff --git a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero_word.rs b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero_word.rs index 7a0f692c59..9fc58013a0 100644 --- a/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero_word.rs +++ b/zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero_word.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use eth_types::Field; +use eth_types::{Field, ToLittleEndian}; use gadgets::util::{or, Expr}; use halo2_proofs::{ circuit::Value, @@ -8,9 +8,12 @@ use halo2_proofs::{ }; use crate::{ - evm_circuit::util::{ - constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder}, - transpose_val_ret, CachedRegion, Cell, CellType, + evm_circuit::{ + param::N_BYTES_HALF_WORD, + util::{ + constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder}, + from_bytes, transpose_val_ret, CachedRegion, Cell, CellType, + }, }, util::word::{Word, WordExpr}, }; @@ -85,6 +88,22 @@ impl> IsZeroWordGadget { }) } + pub(crate) fn assign_u256( + &self, + region: &mut CachedRegion<'_, '_, F>, + offset: usize, + value: eth_types::Word, + ) -> Result { + self.assign( + region, + offset, + Word::new([ + from_bytes::value(&value.to_le_bytes()[0..N_BYTES_HALF_WORD]), + from_bytes::value(&value.to_le_bytes()[N_BYTES_HALF_WORD..]), + ]), + ) + } + pub(crate) fn assign_value( &self, region: &mut CachedRegion<'_, '_, F>,