Skip to content

Commit 69db30c

Browse files
committed
feat: verify proofs
1 parent 993333d commit 69db30c

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

aggregation-mode/src/aggregator/interface.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
use super::sp1::{self, SP1AggregatedProof};
1+
use super::sp1::{self, SP1AggregatedProof, SP1Proof, SP1VerificationError};
22
use serde::{Deserialize, Serialize};
3-
use sp1_aggregator::SP1CompressedProof;
43

54
#[derive(Serialize, Deserialize)]
6-
pub enum InputProofs {
7-
SP1Compressed(Vec<SP1CompressedProof>),
8-
}
9-
10-
#[derive(Serialize, Deserialize)]
11-
pub struct ProgramInput {
12-
proofs: InputProofs,
13-
}
14-
15-
impl ProgramInput {
16-
pub fn new(proofs: InputProofs) -> Self {
17-
ProgramInput { proofs }
18-
}
5+
pub enum ProgramInput {
6+
SP1(sp1_aggregator::Input),
197
}
208

219
pub enum AggregatedProof {
@@ -44,7 +32,21 @@ pub enum AggregatedVerificationError {
4432
}
4533

4634
pub fn aggregate_proofs(input: ProgramInput) -> Result<ProgramOutput, AggregatedVerificationError> {
47-
match input.proofs {
48-
InputProofs::SP1Compressed(proofs) => sp1::aggregate_proofs(proofs),
35+
match input {
36+
ProgramInput::SP1(input) => sp1::aggregate_proofs(input),
37+
}
38+
}
39+
40+
enum Proof {
41+
SP1(SP1Proof),
42+
}
43+
44+
enum VerificationError {
45+
SP1(SP1VerificationError),
46+
}
47+
48+
pub fn verify_proof(proof: &Proof) -> Result<(), VerificationError> {
49+
match proof {
50+
Proof::SP1(proof) => sp1::verify(proof).map_err(VerificationError::SP1),
4951
}
5052
}

aggregation-mode/src/aggregator/sp1.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
use sp1_aggregator::SP1CompressedProof;
21
use sp1_sdk::{Prover, ProverClient, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey};
32

43
use super::interface::{AggregatedProof, AggregatedVerificationError, ProgramOutput};
54

65
const PROGRAM_ELF: &[u8] = include_bytes!("../../zkvm/sp1/elf/sp1_aggregator_program");
76

7+
// TODO lock prover
8+
9+
pub struct SP1Proof {
10+
pub elf: Vec<u8>,
11+
pub proof: Vec<u8>,
12+
}
13+
814
pub struct SP1AggregatedProof {
915
pub proof: SP1ProofWithPublicValues,
1016
pub vk: SP1VerifyingKey,
1117
}
1218

1319
pub(crate) fn aggregate_proofs(
14-
proofs: Vec<SP1CompressedProof>,
20+
input: sp1_aggregator::Input,
1521
) -> Result<ProgramOutput, AggregatedVerificationError> {
1622
let mut stdin = SP1Stdin::new();
17-
stdin.write(&proofs);
23+
stdin.write(&input);
1824

1925
#[cfg(feature = "prove")]
2026
let client = ProverClient::from_env();
@@ -34,7 +40,28 @@ pub(crate) fn aggregate_proofs(
3440
.verify(&proof, &vk)
3541
.map_err(AggregatedVerificationError::SP1Verification)?;
3642

37-
let output = ProgramOutput::new(AggregatedProof::SP1(SP1AggregatedProof { proof, vk }));
43+
let proof = SP1AggregatedProof { proof, vk };
44+
45+
let output = ProgramOutput::new(AggregatedProof::SP1(proof));
3846

3947
Ok(output)
4048
}
49+
50+
pub enum SP1VerificationError {
51+
Verification(sp1_sdk::SP1VerificationError),
52+
DecodeProofBinary,
53+
}
54+
55+
pub(crate) fn verify(proof: &SP1Proof) -> Result<(), SP1VerificationError> {
56+
let client = ProverClient::from_env();
57+
58+
let (_pk, vk) = client.setup(&proof.elf);
59+
60+
if let Ok(proof) = bincode::deserialize(&proof.proof) {
61+
client
62+
.verify(&proof, &vk)
63+
.map_err(SP1VerificationError::Verification)
64+
} else {
65+
Err(SP1VerificationError::DecodeProofBinary)
66+
}
67+
}

aggregation-mode/zkvm/sp1/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ impl SP1CompressedProof {
2727
hasher.finalize().into()
2828
}
2929
}
30+
31+
#[derive(Serialize, Deserialize)]
32+
pub enum Proof {
33+
SP1Compressed(SP1CompressedProof),
34+
}
35+
36+
impl Proof {
37+
pub fn hash(&self) -> [u8; 32] {
38+
match self {
39+
Proof::SP1Compressed(proof) => proof.hash(),
40+
}
41+
}
42+
}
43+
44+
#[derive(Serialize, Deserialize)]
45+
pub struct Input {
46+
pub proofs: Vec<Proof>,
47+
}

aggregation-mode/zkvm/sp1/src/main.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sp1_zkvm::entrypoint!(main);
33

44
use sha2::{Digest, Sha256};
5-
use sp1_aggregator::SP1CompressedProof;
5+
use sp1_aggregator::{Input, Proof};
66

77
fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
88
let mut hasher = Sha256::new();
@@ -12,7 +12,7 @@ fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
1212
}
1313

1414
/// Computes the merkle root for the given proofs using the vk
15-
fn compute_merkle_root(proofs: &[SP1CompressedProof]) -> [u8; 32] {
15+
fn compute_merkle_root(proofs: &[Proof]) -> [u8; 32] {
1616
let mut leaves: Vec<[u8; 32]> = proofs
1717
.chunks(2)
1818
.map(|chunk| match chunk {
@@ -38,16 +38,20 @@ fn compute_merkle_root(proofs: &[SP1CompressedProof]) -> [u8; 32] {
3838

3939
// TODO: Update input and use AlignedVerificationData
4040
pub fn main() {
41-
let input = sp1_zkvm::io::read::<Vec<SP1CompressedProof>>();
41+
let input = sp1_zkvm::io::read::<Input>();
4242

4343
// Verify the proofs.
44-
for proof in input.iter() {
45-
let vkey = proof.vk();
46-
let public_values = &proof.public_inputs;
47-
let public_values_digest = Sha256::digest(public_values);
48-
sp1_zkvm::lib::verify::verify_sp1_proof(&vkey, &public_values_digest.into());
44+
for proof in input.proofs.iter() {
45+
match proof {
46+
Proof::SP1Compressed(proof) => {
47+
let vkey = proof.vk();
48+
let public_values = &proof.public_inputs;
49+
let public_values_digest = Sha256::digest(public_values);
50+
sp1_zkvm::lib::verify::verify_sp1_proof(&vkey, &public_values_digest.into());
51+
}
52+
}
4953
}
5054

51-
let merkle_root = compute_merkle_root(&input);
55+
let merkle_root = compute_merkle_root(&input.proofs);
5256
sp1_zkvm::io::commit_slice(&merkle_root);
5357
}

0 commit comments

Comments
 (0)