2019-01-15 15:03:38 +00:00
|
|
|
#include "cbd.h"
|
|
|
|
|
|
|
|
/*************************************************
|
2019-01-15 15:34:01 +00:00
|
|
|
* Name: load_littleendian
|
|
|
|
*
|
|
|
|
* Description: load bytes into a 64-bit integer
|
|
|
|
* in little-endian order
|
|
|
|
*
|
|
|
|
* Arguments: - const unsigned char *x: pointer to input byte array
|
|
|
|
* - bytes: number of bytes to load, has to be <=
|
|
|
|
*8
|
|
|
|
*
|
|
|
|
* Returns 64-bit unsigned integer loaded from x
|
|
|
|
**************************************************/
|
|
|
|
static uint64_t load_littleendian(const unsigned char *x, int bytes) {
|
2019-01-16 10:02:32 +00:00
|
|
|
int i;
|
|
|
|
uint64_t r = x[0];
|
|
|
|
for (i = 1; i < bytes; i++) {
|
|
|
|
r |= (uint64_t)x[i] << (8 * i);
|
|
|
|
}
|
|
|
|
return r;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-01-15 15:34:01 +00:00
|
|
|
* Name: cbd
|
|
|
|
*
|
|
|
|
* Description: Given an array of uniformly random bytes, compute
|
|
|
|
* polynomial with coefficients distributed according to
|
|
|
|
* a centered binomial distribution with parameter KYBER_ETA
|
|
|
|
*
|
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
|
|
|
* - const unsigned char *buf: pointer to input byte array
|
|
|
|
**************************************************/
|
2019-02-26 16:27:32 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_cbd(poly *r, const unsigned char *buf) {
|
2019-02-13 16:45:09 +00:00
|
|
|
#if KYBER_ETA == 3
|
2019-01-16 10:02:32 +00:00
|
|
|
uint32_t t, d, a[4], b[4];
|
|
|
|
int i, j;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < KYBER_N / 4; i++) {
|
2019-02-14 15:14:47 +00:00
|
|
|
t = (uint32_t)load_littleendian(buf + 3 * i, 3);
|
2019-01-16 10:02:32 +00:00
|
|
|
d = 0;
|
2019-02-13 16:45:09 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2019-01-16 10:02:32 +00:00
|
|
|
d += (t >> j) & 0x249249;
|
2019-02-13 16:45:09 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 14:30:55 +00:00
|
|
|
a[0] = d & 0x7;
|
2019-02-13 16:45:09 +00:00
|
|
|
b[0] = (d >> 3) & 0x7;
|
|
|
|
a[1] = (d >> 6) & 0x7;
|
|
|
|
b[1] = (d >> 9) & 0x7;
|
2019-01-16 10:02:32 +00:00
|
|
|
a[2] = (d >> 12) & 0x7;
|
|
|
|
b[2] = (d >> 15) & 0x7;
|
|
|
|
a[3] = (d >> 18) & 0x7;
|
|
|
|
b[3] = (d >> 21);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 15:16:50 +00:00
|
|
|
r->coeffs[4 * i + 0] = (uint16_t)(a[0] + KYBER_Q - b[0]);
|
|
|
|
r->coeffs[4 * i + 1] = (uint16_t)(a[1] + KYBER_Q - b[1]);
|
|
|
|
r->coeffs[4 * i + 2] = (uint16_t)(a[2] + KYBER_Q - b[2]);
|
|
|
|
r->coeffs[4 * i + 3] = (uint16_t)(a[3] + KYBER_Q - b[3]);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-02-13 16:45:09 +00:00
|
|
|
#elif KYBER_ETA == 4
|
2019-01-16 10:02:32 +00:00
|
|
|
uint32_t t, d, a[4], b[4];
|
|
|
|
int i, j;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < KYBER_N / 4; i++) {
|
2019-02-14 15:14:47 +00:00
|
|
|
t = (uint32_t)load_littleendian(buf + 4 * i, 4);
|
2019-01-16 10:02:32 +00:00
|
|
|
d = 0;
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
d += (t >> j) & 0x11111111;
|
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 14:30:55 +00:00
|
|
|
a[0] = d & 0xf;
|
2019-02-13 16:45:09 +00:00
|
|
|
b[0] = (d >> 4) & 0xf;
|
|
|
|
a[1] = (d >> 8) & 0xf;
|
2019-01-16 10:02:32 +00:00
|
|
|
b[1] = (d >> 12) & 0xf;
|
|
|
|
a[2] = (d >> 16) & 0xf;
|
|
|
|
b[2] = (d >> 20) & 0xf;
|
|
|
|
a[3] = (d >> 24) & 0xf;
|
|
|
|
b[3] = (d >> 28);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 15:16:50 +00:00
|
|
|
r->coeffs[4 * i + 0] = (uint16_t)(a[0] + KYBER_Q - b[0]);
|
|
|
|
r->coeffs[4 * i + 1] = (uint16_t)(a[1] + KYBER_Q - b[1]);
|
|
|
|
r->coeffs[4 * i + 2] = (uint16_t)(a[2] + KYBER_Q - b[2]);
|
|
|
|
r->coeffs[4 * i + 3] = (uint16_t)(a[3] + KYBER_Q - b[3]);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-02-13 16:45:09 +00:00
|
|
|
#elif KYBER_ETA == 5
|
2019-01-16 10:02:32 +00:00
|
|
|
uint64_t t, d, a[4], b[4];
|
|
|
|
int i, j;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < KYBER_N / 4; i++) {
|
|
|
|
t = load_littleendian(buf + 5 * i, 5);
|
|
|
|
d = 0;
|
2019-02-13 16:45:09 +00:00
|
|
|
for (j = 0; j < 5; j++) {
|
2019-01-16 10:02:32 +00:00
|
|
|
d += (t >> j) & 0x0842108421UL;
|
2019-02-13 16:45:09 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 14:30:55 +00:00
|
|
|
a[0] = d & 0x1f;
|
2019-02-13 16:45:09 +00:00
|
|
|
b[0] = (d >> 5) & 0x1f;
|
2019-01-16 10:02:32 +00:00
|
|
|
a[1] = (d >> 10) & 0x1f;
|
|
|
|
b[1] = (d >> 15) & 0x1f;
|
|
|
|
a[2] = (d >> 20) & 0x1f;
|
|
|
|
b[2] = (d >> 25) & 0x1f;
|
|
|
|
a[3] = (d >> 30) & 0x1f;
|
|
|
|
b[3] = (d >> 35);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-14 15:16:50 +00:00
|
|
|
r->coeffs[4 * i + 0] = (uint16_t)(a[0] + KYBER_Q - b[0]);
|
|
|
|
r->coeffs[4 * i + 1] = (uint16_t)(a[1] + KYBER_Q - b[1]);
|
|
|
|
r->coeffs[4 * i + 2] = (uint16_t)(a[2] + KYBER_Q - b[2]);
|
|
|
|
r->coeffs[4 * i + 3] = (uint16_t)(a[3] + KYBER_Q - b[3]);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-02-13 16:45:09 +00:00
|
|
|
#else
|
2019-01-15 15:03:38 +00:00
|
|
|
#error "poly_getnoise in poly.c only supports eta in {3,4,5}"
|
2019-02-13 16:45:09 +00:00
|
|
|
#endif
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|