Skip to content

Commit

Permalink
Serialize EvmProofTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed May 2, 2023
1 parent ae2172e commit 00b7c18
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 13 deletions.
37 changes: 27 additions & 10 deletions evm/src/fixed_recursive_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,29 @@ where
pub public_values: AggregatedPublicValues,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EvmProofTarget<const D: usize> {
pub proof: ProofWithPublicInputsTarget<D>,
pub public_values: AggregatedPublicValuesTarget,
}

impl<const D: usize> EvmProofTarget<D> {
pub fn to_buffer(&self, buffer: &mut Vec<u8>) -> IoResult<()> {
buffer.write_target_proof_with_public_inputs(&self.proof)?;
self.public_values.to_buffer(buffer)
}

pub fn from_buffer(buffer: &mut Buffer) -> IoResult<Self> {
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<F: RichField + Extendable<D>, const D: usize>(
builder: &mut CircuitBuilder<F, D>,
Expand Down Expand Up @@ -369,15 +386,15 @@ pub struct AggregationChildTarget<const D: usize> {
impl<const D: usize> AggregationChildTarget<D> {
pub fn to_buffer(&self, buffer: &mut Vec<u8>) -> 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<Self> {
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,
Expand Down Expand Up @@ -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(())
}
Expand All @@ -425,8 +442,8 @@ where
) -> IoResult<Self> {
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,
Expand Down
106 changes: 103 additions & 3 deletions evm/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<u8>) -> 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<Self> {
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<u8>) -> 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<Self> {
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,
Expand All @@ -129,6 +192,43 @@ pub struct BlockMetadataTarget {
pub block_base_fee: Target,
}

impl BlockMetadataTarget {
pub fn to_buffer(&self, buffer: &mut Vec<u8>) -> 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<Self> {
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<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize> {
/// Merkle cap of LDEs of trace values.
Expand Down

0 comments on commit 00b7c18

Please sign in to comment.