Skip to content

Commit

Permalink
Add groth16 experiment scripts and results
Browse files Browse the repository at this point in the history
  • Loading branch information
oyyblin committed Sep 23, 2024
1 parent b41cd7a commit 4305a05
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 28 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 30 additions & 12 deletions programs/per-raffle-proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@ cargo run --release -- --execute --num-participants 1000 --num-winners 10

Data below assumes random seed of `12345`

| Participants | Winners | Cycle Count | Merkle Root |
| ------------ | ------- | ----------- | ------------------------------------------------------------------ |
| 100 | 10 | 51183 | 0xced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75 |
| 1000 | 10 | 51183 | 0xbf449f1db6ee091b380b110ef54ff7112835222d91753c2ec8998971a2fd6c53 |
| 1000 | 100 | 389721 | 0xcf8d690ca84b2bae826470057e3b7585e5d3530f019e78c7fc6f262cf6869a73 |
| 10000 | 10 | 51183 | 0x06444ad88114b90ab4db3019d37c2955bbde6e15630d06e43074b346862cdac6 |
| 10000 | 100 | 395997 | 0x9971ec5531a3fad1e9c38c6a77a54d24bf6cafe950928608f8304e3a7fe0aa1c |
| 100000 | 10 | 52741 | 0x5d39e729f2250f4ef96561c7afbcd1bc0d03264bc634b5a9c52b07a6bddaf42d |
| 1000000 | 10 | 53091 | 0x9dd3660d5aa5552da30cb6f196bd1c21cf2b9e6676f0a97c7068cc7dcdc9543e |
| 10000000 | 10 | 53418 | 0xb72907e34b589edee7cdb4236efe59f2367d527a2bbb7c06104100fe02f1c0fc |
| 10000000 | 100 | 420673 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 |
| 10000000 | 1000 | 6291455 | 0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d |
| Participants | Winners | Cycle Count | Merkle Root | Groth16 Proof Time | Groth16 Prover Network URL |
| ------------ | ------- | ----------- | ------------------------------------------------------------------ | ------------------ | --------------------------------------------------------------------- |
| 100 | 10 | 51228 | 0xced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75 | 2m43s | https://explorer.succinct.xyz/proofrequest_01j8egy82degrts5fay3cn81wq |
| 1000 | 10 | 51228 | 0xbf449f1db6ee091b380b110ef54ff7112835222d91753c2ec8998971a2fd6c53 | | |
| 1000 | 100 | 389721 | 0xcf8d690ca84b2bae826470057e3b7585e5d3530f019e78c7fc6f262cf6869a73 | | |
| 10000 | 10 | 51228 | 0x06444ad88114b90ab4db3019d37c2955bbde6e15630d06e43074b346862cdac6 | | |
| 10000 | 100 | 395997 | 0x9971ec5531a3fad1e9c38c6a77a54d24bf6cafe950928608f8304e3a7fe0aa1c | | |
| 100000 | 10 | 52741 | 0x5d39e729f2250f4ef96561c7afbcd1bc0d03264bc634b5a9c52b07a6bddaf42d | | |
| 100000 | 100 | 420673 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 | | |
| 10000000 | 10 | 53418 | 0xb72907e34b589edee7cdb4236efe59f2367d527a2bbb7c06104100fe02f1c0fc | | |
| 10000000 | 100 | 420673 | 0x76fe23ca034289be494fcccb15a4f91af2233150fc27b198b5ffa8d6aef26224 | 2m46s | https://explorer.succinct.xyz/proofrequest_01j8eh6905egrr635fymhr2ah7 |
| 10000000 | 1000 | 6290600 | 0x62383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d | 3m6s | https://explorer.succinct.xyz/proofrequest_01j8ema4nvegtt76dqvk0s7brp |

## Logs

100 participants, 10 winners(Groth16)

```
Verification Key: 0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4
Public Values: 0x0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000003039ced48ce2a54393639311900331de7fa22ee1a0ed8410119aa3ee6c17733aeb75
Proof Bytes: 0x6a2906ac092b7e497bcb047ad6e008a2f2b78e77b9a2732f282e897645fa339e29cae7750fc6ed66350f1ed84402ed0ed2a3c7104b896c44dcb93e6897eee6ae42f5f5572ad3ada13877fc89e0275d292c2a176d594220db67e075f6f851a34f50d96dbb16e6a7488579d42bbe8b88badb8a1e2a0bc7194e2f241540bb238928b62195ae2196b9a19d4a4279a8581ebf7a4706dbae9f69b941eab1e62d463f503981d8252022f9d4eee756e6fc1530d2cc1b097d0c715e325ca6e06a9723ca607c6ecbca15aff32d40288ddbdaf08141f76b795584d0dabffa6d563a807da42c951452992ac8cc2676a41c1afcfec6d95f0ec12d378581bff41f802335f6a8e6911cd0a0
```

10000000 participants, 1000 winners Groth16)

