mirror of
https://github.com/luxfi/pqclean.git
synced 2026-11-03 09:57:54 +00:00
112 lines
3.9 KiB
C
112 lines
3.9 KiB
C
|
|
/*
|
|
* This file is licensed
|
|
* under Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.html) or
|
|
* public domain at https://github.com/pq-crystals/kyber/tree/master/ref
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
#include "params.h"
|
|
#include "poly.h"
|
|
#include "polyvec.h"
|
|
|
|
/*************************************************
|
|
* Name: polyvec_compress
|
|
*
|
|
* Description: Compress and serialize vector of polynomials
|
|
*
|
|
* Arguments: - uint8_t *r: pointer to output byte array
|
|
* (needs space for KYBER_POLYVECCOMPRESSEDBYTES)
|
|
* - const polyvec *a: pointer to input vector of polynomials
|
|
**************************************************/
|
|
void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const int16_t b[KYBER_K][KYBER_N]) {
|
|
unsigned int i, j, k;
|
|
uint64_t d0;
|
|
|
|
uint16_t t[4];
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
for (j = 0; j < KYBER_N / 4; j++) {
|
|
for (k = 0; k < 4; k++) {
|
|
t[k] = b[i][4 * j + k];
|
|
t[k] += ((int16_t)t[k] >> 15) & KYBER_Q;
|
|
/* t[k] = ((((uint32_t)t[k] << 10) + KYBER_Q/2)/ KYBER_Q) & 0x3ff; */
|
|
d0 = t[k];
|
|
d0 <<= 10;
|
|
d0 += 1665;
|
|
d0 *= 1290167;
|
|
d0 >>= 32;
|
|
t[k] = d0 & 0x3ff;
|
|
}
|
|
|
|
r[0] = (uint8_t)(t[0] >> 0);
|
|
r[1] = (uint8_t)((t[0] >> 8) | (t[1] << 2));
|
|
r[2] = (uint8_t)((t[1] >> 6) | (t[2] << 4));
|
|
r[3] = (uint8_t)((t[2] >> 4) | (t[3] << 6));
|
|
r[4] = (uint8_t)(t[3] >> 2);
|
|
r += 5;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*************************************************
|
|
* Name: polyvec_decompress
|
|
*
|
|
* Description: De-serialize and decompress vector of polynomials;
|
|
* approximate inverse of polyvec_compress
|
|
*
|
|
* Arguments: - polyvec *r: pointer to output vector of polynomials
|
|
* - const uint8_t *a: pointer to input byte array
|
|
* (of length KYBER_POLYVECCOMPRESSEDBYTES)
|
|
**************************************************/
|
|
void polyvec_decompress(int16_t r[KYBER_K][KYBER_N], const uint8_t a[KYBER_POLYVECCOMPRESSEDBYTES]) {
|
|
unsigned int i, j, k;
|
|
|
|
uint16_t t[4];
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
for (j = 0; j < KYBER_N / 4; j++) {
|
|
t[0] = (a[0] >> 0) | ((uint16_t)a[1] << 8);
|
|
t[1] = (a[1] >> 2) | ((uint16_t)a[2] << 6);
|
|
t[2] = (a[2] >> 4) | ((uint16_t)a[3] << 4);
|
|
t[3] = (a[3] >> 6) | ((uint16_t)a[4] << 2);
|
|
a += 5;
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
r[i][4 * j + k] = ((uint32_t)(t[k] & 0x3FF) * KYBER_Q + 512) >> 10;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*************************************************
|
|
* Name: polyvec_tobytes
|
|
*
|
|
* Description: Serialize vector of polynomials
|
|
*
|
|
* Arguments: - uint8_t *r: pointer to output byte array
|
|
* (needs space for KYBER_POLYVECBYTES)
|
|
* - const polyvec *a: pointer to input vector of polynomials
|
|
**************************************************/
|
|
void polyvec_tobytes(uint8_t r[KYBER_POLYVECBYTES], const int16_t a[KYBER_K][KYBER_N]) {
|
|
unsigned int i;
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
poly_tobytes(r + i * KYBER_POLYBYTES, a[i]);
|
|
}
|
|
}
|
|
|
|
/*************************************************
|
|
* Name: polyvec_frombytes
|
|
*
|
|
* Description: De-serialize vector of polynomials;
|
|
* inverse of polyvec_tobytes
|
|
*
|
|
* Arguments: - uint8_t *r: pointer to output byte array
|
|
* - const polyvec *a: pointer to input vector of polynomials
|
|
* (of length KYBER_POLYVECBYTES)
|
|
**************************************************/
|
|
void polyvec_frombytes(int16_t r[KYBER_K][KYBER_N], const uint8_t a[KYBER_POLYVECBYTES]) {
|
|
unsigned int i;
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
poly_frombytes(r[i], a + i * KYBER_POLYBYTES);
|
|
}
|
|
}
|