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

[WIP] Feat/#1753 stacktrie tx #1812

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions geth-utils/src/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum ProofType {
StorageChanged = 6,
StorageDoesNotExist = 7,
AccountCreate = 8,
TransactionUpdate = 9,
}

#[derive(Default, Debug, Clone)]
Expand Down
34 changes: 33 additions & 1 deletion zkevm-circuits/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod param;
mod rlp_gadgets;
mod start;
mod storage_leaf;
mod transaction_leaf;
/// MPT witness row
pub mod witness_row;

Expand All @@ -31,6 +32,7 @@ use self::{
helpers::RLPItemView,
param::RLP_UNIT_NUM_BYTES,
rlp_gadgets::decode_rlp,
transaction_leaf::TransactionLeafConfig,
witness_row::{
AccountRowType, ExtensionBranchRowType, Node, StartRowType, StorageRowType,
NODE_RLP_TYPES_ACCOUNT, NODE_RLP_TYPES_BRANCH, NODE_RLP_TYPES_START,
Expand All @@ -48,6 +50,7 @@ use crate::{
helpers::{MPTConstraintBuilder, MainRLPGadget, MptCellType, MptTableType},
start::StartConfig,
storage_leaf::StorageLeafConfig,
witness_row::{TransactionRowType, NODE_RLP_TYPES_TRANSACTION},
},
table::{KeccakTable, MPTProofType, MptTable},
util::Challenges,
Expand All @@ -64,6 +67,7 @@ pub(crate) enum MPTRegion {
Branch,
Account,
Storage,
Transaction,
Count,
}

Expand All @@ -74,11 +78,13 @@ pub struct StateMachineConfig<F> {
is_branch: Column<Advice>,
is_account: Column<Advice>,
is_storage: Column<Advice>,
is_transaction: Column<Advice>,

start_config: StartConfig<F>,
branch_config: ExtensionBranchConfig<F>,
storage_config: StorageLeafConfig<F>,
account_config: AccountLeafConfig<F>,
transaction_config: TransactionLeafConfig<F>,
}

impl<F: Field> StateMachineConfig<F> {
Expand All @@ -89,10 +95,12 @@ impl<F: Field> StateMachineConfig<F> {
is_branch: meta.advice_column(),
is_account: meta.advice_column(),
is_storage: meta.advice_column(),
is_transaction: meta.advice_column(),
start_config: StartConfig::default(),
branch_config: ExtensionBranchConfig::default(),
storage_config: StorageLeafConfig::default(),
account_config: AccountLeafConfig::default(),
transaction_config: TransactionLeafConfig::default(),
}
}

Expand All @@ -103,6 +111,7 @@ impl<F: Field> StateMachineConfig<F> {
self.is_branch,
self.is_account,
self.is_storage,
self.is_transaction,
]
}

Expand Down Expand Up @@ -339,10 +348,17 @@ impl<F: Field> MPTConfig<F> {
ctx.memory.build_constraints(&mut cb.base, f!(q_first));
cb.base.pop_region();
},
a!(state_machine.is_transaction) => {
state_machine.step_constraints(meta, &mut cb, TransactionRowType::Count as usize);
cb.base.push_region(MPTRegion::Transaction as usize, TransactionRowType::Count as usize);
state_machine.transaction_config = TransactionLeafConfig::configure(meta, &mut cb, &mut ctx);
ctx.memory.build_constraints(&mut cb.base, f!(q_first));
cb.base.pop_region();
},
_ => ctx.memory.build_constraints(&mut cb.base, f!(q_first)),
)};
// Only account and storage rows can have lookups, disable lookups on all other rows
ifx! {not!(a!(state_machine.is_account) + a!(state_machine.is_storage)) => {
ifx! {not!(a!(state_machine.is_account) + a!(state_machine.is_storage) + a!(state_machine.is_transaction)) => {
require!(a!(ctx.mpt_table.proof_type) => MPTProofType::Disabled.expr());
}}
}}
Expand Down Expand Up @@ -413,6 +429,8 @@ impl<F: Field> MPTConfig<F> {
NODE_RLP_TYPES_ACCOUNT.to_vec()
} else if node.storage.is_some() {
NODE_RLP_TYPES_STORAGE.to_vec()
} else if node.transaction.is_some(){
NODE_RLP_TYPES_TRANSACTION.to_vec()
} else {
unreachable!()
};
Expand Down Expand Up @@ -486,6 +504,20 @@ impl<F: Field> MPTConfig<F> {
)?;
cached_region.pop_region();
}
else if node.transaction.is_some() {
//println!("{}: transaction", offset);
cached_region.push_region(offset, MPTRegion::Transaction as usize);
assign!(cached_region, (self.state_machine.is_transaction, offset) => "is_transaction", true.scalar())?;
self.state_machine.transaction_config.assign(
&mut cached_region,
self,
&mut memory,
offset,
node,
&rlp_values,
)?;
cached_region.pop_region();
}

