Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate class_commitment from proof #460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading