Skip to content

Commit

Permalink
Add proof of work verification error. (#503)
Browse files Browse the repository at this point in the history
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/stwo/503)
<!-- Reviewable:end -->
  • Loading branch information
alonh5 authored Mar 20, 2024
1 parent a4a48c7 commit a6b9f2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/core/proof_of_work.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use thiserror::Error;

use super::channel::Blake2sChannel;
use crate::commitment_scheme::blake2_hash::{Blake2sHash, Blake2sHasher};
use crate::commitment_scheme::hasher::Hasher;
Expand Down Expand Up @@ -26,17 +28,23 @@ impl ProofOfWork {
proof
}

pub fn verify(&self, channel: &mut Blake2sChannel, proof: &ProofOfWorkProof) -> bool {
pub fn verify(
&self,
channel: &mut Blake2sChannel,
proof: &ProofOfWorkProof,
) -> Result<(), ProofOfWorkVerificationError> {
let seed = channel.get_digest().as_ref().to_vec();
let verified = check_leading_zeros(
self.hash_with_nonce(&seed, proof.nonce).as_ref(),
self.n_bits,
);

if verified {
channel.mix_nonce(proof.nonce);
if !verified {
return Err(ProofOfWorkVerificationError::ProofOfWorkVerificationFailed);
}
verified

channel.mix_nonce(proof.nonce);
Ok(())
}

fn grind(&self, seed: Vec<u8>) -> ProofOfWorkProof {
Expand Down Expand Up @@ -76,6 +84,12 @@ fn check_leading_zeros(bytes: &[u8], bound_bits: u32) -> bool {
n_bits >= bound_bits
}

#[derive(Clone, Copy, Debug, Error)]
pub enum ProofOfWorkVerificationError {
#[error("Proof of work verification failed.")]
ProofOfWorkVerificationFailed,
}

#[cfg(test)]
mod tests {
use crate::commitment_scheme::blake2_hash::Blake2sHash;
Expand All @@ -88,7 +102,7 @@ mod tests {
let proof_of_work_prover = ProofOfWork { n_bits: 11 };
let proof = ProofOfWorkProof { nonce: 133 };

assert!(proof_of_work_prover.verify(&mut channel, &proof));
proof_of_work_prover.verify(&mut channel, &proof).unwrap();
}

#[test]
Expand All @@ -97,7 +111,9 @@ mod tests {
let proof_of_work_prover = ProofOfWork { n_bits: 1 };
let invalid_proof = ProofOfWorkProof { nonce: 0 };

assert!(!proof_of_work_prover.verify(&mut channel, &invalid_proof));
proof_of_work_prover
.verify(&mut channel, &invalid_proof)
.unwrap_err();
}

#[test]
Expand All @@ -109,9 +125,8 @@ mod tests {
let verifier = ProofOfWork::new(n_bits);

let proof = prover.prove(&mut prover_channel);
let verified = verifier.verify(&mut verifier_channel, &proof);
verifier.verify(&mut verifier_channel, &proof).unwrap();

assert!(verified);
assert_eq!(prover_channel.get_digest(), verifier_channel.get_digest());
}
}
5 changes: 4 additions & 1 deletion src/core/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use thiserror::Error;

use super::fri::FriVerificationError;
use super::poly::circle::{CanonicCoset, MAX_CIRCLE_DOMAIN_LOG_SIZE};
use super::proof_of_work::ProofOfWorkVerificationError;
use super::queries::SparseSubCircleDomain;
use super::ColumnVec;
use crate::commitment_scheme::blake2_hash::Blake2sHasher;
Expand Down Expand Up @@ -193,7 +194,7 @@ pub fn verify(
let fri_config = FriConfig::new(LOG_LAST_LAYER_DEGREE_BOUND, LOG_BLOWUP_FACTOR, N_QUERIES);
let mut fri_verifier = FriVerifier::commit(channel, fri_config, proof.fri_proof, bounds)?;

ProofOfWork::new(PROOF_OF_WORK_BITS).verify(channel, &proof.proof_of_work);
ProofOfWork::new(PROOF_OF_WORK_BITS).verify(channel, &proof.proof_of_work)?;
let opening_positions = fri_verifier
.column_opening_positions(channel)
.into_values()
Expand Down Expand Up @@ -311,6 +312,8 @@ pub enum VerificationError {
OodsNotMatching,
#[error(transparent)]
Fri(#[from] FriVerificationError),
#[error(transparent)]
ProofOfWork(#[from] ProofOfWorkVerificationError),
}

#[cfg(test)]
Expand Down

0 comments on commit a6b9f2c

Please sign in to comment.