Skip to content

Commit

Permalink
Use platform crate in src files (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamonet authored Aug 28, 2023
1 parent 416c56f commit fb7a563
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 369 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ exclude = ["/tests"]
[lib]
crate-type = ["staticlib", "cdylib", "lib"]

[build-dependencies]
libcrux_platform = { version = "=0.0.1", path = "sys/platform" }

[dependencies]
hacl = { version = "=0.0.2", features = ["hazmat"] }
libcrux_platform = { version = "=0.0.1", path = "sys/platform" }
rand = { version = "0.8" }
log = "0.4"

Expand Down
60 changes: 6 additions & 54 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn simd128_support() -> bool {
std::arch::is_x86_feature_detected!("sse2")
&& std::arch::is_x86_feature_detected!("sse3")
&& std::arch::is_x86_feature_detected!("sse4.1")
&& std::arch::is_x86_feature_detected!("avx")
}

#[cfg(target_arch = "aarch64")]
fn simd128_support() -> bool {
true
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
fn simd128_support() -> bool {
// XXX: Check for z14 or z15
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn simd256_support() -> bool {
std::arch::is_x86_feature_detected!("avx2")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn simd256_support() -> bool {
// XXX: Check for z14 or z15
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn bmi2_adx_support() -> bool {
std::arch::is_x86_feature_detected!("bmi2") && std::arch::is_x86_feature_detected!("adx")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn bmi2_adx_support() -> bool {
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn aes_ni_support() -> bool {
// FIXME: std::arch::is_x86_feature_detected!("movbe") is not supported yet
// we assume here that it is supported :|
std::arch::is_x86_feature_detected!("avx")
&& std::arch::is_x86_feature_detected!("sse")
&& std::arch::is_x86_feature_detected!("aes")
&& std::arch::is_x86_feature_detected!("pclmulqdq")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn aes_ni_support() -> bool {
false
}
use libcrux_platform::{
simd128_support,
simd256_support,
bmi2_adx_support,
aes_ni_support
};

fn main() {
if simd128_support() {
Expand Down
2 changes: 1 addition & 1 deletion src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use hacl::hazmat;
use hacl::hazmat::chacha20_poly1305;

use crate::hw_detection::{aes_ni_support, simd128_support, simd256_support};
use libcrux_platform::{aes_ni_support, simd128_support, simd256_support};

/// The AEAD Errors.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hacl::hazmat::{
sha3,
};

use crate::hw_detection::{simd128_support, simd256_support};
use libcrux_platform::{simd128_support, simd256_support};

#[derive(Debug)]
pub enum Error {
Expand Down
4 changes: 2 additions & 2 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ pub(crate) mod x25519 {

#[cfg(all(bmi2, adx, target_arch = "x86_64"))]
pub(super) fn derive(p: &[u8; 32], s: &[u8; 32]) -> Result<[u8; 32], Error> {
use crate::hw_detection::x25519_cpu_support;
use libcrux_platform::x25519_support;
use hacl::hazmat::curve25519;
// On x64 we use vale if available or hacl as fallback.
// Jasmin exists but is not verified yet.

if x25519_cpu_support() {
if x25519_support() {
curve25519::vale::ecdh(s, p).map_err(|e| Error::Custom(format!("HACL Error {:?}", e)))
// XXX: not verified yet
// crate::jasmin::x25519::mulx::derive(s, p)
Expand Down
6 changes: 2 additions & 4 deletions src/hacl/chacha20poly1305.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use libcrux_hacl::*;

use crate::{
aead::*,
hw_detection::{simd128_support, simd256_support},
};
use crate::aead::*;
use libcrux_platform::{simd128_support, simd256_support};

#[cfg(simd256)]
fn encrypt_256(key: &Chacha20Key, msg_ctxt: &mut [u8], iv: &Iv, aad: &[u8]) -> Tag {
Expand Down
4 changes: 2 additions & 2 deletions src/hacl/curve25519.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use libcrux_hacl::*;

use crate::hw_detection::x25519_cpu_support;
use libcrux_platform::x25519_support;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn fast_x25519(result: &mut [u8], private: &[u8], public: &[u8]) -> bool {
Expand All @@ -19,7 +19,7 @@ fn fast_x25519(_: &mut [u8], _: &[u8], _: &[u8]) -> bool {

pub fn derive(p: &[u8], s: &[u8]) -> Result<[u8; 32], &'static str> {
let mut result = [0u8; 32];
let r = if x25519_cpu_support() {
let r = if x25519_support() {
log::trace!("HACL x25519 mulx");
fast_x25519(&mut result, s, p)
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/hacl/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::ptr::null_mut;

use libcrux_hacl::*;

use crate::{
digest::{digest_size, Algorithm},
hw_detection::{simd128_support, simd256_support},
};
use crate::digest::{digest_size, Algorithm};
use libcrux_platform::{simd128_support, simd256_support};

#[cfg(not(simd128))]
unsafe fn Hacl_Blake2s_128_blake2s(
Expand Down
77 changes: 0 additions & 77 deletions src/hw_detection.rs

This file was deleted.

139 changes: 0 additions & 139 deletions src/hw_detection/cpuid.rs

This file was deleted.

Loading

0 comments on commit fb7a563

Please sign in to comment.