2020-07-31 07:17:42 +01:00
|
|
|
#include "cbd.h"
|
2020-10-27 00:05:07 +00:00
|
|
|
#include "params.h"
|
2019-09-17 13:02:01 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: load32_littleendian
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Description: load 4 bytes into a 32-bit integer
|
2019-09-17 13:02:01 +01:00
|
|
|
* in little-endian order
|
|
|
|
*
|
|
|
|
* Arguments: - const uint8_t *x: pointer to input byte array
|
|
|
|
*
|
|
|
|
* Returns 32-bit unsigned integer loaded from x
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static uint32_t load32_littleendian(const uint8_t x[4]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
uint32_t r;
|
2019-09-17 13:02:01 +01:00
|
|
|
r = (uint32_t)x[0];
|
|
|
|
r |= (uint32_t)x[1] << 8;
|
|
|
|
r |= (uint32_t)x[2] << 16;
|
|
|
|
r |= (uint32_t)x[3] << 24;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-10-27 13:48:42 +00:00
|
|
|
* Name: load24_littleendian
|
|
|
|
*
|
|
|
|
* Description: load 3 bytes into a 32-bit integer
|
|
|
|
* in little-endian order.
|
|
|
|
* This function is only needed for Kyber-512
|
|
|
|
*
|
|
|
|
* Arguments: - const uint8_t *x: pointer to input byte array
|
|
|
|
*
|
|
|
|
* Returns 32-bit unsigned integer loaded from x (most significant byte is zero)
|
|
|
|
**************************************************/
|
|
|
|
static uint32_t load24_littleendian(const uint8_t x[3]) {
|
|
|
|
uint32_t r;
|
|
|
|
r = (uint32_t)x[0];
|
|
|
|
r |= (uint32_t)x[1] << 8;
|
|
|
|
r |= (uint32_t)x[2] << 16;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: cbd2
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Given an array of uniformly random bytes, compute
|
|
|
|
* polynomial with coefficients distributed according to
|
2020-10-27 13:48:42 +00:00
|
|
|
* a centered binomial distribution with parameter eta=2
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2019-09-17 13:02:01 +01:00
|
|
|
* - const uint8_t *buf: pointer to input byte array
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
static void cbd2(poly *r, const uint8_t buf[2 * KYBER_N / 4]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
unsigned int i, j;
|
|
|
|
uint32_t t, d;
|
|
|
|
int16_t a, b;
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N / 8; i++) {
|
|
|
|
t = load32_littleendian(buf + 4 * i);
|
2019-09-17 13:02:01 +01:00
|
|
|
d = t & 0x55555555;
|
|
|
|
d += (t >> 1) & 0x55555555;
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
a = (d >> (4 * j + 0)) & 0x3;
|
2019-09-17 13:02:01 +01:00
|
|
|
b = (d >> (4 * j + 2)) & 0x3;
|
|
|
|
r->coeffs[8 * i + j] = a - b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 13:48:42 +00:00
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: cbd3
|
|
|
|
*
|
|
|
|
* Description: Given an array of uniformly random bytes, compute
|
|
|
|
* polynomial with coefficients distributed according to
|
|
|
|
* a centered binomial distribution with parameter eta=3.
|
|
|
|
* This function is only needed for Kyber-512
|
|
|
|
*
|
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
|
|
|
* - const uint8_t *buf: pointer to input byte array
|
|
|
|
**************************************************/
|
|
|
|
static void cbd3(poly *r, const uint8_t buf[3 * KYBER_N / 4]) {
|
|
|
|
unsigned int i, j;
|
|
|
|
uint32_t t, d;
|
|
|
|
int16_t a, b;
|
|
|
|
|
|
|
|
for (i = 0; i < KYBER_N / 4; i++) {
|
|
|
|
t = load24_littleendian(buf + 3 * i);
|
|
|
|
d = t & 0x00249249;
|
|
|
|
d += (t >> 1) & 0x00249249;
|
|
|
|
d += (t >> 2) & 0x00249249;
|
|
|
|
|
|
|
|
for (j = 0; j < 4; j++) {
|
|
|
|
a = (d >> (6 * j + 0)) & 0x7;
|
|
|
|
b = (d >> (6 * j + 3)) & 0x7;
|
|
|
|
r->coeffs[4 * i + j] = a - b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PQCLEAN_KYBER51290S_CLEAN_poly_cbd_eta1(poly *r, const uint8_t buf[KYBER_ETA1 * KYBER_N / 4]) {
|
|
|
|
cbd3(r, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PQCLEAN_KYBER51290S_CLEAN_poly_cbd_eta2(poly *r, const uint8_t buf[KYBER_ETA2 * KYBER_N / 4]) {
|
|
|
|
cbd2(r, buf);
|
|
|
|
}
|