Skip to content

Commit

Permalink
Merge pull request #19 from tlepoint/master
Browse files Browse the repository at this point in the history
Fix alignment issues, and minor factorization
  • Loading branch information
carlosaguilarmelchor committed Jun 8, 2016
2 parents e734e1c + 59212db commit 4876146
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 72 deletions.
27 changes: 5 additions & 22 deletions tests/nfllib_demo_main_func.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nfllib_demo_main.hpp"
#include <chrono>
#include <iostream>
#include "tools.h"

#define REPETITIONS 10
#define NOISE_UB 4
Expand All @@ -22,24 +23,6 @@
#define TEST_FMAS_SHOUP
#define TEST_LWE_SYMMETRIC

template <class T>
double get_time_us(T const& start, T const& end, uint32_t N)
{
auto diff = end-start;
return (long double)(std::chrono::duration_cast<std::chrono::microseconds>(diff).count())/N;
}

template <class T, size_t Align>
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<size_t degree, size_t modulus, class T>
int run()
{
Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 8 additions & 33 deletions tests/nfllib_demo_main_op.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nfllib_demo_main.hpp"
#include <chrono>
#include <iostream>
#include "tools.h"

#define REPETITIONS 10
#define NOISE_UB 4
Expand All @@ -26,15 +27,15 @@ template <class P>
__attribute__((noinline)) static void encrypt(P& resa, P& resb, P const & pka, P const & pkb, P const & pkaprime, P const & pkbprime, nfl::FastGaussianNoise<uint8_t, typename P::value_type, 2> *g_prng)
{
// u
P tmpu = nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng);
P &tmpu = *alloc_aligned<P, 32>(1, nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng));
tmpu.ntt_pow_phi();

// 2*e_1
P tmpe1 = nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng, 2);
P &tmpe1 = *alloc_aligned<P, 32>(1, nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng, 2));
tmpe1.ntt_pow_phi();

// 2*e_2
P tmpe2 = nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng, 2);
P &tmpe2 = *alloc_aligned<P, 32>(1, nfl::gaussian<uint8_t, typename P::value_type, 2>(g_prng, 2));
tmpe2.ntt_pow_phi();

// resa = pka * u + 2*e_2
Expand All @@ -56,26 +57,6 @@ __attribute__((noinline)) static void decrypt(P& tmp, P const & resa, P const& r
}
}

template <class T>
double get_time_us(T const& start, T const& end, uint32_t N)
{
auto diff = end-start;
return (long double)(std::chrono::duration_cast<std::chrono::microseconds>(diff).count())/N;
}

template <class T, size_t Align, class... Args>
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>(args)...};
}
return ret;
}

template <class P>
bool test_mulmod_shoup(P* resa, P* resb, P* resc, P* resd)
{
Expand Down Expand Up @@ -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<poly_t, 32>(REPETITIONS),
*resb = alloc_aligned<poly_t, 32>(REPETITIONS),
*resc = alloc_aligned<poly_t, 32>(REPETITIONS),
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 1 addition & 17 deletions tests/ntt_perfs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nfllib_demo_main.hpp"
#include <chrono>
#include <iostream>
#include "tools.h"

#define REPETITIONS 50000

Expand Down Expand Up @@ -115,23 +116,6 @@ __attribute__((noinline)) void ntt_new(uint64_t* x, uint64_t* wtab, uint64_t* wi

}

template <class T>
double get_time_us(T const& start, T const& end, uint32_t N)
{
auto diff = end-start;
return (long double)(std::chrono::duration_cast<std::chrono::microseconds>(diff).count())/N;
}

template <class T, size_t Align>
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 <class P>
Expand Down
7 changes: 7 additions & 0 deletions tests/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ void free_aligned(size_t n, T* p)
free(p);
}

template <class T>
double get_time_us(T const& start, T const& end, uint32_t N)
{
auto diff = end-start;
return (long double)(std::chrono::duration_cast<std::chrono::microseconds>(diff).count())/N;
}

#endif

0 comments on commit 4876146

Please sign in to comment.