openvm_keccak256/
lib.rs

1#![no_std]
2
3#[cfg(any(target_os = "zkvm", feature = "tiny_keccak"))]
4use openvm_keccak256_guest::KECCAK_OUTPUT_SIZE;
5
6#[cfg(all(not(target_os = "zkvm"), feature = "tiny_keccak"))]
7mod host_impl;
8#[cfg(target_os = "zkvm")]
9mod zkvm_impl;
10
11#[cfg(all(not(target_os = "zkvm"), feature = "tiny_keccak"))]
12pub use host_impl::{set_keccak256, Keccak256};
13#[cfg(target_os = "zkvm")]
14pub use zkvm_impl::{native_keccak256, set_keccak256, Keccak256};
15
16#[cfg(all(not(target_os = "zkvm"), feature = "tiny_keccak"))]
17impl tiny_keccak::Hasher for Keccak256 {
18    #[inline(always)]
19    fn update(&mut self, input: &[u8]) {
20        Keccak256::update(self, input);
21    }
22
23    #[inline(always)]
24    fn finalize(self, output: &mut [u8]) {
25        Keccak256::finalize(self, output);
26    }
27}
28
29/// Computes the keccak256 hash of the input.
30#[cfg(any(target_os = "zkvm", feature = "tiny_keccak"))]
31#[inline(always)]
32pub fn keccak256(input: &[u8]) -> [u8; KECCAK_OUTPUT_SIZE] {
33    let mut output = [0u8; KECCAK_OUTPUT_SIZE];
34    set_keccak256(input, &mut output);
35    output
36}