-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #78 Uses the risc0 emulation tools to step through the ELF and return the user cycles, total cycles and segment count.
- Loading branch information
1 parent
5832e47
commit 7bbbcd6
Showing
13 changed files
with
374 additions
and
5 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,34 @@ | ||
name: Integration Tests | ||
on: | ||
pull_request: | ||
branches: | ||
- '**' | ||
|
||
concurrency: | ||
group: "integration-tests" | ||
cancel-in-progress: true | ||
|
||
permissions: read-all | ||
|
||
jobs: | ||
integration-tests: | ||
name: Setup Toolchain and Test | ||
runs-on: ubuntu-latest-m | ||
permissions: | ||
id-token: "write" | ||
contents: "read" | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Install Nix With Bonsol Binary Cache | ||
uses: DeterminateSystems/nix-installer-action@main | ||
with: | ||
extra-conf: | | ||
extra-substituters = https://bonsol.cachix.org | ||
extra-trusted-public-keys = bonsol.cachix.org-1:yz7vi1rCPW1BpqoszdJvf08HZxQ/5gPTPxft4NnT74A= | ||
- name: Setup Toolchain, Build and Test | ||
run: | | ||
nix develop --command bash -c " | ||
cargo build && | ||
cargo test --features integration -- --nocapture | ||
" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,74 @@ | ||
//! Bare bones upper bound estimator that uses the rv32im | ||
//! emulation utils for fast lookups in the opcode list | ||
//! to extract the cycle count from an elf. | ||
use anyhow::Result; | ||
use risc0_binfmt::{MemoryImage, Program}; | ||
use risc0_zkvm::{ExecutorEnv, ExecutorImpl, Session, GUEST_MAX_MEM}; | ||
use risc0_zkvm_platform::PAGE_SIZE; | ||
|
||
pub fn estimate<E: MkImage>(elf: E, env: ExecutorEnv) -> Result<()> { | ||
let session = get_session(elf, env)?; | ||
println!( | ||
"User cycles: {}\nTotal cycles: {}\nSegments: {}", | ||
session.user_cycles, | ||
session.total_cycles, | ||
session.segments.len() | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Get the total number of cycles by stepping through the ELF using emulation | ||
/// tools from the risc0_circuit_rv32im module. | ||
pub fn get_session<E: MkImage>(elf: E, env: ExecutorEnv) -> Result<Session> { | ||
Ok(ExecutorImpl::new(env, elf.mk_image()?)?.run()?) | ||
} | ||
|
||
/// Helper trait for loading an image from an elf. | ||
pub trait MkImage { | ||
fn mk_image(self) -> Result<MemoryImage>; | ||
} | ||
impl<'a> MkImage for &'a [u8] { | ||
fn mk_image(self) -> Result<MemoryImage> { | ||
let program = Program::load_elf(self, GUEST_MAX_MEM as u32)?; | ||
MemoryImage::new(&program, PAGE_SIZE as u32) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod estimate_tests { | ||
use anyhow::Result; | ||
use risc0_binfmt::MemoryImage; | ||
use risc0_circuit_rv32im::prove::emu::{ | ||
exec::DEFAULT_SEGMENT_LIMIT_PO2, | ||
testutil::{basic as basic_test_program, DEFAULT_SESSION_LIMIT}, | ||
}; | ||
use risc0_zkvm::{ExecutorEnv, PAGE_SIZE}; | ||
|
||
use super::MkImage; | ||
use crate::estimate; | ||
|
||
impl MkImage for MemoryImage { | ||
fn mk_image(self) -> Result<MemoryImage> { | ||
Ok(self) | ||
} | ||
} | ||
|
||
#[test] | ||
fn estimate_basic() { | ||
let program = basic_test_program(); | ||
let mut env = &mut ExecutorEnv::builder(); | ||
env = env | ||
.segment_limit_po2(DEFAULT_SEGMENT_LIMIT_PO2 as u32) | ||
.session_limit(DEFAULT_SESSION_LIMIT); | ||
let image = MemoryImage::new(&program, PAGE_SIZE as u32) | ||
.expect("failed to create image from basic program"); | ||
let res = estimate::get_session(image, env.build().unwrap()); | ||
|
||
assert_eq!( | ||
res.ok().and_then(|session| Some(session.total_cycles)), | ||
Some(16384) | ||
); | ||
} | ||
} |
Oops, something went wrong.