1- use sp1_sdk:: { Prover , ProverClient , SP1ProofWithPublicValues , SP1Stdin , SP1VerifyingKey } ;
1+ use alloy:: primitives:: Keccak256 ;
2+ use sp1_aggregator:: { ProofInput , SP1ProofInput } ;
3+ use sp1_sdk:: {
4+ HashableKey , Prover , ProverClient , SP1ProofWithPublicValues , SP1Stdin , SP1VerifyingKey ,
5+ } ;
26
37use crate :: zk:: aggregator:: { AggregatedProof , ProgramOutput , ProofAggregationError } ;
48
@@ -7,28 +11,55 @@ const PROGRAM_ELF: &[u8] = include_bytes!("../../../zkvm/sp1/elf/sp1_aggregator_
711// TODO lock prover
812
913pub struct SP1Proof {
10- pub elf : Vec < u8 > ,
14+ pub vk : SP1VerifyingKey ,
1115 pub proof : SP1ProofWithPublicValues ,
1216}
1317
1418impl SP1Proof {
15- pub fn verifying_key ( & self ) -> SP1VerifyingKey {
16- let client = ProverClient :: from_env ( ) ;
17- let ( _pk, vk) = client. setup ( & self . elf ) ;
18- vk
19+ pub fn hash ( & self ) -> [ u8 ; 32 ] {
20+ let mut hasher = Keccak256 :: new ( ) ;
21+ for & word in & self . vk . hash_u32 ( ) {
22+ hasher. update ( word. to_le_bytes ( ) ) ;
23+ }
24+ hasher. update ( self . proof . public_values . as_slice ( ) ) ;
25+ hasher. finalize ( ) . into ( )
1926 }
2027}
2128
22- pub struct SP1AggregatedProof {
23- pub proof : SP1ProofWithPublicValues ,
24- pub vk : SP1VerifyingKey ,
29+ pub struct SP1AggregationInput {
30+ proofs : Vec < SP1Proof > ,
31+ merkle_root : [ u8 ; 32 ] ,
2532}
2633
2734pub ( crate ) fn aggregate_proofs (
28- input : sp1_aggregator :: Input ,
35+ input : SP1AggregationInput ,
2936) -> Result < ProgramOutput , ProofAggregationError > {
3037 let mut stdin = SP1Stdin :: new ( ) ;
31- stdin. write ( & input) ;
38+
39+ let mut program_input = sp1_aggregator:: Input {
40+ proofs : vec ! [ ] ,
41+ merkle_root : input. merkle_root ,
42+ } ;
43+
44+ // write vk + public inputs
45+ for proof in input. proofs . iter ( ) {
46+ program_input
47+ . proofs
48+ . push ( ProofInput :: SP1Compressed ( SP1ProofInput {
49+ public_inputs : proof. proof . public_values . to_vec ( ) ,
50+ vk : proof. vk . hash_u32 ( ) ,
51+ } ) ) ;
52+ }
53+ stdin. write ( & program_input) ;
54+
55+ // write proofs
56+ for SP1Proof { proof, vk } in input. proofs {
57+ // we only support sp1 Compressed proofs for now
58+ let sp1_sdk:: SP1Proof :: Compressed ( proof) = proof. proof else {
59+ return Err ( ProofAggregationError :: UnsupportedProof ) ;
60+ } ;
61+ stdin. write_proof ( * proof, vk. vk ) ;
62+ }
3263
3364 #[ cfg( feature = "prove" ) ]
3465 let client = ProverClient :: from_env ( ) ;
@@ -48,7 +79,7 @@ pub(crate) fn aggregate_proofs(
4879 . verify ( & proof, & vk)
4980 . map_err ( ProofAggregationError :: SP1Verification ) ?;
5081
51- let proof = SP1AggregatedProof { proof, vk } ;
82+ let proof = SP1Proof { proof, vk } ;
5283
5384 let output = ProgramOutput :: new ( AggregatedProof :: SP1 ( proof) ) ;
5485
@@ -59,10 +90,10 @@ pub enum SP1VerificationError {
5990 Verification ( sp1_sdk:: SP1VerificationError ) ,
6091}
6192
62- pub ( crate ) fn verify ( proof : & SP1Proof ) -> Result < ( ) , SP1VerificationError > {
93+ pub ( crate ) fn verify ( proof : & SP1Proof , elf : & [ u8 ] ) -> Result < ( ) , SP1VerificationError > {
6394 let client = ProverClient :: from_env ( ) ;
6495
65- let ( _pk, vk) = client. setup ( & proof . elf ) ;
96+ let ( _pk, vk) = client. setup ( elf) ;
6697 client
6798 . verify ( & proof. proof , & vk)
6899 . map_err ( SP1VerificationError :: Verification )
0 commit comments