-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5d6969
commit ac88d0d
Showing
20 changed files
with
511 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
Cargo.lock | ||
methods/guest/Cargo.lock | ||
target/ | ||
.vscode | ||
|
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,11 @@ | ||
[workspace] | ||
resolver = "2" | ||
members = ["host", "methods"] | ||
|
||
# Always optimize; building and running the guest takes much longer without optimization. | ||
[profile.dev] | ||
opt-level = 3 | ||
|
||
[profile.release] | ||
debug = 1 | ||
lto = true |
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,12 @@ | ||
[package] | ||
name = "host" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
memmap2 = "0.3" | ||
methods = { path = "../methods" } | ||
risc0-zkvm = { version = "0.21.0" } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
hex = "0.4" | ||
sha2 = "0.10" |
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,74 @@ | ||
use std::env; | ||
use memmap2::MmapOptions; | ||
use std::fs::{File}; | ||
use methods::{ | ||
TESTE1_ELF, TESTE1_ID | ||
}; | ||
use risc0_zkvm::{ | ||
default_prover, | ||
ExecutorEnv }; | ||
|
||
|
||
/* | ||
How to run this: | ||
1) create a step log | ||
cartesi-machine.lua --max-mcycle=0 --log-step=1,/tmp/step.bin | ||
Logging step of 1 cycles to /tmp/step.bin | ||
0: 5b4d6e46f7c024a1c108dcd2d7c174ec8ed2259a7ca53e362c611c2476791cb0 | ||
1: f66a12a3601c991025f7658226220f8346365f98958f5859a3add8954c1b1dd4 | ||
2) pass hashes, log file and mcycle_count to the host | ||
cargo run 5b4d6e46f7c024a1c108dcd2d7c174ec8ed2259a7ca53e362c611c2476791cb0 /tmp/step.bin 1 f66a12a3601c991025f7658226220f8346365f98958f5859a3add8954c1b1dd4 | ||
*/ | ||
fn main() { | ||
fn parse_hash(hex: &str) -> [u8; 32] { | ||
let bytes = hex::decode(hex).expect("Invalid hex string"); | ||
let mut array = [0; 32]; | ||
array.copy_from_slice(&bytes); | ||
array | ||
} | ||
|
||
let args: Vec<String> = env::args().collect(); | ||
if args.len() != 5 { | ||
eprintln!("Usage: {} <root_hash_before> <log_file_path> <mcycle_count> <root_hash_after>", args[0]); | ||
std::process::exit(1); | ||
} | ||
let root_hash_before = parse_hash(&args[1]); | ||
let log_file_path = &args[2]; | ||
let mcycle_count: u64 = args[3].parse().expect("Invalid step count"); | ||
let root_hash_after = parse_hash(&args[4]); | ||
assert_eq!(root_hash_before.len(), 32); | ||
assert_eq!(root_hash_after.len(), 32); | ||
|
||
// mmap the step log file | ||
let log_file = File::open(log_file_path).expect("Could not open log file"); | ||
let log_file_len = log_file.metadata().expect("Could not get metadata").len(); | ||
let log_file = unsafe { | ||
MmapOptions::new() | ||
.len(log_file_len as usize) | ||
.map(&log_file) | ||
.expect("Could not memory map log file") | ||
}; | ||
|
||
// build, run and verify the prover | ||
let mut builder = ExecutorEnv::builder(); | ||
builder.write(&mcycle_count).unwrap(); | ||
builder.write(&root_hash_before).unwrap(); | ||
builder.write(&root_hash_after).unwrap(); | ||
builder.write(&log_file_len).unwrap(); | ||
for i in (0..log_file_len).step_by(1) { | ||
builder.write(&log_file[i as usize]).unwrap(); | ||
} | ||
let env = builder.build().unwrap(); | ||
let prover = default_prover(); | ||
let receipt = prover | ||
.prove(env, TESTE1_ELF) | ||
.unwrap(); | ||
|
||
//todo result to be hashes and mcycle_count | ||
let result: bool = receipt.journal.decode().unwrap(); | ||
println!("host: result from guest: {:?}", result); | ||
receipt | ||
.verify(TESTE1_ID) | ||
.unwrap(); | ||
} |
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,10 @@ | ||
[package] | ||
name = "methods" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[build-dependencies] | ||
risc0-build = { version = "0.21.0" } | ||
|
||
[package.metadata.risc0] | ||
methods = ["guest"] |
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() { | ||
risc0_build::embed_methods(); | ||
} |
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,14 @@ | ||
[package] | ||
name = "teste1" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
# If you want to try (experimental) std support, add `features = [ "std" ]` to risc0-zkvm | ||
risc0-zkvm = { version = "0.21.0", default-features = true } | ||
tiny-keccak = { version = "2.0", features = ["keccak"] } | ||
|
||
[build-dependencies] | ||
cc = "1.0" |
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,14 @@ | ||
// fn main() { | ||
// cc::Build::new() | ||
// .object("../../../../zkarch/zkarch-replay-steps.o") | ||
// .compile("loxa"); | ||
// } | ||
|
||
fn main() { | ||
const OBJ_PATH: &str = "../../../../zkarch/zkarch-replay-steps.o"; | ||
|
||
println!("cargo:rerun-if-changed={}", OBJ_PATH); | ||
cc::Build::new() | ||
.object(OBJ_PATH) | ||
.compile("cartesi_zkarch_replay_steps"); | ||
} |
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,89 @@ | ||
#![no_main] | ||
// If you want to try std support, also update the guest Cargo.toml file | ||
//#![no_std] // std support is experimental | ||
|
||
use risc0_zkvm::guest::env; | ||
use std::ffi::{CStr}; | ||
risc0_zkvm::guest::entry!(main); | ||
use std::os::raw::{c_char, c_ulong, c_ulonglong}; | ||
use tiny_keccak::{Hasher, Keccak}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn interop_print(text: *const c_char) { | ||
let str = unsafe { CStr::from_ptr(text).to_string_lossy().into_owned() }; | ||
println!("print_from_c: {}", str); | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn interop_merkle_tree_hash(data: *const c_char, size: c_ulong, hash: *mut c_char) { | ||
let mut hasher = Keccak::v256(); | ||
if size > 32 { | ||
unsafe { | ||
let half_size = size / 2; | ||
let left_hash = [0u8; 32]; | ||
interop_merkle_tree_hash(data, half_size, left_hash.as_ptr() as *mut c_char); | ||
let right_hash = [0u8; 32]; | ||
interop_merkle_tree_hash(data.add(half_size as usize) as *const c_char, half_size, right_hash.as_ptr() as *mut c_char); | ||
hasher.update(left_hash.as_ref()); | ||
hasher.update(right_hash.as_ref()); | ||
let mut result_bytes = [0u8; 32]; | ||
hasher.finalize(&mut result_bytes); | ||
std::ptr::copy(result_bytes.as_ptr(), hash as *mut u8, 32); | ||
} | ||
} else{ | ||
let mut hasher = Keccak::v256(); | ||
hasher.update(unsafe { std::slice::from_raw_parts(data as *const u8, size as usize) }); | ||
let mut result_bytes = [0u8; 32]; | ||
hasher.finalize(&mut result_bytes); | ||
unsafe { | ||
std::ptr::copy(result_bytes.as_ptr(), hash as *mut u8, 32); | ||
} | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn interop_concat_hash(left: *const c_char, right: *const c_char, result: *mut c_char) { | ||
let mut hasher = Keccak::v256(); | ||
hasher.update(unsafe { std::slice::from_raw_parts(left as *const u8, 32) }); | ||
hasher.update(unsafe { std::slice::from_raw_parts(right as *const u8, 32) }); | ||
let mut result_bytes = [0u8; 32]; | ||
hasher.finalize(&mut result_bytes); | ||
unsafe { | ||
std::ptr::copy(result_bytes.as_ptr(), result as *mut u8, 32); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn interop_abort_with_msg(msg: *const c_char) { | ||
let str = unsafe { CStr::from_ptr(msg).to_string_lossy().into_owned() }; | ||
panic!("abort_with_msg: {}", str); | ||
} | ||
|
||
extern "C" { | ||
pub fn zkarch_replay_steps(root_hash_before: *const c_char, raw_log_data: *const c_char, raw_log_size: c_ulonglong, mcycle_count: c_ulonglong, root_hash_after: *const c_char) -> c_ulonglong; | ||
} | ||
|
||
|
||
fn main() { | ||
let mcycle_count: u64 = env::read(); | ||
let root_hash_before : [u8; 32] = env::read(); | ||
let root_hash_after : [u8; 32] = env::read(); | ||
let raw_log_length : u64 = env::read(); | ||
println!("guest: mcycle_count: {:?}", mcycle_count); | ||
println!("guest: root_hash_before: {:?}", root_hash_before); | ||
println!("guest: root_hash_after: {:?}", root_hash_after); | ||
println!("guest: raw_log_length: {:?}", raw_log_length); | ||
let mut raw_log: Vec<u8> = vec![0; raw_log_length as usize]; | ||
for i in (0..raw_log_length).step_by(1) { | ||
raw_log[i as usize] = env::read(); | ||
} | ||
println!("guest: before zkarch_replay_steps"); | ||
unsafe { | ||
zkarch_replay_steps(root_hash_before.as_ptr() as *const c_char, raw_log.as_ptr() as *const c_char, raw_log_length, mcycle_count, root_hash_after.as_ptr() as *const c_char); | ||
} | ||
println!("guest: after zkarch_replay_steps"); | ||
let result : bool = true; // TODO: result -> root_hash_before, mcycle_count and root_hash_after | ||
println!("guest: commiting"); | ||
env::commit(&result); | ||
println!("guest: committed"); | ||
} |
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 @@ | ||
include!(concat!(env!("OUT_DIR"), "/methods.rs")); |
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,4 @@ | ||
[toolchain] | ||
channel = "stable" | ||
components = ["rustfmt", "rust-src"] | ||
profile = "minimal" |
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,10 @@ | ||
*.o | ||
*.tmp | ||
*.ld | ||
*.elf | ||
*.bin | ||
.DS_Store | ||
.vscode | ||
uarch-pristine-hash.c | ||
uarch-pristine-ram.c | ||
compute-uarch-pristine-hash |
Oops, something went wrong.