offset += node.values.len();

Expand Down
9 changes: 7 additions & 2 deletions zkevm-circuits/src/mpt_circuit/account_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ impl<F: Field> AccountLeafConfig<F> {
meta,
&mut cb.base,
address.clone(),
false.expr(),
proof_type.clone(),
WordLoHi::zero(),
config.main_data.new_root.expr(),
Expand All @@ -480,6 +481,7 @@ impl<F: Field> AccountLeafConfig<F> {
meta,
&mut cb.base,
address.clone(),
false.expr(),
proof_type.clone(),
WordLoHi::zero(),
config.main_data.new_root.expr(),
Expand All @@ -490,14 +492,15 @@ impl<F: Field> AccountLeafConfig<F> {
}};
} elsex {
// When the value is set to 0, the leaf is deleted, and if there were only two leaves in the branch,
// the neighbour leaf moves one level up and replaces the branch. When the lookup is executed with
// the neighbor leaf moves one level up and replaces the branch. When the lookup is executed with
// the new value set to 0, the lookup fails (without the code below), because the leaf that is returned
// is the neighbour node that moved up (because the branch and the old leaf doesn’t exist anymore),
// is the neighbor node that moved up (because the branch and the old leaf doesn’t exist anymore),
// but this leaf doesn’t have the zero value.
ctx.mpt_table.constrain(
meta,
&mut cb.base,
address,
false.expr(),
proof_type,
WordLoHi::zero(),
config.main_data.new_root.expr(),
Expand Down Expand Up @@ -713,6 +716,7 @@ impl<F: Field> AccountLeafConfig<F> {
main_data.proof_type,
true,
address_item.word().compress_f(),
F::ZERO,
main_data.new_root,
main_data.old_root,
)?;
Expand Down Expand Up @@ -759,6 +763,7 @@ impl<F: Field> AccountLeafConfig<F> {
&account.address.iter().cloned().rev().collect::<Vec<_>>(),
)),
storage_key: WordLoHi::zero().into_value(),
transaction_index: Value::known(F::ZERO),
proof_type: Value::known(proof_type.scalar()),
new_root: main_data.new_root.into_value(),
old_root: main_data.old_root.into_value(),
Expand Down
20 changes: 14 additions & 6 deletions zkevm-circuits/src/mpt_circuit/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ pub(crate) struct MainData<F> {
pub(crate) proof_type: Cell<F>,
pub(crate) is_below_account: Cell<F>,
pub(crate) address: Cell<F>,
pub(crate) transaction_index: Cell<F>,
pub(crate) new_root: WordLoHiCell<F>,
pub(crate) old_root: WordLoHiCell<F>,
}
Expand All @@ -695,6 +696,7 @@ pub(crate) struct MainDataWitness<F> {
pub(crate) proof_type: usize,
pub(crate) is_below_account: bool,
pub(crate) address: F,
pub(crate) transaction_index: F,
pub(crate) new_root: WordLoHi<F>,
pub(crate) old_root: WordLoHi<F>,
}
Expand All @@ -709,6 +711,7 @@ impl<F: Field> MainData<F> {
proof_type: cb.query_cell(),
is_below_account: cb.query_cell(),
address: cb.query_cell(),
transaction_index: cb.query_cell(),
new_root: cb.query_word_unchecked(),
old_root: cb.query_word_unchecked(),
};
Expand All @@ -720,6 +723,7 @@ impl<F: Field> MainData<F> {
main_data.proof_type.expr(),
main_data.is_below_account.expr(),
main_data.address.expr(),
main_data.transaction_index.expr(),
main_data.new_root.lo().expr(),
main_data.new_root.hi().expr(),
main_data.old_root.lo().expr(),
Expand All @@ -746,13 +750,15 @@ impl<F: Field> MainData<F> {
proof_type: usize,
is_below_account: bool,
address: F,
transaction_index: F,
new_root: WordLoHi<F>,
old_root: WordLoHi<F>,
) -> Result<(), Error> {
let values = [
proof_type.scalar(),
is_below_account.scalar(),
address,
transaction_index,
new_root.lo(),
new_root.hi(),
old_root.lo(),
Expand All @@ -775,17 +781,19 @@ impl<F: Field> MainData<F> {
self.proof_type.assign(region, offset, values[0])?;
self.is_below_account.assign(region, offset, values[1])?;
self.address.assign(region, offset, values[2])?;
self.new_root.lo().assign(region, offset, values[3])?;
self.new_root.hi().assign(region, offset, values[4])?;
self.old_root.lo().assign(region, offset, values[5])?;
self.old_root.hi().assign(region, offset, values[6])?;
self.transaction_index.assign(region, offset, values[3])?;
self.new_root.lo().assign(region, offset, values[4])?;
self.new_root.hi().assign(region, offset, values[5])?;
self.old_root.lo().assign(region, offset, values[6])?;
self.old_root.hi().assign(region, offset, values[7])?;

Ok(MainDataWitness {
proof_type: values[0].get_lower_32() as usize,
is_below_account: values[1] == 1.scalar(),
address: values[2],
new_root: WordLoHi::new([values[3], values[4]]),
old_root: WordLoHi::new([values[5], values[6]]),
transaction_index: values[3],
new_root: WordLoHi::new([values[4], values[5]]),
old_root: WordLoHi::new([values[6], values[7]]),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/mpt_circuit/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub mod mod_extension;
pub mod mpt;
pub mod param;
pub mod storage_leaf;
pub mod transaction_leaf;
pub mod witness_row;
3 changes: 2 additions & 1 deletion zkevm-circuits/src/mpt_circuit/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ impl<F: Field> StartConfig<F> {
&mut memory[main_memory()],
start.proof_type as usize,
false,
0.scalar(),
F::ZERO,
F::ZERO,
root[true.idx()],
root[false.idx()],
)?;
Expand Down
9 changes: 7 additions & 2 deletions zkevm-circuits/src/mpt_circuit/storage_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ impl<F: Field> StorageLeafConfig<F> {
meta,
&mut cb.base,
config.main_data.address.expr(),
false.expr(),
proof_type.clone(),
address_item.word(),
config.main_data.new_root.expr(),
Expand All @@ -373,6 +374,7 @@ impl<F: Field> StorageLeafConfig<F> {
meta,
&mut cb.base,
config.main_data.address.expr(),
false.expr(),
proof_type.clone(),
address_item.word(),
config.main_data.new_root.expr(),
Expand All @@ -383,14 +385,15 @@ impl<F: Field> StorageLeafConfig<F> {
}};
} elsex {
// When the value is set to 0, the leaf is deleted, and if there were only two leaves in the branch,
// the neighbour leaf moves one level up and replaces the branch. When the lookup is executed with
// the neighbor leaf moves one level up and replaces the branch. When the lookup is executed with
// the new value set to 0, the lookup fails (without the code below), because the leaf that is returned
// is the neighbour node that moved up (because the branch and the old leaf doesn’t exist anymore),
// is the neighbor node that moved up (because the branch and the old leaf doesn’t exist anymore),
// but this leaf doesn’t have the zero value.
ctx.mpt_table.constrain(
meta,
&mut cb.base,
config.main_data.address.expr(),
false.expr(),
proof_type,
address_item.word(),
config.main_data.new_root.expr(),
Expand Down Expand Up @@ -571,6 +574,7 @@ impl<F: Field> StorageLeafConfig<F> {
MPTProofType::Disabled as usize,
false,
F::ZERO,
F::ZERO,
WordLoHi::new([F::ZERO, F::ZERO]),
WordLoHi::new([F::ZERO, F::ZERO]),
)?;
Expand Down Expand Up @@ -607,6 +611,7 @@ impl<F: Field> StorageLeafConfig<F> {
&MptUpdateRow {
address: Value::known(main_data.address),
storage_key: address_item.word().into_value(),
transaction_index: Value::known(F::ZERO),
proof_type: Value::known(proof_type.scalar()),
new_root: main_data.new_root.into_value(),
old_root: main_data.old_root.into_value(),
Expand Down
Loading
Loading