initial commit for sha2 and sha3 hash benchmarking

This commit is contained in:
Manish Kumar 2024-08-20 13:01:03 +05:30
parent 882b2218f9
commit ce25376b1d
9 changed files with 3878 additions and 0 deletions

1
hash/jolt/bench/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

3784
hash/jolt/bench/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
[package]
name = "bench"
version = "0.1.0"
edition = "2021"
[workspace]
members = ["guest"]
[profile.release]
debug = 1
codegen-units = 1
lto = "fat"
[dependencies]
jolt = { package = "jolt-sdk", git = "https://github.com/a16z/jolt",rev = "4805d038f7b6e7ca5e7ef64aefa62213aecac01a", features = ["host"] }
guest = { path = "./guest" }
rand = "0.8.3"
[patch.crates-io]
ark-ff = { git = "https://github.com/a16z/arkworks-algebra", branch = "optimize/field-from-u64" }
ark-ec = { git = "https://github.com/a16z/arkworks-algebra", branch = "optimize/field-from-u64" }
ark-serialize = { git = "https://github.com/a16z/arkworks-algebra", branch = "optimize/field-from-u64" }

1
hash/jolt/bench/build.sh Executable file
View File

@ -0,0 +1 @@
cargo build --release

View File

@ -0,0 +1,16 @@
[package]
name = "guest"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "guest"
path = "./src/lib.rs"
[features]
guest = []
[dependencies]
jolt = { package = "jolt-sdk", git = "https://github.com/a16z/jolt", rev = "4805d038f7b6e7ca5e7ef64aefa62213aecac01a"}
sha2 = { version = "0.10.8", default-features = false }
sha3 = { version = "0.10.8", default-features = false }

View File

@ -0,0 +1,23 @@
#![cfg_attr(feature = "guest", no_std)]
#![no_main]
use sha2::{Sha256, Digest};
use sha3::Keccak256;
// sha256
#[jolt::provable]
fn sha2(input: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(input);
let result = hasher.finalize();
Into::<[u8; 32]>::into(result)
}
// keccak
#[jolt::provable]
fn sha3(input: &[u8]) -> [u8; 32] {
let mut hasher = Keccak256::new();
hasher.update(input);
let result = hasher.finalize();
Into::<[u8; 32]>::into(result)
}

1
hash/jolt/bench/run.sh Executable file
View File

@ -0,0 +1 @@
./target/release/bench

View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-08-01"
targets = ["riscv32im-unknown-none-elf"]

View File

@ -0,0 +1,27 @@
use rand::Rng;
fn generate_bytes(size: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..size).map(|_| rng.gen()).collect()
}
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 (output, proof) = prove_sha2(input);
let is_valid = verify_sha2(proof);
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);
}