Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully reduce sampled ternary vectors. #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions include/nfl/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void poly<T, Degree, NbModuli>::set(non_uniform const& mode) {
}
}
}
}
}
else
{
for (unsigned int i = 0; i < degree; i++) {
Expand Down Expand Up @@ -303,10 +303,10 @@ void poly<T, Degree, NbModuli>::set(gaussian<in_class, T, _lu_depth> const& mode
mode.fg_prng->getNoise((value_type *)rnd, degree);

if (amplifier != 1) for (unsigned int i = 0; i < degree; i++) rnd[i]*= amplifier;
for (size_t cm = 0; cm < nmoduli; cm++)
for (size_t cm = 0; cm < nmoduli; cm++)
{
for (size_t i = 0 ; i < degree; i++)
{
{
if(rnd[i]<0)
_data[degree*cm+i] = get_modulus(cm) + rnd[i];
else
Expand Down Expand Up @@ -338,8 +338,12 @@ void poly<T, Degree, NbModuli>::set(ZO_dist const& mode) {
for (size_t cm = 0; cm < NbModuli; ++cm) {
const T pm = params<T>::P[cm] - 1u;
/* sample {-1, 0, 1} */
for (size_t i = 0; i < Degree; ++i)
*ptr++ = rnd[i] <= mode.rho ? pm + (rnd[i] & 2) : 0u;
for (size_t i = 0; i < Degree; ++i, ptr++) {
*ptr = rnd[i] <= mode.rho ? (pm + (rnd[i] & 2)) : 0u;
if (*ptr > params<T>::P[cm]) {
*ptr -= params<T>::P[cm];
}
}
}
}

Expand All @@ -357,7 +361,7 @@ void poly<T, Degree, NbModuli>::set(hwt_dist const& mode) {
auto rnd_end = rnd.end();
auto rnd_ptr = rnd_end;
/* Reservoir Sampling: uniformly select hwt coefficients. */
for (size_t k = mode.hwt; k < degree; ++k)
for (size_t k = mode.hwt; k < degree; ++k)
{
size_t pos = 0;
size_t reject_sample = std::numeric_limits<size_t>::max() / k;
Expand All @@ -384,8 +388,10 @@ void poly<T, Degree, NbModuli>::set(hwt_dist const& mode) {
for (size_t cm = 0, offset = 0; cm < NbModuli; ++cm, offset += degree) {
const T pm = params<T>::P[cm] - 1u;
rnd_ptr = rnd.begin();
for (size_t pos : hitted)
_data[pos + offset] = pm + ((*rnd_ptr++) & 2U); // {-1, 1}
for (size_t pos : hitted) {
_data[pos + offset] = ((*rnd_ptr++) & 2U); // {-1, 1}
_data[pos + offset] = (_data[pos + offset] > 0 ? 1 : pm);
}
}
std::memset(hitted.data(), 0x0, hitted.size() * sizeof(size_t)); // erase from memory
}
Expand All @@ -400,9 +406,9 @@ std::ostream& operator<<(std::ostream& outs, poly<T, Degree, NbModuli> const& p)
{
bool first = true;
std::string term;
if (typeid(T) == typeid(uint64_t)) term = "ULL";
else if (typeid(T) == typeid(uint32_t)) term = "UL";
else term = "U";
if (typeid(T) == typeid(uint64_t)) term = "ULL";
else if (typeid(T) == typeid(uint32_t)) term = "UL";
else term = "U";

outs << "{ ";
for(auto v : p)
Expand Down Expand Up @@ -688,5 +694,3 @@ template<class T, size_t Degree, size_t NbModuli> void poly<T, Degree, NbModuli
}

#endif


2 changes: 2 additions & 0 deletions include/nfl/prng/fastrandombytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define FASTRANDOMBYTES_H

namespace nfl {
void fastrandombytes_seed(const unsigned char *s);
void fastrandombytes_reseed();
void fastrandombytes(unsigned char *r, unsigned long long rlen);
}

Expand Down
169 changes: 141 additions & 28 deletions lib/prng/fastrandombytes.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,148 @@
/*
* File: lattisigns512-20130329/fastrandombytes.c
* Author: Gim Güneysu, Tobias Oder, Thomas Pöppelmann, Peter Schwabe
* Public Domain
/* Adapted from Intel® Advanced Encryption Standard (Intel® AES) Instructions Set - Rev 3.01
* https://software.intel.com/sites/default/files/article/165683/aes-wp-2012-09-22-v01.pdf
*/

#include <inttypes.h>
#include <iostream>
#include "nfl/prng/crypto_stream_salsa20.h"
#include "nfl/prng/randombytes.h"
#include "fastrandombytes.h"
#include "randombytes.h"
#include <string.h>
#include <x86intrin.h>

namespace nfl {
static __m128i round_key[15];
static __m128i iv;
static const __m128i ONE = {1, 0};

#define AES256_KEY_LENGTH 32

static size_t constexpr crypto_stream_salsa20_KEYBYTES = 32;
static size_t constexpr crypto_stream_salsa20_NONCEBYTES = 8;
namespace nfl {

static int init = 0;
static unsigned char key[crypto_stream_salsa20_KEYBYTES];
static unsigned char nonce[crypto_stream_salsa20_NONCEBYTES] = {0};

void fastrandombytes(unsigned char *r, unsigned long long rlen) {
unsigned long long n = 0;
int i;
if (!init) {
randombytes(key, crypto_stream_salsa20_KEYBYTES);
init = 1;
}
nfl_crypto_stream_salsa20_amd64_xmm6(r, rlen, nonce, key);

// Increase 64-bit counter (nonce)
for (i = 0; i < crypto_stream_salsa20_NONCEBYTES; i++) n ^= ((unsigned long long)nonce[i]) << 8 * i;
n++;
for (i = 0; i < crypto_stream_salsa20_NONCEBYTES; i++) nonce[i] = (n >> 8 * i) & 0xff;

static inline void KEY_256_ASSIST_1(__m128i* temp1, __m128i * temp2)
{
__m128i temp4;
*temp2 = _mm_shuffle_epi32(*temp2, 0xff);
temp4 = _mm_slli_si128(*temp1, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp1 = _mm_xor_si128(*temp1, temp4);
*temp1 = _mm_xor_si128(*temp1, *temp2);
}

static inline void KEY_256_ASSIST_2(__m128i* temp1, __m128i * temp3)
{
__m128i temp2,temp4;
temp4 = _mm_aeskeygenassist_si128(*temp1, 0x0);
temp2 = _mm_shuffle_epi32(temp4, 0xaa);
temp4 = _mm_slli_si128(*temp3, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
temp4 = _mm_slli_si128(temp4, 0x4);
*temp3 = _mm_xor_si128(*temp3, temp4);
*temp3 = _mm_xor_si128(*temp3, temp2);
}

/* round_key <-- aes256_key_expansion(randomness), iv <-- 0 */
void fastrandombytes_seed(const unsigned char *randomness)
{
__m128i temp1, temp2, temp3;

temp1 = _mm_loadu_si128((__m128i*)randomness);
temp3 = _mm_loadu_si128((__m128i*)(randomness+16));
round_key[0] = temp1;
round_key[1] = temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x01);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[2]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[3]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x02);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[4]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[5]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x04);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[6]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[7]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x08);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[8]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[9]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x10);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[10]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[11]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x20);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[12]=temp1;
KEY_256_ASSIST_2(&temp1, &temp3);
round_key[13]=temp3;
temp2 = _mm_aeskeygenassist_si128(temp3,0x40);
KEY_256_ASSIST_1(&temp1, &temp2);
round_key[14]=temp1;

iv = _mm_setzero_si128();
init = -1;
}

void fastrandombytes_reseed() {
init = 0;
}

static inline void AES_ctr_round(unsigned char *out)
{
__m128i tmp;

tmp = _mm_xor_si128(iv,round_key[0]);
tmp = _mm_aesenc_si128(tmp,round_key[1]);
tmp = _mm_aesenc_si128(tmp,round_key[2]);
tmp = _mm_aesenc_si128(tmp,round_key[3]);
tmp = _mm_aesenc_si128(tmp,round_key[4]);
tmp = _mm_aesenc_si128(tmp,round_key[5]);
tmp = _mm_aesenc_si128(tmp,round_key[6]);
tmp = _mm_aesenc_si128(tmp,round_key[7]);
tmp = _mm_aesenc_si128(tmp,round_key[8]);
tmp = _mm_aesenc_si128(tmp,round_key[9]);
tmp = _mm_aesenc_si128(tmp,round_key[10]);
tmp = _mm_aesenc_si128(tmp,round_key[11]);
tmp = _mm_aesenc_si128(tmp,round_key[12]);
tmp = _mm_aesenc_si128(tmp,round_key[13]);
tmp = _mm_aesenclast_si128(tmp,round_key[14]);
_mm_storeu_si128((__m128i*)out,tmp);

iv = _mm_add_epi32(iv, ONE);
}

/* r <-- aes256_ctr(round_key, iv, rlen) */
void fastrandombytes(unsigned char *r, unsigned long long rlen)
{
unsigned char ct[16];
unsigned long long num_of_blocks = rlen >> 4;
unsigned long long i;

if (!init) {
unsigned char seed[AES256_KEY_LENGTH];
randombytes(seed, AES256_KEY_LENGTH);
fastrandombytes_seed(seed);
init = 1;
}

for (i = 0; i < num_of_blocks; i++)
{
AES_ctr_round(r + (i << 4));
}

if (rlen & 0x0f)
{
AES_ctr_round(ct);

memcpy(r + (i << 4), ct, rlen & 0x0f);
}
}
}
}
Loading