openvm_sha256_guest/
lib.rs#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(target_os = "zkvm")]
use core::mem::MaybeUninit;
pub const OPCODE: u8 = 0x0b;
pub const SHA256_FUNCT3: u8 = 0b100;
pub const SHA256_FUNCT7: u8 = 0x1;
#[inline(always)]
pub fn sha256(input: &[u8]) -> [u8; 32] {
let mut output = [0u8; 32];
set_sha256(input, &mut output);
output
}
#[cfg(target_os = "zkvm")]
#[inline(always)]
#[no_mangle]
extern "C" fn zkvm_sha256_impl(bytes: *const u8, len: usize, output: *mut u8) {
openvm_platform::custom_insn_r!(opcode = OPCODE, funct3 = SHA256_FUNCT3, funct7 = SHA256_FUNCT7, rd = In output, rs1 = In bytes, rs2 = In len);
}
pub fn set_sha256(input: &[u8], output: &mut [u8; 32]) {
#[cfg(not(target_os = "zkvm"))]
{
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(input);
output.copy_from_slice(hasher.finalize().as_ref());
}
#[cfg(target_os = "zkvm")]
{
zkvm_sha256_impl(input.as_ptr(), input.len(), output.as_mut_ptr() as *mut u8);
}
}