From 98b5e5be51640b65c073a6dc2372c28a40a41604 Mon Sep 17 00:00:00 2001 From: Linda Guiga <101227802+LindaGuiga@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:30:52 -0500 Subject: [PATCH] Initialize blockhashes (#1370) * Initialize blockhashes * Update comment --- evm/src/fixed_recursive_verifier.rs | 23 +++++++++++++++++++++-- evm/src/proof.rs | 3 ++- evm/tests/empty_txn_list.rs | 6 ++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/evm/src/fixed_recursive_verifier.rs b/evm/src/fixed_recursive_verifier.rs index 9f3ec06c..9628e5fc 100644 --- a/evm/src/fixed_recursive_verifier.rs +++ b/evm/src/fixed_recursive_verifier.rs @@ -1033,9 +1033,10 @@ where block_inputs .set_proof_with_pis_target(&self.block.parent_block_proof, parent_block_proof); } else { - // Initialize genesis_state_trie, state_root_after and the block number for correct connection between blocks. + // Initialize genesis_state_trie, state_root_after, block hashes and the block number for correct connection between blocks. // Initialize `state_root_after`. - let state_trie_root_after_keys = 24..32; + let state_trie_root_after_keys = + TrieRootsTarget::SIZE..TrieRootsTarget::SIZE + TrieRootsTarget::HASH_SIZE; let mut nonzero_pis = HashMap::new(); for (key, &value) in state_trie_root_after_keys .zip_eq(&h256_limbs::(public_values.trie_roots_before.state_root)) @@ -1057,6 +1058,24 @@ where nonzero_pis.insert(key, value); } + // Initialize block hashes. + let block_hashes_keys = TrieRootsTarget::SIZE * 2 + BlockMetadataTarget::SIZE + ..TrieRootsTarget::SIZE * 2 + + BlockMetadataTarget::SIZE + + BlockHashesTarget::BLOCK_HASHES_SIZE + - 8; + + for i in 0..public_values.block_hashes.prev_hashes.len() - 1 { + let targets = h256_limbs::(public_values.block_hashes.prev_hashes[i]); + for j in 0..8 { + nonzero_pis.insert(block_hashes_keys.start + 8 * (i + 1) + j, targets[j]); + } + } + let cur_targets = h256_limbs::(public_values.block_hashes.prev_hashes[255]); + for i in 0..8 { + nonzero_pis.insert(block_hashes_keys.end + i, cur_targets[i]); + } + // Initialize the block number. let block_number_key = TrieRootsTarget::SIZE * 2 + 6; nonzero_pis.insert(block_number_key, F::NEG_ONE); diff --git a/evm/src/proof.rs b/evm/src/proof.rs index ea8507b1..f5e4dc6a 100644 --- a/evm/src/proof.rs +++ b/evm/src/proof.rs @@ -385,7 +385,8 @@ pub(crate) struct TrieRootsTarget { impl TrieRootsTarget { /// Number of `Target`s required for all trie hashes. - pub(crate) const SIZE: usize = 24; + pub(crate) const HASH_SIZE: usize = 8; + pub(crate) const SIZE: usize = Self::HASH_SIZE * 3; /// Extracts trie hash `Target`s for all tries from the provided public input `Target`s. /// The provided `pis` should start with the trie hashes. diff --git a/evm/tests/empty_txn_list.rs b/evm/tests/empty_txn_list.rs index 0285286d..eaa9c0cb 100644 --- a/evm/tests/empty_txn_list.rs +++ b/evm/tests/empty_txn_list.rs @@ -4,7 +4,7 @@ use std::time::Duration; use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie}; -use ethereum_types::H256; +use ethereum_types::{BigEndianHash, H256}; use keccak_hash::keccak; use log::info; use plonky2::field::goldilocks_field::GoldilocksField; @@ -47,6 +47,8 @@ fn test_empty_txn_list() -> anyhow::Result<()> { transactions_root: transactions_trie.hash(), receipts_root: receipts_trie.hash(), }; + let mut initial_block_hashes = vec![H256::default(); 256]; + initial_block_hashes[255] = H256::from_uint(&0x200.into()); let inputs = GenerationInputs { signed_txn: None, withdrawals: vec![], @@ -66,7 +68,7 @@ fn test_empty_txn_list() -> anyhow::Result<()> { block_bloom_before: [0.into(); 8], block_bloom_after: [0.into(); 8], block_hashes: BlockHashes { - prev_hashes: vec![H256::default(); 256], + prev_hashes: initial_block_hashes, cur_hash: H256::default(), }, addresses: vec![],