2019-06-11 10:18:05 +01:00
|
|
|
#include "packing.h"
|
|
|
|
#include "params.h"
|
|
|
|
#include "poly.h"
|
|
|
|
#include "polyvec.h"
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_pack_pk
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Bit-pack public key pk = (rho, t1).
|
|
|
|
*
|
2019-12-06 15:16:41 +00:00
|
|
|
* Arguments: - uint8_t pk[]: output byte array
|
|
|
|
* - const uint8_t rho[]: byte array containing rho
|
2019-06-11 10:18:05 +01:00
|
|
|
* - const polyveck *t1: pointer to vector t1
|
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
void PQCLEAN_DILITHIUM4_CLEAN_pack_pk(
|
|
|
|
uint8_t *pk,
|
|
|
|
const uint8_t *rho,
|
|
|
|
const polyveck *t1) {
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
pk[i] = rho[i];
|
|
|
|
}
|
|
|
|
pk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyt1_pack(pk + i * POLT1_SIZE_PACKED, &t1->vec[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_unpack_pk
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Unpack public key pk = (rho, t1).
|
|
|
|
*
|
2019-12-06 15:16:41 +00:00
|
|
|
* Arguments: - const uint8_t rho[]: output byte array for rho
|
2019-06-11 10:18:05 +01:00
|
|
|
* - const polyveck *t1: pointer to output vector t1
|
2019-12-06 15:16:41 +00:00
|
|
|
* - uint8_t pk[]: byte array containing bit-packed pk
|
2019-06-11 10:18:05 +01:00
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
void PQCLEAN_DILITHIUM4_CLEAN_unpack_pk(
|
|
|
|
uint8_t *rho,
|
|
|
|
polyveck *t1,
|
|
|
|
const uint8_t *pk) {
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
rho[i] = pk[i];
|
|
|
|
}
|
|
|
|
pk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyt1_unpack(&t1->vec[i], pk + i * POLT1_SIZE_PACKED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_pack_sk
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Bit-pack secret key sk = (rho, key, tr, s1, s2, t0).
|
|
|
|
*
|
2019-12-06 15:16:41 +00:00
|
|
|
* Arguments: - uint8_t sk[]: output byte array
|
|
|
|
* - const uint8_t rho[]: byte array containing rho
|
|
|
|
* - const uint8_t key[]: byte array containing key
|
|
|
|
* - const uint8_t tr[]: byte array containing tr
|
2019-06-11 10:18:05 +01:00
|
|
|
* - const polyvecl *s1: pointer to vector s1
|
|
|
|
* - const polyveck *s2: pointer to vector s2
|
|
|
|
* - const polyveck *t0: pointer to vector t0
|
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
void PQCLEAN_DILITHIUM4_CLEAN_pack_sk(
|
|
|
|
uint8_t *sk,
|
|
|
|
const uint8_t *rho,
|
|
|
|
const uint8_t *key,
|
|
|
|
const uint8_t *tr,
|
|
|
|
const polyvecl *s1,
|
|
|
|
const polyveck *s2,
|
|
|
|
const polyveck *t0) {
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
sk[i] = rho[i];
|
|
|
|
}
|
|
|
|
sk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
sk[i] = key[i];
|
|
|
|
}
|
|
|
|
sk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < CRHBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
sk[i] = tr[i];
|
|
|
|
}
|
|
|
|
sk += CRHBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < L; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyeta_pack(sk + i * POLETA_SIZE_PACKED, &s1->vec[i]);
|
|
|
|
}
|
|
|
|
sk += L * POLETA_SIZE_PACKED;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyeta_pack(sk + i * POLETA_SIZE_PACKED, &s2->vec[i]);
|
|
|
|
}
|
|
|
|
sk += K * POLETA_SIZE_PACKED;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyt0_pack(sk + i * POLT0_SIZE_PACKED, &t0->vec[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_unpack_sk
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Unpack secret key sk = (rho, key, tr, s1, s2, t0).
|
|
|
|
*
|
2019-12-06 15:16:41 +00:00
|
|
|
* Arguments: - const uint8_t rho[]: output byte array for rho
|
|
|
|
* - const uint8_t key[]: output byte array for key
|
|
|
|
* - const uint8_t tr[]: output byte array for tr
|
2019-06-11 10:18:05 +01:00
|
|
|
* - const polyvecl *s1: pointer to output vector s1
|
|
|
|
* - const polyveck *s2: pointer to output vector s2
|
|
|
|
* - const polyveck *r0: pointer to output vector t0
|
2019-12-06 15:16:41 +00:00
|
|
|
* - uint8_t sk[]: byte array containing bit-packed sk
|
2019-06-11 10:18:05 +01:00
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
void PQCLEAN_DILITHIUM4_CLEAN_unpack_sk(
|
|
|
|
uint8_t *rho,
|
|
|
|
uint8_t *key,
|
|
|
|
uint8_t *tr,
|
|
|
|
polyvecl *s1,
|
|
|
|
polyveck *s2,
|
|
|
|
polyveck *t0,
|
|
|
|
const uint8_t *sk) {
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
rho[i] = sk[i];
|
|
|
|
}
|
|
|
|
sk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < SEEDBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
key[i] = sk[i];
|
|
|
|
}
|
|
|
|
sk += SEEDBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < CRHBYTES; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
tr[i] = sk[i];
|
|
|
|
}
|
|
|
|
sk += CRHBYTES;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < L; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyeta_unpack(&s1->vec[i], sk + i * POLETA_SIZE_PACKED);
|
|
|
|
}
|
|
|
|
sk += L * POLETA_SIZE_PACKED;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyeta_unpack(&s2->vec[i], sk + i * POLETA_SIZE_PACKED);
|
|
|
|
}
|
|
|
|
sk += K * POLETA_SIZE_PACKED;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyt0_unpack(&t0->vec[i], sk + i * POLT0_SIZE_PACKED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_pack_sig
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Bit-pack signature sig = (z, h, c).
|
|
|
|
*
|
2019-12-06 15:16:41 +00:00
|
|
|
* Arguments: - uint8_t sig[]: output byte array
|
2019-06-11 10:18:05 +01:00
|
|
|
* - const polyvecl *z: pointer to vector z
|
|
|
|
* - const polyveck *h: pointer to hint vector h
|
2019-12-12 12:51:46 +00:00
|
|
|
* - const poly *c: pointer to challenge polynomial
|
2019-06-11 10:18:05 +01:00
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
void PQCLEAN_DILITHIUM4_CLEAN_pack_sig(
|
|
|
|
uint8_t *sig,
|
|
|
|
const polyvecl *z,
|
|
|
|
const polyveck *h,
|
|
|
|
const poly *c) {
|
2019-12-09 11:05:25 +00:00
|
|
|
size_t k;
|
2019-06-11 10:18:05 +01:00
|
|
|
uint64_t signs, mask;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < L; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyz_pack(sig + i * POLZ_SIZE_PACKED, &z->vec[i]);
|
|
|
|
}
|
|
|
|
sig += L * POLZ_SIZE_PACKED;
|
|
|
|
|
|
|
|
/* Encode h */
|
|
|
|
k = 0;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
if (h->vec[i].coeffs[j] != 0) {
|
2019-12-06 15:16:41 +00:00
|
|
|
sig[k++] = (uint8_t)j;
|
2019-06-11 10:18:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-06 15:16:41 +00:00
|
|
|
|
|
|
|
sig[OMEGA + i] = (uint8_t)k;
|
2019-06-11 10:18:05 +01:00
|
|
|
}
|
|
|
|
while (k < OMEGA) {
|
|
|
|
sig[k++] = 0;
|
|
|
|
}
|
|
|
|
sig += OMEGA + K;
|
|
|
|
|
|
|
|
/* Encode c */
|
|
|
|
signs = 0;
|
|
|
|
mask = 1;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < N / 8; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
sig[i] = 0;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t j = 0; j < 8; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
if (c->coeffs[8 * i + j] != 0) {
|
2019-12-06 15:16:41 +00:00
|
|
|
sig[i] |= (uint8_t)(1u << j);
|
2019-06-11 10:18:05 +01:00
|
|
|
if (c->coeffs[8 * i + j] == (Q - 1)) {
|
|
|
|
signs |= mask;
|
|
|
|
}
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sig += N / 8;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < 8; ++i) {
|
2019-12-06 15:16:41 +00:00
|
|
|
sig[i] = (uint8_t)(signs >> 8u * i);
|
2019-06-11 10:18:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-12-06 15:16:41 +00:00
|
|
|
* Name: PQCLEAN_DILITHIUM4_CLEAN_unpack_sig
|
2019-06-11 10:18:05 +01:00
|
|
|
*
|
|
|
|
* Description: Unpack signature sig = (z, h, c).
|
|
|
|
*
|
|
|
|
* Arguments: - polyvecl *z: pointer to output vector z
|
|
|
|
* - polyveck *h: pointer to output hint vector h
|
2019-12-12 12:51:46 +00:00
|
|
|
* - poly *c: pointer to output challenge polynomial
|
2019-12-06 15:16:41 +00:00
|
|
|
* - const uint8_t sig[]: byte array containing
|
2019-06-11 10:18:05 +01:00
|
|
|
* bit-packed signature
|
|
|
|
*
|
|
|
|
* Returns 1 in case of malformed signature; otherwise 0.
|
|
|
|
**************************************************/
|
2019-12-06 15:16:41 +00:00
|
|
|
int PQCLEAN_DILITHIUM4_CLEAN_unpack_sig(
|
|
|
|
polyvecl *z,
|
|
|
|
polyveck *h,
|
|
|
|
poly *c,
|
|
|
|
const uint8_t *sig) {
|
2019-12-09 11:05:25 +00:00
|
|
|
size_t k;
|
2019-06-11 10:18:05 +01:00
|
|
|
uint64_t signs;
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < L; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
PQCLEAN_DILITHIUM4_CLEAN_polyz_unpack(&z->vec[i], sig + i * POLZ_SIZE_PACKED);
|
|
|
|
}
|
|
|
|
sig += L * POLZ_SIZE_PACKED;
|
|
|
|
|
|
|
|
/* Decode h */
|
|
|
|
k = 0;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < K; ++i) {
|
|
|
|
for (size_t j = 0; j < N; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
h->vec[i].coeffs[j] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig[OMEGA + i] < k || sig[OMEGA + i] > OMEGA) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t j = k; j < sig[OMEGA + i]; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
/* Coefficients are ordered for strong unforgeability */
|
|
|
|
if (j > k && sig[j] <= sig[j - 1]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
h->vec[i].coeffs[sig[j]] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = sig[OMEGA + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extra indices are zero for strong unforgeability */
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t j = k; j < OMEGA; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
if (sig[j]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 15:16:41 +00:00
|
|
|
|
2019-06-11 10:18:05 +01:00
|
|
|
sig += OMEGA + K;
|
|
|
|
|
|
|
|
/* Decode c */
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < N; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
c->coeffs[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
signs = 0;
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < 8; ++i) {
|
2019-06-11 10:18:05 +01:00
|
|
|
signs |= (uint64_t)sig[N / 8 + i] << 8 * i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extra sign bits are zero for strong unforgeability */
|
|
|
|
if (signs >> 60) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:05:25 +00:00
|
|
|
for (size_t i = 0; i < N / 8; ++i) {
|
|
|
|
for (size_t j = 0; j < 8; ++j) {
|
2019-06-11 10:18:05 +01:00
|
|
|
if ((sig[i] >> j) & 0x01) {
|
|
|
|
c->coeffs[8 * i + j] = 1;
|
|
|
|
c->coeffs[8 * i + j] ^= -((int32_t) signs & 1) & (1 ^ (Q - 1));
|
|
|
|
signs >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|