diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt index 935543386..dc62d83ff 100644 --- a/crypto/CMakeLists.txt +++ b/crypto/CMakeLists.txt @@ -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) diff --git a/crypto/libntrup/src/avx/dec.c b/crypto/libntrup/src/avx/dec.c index 817b22a84..bc189e992 100644 --- a/crypto/libntrup/src/avx/dec.c +++ b/crypto/libntrup/src/avx/dec.c @@ -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 } diff --git a/crypto/libntrup/src/avx/enc.c b/crypto/libntrup/src/avx/enc.c index 319ed49dd..85ac4d946 100644 --- a/crypto/libntrup/src/avx/enc.c +++ b/crypto/libntrup/src/avx/enc.c @@ -9,11 +9,14 @@ #include #include +#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 } diff --git a/crypto/libntrup/src/avx/keypair.c b/crypto/libntrup/src/avx/keypair.c index 18aae4658..32aed4ac0 100644 --- a/crypto/libntrup/src/avx/keypair.c +++ b/crypto/libntrup/src/avx/keypair.c @@ -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 } diff --git a/crypto/libntrup/src/noavx-stubs.c b/crypto/libntrup/src/noavx-stubs.c new file mode 100644 index 000000000..023354c40 --- /dev/null +++ b/crypto/libntrup/src/noavx-stubs.c @@ -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; +} diff --git a/crypto/libntrup/src/ntru.cpp b/crypto/libntrup/src/ntru.cpp index fd8f62037..b21fdc690 100644 --- a/crypto/libntrup/src/ntru.cpp +++ b/crypto/libntrup/src/ntru.cpp @@ -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; } }