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

Commit

Permalink
add int decomposition type
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jun 20, 2023
1 parent 91b3a83 commit 9ad99e7
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 157 deletions.
13 changes: 2 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution/balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_ACCOUNT_ADDRESS,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -20,7 +19,7 @@ use crate::{
Expr,
},
};
use eth_types::{evm_types::GasCost, Field, ToLittleEndian, ToWord};
use eth_types::{evm_types::GasCost, Field, ToWord};
use halo2_proofs::{circuit::Value, plonk::Error};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -119,15 +118,7 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
self.same_context.assign_exec_step(region, offset, step)?;

let address = block.get_rws(step, 0).stack_value();
self.address.assign(
region,
offset,
Some(
address.to_le_bytes()[0..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.unwrap(),
),
)?;
self.address.assign_u256(region, offset, address)?;

self.tx_id
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;
Expand Down
13 changes: 2 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use halo2_proofs::{
circuit::Value,
plonk::{Error, Expression},
};
use itertools::Itertools;

#[derive(Clone, Debug)]
pub(crate) struct BeginTxGadget<F> {
Expand Down Expand Up @@ -252,7 +251,6 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
.to_vec()
.try_into()
.unwrap(),
256.expr(),
)
.to_word(),
);
Expand Down Expand Up @@ -554,21 +552,14 @@ impl<F: Field> ExecutionGadget<F> for BeginTxGadget<F> {
)?;
self.tx_callee_address
.assign_h160(region, offset, tx.callee_address)?;
self.call_callee_address.assign(
self.call_callee_address.assign_h160(
region,
offset,
if tx.is_create {
get_contract_address(tx.caller_address, tx.nonce)
} else {
tx.callee_address
}
.to_fixed_bytes()
.iter()
.rev()
.copied()
.collect_vec()
.try_into()
.ok(),
},
)?;
self.is_caller_callee_equal.assign(
region,
Expand Down
1 change: 0 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
.to_vec()
.try_into()
.unwrap(),
256.expr(),
);

// stack operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_GAS, N_BYTES_MEMORY_WORD_SIZE},
param::{N_BYTES_GAS, N_BYTES_MEMORY_WORD_SIZE},
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
Expand All @@ -20,7 +20,7 @@ use crate::{
};
use eth_types::{
evm_types::{GasCost, OpcodeId},
Field, ToLittleEndian, U256,
Field, U256,
};
use halo2_proofs::{circuit::Value, plonk::Error};

Expand Down Expand Up @@ -186,13 +186,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
.assign(region, offset, Value::known(F::from(u64::from(is_warm))))?;
self.tx_id
.assign(region, offset, Value::known(F::from(transaction.id as u64)))?;
self.external_address.assign(
region,
offset,
external_address.to_le_bytes()[0..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.ok(),
)?;
self.external_address
.assign_u256(region, offset, external_address)?;
self.src_offset.assign_u256(region, offset, src_offset)?;
let memory_addr = self
.dst_memory_addr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_ACCOUNT_ADDRESS,
step::ExecutionState,
util::{
common_gadget::CommonErrorGadget,
Expand All @@ -17,7 +16,7 @@ use crate::{
Expr,
},
};
use eth_types::{evm_types::OpcodeId, Field, ToLittleEndian, U256};
use eth_types::{evm_types::OpcodeId, Field, U256};
use halo2_proofs::{circuit::Value, plonk::Error};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -120,13 +119,8 @@ impl<F: Field> ExecutionGadget<F> for ErrorWriteProtectionGadget<F> {
}

self.gas.assign_u256(region, offset, gas)?;
self.code_address.assign(
region,
offset,
code_address.to_le_bytes()[0..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.ok(),
)?;
self.code_address
.assign_u256(region, offset, code_address)?;
self.value.assign_u256(region, offset, value)?;

self.is_call.assign(
Expand Down
1 change: 0 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
.to_vec()
.try_into()
.unwrap(),
256.expr(),
);

let code_size = cb.query_cell();
Expand Down
1 change: 0 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodehashGadget<F> {
.to_vec()
.try_into()
.unwrap(),
256.expr(),
);

let tx_id = cb.call_context(None, CallContextFieldTag::TxId);
Expand Down
1 change: 0 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodesizeGadget<F> {
.to_vec()
.try_into()
.unwrap(),
256.expr(),
);
cb.stack_pop_word(address_word.to_word());

Expand Down
13 changes: 2 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution/jump.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_PROGRAM_COUNTER,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -15,7 +14,7 @@ use crate::{
},
util::{word::WordExpr, Expr},

Check warning on line 15 in zkevm-circuits/src/evm_circuit/execution/jump.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-unknown-unknown

unused import: `word::WordExpr`

Check warning on line 15 in zkevm-circuits/src/evm_circuit/execution/jump.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unused import: `word::WordExpr`

Check warning on line 15 in zkevm-circuits/src/evm_circuit/execution/jump.rs

View workflow job for this annotation

GitHub Actions / Intra-doc links

unused import: `word::WordExpr`
};
use eth_types::{evm_types::OpcodeId, Field, ToLittleEndian};
use eth_types::{evm_types::OpcodeId, Field};
use halo2_proofs::plonk::Error;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -67,15 +66,7 @@ impl<F: Field> ExecutionGadget<F> for JumpGadget<F> {
self.same_context.assign_exec_step(region, offset, step)?;

let destination = block.get_rws(step, 0).stack_value();
self.destination.assign(
region,
offset,
Some(
destination.to_le_bytes()[..N_BYTES_PROGRAM_COUNTER]
.try_into()
.unwrap(),
),
)?;
self.destination.assign_u256(region, offset, destination)?;

Ok(())
}
Expand Down
12 changes: 3 additions & 9 deletions zkevm-circuits/src/evm_circuit/execution/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::{N_BYTES_MEMORY_ADDRESS, N_BYTES_MEMORY_WORD_SIZE},
param::N_BYTES_MEMORY_WORD_SIZE,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -20,7 +20,7 @@ use crate::{
Expr,
},
};
use eth_types::{evm_types::OpcodeId, Field, ToLittleEndian};
use eth_types::{evm_types::OpcodeId, Field};
use halo2_proofs::plonk::Error;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -129,13 +129,7 @@ impl<F: Field> ExecutionGadget<F> for MemoryGadget<F> {

// Inputs/Outputs
let [address, value] = [0, 1].map(|index| block.get_rws(step, index).stack_value());
self.address.assign(
region,
offset,
address.to_le_bytes()[0..N_BYTES_MEMORY_ADDRESS]
.try_into()
.ok(),
)?;
self.address.assign_u256(region, offset, address)?;
self.value.assign_u256(region, offset, value)?;

// Check if this is an MLOAD
Expand Down
11 changes: 2 additions & 9 deletions zkevm-circuits/src/evm_circuit/execution/origin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_ACCOUNT_ADDRESS,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -14,7 +13,7 @@ use crate::{
util::{word::WordExpr, Expr},

Check warning on line 13 in zkevm-circuits/src/evm_circuit/execution/origin.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-unknown-unknown

unused import: `word::WordExpr`

Check warning on line 13 in zkevm-circuits/src/evm_circuit/execution/origin.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unused import: `word::WordExpr`

Check warning on line 13 in zkevm-circuits/src/evm_circuit/execution/origin.rs

View workflow job for this annotation

GitHub Actions / Intra-doc links

unused import: `word::WordExpr`
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToLittleEndian};
use eth_types::Field;
use halo2_proofs::{circuit::Value, plonk::Error};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -79,13 +78,7 @@ impl<F: Field> ExecutionGadget<F> for OriginGadget<F> {
.assign(region, offset, Value::known(F::from(tx.id as u64)))?;

// Assign Origin addr.
self.origin.assign(
region,
offset,
origin.to_le_bytes()[..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.ok(),
)?;
self.origin.assign_u256(region, offset, origin)?;

// Assign SameContextGadget witnesses.
self.same_context.assign_exec_step(region, offset, step)?;
Expand Down
14 changes: 3 additions & 11 deletions zkevm-circuits/src/evm_circuit/execution/returndatacopy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::{N_BYTES_MEMORY_ADDRESS, N_BYTES_MEMORY_WORD_SIZE},
param::N_BYTES_MEMORY_WORD_SIZE,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
Expand All @@ -22,7 +22,7 @@ use crate::{
},
};
use bus_mapping::{circuit_input_builder::CopyDataType, evm::OpcodeId};
use eth_types::{evm_types::GasCost, Field, ToLittleEndian, ToScalar};
use eth_types::{evm_types::GasCost, Field, ToScalar};
use gadgets::util::not;
use halo2_proofs::{circuit::Value, plonk::Error};

Expand Down Expand Up @@ -176,15 +176,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnDataCopyGadget<F> {
let [dest_offset, data_offset, size] =
[0, 1, 2].map(|index| block.get_rws(step, index).stack_value());

self.data_offset.assign(
region,
offset,
Some(
data_offset.to_le_bytes()[..N_BYTES_MEMORY_ADDRESS]
.try_into()
.unwrap(),
),
)?;
self.data_offset.assign_u256(region, offset, data_offset)?;

let [last_callee_id, return_data_offset, return_data_size] = [
(3, CallContextFieldTag::LastCalleeId),
Expand Down
43 changes: 6 additions & 37 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
},
table::Table,
},
util::int_decomposition::IntDecomposition,

Check failure on line 13 in zkevm-circuits/src/evm_circuit/util.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-unknown-unknown

unresolved import `crate::util::int_decomposition::IntDecomposition`

Check failure on line 13 in zkevm-circuits/src/evm_circuit/util.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unresolved import `crate::util::int_decomposition::IntDecomposition`

Check failure on line 13 in zkevm-circuits/src/evm_circuit/util.rs

View workflow job for this annotation

GitHub Actions / Intra-doc links

unresolved import `crate::util::int_decomposition::IntDecomposition`
witness::{Block, ExecStep, Rw, RwMap},
};
use bus_mapping::state_db::CodeDB;
Expand All @@ -36,7 +37,7 @@ pub use gadgets::util::{and, not, or, select, sum};
use super::param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_U64};

