fix libntrup avx2 detection

pull/13/head
Jeff Becker 6 years ago
parent c12da3dfc7
commit 0d0a3357f7
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -291,7 +291,7 @@ include_directories(crypto/libntrup/include)
set(NTRU_SRC
${NTRU_AVX_SRC}
${NTRU_REF_SRC}
crypto/libntrup/src/ntru.c
crypto/libntrup/src/ntru.cpp
)
set(LIB_SRC

@ -11,12 +11,14 @@
#include "crypto_verify_32.h"
#include "crypto_kem.h"
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];
@ -64,4 +66,7 @@ int crypto_kem_dec_avx2(
for (i = 0;i < 32;++i) k[i] = (hash[32 + i] & ~result);
return result;
#else
return -1;
#endif
}

@ -15,6 +15,7 @@ int crypto_kem_enc_avx2(
const unsigned char *pk
)
{
#if __AVX2__
small r[768];
modq h[768];
modq c[768];
@ -45,4 +46,7 @@ int crypto_kem_enc_avx2(
rq_roundencode(cstr + 32,c);
return 0;
#else
return -1;
#endif
}

@ -15,6 +15,7 @@
int crypto_kem_keypair_avx2(unsigned char *pk,unsigned char *sk)
{
#if __AVX2__
small g[768];
small grecip[768];
small f[768];
@ -36,4 +37,7 @@ int crypto_kem_keypair_avx2(unsigned char *pk,unsigned char *sk)
memcpy(sk + 2 * small_encode_len,pk,rq_encode_len);
return 0;
#else
return -1;
#endif
}

@ -1,68 +0,0 @@
#include <libntrup/ntru.h>
#include <stdbool.h>
#if __AVX__
#include <cpuid.h>
static bool supports_avx2()
{
int cpuinfo[4] = {-1};
__cpuid(0, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
if(cpuinfo[0] < 7)
return false;
__cpuid(7, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
return cpuinfo[1] & (1 << 5);
}
#else
static bool supports_avx2()
{
return false;
}
#endif
int (*__crypto_kem_enc)(unsigned char *cstr, unsigned char *k, const unsigned char *pk);
int (*__crypto_kem_dec)(unsigned char *k, const unsigned char *cstr, const unsigned char *sk);
int (*__crypto_kem_keypair)(unsigned char *pk, unsigned char * sk);
void ntru_init()
{
if(supports_avx2())
{
__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;
}
}
int crypto_kem_enc(unsigned char *cstr, unsigned char *k, const unsigned char *pk)
{
return __crypto_kem_enc(cstr, k, pk);
}
int crypto_kem_dec(unsigned char *k, const unsigned char *cstr, const unsigned char *sk)
{
return __crypto_kem_dec(k, cstr, sk);
}
int crypto_kem_keypair(unsigned char *pk, unsigned char * sk)
{
return __crypto_kem_keypair(pk, sk);
}

@ -0,0 +1,80 @@
#include <libntrup/ntru.h>
#include <stdbool.h>
#if __AVX2__
#include <cpuid.h>
#include <array>
std::array< int, 4 >
CPUID(int funcno)
{
std::array< int, 4 > cpuinfo;
__cpuid(funcno, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
return cpuinfo;
}
bool
supports_avx2()
{
return CPUID(0).at(0) >= 7 && CPUID(7).at(1) & (1 << 5);
}
#else
bool
supports_avx2()
{
printf("AVX2 disabled on compile time\n");
return false;
}
#endif
int (*__crypto_kem_enc)(unsigned char *cstr, unsigned char *k,
const unsigned char *pk);
int (*__crypto_kem_dec)(unsigned char *k, const unsigned char *cstr,
const unsigned char *sk);
int (*__crypto_kem_keypair)(unsigned char *pk, unsigned char *sk);
extern "C"
{
void
ntru_init()
{
if(supports_avx2())
{
__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;
}
}
int
crypto_kem_enc(unsigned char *cstr, unsigned char *k, const unsigned char *pk)
{
return __crypto_kem_enc(cstr, k, pk);
}
int
crypto_kem_dec(unsigned char *k, const unsigned char *cstr,
const unsigned char *sk)
{
return __crypto_kem_dec(k, cstr, sk);
}
int
crypto_kem_keypair(unsigned char *pk, unsigned char *sk)
{
return __crypto_kem_keypair(pk, sk);
}
}
Loading…
Cancel
Save