Skip to content

Commit

Permalink
Fix failing empty storage proofs by always adding storage key hash to…
Browse files Browse the repository at this point in the history
… hash traces
  • Loading branch information
Mason Liang committed Jul 31, 2023
1 parent dc1a0c7 commit 1c11b6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/gadgets/mpt_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ impl MptUpdateConfig {
trie_rows,
old_leaf,
new_leaf,
..
} => {
let other_key = storage.other_key();
let n_trie_rows = self.assign_storage_trie_rows(region, offset, trie_rows);
Expand Down
36 changes: 20 additions & 16 deletions src/types/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use halo2_proofs::{arithmetic::FieldExt, halo2curves::bn256::Fr};
pub enum StorageProof {
Root(Fr), // Not proving a storage update, so we only need the storage root.
Update {
storage_key: U256,
key: Fr,
trie_rows: TrieRows,
old_leaf: StorageLeaf,
Expand Down Expand Up @@ -65,12 +66,21 @@ impl StorageProof {
match self {
Self::Root(_) => vec![],
Self::Update {
storage_key,
key,
trie_rows,
old_leaf,
new_leaf,
..
} => {
let mut lookups = trie_rows.poseidon_lookups();
let (key_high, key_low) = u256_hi_lo(storage_key);
let mut lookups = vec![(
Fr::from_u128(key_high),
Fr::from_u128(key_low),
HashDomain::Pair,
*key,
)];
lookups.extend(trie_rows.poseidon_lookups());
lookups.extend(old_leaf.poseidon_lookups());
lookups.extend(new_leaf.poseidon_lookups());
lookups
Expand Down Expand Up @@ -126,10 +136,10 @@ impl StorageProof {
#[cfg(test)]
pub fn check(&self) {
if let Self::Update {
key: _,
trie_rows,
old_leaf,
new_leaf,
..
} = self
{
// Check that trie rows are consistent and produce claimed roots.
Expand Down Expand Up @@ -242,32 +252,23 @@ impl StorageLeaf {
}

fn poseidon_lookups(&self) -> Vec<(Fr, Fr, HashDomain, Fr)> {
let mut lookups = vec![];
match self {
Self::Empty { .. } => (),
Self::Empty { .. } => vec![],
Self::Leaf { value_hash, .. } => {
lookups.push((self.key(), *value_hash, HashDomain::Leaf, self.hash()))
vec![(self.key(), *value_hash, HashDomain::Leaf, self.hash())]
}
Self::Entry { storage_key, .. } => {
let (key_high, key_low) = u256_hi_lo(storage_key);
lookups.extend(vec![
(
Fr::from_u128(key_high),
Fr::from_u128(key_low),
HashDomain::Pair,
self.key(),
),
Self::Entry { .. } => {
vec![
(
self.value_high(),
self.value_low(),
HashDomain::Pair,
self.value_hash(),
),
(self.key(), self.value_hash(), HashDomain::Leaf, self.hash()),
]);
]
}
}
lookups
}
}

Expand All @@ -289,10 +290,13 @@ impl From<&SMTTrace> for StorageProof {
);

let [old_entry, new_entry] = trace.state_update.unwrap().map(Option::unwrap);
assert_eq!(old_entry.key, new_entry.key);
let storage_key = u256_from_hex(old_entry.key);
let old_leaf = StorageLeaf::new(key, &old_leaf, &old_entry);
let new_leaf = StorageLeaf::new(key, &new_leaf, &new_entry);

let storage_proof = Self::Update {
storage_key,
key,
trie_rows,
old_leaf,
Expand Down

0 comments on commit 1c11b6c

Please sign in to comment.