Skip to content

Commit

Permalink
chore: cleanup (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 authored Oct 7, 2024
1 parent 005c2ad commit 0d6f1a7
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A Groth16 verifier implementation for Solana using BN254 precompiles. This crate verifies Groth16 proofs generated with SP1, leveraging Solana's BN254 precompiles for efficient cryptographic operations.

> [!CAUTION]
>
> This repository is under active development and is not yet ready for production use.
## Features

- **Groth16 Proof Verification**: Implements the Groth16 protocol for zero-knowledge proof verification.
Expand Down
13 changes: 12 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ And run the following commands:
cd script
cargo build
RUST_LOG=info cargo run --release -- --elf fibonacci
```
```

Pass in the `--prove` flag to generate a fresh proof and save it to a file:

```shell
cd script
cargo build
RUST_LOG=info cargo run --release -- --elf fibonacci --prove
```

Note: The pre-generated proof for `fibonacci` assumes the input `n` is 20. The pre-generated proof
for `is-prime` assumes the input `n` is 11.
3 changes: 1 addition & 2 deletions example/script/Cargo.lock

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

3 changes: 1 addition & 2 deletions example/script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example-usage"
name = "example"
authors = ["Bhargav Annem"]
version = "0.1.0"
edition = "2021"
Expand All @@ -10,7 +10,6 @@ hex = "0.4.3"
num-bigint = "0.4.6"
num-traits = "0.2.19"
groth16-solana = { path = "../../verifier" }
sp1-prover = "2.0.0"
sp1-sdk = "2.0.0"
strum = "0.25"
strum_macros = "0.25"
Expand Down
49 changes: 30 additions & 19 deletions example/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub const SHA2_ELF: &[u8] = include_bytes!("../../elfs/sha2-riscv32im-succinct-z
pub const TENDERMINT_ELF: &[u8] =
include_bytes!("../../elfs/tendermint-riscv32im-succinct-zkvm-elf");

pub(crate) const GROTH16_VK_BYTES: &[u8] =
include_bytes!("../../../../.sp1/circuits/v2.0.0/groth16_vk.bin");
pub const GROTH16_VK_BYTES: &[u8] = include_bytes!("../../vk/groth16_v2.0.0.bin");

#[derive(clap::Parser)]
#[command(name = "zkVM Proof Generator")]
Expand All @@ -26,6 +25,13 @@ struct Cli {
help = "Specifies the ELF file to use (e.g., fibonacci, is-prime)"
)]
elf: String,
#[arg(
long,
value_name = "prove",
default_value = "false",
help = "Specifies the ELF file to use (e.g., fibonacci, is-prime)"
)]
prove: bool,
}

#[derive(Debug, EnumString, EnumIter, Display)]
Expand All @@ -52,10 +58,10 @@ impl Elf {
}

fn main() {
// Setup logging for the application
// Setup logging for the application.
utils::setup_logger();

// Parse command line arguments
// Parse command line arguments.
let args = Cli::parse();
let mut stdin = SP1Stdin::new();

Expand All @@ -75,20 +81,25 @@ fn main() {
Elf::Sha2 | Elf::Tendermint => elf_enum.get_elf(),
};

// Initialize the prover client
let client = ProverClient::new();
let (pk, _) = client.setup(elf);

// Generate a proof for the specified program
let proof = client
.prove(&pk, stdin)
.groth16()
.run()
.expect("Groth16 proof generation failed");

// Save the generated proof to a binary file
// Where to save / load the proof from.
let proof_file = format!("../binaries/{}_proof.bin", args.elf);
proof.save(&proof_file).unwrap();

// Only generate a proof if the prove flag is set.
if args.prove {
// Initialize the prover client
let client = ProverClient::new();
let (pk, _) = client.setup(elf);

// Generate a proof for the specified program.
let proof = client
.prove(&pk, stdin)
.groth16()
.run()
.expect("Groth16 proof generation failed");

// Save the generated proof to `proof_file`.
proof.save(&proof_file).unwrap();
}

// Load the saved proof and convert it to a Groth16 proof
let (raw_proof, public_inputs) = SP1ProofWithPublicValues::load(&proof_file)
Expand All @@ -101,7 +112,7 @@ fn main() {
})
.expect("Failed to load proof");

// Convert public inputs to byte representations
// Convert public inputs to byte representations.
let vkey_hash = BigUint::from_str_radix(&public_inputs[0], 10)
.unwrap()
.to_bytes_be();
Expand Down Expand Up @@ -141,7 +152,7 @@ mod tests {
})
.expect("Failed to load proof");

// Convert public inputs to byte representations
// Convert public inputs to byte representations.
let vkey_hash = BigUint::from_str_radix(&public_inputs[0], 10)
.unwrap()
.to_bytes_be();
Expand Down
Binary file added example/vk/groth16_v2.0.0.bin
Binary file not shown.
6 changes: 3 additions & 3 deletions verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ pub struct PublicInputs<const N: usize> {
}

pub fn decompress_g1(g1_bytes: &[u8; 32]) -> Result<[u8; 64], Error> {
let g1_bytes = gnark_commpressed_x_to_ark_commpressed_x(g1_bytes)?;
let g1_bytes = gnark_compressed_x_to_ark_compressed_x(g1_bytes)?;
let g1_bytes = convert_endianness::<32, 32>(&g1_bytes.as_slice().try_into().unwrap());
groth16_solana::decompression::decompress_g1(&g1_bytes).map_err(|_| Error::G1CompressionError)
}

pub fn decompress_g2(g2_bytes: &[u8; 64]) -> Result<[u8; 128], Error> {
let g2_bytes = gnark_commpressed_x_to_ark_commpressed_x(g2_bytes)?;
let g2_bytes = gnark_compressed_x_to_ark_compressed_x(g2_bytes)?;
let g2_bytes = convert_endianness::<64, 64>(&g2_bytes.as_slice().try_into().unwrap());
groth16_solana::decompression::decompress_g2(&g2_bytes).map_err(|_| Error::G2CompressionError)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ fn gnark_flag_to_ark_flag(msb: u8) -> Result<u8, Error> {
Ok(msb & !ARK_MASK | ark_flag)
}

fn gnark_commpressed_x_to_ark_commpressed_x(x: &[u8]) -> Result<Vec<u8>, Error> {
fn gnark_compressed_x_to_ark_compressed_x(x: &[u8]) -> Result<Vec<u8>, Error> {
if x.len() != 32 && x.len() != 64 {
return Err(Error::InvalidInput);
}
Expand Down

0 comments on commit 0d6f1a7

Please sign in to comment.