Skip to content

Commit

Permalink
Drop simd256 cfg from SHA3 crate (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer authored Apr 29, 2024
1 parent 2eae32e commit bc488f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions libcrux-ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ hax-lib = { git = "https://github.com/hacspec/hax/" }

[features]
default = []
simd128 = []
simd256 = []
simd128 = ["libcrux-sha3/simd128"]
simd256 = ["libcrux-sha3/simd256"]

[dev-dependencies]
rand = { version = "0.8" }
Expand Down
42 changes: 21 additions & 21 deletions libcrux-sha3/src/x4/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ use libcrux_hacl::{
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks, Hacl_Hash_SHA3_Scalar_state_free,
Hacl_Hash_SHA3_Scalar_state_malloc,
};
#[cfg(simd256)]
#[cfg(feature = "simd256")]
use libcrux_hacl::{
Hacl_Hash_SHA3_Simd256_shake128_absorb_final, Hacl_Hash_SHA3_Simd256_shake128_absorb_nblocks,
Hacl_Hash_SHA3_Simd256_shake128_squeeze_nblocks, Hacl_Hash_SHA3_Simd256_state_free,
Hacl_Hash_SHA3_Simd256_state_malloc, Lib_IntVector_Intrinsics_vec256,
};
#[cfg(simd256)]
#[cfg(feature = "simd256")]
use libcrux_platform::simd256_support;

/// SHAKE 128
///
/// Handle to internal SHAKE 128 state
#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub struct Shake128StateX4 {
statex4: *mut Lib_IntVector_Intrinsics_vec256,
state: [*mut u64; 4],
}

#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub struct Shake128StateX4 {
state: [*mut u64; 4],
}

impl Shake128StateX4 {
#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub fn new() -> Self {
if cfg!(simd256) && simd256_support() {
if simd256_support() {
Self {
statex4: unsafe { Hacl_Hash_SHA3_Simd256_state_malloc() },
state: [null_mut(), null_mut(), null_mut(), null_mut()],
Expand All @@ -51,7 +51,7 @@ impl Shake128StateX4 {
}
}

#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub fn new() -> Self {
Self {
state: unsafe {
Expand All @@ -68,9 +68,9 @@ impl Shake128StateX4 {
/// Free and consume the state.
///
/// **NOTE:** This consumes the value. It is not usable after this call!
#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub fn free(mut self) {
if cfg!(simd256) && simd256_support() {
if simd256_support() {
unsafe {
Hacl_Hash_SHA3_Simd256_state_free(self.statex4);
// null the pointer (hacl isn't doing that unfortunately)
Expand All @@ -92,7 +92,7 @@ impl Shake128StateX4 {
/// Free and consume the state.
///
/// **NOTE:** This consumes the value. It is not usable after this call!
#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub fn free(mut self) {
for i in 0..4 {
unsafe {
Expand All @@ -109,7 +109,7 @@ impl Shake128StateX4 {
/// The input length must be a multiple of the SHA3 block length of 168.
///
/// The input is truncated at `u32::MAX`.
#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub fn absorb_blocks(&mut self, input: [&[u8]; 4]) {
debug_assert!(
(input[0].len() == input[1].len() || input[1].len() == 0)
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Shake128StateX4 {
/// The input length must be a multiple of the SHA3 block length of 168.
///
/// The input is truncated at `u32::MAX`.
#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub fn absorb_blocks(&mut self, input: [&[u8]; 4]) {
debug_assert!(
(input[0].len() == input[1].len() || input[1].len() == 0)
Expand All @@ -176,7 +176,7 @@ impl Shake128StateX4 {
/// The input length must be a multiple of the SHA3 block length of 168.
///
/// The input is truncated at `u32::MAX`.
#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub fn absorb_final(&mut self, input: [&[u8]; 4]) {
debug_assert!(
(input[0].len() == input[1].len() || input[1].len() == 0)
Expand All @@ -185,7 +185,7 @@ impl Shake128StateX4 {
);
debug_assert!(input[0].len() < 168);

if cfg!(simd256) && simd256_support() {
if simd256_support() {
unsafe {
Hacl_Hash_SHA3_Simd256_shake128_absorb_final(
self.statex4,
Expand Down Expand Up @@ -216,7 +216,7 @@ impl Shake128StateX4 {
/// The input length must be a multiple of the SHA3 block length of 168.
///
/// The input is truncated at `u32::MAX`.
#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub fn absorb_final(&mut self, input: [&[u8]; 4]) {
debug_assert!(
(input[0].len() == input[1].len() || input[1].len() == 0)
Expand All @@ -238,14 +238,14 @@ impl Shake128StateX4 {
}
}

#[cfg(simd256)]
#[cfg(feature = "simd256")]
pub fn squeeze_blocks<const OUTPUT_BYTES: usize, const M: usize>(
&mut self,
) -> [[u8; OUTPUT_BYTES]; M] {
debug_assert!(OUTPUT_BYTES % 168 == 0);
debug_assert!(M <= self.state.len() && (M == 2 || M == 3 || M == 4));

if cfg!(simd256) && simd256_support() {
if simd256_support() {
let mut output = [[0u8; OUTPUT_BYTES]; 4];
unsafe {
Hacl_Hash_SHA3_Simd256_shake128_squeeze_nblocks(
Expand Down Expand Up @@ -273,7 +273,7 @@ impl Shake128StateX4 {
}
}

#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
pub fn squeeze_blocks<const OUTPUT_BYTES: usize, const M: usize>(
&mut self,
) -> [[u8; OUTPUT_BYTES]; M] {
Expand All @@ -299,9 +299,9 @@ impl Shake128StateX4 {
/// **NOTE:** When generating C code with Eurydice, the state needs to be freed
/// manually for now due to a bug in Eurydice.
impl Drop for Shake128StateX4 {
#[cfg(simd256)]
#[cfg(feature = "simd256")]
fn drop(&mut self) {
if cfg!(simd256) && simd256_support() {
if simd256_support() {
// A manual free may have occurred already.
// Avoid double free.
unsafe {
Expand All @@ -322,7 +322,7 @@ impl Drop for Shake128StateX4 {
}
}

#[cfg(not(simd256))]
#[cfg(not(feature = "simd256"))]
fn drop(&mut self) {
// A manual free may have occurred already.
// Avoid double free.
Expand Down

0 comments on commit bc488f3

Please sign in to comment.