Merge ccb9568e7eea9be10865e3dbfe2b830637d33baa into 7ba808630f2e1fa3231503b82429b6f88290b07b

This commit is contained in:
Manish Kumar 2025-03-05 01:45:04 +01:00 committed by GitHub
commit dc0c5bfd72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
36 changed files with 574 additions and 4 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
build
target
a.out
Cargo.lock

3
.gitmodules vendored
View File

@ -13,3 +13,6 @@
[submodule "hash/snark/src/hash-circuits"]
path = hash/snark/external/hash-circuits
url = https://github.com/faulhornlabs/hash-circuits
[submodule "hash/risc0/external/risc0"]
path = hash/risc0/external/risc0
url = https://github.com/risc0/risc0.git

@ -1 +1 @@
Subproject commit dd30dcb00221591db3a983e0215b81d86cff941d
Subproject commit 4d32708f511fd85c6b0fb131295cc73224246738

@ -1 +1 @@
Subproject commit 4dd0a02f1afd338f5207e40a47cac6705196b490
Subproject commit dbd2630daa6d599a9e78ad247e6858baf41664da

@ -1 +1 @@
Subproject commit 96e349786bd004e64a9cf50e0122f89863b24e92
Subproject commit 0fb198a9087531f32bb00bd13d4feaf813bc473a

@ -1 +1 @@
Subproject commit e6b99b20f038f27390f590313ce7de227d6dd42a
Subproject commit 3ae1517526f1061a8d37a159270cc15727e6b503

16
hash/sp1/bench/.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
# Cargo build
**/target
# Cargo config
.cargo
# Profile-guided optimization
/tmp
pgo-data.profdata
# MacOS nuisances
.DS_Store
# Proofs
**/proof-with-pis.json
**/proof-with-io.json

10
hash/sp1/bench/README.md Normal file
View File

@ -0,0 +1,10 @@
Benchmarking inside sp1 ZKVM
--------------------------------
- The `sp1/benches` contains the following hash `program`(the source code that will be proven inside the zkVM): sha256, keccak, blake2, blake3, and poseidon2 ober BN256.
- `script` folder contains the benchmarking code that contains proof generation and verification code for each hash program.
- The `build.sh` script builds the whole code.
- `run.sh` and `run_tree.sh` runs the benchmark. (`run.sh` for sha256, keccak, blake2, blake3 and `run_tree.sh` for poseidon2 over BN256)
- Benchmarks can be parameterized using environment variables. By convention, we start the names of these environment variables with the `ZKBENCH_` prefix.
- By default the `run.sh` will run the sha256 benchmark over 1KB of data. other hashes can be run by settig the environment variables accordingly.
- Additional files `bench.cfg` and `bench_tree.cfg` specifies the configurations and parameters.

11
hash/sp1/bench/bench.cfg Normal file
View File

@ -0,0 +1,11 @@
name: "Hashes benchmarking using sp1 prover"
author:
timeout: 200
params:
[ HASH_TYPE: [ "sha256", "keccak", "blake2", "blake3"]
, INPUT_SIZE_BYTES: [ 256, 512, 1024, 2048 ]
]
tags: sp1, $HASH_TYPE
comments:
The benchmarks includes for sha256, keccak, blake2, blake3 using sp1.

View File

@ -0,0 +1,10 @@
name: "Hashes benchmarking using sp1 prover"
author:
timeout: 500
params:
[ HASH_TYPE_TREE: [ "poseidon2"]
, TREE_DEPTH: [ 2, 4, 8, 16 ]
]
tags: sp1, $HASH_TYPE_TREE
comments:
The benchmarks includes for poseidon2(merkle hashing) over BN256.

View File

@ -0,0 +1,10 @@
[workspace]
[package]
version = "0.1.0"
name = "blake2-program"
edition = "2021"
[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e"}
blake2 = "0.10.6"

Binary file not shown.

View File

@ -0,0 +1,23 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
// blake2 of RustCrypto
use blake2::Digest;
use blake2::Blake2b;
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<u8>>();
// create a Blake2b object
let mut hasher = Blake2b::new();
// write input message
hasher.update(input);
// read hash digest
let result: [u8;32] = hasher.finalize().into();
sp1_zkvm::io::write(&result);
}

View File

@ -0,0 +1,9 @@
[workspace]
[package]
version = "0.1.0"
name = "blake3-program"
edition = "2021"
[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e" }
blake3 = "1.5.0"

Binary file not shown.

View File

@ -0,0 +1,22 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
// blake3 of https://github.com/BLAKE3-team/BLAKE3 (official implementation)
use blake3::Hasher;
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<u8>>();
// create a Blake3 object
let mut hasher = Hasher::new();
// write input message
hasher.update(&input);
// read hash digest
let result: [u8;32] = hasher.finalize().into();
sp1_zkvm::io::write(&result);
}

