-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Keccak precompile (#210)
* add runtime/emulator/prover/tests support for keccak syscall * Fix keccak result when length of data is zero * update test * fix: syscall precompiles * fix fmt and clippy * remove unused binding * fix keccak guest name --------- Co-authored-by: eigmax <stephen@ieigen.com>
- Loading branch information
Showing
26 changed files
with
640 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[workspace] | ||
[package] | ||
version = "0.1.0" | ||
name = "keccak-precompile" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
#zkm-runtime = { git = "https://github.com/zkMIPS/zkm", package = "zkm-runtime" } | ||
zkm-runtime = { path = "../../../../runtime/entrypoint" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate alloc; | ||
use alloc::vec::Vec; | ||
|
||
zkm_runtime::entrypoint!(main); | ||
|
||
pub fn main() { | ||
let public_input: Vec<u8> = zkm_runtime::io::read(); | ||
let input: Vec<u8> = zkm_runtime::io::read(); | ||
|
||
let output = zkm_runtime::io::keccak(&input.as_slice()); | ||
assert_eq!(output.to_vec(), public_input); | ||
zkm_runtime::io::commit::<[u8; 32]>(&output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "keccak-host" | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
publish = false | ||
|
||
[dependencies] | ||
zkm-prover = { workspace = true } | ||
zkm-emulator = { workspace = true } | ||
zkm-utils = { path = "../../utils" } | ||
|
||
|
||
log = { version = "0.4.14", default-features = false } | ||
serde = { version = "1.0.144", features = ["derive"] } | ||
serde_json = "1.0" | ||
byteorder = "1.5.0" | ||
hex = "0.4" | ||
env_logger = "0.11.5" | ||
anyhow = "1.0.75" | ||
|
||
[build-dependencies] | ||
zkm-build = { workspace = true } | ||
[features] | ||
test = ["zkm-prover/test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
zkm_build::build_program(&format!("{}/../guest", env!("CARGO_MANIFEST_DIR"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::env; | ||
|
||
use zkm_emulator::utils::{load_elf_with_patch, split_prog_into_segs}; | ||
use zkm_utils::utils::prove_segments; | ||
|
||
const ELF_PATH: &str = "../guest/elf/mips-zkm-zkvm-elf"; | ||
|
||
fn prove_keccak_rust() { | ||
// 1. split ELF into segs | ||
let seg_path = env::var("SEG_OUTPUT").expect("Segment output path is missing"); | ||
let seg_size = env::var("SEG_SIZE").unwrap_or("65536".to_string()); | ||
let seg_size = seg_size.parse::<_>().unwrap_or(0); | ||
|
||
let mut state = load_elf_with_patch(ELF_PATH, vec![]); | ||
// load input | ||
let args = env::var("ARGS").unwrap_or("data-to-hash".to_string()); | ||
// assume the first arg is the hash output(which is a public input), and the second is the input. | ||
let args: Vec<&str> = args.split_whitespace().collect(); | ||
assert!(args.len() >= 1); | ||
|
||
let public_input: Vec<u8> = hex::decode(args[0]).unwrap(); | ||
state.add_input_stream(&public_input); | ||
log::info!("expected public value in hex: {:X?}", args[0]); | ||
log::info!("expected public value: {:X?}", public_input); | ||
|
||
let private_input: Vec<u8> = if args.len() > 1 { | ||
hex::decode(args[1]).unwrap() | ||
} else { | ||
vec![] | ||
}; | ||
log::info!("private input value: {:X?}", private_input); | ||
state.add_input_stream(&private_input); | ||
|
||
let (_total_steps, seg_num, mut state) = split_prog_into_segs(state, &seg_path, "", seg_size); | ||
|
||
let value = state.read_public_values::<[u8; 32]>(); | ||
log::info!("public value: {:X?}", value); | ||
log::info!("public value: {} in hex", hex::encode(value)); | ||
|
||
let _ = prove_segments(&seg_path, "", "", "", seg_num, 0, vec![]).unwrap(); | ||
} | ||
|
||
fn main() { | ||
env_logger::try_init().unwrap_or_default(); | ||
prove_keccak_rust(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.