Skip to content

Commit c475a63

Browse files
committed
feat: error handling + fetch risc0 proofs
1 parent 62bfb60 commit c475a63

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

aggregation_mode/aggregation_programs/risc0/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tiny_keccak::{Hasher, Keccak};
33

44
#[derive(Serialize, Deserialize)]
55
pub struct Risc0ImageIdAndPubInputs {
6-
pub image_id: [u32; 8],
6+
pub image_id: [u8; 32],
77
pub public_inputs: Vec<u8>,
88
}
99

aggregation_mode/src/aggregators/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use risc0_zkvm::Receipt;
2-
31
use super::{
4-
risc0_aggregator::{self, Risc0AggregationInput},
2+
risc0_aggregator::{self, Risc0AggregationInput, Risc0ProofReceiptAndImageId},
53
sp1_aggregator::{self, SP1AggregationInput, SP1ProofWithPubValuesAndElf},
64
};
75

@@ -12,7 +10,7 @@ pub enum ProgramInput {
1210

1311
pub enum AggregatedProof {
1412
SP1(SP1ProofWithPubValuesAndElf),
15-
Risc0(Receipt),
13+
Risc0(Risc0ProofReceiptAndImageId),
1614
}
1715

1816
pub struct ProgramOutput {
@@ -29,6 +27,7 @@ impl ProgramOutput {
2927
pub enum ProofAggregationError {
3028
SP1Verification(sp1_sdk::SP1VerificationError),
3129
SP1Proving,
30+
Risc0Proving,
3231
UnsupportedProof,
3332
}
3433

aggregation_mode/src/aggregators/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod lib;
22
pub mod risc0_aggregator;
33
pub mod sp1_aggregator;
44

5-
use risc0_aggregator::{AlignedRisc0VerificationError, Risc0ProofWithPubValuesAndImageId};
5+
use risc0_aggregator::{AlignedRisc0VerificationError, Risc0ProofReceiptAndImageId};
66
use sp1_aggregator::{AlignedSP1VerificationError, SP1ProofWithPubValuesAndElf};
77
pub enum ZKVMEngine {
88
SP1,
@@ -11,7 +11,7 @@ pub enum ZKVMEngine {
1111

1212
pub enum AlignedProof {
1313
SP1(SP1ProofWithPubValuesAndElf),
14-
Risc0(Risc0ProofWithPubValuesAndImageId),
14+
Risc0(Risc0ProofReceiptAndImageId),
1515
}
1616

1717
impl AlignedProof {
@@ -38,7 +38,7 @@ impl AlignedProof {
3838
},
3939
),
4040
AlignedProof::Risc0(proof) => {
41-
risc0_aggregator::verify(&proof.receipt).map_err(AlignedVerificationError::Risc0)
41+
risc0_aggregator::verify(proof).map_err(AlignedVerificationError::Risc0)
4242
}
4343
}
4444
}
Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
include!(concat!(env!("OUT_DIR"), "/methods.rs"));
22

33
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, Receipt};
4+
use sha3::{Digest, Keccak256};
45

56
use super::lib::{AggregatedProof, ProgramOutput, ProofAggregationError};
67

7-
pub struct Risc0ProofWithPubValuesAndImageId {
8-
pub image_id: [u32; 8],
8+
const RISC0_AGGREGATOR_PROGRAM_ID_BYTES: [u8; 32] = {
9+
let mut res = [0u8; 32];
10+
let mut i = 0;
11+
while i < 8 {
12+
let bytes = RISC0_AGGREGATOR_PROGRAM_ID[i].to_be_bytes();
13+
res[i * 4] = bytes[0];
14+
res[i * 4 + 1] = bytes[1];
15+
res[i * 4 + 2] = bytes[2];
16+
res[i * 4 + 3] = bytes[3];
17+
i += 1;
18+
}
19+
res
20+
};
21+
22+
pub struct Risc0ProofReceiptAndImageId {
23+
pub image_id: [u8; 32],
924
pub receipt: Receipt,
10-
pub public_values: Vec<u8>,
1125
}
1226

13-
impl Risc0ProofWithPubValuesAndImageId {
27+
impl Risc0ProofReceiptAndImageId {
28+
pub fn public_inputs(&self) -> &Vec<u8> {
29+
&self.receipt.journal.bytes
30+
}
31+
}
32+
33+
impl Risc0ProofReceiptAndImageId {
1434
pub fn hash_image_id_and_public_inputs(&self) -> [u8; 32] {
15-
[0u8; 32]
35+
let mut hasher = Keccak256::new();
36+
hasher.update(&self.image_id);
37+
hasher.update(self.public_inputs());
38+
hasher.finalize().into()
1639
}
1740
}
1841

1942
pub struct Risc0AggregationInput {
20-
pub receipts: Vec<Risc0ProofWithPubValuesAndImageId>,
43+
pub receipts: Vec<Risc0ProofReceiptAndImageId>,
2144
pub merkle_root: [u8; 32],
2245
}
2346

@@ -28,39 +51,57 @@ pub(crate) fn aggregate_proofs(
2851

2952
// write assumptions and proof image id + pub inputs
3053
let mut proofs_image_id_and_pub_inputs = vec![];
31-
for r in input.receipts {
54+
for proof in input.receipts {
3255
proofs_image_id_and_pub_inputs.push(risc0_aggregation_program::Risc0ImageIdAndPubInputs {
33-
image_id: r.image_id,
34-
public_inputs: r.public_values,
56+
image_id: proof.image_id,
57+
public_inputs: proof.receipt.journal.bytes.clone(),
3558
});
36-
env_builder.add_assumption(r.receipt);
59+
env_builder.add_assumption(proof.receipt);
3760
}
3861

3962
// write input data
4063
let input = risc0_aggregation_program::Input {
4164
merkle_root: input.merkle_root,
4265
proofs_image_id_and_pub_inputs,
4366
};
44-
env_builder.write(&input).unwrap();
67+
env_builder
68+
.write(&input)
69+
.map_err(|_| ProofAggregationError::Risc0Proving)?;
4570

46-
let env = env_builder.build().unwrap();
71+
let env = env_builder
72+
.build()
73+
.map_err(|_| ProofAggregationError::Risc0Proving)?;
4774

4875
let prover = default_prover();
4976
let receipt = prover
5077
.prove_with_opts(env, RISC0_AGGREGATOR_PROGRAM_ELF, &ProverOpts::groth16())
51-
.unwrap()
78+
.map_err(|_| ProofAggregationError::Risc0Proving)?
5279
.receipt;
5380

54-
Ok(ProgramOutput::new(AggregatedProof::Risc0(receipt)))
81+
let output = Risc0ProofReceiptAndImageId {
82+
image_id: RISC0_AGGREGATOR_PROGRAM_ID_BYTES,
83+
receipt,
84+
};
85+
86+
Ok(ProgramOutput::new(AggregatedProof::Risc0(output)))
5587
}
5688

5789
#[derive(Debug)]
5890
pub enum AlignedRisc0VerificationError {
59-
Verification,
91+
Verification(String),
6092
UnsupportedProof,
6193
}
6294

63-
pub(crate) fn verify(receipt: &Receipt) -> Result<(), AlignedRisc0VerificationError> {
64-
// TODO validate and verify receipt is of type Compressed, as only they can be aggregated recursively
65-
Ok(())
95+
pub(crate) fn verify(
96+
proof: &Risc0ProofReceiptAndImageId,
97+
) -> Result<(), AlignedRisc0VerificationError> {
98+
// only composite proofs are supported for recursion
99+
if proof.receipt.inner.composite().is_err() {
100+
Err(AlignedRisc0VerificationError::UnsupportedProof)
101+
} else {
102+
proof
103+
.receipt
104+
.verify(proof.image_id)
105+
.map_err(|e| AlignedRisc0VerificationError::Verification(e.to_string()))
106+
}
66107
}

aggregation_mode/src/backend/fetcher.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ use super::{
55
types::{AlignedLayerServiceManager, AlignedLayerServiceManagerContract, RPCProvider},
66
};
77
use crate::{
8-
aggregators::{sp1_aggregator::SP1ProofWithPubValuesAndElf, AlignedProof},
8+
aggregators::{
9+
risc0_aggregator::Risc0ProofReceiptAndImageId, sp1_aggregator::SP1ProofWithPubValuesAndElf,
10+
AlignedProof,
11+
},
912
backend::s3::get_aligned_batch_from_s3,
1013
};
1114
use aligned_sdk::core::types::ProvingSystemId;
1215
use alloy::{
1316
primitives::Address,
1417
providers::{Provider, ProviderBuilder},
1518
};
19+
use risc0_zkvm::Receipt;
1620
use tracing::{error, info};
1721

1822
#[derive(Debug)]
@@ -82,7 +86,7 @@ impl ProofsFetcher {
8286

8387
info!("Data downloaded from S3, number of proofs {}", data.len());
8488

85-
// Filter SP1 compressed proofs to and push to queue to be aggregated
89+
// Filter compatible proofs to be aggregated and push to queue
8690
let proofs_to_add: Vec<AlignedProof> = data
8791
.into_iter()
8892
.filter_map(|p| match p.proving_system {
@@ -96,12 +100,24 @@ impl ProofsFetcher {
96100

97101
Some(AlignedProof::SP1(sp1_proof))
98102
}
103+
ProvingSystemId::Risc0 => {
104+
let mut image_id = [0u8; 32];
105+
image_id.copy_from_slice(p.vm_program_code?.as_slice());
106+
let public_inputs = p.pub_input?;
107+
let inner_receipt: risc0_zkvm::InnerReceipt =
108+
bincode::deserialize(&p.proof).ok()?;
109+
110+
let receipt = Receipt::new(inner_receipt, public_inputs);
111+
let risc0_proof = Risc0ProofReceiptAndImageId { image_id, receipt };
112+
113+
Some(AlignedProof::Risc0(risc0_proof))
114+
}
99115
_ => None,
100116
})
101117
.collect();
102118

103119
info!(
104-
"SP1 proofs filtered, total proofs to add {}",
120+
"Proofs filtered, total proofs to add {}",
105121
proofs_to_add.len()
106122
);
107123

0 commit comments

Comments
 (0)