openvm_sha2/
lib.rs

1#![no_std]
2
3/// The sha256 cryptographic hash function.
4#[inline(always)]
5pub fn sha256(input: &[u8]) -> [u8; 32] {
6    let mut output = [0u8; 32];
7    set_sha256(input, &mut output);
8    output
9}
10
11/// Sets `output` to the sha256 hash of `input`.
12pub fn set_sha256(input: &[u8], output: &mut [u8; 32]) {
13    #[cfg(not(target_os = "zkvm"))]
14    {
15        use sha2::{Digest, Sha256};
16        let mut hasher = Sha256::new();
17        hasher.update(input);
18        output.copy_from_slice(hasher.finalize().as_ref());
19    }
20    #[cfg(target_os = "zkvm")]
21    {
22        openvm_sha256_guest::zkvm_sha256_impl(
23            input.as_ptr(),
24            input.len(),
25            output.as_mut_ptr() as *mut u8,
26        );
27    }
28}