#[derive(Clone, Debug)]
pub(crate) struct Cell<F> {
pub struct Cell<F> {
// expression for constraint
expression: Expression<F>,
column: Column<Advice>,
Expand Down Expand Up @@ -490,6 +491,7 @@ pub struct RandomLinearCombination<F, const N: usize> {
impl<F: Field, const N: usize> RandomLinearCombination<F, N> {
const N_BYTES: usize = N;

/// XXX for randomness 256.expr(), consider using IntDecomposition instead
pub(crate) fn new(cells: [Cell<F>; N], randomness: Expression<F>) -> Self {
Self {
expression: rlc::expr(&cells.clone().map(|cell| cell.expr()), randomness),
Expand Down Expand Up @@ -521,44 +523,11 @@ impl<F: Field, const N: usize> Expr<F> for RandomLinearCombination<F, N> {
}
}

pub(crate) type MemoryAddress<F> = RandomLinearCombination<F, N_BYTES_MEMORY_ADDRESS>;
pub(crate) type MemoryAddress<F> = IntDecomposition<F, N_BYTES_MEMORY_ADDRESS>;

impl<F: Field> WordExpr<F> for MemoryAddress<F> {
fn to_word(&self) -> Word<Expression<F>> {
Word::from_lo_unchecked(self.expr())
}
}

pub(crate) type AccountAddress<F> = RandomLinearCombination<F, N_BYTES_ACCOUNT_ADDRESS>;

impl<F: Field> WordExpr<F> for AccountAddress<F> {
fn to_word(&self) -> Word<Expression<F>> {
Word::new([
rlc::expr(
&self.cells[0..16]
.iter()
.map(|cell| cell.expr())
.collect_vec(),
256.expr(),
),
rlc::expr(
&self.cells[16..]
.iter()
.map(|cell| cell.expr())
.collect_vec(),
256.expr(),
),
])
}
}

pub(crate) type U64Cell<F> = RandomLinearCombination<F, N_BYTES_U64>;
pub(crate) type AccountAddress<F> = IntDecomposition<F, N_BYTES_ACCOUNT_ADDRESS>;

impl<F: Field> WordExpr<F> for U64Cell<F> {
fn to_word(&self) -> Word<Expression<F>> {
Word::from_lo_unchecked(self.expr())
}
}
pub(crate) type U64Cell<F> = IntDecomposition<F, N_BYTES_U64>;

/// Decodes a field element from its byte representation in little endian order
pub(crate) mod from_bytes {
Expand Down
Loading

0 comments on commit 9ad99e7

Please sign in to comment.