Skip to content

Commit

Permalink
Merge branch 'v0.7' into fix/lookup_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
z2trillion authored Nov 14, 2023
2 parents d1945fd + 757675b commit 18f08e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 79 deletions.
111 changes: 33 additions & 78 deletions src/gadgets/mpt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,15 @@ fn configure_extension_old<F: FieldExt>(
poseidon: &impl PoseidonLookup,
) {
cb.assert(
"can only delete existing nodes for storage proofs",
"can only delete existing storage trie nodes for storage proofs",
config
.proof_type
.current_matches(&[MPTProofType::StorageChanged]),
.current_matches(&[MPTProofType::StorageChanged])
.and(
config
.segment_type
.current_matches(&[SegmentType::StorageTrie, SegmentType::StorageLeaf0]),
),
);
cb.assert_zero(
"new value is 0 when deleting node",
Expand Down Expand Up @@ -1354,10 +1359,10 @@ fn configure_nonce<F: FieldExt>(
config.path_type.current_matches(&[PathType::ExtensionNew]),
|cb| {
cb.assert_equal(
"sibling is hash(0, hash(0, 0)) for nonce extension new at AccountLeaf2",
config.sibling.current(),
Query::from(*ZERO_STORAGE_ROOT_KECCAK_CODEHASH_HASH),
);
"sibling is hash(0, hash(0, 0)) for nonce extension new at AccountLeaf2",
config.sibling.current(),
Query::from(*ZERO_STORAGE_ROOT_KECCAK_CODEHASH_HASH),
);
},
);
}
Expand Down Expand Up @@ -1427,12 +1432,6 @@ fn configure_code_size<F: FieldExt>(
bytes: &impl BytesLookup,
poseidon: &impl PoseidonLookup,
) {
cb.assert(
"new accounts have balance or nonce set first",
config
.path_type
.current_matches(&[PathType::Start, PathType::Common]),
);
for variant in SegmentType::iter() {
let conditional_constraints = |cb: &mut ConstraintBuilder<F>| match variant {
SegmentType::Start | SegmentType::AccountTrie => {
Expand Down Expand Up @@ -1603,19 +1602,21 @@ fn configure_balance<F: FieldExt>(
);
},
);
cb.add_lookup(
"new balance is rlc(new_hash) and fits into 31 bytes",
[
config.new_hash.current(),
Query::from(30),
config.new_value.current(),
],
rlc.lookup(),
);
cb.condition(
config
.path_type
.current_matches(&[PathType::Common, PathType::ExtensionNew]),
config.path_type.current_matches(&[PathType::ExtensionNew]),
|cb| {
cb.add_lookup(
"new balance is rlc(new_hash) and fits into 31 bytes",
[
config.new_hash.current(),
Query::from(30),
config.new_value.current(),
],
rlc.lookup(),
cb.assert_zero(
"sibling (code_size + nonce << 64) is 0 for new account)",
config.sibling.current(),
);
},
);
Expand All @@ -1642,42 +1643,22 @@ fn configure_poseidon_code_hash<F: FieldExt>(
cb: &mut ConstraintBuilder<F>,
config: &MptUpdateConfig,
) {
cb.assert(
"new accounts have balance or nonce set first",
config
.path_type
.current_matches(&[PathType::Start, PathType::Common]),
);
for variant in SegmentType::iter() {
let conditional_constraints = |cb: &mut ConstraintBuilder<F>| match variant {
SegmentType::AccountLeaf0 => {
cb.assert_equal("direction is 1", config.direction.current(), Query::one());
}
SegmentType::AccountLeaf1 => {
cb.assert_equal("direction is 1", config.direction.current(), Query::one());
cb.condition(
config
.path_type
.current_matches(&[PathType::Common, PathType::ExtensionOld]),
|cb| {
cb.assert_equal(
"old_hash is old poseidon code hash",
config.old_value.current(),
config.old_hash.current(),
);
},
cb.assert_equal(
"old_hash is old poseidon code hash",
config.old_value.current(),
config.old_hash.current(),
);
cb.condition(
config
.path_type
.current_matches(&[PathType::Common, PathType::ExtensionNew]),
|cb| {
cb.assert_equal(
"new_hash is new poseidon code hash",
config.new_value.current(),
config.new_hash.current(),
);
},
cb.assert_equal(
"new_hash is new poseidon code hash",
config.new_value.current(),
config.new_hash.current(),
);
}
_ => {}
Expand All @@ -1697,12 +1678,6 @@ fn configure_keccak_code_hash<F: FieldExt>(
rlc: &impl RlcLookup,
randomness: Query<F>,
) {
cb.assert(
"new accounts have balance or nonce set first",
config
.path_type
.current_matches(&[PathType::Start, PathType::Common]),
);
for variant in SegmentType::iter() {
let conditional_constraints = |cb: &mut ConstraintBuilder<F>| match variant {
SegmentType::Start | SegmentType::AccountTrie => {
Expand Down Expand Up @@ -1798,10 +1773,6 @@ fn configure_storage<F: FieldExt>(
cb.assert_equal("direction is 1", config.direction.current(), Query::one());
}
SegmentType::AccountLeaf3 => {
cb.assert(
"storage modifications must be on an existing account",
config.path_type.current_matches(&[PathType::Common]),
);
cb.assert_zero("direction is 0", config.direction.current());
let [key_high, key_low, ..] = config.intermediate_values;
let [rlc_key_high, rlc_key_low, ..] = config.second_phase_intermediate_values;
Expand All @@ -1821,11 +1792,8 @@ fn configure_storage<F: FieldExt>(
let [old_high, old_low, new_high, new_low, ..] = config.intermediate_values;
let [rlc_old_high, rlc_old_low, rlc_new_high, rlc_new_low, ..] =
config.second_phase_intermediate_values;

cb.condition(
config
.path_type
.current_matches(&[PathType::Common, PathType::ExtensionOld]),
config.path_type.current_matches(&[PathType::Common]),
|cb| {
configure_word_rlc(
cb,
Expand All @@ -1836,13 +1804,6 @@ fn configure_storage<F: FieldExt>(
rlc,
randomness.clone(),
);
},
);
cb.condition(
config
.path_type
.current_matches(&[PathType::Common, PathType::ExtensionNew]),
|cb| {
configure_word_rlc(
cb,
[config.new_hash, new_high, new_low],
Expand Down Expand Up @@ -1971,12 +1932,6 @@ fn configure_empty_account<F: FieldExt>(
config: &MptUpdateConfig,
poseidon: &impl PoseidonLookup,
) {
cb.assert(
"path type is start or common for empty account proof",
config
.path_type
.current_matches(&[PathType::Start, PathType::Common]),
);
cb.assert_zero("old value is 0", config.old_value.current());
cb.assert_zero("new value is 0", config.new_value.current());
cb.assert_equal(
Expand Down
6 changes: 5 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ impl From<(&MPTProofType, &SMTTrace)> for ClaimKind {
match update {
[None, None] => (),
[Some(old), Some(new)] => {
// The account must exist, because only contracts with bytecode can modify their own storage slots.
// Accesses to the MPT happen in the order defined in the state (aka rw) circuit, which is not the
// same as the order they occur in the EVM. In the state circuit, nonce and balance modifications
// will precede storage modifications for a given address, which means that the MPT circuit only
// needs to handle storage modifications for existing accounts, even though this is not true in the
// EVM, where the storage of an account can be modified during its construction.
if !(account_old == account_new
|| (account_old.is_none() && account_new == &Some(Default::default())))
{
Expand Down

0 comments on commit 18f08e5

Please sign in to comment.