13
hash/sp1/bench/build.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
cd blake2
cargo prove build
cd ../keccak
cargo prove build
cd ../sha256
cargo prove build
cd ../blake3
cargo prove build
cd ../poseidon2
cargo prove build
cd ../script
cargo build --release

View File

@ -0,0 +1,9 @@
[workspace]
[package]
version = "0.1.0"
name = "keccak-program"
edition = "2021"
[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git", rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e" }
tiny-keccak = { git = "https://github.com/succinctlabs/tiny-keccak-private", branch = "patch-v2.0.2", features = ["keccak"] }

Binary file not shown.

View File

@ -0,0 +1,23 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
// patched keccak rust crate https://github.com/sp1-patches/tiny-keccak
use tiny_keccak::{Hasher, Keccak};
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<u8>>();
// create a keccak object
let mut hasher = Keccak::v256();
// write input message
hasher.update(&input);
// read hash digest
let mut result: [u8;32] = [0;32];
hasher.finalize(&mut result);
sp1_zkvm::io::write(&result);
}

View File

@ -0,0 +1,12 @@
[workspace]
[package]
version = "0.1.0"
name = "poseidon2-program"
edition = "2021"
[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git" , rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e"}
zkhash = {git = "https://github.com/HorizenLabs/poseidon2"}
ark-serialize = "0.4"

Binary file not shown.

View File

@ -0,0 +1,35 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
// poseidon2 https://github.com/HorizenLabs/poseidon2
use zkhash::poseidon2::poseidon2::Poseidon2;
use zkhash::poseidon2::poseidon2_instance_bn256::POSEIDON2_BN256_PARAMS;
use zkhash::merkle_tree::merkle_tree_fp::MerkleTree;
use zkhash::fields::bn256::FpBN256;
use ark_serialize::{CanonicalSerialize, CanonicalDeserialize};
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<Vec<u8>>>();
//build input
let mut hash_data: Vec<FpBN256> = Vec::new();
for i in 0..input.len() {
let a_uncompressed = FpBN256::deserialize_uncompressed(&**input.get(i).unwrap()).unwrap();
hash_data.push(a_uncompressed);
}
// create a poseidon2 merkle object
let perm = Poseidon2::new(&POSEIDON2_BN256_PARAMS);
let mut mt = MerkleTree::new(perm.clone());
let hash_final = mt.accumulate(&hash_data);
let mut hash_bytes: Vec<u8> = Vec::new();
hash_final.serialize_uncompressed(&mut hash_bytes).unwrap();
sp1_zkvm::io::write(&hash_bytes);
}

15
hash/sp1/bench/run.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
if [ -z ${ZKBENCH_HASH_TYPE} ]; then
ZKBENCH_HASH_TYPE="sha256"
fi
if [ -z ${ZKBENCH_INPUT_SIZE_BYTES} ]; then
ZKBENCH_INPUT_SIZE_BYTES=1024
fi
cd script
echo "Running benchmarks with the following configurations:"
echo "HASH = $ZKBENCH_HASH_TYPE"
echo "Input Size (Bytes) = $ZKBENCH_INPUT_SIZE_BYTES"
# Run the benchmarks
RUST_LOG=info ./target/release/bench-script $ZKBENCH_HASH_TYPE $ZKBENCH_INPUT_SIZE_BYTES

16
hash/sp1/bench/run_tree.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/bash
if [ -z ${ZKBENCH_HASH_TYPE_TREE} ]; then
ZKBENCH_HASH_TYPE_TREE="poseidon2"
fi
if [ -z ${ZKBENCH_TREE_DEPTH} ]; then
ZKBENCH_TREE_DEPTH=4
fi
cd script
echo "Running benchmarks with the following configurations:"
echo "HASH = $ZKBENCH_HASH_TYPE_TREE"
echo "Tree Depth = $ZKBENCH_TREE_DEPTH"
# Run the benchmarks
RUST_LOG=info ./target/release/bench-script $ZKBENCH_HASH_TYPE_TREE $ZKBENCH_TREE_DEPTH

View File

