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

Commit

Permalink
fix evm circuit compiling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Sung-Ming committed May 17, 2023
1 parent 1897888 commit a8f84d8
Show file tree
Hide file tree
Showing 57 changed files with 911 additions and 767 deletions.
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/addmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) struct AddModGadget<F> {
sum_areduced_b_overflow: Word32Cell<F>,
muladd_d_n_r: MulAddWords512Gadget<F>,

n_is_zero: IsZeroWordGadget<F>,
n_is_zero: IsZeroWordGadget<F, Word32Cell<F>>,
cmp_r_n: CmpWordsGadget<F, Word32Cell<F>, Word32Cell<F>>,
cmp_areduced_n: CmpWordsGadget<F, Word32Cell<F>, Word32Cell<F>>,
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<F: Field> ExecutionGadget<F> for AddModGadget<F> {
cb.require_equal_word(
"check a_reduced + b 512 bit carry if n != 0",
sum_areduced_b_overflow.to_word(),
Word::from_lo_unchecked(sum_areduced_b.carry())
Word::from_lo_unchecked(sum_areduced_b.carry().unwrap().expr())
.mul_selector(not::expr(n_is_zero.expr())),
);

Expand Down
30 changes: 18 additions & 12 deletions zkevm-circuits/src/evm_circuit/execution/balance.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_ACCOUNT_ADDRESS,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{
ConstrainBuilderCommon, EVMConstraintBuilder, ReversionInfo, StepStateTransition,
Transition::Delta,
},
math_gadget::{IsZeroGadget, IsZeroWordGadget},
math_gadget::IsZeroWordGadget,
not, select, AccountAddress, CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
},
table::{AccountFieldTag, CallContextFieldTag},
util::{
word::{Word32Cell, WordExpr},
word::{Word, Word32Cell, WordExpr},
Expr,
},
};
Expand All @@ -30,8 +31,8 @@ pub(crate) struct BalanceGadget<F> {
tx_id: Cell<F>,
is_warm: Cell<F>,
code_hash: Word32Cell<F>,
not_exists: IsZeroGadget<F>,
balance: Cell<F>,
not_exists: IsZeroWordGadget<F, Word32Cell<F>>,
balance: Word32Cell<F>,
}

impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
Expand All @@ -49,8 +50,8 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
cb.account_access_list_write(
tx_id.expr(),
address.expr(),
1.expr(),
is_warm.expr(),
Word::from_lo_unchecked(1.expr()),
Word::from_lo_unchecked(is_warm.expr()),
Some(&mut reversion_info),
);
let code_hash = cb.query_word32();
Expand All @@ -60,7 +61,7 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
AccountFieldTag::CodeHash,
code_hash.to_word(),
);
let not_exists = IsZeroWordGadget::construct(cb, code_hash.to_word());
let not_exists = IsZeroWordGadget::construct(cb, code_hash);
let exists = not::expr(not_exists.expr());
let balance = cb.query_word32();
cb.condition(exists.expr(), |cb| {
Expand Down Expand Up @@ -114,8 +115,13 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
self.same_context.assign_exec_step(region, offset, step)?;

let address = block.rws[step.rw_indices[0]].stack_value();
self.address_word
.assign(region, offset, Some(address.to_le_bytes()))?;
self.address.assign(
region,
offset,
(address.to_le_bytes()[0..N_BYTES_ACCOUNT_ADDRESS])
.try_into()
.ok(),
)?;

self.tx_id
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;
Expand All @@ -133,16 +139,16 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {

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, Some(code_hash.to_le_bytes()))?;
self.not_exists
.assign_value(region, offset, region.word_rlc(code_hash))?;
.assign_value(region, offset, Value::known(Word::from_u256(code_hash)))?;
let balance = if code_hash.is_zero() {
eth_types::Word::zero()
} else {
block.rws[step.rw_indices[6]].account_value_pair().0
};
self.balance
.assign(region, offset, region.word_rlc(balance))?;
.assign(region, offset, Some(balance.to_le_bytes()))?;

Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
let call_id = cb.curr.state.rw_counter.clone();

let tx_id = cb.query_cell();
cb.call_context_lookup(
1.expr(),
cb.call_context_lookup_write_unchecked(
Some(call_id.expr()),
CallContextFieldTag::TxId,
tx_id.expr(),
Expand Down
5 changes: 4 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
},
witness::{Block, Call, ExecStep, Transaction},
},
util::{word::Word32Cell, Expr},
util::{
word::{Word32Cell, WordExpr},
Expr,
},
};
use eth_types::{evm_types::OpcodeId, Field, ToLittleEndian};
use halo2_proofs::plonk::Error;
Expand Down
31 changes: 13 additions & 18 deletions zkevm-circuits/src/evm_circuit/execution/block_ctx.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_U64, N_BYTES_WORD},
param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_HALF_WORD, N_BYTES_U64, N_BYTES_WORD},
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
from_bytes, CachedRegion, RandomLinearCombination,
CachedRegion,
},
witness::{Block, Call, ExecStep, Transaction},
},
table::BlockContextFieldTag,
util::Expr,
util::{
word::{WordCell, WordExpr},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToLittleEndian};
Expand All @@ -20,15 +23,15 @@ use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct BlockCtxGadget<F, const N_BYTES: usize> {
same_context: SameContextGadget<F>,
value: RandomLinearCombination<F, N_BYTES>,
value: WordCell<F>,
}

