Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/examples/hello-world/methods/guest/Cargo.toml b/examples/hello-world/methods/guest/Cargo.toml
- index 11940b93..a3c1d315 100644
- --- a/examples/hello-world/methods/guest/Cargo.toml
- +++ b/examples/hello-world/methods/guest/Cargo.toml
- @@ -7,3 +7,4 @@ edition = "2021"
- [dependencies]
- risc0-zkvm = { path = "../../../../risc0/zkvm", default-features = false }
- +nes = { git = "https://github.com/koute/pinky.git", rev = "5a9d4ff5f22731191cffd7d6202a1d144aacd740", default-features = false, features = ["softfloat"] }
- diff --git a/examples/hello-world/methods/guest/src/main.rs b/examples/hello-world/methods/guest/src/main.rs
- index 2ddce6e2..43682564 100644
- --- a/examples/hello-world/methods/guest/src/main.rs
- +++ b/examples/hello-world/methods/guest/src/main.rs
- @@ -15,20 +15,102 @@
- #![no_main]
- #![no_std]
- +extern crate alloc;
- +use alloc::boxed::Box;
- +
- use risc0_zkvm::guest::env;
- risc0_zkvm::guest::entry!(main);
- +use nes::Interface;
- +
- +struct Emulator {
- + state: nes::State,
- +}
- +
- +impl nes::Context for Emulator {
- + fn state(&self) -> &nes::State {
- + &self.state
- + }
- +
- + fn state_mut(&mut self) -> &mut nes::State {
- + &mut self.state
- + }
- +}
- +
- +// Source of the ROM: https://github.com/christopherpow/nes-test-roms/tree/97720008e51db15dd281a2a1e64d4c65cf1bca4c/nes15-1.0.0
- +// Licensed under a BSD-style license.
- +const ROM: &[u8] = core::include_bytes!("nes15-NTSC.nes");
- +
- +struct State {
- + nes: Emulator,
- + framebuffer: Option<Box<[u32; 256 * 240]>>,
- +}
- +
- +#[inline]
- +unsafe fn alloc_zeroed_box<T>() -> Box<T> {
- + Box::from_raw(alloc::alloc::alloc_zeroed(alloc::alloc::Layout::new::<T>()) as *mut T)
- +}
- +
- +fn benchmark_initialize(state: &mut State) {
- + state.framebuffer = Some(unsafe { alloc_zeroed_box() });
- + state.nes.load_rom(ROM).unwrap();
- +
- + // By default the game which we're emulating stays on the main menu
- + // screen without anything happening on screen; it doesn't even try
- + // to play any music.
- + //
- + // Pressing any button on the gamepad actually starts the game, and
- + // once the game is started pressing the select button triggers
- + // an autosolve mechanism which makes the game automatically play itself.
- + //
- + // So let's trigger this here.
- +
- + // We need to wait for four frames until the game accepts any input.
- + //
- + // I don't know why; I just started to empirically increase the number
- + // of frames until it worked.
- + for _ in 0..4 {
- + state.nes.execute_until_vblank().unwrap();
- + }
- +
- + // Now we can press a button to start the game.
- + state.nes.press(nes::ControllerPort::First, nes::Button::Select);
- + state.nes.execute_until_vblank().unwrap();
- + state.nes.release(nes::ControllerPort::First, nes::Button::Select);
- +
- + // We need to wait for at least three frames until we can trigger
- + // the autosolve mechanism.
- + for _ in 0..3 {
- + state.nes.execute_until_vblank().unwrap();
- + }
- +
- + // Now we can press select to make the game start playing itself.
- + state.nes.press(nes::ControllerPort::First, nes::Button::Select);
- + state.nes.execute_until_vblank().unwrap();
- + state.nes.release(nes::ControllerPort::First, nes::Button::Select);
- +}
- +
- +fn benchmark_run(state: &mut State) {
- + state.nes.execute_until_vblank().unwrap();
- + state
- + .nes
- + .framebuffer()
- + .convert_to_abgr(&nes::Palette::default(), &mut state.framebuffer.as_mut().unwrap()[..]);
- +}
- +
- pub fn main() {
- - // Load the first number from the host
- - let a: u64 = env::read();
- - // Load the second number from the host
- - let b: u64 = env::read();
- - // Verify that neither of them are 1 (i.e. nontrivial factors)
- - if a == 1 || b == 1 {
- - panic!("Trivial factors")
- + let mut state = State {
- + nes: Emulator { state: nes::State::new() },
- + framebuffer: None,
- + };
- +
- + benchmark_initialize(&mut state);
- + benchmark_run(&mut state);
- +
- + let mut sum: u32 = 0;
- + for &value in state.framebuffer.as_ref().unwrap()[..].iter() {
- + sum = sum.wrapping_add(value);
- }
- - // Compute the product while being careful with integer overflow
- - let product = a.checked_mul(b).expect("Integer overflow");
- - env::commit(&product);
- + env::commit(&sum);
- }
- diff --git a/examples/hello-world/src/lib.rs b/examples/hello-world/src/lib.rs
- index 35424f83..3931d87c 100644
- --- a/examples/hello-world/src/lib.rs
- +++ b/examples/hello-world/src/lib.rs
- @@ -22,13 +22,9 @@ use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
- // The factors a and b are kept secret.
- // Compute the product a*b inside the zkVM
- -pub fn multiply(a: u64, b: u64) -> (Receipt, u64) {
- +pub fn multiply() -> (Receipt, u32) {
- let env = ExecutorEnv::builder()
- // Send a & b to the guest
- - .write(&a)
- - .unwrap()
- - .write(&b)
- - .unwrap()
- .build()
- .unwrap();
- @@ -39,7 +35,7 @@ pub fn multiply(a: u64, b: u64) -> (Receipt, u64) {
- let receipt = prover.prove_elf(env, MULTIPLY_ELF).unwrap();
- // Extract journal of receipt (i.e. output c, where c = a * b)
- - let c: u64 = receipt.journal.decode().expect(
- + let c: u32 = receipt.journal.decode().expect(
- "Journal output should deserialize into the same types (& order) that it was written",
- );
- diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs
- index b66b131c..ee7b4381 100644
- --- a/examples/hello-world/src/main.rs
- +++ b/examples/hello-world/src/main.rs
- @@ -15,14 +15,23 @@
- use hello_world::multiply;
- use hello_world_methods::MULTIPLY_ID;
- +use std::time::Instant;
- +
- fn main() {
- + let start = Instant::now();
- // Pick two numbers
- - let (receipt, _) = multiply(17, 23);
- + let (receipt, _) = multiply();
- +
- + let elapsed = start.elapsed();
- + println!("Finished in: {}", elapsed.as_secs_f64() * 1000.0);
- // Here is where one would send 'receipt' over the network...
- + let start = Instant::now();
- // Verify receipt, panic if it's wrong
- receipt.verify(MULTIPLY_ID).expect(
- "Code you have proven should successfully verify; did you specify the correct image ID?",
- );
- + let elapsed = start.elapsed();
- + println!("Verified in: {}", elapsed.as_secs_f64() * 1000.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment