1010//! RUST_LOG=info cargo run --release --bin evm -- --system plonk
1111//! ```
1212
13+ use std:: { fs:: File , io:: BufReader } ;
14+
1315use alloy_sol_types:: SolType ;
1416use clap:: { Parser , ValueEnum } ;
17+ use energy_tracker_lib:: { Payload , PublicValuesStruct } ;
1518use serde:: { Deserialize , Serialize } ;
1619use sp1_sdk:: {
1720 include_elf, HashableKey , ProverClient , SP1ProofWithPublicValues , SP1Stdin , SP1VerifyingKey ,
@@ -41,10 +44,11 @@ enum ProofSystem {
4144/// A fixture that can be used to test the verification of SP1 zkVM proofs inside Solidity.
4245#[ derive( Debug , Clone , Serialize , Deserialize ) ]
4346#[ serde( rename_all = "camelCase" ) ]
44- struct SP1FibonacciProofFixture {
45- a : u32 ,
46- b : u32 ,
47- n : u32 ,
47+ struct ProofFixture {
48+ previous_balances : Vec < f32 > ,
49+ previous_nonces : Vec < i32 > ,
50+ new_balances : Vec < f32 > ,
51+ new_nonces : Vec < i32 > ,
4852 vkey : String ,
4953 public_values : String ,
5054 proof : String ,
@@ -57,15 +61,24 @@ fn main() {
5761 // Parse the command line arguments.
5862 let args = EVMArgs :: parse ( ) ;
5963
64+ std:: env:: set_var ( "SP1_PROVER" , "network" ) ;
65+ std:: env:: set_var ( "NETWORK_PRIVATE_KEY" , "3b62b0fb8da4fc79eff9236c50527cd8bb9cd7c264f1c838b105d4570aa0491e" ) ;
66+
6067 // Setup the prover client.
6168 let client = ProverClient :: from_env ( ) ;
6269
6370 // Setup the program.
6471 let ( pk, vk) = client. setup ( ENERGY_TRACKER_ELF ) ;
6572
6673 // Setup the inputs.
74+ let file = File :: open ( "script/src/sample.json" ) . unwrap ( ) ;
75+ let reader = BufReader :: new ( file) ;
76+ let payload: Payload = serde_json:: from_reader ( reader) . unwrap ( ) ;
77+ // let previous_nonces = &payload.previous_nonces;
78+ // let previous_balances = &payload.previous_balances;
79+
6780 let mut stdin = SP1Stdin :: new ( ) ;
68- stdin. write ( & args . n ) ;
81+ stdin. write ( & payload ) ;
6982
7083 println ! ( "Proof System: {:?}" , args. system) ;
7184
@@ -76,51 +89,56 @@ fn main() {
7689 }
7790 . expect ( "failed to generate proof" ) ;
7891
79- // create_proof_fixture(&proof, &vk, args.system);
92+ create_proof_fixture ( & proof, & vk, args. system ) ;
8093}
8194
82- //// Create a fixture for the given proof.
83- // fn create_proof_fixture(
84- // proof: &SP1ProofWithPublicValues,
85- // vk: &SP1VerifyingKey,
86- // system: ProofSystem,
87- // ) {
88- // // Deserialize the public values.
89- // let bytes = proof.public_values.as_slice();
90- // let PublicValuesStruct { n, a, b } = PublicValuesStruct::abi_decode(bytes).unwrap();
91-
92- // // Create the testing fixture so we can test things end-to-end.
93- // let fixture = SP1FibonacciProofFixture {
94- // a,
95- // b,
96- // n,
97- // vkey: vk.bytes32().to_string(),
98- // public_values: format!("0x{}", hex::encode(bytes)),
99- // proof: format!("0x{}", hex::encode(proof.bytes())),
100- // };
101-
102- // // The verification key is used to verify that the proof corresponds to the execution of the
103- // // program on the given input.
104- // //
105- // // Note that the verification key stays the same regardless of the input.
106- // println!("Verification Key: {}", fixture.vkey);
107-
108- // // The public values are the values which are publicly committed to by the zkVM.
109- // //
110- // // If you need to expose the inputs or outputs of your program, you should commit them in
111- // // the public values.
112- // println!("Public Values: {}", fixture.public_values);
113-
114- // // The proof proves to the verifier that the program was executed with some inputs that led to
115- // // the give public values.
116- // println!("Proof Bytes: {}", fixture.proof);
95+ // Create a fixture for the given proof.
96+ fn create_proof_fixture (
97+ proof : & SP1ProofWithPublicValues ,
98+ vk : & SP1VerifyingKey ,
99+ system : ProofSystem ,
100+ ) {
101+ // Deserialize the public values.
102+ let bytes = proof. public_values . as_slice ( ) ;
103+ let PublicValuesStruct {
104+ previous_balances,
105+ previous_nonces,
106+ new_balances,
107+ new_nonces } = bincode:: deserialize ( bytes) . unwrap ( ) ;
108+
109+ // Create the testing fixture so we can test things end-to-end.
110+ let fixture = ProofFixture {
111+ previous_balances,
112+ previous_nonces,
113+ new_balances,
114+ new_nonces,
115+ vkey : vk. bytes32 ( ) . to_string ( ) ,
116+ public_values : format ! ( "0x{}" , hex:: encode( bytes) ) ,
117+ proof : format ! ( "0x{}" , hex:: encode( proof. bytes( ) ) ) ,
118+ } ;
119+
120+ // The verification key is used to verify that the proof corresponds to the execution of the
121+ // program on the given input.
122+ //
123+ // Note that the verification key stays the same regardless of the input.
124+ println ! ( "Verification Key: {}" , fixture. vkey) ;
125+
126+ // The public values are the values which are publicly committed to by the zkVM.
127+ //
128+ // If you need to expose the inputs or outputs of your program, you should commit them in
129+ // the public values.
130+ println ! ( "Public Values: {}" , fixture. public_values) ;
131+
132+ // The proof proves to the verifier that the program was executed with some inputs that led to
133+ // the give public values.
134+ println ! ( "Proof Bytes: {}" , fixture. proof) ;
117135
118136 // Save the fixture to a file.
119- // let fixture_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../contracts/src/fixtures");
120- // std::fs::create_dir_all(&fixture_path).expect("failed to create fixture path");
121- // std::fs::write(
122- // fixture_path.join(format!("{:?}-fixture.json", system).to_lowercase()),
123- // serde_json::to_string_pretty(&fixture).unwrap(),
124- // )
125- // .expect("failed to write fixture");
126- // }
137+ let fixture_path = PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( "../contracts/src/fixtures" ) ;
138+ std:: fs:: create_dir_all ( & fixture_path) . expect ( "failed to create fixture path" ) ;
139+ std:: fs:: write (
140+ fixture_path. join ( format ! ( "{:?}-fixture.json" , system) . to_lowercase ( ) ) ,
141+ serde_json:: to_string_pretty ( & fixture) . unwrap ( ) ,
142+ )
143+ . expect ( "failed to write fixture" ) ;
144+ }
0 commit comments