diff --git a/crates/zkevm-zisk/.gitignore b/crates/zkevm-zisk/.gitignore new file mode 100644 index 00000000..168da4f7 --- /dev/null +++ b/crates/zkevm-zisk/.gitignore @@ -0,0 +1,3 @@ +build +target +Cargo.lock \ No newline at end of file diff --git a/crates/zkevm-zisk/Cargo.toml b/crates/zkevm-zisk/Cargo.toml new file mode 100644 index 00000000..1bfcd482 --- /dev/null +++ b/crates/zkevm-zisk/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "sha_hasher" +version = "0.1.0" +edition = "2021" +default-run = "sha_hasher" + +[dependencies] +byteorder = "1.5.0" +sha2 = "0.10.8" +ziskos = { git = "https://github.com/0xPolygonHermez/zisk.git" } + +# reth-ethereum-primitives = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" } +# reth-primitives-traits = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" } +# reth-stateless = { git = "https://github.com/kevaundray/reth", rev = "96de1f325d8835d589bd505b9f94507904f20963" } + diff --git a/crates/zkevm-zisk/build.rs b/crates/zkevm-zisk/build.rs new file mode 100644 index 00000000..15bdd4cb --- /dev/null +++ b/crates/zkevm-zisk/build.rs @@ -0,0 +1,25 @@ +use std::fs::{self, File}; +use std::io::{self, Write}; +use std::path::Path; + +// Define constants for the directory and input file name +const OUTPUT_DIR: &str = "build/"; +const FILE_NAME: &str = "input.bin"; + +fn main() -> io::Result<()> { + let n: u64 = 20; + + // Ensure the output directory exists + let output_dir = Path::new(OUTPUT_DIR); + if !output_dir.exists() { + // Create the directory and any necessary parent directories + fs::create_dir_all(output_dir)?; + } + + // Create the file and write the 'n' value in little-endian format + let file_path = output_dir.join(FILE_NAME); + let mut file = File::create(&file_path)?; + file.write_all(&n.to_le_bytes())?; + + Ok(()) +} diff --git a/crates/zkevm-zisk/file.patch b/crates/zkevm-zisk/file.patch new file mode 100644 index 00000000..e69de29b diff --git a/crates/zkevm-zisk/src/main.rs b/crates/zkevm-zisk/src/main.rs new file mode 100644 index 00000000..3ea80254 --- /dev/null +++ b/crates/zkevm-zisk/src/main.rs @@ -0,0 +1,40 @@ +// This example program takes a number `n` as input and computes the SHA-256 hash `n` times sequentially. + +// Mark the main function as the entry point for ZisK +#![no_main] +ziskos::entrypoint!(main); + +use byteorder::ByteOrder; +use sha2::{Digest, Sha256}; +use std::convert::TryInto; +use ziskos::{read_input, set_output}; + +fn main() { + // Read the input data as a byte array from ziskos + let input: Vec = read_input(); + + // Convert the input data to a u64 integer + let n: u64 = match input.try_into() { + Ok(input_bytes) => u64::from_le_bytes(input_bytes), + Err(input) => panic!( + "Invalid input length. Expected 8 bytes, got {}", + input.len() + ), + }; + + let mut hash = [0u8; 32]; + + // Compute SHA-256 hashing 'n' times + for _ in 0..n { + let mut hasher = Sha256::new(); + hasher.update(hash); + let digest = &hasher.finalize(); + hash = Into::<[u8; 32]>::into(*digest); + } + + // Split 'hash' value into chunks of 32 bits and write them to ziskos output + for i in 0..8 { + let val = byteorder::BigEndian::read_u32(&mut hash[i * 4..i * 4 + 4]); + set_output(i, val); + } +}