Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
account address optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed May 19, 2023
1 parent 5d3b219 commit 75d64e7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
15 changes: 8 additions & 7 deletions zkevm-circuits/src/evm_circuit/execution/address.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -20,7 +23,7 @@ use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct AddressGadget<F> {
same_context: SameContextGadget<F>,
address: AccountAddress<F>,
address: WordCell<F>,
}

impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
Expand All @@ -29,9 +32,7 @@ impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::ADDRESS;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> 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());
Expand Down
13 changes: 7 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,7 +24,7 @@ use halo2_proofs::plonk::Error;
pub(crate) struct CallerGadget<F> {
same_context: SameContextGadget<F>,
// Using RLC to match against rw_table->stack_op value
caller_address: AccountAddress<F>,
caller_address: WordCell<F>,
}

impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {
Expand All @@ -30,9 +33,7 @@ impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::CALLER;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> 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(
Expand Down
25 changes: 22 additions & 3 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::marker::PhantomData;

use eth_types::Field;
use eth_types::{Field, ToLittleEndian};
use gadgets::util::and;
use halo2_proofs::{
circuit::Value,
plonk::{Error, Expression},
};

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},
};
Expand Down Expand Up @@ -68,6 +71,22 @@ impl<F: Field, T1: WordExpr<F>, T2: WordExpr<F>> IsEqualWordGadget<F, T1, T2> {
.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<F, Error> {
let lhs_lo = from_bytes::value::<F>(&lhs.to_le_bytes()[0..N_BYTES_HALF_WORD]);
let lhs_hi = from_bytes::value::<F>(&lhs.to_le_bytes()[N_BYTES_HALF_WORD..]);
let rhs_lo = from_bytes::value::<F>(&rhs.to_le_bytes()[0..N_BYTES_HALF_WORD]);
let rhs_hi = from_bytes::value::<F>(&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
27 changes: 23 additions & 4 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/is_zero_word.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::marker::PhantomData;

use eth_types::Field;
use eth_types::{Field, ToLittleEndian};
use gadgets::util::{or, Expr};
use halo2_proofs::{
circuit::Value,
plonk::{Error, Expression},
};

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},
};
Expand Down Expand Up @@ -85,6 +88,22 @@ impl<F: Field, T: WordExpr<F>> IsZeroWordGadget<F, T> {
})
}

pub(crate) fn assign_u256(
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
value: eth_types::Word,
) -> Result<F, Error> {
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>,
Expand Down

0 comments on commit 75d64e7

Please sign in to comment.