diff --git a/crates/aggchain-proof-core/src/bridge/inserted_ger.rs b/crates/aggchain-proof-core/src/bridge/inserted_ger.rs index 879655cd..05bad217 100644 --- a/crates/aggchain-proof-core/src/bridge/inserted_ger.rs +++ b/crates/aggchain-proof-core/src/bridge/inserted_ger.rs @@ -2,6 +2,18 @@ use agglayer_primitives::Digest; use serde::{Deserialize, Serialize}; use unified_bridge::{L1InfoTreeLeaf, MerkleProof}; +/// Errors that can occur during GER verification +#[derive(Debug, thiserror::Error)] +pub enum GerVerificationError { + /// The provided L1 info root does not match the proof root + #[error("L1 info root mismatch: expected {expected:?}, got {got:?}")] + L1InfoRootMismatch { expected: Digest, got: Digest }, + + /// The Merkle proof verification failed + #[error("Merkle proof verification failed for leaf hash {leaf_hash:?} at index {index}")] + MerkleProofVerificationFailed { leaf_hash: Digest, index: u32 }, +} + /// Data to verify the legitimacy of one inserted GER. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct InsertedGER { @@ -17,16 +29,25 @@ pub struct InsertedGER { impl InsertedGER { /// Verify the inclusion proof against one L1 info root. - pub fn verify(&self, l1_info_root: Digest) -> bool { - // TODO: return differentiated errors + pub fn verify(&self, l1_info_root: Digest) -> Result<(), GerVerificationError> { if l1_info_root != self.proof.root { - return false; + return Err(GerVerificationError::L1InfoRootMismatch { + expected: self.proof.root, + got: l1_info_root, + }); + } + + let leaf_hash = self.l1_info_tree_leaf.hash(); + let index = self.l1_info_tree_leaf.l1_info_tree_index; + + if !self.proof.verify(leaf_hash, index) { + return Err(GerVerificationError::MerkleProofVerificationFailed { + leaf_hash, + index, + }); } - self.proof.verify( - self.l1_info_tree_leaf.hash(), - self.l1_info_tree_leaf.l1_info_tree_index, - ) + Ok(()) } /// Returns the inserted GER. diff --git a/crates/aggchain-proof-core/src/bridge/mod.rs b/crates/aggchain-proof-core/src/bridge/mod.rs index a7946d3f..856f7ceb 100644 --- a/crates/aggchain-proof-core/src/bridge/mod.rs +++ b/crates/aggchain-proof-core/src/bridge/mod.rs @@ -384,7 +384,7 @@ impl BridgeConstraintsInput { .bridge_witness .inserted_gers .iter() - .find(|ger| !ger.verify(self.l1_info_root)); + .find(|ger| ger.verify(self.l1_info_root).is_err()); if let Some(wrong_ger) = maybe_wrong_inserted_ger { return Err(BridgeConstraintsError::InvalidMerklePathGERToL1Root {