bankai_verify/evm/beacon.rs
1extern crate alloc;
2
3use alloy_primitives::FixedBytes;
4use bankai_types::fetch::evm::beacon::BeaconHeaderProof;
5use bankai_types::verify::evm::beacon::BeaconHeader;
6use tree_hash::TreeHash;
7
8use crate::bankai::mmr::MmrVerifier;
9use crate::VerifyError;
10
11/// Verifier for EVM beacon chain (consensus layer) proofs
12///
13/// Provides methods to verify beacon chain headers against trusted MMR roots.
14/// Beacon headers contain consensus layer information including validator data,
15/// randao values, and execution payload commitments.
16pub struct BeaconVerifier;
17
18impl BeaconVerifier {
19 /// Verifies a beacon chain header using an MMR inclusion proof
20 ///
21 /// This method establishes trust in a beacon chain header by:
22 /// 1. Verifying the MMR root matches the expected root from the STWO proof
23 /// 2. Verifying the MMR inclusion proof
24 /// 3. Verifying the header's tree hash root matches the value committed in the MMR
25 ///
26 /// Once verified, the beacon header can be trusted and used to verify consensus layer data.
27 ///
28 /// # Arguments
29 ///
30 /// * `proof` - The beacon header proof containing the header and MMR inclusion proof
31 /// * `root` - The trusted MMR root from the verified STWO proof
32 ///
33 /// # Returns
34 ///
35 /// Returns the verified `BeaconHeader` containing all beacon chain data including:
36 /// - Slot number
37 /// - Proposer index
38 /// - Parent root
39 /// - State root
40 /// - Body root (contains execution payload commitment)
41 ///
42 /// # Errors
43 ///
44 /// Returns an error if:
45 /// - `InvalidMmrRoot`: The MMR root in the proof doesn't match the expected root
46 /// - `InvalidMmrProof`: The MMR inclusion proof is invalid
47 /// - `InvalidHeaderHash`: The header's tree hash root doesn't match the MMR commitment
48 ///
49 /// # Example
50 ///
51 /// ```no_run
52 /// use bankai_verify::evm::BeaconVerifier;
53 /// use bankai_types::fetch::evm::beacon::BeaconHeaderProof;
54 /// use alloy_primitives::FixedBytes;
55 ///
56 /// # fn example(proof: BeaconHeaderProof, mmr_root: FixedBytes<32>) -> Result<(), Box<dyn std::error::Error>> {
57 /// let verified_header = BeaconVerifier::verify_header_proof(&proof, mmr_root)?;
58 /// println!("Verified beacon slot {}", verified_header.slot);
59 /// println!("Proposer index: {}", verified_header.proposer_index);
60 /// # Ok(())
61 /// # }
62 /// ```
63 pub fn verify_header_proof(
64 proof: &BeaconHeaderProof,
65 root: FixedBytes<32>,
66 ) -> Result<BeaconHeader, VerifyError> {
67 if proof.mmr_proof.root != root {
68 return Err(VerifyError::InvalidMmrRoot);
69 }
70
71 MmrVerifier::verify_mmr_proof(&proof.mmr_proof.clone())?;
72
73 let hash = proof.header.tree_hash_root();
74 if hash != proof.mmr_proof.header_hash {
75 return Err(VerifyError::InvalidHeaderHash);
76 }
77
78 Ok(proof.header.clone())
79 }
80}