Skip to content

Keccak256

The Keccak256 extension guest provides functions that are meant to be linked to other external libraries. The external libraries can use these functions as hooks for the Keccak-256 intrinsics. This is enabled only when the target is zkvm.

We provide the following functions for the low-level Keccak sponge operations:

  • native_xorin(buffer: *mut u8, input: *const u8, len: usize): XORs len input bytes into a state buffer in-place. len is constrained in-circuit to be a non-negative multiple of 4 and at most 136.
  • native_keccakf(buffer: *mut u8): Applies the keccak-f[1600] permutation in-place on a 200-byte state buffer.

The openvm-keccak256 guest library builds on these to provide native_keccak256, a C ABI function that implements the full Keccak-256 hash:

  • native_keccak256(input: *const u8, len: usize, output: *mut u8): Computes the Keccak-256 hash of len bytes at input and writes the 32-byte digest to output.

In the external library, you can do the following:

extern "C" {
    fn native_keccak256(input: *const u8, len: usize, output: *mut u8);
}
 
fn keccak256(input: &[u8]) -> [u8; 32] {
    #[cfg(target_os = "zkvm")]
    {
        let mut output = [0u8; 32];
        unsafe {
            native_keccak256(input.as_ptr(), input.len(), output.as_mut_ptr() as *mut u8);
        }
        output
    }
    #[cfg(not(target_os = "zkvm"))] {
        // Regular Keccak-256 implementation
    }
}

Config parameters

For the guest program to build successfully add the following to your .toml file:

[app_vm_config.keccak]