@ -0,0 +1,12 @@
[workspace]
[package]
version = "0.1.0"
name = "bench-script"
edition = "2021"
[dependencies]
sp1-core = { git = "https://github.com/succinctlabs/sp1.git", rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e" }
rand = "0.8"
hex = "0.4"
zkhash = {git = "https://github.com/HorizenLabs/poseidon2"}
ark-serialize = "0.4"

View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-01-25"
components = ["llvm-tools", "rustc-dev"]

View File

@ -0,0 +1,43 @@
use sp1_core::{SP1Prover, SP1Stdin, SP1Verifier};
use rand::Rng;
use hex::encode;
const BLAKE2_ELF: &[u8] = include_bytes!("../../../blake2/elf/riscv32im-succinct-zkvm-elf");
pub fn generate_random_bytes(length: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..length).map(|_| rng.gen::<u8>()).collect()
}
pub fn blake2_benchmark(size: usize) {
// Generate proof.
let mut stdin = SP1Stdin::new();
let data = generate_random_bytes(size);
stdin.write(&data);
let t0 = std::time::Instant::now();
let mut proof = SP1Prover::prove(BLAKE2_ELF, stdin).expect("proving failed");
let t1 = t0.elapsed();
// Read output.
let hash_bytes = proof.stdout.read::<[u8;32]>();
let hash = encode(hash_bytes);
println!("hash: {}", hash);
// Verify proof.
let t2 = std::time::Instant::now();
SP1Verifier::verify(BLAKE2_ELF, &proof).expect("verification failed");
let t3 = t2.elapsed();
// Save proof.
proof
.save("proof-with-io.json")
.expect("saving proof failed");
println!("succesfully generated and verified proof for the program!");
println!("Proof Generation Time: {:?}", t1);
println!("Proof verification Time: {:?}", t3);
}

View File

@ -0,0 +1,43 @@
use sp1_core::{SP1Prover, SP1Stdin, SP1Verifier};
use rand::Rng;
use hex::encode;
const BLAKE3_ELF: &[u8] = include_bytes!("../../../blake3/elf/riscv32im-succinct-zkvm-elf");
pub fn generate_random_bytes(length: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..length).map(|_| rng.gen::<u8>()).collect()
}
pub fn blake3_benchmark(size: usize) {
// Generate proof.
let mut stdin = SP1Stdin::new();
let data = generate_random_bytes(size);
stdin.write(&data);
let t0 = std::time::Instant::now();
let mut proof = SP1Prover::prove(BLAKE3_ELF, stdin).expect("proving failed");
let t1 = t0.elapsed();
// Read output.
let hash_bytes = proof.stdout.read::<[u8;32]>();
let hash = encode(hash_bytes);
println!("hash: {}", hash);
// Verify proof.
let t2 = std::time::Instant::now();
SP1Verifier::verify(BLAKE3_ELF, &proof).expect("verification failed");
let t3 = t2.elapsed();
// Save proof.
proof
.save("proof-with-io.json")
.expect("saving proof failed");
println!("succesfully generated and verified proof for the program!");
println!("Proof Generation Time: {:?}", t1);
println!("Proof verification Time: {:?}", t3);
}

View File

@ -0,0 +1,42 @@
use sp1_core::{SP1Prover, SP1Stdin, SP1Verifier};
use rand::Rng;
use hex::encode;
const KECCAK_ELF: &[u8] = include_bytes!("../../../keccak/elf/riscv32im-succinct-zkvm-elf");
pub fn generate_random_bytes(length: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..length).map(|_| rng.gen::<u8>()).collect()
}
pub fn keccak_benchmark(size: usize) {
// Generate proof.
let mut stdin = SP1Stdin::new();
let data = generate_random_bytes(size);
stdin.write(&data);
let t0 = std::time::Instant::now();
let mut proof = SP1Prover::prove(KECCAK_ELF, stdin).expect("proving failed");
let t1 = t0.elapsed();
// Read output.
let hash_bytes = proof.stdout.read::<[u8;32]>();
let hash = encode(hash_bytes);
println!("hash: {}", hash);
// Verify proof.
let t2 = std::time::Instant::now();
SP1Verifier::verify(KECCAK_ELF, &proof).expect("verification failed");
let t3 = t2.elapsed();
// Save proof.
proof
.save("proof-with-io.json")
.expect("saving proof failed");
println!("succesfully generated and verified proof for the program!");
println!("Proof Generation Time: {:?}", t1);
println!("Proof verification Time: {:?}", t3);
}

View File