```
Verification Key: 0x007502a17bd054ca2d7bfdd7a0a9a7066b41d142eb76c6c2b296880305f8c7c4
Public Values: 0x000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000003e8000000000000000000000000000000000000000000000000000000000000303962383a7facecc690676ae8f078d164a26f8c75c1f58d66eae2602740d500345d
Proof Bytes: 0x6a2906ac2bdc11c34385d676b280cbaa646d584c2f65f88341b11405e12fb18a750db8aa0e2d0a73536b05d5a67fbac543df23ff862d1e61f8c4bb49acc2aa9ad5b4edee1ed8af0233d985f535aa9518f533d1a9ab06f1e1680fd12e567d495ea546c0fd0a67b50e220ec2c9576666b69084f1853c6ec0d64eda22e76a30a2cd111eb12e06dcc8dd1fc8cd8f533bb35092d0f59591c54e4ef100919b6e53bd824473b56f0f63cc18213c64404a224cf4b98f320e286ce024b9db9dc7dce3648fb04ced733056bcd3e70ed46e9417853fc41ff6e45be5a48d5ecb374e57eba8994b54926103846766c514836ac41b58d4dba5ae591e4ec8a6436c4e7c5ac536a5b80e998f
```
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"
}
2 changes: 2 additions & 0 deletions programs/per-raffle-proof/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ path = "src/bin/evm.rs"
sp1-sdk = { workspace = true }
alloy-sol-types = { workspace = true }
clap = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
hex = { workspace = true }

[build-dependencies]
Expand Down
155 changes: 155 additions & 0 deletions programs/per-raffle-proof/script/src/bin/evm.rs
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");
}
51 changes: 35 additions & 16 deletions programs/per-user-proof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,45 @@
- Using patched crate to improve performance: https://docs.succinct.xyz/writing-programs/patched-crates.html
- Shuffle: Fisher-Yates

## Performance
## Performance (cycle tracking)

```
cargo run --release -- --execute --num-participants 1000 --num-winners 10
```

Using `raffle_naive`:

| Participants | Winners | Cycle Count |
| ------------ | ------- | ----------- |
| 100 | 10 | 62338 |
| 1000 | 10 | 62338 |
| 1000 | 100 | 512989 |
| 10000 | 10 | 62338 |
| 10000 | 100 | 513868 |
| 100000 | 10 | 63896 |
| 100000 | 100 | 530818 |
| 100000 | 1000 | 7412270 |
| 100000 | 10000 | 284134198 |
| 1000000 | 10 | 64246 |
| 1000000 | 100 | 534856 |
| 1000000 | 1000 | 7463342 |
| 1000000 | 10000 | 298288259 |
| Participants | Winners | Cycle Count | Groth16 Proof Time | Groth16 Prover Network URL |
| ------------ | ------- | ----------- | ------------------ | --------------------------------------------------------------------- |
| 100 | 10 | 62338 | 2m30s | https://explorer.succinct.xyz/proofrequest_01j8eghd0wfpz8yy33ph1qxj15 |
| 1000 | 10 | 62338 | | |
| 1000 | 100 | 512989 | | |
| 10000 | 10 | 62338 | | |
| 10000 | 100 | 513868 | | |
| 100000 | 10 | 63896 | | |
| 100000 | 100 | 530818 | | |
| 100000 | 1000 | 7412270 | | |
| 100000 | 10000 | 284134198 | | |
| 1000000 | 10 | 64246 | | |
| 1000000 | 100 | 534856 | | |
| 1000000 | 1000 | 7463342 | | |
| 1000000 | 10000 | 298288259 | 5m56s | https://explorer.succinct.xyz/proofrequest_01j8emngr4egtsharwzjv3f8fm |

## Logs

100 participants, 10 winners (Groth16)

```
2024-09-23T04:02:00.006226Z INFO Proof request fulfilled
Verification Key: 0x0045e7d83fdafd41a15012355b47c5e83cf232237dbacb8cc6d63da869e2f5a8
Public Values: 0x0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000303900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000
Proof Bytes: 0x6a2906ac1bcf54c145c83d3b3849fc724d07a38035d0ba1e740dd20880ff6e97fe3feafd22e5b2f69a58dd8a3207a7099176711c57b753bb06a42cb28af1b86a9a71ac581cbdcc95f222014da8a7d969838caf760408c3d16c6c70a523831cb1237a14140ad30389e6e089de657e2c6855b590192b719ce20451e88c0cfde64e974253c62903eea65c5119d99d9ee41ca7105c73de472abb80c182943ae99a077a96e5e2116c39c14ccb14710c17c374caa7416627c99daf03e02f67c2028eab897a02ca1758f968d26d6426e8d1ed09d7297e74dca31cfb54ea592e91419e1059e83f4c0700aa813d250b6f2e2bd5b427974fc9e22906727c1967966c38f0125b7df5ec
```

1000000 participants, 10000 winners

```
Verification Key: 0x0045e7d83fdafd41a15012355b47c5e83cf232237dbacb8cc6d63da869e2f5a8
Public Values: 0x00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000000000303900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000
Proof Bytes: 0x6a2906ac0dfeea46ba844e22fa44f7edef156c390756272f705fd2c7df5747ba6c347211296ca2cf1540c3878f666aa5ac55481bb2a07f2b3b483dbf38079b23f11a63d90440863829090e0d0a76fe6039a1d63e321ef57677279001c256c9212479bb960e17dd625c798b58c73d80245d5acdbcd864f35c10029ef19f781c693c56448d074896dddc64961d04d155b7daecb01ffcd401c659da812e7f910a468e2f72f62b93abc6e80a6c74f9e02c0a5b170985b40c1924b41756e7c8e1b3fa233d17762e00175aecae51d17333016146806b6e
```
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"
}
3 changes: 3 additions & 0 deletions programs/per-user-proof/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ path = "src/bin/evm.rs"
sp1-sdk = { workspace = true }
alloy-sol-types = { workspace = true }
clap = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
hex = { workspace = true }

[build-dependencies]
sp1-helper = { workspace = true }
Binary file removed programs/per-user-proof/script/per-user-proof.bin
Binary file not shown.
Loading

0 comments on commit 4305a05

Please sign in to comment.