2019-01-15 15:34:01 +00:00
|
|
|
#include "cbd.h"
|
2019-01-15 15:03:38 +00:00
|
|
|
#include "ntt.h"
|
2020-10-27 00:05:07 +00:00
|
|
|
#include "params.h"
|
2019-09-10 10:45:01 +01:00
|
|
|
#include "poly.h"
|
2019-01-15 15:03:38 +00:00
|
|
|
#include "reduce.h"
|
2019-05-06 13:50:27 +01:00
|
|
|
#include "symmetric.h"
|
|
|
|
#include <stdint.h>
|
2020-07-31 07:17:42 +01:00
|
|
|
|
2019-01-15 15:03:38 +00:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_compress
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Compression and subsequent serialization of a polynomial
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *r: pointer to output byte array
|
|
|
|
* (of length KYBER_POLYCOMPRESSEDBYTES)
|
2020-10-27 13:48:42 +00:00
|
|
|
* - const poly *a: pointer to input polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i, j;
|
2020-10-27 13:48:42 +00:00
|
|
|
int16_t u;
|
2019-05-06 13:50:27 +01:00
|
|
|
uint8_t t[8];
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N / 8; i++) {
|
|
|
|
for (j = 0; j < 8; j++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
// map to positive standard representatives
|
|
|
|
u = a->coeffs[8 * i + j];
|
|
|
|
u += (u >> 15) & KYBER_Q;
|
|
|
|
t[j] = ((((uint16_t)u << 4) + KYBER_Q / 2) / KYBER_Q) & 15;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
r[0] = t[0] | (t[1] << 4);
|
|
|
|
r[1] = t[2] | (t[3] << 4);
|
|
|
|
r[2] = t[4] | (t[5] << 4);
|
|
|
|
r[3] = t[6] | (t[7] << 4);
|
|
|
|
r += 4;
|
2019-01-16 07:18:33 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_decompress
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: De-serialization and subsequent decompression of a polynomial;
|
2020-07-31 07:17:42 +01:00
|
|
|
* approximate inverse of PQCLEAN_KYBER768_CLEAN_poly_compress
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const uint8_t *a: pointer to input byte array
|
|
|
|
* (of length KYBER_POLYCOMPRESSEDBYTES bytes)
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_decompress(poly *r, const uint8_t a[KYBER_POLYCOMPRESSEDBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
|
|
|
|
for (i = 0; i < KYBER_N / 2; i++) {
|
|
|
|
r->coeffs[2 * i + 0] = (((uint16_t)(a[0] & 15) * KYBER_Q) + 8) >> 4;
|
|
|
|
r->coeffs[2 * i + 1] = (((uint16_t)(a[0] >> 4) * KYBER_Q) + 8) >> 4;
|
|
|
|
a += 1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_tobytes
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Serialization of a polynomial
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *r: pointer to output byte array
|
|
|
|
* (needs space for KYBER_POLYBYTES bytes)
|
2020-10-27 13:48:42 +00:00
|
|
|
* - const poly *a: pointer to input polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_tobytes(uint8_t r[KYBER_POLYBYTES], const poly *a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
|
|
|
uint16_t t0, t1;
|
2019-01-16 10:02:32 +00:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N / 2; i++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
// map to positive standard representatives
|
|
|
|
t0 = a->coeffs[2 * i];
|
|
|
|
t0 += ((int16_t)t0 >> 15) & KYBER_Q;
|
2019-05-06 13:50:27 +01:00
|
|
|
t1 = a->coeffs[2 * i + 1];
|
2020-10-27 13:48:42 +00:00
|
|
|
t1 += ((int16_t)t1 >> 15) & KYBER_Q;
|
|
|
|
r[3 * i + 0] = (t0 >> 0);
|
|
|
|
r[3 * i + 1] = (t0 >> 8) | (t1 << 4);
|
|
|
|
r[3 * i + 2] = (t1 >> 4);
|
2019-01-16 07:18:33 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_frombytes
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: De-serialization of a polynomial;
|
2020-07-31 07:17:42 +01:00
|
|
|
* inverse of PQCLEAN_KYBER768_CLEAN_poly_tobytes
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const uint8_t *a: pointer to input byte array
|
|
|
|
* (of KYBER_POLYBYTES bytes)
|
|
|
|
**************************************************/
|
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_frombytes(poly *r, const uint8_t a[KYBER_POLYBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N / 2; i++) {
|
|
|
|
r->coeffs[2 * i] = ((a[3 * i + 0] >> 0) | ((uint16_t)a[3 * i + 1] << 8)) & 0xFFF;
|
|
|
|
r->coeffs[2 * i + 1] = ((a[3 * i + 1] >> 4) | ((uint16_t)a[3 * i + 2] << 4)) & 0xFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_frommsg
|
|
|
|
*
|
|
|
|
* Description: Convert 32-byte message to polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const uint8_t *msg: pointer to input message
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_frommsg(poly *r, const uint8_t msg[KYBER_INDCPA_MSGBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i, j;
|
|
|
|
int16_t mask;
|
2020-07-31 07:17:42 +01:00
|
|
|
|
|
|
|
for (i = 0; i < KYBER_N / 8; i++) {
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
mask = -(int16_t)((msg[i] >> j) & 1);
|
|
|
|
r->coeffs[8 * i + j] = mask & ((KYBER_Q + 1) / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_tomsg
|
|
|
|
*
|
|
|
|
* Description: Convert polynomial to 32-byte message
|
|
|
|
*
|
|
|
|
* Arguments: - uint8_t *msg: pointer to output message
|
2020-10-27 13:48:42 +00:00
|
|
|
* - const poly *a: pointer to input polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const poly *a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i, j;
|
|
|
|
uint16_t t;
|
2020-07-31 07:17:42 +01:00
|
|
|
|
|
|
|
for (i = 0; i < KYBER_N / 8; i++) {
|
|
|
|
msg[i] = 0;
|
|
|
|
for (j = 0; j < 8; j++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
t = a->coeffs[8 * i + j];
|
|
|
|
t += ((int16_t)t >> 15) & KYBER_Q;
|
|
|
|
t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1;
|
2020-07-31 07:17:42 +01:00
|
|
|
msg[i] |= t << j;
|
|
|
|
}
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-10-27 13:48:42 +00:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_getnoise_eta1
|
|
|
|
*
|
|
|
|
* Description: Sample a polynomial deterministically from a seed and a nonce,
|
|
|
|
* with output polynomial close to centered binomial distribution
|
|
|
|
* with parameter KYBER_ETA1
|
|
|
|
*
|
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
|
|
|
* - const uint8_t *seed: pointer to input seed
|
|
|
|
* (of length KYBER_SYMBYTES bytes)
|
|
|
|
* - uint8_t nonce: one-byte input nonce
|
|
|
|
**************************************************/
|
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_getnoise_eta1(poly *r, const uint8_t seed[KYBER_SYMBYTES], uint8_t nonce) {
|
|
|
|
uint8_t buf[KYBER_ETA1 * KYBER_N / 4];
|
|
|
|
prf(buf, sizeof(buf), seed, nonce);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_cbd_eta1(r, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_getnoise_eta2
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Sample a polynomial deterministically from a seed and a nonce,
|
|
|
|
* with output polynomial close to centered binomial distribution
|
2020-10-27 13:48:42 +00:00
|
|
|
* with parameter KYBER_ETA2
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const uint8_t *seed: pointer to input seed
|
|
|
|
* (of length KYBER_SYMBYTES bytes)
|
2020-10-27 13:48:42 +00:00
|
|
|
* - uint8_t nonce: one-byte input nonce
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_getnoise_eta2(poly *r, const uint8_t seed[KYBER_SYMBYTES], uint8_t nonce) {
|
|
|
|
uint8_t buf[KYBER_ETA2 * KYBER_N / 4];
|
2020-07-31 07:17:42 +01:00
|
|
|
prf(buf, sizeof(buf), seed, nonce);
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_cbd_eta2(r, buf);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
|
2019-01-15 15:03:38 +00:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_ntt
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Computes negacyclic number-theoretic transform (NTT) of
|
|
|
|
* a polynomial in place;
|
|
|
|
* inputs assumed to be in normal order, output in bitreversed order
|
|
|
|
*
|
|
|
|
* Arguments: - uint16_t *r: pointer to in/output polynomial
|
|
|
|
**************************************************/
|
2019-02-26 16:27:32 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_ntt(poly *r) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_ntt(r->coeffs);
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_reduce(r);
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_invntt_tomont
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Description: Computes inverse of negacyclic number-theoretic transform (NTT)
|
|
|
|
* of a polynomial in place;
|
2019-05-06 13:50:27 +01:00
|
|
|
* inputs assumed to be in bitreversed order, output in normal order
|
|
|
|
*
|
|
|
|
* Arguments: - uint16_t *a: pointer to in/output polynomial
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_invntt_tomont(poly *r) {
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_invntt(r->coeffs);
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
2019-01-15 15:34:01 +00:00
|
|
|
|
2019-01-15 15:03:38 +00:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_basemul_montgomery
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Multiplication of two polynomials in NTT domain
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
* - const poly *a: pointer to first input polynomial
|
|
|
|
* - const poly *b: pointer to second input polynomial
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_basemul_montgomery(poly *r, const poly *a, const poly *b) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N / 4; i++) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_basemul(&r->coeffs[4 * i], &a->coeffs[4 * i], &b->coeffs[4 * i], PQCLEAN_KYBER768_CLEAN_zetas[64 + i]);
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_basemul(&r->coeffs[4 * i + 2], &a->coeffs[4 * i + 2], &b->coeffs[4 * i + 2], -PQCLEAN_KYBER768_CLEAN_zetas[64 + i]);
|
2019-05-06 13:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_tomont
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Inplace conversion of all coefficients of a polynomial
|
2020-07-31 07:17:42 +01:00
|
|
|
* from normal domain to Montgomery domain
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - poly *r: pointer to input/output polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_tomont(poly *r) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2019-05-06 13:50:27 +01:00
|
|
|
const int16_t f = (1ULL << 32) % KYBER_Q;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N; i++) {
|
|
|
|
r->coeffs[i] = PQCLEAN_KYBER768_CLEAN_montgomery_reduce((int32_t)r->coeffs[i] * f);
|
2019-05-06 13:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_reduce
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Applies Barrett reduction to all coefficients of a polynomial
|
|
|
|
* for details of the Barrett reduction see comments in reduce.c
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - poly *r: pointer to input/output polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_reduce(poly *r) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N; i++) {
|
2019-05-06 13:50:27 +01:00
|
|
|
r->coeffs[i] = PQCLEAN_KYBER768_CLEAN_barrett_reduce(r->coeffs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_add
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Description: Add two polynomials; no modular reduction is performed
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
2019-05-06 13:50:27 +01:00
|
|
|
* - const poly *a: pointer to first input polynomial
|
|
|
|
* - const poly *b: pointer to second input polynomial
|
|
|
|
**************************************************/
|
2019-02-26 16:27:32 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_add(poly *r, const poly *a, const poly *b) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N; i++) {
|
2019-05-06 13:50:27 +01:00
|
|
|
r->coeffs[i] = a->coeffs[i] + b->coeffs[i];
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_poly_sub
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Description: Subtract two polynomials; no modular reduction is performed
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Arguments: - poly *r: pointer to output polynomial
|
|
|
|
* - const poly *a: pointer to first input polynomial
|
|
|
|
* - const poly *b: pointer to second input polynomial
|
|
|
|
**************************************************/
|
2019-02-26 16:27:32 +00:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_poly_sub(poly *r, const poly *a, const poly *b) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_N; i++) {
|
2019-05-06 13:50:27 +01:00
|
|
|
r->coeffs[i] = a->coeffs[i] - b->coeffs[i];
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|