Skip to content

Commit 3cb6651

Browse files
fix(raiko): fix r0 aggregation proof format (#386)
* fix r0 aggregation proof format * Update provers/risc0/driver/src/lib.rs Co-authored-by: Petar Vujović <[email protected]> * fix review comments * refine info print * refine info print * add timeout to sp1 wait_proof * refine info print --------- Co-authored-by: Petar Vujović <[email protected]>
1 parent ec483b7 commit 3cb6651

File tree

8 files changed

+85
-45
lines changed

8 files changed

+85
-45
lines changed

host/src/proof.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ impl ProofActor {
211211
}
212212
result = Self::handle_aggregate(request_clone, &opts) => {
213213
match result {
214-
Ok(()) => {
215-
info!("Host handling message");
214+
Ok(status) => {
215+
info!("Host handling message: {status:?}");
216216
}
217217
Err(error) => {
218218
error!("Worker failed due to: {error:?}");
@@ -231,20 +231,20 @@ impl ProofActor {
231231
while let Some(message) = self.receiver.recv().await {
232232
match message {
233233
Message::Cancel(key) => {
234-
debug!("Message::Cancel task: {key:?}");
234+
debug!("Message::Cancel({key:?})");
235235
if let Err(error) = self.cancel_task(key).await {
236236
error!("Failed to cancel task: {error}")
237237
}
238238
}
239239
Message::Task(proof_request) => {
240-
debug!("Message::Task proof_request: {proof_request:?}");
240+
debug!("Message::Task({proof_request:?})");
241241
let running_task_count = self.running_tasks.lock().await.len();
242242
if running_task_count < self.opts.concurrency_limit {
243243
info!("Running task {proof_request:?}");
244244
self.run_task(proof_request).await;
245245
} else {
246246
info!(
247-
"Task concurrency limit reached, current running {running_task_count:?}, pending: {:?}",
247+
"Task concurrency status: running:{running_task_count:?}, add {proof_request:?} to pending list[{:?}]",
248248
self.pending_tasks.lock().await.len()
249249
);
250250
let mut pending_tasks = self.pending_tasks.lock().await;
@@ -253,9 +253,9 @@ impl ProofActor {
253253
}
254254
Message::TaskComplete(req) => {
255255
// pop up pending task if any task complete
256-
debug!("Message::TaskComplete: {req:?}");
256+
debug!("Message::TaskComplete({req:?})");
257257
info!(
258-
"task completed, current running {:?}, pending: {:?}",
258+
"task {req:?} completed, current running {:?}, pending: {:?}",
259259
self.running_tasks.lock().await.len(),
260260
self.pending_tasks.lock().await.len()
261261
);
@@ -269,11 +269,13 @@ impl ProofActor {
269269
}
270270
}
271271
Message::CancelAggregate(request) => {
272+
debug!("Message::CancelAggregate({request:?})");
272273
if let Err(error) = self.cancel_aggregation_task(request).await {
273274
error!("Failed to cancel task: {error}")
274275
}
275276
}
276277
Message::Aggregate(request) => {
278+
debug!("Message::Aggregate({request:?})");
277279
let permit = Arc::clone(&semaphore)
278280
.acquire_owned()
279281
.await

host/src/server/api/v3/proof/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use axum::{debug_handler, extract::State, routing::post, Json, Router};
2-
use raiko_core::{
3-
interfaces::{AggregationOnlyRequest, AggregationRequest, ProofRequest, ProofRequestOpt},
4-
provider::get_task_data,
5-
};
6-
use raiko_tasks::{TaskDescriptor, TaskManager, TaskStatus};
7-
use utoipa::OpenApi;
8-
91
use crate::{
102
interfaces::HostResult,
113
metrics::{inc_current_req, inc_guest_req_count, inc_host_req_count},
124
server::api::{v2, v3::Status},
135
Message, ProverState,
146
};
7+
use axum::{debug_handler, extract::State, routing::post, Json, Router};
8+
use raiko_core::{
9+
interfaces::{AggregationOnlyRequest, AggregationRequest, ProofRequest, ProofRequestOpt},
10+
provider::get_task_data,
11+
};
12+
use raiko_lib::prover::Proof;
13+
use raiko_tasks::{TaskDescriptor, TaskManager, TaskStatus};
1514
use tracing::{debug, info};
15+
use utoipa::OpenApi;
1616

1717
mod aggregate;
1818
mod cancel;
@@ -125,8 +125,11 @@ async fn proof_handler(
125125
let mut proofs = Vec::with_capacity(tasks.len());
126126
for (task, req) in tasks {
127127
let raw_proof = manager.get_task_proof(&task).await?;
128-
let proof = serde_json::from_slice(&raw_proof)?;
129-
debug!("req: {req:?} gets proof: {proof:?}");
128+
let proof: Proof = serde_json::from_slice(&raw_proof)?;
129+
debug!(
130+
"Aggregation sub-req: {req:?} gets proof {:?} with input {:?}.",
131+
proof.proof, proof.input
132+
);
130133
proofs.push(proof);
131134
}
132135

provers/risc0/driver/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,18 @@ impl Prover for Risc0Prover {
180180
"Generate aggregatino receipt journal: {:?}",
181181
receipt.journal
182182
);
183+
let block_proof_image_id = compute_image_id(RISC0_GUEST_ELF).unwrap();
183184
let aggregation_image_id = compute_image_id(RISC0_AGGREGATION_ELF).unwrap();
184-
let enc_proof =
185-
snarks::verify_groth16_snark_from_receipt(aggregation_image_id, receipt.clone())
186-
.await
187-
.map_err(|err| format!("Failed to verify SNARK: {err:?}"))?;
188-
let snark_proof = format!("0x{}", hex::encode(enc_proof));
185+
let proof_data = snarks::verify_aggregation_groth16_proof(
186+
block_proof_image_id,
187+
aggregation_image_id,
188+
receipt.clone(),
189+
)
190+
.await
191+
.map_err(|err| format!("Failed to verify SNARK: {err:?}"))?;
192+
let snark_proof = alloy_primitives::hex::encode_prefixed(proof_data);
189193

194+
info!("Aggregation proof: {snark_proof:?}");
190195
let proof_gen_result = Ok(Risc0Response {
191196
proof: snark_proof,
192197
receipt: serde_json::to_string(&receipt).unwrap(),
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub const RISC0_AGGREGATION_ELF: &[u8] =
22
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-aggregation");
33
pub const RISC0_AGGREGATION_ID: [u32; 8] = [
4-
440526723, 3767976668, 67051936, 881100330, 2605787818, 1152192925, 943988177, 1141581874,
4+
3593026424, 359928015, 3488866833, 2676323972, 1129344711, 55769507, 233041442, 3293280986,
55
];
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub const RISC0_GUEST_ELF: &[u8] =
22
include_bytes!("../../../guest/target/riscv32im-risc0-zkvm-elf/release/risc0-guest");
33
pub const RISC0_GUEST_ID: [u32; 8] = [
4-
2426111784, 2252773481, 4093155148, 2853313326, 836865213, 1159934005, 790932950, 229907112,
4+
2522428380, 1790994278, 397707036, 244564411, 3780865207, 1282154214, 1673205005, 3172292887,
55
];

provers/risc0/driver/src/snarks.rs

+43-16
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,53 @@ pub async fn verify_groth16_from_snark_receipt(
157157
let seal = encode(snark_receipt.snark.to_vec())?;
158158
let journal_digest = snark_receipt.journal.digest();
159159
let post_state_digest = snark_receipt.post_state_digest.digest();
160-
verify_groth16_snark_impl(image_id, seal, journal_digest, post_state_digest).await
160+
let encoded_proof =
161+
verify_groth16_snark_impl(image_id, seal, journal_digest, post_state_digest).await?;
162+
let proof = (encoded_proof, B256::from_slice(image_id.as_bytes()))
163+
.abi_encode()
164+
.iter()
165+
.skip(32)
166+
.copied()
167+
.collect();
168+
Ok(proof)
161169
}
162170

163-
pub async fn verify_groth16_snark_from_receipt(
164-
image_id: Digest,
171+
pub async fn verify_aggregation_groth16_proof(
172+
block_proof_image_id: Digest,
173+
aggregation_image_id: Digest,
165174
receipt: Receipt,
166175
) -> Result<Vec<u8>> {
167-
let seal = receipt.inner.groth16().unwrap().seal.clone();
176+
let seal = receipt
177+
.inner
178+
.groth16()
179+
.map_err(|e| anyhow::Error::msg(format!("receipt.inner.groth16() failed: {e:?}")))?
180+
.seal
181+
.clone();
168182
let journal_digest = receipt.journal.digest();
169-
let post_state_digest = receipt.claim()?.as_value().unwrap().post.digest();
170-
verify_groth16_snark_impl(image_id, seal, journal_digest, post_state_digest).await
183+
let post_state_digest = receipt
184+
.claim()?
185+
.as_value()
186+
.map_err(|e| anyhow::Error::msg(format!("receipt.claim()?.as_value() failed: {e:?}")))?
187+
.post
188+
.digest();
189+
let encoded_proof = verify_groth16_snark_impl(
190+
aggregation_image_id,
191+
seal,
192+
journal_digest,
193+
post_state_digest,
194+
)
195+
.await?;
196+
let proof = (
197+
encoded_proof,
198+
B256::from_slice(block_proof_image_id.as_bytes()),
199+
B256::from_slice(aggregation_image_id.as_bytes()),
200+
)
201+
.abi_encode()
202+
.iter()
203+
.skip(32)
204+
.copied()
205+
.collect();
206+
Ok(proof)
171207
}
172208

173209
pub async fn verify_groth16_snark_impl(
@@ -209,14 +245,5 @@ pub async fn verify_groth16_snark_impl(
209245
tracing_err!("SNARK verification failed: {verify_call_res:?}!");
210246
}
211247

212-
Ok(make_risc0_groth16_proof(enc_seal, image_id))
213-
}
214-
215-
pub fn make_risc0_groth16_proof(seal: Vec<u8>, image_id: Digest) -> Vec<u8> {
216-
(seal, B256::from_slice(image_id.as_bytes()))
217-
.abi_encode()
218-
.iter()
219-
.skip(32)
220-
.copied()
221-
.collect()
248+
Ok(enc_seal)
222249
}

provers/sp1/driver/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::{
2424
borrow::BorrowMut,
2525
env, fs,
2626
path::{Path, PathBuf},
27+
time::Duration,
2728
};
2829
use tracing::{debug, error, info};
2930

@@ -170,7 +171,10 @@ impl Prover for Sp1Prover {
170171
output.header.number
171172
);
172173
network_prover
173-
.wait_proof::<sp1_sdk::SP1ProofWithPublicValues>(&proof_id, None)
174+
.wait_proof::<sp1_sdk::SP1ProofWithPublicValues>(
175+
&proof_id,
176+
Some(Duration::from_secs(3600)),
177+
)
174178
.await
175179
.map_err(|e| ProverError::GuestError(format!("Sp1: network proof failed {e:?}")))?
176180
};
@@ -253,9 +257,6 @@ impl Prover for Sp1Prover {
253257
_store: Option<&mut dyn IdWrite>,
254258
) -> ProverResult<Proof> {
255259
let param = Sp1Param::deserialize(config.get("sp1").unwrap()).unwrap();
256-
257-
info!("aggregate proof with param: {param:?}");
258-
259260
let block_inputs: Vec<B256> = input
260261
.proofs
261262
.iter()
@@ -312,6 +313,7 @@ impl Prover for Sp1Prover {
312313
let prove_result = client
313314
.prove(&pk, stdin)
314315
.plonk()
316+
.timeout(Duration::from_secs(3600))
315317
.run()
316318
.expect("proving failed");
317319

script/prove-block.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ for block in $(eval echo {$rangeStart..$rangeEnd}); do
144144
fi
145145

146146
echo "- proving block $block"
147-
curl --location --request POST 'http://localhost:8080/v3/proof' \
147+
curl --location --request POST 'http://localhost:8080/v2/proof' \
148148
--header 'Content-Type: application/json' \
149149
--header 'Authorization: Bearer 4cbd753fbcbc2639de804f8ce425016a50e0ecd53db00cb5397912e83f5e570e' \
150150
--data-raw "{
151151
\"network\": \"$chain\",
152152
\"l1_network\": \"$l1_network\",
153153
\"block_numbers\": [[$block, null], [$(($block+1)), null]],
154+
\"block_number\": $block,
154155
\"prover\": \"$prover\",
155156
\"graffiti\": \"$graffiti\",
156157
$proofParam

0 commit comments

Comments
 (0)