diff --git a/evm/src/fixed_recursive_verifier.rs b/evm/src/fixed_recursive_verifier.rs index a9aa92e9bd..30aae90fb3 100644 --- a/evm/src/fixed_recursive_verifier.rs +++ b/evm/src/fixed_recursive_verifier.rs @@ -58,12 +58,29 @@ where pub public_values: AggregatedPublicValues, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct EvmProofTarget { pub proof: ProofWithPublicInputsTarget, pub public_values: AggregatedPublicValuesTarget, } +impl EvmProofTarget { + pub fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { + buffer.write_target_proof_with_public_inputs(&self.proof)?; + self.public_values.to_buffer(buffer) + } + + pub fn from_buffer(buffer: &mut Buffer) -> IoResult { + let proof = buffer.read_target_proof_with_public_inputs()?; + let public_values = AggregatedPublicValuesTarget::from_buffer(buffer)?; + + Ok(Self { + proof, + public_values, + }) + } +} + /// Adds a new "virtual" EVM proof target. fn add_virtual_evm_proof, const D: usize>( builder: &mut CircuitBuilder, @@ -369,15 +386,15 @@ pub struct AggregationChildTarget { impl AggregationChildTarget { pub fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { buffer.write_target_bool(self.is_agg)?; - buffer.write_target_proof_with_public_inputs(&self.agg_proof)?; - buffer.write_target_proof_with_public_inputs(&self.evm_proof)?; - Ok(()) + self.agg_proof.to_buffer(buffer)?; + self.evm_proof.to_buffer(buffer) } pub fn from_buffer(buffer: &mut Buffer) -> IoResult { let is_agg = buffer.read_target_bool()?; - let agg_proof = buffer.read_target_proof_with_public_inputs()?; - let evm_proof = buffer.read_target_proof_with_public_inputs()?; + let agg_proof = EvmProofTarget::from_buffer(buffer)?; + let evm_proof = EvmProofTarget::from_buffer(buffer)?; + Ok(Self { is_agg, agg_proof, @@ -412,8 +429,8 @@ where ) -> IoResult<()> { buffer.write_circuit_data(&self.circuit, gate_serializer, generator_serializer)?; buffer.write_target_bool(self.has_parent_block)?; - buffer.write_target_proof_with_public_inputs(&self.parent_block_proof)?; - buffer.write_target_proof_with_public_inputs(&self.agg_root_proof)?; + self.parent_block_proof.to_buffer(buffer)?; + self.agg_root_proof.to_buffer(buffer)?; buffer.write_target_verifier_circuit(&self.cyclic_vk)?; Ok(()) } @@ -425,8 +442,8 @@ where ) -> IoResult { let circuit = buffer.read_circuit_data(gate_serializer, generator_serializer)?; let has_parent_block = buffer.read_target_bool()?; - let parent_block_proof = buffer.read_target_proof_with_public_inputs()?; - let agg_root_proof = buffer.read_target_proof_with_public_inputs()?; + let parent_block_proof = EvmProofTarget::from_buffer(buffer)?; + let agg_root_proof = EvmProofTarget::from_buffer(buffer)?; let cyclic_vk = buffer.read_target_verifier_circuit()?; Ok(Self { circuit, diff --git a/evm/src/proof.rs b/evm/src/proof.rs index 65e0db94b4..c498869e25 100644 --- a/evm/src/proof.rs +++ b/evm/src/proof.rs @@ -102,7 +102,7 @@ pub struct PublicValuesTarget { } /// Memory values which are public over an aggregaton of blocks. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct AggregatedPublicValuesTarget { pub trie_roots_before: TrieRootsTarget, pub trie_roots_after: TrieRootsTarget, @@ -111,14 +111,77 @@ pub struct AggregatedPublicValuesTarget { pub block_metadata_pair: (BlockMetadataTarget, BlockMetadataTarget), } -#[derive(Debug, Clone)] +impl AggregatedPublicValuesTarget { + pub fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { + self.trie_roots_before.to_buffer(buffer)?; + self.trie_roots_after.to_buffer(buffer)?; + self.block_metadata_pair.0.to_buffer(buffer)?; + self.block_metadata_pair.1.to_buffer(buffer)?; + + Ok(()) + } + + pub fn from_buffer(buffer: &mut Buffer) -> IoResult { + let trie_roots_before = TrieRootsTarget::from_buffer(buffer)?; + let trie_roots_after = TrieRootsTarget::from_buffer(buffer)?; + let block_metadata_pair = ( + BlockMetadataTarget::from_buffer(buffer)?, + BlockMetadataTarget::from_buffer(buffer)?, + ); + + Ok(Self { + trie_roots_before, + trie_roots_after, + block_metadata_pair, + }) + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] pub struct TrieRootsTarget { pub state_root: [Target; 8], pub transactions_root: [Target; 8], pub receipts_root: [Target; 8], } -#[derive(Debug, Clone)] +impl TrieRootsTarget { + pub fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { + for limb in self.state_root.iter() { + buffer.write_target(limb)?; + } + for limb in self.transactions_root.iter() { + buffer.write_target(limb)?; + } + for limb in self.receipts_root.iter() { + buffer.write_target(limb)?; + } + + Ok(()) + } + + pub fn from_buffer(buffer: &mut Buffer) -> IoResult { + let mut state_root = [Target::default(); 8]; + for limb in state_root.iter_mut() { + *limb = buffer.read_target()?; + } + let mut transactions_root = [Target::default(); 8]; + for limb in transactions_root.iter_mut() { + *limb = buffer.read_target()?; + } + let mut receipts_root = [Target::default(); 8]; + for limb in receipts_root.iter_mut() { + *limb = buffer.read_target()?; + } + + Ok(Self { + state_root, + transactions_root, + receipts_root, + }) + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] pub struct BlockMetadataTarget { pub block_beneficiary: [Target; 5], pub block_timestamp: Target, @@ -129,6 +192,43 @@ pub struct BlockMetadataTarget { pub block_base_fee: Target, } +impl BlockMetadataTarget { + pub fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { + for limb in self.block_beneficiary.iter() { + buffer.write_target(limb)?; + } + buffer.write_target(self.block_timestamp)?; + buffer.write_target(self.block_number)?; + buffer.write_target(self.block_difficulty)?; + buffer.write_target(self.block_gaslimit)?; + buffer.write_target(self.block_chain_id)?; + buffer.write_target(self.block_base_fee) + } + + pub fn from_buffer(buffer: &mut Buffer) -> IoResult { + let mut block_beneficiary = [Target::default(); 5]; + for limb in block_beneficiary.iter_mut() { + *limb = buffer.read_target()?; + } + let block_timestamp = buffer.read_target()?; + let block_number = buffer.read_target()?; + let block_difficulty = buffer.read_target()?; + let block_gaslimit = buffer.read_target()?; + let block_chain_id = buffer.read_target()?; + let block_base_fee = buffer.read_target()?; + + Ok(Self { + block_beneficiary, + block_timestamp, + block_number, + block_difficulty, + block_gaslimit, + block_chain_id, + block_base_fee, + }) + } +} + #[derive(Debug, Clone)] pub struct StarkProof, C: GenericConfig, const D: usize> { /// Merkle cap of LDEs of trace values.