Skip to content

Commit 289ab82

Browse files
committed
fix evm proof generation
1 parent 4b0f52c commit 289ab82

7 files changed

Lines changed: 80 additions & 71 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pgo-data.profdata
1414
# Proofs
1515
**/proof-with-pis.json
1616
**/proof-with-io.json
17+
contracts
1718

1819
# Env
1920
.env

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ pub fn track_energy(m3ter: M3ter, m3ter_payloads: &[M3terPayload], start_nonce:
7979
let mut energy_sum = 0.0;
8080
let mut latest_nonce = start_nonce;
8181
for payload in m3ter_payloads.iter() {
82-
if latest_nonce + 1 != payload.nonce || payload.nonce < latest_nonce {
82+
if latest_nonce + 1 != payload.nonce {
8383
println!("Invalid nonce: {} < {} for m3ter_id {}", &payload.nonce, &latest_nonce, &m3ter.m3ter_id);
84-
return (0.0, start_nonce);
84+
break; // Nonce is not sequential or is less than the latest nonce
8585
}
8686
// if !m3ter.validate_payload(payload) {
8787
// println!("Invalid payload: {:?}", payload);
88-
// return (energy_sum, latest_nonce);
88+
// break
8989
// }
9090
energy_sum += payload.energy;
9191
latest_nonce = payload.nonce;

lib/src/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ pub fn validate_signature(message_hash: Vec<u8>, public_key: &str, signature_str
1515
}
1616
}
1717

18-
pub fn _validate_unique_nonce(nonce: u128) -> bool {
19-
// Check if the nonce is unique (greater than 0)
20-
nonce > 0
21-
}
18+
// pub fn _validate_unique_nonce(nonce: u128) -> bool {
19+
// // Check if the nonce is unique (greater than 0)
20+
// nonce > 0
21+
// }
2222

2323
fn build_signature(raw_signature: &str) -> Option<Signature> {
2424
let signature = Signature::from_slice(&decode_hex(raw_signature));

script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde = { workspace = true }
2727
energy-tracker-lib = { path = "../lib" }
2828
dotenv = "0.15.0"
2929
base64 = "0.22.1"
30+
bincode = "1.3.3"
3031

3132
[build-dependencies]
3233
sp1-build = "5.0.0"

script/src/bin/evm.rs

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
//! RUST_LOG=info cargo run --release --bin evm -- --system plonk
1111
//! ```
1212
13+
use std::{fs::File, io::BufReader};
14+
1315
use alloy_sol_types::SolType;
1416
use clap::{Parser, ValueEnum};
17+
use energy_tracker_lib::{Payload, PublicValuesStruct};
1518
use serde::{Deserialize, Serialize};
1619
use 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+
}

script/src/bin/main.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{fs::File, io::BufReader};
1414

1515
use alloy_sol_types::SolType;
1616
use clap::Parser;
17-
use energy_tracker_lib::Payload;
17+
use energy_tracker_lib::{Payload, PublicValuesStruct};
1818
use sp1_sdk::{include_elf, ProverClient, SP1Stdin};
1919

2020
use base64::{Engine as _, alphabet, engine::{self, general_purpose}};
@@ -58,33 +58,21 @@ fn main() {
5858
// let previous_nonces = &payload.previous_nonces;
5959
// let previous_balances = &payload.previous_balances;
6060

61-
// let decoded_nonces = engine::general_purpose::URL_SAFE_NO_PAD.decode(previous_nonces).unwrap();
62-
// let values = m3ter_payload.previous_nonces.len();
63-
// loop {
64-
// if after_strip.len() / 64 >= 1 {
65-
// let split = data_to_bytes.split_off(32);
66-
// values.push(hex::encode(data_to_bytes.clone()));
67-
// data_to_bytes = split;
68-
// } else { break; }
69-
// }
70-
71-
// assert_eq!(hex_input, &hex::encode(decoded_nonces));
72-
7361
// Setup the inputs.
7462
let mut stdin = SP1Stdin::new();
7563
stdin.write(&payload);
7664

7765
if args.execute {
7866
// Execute the program
79-
let (output, report) = client.execute(ENERGY_TRACKER_ELF, &stdin).run().unwrap();
67+
let (mut output, report) = client.execute(ENERGY_TRACKER_ELF, &stdin).run().unwrap();
8068
println!("Program executed successfully.");
8169

8270
// Read the output.
8371
// let decoded = PublicValuesStruct::abi_decode(output.as_slice()).unwrap();
8472
// let PublicValuesStruct { n, a, b } = decoded;
8573
// println!("n: {}", n);
8674
// println!("a: {}", a);
87-
println!("output: {}", output.raw());
75+
println!("output: {:?}", bincode::deserialize::<PublicValuesStruct>(output.as_slice()).unwrap());
8876

8977
// let (expected_a, expected_b) = fibonacci_lib::fibonacci(n);
9078
// assert_eq!(a, expected_a);

0 commit comments

Comments
 (0)