sha256 and keccak benchmark

This commit is contained in:
Manish Kumar 2024-03-05 14:18:30 +05:30
parent 8f98119c3b
commit de7edef382
12 changed files with 212 additions and 0 deletions

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

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" }
sha3 = "0.10.8"

Binary file not shown.

View File

@ -0,0 +1,20 @@
#![no_main]
sp1_zkvm::entrypoint!(main);
use sha3::{Digest, Keccak256};
pub fn main() {
let input = sp1_zkvm::io::read::<Vec<u8>>();
// create a keccak object
let mut hasher = Keccak256::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,10 @@
[workspace]
[package]
version = "0.1.0"
name = "bench-script"
edition = "2021"
[dependencies]
sp1-core = { git = "https://github.com/succinctlabs/sp1.git" }
rand = "0.8"
hex = "0.4"

View File

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

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,43 @@
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,37 @@
pub mod benches{
pub mod keccak;
pub mod sha256;
}
use crate::benches::keccak::keccak_benchmark;
use crate::benches::sha256::sha256_benchmark;
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();
match bench_type.as_str() {
"sha256" => {
println!("Running sha256: ");
let _ = sha256_benchmark(size);
}
"keccak" => {
println!("Running keccak benchmark: ");
let _ = keccak_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" }
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);
}