ziskos 1.1.0-alpha

Guest runtime and entrypoint for programs targeting the ZisK zkVM
//! Keccak system call interception

#[cfg(zisk_guest)]
use core::arch::asm;

#[cfg(zisk_guest)]
use crate::ziskos_syscall;

#[cfg(not(zisk_guest))]
use tiny_keccak::keccakf;

/// Executes the Keccak256 permutation on the given state.
///
/// The Keccak256 permutation operates on an array of twenty-five `u64` elements, which represents the internal state of the Keccak algorithm.
/// The input state is modified in place to produce the output.
///
/// ### Safety
///
/// The caller must ensure that the data is aligned to a 64-bit boundary.
#[allow(unused_variables)]
#[cfg_attr(not(feature = "hints"), no_mangle)]
#[cfg_attr(feature = "hints", export_name = "hints_syscall_keccak_f")]
pub unsafe extern "C" fn syscall_keccak_f(
    state: *mut [u64; 25],
    #[cfg(feature = "hints")] hints: &mut Vec<u64>,
) {
    #[cfg(zisk_guest)]
    ziskos_syscall!(zisk_definitions::SYSCALL_KECCAKF_ID, state);
    #[cfg(not(zisk_guest))]
    {
        // Register the input state if fcall_set_keccakf_cache_index() asked for it, mirroring
        // what the emulator does before running the permutation
        crate::zisklib::keccakf_cache::keccakf_cache_on_keccakf(unsafe { &*state });

        // Call keccakf
        keccakf(unsafe { &mut *state });

        // Store results in hints vector
        #[cfg(feature = "hints")]
        if zisk_definitions::KECCAK_RESULTS {
            hints.extend_from_slice(unsafe { &*state });
        }
    }
}