diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index a8b86892b..57021a85e 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -2153,7 +2153,7 @@ where })?; let block_proof = self.block.circuit.prove(block_inputs)?; - Ok((block_proof, block_public_values.into())) + Ok((block_proof, block_public_values)) } pub fn verify_block(&self, block_proof: &ProofWithPublicInputs) -> anyhow::Result<()> { diff --git a/evm_arithmetization/src/proof.rs b/evm_arithmetization/src/proof.rs index 417e58332..7daa697f7 100644 --- a/evm_arithmetization/src/proof.rs +++ b/evm_arithmetization/src/proof.rs @@ -454,7 +454,7 @@ impl MemCap { /// Memory values which are public. /// Note: All the larger integers are encoded with 32-bit limbs in little-endian /// order. -#[derive(Eq, PartialEq, Debug)] +#[derive(Clone, Eq, PartialEq, Debug)] pub struct PublicValuesTarget { /// Trie hashes before the execution of the local state transition. pub trie_roots_before: TrieRootsTarget, @@ -479,69 +479,8 @@ pub struct PublicValuesTarget { impl PublicValuesTarget { /// Serializes public value targets. pub(crate) fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { - let TrieRootsTarget { - state_root: state_root_before, - transactions_root: transactions_root_before, - receipts_root: receipts_root_before, - } = self.trie_roots_before; - - buffer.write_target_array(&state_root_before)?; - buffer.write_target_array(&transactions_root_before)?; - buffer.write_target_array(&receipts_root_before)?; - - let TrieRootsTarget { - state_root: state_root_after, - transactions_root: transactions_root_after, - receipts_root: receipts_root_after, - } = self.trie_roots_after; + FinalPublicValuesTarget::from(self).to_buffer(buffer)?; - buffer.write_target_array(&state_root_after)?; - buffer.write_target_array(&transactions_root_after)?; - buffer.write_target_array(&receipts_root_after)?; - - let BlockMetadataTarget { - block_beneficiary, - block_timestamp, - block_number, - block_difficulty, - block_random, - block_gaslimit, - block_chain_id, - block_base_fee, - block_gas_used, - block_bloom, - } = self.block_metadata; - - buffer.write_target_array(&block_beneficiary)?; - buffer.write_target(block_timestamp)?; - buffer.write_target(block_number)?; - buffer.write_target(block_difficulty)?; - buffer.write_target_array(&block_random)?; - buffer.write_target(block_gaslimit)?; - buffer.write_target(block_chain_id)?; - buffer.write_target_array(&block_base_fee)?; - buffer.write_target(block_gas_used)?; - buffer.write_target_array(&block_bloom)?; - - let BlockHashesTarget { - prev_hashes, - cur_hash, - } = self.block_hashes; - buffer.write_target_array(&prev_hashes)?; - buffer.write_target_array(&cur_hash)?; - - let ExtraBlockDataTarget { - checkpoint_state_trie_root, - txn_number_before, - txn_number_after, - gas_used_before, - gas_used_after, - } = self.extra_block_data; - buffer.write_target_array(&checkpoint_state_trie_root)?; - buffer.write_target(txn_number_before)?; - buffer.write_target(txn_number_after)?; - buffer.write_target(gas_used_before)?; - buffer.write_target(gas_used_after)?; let RegistersDataTarget { program_counter: program_counter_before, is_kernel: is_kernel_before, @@ -556,6 +495,7 @@ impl PublicValuesTarget { buffer.write_target_array(&stack_top_before)?; buffer.write_target(context_before)?; buffer.write_target(gas_used_before)?; + let RegistersDataTarget { program_counter: program_counter_after, is_kernel: is_kernel_after, @@ -579,43 +519,13 @@ impl PublicValuesTarget { /// Deserializes public value targets. pub(crate) fn from_buffer(buffer: &mut Buffer) -> IoResult { - let trie_roots_before = TrieRootsTarget { - state_root: buffer.read_target_array()?, - transactions_root: buffer.read_target_array()?, - receipts_root: buffer.read_target_array()?, - }; - - let trie_roots_after = TrieRootsTarget { - state_root: buffer.read_target_array()?, - transactions_root: buffer.read_target_array()?, - receipts_root: buffer.read_target_array()?, - }; - - let block_metadata = BlockMetadataTarget { - block_beneficiary: buffer.read_target_array()?, - block_timestamp: buffer.read_target()?, - block_number: buffer.read_target()?, - block_difficulty: buffer.read_target()?, - block_random: buffer.read_target_array()?, - block_gaslimit: buffer.read_target()?, - block_chain_id: buffer.read_target()?, - block_base_fee: buffer.read_target_array()?, - block_gas_used: buffer.read_target()?, - block_bloom: buffer.read_target_array()?, - }; - - let block_hashes = BlockHashesTarget { - prev_hashes: buffer.read_target_array()?, - cur_hash: buffer.read_target_array()?, - }; - - let extra_block_data = ExtraBlockDataTarget { - checkpoint_state_trie_root: buffer.read_target_array()?, - txn_number_before: buffer.read_target()?, - txn_number_after: buffer.read_target()?, - gas_used_before: buffer.read_target()?, - gas_used_after: buffer.read_target()?, - }; + let FinalPublicValuesTarget { + trie_roots_before, + trie_roots_after, + block_metadata, + block_hashes, + extra_block_data, + } = FinalPublicValuesTarget::from_buffer(buffer)?; let registers_before = RegistersDataTarget { program_counter: buffer.read_target()?, @@ -660,83 +570,42 @@ impl PublicValuesTarget { pub(crate) fn from_public_inputs(pis: &[Target], len_mem_cap: usize) -> Self { assert!( pis.len() - > TrieRootsTarget::SIZE * 2 + BlockMetadataTarget::SIZE + BlockHashesTarget::SIZE - - 1 + >= FinalPublicValuesTarget::SIZE + + RegistersDataTarget::SIZE * 2 + + NUM_HASH_OUT_ELTS * len_mem_cap * 2 ); + let FinalPublicValuesTarget { + trie_roots_before, + trie_roots_after, + block_metadata, + block_hashes, + extra_block_data, + } = FinalPublicValuesTarget::from_public_inputs(pis); + + let pis = &pis[FinalPublicValuesTarget::SIZE..]; + Self { - trie_roots_before: TrieRootsTarget::from_public_inputs(&pis[0..TrieRootsTarget::SIZE]), - trie_roots_after: TrieRootsTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE..TrieRootsTarget::SIZE * 2], - ), - block_metadata: BlockMetadataTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 - ..TrieRootsTarget::SIZE * 2 + BlockMetadataTarget::SIZE], - ), - block_hashes: BlockHashesTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 + BlockMetadataTarget::SIZE - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE], - ), - extra_block_data: ExtraBlockDataTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 + BlockMetadataTarget::SIZE + BlockHashesTarget::SIZE - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE], - ), + trie_roots_before, + trie_roots_after, + block_metadata, + block_hashes, + extra_block_data, registers_before: RegistersDataTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE], + &pis[..RegistersDataTarget::SIZE], ), registers_after: RegistersDataTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE * 2], + &pis[RegistersDataTarget::SIZE..RegistersDataTarget::SIZE * 2], ), mem_before: MemCapTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE * 2 - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE * 2 - + len_mem_cap * NUM_HASH_OUT_ELTS], + &pis[RegistersDataTarget::SIZE * 2 + ..RegistersDataTarget::SIZE * 2 + len_mem_cap * NUM_HASH_OUT_ELTS], len_mem_cap, ), mem_after: MemCapTarget::from_public_inputs( - &pis[TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE * 2 - + len_mem_cap * NUM_HASH_OUT_ELTS - ..TrieRootsTarget::SIZE * 2 - + BlockMetadataTarget::SIZE - + BlockHashesTarget::SIZE - + ExtraBlockDataTarget::SIZE - + RegistersDataTarget::SIZE * 2 - + 2 * len_mem_cap * NUM_HASH_OUT_ELTS], + &pis[RegistersDataTarget::SIZE * 2 + len_mem_cap * NUM_HASH_OUT_ELTS + ..RegistersDataTarget::SIZE * 2 + 2 * len_mem_cap * NUM_HASH_OUT_ELTS], len_mem_cap, ), } @@ -830,7 +699,7 @@ impl From for FinalPublicValuesTarget { impl<'a> From<&'a PublicValuesTarget> for FinalPublicValuesTarget { fn from(value: &'a PublicValuesTarget) -> Self { - value.into() + value.clone().into() } } @@ -958,10 +827,10 @@ impl FinalPublicValuesTarget { }) } - /// Extracts public value `Target`s from the given public input `Target`s. - /// Public values are always the first public inputs added to the circuit, - /// so we can start extracting at index 0. - pub(crate) fn from_public_inputs(pis: &[Target], len_mem_cap: usize) -> Self { + /// Extracts final public value `Target`s from the given public input + /// `Target`s. Final public values are always the first public inputs + /// added to the circuit, so we can start extracting at index 0. + pub(crate) fn from_public_inputs(pis: &[Target]) -> Self { assert!(pis.len() >= Self::SIZE); Self { @@ -988,47 +857,6 @@ impl FinalPublicValuesTarget { ), } } - - /// Returns the public values in `pv0` or `pv1` depending on `condition`. - pub(crate) fn select, const D: usize>( - builder: &mut CircuitBuilder, - condition: BoolTarget, - pv0: Self, - pv1: Self, - ) -> Self { - Self { - trie_roots_before: TrieRootsTarget::select( - builder, - condition, - pv0.trie_roots_before, - pv1.trie_roots_before, - ), - trie_roots_after: TrieRootsTarget::select( - builder, - condition, - pv0.trie_roots_after, - pv1.trie_roots_after, - ), - block_metadata: BlockMetadataTarget::select( - builder, - condition, - pv0.block_metadata, - pv1.block_metadata, - ), - block_hashes: BlockHashesTarget::select( - builder, - condition, - pv0.block_hashes, - pv1.block_hashes, - ), - extra_block_data: ExtraBlockDataTarget::select( - builder, - condition, - pv0.extra_block_data, - pv1.extra_block_data, - ), - } - } } /// Circuit version of `TrieRoots`.