-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add groth16 experiment scripts and results
- Loading branch information
Showing
10 changed files
with
413 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
programs/per-raffle-proof/contracts/src/fixtures/groth16-fixture.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"numParticipants": 10000000, | ||
"numWinners": 1000, | ||
"randomSeed": 12345, | ||
"winnersMerkleRoot": "0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d", | ||
"vkey": "0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4", | ||
"publicValues": "0x000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000003e8000000000000000000000000000000000000000000000000000000000000303962383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d", | ||
"proof": "0x6a2906ac2bdc11c34385d676b280cbaa646d584c2f65f88341b11405e12fb18a750db8aa0e2d0a73536b05d5a67fbac543df23ff862d1e61f8c4bb49acc2aa9ad5b4edee1ed8af0233d985f535aa9518f533d1a9ab06f1e1680fd12e567d495ea546c0fd0a67b50e220ec2c9576666b69084f1853c6ec0d64eda22e76a30a2cd111eb12e06dcc8dd1fc8cd8f533bb35092d0f59591c54e4ef100919b6e53bd824473b56f0f63cc18213c64404a224cf4b98f320e286ce024b9db9dc7dce3648fb04ced733056bcd3e70ed46e9417853fc41ff6e45be5a48d5ecb374e57eba8994b54926103846766c514836ac41b58d4dba5ae591e4ec8a6436c4e7c5ac536a5b80e998f" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//! An end-to-end example of using the SP1 SDK to generate a proof of a program that can have an | ||
//! EVM-Compatible proof generated which can be verified on-chain. | ||
//! | ||
//! You can run this script using the following command: | ||
//! ```shell | ||
//! RUST_LOG=info cargo run --release --bin evm -- --system groth16 | ||
//! ``` | ||
//! or | ||
//! ```shell | ||
//! RUST_LOG=info cargo run --release --bin evm -- --system plonk | ||
//! ``` | ||
|
||
use alloy_sol_types::sol; | ||
use alloy_sol_types::SolType; | ||
|
||
use clap::{Parser, ValueEnum}; | ||
use serde::{Deserialize, Serialize}; | ||
use sp1_sdk::{HashableKey, ProverClient, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey}; | ||
use std::path::PathBuf; | ||
|
||
/// The ELF (executable and linkable format) file for the Succinct RISC-V zkVM. | ||
pub const ELF: &[u8] = include_bytes!("../../../program/elf/riscv32im-succinct-zkvm-elf"); | ||
|
||
/// The arguments for the EVM command. | ||
#[derive(Parser, Debug)] | ||
#[clap(author, version, about, long_about = None)] | ||
struct EVMArgs { | ||
#[clap(long, value_enum, default_value = "groth16")] | ||
system: ProofSystem, | ||
|
||
#[clap(long, default_value = "100")] | ||
num_participants: u32, | ||
|
||
#[clap(long, default_value = "10")] | ||
num_winners: u32, | ||
|
||
#[clap(long, default_value = "12345")] | ||
random_seed: u64, | ||
} | ||
|
||
/// Enum representing the available proof systems | ||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)] | ||
enum ProofSystem { | ||
Plonk, | ||
Groth16, | ||
} | ||
|
||
/// 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 SP1ProofFixture { | ||
num_participants: u32, | ||
num_winners: u32, | ||
random_seed: u64, | ||
winners_merkle_root: String, | ||
vkey: String, | ||
public_values: String, | ||
proof: String, | ||
} | ||
|
||
sol! { | ||
/// The public values encoded as a struct that can be easily deserialized inside Solidity. | ||
struct PubValStruct { | ||
uint32 num_participants; | ||
uint32 num_winners; | ||
uint64 random_seed; | ||
bytes32 winners_merkle_root; | ||
} | ||
} | ||
|
||
fn main() { | ||
// Setup the logger. | ||
sp1_sdk::utils::setup_logger(); | ||
|
||
// Parse the command line arguments. | ||
let args = EVMArgs::parse(); | ||
|
||
// Setup the prover client. | ||
let client = ProverClient::new(); | ||
|
||
// Setup the program. | ||
let (pk, vk) = client.setup(ELF); | ||
|
||
// Setup the inputs. | ||
let mut stdin = SP1Stdin::new(); | ||
stdin.write(&args.num_participants); | ||
stdin.write(&args.num_winners); | ||
stdin.write(&args.random_seed); | ||
|
||
println!("Num Participants: {}", args.num_participants); | ||
println!("Num Winners: {}", args.num_winners); | ||
println!("Random Seed: {}", args.random_seed); | ||
println!("Proof System: {:?}", args.system); | ||
|
||
// Generate the proof based on the selected proof system. | ||
let proof = match args.system { | ||
ProofSystem::Plonk => client.prove(&pk, stdin).plonk().run(), | ||
ProofSystem::Groth16 => client.prove(&pk, stdin).groth16().run(), | ||
} | ||
.expect("failed to generate proof"); | ||
|
||
create_proof_fixture(&proof, &vk, args.system); | ||
} | ||
|
||
/// Create a fixture for the given proof. | ||
fn create_proof_fixture( | ||
proof: &SP1ProofWithPublicValues, | ||
vk: &SP1VerifyingKey, | ||
system: ProofSystem, | ||
) { | ||
// Deserialize the public values. | ||
let bytes = proof.public_values.as_slice(); | ||
let PubValStruct { | ||
num_participants, | ||
num_winners, | ||
random_seed, | ||
winners_merkle_root, | ||
} = PubValStruct::abi_decode(bytes, false).unwrap(); | ||
|
||
// Create the testing fixture so we can test things end-to-end. | ||
let fixture = SP1ProofFixture { | ||
num_participants, | ||
num_winners, | ||
random_seed, | ||
winners_merkle_root: format!("0x{}", hex::encode(winners_merkle_root.as_slice())), | ||
vkey: vk.bytes32().to_string(), | ||
public_values: format!("0x{}", hex::encode(bytes)), | ||
proof: format!("0x{}", hex::encode(proof.bytes())), | ||
}; | ||
|
||
// The verification key is used to verify that the proof corresponds to the execution of the | ||
// program on the given input. | ||
// | ||
// Note that the verification key stays the same regardless of the input. | ||
println!("Verification Key: {}", fixture.vkey); | ||
|
||
// The public values are the values which are publicly committed to by the zkVM. | ||
// | ||
// If you need to expose the inputs or outputs of your program, you should commit them in | ||
// the public values. | ||
println!("Public Values: {}", fixture.public_values); | ||
|
||
// The proof proves to the verifier that the program was executed with some inputs that led to | ||
// the give public values. | ||
println!("Proof Bytes: {}", fixture.proof); | ||
|
||
// Save the fixture to a file. | ||
let fixture_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../contracts/src/fixtures"); | ||
std::fs::create_dir_all(&fixture_path).expect("failed to create fixture path"); | ||
std::fs::write( | ||
fixture_path.join(format!("{:?}-fixture.json", system).to_lowercase()), | ||
serde_json::to_string_pretty(&fixture).unwrap(), | ||
) | ||
.expect("failed to write fixture"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
programs/per-user-proof/contracts/src/fixtures/groth16-fixture.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"numParticipants": 1000000, | ||
"numWinners": 10000, | ||
"randomSeed": 12345, | ||
"participationId": 1, | ||
"isWinner": false, | ||
"vkey": "0x0045e7d83fdafd41a15012355b47c5e83cf232237dbacb8cc6d63da869e2f5a8", | ||
"publicValues": "0x00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000000000303900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000", | ||
"proof": "0x6a2906ac0dfeea46ba844e22fa44f7edef156c390756272f705fd2c7df5747ba6c347211296ca2cf1540c3878f666aa5ac55481bb2a07f2b3b483dbf38079b23f11a63d90440863829090e0d0a76fe6039a1d63e321ef57677279001c256c9212479bb960e17dd625c798b58c73d80245d5acdbcd864f35c10029ef19f781c693c56448d074896dddc64961d04d155b7daecb01ffcd401c659da812e7f910a468e2f72f62b93abc6e80a6c74f9e02c0a5b170985b40c1924b41756e7c8e1b3fa233d17762e00175aecae51d17333016146806b6ee40bc336d5c139095cdffb64ba147e882ce30935531deddd897543f02340b0b0c560598237f9491e96bf55b78c76a49e" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.