AVX code: move stub into separate file; always check -mavx2/-mfma

This simplifies the build a bit by moving the avx stubs into a single .c
file: we can avoid compiling all of the avx2 code and just add a single
stub file instead when the compiler doesn't have -mavx2/-mfma.

This also simplifies cmake to just always test the flags; there are some
cases (like using NATIVE_BUILD) where USE_AVX2 doesn't necessarily
apply, and it's cheap to just check them.
pull/1273/head
Jason Rhinelander 4 years ago
parent b4fce0e3ee
commit 5e91c946c5

@ -42,20 +42,17 @@ set(NTRU_AVX_SRC
libntrup/src/avx/rq.c
libntrup/src/avx/rq_mod3.c
)
if(NOT NATIVE_BUILD AND USE_AVX2)
# Assume cxxflags are already enabling AVX2
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-mavx2 COMPILER_SUPPORTS_AVX2)
check_cxx_compiler_flag(-mfma COMPILER_SUPPORTS_FMA)
if(COMPILER_SUPPORTS_AVX2 AND COMPILER_SUPPORTS_FMA)
target_sources(lokinet-cryptography PRIVATE ${NTRU_AVX_SRC})
set_property(SOURCE ${NTRU_AVX_SRC} APPEND PROPERTY COMPILE_FLAGS "-mavx2 -mfma")
message(STATUS "Building libntrup with runtime AVX2/FMA support")
else()
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-mavx2 COMPILER_SUPPORTS_AVX2)
check_cxx_compiler_flag(-mfma COMPILER_SUPPORTS_FMA)
if(COMPILER_SUPPORTS_AVX2 AND COMPILER_SUPPORTS_FMA)
target_sources(lokinet-cryptography PRIVATE ${NTRU_AVX_SRC})
set_property(SOURCE ${NTRU_AVX_SRC} APPEND PROPERTY COMPILE_FLAGS "-mavx2 -mfma")
message(STATUS "Building libntrup with runtime AVX2/FMA support")
else()
message(STATUS "Not building with libntrup runtime AVX2/FMA support (can't figure out how to compile with AVX2/FMA: -mavx2 -mfma didn't work)")
endif()
target_sources(lokinet-cryptography PRIVATE libntrup/src/noavx-stubs.c)
message(STATUS "Not building with libntrup runtime AVX2/FMA support (either this architecture doesn't support them, or your compile doesn't support the -mavx2 -mfma flags")
endif()
enable_lto(lokinet-cryptography)

@ -11,11 +11,14 @@
#include "rq.h"
#include "r3.h"
#ifndef __AVX2__
#error "This file requires compilation with AVX2 support"
#endif
int
crypto_kem_dec_avx2(unsigned char *k, const unsigned char *cstr,
const unsigned char *sk)
{
#if __AVX2__
small f[768];
modq h[768];
small grecip[768];
@ -67,10 +70,4 @@ crypto_kem_dec_avx2(unsigned char *k, const unsigned char *cstr,
for(i = 0; i < 32; ++i)
k[i] = (hash[32 + i] & ~result);
return result;
#else
(void)(k);
(void)(sk);
(void)(cstr);
return -1;
#endif
}

@ -9,11 +9,14 @@
#include <sodium/crypto_hash_sha512.h>
#include <sodium/crypto_kem.h>
#ifndef __AVX2__
#error "This file requires compilation with AVX2 support"
#endif
int
crypto_kem_enc_avx2(unsigned char *cstr, unsigned char *k,
const unsigned char *pk)
{
#if __AVX2__
small r[768];
modq h[768];
modq c[768];
@ -46,10 +49,4 @@ crypto_kem_enc_avx2(unsigned char *cstr, unsigned char *k,
rq_roundencode(cstr + 32, c);
return 0;
#else
(void)(cstr);
(void)(k);
(void)(pk);
return -1;
#endif
}

@ -14,10 +14,13 @@
"crypto_kem_SECRETKEYBYTES must match rq_encode_len + 2 * small_encode_len"
#endif
#ifndef __AVX2__
#error "This file requires compilation with AVX2 support"
#endif
int
crypto_kem_keypair_avx2(unsigned char *pk, unsigned char *sk)
{
#if __AVX2__
small g[768];
small grecip[768];
small f[768];
@ -39,9 +42,4 @@ crypto_kem_keypair_avx2(unsigned char *pk, unsigned char *sk)
memcpy(sk + 2 * small_encode_len, pk, rq_encode_len);
return 0;
#else
(void)(pk);
(void)(sk);
return -1;
#endif
}

@ -0,0 +1,29 @@
// Stubs for compilers/builds without avx2 support
//
int
crypto_kem_enc_avx2(unsigned char *cstr, unsigned char *k,
const unsigned char *pk)
{
(void)(cstr);
(void)(k);
(void)(pk);
return -1;
}
int
crypto_kem_dec_avx2(unsigned char *k, const unsigned char *cstr,
const unsigned char *sk)
{
(void)(k);
(void)(sk);
(void)(cstr);
return -1;
}
int
crypto_kem_keypair_avx2(unsigned char *pk, unsigned char *sk)
{
(void)(pk);
(void)(sk);
return -1;
}

@ -43,14 +43,12 @@ extern "C"
{
__crypto_kem_dec = &crypto_kem_dec_avx2;
__crypto_kem_enc = &crypto_kem_enc_avx2;
__crypto_kem_dec = &crypto_kem_dec_avx2;
__crypto_kem_keypair = &crypto_kem_keypair_avx2;
}
else
{
__crypto_kem_dec = &crypto_kem_dec_ref;
__crypto_kem_enc = &crypto_kem_enc_ref;
__crypto_kem_dec = &crypto_kem_dec_ref;
__crypto_kem_keypair = &crypto_kem_keypair_ref;
}
}

Loading…
Cancel
Save