-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathlib.rs
451 lines (402 loc) · 14.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#![cfg(feature = "enable")]
#![feature(iter_advance_by)]
use once_cell::sync::Lazy;
use raiko_lib::{
input::{
AggregationGuestInput, AggregationGuestOutput, GuestInput, GuestOutput,
ZkAggregationGuestInput,
},
prover::{IdStore, IdWrite, Proof, ProofKey, Prover, ProverConfig, ProverError, ProverResult},
Measurement,
};
use reth_primitives::B256;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use sp1_sdk::{
action,
network::client::NetworkClient,
proto::network::{ProofMode, UnclaimReason},
SP1Proof,
};
use sp1_sdk::{HashableKey, ProverClient, SP1Stdin, SP1VerifyingKey};
use std::env;
use std::fs;
use std::path::PathBuf;
use tracing::{info, warn};
pub const ELF: &[u8] = include_bytes!("../../guest/elf/sp1-guest");
pub const AGGREGATION_ELF: &[u8] = include_bytes!("../../guest/elf/sp1-aggregation");
pub const FIXTURE_PATH: &str = "./provers/sp1/contracts/src/fixtures/";
pub const CONTRACT_PATH: &str = "./provers/sp1/contracts/src/exports/";
const SP1_PROVER_CODE: u8 = 1;
pub static VERIFIER: Lazy<Result<PathBuf, ProverError>> = Lazy::new(init_verifier);
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Sp1Param {
pub recursion: RecursionMode,
pub prover: Option<ProverMode>,
pub verify: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RecursionMode {
/// The proof mode for an SP1 core proof.
Core,
/// The proof mode for a compressed proof.
Compressed,
/// The proof mode for a PlonK proof.
Plonk,
}
impl From<RecursionMode> for ProofMode {
fn from(value: RecursionMode) -> Self {
match value {
RecursionMode::Core => ProofMode::Core,
RecursionMode::Compressed => ProofMode::Compressed,
RecursionMode::Plonk => ProofMode::Plonk,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProverMode {
Mock,
Local,
Network,
}
impl From<Sp1Response> for Proof {
fn from(value: Sp1Response) -> Self {
Self {
proof: Some(value.proof),
..Default::default()
}
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Sp1Response {
pub proof: String,
}
pub struct Sp1Prover;
impl Prover for Sp1Prover {
async fn run(
input: GuestInput,
output: &GuestOutput,
config: &ProverConfig,
id_store: Option<&mut dyn IdWrite>,
) -> ProverResult<Proof> {
let param = Sp1Param::deserialize(config.get("sp1").unwrap()).unwrap();
let mode = param.prover.clone().unwrap_or_else(get_env_mock);
println!("param: {:?}", param);
let mut stdin = SP1Stdin::new();
stdin.write(&input);
// Generate the proof for the given program.
let client = param
.prover
.map(|mode| match mode {
ProverMode::Mock => ProverClient::mock(),
ProverMode::Local => ProverClient::local(),
ProverMode::Network => ProverClient::network(),
})
.unwrap_or_else(ProverClient::new);
let (pk, vk) = client.setup(ELF);
let prove_action = action::Prove::new(client.prover.as_ref(), &pk, stdin.clone());
let prove_result = if !matches!(mode, ProverMode::Network) {
tracing::debug!("Proving locally with recursion mode: {:?}", param.recursion);
match param.recursion {
RecursionMode::Core => prove_action.run(),
RecursionMode::Compressed => prove_action.compressed().run(),
RecursionMode::Plonk => prove_action.plonk().run(),
}
.map_err(|e| ProverError::GuestError(format!("Sp1: local proving failed: {}", e)))
.unwrap()
} else {
let network_prover = sp1_sdk::NetworkProver::new();
let proof_id = network_prover
.request_proof(ELF, stdin, param.recursion.clone().into())
.await
.map_err(|e| {
ProverError::GuestError(format!("Sp1: requesting proof failed: {e}"))
})?;
if let Some(id_store) = id_store {
id_store
.store_id(
(input.chain_spec.chain_id, output.hash, SP1_PROVER_CODE),
proof_id.clone(),
)
.await?;
}
info!(
"Sp1 Prover: block {:?} - proof id {:?}",
output.header.number, proof_id
);
network_prover
.wait_proof::<sp1_sdk::SP1ProofWithPublicValues>(&proof_id)
.await
.map_err(|e| ProverError::GuestError(format!("Sp1: network proof failed {:?}", e)))
.unwrap()
};
let proof = Proof {
proof: serde_json::to_string(&prove_result).ok(),
..Default::default()
};
if param.verify {
if matches!(param.recursion, RecursionMode::Plonk) {
let time = Measurement::start("verify", false);
verify_sol(vk, prove_result)?;
time.stop_with("==> Verification complete");
} else {
warn!("Cannot verify a non PLONK proof");
}
}
Ok::<_, ProverError>(proof)
}
async fn aggregate(
input: AggregationGuestInput,
output: &AggregationGuestOutput,
config: &ProverConfig,
id_store: Option<&mut dyn IdWrite>,
) -> ProverResult<Proof> {
let param = Sp1Param::deserialize(config.get("sp1").unwrap()).unwrap();
let mode = param.prover.clone().unwrap_or_else(get_env_mock);
// Extract the block proofs
let proofs: Vec<sp1_sdk::SP1ProofWithPublicValues> = input
.proofs
.iter()
.map(|input| {
serde_json::from_str::<sp1_sdk::SP1ProofWithPublicValues>(
&input.proof.clone().unwrap(),
)
.unwrap()
})
.collect::<Vec<_>>();
// Generate the proof for the given program.
let client = param
.prover
.map(|mode| match mode {
ProverMode::Mock => ProverClient::mock(),
ProverMode::Local => ProverClient::local(),
ProverMode::Network => ProverClient::network(),
})
.unwrap_or_else(ProverClient::new);
let (_guest_pk, guest_vk) = client.setup(ELF);
// Write the public values for each block proof
let block_inputs = proofs
.iter()
.map(|proof| B256::from_slice(&proof.public_values.to_vec()))
.collect::<Vec<_>>();
let input = ZkAggregationGuestInput {
image_id: guest_vk.hash_u32(),
block_inputs,
};
let mut stdin = SP1Stdin::new();
stdin.write(&input);
// Write the proofs.
//
// Note: this data will not actually be read by the aggregation program, instead it will be
// witnessed by the prover during the recursive aggregation process inside SP1 itself.
for proof in proofs {
let SP1Proof::Compressed(proof) = proof.proof else {
panic!()
};
stdin.write_proof(proof, guest_vk.vk.clone());
}
let (pk, vk) = client.setup(AGGREGATION_ELF);
let prove_action = action::Prove::new(client.prover.as_ref(), &pk, stdin.clone());
let prove_result = if !matches!(mode, ProverMode::Network) {
tracing::debug!("Proving locally with recursion mode: {:?}", param.recursion);
match param.recursion {
RecursionMode::Core => prove_action.run(),
RecursionMode::Compressed => prove_action.compressed().run(),
RecursionMode::Plonk => prove_action.plonk().run(),
}
.map_err(|e| ProverError::GuestError(format!("Sp1: local proving failed: {}", e)))
.unwrap()
} else {
let network_prover = sp1_sdk::NetworkProver::new();
let proof_id = network_prover
.request_proof(AGGREGATION_ELF, stdin, param.recursion.clone().into())
.await
.map_err(|_| ProverError::GuestError("Sp1: requesting proof failed".to_owned()))?;
if let Some(id_store) = id_store {
id_store
.store_id((123456, output.hash, SP1_PROVER_CODE), proof_id.clone())
.await?;
}
info!("Sp1 Prover: aggregation proof id {:?}", proof_id);
network_prover
.wait_proof::<sp1_sdk::SP1ProofWithPublicValues>(&proof_id)
.await
.map_err(|e| ProverError::GuestError(format!("Sp1: network proof failed {:?}", e)))
.unwrap()
};
let proof = Proof {
proof: serde_json::to_string(&prove_result).ok(),
..Default::default()
};
if param.verify {
if matches!(param.recursion, RecursionMode::Plonk) {
let time = Measurement::start("verify", false);
verify_sol(vk, prove_result)?;
time.stop_with("==> Verification complete");
} else {
warn!("Cannot verify a non PLONK proof");
}
}
Ok::<_, ProverError>(proof)
}
async fn cancel(key: ProofKey, id_store: Box<&mut dyn IdStore>) -> ProverResult<()> {
let proof_id = match id_store.read_id(key).await {
Ok(proof_id) => proof_id,
Err(e) => {
if e.to_string().contains("No data for query") {
return Ok(());
} else {
return Err(ProverError::GuestError(e.to_string()));
}
}
};
let private_key = env::var("SP1_PRIVATE_KEY").map_err(|_| {
ProverError::GuestError("SP1_PRIVATE_KEY must be set for remote proving".to_owned())
})?;
let network_client = NetworkClient::new(&private_key);
network_client
.unclaim_proof(proof_id, UnclaimReason::Abandoned, "".to_owned())
.await
.map_err(|_| ProverError::GuestError("Sp1: couldn't unclaim proof".to_owned()))?;
id_store.remove_id(key).await?;
Ok(())
}
}
fn get_env_mock() -> ProverMode {
match env::var("SP1_PROVER")
.unwrap_or("local".to_string())
.to_lowercase()
.as_str()
{
"mock" => ProverMode::Mock,
"local" => ProverMode::Local,
"network" => ProverMode::Network,
_ => ProverMode::Local,
}
}
fn init_verifier() -> Result<PathBuf, ProverError> {
// In cargo run, Cargo sets the working directory to the root of the workspace
let output_dir: PathBuf = CONTRACT_PATH.into();
let artifacts_dir = sp1_sdk::install::try_install_plonk_bn254_artifacts();
if !artifacts_dir.join("SP1Verifier.sol").exists() {
return Err(ProverError::GuestError(format!(
"verifier file not found at {:?}",
artifacts_dir
)));
}
std::fs::create_dir_all(&output_dir).map_err(ProverError::FileIo)?;
copy_dir_all(&artifacts_dir, &output_dir).map_err(ProverError::FileIo)?;
println!(
"exported verifier from {} to {}",
artifacts_dir.display(),
output_dir.display()
);
Ok(output_dir)
}
fn copy_dir_all(
src: impl AsRef<std::path::Path>,
dst: impl AsRef<std::path::Path>,
) -> std::io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry.unwrap();
if entry.file_type()?.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
println!(
"copying {:?} to {:?}",
entry.path(),
dst.as_ref().join(entry.file_name())
);
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
/// A fixture that can be used to test the verification of SP1 zkVM proofs inside Solidity.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RaikoProofFixture {
vkey: String,
public_values: String,
proof: String,
}
pub fn verify_sol(
vk: SP1VerifyingKey,
mut proof: sp1_sdk::SP1ProofWithPublicValues,
) -> ProverResult<()> {
assert!(VERIFIER.is_ok());
// Deserialize the public values.
let pi_hash = proof.public_values.read::<[u8; 32]>();
// Create the testing fixture so we can test things end-to-end.
let fixture = RaikoProofFixture {
vkey: vk.bytes32().to_string(),
public_values: B256::from_slice(&pi_hash).to_string(),
proof: format!("0x{}", reth_primitives::hex::encode(proof.bytes())),
};
println!("===> Fixture: {:#?}", fixture);
// Save the fixture to a file.
println!("Writing fixture to: {:?}", FIXTURE_PATH);
let fixture_path = PathBuf::from(FIXTURE_PATH);
if !fixture_path.exists() {
std::fs::create_dir_all(&fixture_path).map_err(|e| {
ProverError::GuestError(format!("Failed to create fixture path: {}", e))
})?;
}
std::fs::write(
fixture_path.join("fixture.json"),
serde_json::to_string_pretty(&fixture).unwrap(),
)
.map_err(|e| ProverError::GuestError(format!("Failed to write fixture: {}", e)))?;
let child = std::process::Command::new("forge")
.arg("test")
.current_dir(CONTRACT_PATH)
.stdout(std::process::Stdio::inherit()) // Inherit the parent process' stdout
.spawn();
println!("Verification started {:?}", child);
child.map_err(|e| ProverError::GuestError(format!("Failed to run forge: {}", e)))?;
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use serde_json::json;
const TEST_ELF: &[u8] = include_bytes!("../../guest/elf/test-sp1-guest");
#[test]
fn test_deserialize_sp1_param() {
let json = json!(
{
"recursion": "core",
"prover": "network",
"verify": true
}
);
let param = Sp1Param {
recursion: RecursionMode::Core,
prover: Some(ProverMode::Network),
verify: true,
};
let serialized = serde_json::to_value(¶m).unwrap();
assert_eq!(json, serialized);
let deserialized: Sp1Param = serde_json::from_value(serialized).unwrap();
println!("{:?} {:?}", json, deserialized);
}
#[test]
fn test_init_verifier() {
assert!(VERIFIER.is_ok());
}
#[test]
fn run_unittest_elf() {
// TODO(Cecilia): imple GuestInput::mock() for unit test
let client = ProverClient::new();
let stdin = SP1Stdin::new();
let (pk, vk) = client.setup(TEST_ELF);
let proof = client.prove(&pk, stdin).run().unwrap();
client
.verify(&proof, &vk)
.expect("Sp1: verification failed");
}
}