Skip to content

Commit

Permalink
Calculate class_commitment from proof
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh committed Jan 7, 2025
1 parent 6e0514d commit bab3328
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions crates/rpc-client/src/pathfinder/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -84,14 +87,28 @@ pub struct PathfinderProof {
#[allow(dead_code)]
#[derive(Clone, Deserialize)]
pub struct PathfinderClassProof {
pub class_commitment: Felt,
pub class_proof: Vec<TrieNode>,
}

impl PathfinderClassProof {
/// Verifies that the class proof is valid.

pub fn verify(&self, class_hash: Felt) -> Result<(), ProofVerificationError> {
verify_proof::<PoseidonHash>(class_hash, self.class_commitment, &self.class_proof)
verify_proof::<PoseidonHash>(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<Felt, ProofVerificationError> {
return if self.class_proof.len() > 0 {
Ok(self.class_proof[0].hash::<PoseidonHash>())
} else {
Err(ProofVerificationError::EmptyProof) // TODO: give an error type or change fn return type
};
}
}

Expand Down

0 comments on commit bab3328

Please sign in to comment.