openvm_sha256_guest/
lib.rs
1#![no_std]
2
3pub const OPCODE: u8 = 0x0b;
5pub const SHA256_FUNCT3: u8 = 0b100;
6pub const SHA256_FUNCT7: u8 = 0x1;
7
8#[inline(always)]
10pub fn sha256(input: &[u8]) -> [u8; 32] {
11 let mut output = [0u8; 32];
12 set_sha256(input, &mut output);
13 output
14}
15
16#[cfg(target_os = "zkvm")]
26#[inline(always)]
27#[no_mangle]
28extern "C" fn zkvm_sha256_impl(bytes: *const u8, len: usize, output: *mut u8) {
29 openvm_platform::custom_insn_r!(opcode = OPCODE, funct3 = SHA256_FUNCT3, funct7 = SHA256_FUNCT7, rd = In output, rs1 = In bytes, rs2 = In len);
30}
31
32pub fn set_sha256(input: &[u8], output: &mut [u8; 32]) {
34 #[cfg(not(target_os = "zkvm"))]
35 {
36 use sha2::{Digest, Sha256};
37 let mut hasher = Sha256::new();
38 hasher.update(input);
39 output.copy_from_slice(hasher.finalize().as_ref());
40 }
41 #[cfg(target_os = "zkvm")]
42 {
43 zkvm_sha256_impl(input.as_ptr(), input.len(), output.as_mut_ptr() as *mut u8);
44 }
45}