Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZK 37] only check path_type in configure_extension_old and configure_extension_new #79

Merged
merged 9 commits into from
Nov 14, 2023
111 changes: 33 additions & 78 deletions src/gadgets/mpt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,10 +1134,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 @@ -1360,10 +1365,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 @@ -1433,12 +1438,6 @@ fn configure_code_size<F: FieldExt>(
bytes: &impl BytesLookup,
poseidon: &impl PoseidonLookup,
) {
cb.assert(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtensionNew(Old) are already disallowed for CodeSizeExists in configure_extension_new(old).

"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 @@ -1609,19 +1608,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 @@ -1648,42 +1649,22 @@ fn configure_poseidon_code_hash<F: FieldExt>(
cb: &mut ConstraintBuilder<F>,
config: &MptUpdateConfig,
) {
cb.assert(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtensionNew(Old) are already disallowed for PoseidonCodeHashExists in configure_extension_new(old).

"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 @@ -1703,12 +1684,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 @@ -1804,10 +1779,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 @@ -1827,11 +1798,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 @@ -1842,13 +1810,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 @@ -1977,12 +1938,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
Loading