2020-07-31 07:17:42 +01:00
|
|
|
#include "align.h"
|
2019-09-10 10:45:01 +01:00
|
|
|
#include "cbd.h"
|
|
|
|
#include "indcpa.h"
|
|
|
|
#include "ntt.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include "params.h"
|
2019-09-10 10:45:01 +01:00
|
|
|
#include "poly.h"
|
|
|
|
#include "polyvec.h"
|
|
|
|
#include "randombytes.h"
|
|
|
|
#include "rejsample.h"
|
|
|
|
#include "symmetric.h"
|
2020-10-27 13:48:42 +00:00
|
|
|
#include <immintrin.h>
|
2020-07-31 07:17:42 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: pack_pk
|
|
|
|
*
|
|
|
|
* Description: Serialize the public key as concatenation of the
|
2020-10-27 13:48:42 +00:00
|
|
|
* serialized vector of polynomials pk and the
|
|
|
|
* public seed used to generate the matrix A.
|
|
|
|
* The polynomial coefficients in pk are assumed to
|
|
|
|
* lie in the invertal [0,q], i.e. pk must be reduced
|
|
|
|
* by PQCLEAN_KYBER768_AVX2_polyvec_reduce().
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: uint8_t *r: pointer to the output serialized public key
|
|
|
|
* polyvec *pk: pointer to the input public-key polyvec
|
2019-09-10 10:45:01 +01:00
|
|
|
* const uint8_t *seed: pointer to the input public seed
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void pack_pk(uint8_t r[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
polyvec *pk,
|
|
|
|
const uint8_t seed[KYBER_SYMBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_tobytes(r, pk);
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_SYMBYTES; i++) {
|
2019-09-10 10:45:01 +01:00
|
|
|
r[i + KYBER_POLYVECBYTES] = seed[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: unpack_pk
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Description: De-serialize public key from a byte array;
|
2019-09-10 10:45:01 +01:00
|
|
|
* approximate inverse of pack_pk
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *pk: pointer to output public-key polynomial vector
|
|
|
|
* - uint8_t *seed: pointer to output seed to generate matrix A
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *packedpk: pointer to input serialized public key
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void unpack_pk(polyvec *pk,
|
|
|
|
uint8_t seed[KYBER_SYMBYTES],
|
|
|
|
const uint8_t packedpk[KYBER_INDCPA_PUBLICKEYBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_frombytes(pk, packedpk);
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_SYMBYTES; i++) {
|
2019-09-10 10:45:01 +01:00
|
|
|
seed[i] = packedpk[i + KYBER_POLYVECBYTES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: pack_sk
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Description: Serialize the secret key.
|
|
|
|
* The polynomial coefficients in sk are assumed to
|
|
|
|
* lie in the invertal [0,q], i.e. sk must be reduced
|
|
|
|
* by PQCLEAN_KYBER768_AVX2_polyvec_reduce().
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - uint8_t *r: pointer to output serialized secret key
|
2020-07-31 07:17:42 +01:00
|
|
|
* - polyvec *sk: pointer to input vector of polynomials (secret key)
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void pack_sk(uint8_t r[KYBER_INDCPA_SECRETKEYBYTES], polyvec *sk) {
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_tobytes(r, sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: unpack_sk
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Description: De-serialize the secret key; inverse of pack_sk
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - polyvec *sk: pointer to output vector of polynomials (secret key)
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *packedsk: pointer to input serialized secret key
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
static void unpack_sk(polyvec *sk, const uint8_t packedsk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_frombytes(sk, packedsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: pack_ciphertext
|
|
|
|
*
|
|
|
|
* Description: Serialize the ciphertext as concatenation of the
|
|
|
|
* compressed and serialized vector of polynomials b
|
2020-10-27 13:48:42 +00:00
|
|
|
* and the compressed and serialized polynomial v.
|
|
|
|
* The polynomial coefficients in b and v are assumed to
|
|
|
|
* lie in the invertal [0,q], i.e. b and v must be reduced
|
|
|
|
* by PQCLEAN_KYBER768_AVX2_polyvec_reduce() and PQCLEAN_KYBER768_AVX2_poly_reduce(), respectively.
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: uint8_t *r: pointer to the output serialized ciphertext
|
2020-10-27 13:48:42 +00:00
|
|
|
* poly *pk: pointer to the input vector of polynomials b
|
|
|
|
* poly *v: pointer to the input polynomial v
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
static void pack_ciphertext(uint8_t r[KYBER_INDCPA_BYTES], polyvec *b, poly *v) {
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_compress(r, b);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_compress(r + KYBER_POLYVECCOMPRESSEDBYTES, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: unpack_ciphertext
|
|
|
|
*
|
|
|
|
* Description: De-serialize and decompress ciphertext from a byte array;
|
|
|
|
* approximate inverse of pack_ciphertext
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - polyvec *b: pointer to the output vector of polynomials b
|
|
|
|
* - poly *v: pointer to the output polynomial v
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *c: pointer to the input serialized ciphertext
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
static void unpack_ciphertext(polyvec *b, poly *v, const uint8_t c[KYBER_INDCPA_BYTES]) {
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_decompress(b, c);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_decompress(v, c + KYBER_POLYVECCOMPRESSEDBYTES);
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/*************************************************
|
|
|
|
* Name: rej_uniform
|
|
|
|
*
|
|
|
|
* Description: Run rejection sampling on uniform random bytes to generate
|
|
|
|
* uniform random integers mod q
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - int16_t *r: pointer to output array
|
|
|
|
* - unsigned int len: requested number of 16-bit integers (uniform mod q)
|
|
|
|
* - const uint8_t *buf: pointer to input buffer (assumed to be uniformly random bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - unsigned int buflen: length of input buffer in bytes
|
|
|
|
*
|
|
|
|
* Returns number of sampled 16-bit integers (at most len)
|
|
|
|
**************************************************/
|
|
|
|
static unsigned int rej_uniform(int16_t *r,
|
|
|
|
unsigned int len,
|
|
|
|
const uint8_t *buf,
|
|
|
|
unsigned int buflen) {
|
2020-10-27 00:05:07 +00:00
|
|
|
unsigned int ctr, pos;
|
2020-10-27 13:48:42 +00:00
|
|
|
uint16_t val0, val1;
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
ctr = pos = 0;
|
2020-10-27 13:48:42 +00:00
|
|
|
while (ctr < len && pos + 3 <= buflen) {
|
|
|
|
val0 = ((buf[pos + 0] >> 0) | ((uint16_t)buf[pos + 1] << 8)) & 0xFFF;
|
|
|
|
val1 = ((buf[pos + 1] >> 4) | ((uint16_t)buf[pos + 2] << 4)) & 0xFFF;
|
|
|
|
pos += 3;
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
if (val0 < KYBER_Q) {
|
|
|
|
r[ctr++] = val0;
|
|
|
|
}
|
|
|
|
if (ctr < len && val1 < KYBER_Q) {
|
|
|
|
r[ctr++] = val1;
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
#define gen_a(A,B) PQCLEAN_KYBER768_AVX2_gen_matrix(A,B,0)
|
|
|
|
#define gen_at(A,B) PQCLEAN_KYBER768_AVX2_gen_matrix(A,B,1)
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_AVX2_gen_matrix
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
|
|
|
* Description: Deterministically generate matrix A (or the transpose of A)
|
|
|
|
* from a seed. Entries of the matrix are polynomials that look
|
|
|
|
* uniformly random. Performs rejection sampling on output of
|
|
|
|
* a XOF
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *a: pointer to ouptput matrix A
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *seed: pointer to input seed
|
2020-07-31 07:17:42 +01:00
|
|
|
* - int transposed: boolean deciding whether A or A^T is generated
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_AVX2_gen_matrix(polyvec *a, const uint8_t seed[32], int transposed) {
|
2020-10-27 00:05:07 +00:00
|
|
|
unsigned int ctr0, ctr1, ctr2, ctr3;
|
2020-10-27 13:48:42 +00:00
|
|
|
ALIGNED_UINT8(REJ_UNIFORM_AVX_NBLOCKS * SHAKE128_RATE) buf[4];
|
2020-07-31 07:17:42 +01:00
|
|
|
__m256i f;
|
|
|
|
keccakx4_state state;
|
|
|
|
xof_state state1x;
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
f = _mm256_loadu_si256((__m256i *)seed);
|
|
|
|
_mm256_store_si256(buf[0].vec, f);
|
|
|
|
_mm256_store_si256(buf[1].vec, f);
|
|
|
|
_mm256_store_si256(buf[2].vec, f);
|
|
|
|
_mm256_store_si256(buf[3].vec, f);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
if (transposed) {
|
2020-10-27 13:48:42 +00:00
|
|
|
buf[0].coeffs[32] = 0;
|
|
|
|
buf[0].coeffs[33] = 0;
|
|
|
|
buf[1].coeffs[32] = 0;
|
|
|
|
buf[1].coeffs[33] = 1;
|
|
|
|
buf[2].coeffs[32] = 0;
|
|
|
|
buf[2].coeffs[33] = 2;
|
|
|
|
buf[3].coeffs[32] = 1;
|
|
|
|
buf[3].coeffs[33] = 0;
|
2019-09-10 10:45:01 +01:00
|
|
|
} else {
|
2020-10-27 13:48:42 +00:00
|
|
|
buf[0].coeffs[32] = 0;
|
|
|
|
buf[0].coeffs[33] = 0;
|
|
|
|
buf[1].coeffs[32] = 1;
|
|
|
|
buf[1].coeffs[33] = 0;
|
|
|
|
buf[2].coeffs[32] = 2;
|
|
|
|
buf[2].coeffs[33] = 0;
|
|
|
|
buf[3].coeffs[32] = 0;
|
|
|
|
buf[3].coeffs[33] = 1;
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_absorb_once(&state, buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 34);
|
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, REJ_UNIFORM_AVX_NBLOCKS, &state);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
ctr0 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[0].vec[0].coeffs, buf[0].coeffs);
|
|
|
|
ctr1 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[0].vec[1].coeffs, buf[1].coeffs);
|
|
|
|
ctr2 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[0].vec[2].coeffs, buf[2].coeffs);
|
|
|
|
ctr3 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[1].vec[0].coeffs, buf[3].coeffs);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
while (ctr0 < KYBER_N || ctr1 < KYBER_N || ctr2 < KYBER_N || ctr3 < KYBER_N) {
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 1, &state);
|
|
|
|
|
|
|
|
ctr0 += rej_uniform(a[0].vec[0].coeffs + ctr0, KYBER_N - ctr0, buf[0].coeffs, SHAKE128_RATE);
|
|
|
|
ctr1 += rej_uniform(a[0].vec[1].coeffs + ctr1, KYBER_N - ctr1, buf[1].coeffs, SHAKE128_RATE);
|
|
|
|
ctr2 += rej_uniform(a[0].vec[2].coeffs + ctr2, KYBER_N - ctr2, buf[2].coeffs, SHAKE128_RATE);
|
|
|
|
ctr3 += rej_uniform(a[1].vec[0].coeffs + ctr3, KYBER_N - ctr3, buf[3].coeffs, SHAKE128_RATE);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[0].vec[0]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[0].vec[1]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[0].vec[2]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[1].vec[0]);
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
f = _mm256_loadu_si256((__m256i *)seed);
|
|
|
|
_mm256_store_si256(buf[0].vec, f);
|
|
|
|
_mm256_store_si256(buf[1].vec, f);
|
|
|
|
_mm256_store_si256(buf[2].vec, f);
|
|
|
|
_mm256_store_si256(buf[3].vec, f);
|
2020-07-31 07:17:42 +01:00
|
|
|
|
2019-09-10 10:45:01 +01:00
|
|
|
if (transposed) {
|
2020-10-27 13:48:42 +00:00
|
|
|
buf[0].coeffs[32] = 1;
|
|
|
|
buf[0].coeffs[33] = 1;
|
|
|
|
buf[1].coeffs[32] = 1;
|
|
|
|
buf[1].coeffs[33] = 2;
|
|
|
|
buf[2].coeffs[32] = 2;
|
|
|
|
buf[2].coeffs[33] = 0;
|
|
|
|
buf[3].coeffs[32] = 2;
|
|
|
|
buf[3].coeffs[33] = 1;
|
2019-09-10 10:45:01 +01:00
|
|
|
} else {
|
2020-10-27 13:48:42 +00:00
|
|
|
buf[0].coeffs[32] = 1;
|
|
|
|
buf[0].coeffs[33] = 1;
|
|
|
|
buf[1].coeffs[32] = 2;
|
|
|
|
buf[1].coeffs[33] = 1;
|
|
|
|
buf[2].coeffs[32] = 0;
|
|
|
|
buf[2].coeffs[33] = 2;
|
|
|
|
buf[3].coeffs[32] = 1;
|
|
|
|
buf[3].coeffs[33] = 2;
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_absorb_once(&state, buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 34);
|
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, REJ_UNIFORM_AVX_NBLOCKS, &state);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
ctr0 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[1].vec[1].coeffs, buf[0].coeffs);
|
|
|
|
ctr1 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[1].vec[2].coeffs, buf[1].coeffs);
|
|
|
|
ctr2 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[2].vec[0].coeffs, buf[2].coeffs);
|
|
|
|
ctr3 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[2].vec[1].coeffs, buf[3].coeffs);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
while (ctr0 < KYBER_N || ctr1 < KYBER_N || ctr2 < KYBER_N || ctr3 < KYBER_N) {
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_shake128x4_squeezeblocks(buf[0].coeffs, buf[1].coeffs, buf[2].coeffs, buf[3].coeffs, 1, &state);
|
|
|
|
|
|
|
|
ctr0 += rej_uniform(a[1].vec[1].coeffs + ctr0, KYBER_N - ctr0, buf[0].coeffs, SHAKE128_RATE);
|
|
|
|
ctr1 += rej_uniform(a[1].vec[2].coeffs + ctr1, KYBER_N - ctr1, buf[1].coeffs, SHAKE128_RATE);
|
|
|
|
ctr2 += rej_uniform(a[2].vec[0].coeffs + ctr2, KYBER_N - ctr2, buf[2].coeffs, SHAKE128_RATE);
|
|
|
|
ctr3 += rej_uniform(a[2].vec[1].coeffs + ctr3, KYBER_N - ctr3, buf[3].coeffs, SHAKE128_RATE);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[1].vec[1]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[1].vec[2]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[2].vec[0]);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[2].vec[1]);
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
f = _mm256_loadu_si256((__m256i *)seed);
|
|
|
|
_mm256_store_si256(buf[0].vec, f);
|
|
|
|
buf[0].coeffs[32] = 2;
|
|
|
|
buf[0].coeffs[33] = 2;
|
|
|
|
shake128_absorb(&state1x, buf[0].coeffs, 34);
|
|
|
|
shake128_squeezeblocks(buf[0].coeffs, REJ_UNIFORM_AVX_NBLOCKS, &state1x);
|
|
|
|
ctr0 = PQCLEAN_KYBER768_AVX2_rej_uniform_avx(a[2].vec[2].coeffs, buf[0].coeffs);
|
2019-09-10 10:45:01 +01:00
|
|
|
while (ctr0 < KYBER_N) {
|
2020-10-27 13:48:42 +00:00
|
|
|
shake128_squeezeblocks(buf[0].coeffs, 1, &state1x);
|
|
|
|
ctr0 += rej_uniform(a[2].vec[2].coeffs + ctr0, KYBER_N - ctr0, buf[0].coeffs, SHAKE128_RATE);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
2020-10-27 13:48:42 +00:00
|
|
|
xof_ctx_release(&state1x);
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_nttunpack(&a[2].vec[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_AVX2_indcpa_keypair
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
|
|
|
* Description: Generates public and private key for the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *pk: pointer to output public key
|
|
|
|
* (of length KYBER_INDCPA_PUBLICKEYBYTES bytes)
|
|
|
|
* - uint8_t *sk: pointer to output private key
|
|
|
|
(of length KYBER_INDCPA_SECRETKEYBYTES bytes)
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_AVX2_indcpa_keypair(uint8_t pk[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
uint8_t sk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
unsigned int i;
|
2020-10-27 13:48:42 +00:00
|
|
|
uint8_t buf[2 * KYBER_SYMBYTES];
|
|
|
|
const uint8_t *publicseed = buf;
|
|
|
|
const uint8_t *noiseseed = buf + KYBER_SYMBYTES;
|
2020-07-31 07:17:42 +01:00
|
|
|
polyvec a[KYBER_K], e, pkpv, skpv;
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
randombytes(buf, KYBER_SYMBYTES);
|
|
|
|
hash_g(buf, buf, KYBER_SYMBYTES);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
gen_a(a, publicseed);
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_getnoise_eta1_4x(skpv.vec + 0, skpv.vec + 1, skpv.vec + 2, e.vec + 0, noiseseed, 0, 1, 2, 3);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_getnoise_eta1_4x(e.vec + 1, e.vec + 2, pkpv.vec + 0, pkpv.vec + 1, noiseseed, 4, 5, 6, 7);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_ntt(&skpv);
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_reduce(&skpv);
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_ntt(&e);
|
|
|
|
|
|
|
|
// matrix-vector multiplication
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_basemul_acc_montgomery(&pkpv.vec[i], &a[i], &skpv);
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_tomont(&pkpv.vec[i]);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_add(&pkpv, &pkpv, &e);
|
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_reduce(&pkpv);
|
|
|
|
|
|
|
|
pack_sk(sk, &skpv);
|
|
|
|
pack_pk(pk, &pkpv, publicseed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_AVX2_indcpa_enc
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
|
|
|
* Description: Encryption function of the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber.
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - uint8_t *c: pointer to output ciphertext
|
|
|
|
* (of length KYBER_INDCPA_BYTES bytes)
|
|
|
|
* - const uint8_t *m: pointer to input message
|
|
|
|
* (of length KYBER_INDCPA_MSGBYTES bytes)
|
|
|
|
* - const uint8_t *pk: pointer to input public key
|
|
|
|
* (of length KYBER_INDCPA_PUBLICKEYBYTES)
|
|
|
|
* - const uint8_t *coins: pointer to input random coins used as seed
|
|
|
|
* (of length KYBER_SYMBYTES) to deterministically
|
|
|
|
* generate all randomness
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_AVX2_indcpa_enc(uint8_t c[KYBER_INDCPA_BYTES],
|
|
|
|
const uint8_t m[KYBER_INDCPA_MSGBYTES],
|
|
|
|
const uint8_t pk[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
const uint8_t coins[KYBER_SYMBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
unsigned int i;
|
2020-10-27 13:48:42 +00:00
|
|
|
uint8_t seed[KYBER_SYMBYTES];
|
|
|
|
polyvec sp, pkpv, ep, at[KYBER_K], b;
|
2020-07-31 07:17:42 +01:00
|
|
|
poly v, k, epp;
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
unpack_pk(&pkpv, seed, pk);
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_frommsg(&k, m);
|
2020-10-27 13:48:42 +00:00
|
|
|
gen_at(at, seed);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_getnoise_eta1_4x(sp.vec + 0, sp.vec + 1, sp.vec + 2, ep.vec + 0, coins, 0, 1, 2, 3);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_getnoise_eta1_4x(ep.vec + 1, ep.vec + 2, &epp, b.vec + 0, coins, 4, 5, 6, 7);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_ntt(&sp);
|
|
|
|
|
|
|
|
// matrix-vector multiplication
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_basemul_acc_montgomery(&b.vec[i], &at[i], &sp);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_basemul_acc_montgomery(&v, &pkpv, &sp);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_invntt_tomont(&b);
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_invntt_tomont(&v);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_add(&b, &b, &ep);
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_add(&v, &v, &epp);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_add(&v, &v, &k);
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_reduce(&b);
|
2019-09-10 10:45:01 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_reduce(&v);
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
pack_ciphertext(c, &b, &v);
|
2019-09-10 10:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_AVX2_indcpa_dec
|
2019-09-10 10:45:01 +01:00
|
|
|
*
|
|
|
|
* Description: Decryption function of the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber.
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Arguments: - uint8_t *m: pointer to output decrypted message
|
|
|
|
* (of length KYBER_INDCPA_MSGBYTES)
|
|
|
|
* - const uint8_t *c: pointer to input ciphertext
|
|
|
|
* (of length KYBER_INDCPA_BYTES)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const uint8_t *sk: pointer to input secret key
|
|
|
|
* (of length KYBER_INDCPA_SECRETKEYBYTES)
|
2019-09-10 10:45:01 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_AVX2_indcpa_dec(uint8_t m[KYBER_INDCPA_MSGBYTES],
|
|
|
|
const uint8_t c[KYBER_INDCPA_BYTES],
|
|
|
|
const uint8_t sk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
2020-10-27 13:48:42 +00:00
|
|
|
polyvec b, skpv;
|
2019-09-10 10:45:01 +01:00
|
|
|
poly v, mp;
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
unpack_ciphertext(&b, &v, c);
|
2019-09-10 10:45:01 +01:00
|
|
|
unpack_sk(&skpv, sk);
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_ntt(&b);
|
|
|
|
PQCLEAN_KYBER768_AVX2_polyvec_basemul_acc_montgomery(&mp, &skpv, &b);
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_AVX2_poly_invntt_tomont(&mp);
|
2019-09-10 10:45:01 +01:00
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_sub(&mp, &v, &mp);
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_reduce(&mp);
|
|
|
|
|
|
|
|
PQCLEAN_KYBER768_AVX2_poly_tomsg(m, &mp);
|
|
|
|
}
|