-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathio.rs
74 lines (68 loc) · 2.08 KB
/
io.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
cfg_if::cfg_if! {
if #[cfg(target_os = "zkvm")] {
use core::arch::asm;
use crate::zkvm;
use sha2::digest::Update;
use sp1_primitives::consts::fd::FD_PUBLIC_VALUES;
}
}
/// Write `nbytes` of data to the prover to a given file descriptor `fd` from `write_buf`.
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn syscall_write(fd: u32, write_buf: *const u8, nbytes: usize) {
cfg_if::cfg_if! {
if #[cfg(target_os = "zkvm")] {
unsafe {
asm!(
"ecall",
in("t0") crate::syscalls::WRITE,
in("a0") fd,
in("a1") write_buf,
in("a2") nbytes,
);
}
// For writes to the public values fd, we update a global program hasher with the bytes
// being written. At the end of the program, we call the COMMIT ecall with the finalized
// version of this hash.
if fd == FD_PUBLIC_VALUES {
let pi_slice: &[u8] = unsafe { core::slice::from_raw_parts(write_buf, nbytes) };
unsafe { zkvm::PUBLIC_VALUES_HASHER.as_mut().unwrap().update(pi_slice) };
}
} else {
unreachable!()
}
}
}
/// Returns the length of the next element in the hint stream.
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn syscall_hint_len() -> usize {
#[cfg(target_os = "zkvm")]
unsafe {
let len;
asm!(
"ecall",
in("t0") crate::syscalls::HINT_LEN,
lateout("t0") len,
);
len
}
#[cfg(not(target_os = "zkvm"))]
unreachable!()
}
/// Reads the next element in the hint stream into the given buffer.
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn syscall_hint_read(ptr: *mut u8, len: usize) {
#[cfg(target_os = "zkvm")]
unsafe {
asm!(
"ecall",
in("t0") crate::syscalls::HINT_READ,
in("a0") ptr,
in("a1") len,
);
}
#[cfg(not(target_os = "zkvm"))]
unreachable!()
}