1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-23 07:59:01 +00:00
pqcrypto/crypto_kem/kyber1024/clean/cbd.c

54 lines
1.6 KiB
C
Raw Normal View History

Kyberv2 (#150) * Replaced round-1 Kyber code with round-2 Kyber code (not yet cleaned/namespaced) * Namespacing for Kyber * Some more work on round-2 Kyber (more namespacing) * Added missing files * Round-2 Kyber768 now passing all tests under Linux * Various small tweaks to make MS compiler happy * Two more tweaks for MS compiler * Added Kyber512 and Kyber1024 (round-2 versions) * Making MS compiler happy * More fixes for MS compiler * Replaced round-1 Kyber code with round-2 Kyber code (not yet cleaned/namespaced) * Namespacing for Kyber * Some more work on round-2 Kyber (more namespacing) * Added missing files * Round-2 Kyber768 now passing all tests under Linux * Various small tweaks to make MS compiler happy * Two more tweaks for MS compiler * Added Kyber512 and Kyber1024 (round-2 versions) * Making MS compiler happy * More fixes for MS compiler * Started more cleanup work on Kyber768 * Replaced round-1 Kyber code with round-2 Kyber code (not yet cleaned/namespaced) * Namespacing for Kyber * Some more work on round-2 Kyber (more namespacing) * Added missing files * Round-2 Kyber768 now passing all tests under Linux * Various small tweaks to make MS compiler happy * Two more tweaks for MS compiler * Added Kyber512 and Kyber1024 (round-2 versions) * Replaced round-1 Kyber code with round-2 Kyber code (not yet cleaned/namespaced) * Namespacing for Kyber * Some more work on round-2 Kyber (more namespacing) * Added missing files * Round-2 Kyber768 now passing all tests under Linux * Various small tweaks to make MS compiler happy * Two more tweaks for MS compiler * Added Kyber512 and Kyber1024 (round-2 versions) * Making MS compiler happy * Making MS compiler happy * More fixes for MS compiler * More fixes for MS compiler * Started more cleanup work on Kyber768 * Kyber768 passing all tests locally * Kyber512 passes all tests locally * Kyber1024 now also passing all tests locally * Now passing all tests with -Wmissing-prototypes * Local tests (on Linux) passing again
2019-05-06 13:50:27 +01:00
#include "cbd.h"
#include "params.h"
#include <stdint.h>
/*************************************************
* Name: load32_littleendian
*
* Description: load bytes into a 32-bit integer
* in little-endian order
*
* Arguments: - const unsigned char *x: pointer to input byte array
*
* Returns 32-bit unsigned integer loaded from x
**************************************************/
static uint32_t load32_littleendian(const unsigned char *x) {
uint32_t r;
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;
}
/*************************************************
* 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
* specialized for KYBER_ETA=2
*
* Arguments: - poly *r: pointer to output polynomial
* - const unsigned char *buf: pointer to input byte array
**************************************************/
void PQCLEAN_KYBER1024_CLEAN_cbd(poly *r, const unsigned char *buf) {
uint32_t d, t;
int16_t a, b;
int i, j;
for (i = 0; i < KYBER_N / 8; i++) {
t = load32_littleendian(buf + 4 * i);
d = t & 0x55555555;
d += (t >> 1) & 0x55555555;
for (j = 0; j < 8; j++) {
a = (d >> 4 * j) & 0x3;
b = (d >> (4 * j + 2)) & 0x3;
r->coeffs[8 * i + j] = a - b;
}
}
}