diff --git a/.gitignore b/.gitignore index 9479611..0bc5b19 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build target a.out +Cargo.lock \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 0545ce4..e579aae 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/hash/cpu/external/Blake3 b/hash/cpu/external/Blake3 index dd30dcb..4d32708 160000 --- a/hash/cpu/external/Blake3 +++ b/hash/cpu/external/Blake3 @@ -1 +1 @@ -Subproject commit dd30dcb00221591db3a983e0215b81d86cff941d +Subproject commit 4d32708f511fd85c6b0fb131295cc73224246738 diff --git a/hash/cpu/external/constantine b/hash/cpu/external/constantine index 4dd0a02..dbd2630 160000 --- a/hash/cpu/external/constantine +++ b/hash/cpu/external/constantine @@ -1 +1 @@ -Subproject commit 4dd0a02f1afd338f5207e40a47cac6705196b490 +Subproject commit dbd2630daa6d599a9e78ad247e6858baf41664da diff --git a/hash/cpu/external/zikkurat-algebra b/hash/cpu/external/zikkurat-algebra index 96e3497..0fb198a 160000 --- a/hash/cpu/external/zikkurat-algebra +++ b/hash/cpu/external/zikkurat-algebra @@ -1 +1 @@ -Subproject commit 96e349786bd004e64a9cf50e0122f89863b24e92 +Subproject commit 0fb198a9087531f32bb00bd13d4feaf813bc473a diff --git a/hash/snark/external/hash-circuits b/hash/snark/external/hash-circuits index e6b99b2..3ae1517 160000 --- a/hash/snark/external/hash-circuits +++ b/hash/snark/external/hash-circuits @@ -1 +1 @@ -Subproject commit e6b99b20f038f27390f590313ce7de227d6dd42a +Subproject commit 3ae1517526f1061a8d37a159270cc15727e6b503 diff --git a/hash/sp1/bench/.gitignore b/hash/sp1/bench/.gitignore new file mode 100644 index 0000000..0be6e72 --- /dev/null +++ b/hash/sp1/bench/.gitignore @@ -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 diff --git a/hash/sp1/bench/README.md b/hash/sp1/bench/README.md new file mode 100644 index 0000000..55f09f8 --- /dev/null +++ b/hash/sp1/bench/README.md @@ -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. diff --git a/hash/sp1/bench/bench.cfg b/hash/sp1/bench/bench.cfg new file mode 100644 index 0000000..0aa0b9d --- /dev/null +++ b/hash/sp1/bench/bench.cfg @@ -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. diff --git a/hash/sp1/bench/bench_tree.cfg b/hash/sp1/bench/bench_tree.cfg new file mode 100644 index 0000000..c09e4bc --- /dev/null +++ b/hash/sp1/bench/bench_tree.cfg @@ -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. \ No newline at end of file diff --git a/hash/sp1/bench/blake2/Cargo.toml b/hash/sp1/bench/blake2/Cargo.toml new file mode 100644 index 0000000..8c830ea --- /dev/null +++ b/hash/sp1/bench/blake2/Cargo.toml @@ -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" + diff --git a/hash/sp1/bench/blake2/elf/riscv32im-succinct-zkvm-elf b/hash/sp1/bench/blake2/elf/riscv32im-succinct-zkvm-elf new file mode 100755 index 0000000..54aee86 Binary files /dev/null and b/hash/sp1/bench/blake2/elf/riscv32im-succinct-zkvm-elf differ diff --git a/hash/sp1/bench/blake2/src/main.rs b/hash/sp1/bench/blake2/src/main.rs new file mode 100644 index 0000000..d1c3a43 --- /dev/null +++ b/hash/sp1/bench/blake2/src/main.rs @@ -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::>(); + + // 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); + +} diff --git a/hash/sp1/bench/blake3/Cargo.toml b/hash/sp1/bench/blake3/Cargo.toml new file mode 100644 index 0000000..eae6595 --- /dev/null +++ b/hash/sp1/bench/blake3/Cargo.toml @@ -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" \ No newline at end of file diff --git a/hash/sp1/bench/blake3/elf/riscv32im-succinct-zkvm-elf b/hash/sp1/bench/blake3/elf/riscv32im-succinct-zkvm-elf new file mode 100755 index 0000000..8d75bf2 Binary files /dev/null and b/hash/sp1/bench/blake3/elf/riscv32im-succinct-zkvm-elf differ diff --git a/hash/sp1/bench/blake3/src/main.rs b/hash/sp1/bench/blake3/src/main.rs new file mode 100644 index 0000000..e44992d --- /dev/null +++ b/hash/sp1/bench/blake3/src/main.rs @@ -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::>(); + + // 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); + +} diff --git a/hash/sp1/bench/build.sh b/hash/sp1/bench/build.sh new file mode 100755 index 0000000..d05e9b0 --- /dev/null +++ b/hash/sp1/bench/build.sh @@ -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 diff --git a/hash/sp1/bench/keccak/Cargo.toml b/hash/sp1/bench/keccak/Cargo.toml new file mode 100644 index 0000000..cdc9e04 --- /dev/null +++ b/hash/sp1/bench/keccak/Cargo.toml @@ -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"] } \ No newline at end of file diff --git a/hash/sp1/bench/keccak/elf/riscv32im-succinct-zkvm-elf b/hash/sp1/bench/keccak/elf/riscv32im-succinct-zkvm-elf new file mode 100755 index 0000000..688d2e0 Binary files /dev/null and b/hash/sp1/bench/keccak/elf/riscv32im-succinct-zkvm-elf differ diff --git a/hash/sp1/bench/keccak/src/main.rs b/hash/sp1/bench/keccak/src/main.rs new file mode 100644 index 0000000..ba94ff2 --- /dev/null +++ b/hash/sp1/bench/keccak/src/main.rs @@ -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::>(); + + // 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); +} diff --git a/hash/sp1/bench/poseidon2/Cargo.toml b/hash/sp1/bench/poseidon2/Cargo.toml new file mode 100644 index 0000000..54b67d4 --- /dev/null +++ b/hash/sp1/bench/poseidon2/Cargo.toml @@ -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" + + diff --git a/hash/sp1/bench/poseidon2/elf/riscv32im-succinct-zkvm-elf b/hash/sp1/bench/poseidon2/elf/riscv32im-succinct-zkvm-elf new file mode 100755 index 0000000..eb5740f Binary files /dev/null and b/hash/sp1/bench/poseidon2/elf/riscv32im-succinct-zkvm-elf differ diff --git a/hash/sp1/bench/poseidon2/src/main.rs b/hash/sp1/bench/poseidon2/src/main.rs new file mode 100644 index 0000000..9e5b56e --- /dev/null +++ b/hash/sp1/bench/poseidon2/src/main.rs @@ -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::>>(); + + //build input + let mut hash_data: Vec = 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 = Vec::new(); + hash_final.serialize_uncompressed(&mut hash_bytes).unwrap(); + + sp1_zkvm::io::write(&hash_bytes); + +} diff --git a/hash/sp1/bench/run.sh b/hash/sp1/bench/run.sh new file mode 100755 index 0000000..2cde35d --- /dev/null +++ b/hash/sp1/bench/run.sh @@ -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 \ No newline at end of file diff --git a/hash/sp1/bench/run_tree.sh b/hash/sp1/bench/run_tree.sh new file mode 100755 index 0000000..c8db6c6 --- /dev/null +++ b/hash/sp1/bench/run_tree.sh @@ -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 \ No newline at end of file diff --git a/hash/sp1/bench/script/Cargo.toml b/hash/sp1/bench/script/Cargo.toml new file mode 100644 index 0000000..f8cba27 --- /dev/null +++ b/hash/sp1/bench/script/Cargo.toml @@ -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" diff --git a/hash/sp1/bench/script/rust-toolchain b/hash/sp1/bench/script/rust-toolchain new file mode 100644 index 0000000..f7ee87e --- /dev/null +++ b/hash/sp1/bench/script/rust-toolchain @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly-2024-01-25" +components = ["llvm-tools", "rustc-dev"] \ No newline at end of file diff --git a/hash/sp1/bench/script/src/benches/blake2.rs b/hash/sp1/bench/script/src/benches/blake2.rs new file mode 100644 index 0000000..7b497f7 --- /dev/null +++ b/hash/sp1/bench/script/src/benches/blake2.rs @@ -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 { + let mut rng = rand::thread_rng(); + (0..length).map(|_| rng.gen::()).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); +} diff --git a/hash/sp1/bench/script/src/benches/blake3.rs b/hash/sp1/bench/script/src/benches/blake3.rs new file mode 100644 index 0000000..5c45776 --- /dev/null +++ b/hash/sp1/bench/script/src/benches/blake3.rs @@ -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 { + let mut rng = rand::thread_rng(); + (0..length).map(|_| rng.gen::()).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); +} diff --git a/hash/sp1/bench/script/src/benches/keccak.rs b/hash/sp1/bench/script/src/benches/keccak.rs new file mode 100644 index 0000000..2ede7ca --- /dev/null +++ b/hash/sp1/bench/script/src/benches/keccak.rs @@ -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 { + let mut rng = rand::thread_rng(); + (0..length).map(|_| rng.gen::()).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); +} diff --git a/hash/sp1/bench/script/src/benches/poseidon2.rs b/hash/sp1/bench/script/src/benches/poseidon2.rs new file mode 100644 index 0000000..7a831d1 --- /dev/null +++ b/hash/sp1/bench/script/src/benches/poseidon2.rs @@ -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::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::>(); + 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); +} diff --git a/hash/sp1/bench/script/src/benches/sha256.rs b/hash/sp1/bench/script/src/benches/sha256.rs new file mode 100644 index 0000000..39e53f0 --- /dev/null +++ b/hash/sp1/bench/script/src/benches/sha256.rs @@ -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 { + let mut rng = rand::thread_rng(); + (0..length).map(|_| rng.gen::()).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); +} diff --git a/hash/sp1/bench/script/src/main.rs b/hash/sp1/bench/script/src/main.rs new file mode 100644 index 0000000..3c4ded1 --- /dev/null +++ b/hash/sp1/bench/script/src/main.rs @@ -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 = std::env::args().collect(); + + if args.len() != 3 { + println!("Wrong number of arguments! The program expects two arguments: and "); + // Exit the program with a non-zero exit code + process::exit(1); + } + + let bench_type = &args[1]; + let size = args[2].parse::().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!"); + } + } +} diff --git a/hash/sp1/bench/sha256/Cargo.toml b/hash/sp1/bench/sha256/Cargo.toml new file mode 100644 index 0000000..81b1556 --- /dev/null +++ b/hash/sp1/bench/sha256/Cargo.toml @@ -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" } + diff --git a/hash/sp1/bench/sha256/elf/riscv32im-succinct-zkvm-elf b/hash/sp1/bench/sha256/elf/riscv32im-succinct-zkvm-elf new file mode 100755 index 0000000..c549870 Binary files /dev/null and b/hash/sp1/bench/sha256/elf/riscv32im-succinct-zkvm-elf differ diff --git a/hash/sp1/bench/sha256/src/main.rs b/hash/sp1/bench/sha256/src/main.rs new file mode 100644 index 0000000..e5223fd --- /dev/null +++ b/hash/sp1/bench/sha256/src/main.rs @@ -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::>(); + + // 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); +}