From 3e7462e04627f3f752f4299b00db4a59574e6084 Mon Sep 17 00:00:00 2001 From: John Guibas Date: Tue, 11 Jun 2024 15:27:21 -0700 Subject: [PATCH] feat: sys rand + hashmap random keys fix --- library/std/src/sys/zkvm/abi.rs | 2 +- library/std/src/sys/zkvm/mod.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/zkvm/abi.rs b/library/std/src/sys/zkvm/abi.rs index 0e5338ae35a5..bc365fce1943 100644 --- a/library/std/src/sys/zkvm/abi.rs +++ b/library/std/src/sys/zkvm/abi.rs @@ -34,7 +34,7 @@ extern "C" { buf: *const u8, count: u32, ); - pub fn sys_rand(recv_buf: *mut u32, words: usize); + pub fn sys_rand(recv_buf: *mut u8, words: usize); pub fn sys_panic(msg_ptr: *const u8, len: usize) -> !; pub fn sys_log(msg_ptr: *const u8, len: usize); pub fn sys_cycle_count() -> usize; diff --git a/library/std/src/sys/zkvm/mod.rs b/library/std/src/sys/zkvm/mod.rs index eb23608fd2f9..0f422e333a4f 100644 --- a/library/std/src/sys/zkvm/mod.rs +++ b/library/std/src/sys/zkvm/mod.rs @@ -85,5 +85,12 @@ pub fn abort_internal() -> ! { } pub fn hashmap_random_keys() -> (u64, u64) { - (1, 2) + let mut buf = [0u8; 16]; + unsafe { + abi::sys_rand(buf.as_mut_ptr(), buf.len()); + }; + + let a = u64::from_le_bytes(buf[0..8].try_into().unwrap()); + let b = u64::from_le_bytes(buf[8..16].try_into().unwrap()); + (a, b) }