From bab3328c06db22fac34d602b8eaed36a0aa4166e Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Tue, 7 Jan 2025 16:59:04 -0700 Subject: [PATCH] Calculate class_commitment from proof --- crates/rpc-client/src/pathfinder/proofs.rs | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/rpc-client/src/pathfinder/proofs.rs b/crates/rpc-client/src/pathfinder/proofs.rs index f0315a40..ef1ead53 100644 --- a/crates/rpc-client/src/pathfinder/proofs.rs +++ b/crates/rpc-client/src/pathfinder/proofs.rs @@ -54,6 +54,9 @@ pub enum ProofVerificationError<'a> { #[error("Proof verification failed, node_hash {node_hash:x} != parent_hash {parent_hash:x}")] InvalidChildNodeHash { node_hash: Felt, parent_hash: Felt }, + #[error("Proof is empty")] + EmptyProof, + #[error("Conversion error")] ConversionError, } @@ -84,14 +87,28 @@ pub struct PathfinderProof { #[allow(dead_code)] #[derive(Clone, Deserialize)] pub struct PathfinderClassProof { - pub class_commitment: Felt, pub class_proof: Vec, } impl PathfinderClassProof { - /// Verifies that the class proof is valid. + pub fn verify(&self, class_hash: Felt) -> Result<(), ProofVerificationError> { - verify_proof::(class_hash, self.class_commitment, &self.class_proof) + verify_proof::(class_hash, self.class_commitment()?, &self.class_proof) + } + + /// Gets the "class_commitment" which is aka the root node of the class Merkle tree. + /// Pathfinder used to provide this explicitly, but stopped doing so in #2452: + /// + /// https://github.com/eqlabs/pathfinder/pull/2452 + /// + /// However, the proof should always start with the root node, which means all we have + /// to do is hash the first node in the proof to get the same thing. + pub fn class_commitment(&self) -> Result { + return if self.class_proof.len() > 0 { + Ok(self.class_proof[0].hash::()) + } else { + Err(ProofVerificationError::EmptyProof) // TODO: give an error type or change fn return type + }; } }