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