Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions zkevm-circuits/src/rlp_circuit_fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ pub struct RlpCircuitConfig<F> {
/// When the tag occupies several bytes, this index denotes the
/// incremental index of the byte within this tag instance.
tag_idx: Column<Advice>,
/// The length of bytes that hold this tag's value.
tag_length: Column<Advice>,
/// The accumulated value of the tag's bytes up to `tag_idx`.
tag_value_acc: Column<Advice>,
/// The depth at this row. Since RLP encoded data can be nested, we use
Expand Down Expand Up @@ -314,6 +312,8 @@ pub struct RlpCircuitConfig<F> {
depth_check: IsEqualConfig<F>,
/// Check for depth == 1
depth_eq_one: IsEqualConfig<F>,
/// Check for byte_value == 0
byte_value_is_zero: IsZeroConfig<F>,

/// Internal tables
/// Data table
Expand All @@ -335,6 +335,7 @@ impl<F: Field> RlpCircuitConfig<F> {
challenges: &Challenges<Expression<F>>,
) -> Self {
let (tx_id, format) = (rlp_table.tx_id, rlp_table.format);
let tag_length = rlp_table.tag_length;
let q_enabled = rlp_table.q_enable;
let (
q_first,
Expand All @@ -349,7 +350,6 @@ impl<F: Field> RlpCircuitConfig<F> {
is_list,
max_length,
tag_idx,
tag_length,
depth,
is_tag_begin,
is_tag_end,
Expand All @@ -375,7 +375,6 @@ impl<F: Field> RlpCircuitConfig<F> {
meta.advice_column(),
meta.advice_column(),
meta.advice_column(),
meta.advice_column(),
);

let tag_value_acc = meta.advice_column_in(SecondPhase);
Expand Down Expand Up @@ -811,6 +810,12 @@ impl<F: Field> RlpCircuitConfig<F> {
|meta| meta.query_advice(format, Rotation::cur()),
|meta| meta.query_advice(format, Rotation::next()),
);
let byte_value_is_zero = IsZeroChip::configure(
meta,
|meta| meta.query_fixed(q_enabled, Rotation::cur()),
byte_value,
|meta| meta.advice_column(),
);

// constrain diff belong to range256 table for each comparator
let mut diffs = vec![];
Expand Down Expand Up @@ -920,6 +925,9 @@ impl<F: Field> RlpCircuitConfig<F> {
let tag_idx_expr = |meta: &mut VirtualCells<F>| meta.query_advice(tag_idx, Rotation::cur());
let tag_value_acc_expr =
|meta: &mut VirtualCells<F>| meta.query_advice(tag_value_acc, Rotation::cur());
let tag_bytes_rlc_expr = |meta: &mut VirtualCells<F>| {
meta.query_advice(rlp_table.tag_bytes_rlc, Rotation::cur())
};
let is_tag_next_end_expr =
|meta: &mut VirtualCells<F>| meta.query_advice(is_tag_end, Rotation::next());
let is_tag_end_expr =
Expand Down Expand Up @@ -981,6 +989,8 @@ impl<F: Field> RlpCircuitConfig<F> {
// is_list = false, tag_value_acc = byte_value
constrain_eq!(meta, cb, is_list, false);
constrain_eq!(meta, cb, rlp_table.tag_value, byte_value_expr);
constrain_eq!(meta, cb, rlp_table.tag_bytes_rlc, byte_value_expr);
constrain_eq!(meta, cb, rlp_table.tag_length, 1);

// state transitions.
update_state!(meta, cb, tag, tag_next_expr(meta));
Expand All @@ -997,6 +1007,8 @@ impl<F: Field> RlpCircuitConfig<F> {

constrain_eq!(meta, cb, is_list, false);
constrain_eq!(meta, cb, rlp_table.tag_value, 0);
constrain_eq!(meta, cb, rlp_table.tag_bytes_rlc, 0);
constrain_eq!(meta, cb, rlp_table.tag_length, 0);

// state transitions.
update_state!(meta, cb, tag, tag_next_expr(meta));
Expand Down Expand Up @@ -1149,6 +1161,7 @@ impl<F: Field> RlpCircuitConfig<F> {
update_state!(meta, cb, tag_idx, 1);
update_state!(meta, cb, tag_length, byte_value_expr(meta) - 0x80.expr());
update_state!(meta, cb, tag_value_acc, byte_value_next_expr(meta));
update_state!(meta, cb, rlp_table.tag_bytes_rlc, byte_value_next_expr(meta));
update_state!(meta, cb, state, State::Bytes);

// depth is unchanged.
Expand All @@ -1173,7 +1186,7 @@ impl<F: Field> RlpCircuitConfig<F> {
let b = select::expr(
mlen_lt_0x20,
256.expr(),
select::expr(mlen_eq_0x20, evm_word_rand, keccak_input_rand),
select::expr(mlen_eq_0x20, evm_word_rand, keccak_input_rand.expr()),
);

// Bytes => Bytes
Expand All @@ -1185,6 +1198,8 @@ impl<F: Field> RlpCircuitConfig<F> {
update_state!(meta, cb, tag_idx, tag_idx_expr(meta) + 1.expr());
update_state!(meta, cb, tag_value_acc,
tag_value_acc_expr(meta) * b.expr() + byte_value_next_expr(meta));
update_state!(meta, cb, rlp_table.tag_bytes_rlc,
tag_bytes_rlc_expr(meta) * keccak_input_rand.expr() + byte_value_next_expr(meta));
update_state!(meta, cb, state, State::Bytes);

// depth, tag_length unchanged.
Expand Down Expand Up @@ -1286,6 +1301,8 @@ impl<F: Field> RlpCircuitConfig<F> {
// state transition.
update_state!(meta, cb, tag_length, tag_value_acc_expr(meta));
update_state!(meta, cb, tag_idx, 1);
update_state!(meta, cb, rlp_table.tag_bytes_rlc, byte_value_next_expr(meta));
update_state!(meta, cb, tag_value_acc, byte_value_next_expr(meta));
update_state!(meta, cb, state, State::Bytes);

// depth is unchanged.
Expand Down Expand Up @@ -1459,7 +1476,6 @@ impl<F: Field> RlpCircuitConfig<F> {
bytes_rlc,
gas_cost_acc,
tag_idx,
tag_length,
tag_value_acc,
is_list,
max_length,
Expand Down Expand Up @@ -1493,6 +1509,7 @@ impl<F: Field> RlpCircuitConfig<F> {
tlength_lte_mlength,
depth_check,
depth_eq_one,
byte_value_is_zero,

// internal tables
data_table,
Expand Down Expand Up @@ -1544,6 +1561,18 @@ impl<F: Field> RlpCircuitConfig<F> {
row,
|| witness.rlp_table.tag_value,
)?;
region.assign_advice(
|| "rlp_table.tag_bytes_rlc",
self.rlp_table.tag_bytes_rlc,
row,
|| witness.rlp_table.tag_bytes_rlc,
)?;
region.assign_advice(
|| "rlp_table.tag_length",
self.rlp_table.tag_length,
row,
|| Value::known(F::from(witness.rlp_table.tag_length as u64)),
)?;
region.assign_advice(
|| "rlp_table.is_output",
self.rlp_table.is_output,
Expand Down Expand Up @@ -1594,12 +1623,6 @@ impl<F: Field> RlpCircuitConfig<F> {
row,
|| Value::known(F::from(witness.state_machine.tag_idx as u64)),
)?;
region.assign_advice(
|| "sm.tag_length",
self.tag_length,
row,
|| Value::known(F::from(witness.state_machine.tag_length as u64)),
)?;
region.assign_advice(
|| "sm.depth",
self.depth,
Expand Down Expand Up @@ -1710,13 +1733,13 @@ impl<F: Field> RlpCircuitConfig<F> {
region,
row,
F::from(witness.state_machine.tag_idx as u64),
F::from(witness.state_machine.tag_length as u64),
F::from(witness.rlp_table.tag_length as u64),
)?;
let tlength_lte_mlength_chip = ComparatorChip::construct(self.tlength_lte_mlength.clone());
tlength_lte_mlength_chip.assign(
region,
row,
F::from(witness.state_machine.tag_length as u64),
F::from(witness.rlp_table.tag_length as u64),
F::from(witness.state_machine.max_length as u64),
)?;

Expand Down Expand Up @@ -1776,6 +1799,8 @@ impl<F: Field> RlpCircuitConfig<F> {
for (chip, lhs, rhs) in byte_value_checks {
chip.assign(region, row, lhs, rhs)?;
}
let bv_chip = IsZeroChip::construct(self.byte_value_is_zero.clone());
bv_chip.assign(region, row, Value::known(byte_value))?;

Ok(())
}
Expand Down
20 changes: 20 additions & 0 deletions zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,10 @@ pub struct RlpFsmRlpTable {
pub rlp_tag: Column<Advice>,
/// The actual value of the current tag being decoded.
pub tag_value: Column<Advice>,
/// RLC of the tag's big-endian bytes
pub tag_bytes_rlc: Column<Advice>,
/// The actual length of bytes of the current tag being decoded.
pub tag_length: Column<Advice>,
/// Whether or not the row emits an output value.
pub is_output: Column<Advice>,
/// Whether or not the current tag's value was nil.
Expand All @@ -2074,6 +2078,8 @@ impl<F: Field> LookupTable<F> for RlpFsmRlpTable {
self.format.into(),
self.rlp_tag.into(),
self.tag_value.into(),
self.tag_bytes_rlc.into(),
self.tag_length.into(),
self.is_output.into(),
self.is_none.into(),
]
Expand All @@ -2086,6 +2092,8 @@ impl<F: Field> LookupTable<F> for RlpFsmRlpTable {
String::from("format"),
String::from("rlp_tag"),
String::from("tag_value_acc"),
String::from("tag_bytes_rlc"),
String::from("tag_length"),
String::from("is_output"),
String::from("is_none"),
]
Expand All @@ -2101,6 +2109,8 @@ impl RlpFsmRlpTable {
format: meta.advice_column(),
rlp_tag: meta.advice_column(),
tag_value: meta.advice_column_in(SecondPhase),
tag_bytes_rlc: meta.advice_column_in(SecondPhase),
tag_length: meta.advice_column(),
is_output: meta.advice_column(),
is_none: meta.advice_column(),
}
Expand Down Expand Up @@ -2154,6 +2164,16 @@ impl RlpFsmRlpTable {
Value::known(F::from(usize::from(row.rlp_tag) as u64)),
),
("tag_value", self.tag_value.into(), row.tag_value),
(
"tag_bytes_rlc",
self.tag_bytes_rlc.into(),
row.tag_bytes_rlc,
),
(
"tag_length",
self.tag_length.into(),
Value::known(F::from(row.tag_length as u64)),
),
("is_output", self.is_output.into(), Value::known(F::one())),
(
"is_none",
Expand Down
Loading