Skip to content

Commit 69992e1

Browse files
committed
functionality to verify proofs
1 parent ebb517c commit 69992e1

File tree

2 files changed

+85
-23
lines changed

2 files changed

+85
-23
lines changed

crates/verifier/src/compressed/internal.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,19 @@ pub const RECURSION_VK_SET: &[[u32; 8]] = &[
5757
/// Then, calls [`verify_sp1_reduce_proof`] and returns the result.
5858
pub fn verify_sp1_proof(
5959
sp1_proof: &SP1Proof,
60-
sp1_public_inputs: &[u8],
6160
vkey_hash: &[BabyBear; 8],
6261
) -> Result<(), CompressedError> {
6362
let SP1Proof::Compressed(reduce_proof) = sp1_proof else {
6463
return Err(CompressedError::Mode(sp1_proof.into()));
6564
};
66-
verify_sp1_reduce_proof(reduce_proof.as_ref(), sp1_public_inputs, vkey_hash)
65+
verify_sp1_reduce_proof(reduce_proof.as_ref(), vkey_hash)
6766
}
6867

6968
// The rest of the functions in this file have been copied from elsewhere with slight modifications.
7069

7170
/// Verify a compressed proof.
7271
pub fn verify_sp1_reduce_proof(
7372
reduce_proof: &SP1ReduceProof<SC>,
74-
sp1_public_inputs: &[u8],
7573
vkey_hash: &[BabyBear; 8],
7674
) -> Result<(), CompressedError> {
7775
let SP1ReduceProof { vk: compress_vk, proof } = reduce_proof;
@@ -89,20 +87,6 @@ pub fn verify_sp1_reduce_proof(
8987
let machine_proof = MachineProof { shard_proofs: vec![proof.clone()] };
9088
compress_machine.verify(compress_vk, &machine_proof, &mut challenger)?;
9189

92-
// Validate the SP1 public values against the committed digest.
93-
let committed_value_digest_bytes = public_values
94-
.committed_value_digest
95-
.iter()
96-
.flat_map(|w| w.0.iter().map(|x| x.as_canonical_u32() as u8))
97-
.collect::<Vec<_>>();
98-
99-
if committed_value_digest_bytes.as_slice() != hash_public_inputs(sp1_public_inputs).as_slice() &&
100-
committed_value_digest_bytes.as_slice() !=
101-
hash_public_inputs_with_fn(sp1_public_inputs, blake3_hash)
102-
{
103-
return Err(CompressedError::PublicValuesMismatch);
104-
}
105-
10690
// Validate recursion's public values.
10791
if !is_recursion_public_values_valid(compress_machine.config(), public_values) {
10892
return Err(MachineVerificationError::InvalidPublicValues(
@@ -134,6 +118,55 @@ pub fn verify_sp1_reduce_proof(
134118
Ok(())
135119
}
136120

121+
/// Verify a compressed proof.
122+
pub fn verify_sp1_public_values(
123+
reduce_proof: &SP1ReduceProof<SC>,
124+
sp1_public_inputs: &[u8],
125+
) -> Result<(), CompressedError> {
126+
let SP1ReduceProof { proof, .. } = reduce_proof;
127+
128+
let public_values: &RecursionPublicValues<_> = proof.public_values.as_slice().borrow();
129+
// Validate the SP1 public values against the committed digest.
130+
let committed_value_digest_bytes = public_values
131+
.committed_value_digest
132+
.iter()
133+
.flat_map(|w| w.0.iter().map(|x| x.as_canonical_u32() as u8))
134+
.collect::<Vec<_>>();
135+
136+
if committed_value_digest_bytes.as_slice() != hash_public_inputs(sp1_public_inputs).as_slice()
137+
&& committed_value_digest_bytes.as_slice()
138+
!= hash_public_inputs_with_fn(sp1_public_inputs, blake3_hash)
139+
{
140+
return Err(CompressedError::PublicValuesMismatch);
141+
}
142+
143+
Ok(())
144+
}
145+
146+
/// Verify a compressed proof with public values.
147+
pub fn verify_sp1_proof_with_public_values(
148+
sp1_proof: &SP1Proof,
149+
sp1_public_inputs: &[u8],
150+
vkey_hash: &[BabyBear; 8],
151+
) -> Result<(), CompressedError> {
152+
let SP1Proof::Compressed(reduce_proof) = sp1_proof else {
153+
return Err(CompressedError::Mode(sp1_proof.into()));
154+
};
155+
verify_sp1_reduce_proof_with_public_values(reduce_proof, sp1_public_inputs, vkey_hash)
156+
}
157+
158+
/// Verify a compressed proof.
159+
pub fn verify_sp1_reduce_proof_with_public_values(
160+
reduce_proof: &SP1ReduceProof<SC>,
161+
sp1_public_inputs: &[u8],
162+
vkey_hash: &[BabyBear; 8],
163+
) -> Result<(), CompressedError> {
164+
// Verify the proof
165+
verify_sp1_reduce_proof(reduce_proof, vkey_hash)?;
166+
// Verify the public values against the committed digest
167+
verify_sp1_public_values(reduce_proof, sp1_public_inputs)
168+
}
169+
137170
/// Compute the digest of the public values.
138171
fn recursion_public_values_digest(
139172
config: &SC,

crates/verifier/src/compressed/mod.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use thiserror::Error;
66

77
pub mod internal;
88

9-
use internal::{verify_sp1_proof, verify_sp1_reduce_proof, F, SC};
9+
use internal::{
10+
verify_sp1_proof, verify_sp1_proof_with_public_values, verify_sp1_reduce_proof,
11+
verify_sp1_reduce_proof_with_public_values, F, SC,
12+
};
1013

1114
/// A reason why the verifier rejects a given proof.
1215
#[derive(Debug, Error)]
@@ -51,29 +54,55 @@ impl CompressedVerifier {
5154
/// let sp1_vkey_hash = bincode::serialize(&vk.hash_babybear()).unwrap();
5255
/// let proof: SP1Proof = match client.prove(&pk, &stdin).compressed().run().unwrap().proof;
5356
/// ```
54-
pub fn verify_sp1_proof(
57+
pub fn verify_sp1_proof(sp1_proof: &[u8], sp1_vkey_hash: &[u8]) -> Result<(), CompressedError> {
58+
let sp1_proof: SP1Proof =
59+
bincode::deserialize(sp1_proof).map_err(CompressedError::DeserializeProof)?;
60+
let vkey_hash: [F; 8] = deserialize_vkey(sp1_vkey_hash)?;
61+
62+
verify_sp1_proof(&sp1_proof, &vkey_hash)?;
63+
64+
Ok(())
65+
}
66+
67+
pub fn verify_sp1_proof_with_public_values(
5568
sp1_proof: &[u8],
5669
sp1_public_inputs: &[u8],
5770
sp1_vkey_hash: &[u8],
5871
) -> Result<(), CompressedError> {
5972
let sp1_proof: SP1Proof =
6073
bincode::deserialize(sp1_proof).map_err(CompressedError::DeserializeProof)?;
6174
let vkey_hash: [F; 8] = deserialize_vkey(sp1_vkey_hash)?;
62-
63-
verify_sp1_proof(&sp1_proof, sp1_public_inputs, &vkey_hash)?;
75+
verify_sp1_proof_with_public_values(&sp1_proof, sp1_public_inputs, &vkey_hash)?;
6476

6577
Ok(())
6678
}
79+
6780
pub fn verify_sp1_reduce_proof(
6881
sp1_reduce_proof: &[u8],
69-
sp1_public_inputs: &[u8],
7082
sp1_vkey_hash: &[u8],
7183
) -> Result<(), CompressedError> {
7284
let reduce_proof: Box<SP1ReduceProof<SC>> =
7385
bincode::deserialize(sp1_reduce_proof).map_err(CompressedError::DeserializeProof)?;
7486
let vkey_hash: [F; 8] = deserialize_vkey(sp1_vkey_hash)?;
7587

76-
verify_sp1_reduce_proof(reduce_proof.as_ref(), sp1_public_inputs, &vkey_hash)?;
88+
verify_sp1_reduce_proof(reduce_proof.as_ref(), &vkey_hash)?;
89+
90+
Ok(())
91+
}
92+
93+
pub fn verify_sp1_reduce_proof_with_public_values(
94+
sp1_reduce_proof: &[u8],
95+
sp1_public_inputs: &[u8],
96+
sp1_vkey_hash: &[u8],
97+
) -> Result<(), CompressedError> {
98+
let reduce_proof: Box<SP1ReduceProof<SC>> =
99+
bincode::deserialize(sp1_reduce_proof).map_err(CompressedError::DeserializeProof)?;
100+
let vkey_hash: [F; 8] = deserialize_vkey(sp1_vkey_hash)?;
101+
verify_sp1_reduce_proof_with_public_values(
102+
reduce_proof.as_ref(),
103+
sp1_public_inputs,
104+
&vkey_hash,
105+
)?;
77106

78107
Ok(())
79108
}

0 commit comments

Comments
 (0)