impl<F: Field, const N_BYTES: usize> BlockCtxGadget<F, N_BYTES> {
fn construct(cb: &mut EVMConstraintBuilder<F>) -> Self {
let value = cb.query_word_rlc();
let value = cb.query_word_unchecked(); // block lookup below

// Push the const generic parameter N_BYTES value to the stack
cb.stack_push(value.expr());
cb.stack_push(value.to_word());

// Get op's FieldTag
let opcode = cb.query_cell();
Expand All @@ -37,12 +40,7 @@ impl<F: Field, const N_BYTES: usize> BlockCtxGadget<F, N_BYTES> {

// Lookup block table with block context ops
// TIMESTAMP/NUMBER/GASLIMIT, COINBASE and DIFFICULTY/BASEFEE
let value_expr = if N_BYTES == N_BYTES_WORD {
value.expr()
} else {
from_bytes::expr(&value.cells)
};
cb.block_lookup(blockctx_tag, None, value_expr);
cb.block_lookup(blockctx_tag, None, value.to_word());

// State transition
let step_state_transition = StepStateTransition {
Expand Down Expand Up @@ -133,14 +131,11 @@ impl<F: Field> ExecutionGadget<F> for BlockCtxU160Gadget<F> {

let value = block.rws[step.rw_indices[0]].stack_value();

self.value_u160.value.assign(
self.value_u160.value.assign_lo_hi(
region,
offset,
Some(
value.to_le_bytes()[..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.unwrap(),
),
value.to_le_bytes()[..N_BYTES_HALF_WORD].try_into().ok(),
value.to_le_bytes()[N_BYTES_HALF_WORD..].try_into().ok(),
)?;

Ok(())
Expand Down
13 changes: 8 additions & 5 deletions zkevm-circuits/src/evm_circuit/execution/blockhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
table::BlockContextFieldTag,
util::{word::WordCell, Expr},
util::{
word::{Word, WordCell, WordExpr},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToLittleEndian, ToScalar};
Expand All @@ -42,11 +45,11 @@ impl<F: Field> ExecutionGadget<F> for BlockHashGadget<F> {
cb.block_lookup(
BlockContextFieldTag::Number.expr(),
None,
current_block_number.expr(),
Word::from_lo_unchecked(current_block_number.expr()),
);

let block_number = WordByteCapGadget::construct(cb, current_block_number.expr());
cb.stack_pop(block_number.original_word());
cb.stack_pop(block_number.original_word().to_word());

let block_hash = cb.query_word_unchecked();

Expand All @@ -67,9 +70,9 @@ impl<F: Field> ExecutionGadget<F> for BlockHashGadget<F> {
});

cb.condition(not::expr(is_valid), |cb| {
cb.require_zero(
cb.require_zero_word(
"Invalid block number for block hash lookup",
block_hash.expr(),
block_hash.to_word(),
);
});

Expand Down
27 changes: 15 additions & 12 deletions zkevm-circuits/src/evm_circuit/execution/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use crate::{
common_gadget::SameContextGadget,
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
math_gadget::{IsEqualGadget, IsZeroGadget},
sum, CachedRegion, Word,
sum, CachedRegion,
},
witness::{Block, Call, ExecStep, Transaction},
},
util::Expr,
util::{
word::{Word, Word32Cell, WordExpr},
Expr,
},
};
use array_init::array_init;
use bus_mapping::evm::OpcodeId;
Expand All @@ -20,8 +23,8 @@ use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct ByteGadget<F> {
same_context: SameContextGadget<F>,
index: Word<F>,
value: Word<F>,
index: Word32Cell<F>,
value: Word32Cell<F>,
is_msb_sum_zero: IsZeroGadget<F>,
is_byte_selected: [IsEqualGadget<F>; 32],
}
Expand All @@ -32,14 +35,14 @@ impl<F: Field> ExecutionGadget<F> for ByteGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::BYTE;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let index = cb.query_word_rlc();
let value = cb.query_word_rlc();
let index = cb.query_word32();
let value = cb.query_word32();

// If any of the non-LSB bytes of the index word are non-zero we never
// need to copy any bytes. So just sum all the non-LSB byte
// values here and then check if it's non-zero so we can use
// that as an additional condition when to copy the byte value.
let is_msb_sum_zero = IsZeroGadget::construct(cb, sum::expr(&index.cells[1..32]));
let is_msb_sum_zero = IsZeroGadget::construct(cb, sum::expr(&index.limbs[1..32]));

// Now we just need to check that `result[0]` is the sum of all copied
// bytes. We go byte by byte and check if `idx == index[0]`.
Expand All @@ -50,11 +53,11 @@ impl<F: Field> ExecutionGadget<F> for ByteGadget<F> {
let is_byte_selected = array_init(|idx| {
// Check if this byte is selected looking only at the LSB of the
// index word
IsEqualGadget::construct(cb, index.cells[0].expr(), (31 - idx).expr())
IsEqualGadget::construct(cb, index.limbs[0].expr(), (31 - idx).expr())
});

// Sum all possible selected bytes
let selected_byte = value.cells.iter().zip(is_byte_selected.iter()).fold(
let selected_byte = value.limbs.iter().zip(is_byte_selected.iter()).fold(
0.expr(),
|acc, (cell, is_selected)| {
acc + is_selected.expr() * is_msb_sum_zero.expr() * cell.expr()
Expand All @@ -65,9 +68,9 @@ impl<F: Field> ExecutionGadget<F> for ByteGadget<F> {
// push the selected byte on the stack
// We can push the selected byte here directly because
// it only uses the LSB of a word.
cb.stack_pop(index.expr());
cb.stack_pop(value.expr());
cb.stack_push(selected_byte);
cb.stack_pop(index.to_word());
cb.stack_pop(value.to_word());
cb.stack_push(Word::from_lo_unchecked(selected_byte));

// State transition
let step_state_transition = StepStateTransition {
Expand Down
41 changes: 22 additions & 19 deletions zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
table::CallContextFieldTag,
util::Expr,
util::{
word::{Word, WordExpr},
Expr,
},
};
use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId};
use eth_types::{evm_types::GasCost, Field, ToScalar};
Expand Down Expand Up @@ -48,48 +51,48 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
let call_data_length = cb.query_cell();
let call_data_offset = cb.query_cell();

let length = cb.query_word_rlc();
let memory_offset = cb.query_cell_phase2();
let length = cb.query_memory_address();
let memory_offset = cb.query_word_unchecked();
let data_offset = WordByteCapGadget::construct(cb, call_data_length.expr());

// Pop memory_offset, data_offset, length from stack
cb.stack_pop(memory_offset.expr());
cb.stack_pop(data_offset.original_word());
cb.stack_pop(length.expr());
cb.stack_pop(memory_offset.to_word());
cb.stack_pop(data_offset.original_word().to_word());
cb.stack_pop(length.to_word());

// Lookup the calldata_length and caller_address in Tx context table or
// Call context table
cb.condition(cb.curr.state.is_root.expr(), |cb| {
cb.call_context_lookup(false.expr(), None, CallContextFieldTag::TxId, src_id.expr());
cb.call_context_lookup(
false.expr(),
cb.call_context_lookup_read(
None,
CallContextFieldTag::TxId,
Word::from_lo_unchecked(src_id.expr()),
);
cb.call_context_lookup_read(
None,
CallContextFieldTag::CallDataLength,
call_data_length.expr(),
Word::from_lo_unchecked(call_data_length.expr()),
);
cb.require_zero(
"call_data_offset == 0 in the root call",
call_data_offset.expr(),
);
});
cb.condition(1.expr() - cb.curr.state.is_root.expr(), |cb| {
cb.call_context_lookup(
false.expr(),
cb.call_context_lookup_read(
None,
CallContextFieldTag::CallerId,
src_id.expr(),
Word::from_lo_unchecked(src_id.expr()),
);
cb.call_context_lookup(
false.expr(),
cb.call_context_lookup_read(
None,
CallContextFieldTag::CallDataLength,
call_data_length.expr(),
Word::from_lo_unchecked(call_data_length.expr()),
);
cb.call_context_lookup(
false.expr(),
cb.call_context_lookup_read(
None,
CallContextFieldTag::CallDataOffset,
call_data_offset.expr(),
Word::from_lo_unchecked(call_data_offset.expr()),
);
});

Expand Down
Loading

0 comments on commit a8f84d8

Please sign in to comment.