-
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
c161181
commit 95afe40
Showing
37 changed files
with
2,357 additions
and
11 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
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,11 @@ | ||
[package] | ||
name = "host" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
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,83 @@ | ||
use std::fs::{self, File}; | ||
use std::io::Read; | ||
use methods::{ | ||
TESTE1_ELF, TESTE1_ID | ||
}; | ||
use risc0_zkvm::{ | ||
default_prover, | ||
ExecutorEnv }; | ||
|
||
pub struct PageInfo { | ||
pub address: u64, | ||
pub data: [u8; 4986], | ||
} | ||
|
||
fn load_pages(dir_path: &str) -> Vec<PageInfo> { | ||
let mut files = fs::read_dir(dir_path) | ||
.expect("Could not read directory") | ||
.filter_map(|entry| { | ||
let entry = entry.ok()?; | ||
let path = entry.path(); | ||
if path.is_file() { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
files.sort_by_key(|path| { | ||
u64::from_str_radix(path.file_name().unwrap().to_str().unwrap(), 16).unwrap() | ||
}); | ||
let mut pages = Vec::new(); | ||
for file_path in files { | ||
let filename = file_path.file_name().unwrap().to_str().unwrap(); | ||
let address = u64::from_str_radix(filename, 16).unwrap(); | ||
let mut file = File::open(&file_path).expect("Could not open file"); | ||
let mut data = [0 as u8; 4986]; | ||
// read file contents and push a new page info | ||
file.read(&mut data).expect("Could not read file contents"); | ||
pages.push(PageInfo { | ||
address, | ||
data, | ||
}); | ||
} | ||
pages | ||
} | ||
|
||
// Page files generated by the following command: | ||
// mkdir -p /tmp/loxa3 | ||
// cartesi-machine.lua --max-mcycle=0 --log-steps=1,/tmp/loxa3 | ||
fn main() { | ||
let pages = load_pages("/tmp/loxa3"); | ||
// Initialize tracing. In order to view logs, run `RUST_LOG=info cargo run` | ||
tracing_subscriber::fmt() | ||
.with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) | ||
.init(); | ||
|
||
let mut builder = ExecutorEnv::builder(); | ||
// stupid way to send pages to the guest. Will improve later. | ||
builder.write(&pages.len()).unwrap(); | ||
let mut temp: [u8; 32] = [0; 32]; | ||
for page in pages { | ||
builder.write(&page.address).unwrap(); | ||
println!("writing page: {:x}", page.address); | ||
for i in (0..4096).step_by(32) { | ||
temp.copy_from_slice(&page.data[i..(i + 32)]); | ||
builder.write(&temp).unwrap(); | ||
} | ||
} | ||
|
||
let env = builder.build().unwrap(); | ||
|
||
let prover = default_prover(); | ||
let receipt = prover | ||
.prove(env, TESTE1_ELF) | ||
.unwrap(); | ||
|
||
let output:u64 = receipt.journal.decode().unwrap(); | ||
println!("result: {:x}", output); | ||
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,13 @@ | ||
[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 } | ||
|
||
[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,5 @@ | ||
fn main() { | ||
cc::Build::new() | ||
.object("../../../../zkarch/zkarch-replay-steps.o") | ||
.compile("loxa"); | ||
} |
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,57 @@ | ||
#![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; | ||
risc0_zkvm::guest::entry!(main); | ||
use std::ptr; | ||
use std::os::raw::{c_char, c_ulonglong}; | ||
|
||
#[repr(C)] | ||
pub struct PageInfo { | ||
pub address: c_ulonglong, | ||
pub data: [c_char; 4096], | ||
pub next: *mut PageInfo, | ||
} | ||
|
||
extern "C" { | ||
pub fn zkarch_replay_steps(steps: c_ulonglong, pages: *mut PageInfo) -> c_ulonglong; | ||
} | ||
|
||
fn main() { | ||
// linked list of pages | ||
let mut _head : *mut PageInfo = ptr::null_mut(); | ||
let mut _current : *mut PageInfo = ptr::null_mut(); | ||
// read page count | ||
let _page_count : u64 = env::read(); | ||
// stupid way of reading pages. Will improve later. | ||
for _ in 0.._page_count { | ||
let _asdress : u64 = env::read(); | ||
let mut _page = PageInfo { | ||
address: _asdress, | ||
data: [0; 4096], | ||
next: ptr::null_mut(), | ||
}; | ||
// even stupider way of reading page data. Will improve later. | ||
for _i in (0..4096).step_by(32) { | ||
env::read_slice(&mut _page.data[_i..(_i + 32)]); | ||
} | ||
if _head.is_null() { | ||
_head = Box::into_raw(Box::new(_page)); | ||
_current = _head; | ||
} else { | ||
let _new_page = Box::into_raw(Box::new(_page)); | ||
unsafe { | ||
(*_current).next = _new_page; | ||
_current = _new_page; | ||
} | ||
} | ||
} | ||
|
||
let mctcle_end: u64; | ||
unsafe { | ||
mctcle_end = zkarch_replay_steps(1, _head); | ||
} | ||
|
||
env::commit(&_page_count); | ||
} |
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
Oops, something went wrong.