-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathsp1.rs
More file actions
69 lines (53 loc) · 1.86 KB
/
sp1.rs
File metadata and controls
69 lines (53 loc) · 1.86 KB
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
use sp1_sdk::{Prover, ProverClient, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey};
use crate::zk::interface::aggregator::{
AggregatedProof, AggregatedVerificationError, ProgramOutput,
};
const PROGRAM_ELF: &[u8] = include_bytes!("../../../zkvm/sp1/elf/sp1_aggregator_program");
// TODO lock prover
pub struct SP1Proof {
pub elf: Vec<u8>,
pub proof: Vec<u8>,
}
pub struct SP1AggregatedProof {
pub proof: SP1ProofWithPublicValues,
pub vk: SP1VerifyingKey,
}
pub(crate) fn aggregate_proofs(
input: sp1_aggregator::Input,
) -> Result<ProgramOutput, AggregatedVerificationError> {
let mut stdin = SP1Stdin::new();
stdin.write(&input);
#[cfg(feature = "prove")]
let client = ProverClient::from_env();
// If not in prove mode, create a mock proof via mock client
#[cfg(not(feature = "prove"))]
let client = ProverClient::builder().mock().build();
let (pk, vk) = client.setup(PROGRAM_ELF);
let proof = client
.prove(&pk, &stdin)
.groth16()
.run()
.map_err(|_| AggregatedVerificationError::SP1Proving)?;
// a sanity check, vm already performs it
client
.verify(&proof, &vk)
.map_err(AggregatedVerificationError::SP1Verification)?;
let proof = SP1AggregatedProof { proof, vk };
let output = ProgramOutput::new(AggregatedProof::SP1(proof));
Ok(output)
}
pub enum SP1VerificationError {
Verification(sp1_sdk::SP1VerificationError),
DecodeProofBinary,
}
pub(crate) fn verify(proof: &SP1Proof) -> Result<(), SP1VerificationError> {
let client = ProverClient::from_env();
let (_pk, vk) = client.setup(&proof.elf);
if let Ok(proof) = bincode::deserialize(&proof.proof) {
client
.verify(&proof, &vk)
.map_err(SP1VerificationError::Verification)
} else {
Err(SP1VerificationError::DecodeProofBinary)
}
}