arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/hash/rescue/arch/mod.rs
src/hash/rescue/arch/mod.rsRaw
  1#[cfg(target_feature = "sve")]
  2pub mod optimized {
  3    use crate::{hash::rescue::STATE_WIDTH, Felt};
  4
  5    mod ffi {
  6        #[link(name = "rpo_sve", kind = "static")]
  7        extern "C" {
  8            pub fn add_constants_and_apply_sbox(
  9                state: *mut std::ffi::c_ulong,
 10                constants: *const std::ffi::c_ulong,
 11            ) -> bool;
 12            pub fn add_constants_and_apply_inv_sbox(
 13                state: *mut std::ffi::c_ulong,
 14                constants: *const std::ffi::c_ulong,
 15            ) -> bool;
 16        }
 17    }
 18
 19    #[inline(always)]
 20    pub fn add_constants_and_apply_sbox(
 21        state: &mut [Felt; STATE_WIDTH],
 22        ark: &[Felt; STATE_WIDTH],
 23    ) -> bool {
 24        unsafe {
 25            ffi::add_constants_and_apply_sbox(
 26                state.as_mut_ptr() as *mut u64,
 27                ark.as_ptr() as *const u64,
 28            )
 29        }
 30    }
 31
 32    #[inline(always)]
 33    pub fn add_constants_and_apply_inv_sbox(
 34        state: &mut [Felt; STATE_WIDTH],
 35        ark: &[Felt; STATE_WIDTH],
 36    ) -> bool {
 37        unsafe {
 38            ffi::add_constants_and_apply_inv_sbox(
 39                state.as_mut_ptr() as *mut u64,
 40                ark.as_ptr() as *const u64,
 41            )
 42        }
 43    }
 44}
 45
 46#[cfg(target_feature = "avx2")]
 47mod x86_64_avx2;
 48
 49#[cfg(target_feature = "avx2")]
 50pub mod optimized {
 51    use super::x86_64_avx2::{apply_inv_sbox, apply_sbox};
 52    use crate::{
 53        hash::rescue::{add_constants, STATE_WIDTH},
 54        Felt,
 55    };
 56
 57    #[inline(always)]
 58    pub fn add_constants_and_apply_sbox(
 59        state: &mut [Felt; STATE_WIDTH],
 60        ark: &[Felt; STATE_WIDTH],
 61    ) -> bool {
 62        add_constants(state, ark);
 63        unsafe {
 64            apply_sbox(std::mem::transmute(state));
 65        }
 66        true
 67    }
 68
 69    #[inline(always)]
 70    pub fn add_constants_and_apply_inv_sbox(
 71        state: &mut [Felt; STATE_WIDTH],
 72        ark: &[Felt; STATE_WIDTH],
 73    ) -> bool {
 74        add_constants(state, ark);
 75        unsafe {
 76            apply_inv_sbox(std::mem::transmute(state));
 77        }
 78        true
 79    }
 80}
 81
 82#[cfg(not(any(target_feature = "avx2", target_feature = "sve")))]
 83pub mod optimized {
 84    use crate::{hash::rescue::STATE_WIDTH, Felt};
 85
 86    #[inline(always)]
 87    pub fn add_constants_and_apply_sbox(
 88        _state: &mut [Felt; STATE_WIDTH],
 89        _ark: &[Felt; STATE_WIDTH],
 90    ) -> bool {
 91        false
 92    }
 93
 94    #[inline(always)]
 95    pub fn add_constants_and_apply_inv_sbox(
 96        _state: &mut [Felt; STATE_WIDTH],
 97        _ark: &[Felt; STATE_WIDTH],
 98    ) -> bool {
 99        false
100    }
101}