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

Commit

Permalink
Feat/#1387 tx circuit refactor word rlc into word lo/hi (#1418)
Browse files Browse the repository at this point in the history
### Description

part of word lo/hi

### Issue Link

#1387 

### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Contents

- applied word lo/hi to tx_circuit

---------

Co-authored-by: sm.wu <[email protected]>
Co-authored-by: Eduard S <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2023
1 parent 1264bfc commit f14a3d4
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 140 deletions.
4 changes: 2 additions & 2 deletions eth-types/src/geth_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ impl Transaction {
.fold(0, |acc, byte| acc + if *byte == 0 { 4 } else { 16 })
}

/// Get the "to" address. If `to` is None then zero adddress
/// Get the "to" address. If `to` is None then zero address
pub fn to_or_zero(&self) -> Address {
self.to.unwrap_or_default()
}
/// Get the "to" address. If `to` is None then compute contract adddress
/// Get the "to" address. If `to` is None then compute contract address
pub fn to_or_contract_addr(&self) -> Address {
self.to
.unwrap_or_else(|| get_contract_address(self.from, self.nonce.to_word()))
Expand Down
90 changes: 46 additions & 44 deletions zkevm-circuits/src/tx_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub use dev::TxCircuit as TestTxCircuit;

use crate::{
table::{KeccakTable, TxFieldTag, TxTable},
util::{random_linear_combine_word as rlc, Challenges, SubCircuit, SubCircuitConfig},
util::{word::Word, Challenges, SubCircuit, SubCircuitConfig},
witness,
};
use eth_types::{geth_types::Transaction, sign_types::SignData, Field, ToLittleEndian, ToScalar};
use eth_types::{geth_types::Transaction, sign_types::SignData, Field};
use halo2_proofs::{
circuit::{AssignedCell, Layouter, Region, Value},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed},
Expand All @@ -41,7 +41,7 @@ pub struct TxCircuitConfig<F: Field> {
tx_id: Column<Advice>,
tag: Column<Fixed>,
index: Column<Advice>,
value: Column<Advice>,
value: Word<Column<Advice>>,
sign_verify: SignVerifyConfig,
_marker: PhantomData<F>,
// External tables
Expand Down Expand Up @@ -73,8 +73,9 @@ impl<F: Field> SubCircuitConfig<F> for TxCircuitConfig<F> {
let tx_id = tx_table.tx_id;
let tag = tx_table.tag;
let index = tx_table.index;
let value = tx_table.value;
meta.enable_equality(value);
let value = tx_table.value_word;
meta.enable_equality(value.lo());
meta.enable_equality(value.hi());

let sign_verify = SignVerifyConfig::new(meta, keccak_table.clone(), challenges);

Expand All @@ -96,7 +97,7 @@ impl<F: Field> TxCircuitConfig<F> {
self.sign_verify.load_range(layouter)
}

/// Assigns a tx circuit row and returns the assigned cell of the value in
/// Assigns a tx circuit row and returns the assigned cell of the value in `word` in
/// the row.
fn assign_row(
&self,
Expand All @@ -105,8 +106,8 @@ impl<F: Field> TxCircuitConfig<F> {
tx_id: usize,
tag: TxFieldTag,
index: usize,
value: Value<F>,
) -> Result<AssignedCell<F, F>, Error> {
value: Word<Value<F>>,
) -> Result<Word<AssignedCell<F, F>>, Error> {
region.assign_advice(
|| "tx_id",
self.tx_id,
Expand All @@ -125,7 +126,7 @@ impl<F: Field> TxCircuitConfig<F> {
offset,
|| Value::known(F::from(index as u64)),
)?;
region.assign_advice(|| "value", self.value, offset, || value)
value.assign_advice(region, || "value", self.value, offset)
}

/// Get number of rows required.
Expand Down Expand Up @@ -174,7 +175,6 @@ impl<F: Field> TxCircuit<F> {
fn assign_tx_table(
&self,
config: &TxCircuitConfig<F>,
challenges: &Challenges<Value<F>>,
layouter: &mut impl Layouter<F>,
assigned_sig_verifs: Vec<AssignedSignatureVerify<F>>,
) -> Result<(), Error> {
Expand All @@ -189,7 +189,7 @@ impl<F: Field> TxCircuit<F> {
0,
TxFieldTag::Null,
0,
Value::known(F::ZERO),
Word::default().into_value(),
)?;
offset += 1;
// Assign al Tx fields except for call data
Expand All @@ -202,46 +202,36 @@ impl<F: Field> TxCircuit<F> {
};

for (tag, value) in [
(TxFieldTag::Nonce, Value::known(F::from(tx.nonce.as_u64()))),
(
TxFieldTag::Gas,
Value::known(F::from(tx.gas_limit.as_u64())),
),
(
TxFieldTag::GasPrice,
challenges
.evm_word()
.map(|challenge| rlc(tx.gas_price.to_le_bytes(), challenge)),
TxFieldTag::Nonce,
Word::from(tx.nonce.as_u64()).into_value(),
),
(
TxFieldTag::CallerAddress,
Value::known(tx.from.to_scalar().expect("tx.from too big")),
TxFieldTag::Gas,
Word::from(tx.gas_limit.as_u64()).into_value(),
),
(TxFieldTag::GasPrice, Word::from(tx.gas_price).into_value()),
(TxFieldTag::CallerAddress, Word::from(tx.from).into_value()),
(
TxFieldTag::CalleeAddress,
Value::known(tx.to_or_zero().to_scalar().expect("tx.to too big")),
Word::from(tx.to_or_zero()).into_value(),
),
(
TxFieldTag::IsCreate,
Value::known(F::from(tx.is_create() as u64)),
),
(
TxFieldTag::Value,
challenges
.evm_word()
.map(|challenge| rlc(tx.value.to_le_bytes(), challenge)),
Word::from(tx.is_create() as u64).into_value(),
),
(TxFieldTag::Value, Word::from(tx.value).into_value()),
(
TxFieldTag::CallDataLength,
Value::known(F::from(tx.call_data.0.len() as u64)),
Word::from(tx.call_data.0.len() as u64).into_value(),
),
(
TxFieldTag::CallDataGasCost,
Value::known(F::from(tx.call_data_gas_cost())),
Word::from(tx.call_data_gas_cost()).into_value(),
),
(
TxFieldTag::TxSignHash,
assigned_sig_verif.msg_hash_rlc.value().copied(),
assigned_sig_verif.msg_hash.map(|x| x.value().copied()),
),
] {
let assigned_cell =
Expand All @@ -251,14 +241,26 @@ impl<F: Field> TxCircuit<F> {
// Ref. spec 0. Copy constraints using fixed offsets between the tx rows and
// the SignVerifyChip
match tag {
TxFieldTag::CallerAddress => region.constrain_equal(
assigned_cell.cell(),
assigned_sig_verif.address.cell(),
)?,
TxFieldTag::TxSignHash => region.constrain_equal(
assigned_cell.cell(),
assigned_sig_verif.msg_hash_rlc.cell(),
)?,
TxFieldTag::CallerAddress => {
region.constrain_equal(
assigned_cell.lo().cell(),
assigned_sig_verif.address.lo().cell(),
)?;
region.constrain_equal(
assigned_cell.hi().cell(),
assigned_sig_verif.address.hi().cell(),
)?
}
TxFieldTag::TxSignHash => {
region.constrain_equal(
assigned_cell.lo().cell(),
assigned_sig_verif.msg_hash.lo().cell(),
)?;
region.constrain_equal(
assigned_cell.hi().cell(),
assigned_sig_verif.msg_hash.hi().cell(),
)?
}
_ => (),
}
}
Expand All @@ -275,7 +277,7 @@ impl<F: Field> TxCircuit<F> {
i + 1, // tx_id
TxFieldTag::CallData,
index,
Value::known(F::from(*byte as u64)),
Word::from(*byte as u64).into_value(),
)?;
offset += 1;
calldata_count += 1;
Expand All @@ -288,7 +290,7 @@ impl<F: Field> TxCircuit<F> {
0, // tx_id
TxFieldTag::CallData,
0,
Value::known(F::ZERO),
Word::default().into_value(),
)?;
offset += 1;
}
Expand Down Expand Up @@ -358,7 +360,7 @@ impl<F: Field> SubCircuit<F> for TxCircuit<F> {
let assigned_sig_verifs =
self.sign_verify
.assign(&config.sign_verify, layouter, &sign_datas, challenges)?;
self.assign_tx_table(config, challenges, layouter, assigned_sig_verifs)?;
self.assign_tx_table(config, layouter, assigned_sig_verifs)?;
Ok(())
}

Expand Down
Loading

0 comments on commit f14a3d4

Please sign in to comment.