Skip to content

Commit

Permalink
zkvm: add guest support for getrandom (risc0#445)
Browse files Browse the repository at this point in the history
This provides a zkvm with the getrandom functionality builtin to the guest
crate. This allows the guest to use the getrandom crate without any
customization.
  • Loading branch information
SchmErik authored Mar 17, 2023
1 parent a646ebe commit a34db96
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions risc0/zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protobuf-src = { version = "1.1", optional = true }
anyhow = { version = "1.0", default-features = false }
bytemuck = "1.12"
cfg-if = "1.0"
getrandom = { version = "0.2", features = ["custom"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
risc0-circuit-rv32im = { workspace = true }
risc0-core = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions risc0/zkvm/methods/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ risc0-zkvm = { path = "../..", default-features = false }
risc0-zkvm-methods = { path = "..", default-features = false }
risc0-zkvm-platform = { path = "../../platform" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
getrandom = "0.2"

[profile.release]
lto = true
Expand Down
18 changes: 7 additions & 11 deletions risc0/zkvm/methods/guest/src/bin/multi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ extern crate alloc;
use alloc::vec;
use core::arch::asm;

use getrandom::getrandom;
use risc0_zeroio::deserialize::Deserialize;
use risc0_zkp::core::sha::{testutil::test_sha_impl, Digest, Sha256};
use risc0_zkvm::guest::{env, memory_barrier, sha};
use risc0_zkvm_methods::multi_test::{MultiTestSpec, MultiTestSpecRef, SYS_MULTI_TEST};
use risc0_zkvm_platform::syscall::{nr::SYS_INITIAL_INPUT, sys_rand, sys_read};
use risc0_zkvm_platform::syscall::{nr::SYS_INITIAL_INPUT, sys_read};

risc0_zkvm::entry!(main);

Expand Down Expand Up @@ -117,16 +118,11 @@ pub fn main() {
}
}
MultiTestSpecRef::DoRandom(_) => {
// TODO: replace this code with getrandom after merging code to getrandom crate
let mut buf = [0u32; 6];
unsafe {
sys_rand(buf.as_mut_ptr(), buf.len());
}
let mut result_buf = vec![0u8; 5];
result_buf.clone_from_slice(&bytemuck::cast_slice(buf.as_slice())[..5]);
assert_ne!(result_buf, vec![0u8; result_buf.len()]);

env::commit_slice(&result_buf);
// Test random number generation in the zkvm
let mut rand_buf = [0u8; 7];
getrandom(rand_buf.as_mut_slice()).expect("random number generation failed");
env::commit_slice(&rand_buf);
assert_ne!(rand_buf, vec![0u8; rand_buf.len()].as_slice());
}
MultiTestSpecRef::SysRead(sysread) => {
let mut orig = sysread.orig().to_vec();
Expand Down
26 changes: 25 additions & 1 deletion risc0/zkvm/src/guest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,34 @@ pub mod sha;

use core::{arch::asm, mem, ptr};

use risc0_zkvm_platform::{syscall::nr::SYS_PANIC, syscall::sys_panic};
use getrandom::register_custom_getrandom;
use getrandom::Error;
use risc0_zkvm_platform::WORD_SIZE;
use risc0_zkvm_platform::{
syscall::nr::SYS_PANIC,
syscall::{sys_panic, sys_rand},
};

pub use crate::entry;

/// This is a getrandom handler for the zkvm. It's intended to hook into a
/// getrandom crate or a depdent of the getrandom crate used by the guest code.
pub fn zkvm_getrandom(dest: &mut [u8]) -> Result<(), Error> {
if dest.is_empty() {
return Ok(());
}

let words = (dest.len() + WORD_SIZE - 1) / WORD_SIZE;
let mut buf = ::alloc::vec![0u32; words];
unsafe {
sys_rand(buf.as_mut_ptr(), words);
}
dest.clone_from_slice(&bytemuck::cast_slice(buf.as_slice())[..dest.len()]);
Ok(())
}

register_custom_getrandom!(zkvm_getrandom);

#[cfg(target_os = "zkvm")]
core::arch::global_asm!(include_str!("memset.s"));
#[cfg(target_os = "zkvm")]
Expand Down

0 comments on commit a34db96

Please sign in to comment.