sha256 and keccak benchmark

This commit is contained in:
Manish Kumar 2024-08-20 17:53:46 +05:30
parent ce25376b1d
commit 20210448a2
7 changed files with 145 additions and 16 deletions

View File

@ -473,7 +473,9 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
name = "bench"
version = "0.1.0"
dependencies = [
"ark-serialize 0.4.2",
"guest",
"hex",
"jolt-sdk",
"rand 0.8.5",
]

View File

@ -15,6 +15,8 @@ lto = "fat"
jolt = { package = "jolt-sdk", git = "https://github.com/a16z/jolt",rev = "4805d038f7b6e7ca5e7ef64aefa62213aecac01a", features = ["host"] }
guest = { path = "./guest" }
rand = "0.8.3"
hex = "0.4.3"
ark-serialize = "0.4.2"
[patch.crates-io]
ark-ff = { git = "https://github.com/a16z/arkworks-algebra", branch = "optimize/field-from-u64" }

View File

@ -1 +1,16 @@
./target/release/bench
#!/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
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 $ZKBENCH_HASH_TYPE $ZKBENCH_INPUT_SIZE_BYTES

View File

@ -0,0 +1,2 @@
pub mod sha2;
pub mod sha3;

View File

@ -0,0 +1,43 @@
use std::time::Instant;
use ark_serialize::CanonicalSerialize;
pub fn sha2_bench(input: Vec<u8>) {
let (prove_sha2, verify_sha2, guest_build_time) = {
let start = Instant::now();
let (prove, verify) = guest::build_sha2();
let elapsed = start.elapsed();
(prove, verify, elapsed)
};
let (output, proof, proving_time) = {
let start = Instant::now();
let (output, proof) = prove_sha2(input.as_slice());
let elapsed = start.elapsed();
(output, proof, elapsed)
};
let mut proof_bytes = Vec::new();
proof.serialize_compressed(&mut proof_bytes).unwrap();
let (is_valid, verification_time) = {
let start = Instant::now();
let is_valid = verify_sha2(proof);
let elapsed = start.elapsed();
(is_valid, elapsed)
};
assert!(is_valid);
println!("output: {:?}", hex::encode(output));
println!("guest build time: {:?}", guest_build_time);
println!("proving time: {:?}", proving_time);
println!("verification time: {:?}", verification_time);
println!("proof size: {:?} bytes", proof_bytes.len());
}

View File

@ -0,0 +1,42 @@
use std::time::Instant;
use ark_serialize::CanonicalSerialize;
pub fn sha3_bench(input: Vec<u8>) {
let (prove_sha3, verify_sha3, guest_build_time) = {
let start = Instant::now();
let (prove, verify) = guest::build_sha3();
let elapsed = start.elapsed();
(prove, verify, elapsed)
};
let (output, proof, proving_time) = {
let start = Instant::now();
let (output, proof) = prove_sha3(input.as_slice());
let elapsed = start.elapsed();
(output, proof, elapsed)
};
let mut proof_bytes = Vec::new();
proof.serialize_compressed(&mut proof_bytes).unwrap();
let (is_valid, verification_time) = {
let start = Instant::now();
let is_valid = verify_sha3(proof);
let elapsed = start.elapsed();
(is_valid, elapsed)
};
assert!(is_valid);
println!("output: {:?}", hex::encode(output));
println!("guest build time: {:?}", guest_build_time);
println!("proving time: {:?}", proving_time);
println!("verification time: {:?}", verification_time);
println!("proof size: {:?} bytes", proof_bytes.len());
}

View File

@ -1,4 +1,11 @@
use rand::Rng;
use std::process;
mod benches;
use benches::{
sha2::sha2_bench,
sha3::sha3_bench,
};
fn generate_bytes(size: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
@ -6,22 +13,38 @@ fn generate_bytes(size: usize) -> Vec<u8> {
}
pub fn main() {
let (prove_sha2, verify_sha2) = guest::build_sha2();
let (prove_sha3, verify_sha3) = guest::build_sha3();
// random data
let binding = generate_bytes(1024);
let input = binding.as_slice();
let args: Vec<String> = std::env::args().collect();
let (output, proof) = prove_sha2(input);
let is_valid = verify_sha2(proof);
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 hash_type = &args[1];
let size = args[2].parse::<usize>().unwrap();
println!("sha2 output: {:?}", output);
println!("sha2 valid: {}", is_valid);
let (output, proof) = prove_sha3(input);
let is_valid = verify_sha3(proof);
println!("sha3 output: {:?}", output);
println!("sha3 valid: {}", is_valid);
match hash_type.as_str() {
"sha256" => {
println!("SHA256 Benchmarking: ");
eprintln!("data size(bytes): {:?}", size);
let input = generate_bytes(size);
sha2_bench(input.clone());
}
"keccak" => {
println!("KECCAK Benchmarking: ");
eprintln!("data size(bytes): {:?}", size);
let input = generate_bytes(size);
sha3_bench(input.clone());
}
_ => {
println!("Wrong Benchmark Name!");
}
}
println!("All Done!");
}