@ -0,0 +1,50 @@
use sp1_core::{SP1Prover, SP1Stdin, SP1Verifier};
use zkhash::fields::{bn256::FpBN256, utils::random_scalar};
use ark_serialize::{CanonicalSerialize, CanonicalDeserialize};
const POSEIDON2_ELF: &[u8] = include_bytes!("../../../poseidon2/elf/riscv32im-succinct-zkvm-elf");
pub fn poseidon2_benchmark(mt_depth: usize) {
// Generate proof.
let mut stdin = SP1Stdin::new();
type Scalar = FpBN256;
// generating data and serialize
let mut input_scalar: Vec<Vec<u8>> = Vec::new();
let number_of_leaves: u32 = 1 << mt_depth;
for _ in 0..number_of_leaves {
let mut uncompressed_bytes = Vec::new();
let a: Scalar = random_scalar();
a.serialize_uncompressed(&mut uncompressed_bytes).unwrap();
input_scalar.push(uncompressed_bytes);
}
stdin.write(&input_scalar);
let t0 = std::time::Instant::now();
let mut proof = SP1Prover::prove(POSEIDON2_ELF, stdin).expect("proving failed");
let t1 = t0.elapsed();
// Read output.
let hash_bytes = proof.stdout.read::<Vec<u8>>();
let hash_final = Scalar::deserialize_uncompressed(&*hash_bytes).unwrap();
println!("hash: {}", hash_final);
// Verify proof.
let t2 = std::time::Instant::now();
SP1Verifier::verify(POSEIDON2_ELF, &proof).expect("verification failed");
let t3 = t2.elapsed();
// Save proof.
proof
.save("proof-with-io.json")
.expect("saving proof failed");
println!("succesfully generated and verified proof for the program!");
println!("Proof Generation Time: {:?}", t1);
println!("Proof verification Time: {:?}", t3);
}

View File

@ -0,0 +1,42 @@
use sp1_core::{SP1Prover, SP1Stdin, SP1Verifier};
use rand::Rng;
use hex::encode;
const SHA256_ELF: &[u8] = include_bytes!("../../../sha256/elf/riscv32im-succinct-zkvm-elf");
pub fn generate_random_bytes(length: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..length).map(|_| rng.gen::<u8>()).collect()
}
pub fn sha256_benchmark(size: usize) {
// Generate proof.
let mut stdin = SP1Stdin::new();
let data = generate_random_bytes(size);
stdin.write(&data);
let t0 = std::time::Instant::now();
let mut proof = SP1Prover::prove(SHA256_ELF, stdin).expect("proving failed");
let t1 = t0.elapsed();
// Read output.
let hash_bytes = proof.stdout.read::<[u8;32]>();
let hash = encode(hash_bytes);
println!("hash: {}", hash);
// Verify proof.
let t2 = std::time::Instant::now();
SP1Verifier::verify(SHA256_ELF, &proof).expect("verification failed");
let t3 = t2.elapsed();
// Save proof.
proof
.save("proof-with-io.json")
.expect("saving proof failed");
println!("succesfully generated and verified proof for the program!");
println!("Proof Generation Time: {:?}", t1);
println!("Proof verification Time: {:?}", t3);
}

View File

@ -0,0 +1,65 @@
pub mod benches{
pub mod keccak;
pub mod sha256;
pub mod blake2;
pub mod blake3;
pub mod poseidon2;
}
use crate::benches::{
keccak::keccak_benchmark,
sha256::sha256_benchmark,
blake2::blake2_benchmark,
blake3::blake3_benchmark,
poseidon2::poseidon2_benchmark
};
use sp1_core::utils;
use std::process;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 3 {
println!("Wrong number of arguments! The program expects two arguments: <hash_type> and <size>");
// Exit the program with a non-zero exit code
process::exit(1);
}
let bench_type = &args[1];
let size = args[2].parse::<usize>().unwrap();
// Setup a tracer for logging.
utils::setup_logger();
match bench_type.as_str() {
"sha256" => {
println!("Running sha256: ");
let _ = sha256_benchmark(size);
}
"keccak" => {
println!("Running keccak benchmark: ");
let _ = keccak_benchmark(size);
}
"blake2" => {
println!("Running blake2 benchmark: ");
let _ = blake2_benchmark(size);
}
"blake3" => {
println!("Running blake3 benchmark: ");
let _ = blake3_benchmark(size);
}
"poseidon2" => {
println!("Running poseidon2 benchmark: ");
let _ = poseidon2_benchmark(size);
}
_ => {
println!("Wrong Benchmark Name!");
}
}
}

View File

@ -0,0 +1,10 @@
[workspace]
[package]
version = "0.1.0"
name = "sha256-program"
edition = "2021"
[dependencies]
sp1-zkvm = { git = "https://github.com/succinctlabs/sp1.git" , rev = "4528b7861c60ef5e25c2d0b328fc6d244e2ac98e"}
sha2-v0-10-8 = { git = "https://github.com/sp1-patches/RustCrypto-hashes", package = "sha2", branch = "patch-v0.10.8" }

Binary file not shown.

View File

@ -0,0 +1,22 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
//The patched sha2 rust crate https://github.com/sp1-patches/RustCrypto-hashes
use sha2_v0_10_8::{Digest, Sha256};
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<u8>>();
// create a Sha256 object
let mut hasher = Sha256::new();
// write input message
hasher.update(input);
// read hash digest
let result: [u8;32] = hasher.finalize().into();
sp1_zkvm::io::write(&result);
}