2019-09-17 13:02:01 +01:00
|
|
|
#include "params.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include "cbd.h"
|
2019-09-17 13:02:01 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: load32_littleendian
|
|
|
|
*
|
|
|
|
* Description: load bytes into a 32-bit integer
|
|
|
|
* 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]) {
|
|
|
|
uint32_t r = 0;
|
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-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER51290S_CLEAN_cbd
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Given an array of uniformly random bytes, compute
|
|
|
|
* polynomial with coefficients distributed according to
|
|
|
|
* a centered binomial distribution with parameter KYBER_ETA
|
|
|
|
*
|
2020-07-31 07:17:42 +01: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-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER51290S_CLEAN_cbd(poly *r, const uint8_t buf[KYBER_ETA * KYBER_N / 4]) {
|
|
|
|
unsigned int i = 0, j = 0;
|
|
|
|
uint32_t t = 0, d = 0;
|
|
|
|
int16_t a = 0, b = 0;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|