diff --git a/tests/nfllib_demo_main_func.cpp b/tests/nfllib_demo_main_func.cpp index 6ff16e2..2064c0e 100644 --- a/tests/nfllib_demo_main_func.cpp +++ b/tests/nfllib_demo_main_func.cpp @@ -1,6 +1,7 @@ #include "nfllib_demo_main.hpp" #include #include +#include "tools.h" #define REPETITIONS 10 #define NOISE_UB 4 @@ -22,24 +23,6 @@ #define TEST_FMAS_SHOUP #define TEST_LWE_SYMMETRIC -template -double get_time_us(T const& start, T const& end, uint32_t N) -{ - auto diff = end-start; - return (long double)(std::chrono::duration_cast(diff).count())/N; -} - -template -T* alloc_aligned(size_t n) -{ - T* ret; - if (posix_memalign((void**) &ret, Align, sizeof(T)*n) != 0) { - throw std::bad_alloc(); - } - return ret; -} - - template int run() { @@ -126,10 +109,10 @@ int run() #endif // Cleaning - delete [] resa; - delete [] resb; - delete [] resc; - delete [] resd; + free_aligned(REPETITIONS, resa); + free_aligned(REPETITIONS, resb); + free_aligned(REPETITIONS, resc); + free_aligned(REPETITIONS, resd); return 0; } diff --git a/tests/nfllib_demo_main_op.cpp b/tests/nfllib_demo_main_op.cpp index 9149e5c..533e60f 100644 --- a/tests/nfllib_demo_main_op.cpp +++ b/tests/nfllib_demo_main_op.cpp @@ -1,6 +1,7 @@ #include "nfllib_demo_main.hpp" #include #include +#include "tools.h" #define REPETITIONS 10 #define NOISE_UB 4 @@ -26,15 +27,15 @@ template __attribute__((noinline)) static void encrypt(P& resa, P& resb, P const & pka, P const & pkb, P const & pkaprime, P const & pkbprime, nfl::FastGaussianNoise *g_prng) { // u - P tmpu = nfl::gaussian(g_prng); + P &tmpu = *alloc_aligned(1, nfl::gaussian(g_prng)); tmpu.ntt_pow_phi(); // 2*e_1 - P tmpe1 = nfl::gaussian(g_prng, 2); + P &tmpe1 = *alloc_aligned(1, nfl::gaussian(g_prng, 2)); tmpe1.ntt_pow_phi(); // 2*e_2 - P tmpe2 = nfl::gaussian(g_prng, 2); + P &tmpe2 = *alloc_aligned(1, nfl::gaussian(g_prng, 2)); tmpe2.ntt_pow_phi(); // resa = pka * u + 2*e_2 @@ -56,26 +57,6 @@ __attribute__((noinline)) static void decrypt(P& tmp, P const & resa, P const& r } } -template -double get_time_us(T const& start, T const& end, uint32_t N) -{ - auto diff = end-start; - return (long double)(std::chrono::duration_cast(diff).count())/N; -} - -template -T* alloc_aligned(size_t n, Args&& ... args) -{ - T* ret; - if (posix_memalign((void**) &ret, Align, sizeof(T)*n) != 0) { - throw std::bad_alloc(); - } - for (size_t i = 0; i < n; i++) { - new (&ret[i]) T{std::forward(args)...}; - } - return ret; -} - template bool test_mulmod_shoup(P* resa, P* resb, P* resc, P* resd) { @@ -119,12 +100,6 @@ int run() // Polynomial arrays to do the tests start = std::chrono::steady_clock::now(); -/* AG: on my system, this gives pointers non aligned on 32-bytes! - poly_t *resa = new poly_t[REPETITIONS], - *resb = new poly_t[REPETITIONS], - *resc = new poly_t[REPETITIONS], - *resd = new poly_t[REPETITIONS]; -*/ poly_t *resa = alloc_aligned(REPETITIONS), *resb = alloc_aligned(REPETITIONS), *resc = alloc_aligned(REPETITIONS), @@ -357,10 +332,10 @@ int run() #endif // Cleaning - delete [] resa; - delete [] resb; - delete [] resc; - delete [] resd; + free_aligned(REPETITIONS, resa); + free_aligned(REPETITIONS, resb); + free_aligned(REPETITIONS, resc); + free_aligned(REPETITIONS, resd); return 0; } diff --git a/tests/ntt_perfs.cpp b/tests/ntt_perfs.cpp index 0d8a4ee..d5178df 100644 --- a/tests/ntt_perfs.cpp +++ b/tests/ntt_perfs.cpp @@ -1,6 +1,7 @@ #include "nfllib_demo_main.hpp" #include #include +#include "tools.h" #define REPETITIONS 50000 @@ -115,23 +116,6 @@ __attribute__((noinline)) void ntt_new(uint64_t* x, uint64_t* wtab, uint64_t* wi } - template -double get_time_us(T const& start, T const& end, uint32_t N) -{ - auto diff = end-start; - return (long double)(std::chrono::duration_cast(diff).count())/N; -} - - template -T* alloc_aligned(size_t n) -{ - T* ret; - if (posix_memalign((void**) &ret, Align, sizeof(T)*n) != 0) { - throw std::bad_alloc(); - } - return ret; -} - namespace nfl { namespace tests { template diff --git a/tests/tools.h b/tests/tools.h index 32c34e5..991cebe 100644 --- a/tests/tools.h +++ b/tests/tools.h @@ -23,4 +23,11 @@ void free_aligned(size_t n, T* p) free(p); } +template +double get_time_us(T const& start, T const& end, uint32_t N) +{ + auto diff = end-start; + return (long double)(std::chrono::duration_cast(diff).count())/N; +} + #endif