| 1 |
/* dilithium-dep.c - the Dilithium (DILITHIUM_MODE dependent part) |
| 2 |
* Copyright (C) 2025 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file was modified for use by Libgcrypt. |
| 5 |
* |
| 6 |
* This file is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This file is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
* |
| 20 |
* You can also use this file under the same licence of original code. |
| 21 |
* SPDX-License-Identifier: CC0 OR Apache-2.0 |
| 22 |
* |
| 23 |
*/ |
| 24 |
/* |
| 25 |
Original code from: |
| 26 |
|
| 27 |
Repository: https://github.com/pq-crystals/dilithium.git |
| 28 |
Branch: master |
| 29 |
Commit: 444cdcc84eb36b66fe27b3a2529ee48f6d8150c2 |
| 30 |
|
| 31 |
Licence: |
| 32 |
Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/); |
| 33 |
or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html). |
| 34 |
|
| 35 |
Authors: |
| 36 |
Léo Ducas |
| 37 |
Eike Kiltz |
| 38 |
Tancrède Lepoint |
| 39 |
Vadim Lyubashevsky |
| 40 |
Gregor Seiler |
| 41 |
Peter Schwabe |
| 42 |
Damien Stehlé |
| 43 |
|
| 44 |
Dilithium Home: https://github.com/pq-crystals/dilithium.git |
| 45 |
*/ |
| 46 |
/*************** dilithium/ref/polyvec.h */ |
| 47 |
/* Vectors of polynomials of length L */ |
| 48 |
typedef struct { |
| 49 |
poly vec[L]; |
| 50 |
} polyvecl; |
| 51 |
|
| 52 |
static void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); |
| 53 |
|
| 54 |
static void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce); |
| 55 |
|
| 56 |
static void polyvecl_reduce(polyvecl *v); |
| 57 |
|
| 58 |
static void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v); |
| 59 |
|
| 60 |
static void polyvecl_ntt(polyvecl *v); |
| 61 |
static void polyvecl_invntt_tomont(polyvecl *v); |
| 62 |
static void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v); |
| 63 |
static void polyvecl_pointwise_acc_montgomery(poly *w, |
| 64 |
const polyvecl *u, |
| 65 |
const polyvecl *v); |
| 66 |
|
| 67 |
|
| 68 |
static int polyvecl_chknorm(const polyvecl *v, int32_t B); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
/* Vectors of polynomials of length K */ |
| 73 |
typedef struct { |
| 74 |
poly vec[K]; |
| 75 |
} polyveck; |
| 76 |
|
| 77 |
static void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce); |
| 78 |
|
| 79 |
static void polyveck_reduce(polyveck *v); |
| 80 |
static void polyveck_caddq(polyveck *v); |
| 81 |
|
| 82 |
static void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v); |
| 83 |
static void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v); |
| 84 |
static void polyveck_shiftl(polyveck *v); |
| 85 |
|
| 86 |
static void polyveck_ntt(polyveck *v); |
| 87 |
static void polyveck_invntt_tomont(polyveck *v); |
| 88 |
static void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v); |
| 89 |
|
| 90 |
static int polyveck_chknorm(const polyveck *v, int32_t B); |
| 91 |
|
| 92 |
static void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v); |
| 93 |
static void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v); |
| 94 |
static unsigned int polyveck_make_hint(polyveck *h, |
| 95 |
const polyveck *v0, |
| 96 |
const polyveck *v1); |
| 97 |
static void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h); |
| 98 |
|
| 99 |
static void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1); |
| 100 |
|
| 101 |
static void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]); |
| 102 |
|
| 103 |
static void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v); |
| 104 |
|
| 105 |
/*************** dilithium/ref/packing.h */ |
| 106 |
static void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], const uint8_t rho[SEEDBYTES], const polyveck *t1); |
| 107 |
|
| 108 |
static void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], |
| 109 |
const uint8_t rho[SEEDBYTES], |
| 110 |
const uint8_t tr[TRBYTES], |
| 111 |
const uint8_t key[SEEDBYTES], |
| 112 |
const polyveck *t0, |
| 113 |
const polyvecl *s1, |
| 114 |
const polyveck *s2); |
| 115 |
|
| 116 |
static void pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[CTILDEBYTES], const polyvecl *z, const polyveck *h); |
| 117 |
|
| 118 |
static void unpack_pk(uint8_t rho[SEEDBYTES], polyveck *t1, const uint8_t pk[CRYPTO_PUBLICKEYBYTES]); |
| 119 |
|
| 120 |
static void unpack_sk(uint8_t rho[SEEDBYTES], |
| 121 |
uint8_t tr[TRBYTES], |
| 122 |
uint8_t key[SEEDBYTES], |
| 123 |
polyveck *t0, |
| 124 |
polyvecl *s1, |
| 125 |
polyveck *s2, |
| 126 |
const uint8_t sk[CRYPTO_SECRETKEYBYTES]); |
| 127 |
|
| 128 |
static int unpack_sig(uint8_t c[CTILDEBYTES], polyvecl *z, polyveck *h, const uint8_t sig[CRYPTO_BYTES]); |
| 129 |
|
| 130 |
/*************** dilithium/ref/packing.c */ |
| 131 |
|
| 132 |
/************************************************* |
| 133 |
* Name: pack_pk |
| 134 |
* |
| 135 |
* Description: Bit-pack public key pk = (rho, t1). |
| 136 |
* |
| 137 |
* Arguments: - uint8_t pk[]: output byte array |
| 138 |
* - const uint8_t rho[]: byte array containing rho |
| 139 |
* - const polyveck *t1: pointer to vector t1 |
| 140 |
**************************************************/ |
| 141 |
void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES], |
| 142 |
const uint8_t rho[SEEDBYTES], |
| 143 |
const polyveck *t1) |
| 144 |
{ |
| 145 |
unsigned int i; |
| 146 |
|
| 147 |
for(i = 0; i < SEEDBYTES; ++i) |
| 148 |
pk[i] = rho[i]; |
| 149 |
pk += SEEDBYTES; |
| 150 |
|
| 151 |
for(i = 0; i < K; ++i) |
| 152 |
polyt1_pack(pk + i*POLYT1_PACKEDBYTES, &t1->vec[i]); |
| 153 |
} |
| 154 |
|
| 155 |
/************************************************* |
| 156 |
* Name: unpack_pk |
| 157 |
* |
| 158 |
* Description: Unpack public key pk = (rho, t1). |
| 159 |
* |
| 160 |
* Arguments: - const uint8_t rho[]: output byte array for rho |
| 161 |
* - const polyveck *t1: pointer to output vector t1 |
| 162 |
* - uint8_t pk[]: byte array containing bit-packed pk |
| 163 |
**************************************************/ |
| 164 |
void unpack_pk(uint8_t rho[SEEDBYTES], |
| 165 |
polyveck *t1, |
| 166 |
const uint8_t pk[CRYPTO_PUBLICKEYBYTES]) |
| 167 |
{ |
| 168 |
unsigned int i; |
| 169 |
|
| 170 |
for(i = 0; i < SEEDBYTES; ++i) |
| 171 |
rho[i] = pk[i]; |
| 172 |
pk += SEEDBYTES; |
| 173 |
|
| 174 |
for(i = 0; i < K; ++i) |
| 175 |
polyt1_unpack(&t1->vec[i], pk + i*POLYT1_PACKEDBYTES); |
| 176 |
} |
| 177 |
|
| 178 |
/************************************************* |
| 179 |
* Name: pack_sk |
| 180 |
* |
| 181 |
* Description: Bit-pack secret key sk = (rho, tr, key, t0, s1, s2). |
| 182 |
* |
| 183 |
* Arguments: - uint8_t sk[]: output byte array |
| 184 |
* - const uint8_t rho[]: byte array containing rho |
| 185 |
* - const uint8_t tr[]: byte array containing tr |
| 186 |
* - const uint8_t key[]: byte array containing key |
| 187 |
* - const polyveck *t0: pointer to vector t0 |
| 188 |
* - const polyvecl *s1: pointer to vector s1 |
| 189 |
* - const polyveck *s2: pointer to vector s2 |
| 190 |
**************************************************/ |
| 191 |
void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES], |
| 192 |
const uint8_t rho[SEEDBYTES], |
| 193 |
const uint8_t tr[TRBYTES], |
| 194 |
const uint8_t key[SEEDBYTES], |
| 195 |
const polyveck *t0, |
| 196 |
const polyvecl *s1, |
| 197 |
const polyveck *s2) |
| 198 |
{ |
| 199 |
unsigned int i; |
| 200 |
|
| 201 |
for(i = 0; i < SEEDBYTES; ++i) |
| 202 |
sk[i] = rho[i]; |
| 203 |
sk += SEEDBYTES; |
| 204 |
|
| 205 |
for(i = 0; i < SEEDBYTES; ++i) |
| 206 |
sk[i] = key[i]; |
| 207 |
sk += SEEDBYTES; |
| 208 |
|
| 209 |
for(i = 0; i < TRBYTES; ++i) |
| 210 |
sk[i] = tr[i]; |
| 211 |
sk += TRBYTES; |
| 212 |
|
| 213 |
for(i = 0; i < L; ++i) |
| 214 |
polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s1->vec[i]); |
| 215 |
sk += L*POLYETA_PACKEDBYTES; |
| 216 |
|
| 217 |
for(i = 0; i < K; ++i) |
| 218 |
polyeta_pack(sk + i*POLYETA_PACKEDBYTES, &s2->vec[i]); |
| 219 |
sk += K*POLYETA_PACKEDBYTES; |
| 220 |
|
| 221 |
for(i = 0; i < K; ++i) |
| 222 |
polyt0_pack(sk + i*POLYT0_PACKEDBYTES, &t0->vec[i]); |
| 223 |
} |
| 224 |
|
| 225 |
/************************************************* |
| 226 |
* Name: unpack_sk |
| 227 |
* |
| 228 |
* Description: Unpack secret key sk = (rho, tr, key, t0, s1, s2). |
| 229 |
* |
| 230 |
* Arguments: - const uint8_t rho[]: output byte array for rho |
| 231 |
* - const uint8_t tr[]: output byte array for tr |
| 232 |
* - const uint8_t key[]: output byte array for key |
| 233 |
* - const polyveck *t0: pointer to output vector t0 |
| 234 |
* - const polyvecl *s1: pointer to output vector s1 |
| 235 |
* - const polyveck *s2: pointer to output vector s2 |
| 236 |
* - uint8_t sk[]: byte array containing bit-packed sk |
| 237 |
**************************************************/ |
| 238 |
void unpack_sk(uint8_t rho[SEEDBYTES], |
| 239 |
uint8_t tr[TRBYTES], |
| 240 |
uint8_t key[SEEDBYTES], |
| 241 |
polyveck *t0, |
| 242 |
polyvecl *s1, |
| 243 |
polyveck *s2, |
| 244 |
const uint8_t sk[CRYPTO_SECRETKEYBYTES]) |
| 245 |
{ |
| 246 |
unsigned int i; |
| 247 |
|
| 248 |
for(i = 0; i < SEEDBYTES; ++i) |
| 249 |
rho[i] = sk[i]; |
| 250 |
sk += SEEDBYTES; |
| 251 |
|
| 252 |
for(i = 0; i < SEEDBYTES; ++i) |
| 253 |
key[i] = sk[i]; |
| 254 |
sk += SEEDBYTES; |
| 255 |
|
| 256 |
for(i = 0; i < TRBYTES; ++i) |
| 257 |
tr[i] = sk[i]; |
| 258 |
sk += TRBYTES; |
| 259 |
|
| 260 |
for(i=0; i < L; ++i) |
| 261 |
polyeta_unpack(&s1->vec[i], sk + i*POLYETA_PACKEDBYTES); |
| 262 |
sk += L*POLYETA_PACKEDBYTES; |
| 263 |
|
| 264 |
for(i=0; i < K; ++i) |
| 265 |
polyeta_unpack(&s2->vec[i], sk + i*POLYETA_PACKEDBYTES); |
| 266 |
sk += K*POLYETA_PACKEDBYTES; |
| 267 |
|
| 268 |
for(i=0; i < K; ++i) |
| 269 |
polyt0_unpack(&t0->vec[i], sk + i*POLYT0_PACKEDBYTES); |
| 270 |
} |
| 271 |
|
| 272 |
/************************************************* |
| 273 |
* Name: pack_sig |
| 274 |
* |
| 275 |
* Description: Bit-pack signature sig = (c, z, h). |
| 276 |
* |
| 277 |
* Arguments: - uint8_t sig[]: output byte array |
| 278 |
* - const uint8_t *c: pointer to challenge hash length SEEDBYTES |
| 279 |
* - const polyvecl *z: pointer to vector z |
| 280 |
* - const polyveck *h: pointer to hint vector h |
| 281 |
**************************************************/ |
| 282 |
void pack_sig(uint8_t sig[CRYPTO_BYTES], |
| 283 |
const uint8_t c[CTILDEBYTES], |
| 284 |
const polyvecl *z, |
| 285 |
const polyveck *h) |
| 286 |
{ |
| 287 |
unsigned int i, j, k; |
| 288 |
|
| 289 |
for(i=0; i < CTILDEBYTES; ++i) |
| 290 |
sig[i] = c[i]; |
| 291 |
sig += CTILDEBYTES; |
| 292 |
|
| 293 |
for(i = 0; i < L; ++i) |
| 294 |
polyz_pack(sig + i*POLYZ_PACKEDBYTES, &z->vec[i]); |
| 295 |
sig += L*POLYZ_PACKEDBYTES; |
| 296 |
|
| 297 |
/* Encode h */ |
| 298 |
for(i = 0; i < OMEGA + K; ++i) |
| 299 |
sig[i] = 0; |
| 300 |
|
| 301 |
k = 0; |
| 302 |
for(i = 0; i < K; ++i) { |
| 303 |
for(j = 0; j < N; ++j) |
| 304 |
if(h->vec[i].coeffs[j] != 0) |
| 305 |
sig[k++] = j; |
| 306 |
|
| 307 |
sig[OMEGA + i] = k; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/************************************************* |
| 312 |
* Name: unpack_sig |
| 313 |
* |
| 314 |
* Description: Unpack signature sig = (c, z, h). |
| 315 |
* |
| 316 |
* Arguments: - uint8_t *c: pointer to output challenge hash |
| 317 |
* - polyvecl *z: pointer to output vector z |
| 318 |
* - polyveck *h: pointer to output hint vector h |
| 319 |
* - const uint8_t sig[]: byte array containing |
| 320 |
* bit-packed signature |
| 321 |
* |
| 322 |
* Returns 1 in case of malformed signature; otherwise 0. |
| 323 |
**************************************************/ |
| 324 |
int unpack_sig(uint8_t c[CTILDEBYTES], |
| 325 |
polyvecl *z, |
| 326 |
polyveck *h, |
| 327 |
const uint8_t sig[CRYPTO_BYTES]) |
| 328 |
{ |
| 329 |
unsigned int i, j, k; |
| 330 |
|
| 331 |
for(i = 0; i < CTILDEBYTES; ++i) |
| 332 |
c[i] = sig[i]; |
| 333 |
sig += CTILDEBYTES; |
| 334 |
|
| 335 |
for(i = 0; i < L; ++i) |
| 336 |
polyz_unpack(&z->vec[i], sig + i*POLYZ_PACKEDBYTES); |
| 337 |
sig += L*POLYZ_PACKEDBYTES; |
| 338 |
|
| 339 |
/* Decode h */ |
| 340 |
k = 0; |
| 341 |
for(i = 0; i < K; ++i) { |
| 342 |
for(j = 0; j < N; ++j) |
| 343 |
h->vec[i].coeffs[j] = 0; |
| 344 |
|
| 345 |
if(sig[OMEGA + i] < k || sig[OMEGA + i] > OMEGA) |
| 346 |
return 1; |
| 347 |
|
| 348 |
for(j = k; j < sig[OMEGA + i]; ++j) { |
| 349 |
/* Coefficients are ordered for strong unforgeability */ |
| 350 |
if(j > k && sig[j] <= sig[j-1]) return 1; |
| 351 |
h->vec[i].coeffs[sig[j]] = 1; |
| 352 |
} |
| 353 |
|
| 354 |
k = sig[OMEGA + i]; |
| 355 |
} |
| 356 |
|
| 357 |
/* Extra indices are zero for strong unforgeability */ |
| 358 |
for(j = k; j < OMEGA; ++j) |
| 359 |
if(sig[j]) |
| 360 |
return 1; |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
/*************** dilithium/ref/poly.c */ |
| 365 |
/************************************************* |
| 366 |
* Name: challenge |
| 367 |
* |
| 368 |
* Description: Implementation of H. Samples polynomial with TAU nonzero |
| 369 |
* coefficients in {-1,1} using the output stream of |
| 370 |
* SHAKE256(seed). |
| 371 |
* |
| 372 |
* Arguments: - poly *c: pointer to output polynomial |
| 373 |
* - const uint8_t mu[]: byte array containing seed of length CTILDEBYTES |
| 374 |
**************************************************/ |
| 375 |
static |
| 376 |
void poly_challenge(poly *c, const uint8_t seed[CTILDEBYTES]) { |
| 377 |
unsigned int i, b, pos; |
| 378 |
uint64_t signs; |
| 379 |
uint8_t buf[SHAKE256_RATE]; |
| 380 |
keccak_state state; |
| 381 |
|
| 382 |
shake256_init(&state); |
| 383 |
shake256_absorb(&state, seed, CTILDEBYTES); |
| 384 |
shake256_finalize(&state); |
| 385 |
shake256_squeezeblocks(buf, 1, &state); |
| 386 |
|
| 387 |
signs = 0; |
| 388 |
for(i = 0; i < 8; ++i) |
| 389 |
signs |= (uint64_t)buf[i] << 8*i; |
| 390 |
pos = 8; |
| 391 |
|
| 392 |
for(i = 0; i < N; ++i) |
| 393 |
c->coeffs[i] = 0; |
| 394 |
for(i = N-TAU; i < N; ++i) { |
| 395 |
do { |
| 396 |
if(pos >= SHAKE256_RATE) { |
| 397 |
shake256_squeezeblocks(buf, 1, &state); |
| 398 |
pos = 0; |
| 399 |
} |
| 400 |
|
| 401 |
b = buf[pos++]; |
| 402 |
} while(b > i); |
| 403 |
|
| 404 |
c->coeffs[i] = c->coeffs[b]; |
| 405 |
c->coeffs[b] = 1 - 2*(signs & 1); |
| 406 |
signs >>= 1; |
| 407 |
} |
| 408 |
shake256_close(&state); |
| 409 |
} |
| 410 |
/*************** dilithium/ref/polyvec.c */ |
| 411 |
|
| 412 |
/************************************************* |
| 413 |
* Name: expand_mat |
| 414 |
* |
| 415 |
* Description: Implementation of ExpandA. Generates matrix A with uniformly |
| 416 |
* random coefficients a_{i,j} by performing rejection |
| 417 |
* sampling on the output stream of SHAKE128(rho|j|i) |
| 418 |
* |
| 419 |
* Arguments: - polyvecl mat[K]: output matrix |
| 420 |
* - const uint8_t rho[]: byte array containing seed rho |
| 421 |
**************************************************/ |
| 422 |
void polyvec_matrix_expand(polyvecl mat[K], const uint8_t rho[SEEDBYTES]) { |
| 423 |
unsigned int i, j; |
| 424 |
|
| 425 |
for(i = 0; i < K; ++i) |
| 426 |
for(j = 0; j < L; ++j) |
| 427 |
poly_uniform(&mat[i].vec[j], rho, (i << 8) + j); |
| 428 |
} |
| 429 |
|
| 430 |
void polyvec_matrix_pointwise_montgomery(polyveck *t, const polyvecl mat[K], const polyvecl *v) { |
| 431 |
unsigned int i; |
| 432 |
|
| 433 |
for(i = 0; i < K; ++i) |
| 434 |
polyvecl_pointwise_acc_montgomery(&t->vec[i], &mat[i], v); |
| 435 |
} |
| 436 |
|
| 437 |
/**************************************************************/ |
| 438 |
/************ Vectors of polynomials of length L **************/ |
| 439 |
/**************************************************************/ |
| 440 |
|
| 441 |
void polyvecl_uniform_eta(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { |
| 442 |
unsigned int i; |
| 443 |
|
| 444 |
for(i = 0; i < L; ++i) |
| 445 |
poly_uniform_eta(&v->vec[i], seed, nonce++); |
| 446 |
} |
| 447 |
|
| 448 |
void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { |
| 449 |
unsigned int i; |
| 450 |
|
| 451 |
for(i = 0; i < L; ++i) |
| 452 |
poly_uniform_gamma1(&v->vec[i], seed, L*nonce + i); |
| 453 |
} |
| 454 |
|
| 455 |
void polyvecl_reduce(polyvecl *v) { |
| 456 |
unsigned int i; |
| 457 |
|
| 458 |
for(i = 0; i < L; ++i) |
| 459 |
poly_reduce(&v->vec[i]); |
| 460 |
} |
| 461 |
|
| 462 |
/************************************************* |
| 463 |
* Name: polyvecl_add |
| 464 |
* |
| 465 |
* Description: Add vectors of polynomials of length L. |
| 466 |
* No modular reduction is performed. |
| 467 |
* |
| 468 |
* Arguments: - polyvecl *w: pointer to output vector |
| 469 |
* - const polyvecl *u: pointer to first summand |
| 470 |
* - const polyvecl *v: pointer to second summand |
| 471 |
**************************************************/ |
| 472 |
void polyvecl_add(polyvecl *w, const polyvecl *u, const polyvecl *v) { |
| 473 |
unsigned int i; |
| 474 |
|
| 475 |
for(i = 0; i < L; ++i) |
| 476 |
poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); |
| 477 |
} |
| 478 |
|
| 479 |
/************************************************* |
| 480 |
* Name: polyvecl_ntt |
| 481 |
* |
| 482 |
* Description: Forward NTT of all polynomials in vector of length L. Output |
| 483 |
* coefficients can be up to 16*Q larger than input coefficients. |
| 484 |
* |
| 485 |
* Arguments: - polyvecl *v: pointer to input/output vector |
| 486 |
**************************************************/ |
| 487 |
void polyvecl_ntt(polyvecl *v) { |
| 488 |
unsigned int i; |
| 489 |
|
| 490 |
for(i = 0; i < L; ++i) |
| 491 |
poly_ntt(&v->vec[i]); |
| 492 |
} |
| 493 |
|
| 494 |
void polyvecl_invntt_tomont(polyvecl *v) { |
| 495 |
unsigned int i; |
| 496 |
|
| 497 |
for(i = 0; i < L; ++i) |
| 498 |
poly_invntt_tomont(&v->vec[i]); |
| 499 |
} |
| 500 |
|
| 501 |
void polyvecl_pointwise_poly_montgomery(polyvecl *r, const poly *a, const polyvecl *v) { |
| 502 |
unsigned int i; |
| 503 |
|
| 504 |
for(i = 0; i < L; ++i) |
| 505 |
poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); |
| 506 |
} |
| 507 |
|
| 508 |
/************************************************* |
| 509 |
* Name: polyvecl_pointwise_acc_montgomery |
| 510 |
* |
| 511 |
* Description: Pointwise multiply vectors of polynomials of length L, multiply |
| 512 |
* resulting vector by 2^{-32} and add (accumulate) polynomials |
| 513 |
* in it. Input/output vectors are in NTT domain representation. |
| 514 |
* |
| 515 |
* Arguments: - poly *w: output polynomial |
| 516 |
* - const polyvecl *u: pointer to first input vector |
| 517 |
* - const polyvecl *v: pointer to second input vector |
| 518 |
**************************************************/ |
| 519 |
void polyvecl_pointwise_acc_montgomery(poly *w, |
| 520 |
const polyvecl *u, |
| 521 |
const polyvecl *v) |
| 522 |
{ |
| 523 |
unsigned int i; |
| 524 |
poly t; |
| 525 |
|
| 526 |
poly_pointwise_montgomery(w, &u->vec[0], &v->vec[0]); |
| 527 |
for(i = 1; i < L; ++i) { |
| 528 |
poly_pointwise_montgomery(&t, &u->vec[i], &v->vec[i]); |
| 529 |
poly_add(w, w, &t); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/************************************************* |
| 534 |
* Name: polyvecl_chknorm |
| 535 |
* |
| 536 |
* Description: Check infinity norm of polynomials in vector of length L. |
| 537 |
* Assumes input polyvecl to be reduced by polyvecl_reduce(). |
| 538 |
* |
| 539 |
* Arguments: - const polyvecl *v: pointer to vector |
| 540 |
* - int32_t B: norm bound |
| 541 |
* |
| 542 |
* Returns 0 if norm of all polynomials is strictly smaller than B <= (Q-1)/8 |
| 543 |
* and 1 otherwise. |
| 544 |
**************************************************/ |
| 545 |
int polyvecl_chknorm(const polyvecl *v, int32_t bound) { |
| 546 |
unsigned int i; |
| 547 |
|
| 548 |
for(i = 0; i < L; ++i) |
| 549 |
if(poly_chknorm(&v->vec[i], bound)) |
| 550 |
return 1; |
| 551 |
|
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
/**************************************************************/ |
| 556 |
/************ Vectors of polynomials of length K **************/ |
| 557 |
/**************************************************************/ |
| 558 |
|
| 559 |
void polyveck_uniform_eta(polyveck *v, const uint8_t seed[CRHBYTES], uint16_t nonce) { |
| 560 |
unsigned int i; |
| 561 |
|
| 562 |
for(i = 0; i < K; ++i) |
| 563 |
poly_uniform_eta(&v->vec[i], seed, nonce++); |
| 564 |
} |
| 565 |
|
| 566 |
/************************************************* |
| 567 |
* Name: polyveck_reduce |
| 568 |
* |
| 569 |
* Description: Reduce coefficients of polynomials in vector of length K |
| 570 |
* to representatives in [-6283008,6283008]. |
| 571 |
* |
| 572 |
* Arguments: - polyveck *v: pointer to input/output vector |
| 573 |
**************************************************/ |
| 574 |
void polyveck_reduce(polyveck *v) { |
| 575 |
unsigned int i; |
| 576 |
|
| 577 |
for(i = 0; i < K; ++i) |
| 578 |
poly_reduce(&v->vec[i]); |
| 579 |
} |
| 580 |
|
| 581 |
/************************************************* |
| 582 |
* Name: polyveck_caddq |
| 583 |
* |
| 584 |
* Description: For all coefficients of polynomials in vector of length K |
| 585 |
* add Q if coefficient is negative. |
| 586 |
* |
| 587 |
* Arguments: - polyveck *v: pointer to input/output vector |
| 588 |
**************************************************/ |
| 589 |
void polyveck_caddq(polyveck *v) { |
| 590 |
unsigned int i; |
| 591 |
|
| 592 |
for(i = 0; i < K; ++i) |
| 593 |
poly_caddq(&v->vec[i]); |
| 594 |
} |
| 595 |
|
| 596 |
/************************************************* |
| 597 |
* Name: polyveck_add |
| 598 |
* |
| 599 |
* Description: Add vectors of polynomials of length K. |
| 600 |
* No modular reduction is performed. |
| 601 |
* |
| 602 |
* Arguments: - polyveck *w: pointer to output vector |
| 603 |
* - const polyveck *u: pointer to first summand |
| 604 |
* - const polyveck *v: pointer to second summand |
| 605 |
**************************************************/ |
| 606 |
void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v) { |
| 607 |
unsigned int i; |
| 608 |
|
| 609 |
for(i = 0; i < K; ++i) |
| 610 |
poly_add(&w->vec[i], &u->vec[i], &v->vec[i]); |
| 611 |
} |
| 612 |
|
| 613 |
/************************************************* |
| 614 |
* Name: polyveck_sub |
| 615 |
* |
| 616 |
* Description: Subtract vectors of polynomials of length K. |
| 617 |
* No modular reduction is performed. |
| 618 |
* |
| 619 |
* Arguments: - polyveck *w: pointer to output vector |
| 620 |
* - const polyveck *u: pointer to first input vector |
| 621 |
* - const polyveck *v: pointer to second input vector to be |
| 622 |
* subtracted from first input vector |
| 623 |
**************************************************/ |
| 624 |
void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v) { |
| 625 |
unsigned int i; |
| 626 |
|
| 627 |
for(i = 0; i < K; ++i) |
| 628 |
poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]); |
| 629 |
} |
| 630 |
|
| 631 |
/************************************************* |
| 632 |
* Name: polyveck_shiftl |
| 633 |
* |
| 634 |
* Description: Multiply vector of polynomials of Length K by 2^D without modular |
| 635 |
* reduction. Assumes input coefficients to be less than 2^{31-D}. |
| 636 |
* |
| 637 |
* Arguments: - polyveck *v: pointer to input/output vector |
| 638 |
**************************************************/ |
| 639 |
void polyveck_shiftl(polyveck *v) { |
| 640 |
unsigned int i; |
| 641 |
|
| 642 |
for(i = 0; i < K; ++i) |
| 643 |
poly_shiftl(&v->vec[i]); |
| 644 |
} |
| 645 |
|
| 646 |
/************************************************* |
| 647 |
* Name: polyveck_ntt |
| 648 |
* |
| 649 |
* Description: Forward NTT of all polynomials in vector of length K. Output |
| 650 |
* coefficients can be up to 16*Q larger than input coefficients. |
| 651 |
* |
| 652 |
* Arguments: - polyveck *v: pointer to input/output vector |
| 653 |
**************************************************/ |
| 654 |
void polyveck_ntt(polyveck *v) { |
| 655 |
unsigned int i; |
| 656 |
|
| 657 |
for(i = 0; i < K; ++i) |
| 658 |
poly_ntt(&v->vec[i]); |
| 659 |
} |
| 660 |
|
| 661 |
/************************************************* |
| 662 |
* Name: polyveck_invntt_tomont |
| 663 |
* |
| 664 |
* Description: Inverse NTT and multiplication by 2^{32} of polynomials |
| 665 |
* in vector of length K. Input coefficients need to be less |
| 666 |
* than 2*Q. |
| 667 |
* |
| 668 |
* Arguments: - polyveck *v: pointer to input/output vector |
| 669 |
**************************************************/ |
| 670 |
void polyveck_invntt_tomont(polyveck *v) { |
| 671 |
unsigned int i; |
| 672 |
|
| 673 |
for(i = 0; i < K; ++i) |
| 674 |
poly_invntt_tomont(&v->vec[i]); |
| 675 |
} |
| 676 |
|
| 677 |
void polyveck_pointwise_poly_montgomery(polyveck *r, const poly *a, const polyveck *v) { |
| 678 |
unsigned int i; |
| 679 |
|
| 680 |
for(i = 0; i < K; ++i) |
| 681 |
poly_pointwise_montgomery(&r->vec[i], a, &v->vec[i]); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
/************************************************* |
| 686 |
* Name: polyveck_chknorm |
| 687 |
* |
| 688 |
* Description: Check infinity norm of polynomials in vector of length K. |
| 689 |
* Assumes input polyveck to be reduced by polyveck_reduce(). |
| 690 |
* |
| 691 |
* Arguments: - const polyveck *v: pointer to vector |
| 692 |
* - int32_t B: norm bound |
| 693 |
* |
| 694 |
* Returns 0 if norm of all polynomials are strictly smaller than B <= (Q-1)/8 |
| 695 |
* and 1 otherwise. |
| 696 |
**************************************************/ |
| 697 |
int polyveck_chknorm(const polyveck *v, int32_t bound) { |
| 698 |
unsigned int i; |
| 699 |
|
| 700 |
for(i = 0; i < K; ++i) |
| 701 |
if(poly_chknorm(&v->vec[i], bound)) |
| 702 |
return 1; |
| 703 |
|
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
/************************************************* |
| 708 |
* Name: polyveck_power2round |
| 709 |
* |
| 710 |
* Description: For all coefficients a of polynomials in vector of length K, |
| 711 |
* compute a0, a1 such that a mod^+ Q = a1*2^D + a0 |
| 712 |
* with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be |
| 713 |
* standard representatives. |
| 714 |
* |
| 715 |
* Arguments: - polyveck *v1: pointer to output vector of polynomials with |
| 716 |
* coefficients a1 |
| 717 |
* - polyveck *v0: pointer to output vector of polynomials with |
| 718 |
* coefficients a0 |
| 719 |
* - const polyveck *v: pointer to input vector |
| 720 |
**************************************************/ |
| 721 |
void polyveck_power2round(polyveck *v1, polyveck *v0, const polyveck *v) { |
| 722 |
unsigned int i; |
| 723 |
|
| 724 |
for(i = 0; i < K; ++i) |
| 725 |
poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]); |
| 726 |
} |
| 727 |
|
| 728 |
/************************************************* |
| 729 |
* Name: polyveck_decompose |
| 730 |
* |
| 731 |
* Description: For all coefficients a of polynomials in vector of length K, |
| 732 |
* compute high and low bits a0, a1 such a mod^+ Q = a1*ALPHA + a0 |
| 733 |
* with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we |
| 734 |
* set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0. |
| 735 |
* Assumes coefficients to be standard representatives. |
| 736 |
* |
| 737 |
* Arguments: - polyveck *v1: pointer to output vector of polynomials with |
| 738 |
* coefficients a1 |
| 739 |
* - polyveck *v0: pointer to output vector of polynomials with |
| 740 |
* coefficients a0 |
| 741 |
* - const polyveck *v: pointer to input vector |
| 742 |
**************************************************/ |
| 743 |
void polyveck_decompose(polyveck *v1, polyveck *v0, const polyveck *v) { |
| 744 |
unsigned int i; |
| 745 |
|
| 746 |
for(i = 0; i < K; ++i) |
| 747 |
poly_decompose(&v1->vec[i], &v0->vec[i], &v->vec[i]); |
| 748 |
} |
| 749 |
|
| 750 |
/************************************************* |
| 751 |
* Name: polyveck_make_hint |
| 752 |
* |
| 753 |
* Description: Compute hint vector. |
| 754 |
* |
| 755 |
* Arguments: - polyveck *h: pointer to output vector |
| 756 |
* - const polyveck *v0: pointer to low part of input vector |
| 757 |
* - const polyveck *v1: pointer to high part of input vector |
| 758 |
* |
| 759 |
* Returns number of 1 bits. |
| 760 |
**************************************************/ |
| 761 |
unsigned int polyveck_make_hint(polyveck *h, |
| 762 |
const polyveck *v0, |
| 763 |
const polyveck *v1) |
| 764 |
{ |
| 765 |
unsigned int i, s = 0; |
| 766 |
|
| 767 |
for(i = 0; i < K; ++i) |
| 768 |
s += poly_make_hint(&h->vec[i], &v0->vec[i], &v1->vec[i]); |
| 769 |
|
| 770 |
return s; |
| 771 |
} |
| 772 |
|
| 773 |
/************************************************* |
| 774 |
* Name: polyveck_use_hint |
| 775 |
* |
| 776 |
* Description: Use hint vector to correct the high bits of input vector. |
| 777 |
* |
| 778 |
* Arguments: - polyveck *w: pointer to output vector of polynomials with |
| 779 |
* corrected high bits |
| 780 |
* - const polyveck *u: pointer to input vector |
| 781 |
* - const polyveck *h: pointer to input hint vector |
| 782 |
**************************************************/ |
| 783 |
void polyveck_use_hint(polyveck *w, const polyveck *u, const polyveck *h) { |
| 784 |
unsigned int i; |
| 785 |
|
| 786 |
for(i = 0; i < K; ++i) |
| 787 |
poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]); |
| 788 |
} |
| 789 |
|
| 790 |
void polyveck_pack_w1(uint8_t r[K*POLYW1_PACKEDBYTES], const polyveck *w1) { |
| 791 |
unsigned int i; |
| 792 |
|
| 793 |
for(i = 0; i < K; ++i) |
| 794 |
polyw1_pack(&r[i*POLYW1_PACKEDBYTES], &w1->vec[i]); |
| 795 |
} |
| 796 |
/*************** dilithium/ref/sign.c */ |
| 797 |
|
| 798 |
/************************************************* |
| 799 |
* Name: crypto_sign_keypair |
| 800 |
* |
| 801 |
* Description: Generates public and private key. |
| 802 |
* |
| 803 |
* Arguments: - uint8_t *pk: pointer to output public key (allocated |
| 804 |
* array of CRYPTO_PUBLICKEYBYTES bytes) |
| 805 |
* - uint8_t *sk: pointer to output private key (allocated |
| 806 |
* array of CRYPTO_SECRETKEYBYTES bytes) |
| 807 |
* |
| 808 |
* Returns 0 (success) |
| 809 |
**************************************************/ |
| 810 |
#ifndef DILITHIUM_INTERNAL_API_ONLY |
| 811 |
int crypto_sign_keypair(uint8_t *pk, uint8_t *sk) { |
| 812 |
uint8_t seedbuf[2*SEEDBYTES + CRHBYTES]; |
| 813 |
uint8_t tr[TRBYTES]; |
| 814 |
const uint8_t *rho, *rhoprime, *key; |
| 815 |
polyvecl mat[K]; |
| 816 |
polyvecl s1, s1hat; |
| 817 |
polyveck s2, t1, t0; |
| 818 |
|
| 819 |
/* Get randomness for rho, rhoprime and key */ |
| 820 |
randombytes(seedbuf, SEEDBYTES); |
| 821 |
seedbuf[SEEDBYTES+0] = K; |
| 822 |
seedbuf[SEEDBYTES+1] = L; |
| 823 |
shake256(seedbuf, 2*SEEDBYTES + CRHBYTES, seedbuf, SEEDBYTES+2); |
| 824 |
rho = seedbuf; |
| 825 |
rhoprime = rho + SEEDBYTES; |
| 826 |
key = rhoprime + CRHBYTES; |
| 827 |
|
| 828 |
/* Expand matrix */ |
| 829 |
polyvec_matrix_expand(mat, rho); |
| 830 |
|
| 831 |
/* Sample short vectors s1 and s2 */ |
| 832 |
polyvecl_uniform_eta(&s1, rhoprime, 0); |
| 833 |
polyveck_uniform_eta(&s2, rhoprime, L); |
| 834 |
|
| 835 |
/* Matrix-vector multiplication */ |
| 836 |
s1hat = s1; |
| 837 |
polyvecl_ntt(&s1hat); |
| 838 |
polyvec_matrix_pointwise_montgomery(&t1, mat, &s1hat); |
| 839 |
polyveck_reduce(&t1); |
| 840 |
polyveck_invntt_tomont(&t1); |
| 841 |
|
| 842 |
/* Add error vector s2 */ |
| 843 |
polyveck_add(&t1, &t1, &s2); |
| 844 |
|
| 845 |
/* Extract t1 and write public key */ |
| 846 |
polyveck_caddq(&t1); |
| 847 |
polyveck_power2round(&t1, &t0, &t1); |
| 848 |
pack_pk(pk, rho, &t1); |
| 849 |
|
| 850 |
/* Compute H(rho, t1) and write secret key */ |
| 851 |
shake256(tr, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); |
| 852 |
pack_sk(sk, rho, tr, key, &t0, &s1, &s2); |
| 853 |
|
| 854 |
return 0; |
| 855 |
} |
| 856 |
#else |
| 857 |
int crypto_sign_keypair_internal(uint8_t *pk, uint8_t *sk, |
| 858 |
const uint8_t seed[SEEDBYTES]) |
| 859 |
{ |
| 860 |
uint8_t seedbuf[2*SEEDBYTES + CRHBYTES]; |
| 861 |
uint8_t tr[TRBYTES]; |
| 862 |
const uint8_t *rho, *rhoprime, *key; |
| 863 |
polyvecl mat[K]; |
| 864 |
polyvecl s1, s1hat; |
| 865 |
polyveck s2, t1, t0; |
| 866 |
size_t i; |
| 867 |
|
| 868 |
/* Get randomness for rho, rhoprime and key */ |
| 869 |
for (i = 0; i < SEEDBYTES; i++) |
| 870 |
seedbuf[i] = seed[i]; |
| 871 |
seedbuf[SEEDBYTES+0] = K; |
| 872 |
seedbuf[SEEDBYTES+1] = L; |
| 873 |
shake256(seedbuf, 2*SEEDBYTES + CRHBYTES, seedbuf, SEEDBYTES+2); |
| 874 |
rho = seedbuf; |
| 875 |
rhoprime = rho + SEEDBYTES; |
| 876 |
key = rhoprime + CRHBYTES; |
| 877 |
|
| 878 |
/* Expand matrix */ |
| 879 |
polyvec_matrix_expand(mat, rho); |
| 880 |
|
| 881 |
/* Sample short vectors s1 and s2 */ |
| 882 |
polyvecl_uniform_eta(&s1, rhoprime, 0); |
| 883 |
polyveck_uniform_eta(&s2, rhoprime, L); |
| 884 |
|
| 885 |
/* Matrix-vector multiplication */ |
| 886 |
s1hat = s1; |
| 887 |
polyvecl_ntt(&s1hat); |
| 888 |
polyvec_matrix_pointwise_montgomery(&t1, mat, &s1hat); |
| 889 |
polyveck_reduce(&t1); |
| 890 |
polyveck_invntt_tomont(&t1); |
| 891 |
|
| 892 |
/* Add error vector s2 */ |
| 893 |
polyveck_add(&t1, &t1, &s2); |
| 894 |
|
| 895 |
/* Extract t1 and write public key */ |
| 896 |
polyveck_caddq(&t1); |
| 897 |
polyveck_power2round(&t1, &t0, &t1); |
| 898 |
pack_pk(pk, rho, &t1); |
| 899 |
|
| 900 |
/* Compute H(rho, t1) and write secret key */ |
| 901 |
shake256(tr, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); |
| 902 |
pack_sk(sk, rho, tr, key, &t0, &s1, &s2); |
| 903 |
|
| 904 |
return 0; |
| 905 |
} |
| 906 |
#endif |
| 907 |
|
| 908 |
/************************************************* |
| 909 |
* Name: crypto_sign_signature_internal |
| 910 |
* |
| 911 |
* Description: Computes signature. Internal API. |
| 912 |
* |
| 913 |
* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) |
| 914 |
* - size_t *siglen: pointer to output length of signature |
| 915 |
* - uint8_t *m: pointer to message to be signed |
| 916 |
* - size_t mlen: length of message |
| 917 |
* - uint8_t *pre: pointer to prefix string |
| 918 |
* - size_t prelen: length of prefix string |
| 919 |
* - uint8_t *rnd: pointer to random seed |
| 920 |
* - uint8_t *sk: pointer to bit-packed secret key |
| 921 |
* |
| 922 |
* Returns 0 (success) |
| 923 |
**************************************************/ |
| 924 |
int crypto_sign_signature_internal(uint8_t *sig, |
| 925 |
size_t *siglen, |
| 926 |
const uint8_t *m, |
| 927 |
size_t mlen, |
| 928 |
const uint8_t *pre, |
| 929 |
size_t prelen, |
| 930 |
const uint8_t rnd[RNDBYTES], |
| 931 |
const uint8_t *sk) |
| 932 |
{ |
| 933 |
unsigned int n; |
| 934 |
uint8_t seedbuf[2*SEEDBYTES + TRBYTES + 2*CRHBYTES]; |
| 935 |
uint8_t *rho, *tr, *key, *mu, *rhoprime; |
| 936 |
uint16_t nonce = 0; |
| 937 |
polyvecl mat[K], s1, y, z; |
| 938 |
polyveck t0, s2, w1, w0, h; |
| 939 |
poly cp; |
| 940 |
keccak_state state; |
| 941 |
|
| 942 |
rho = seedbuf; |
| 943 |
tr = rho + SEEDBYTES; |
| 944 |
key = tr + TRBYTES; |
| 945 |
mu = key + SEEDBYTES; |
| 946 |
rhoprime = mu + CRHBYTES; |
| 947 |
unpack_sk(rho, tr, key, &t0, &s1, &s2, sk); |
| 948 |
|
| 949 |
/* Compute mu = CRH(tr, pre, msg) */ |
| 950 |
shake256_init(&state); |
| 951 |
shake256_absorb(&state, tr, TRBYTES); |
| 952 |
shake256_absorb(&state, pre, prelen); |
| 953 |
shake256_absorb(&state, m, mlen); |
| 954 |
shake256_finalize(&state); |
| 955 |
shake256_squeeze(mu, CRHBYTES, &state); |
| 956 |
shake256_close(&state); |
| 957 |
|
| 958 |
/* Compute rhoprime = CRH(key, rnd, mu) */ |
| 959 |
shake256_init(&state); |
| 960 |
shake256_absorb(&state, key, SEEDBYTES); |
| 961 |
shake256_absorb(&state, rnd, RNDBYTES); |
| 962 |
shake256_absorb(&state, mu, CRHBYTES); |
| 963 |
shake256_finalize(&state); |
| 964 |
shake256_squeeze(rhoprime, CRHBYTES, &state); |
| 965 |
shake256_close(&state); |
| 966 |
|
| 967 |
/* Expand matrix and transform vectors */ |
| 968 |
polyvec_matrix_expand(mat, rho); |
| 969 |
polyvecl_ntt(&s1); |
| 970 |
polyveck_ntt(&s2); |
| 971 |
polyveck_ntt(&t0); |
| 972 |
|
| 973 |
rej: |
| 974 |
/* Sample intermediate vector y */ |
| 975 |
polyvecl_uniform_gamma1(&y, rhoprime, nonce++); |
| 976 |
|
| 977 |
/* Matrix-vector multiplication */ |
| 978 |
z = y; |
| 979 |
polyvecl_ntt(&z); |
| 980 |
polyvec_matrix_pointwise_montgomery(&w1, mat, &z); |
| 981 |
polyveck_reduce(&w1); |
| 982 |
polyveck_invntt_tomont(&w1); |
| 983 |
|
| 984 |
/* Decompose w and call the random oracle */ |
| 985 |
polyveck_caddq(&w1); |
| 986 |
polyveck_decompose(&w1, &w0, &w1); |
| 987 |
polyveck_pack_w1(sig, &w1); |
| 988 |
|
| 989 |
shake256_init(&state); |
| 990 |
shake256_absorb(&state, mu, CRHBYTES); |
| 991 |
shake256_absorb(&state, sig, K*POLYW1_PACKEDBYTES); |
| 992 |
shake256_finalize(&state); |
| 993 |
shake256_squeeze(sig, CTILDEBYTES, &state); |
| 994 |
shake256_close(&state); |
| 995 |
poly_challenge(&cp, sig); |
| 996 |
poly_ntt(&cp); |
| 997 |
|
| 998 |
/* Compute z, reject if it reveals secret */ |
| 999 |
polyvecl_pointwise_poly_montgomery(&z, &cp, &s1); |
| 1000 |
polyvecl_invntt_tomont(&z); |
| 1001 |
polyvecl_add(&z, &z, &y); |
| 1002 |
polyvecl_reduce(&z); |
| 1003 |
if(polyvecl_chknorm(&z, GAMMA1 - BETA)) |
| 1004 |
goto rej; |
| 1005 |
|
| 1006 |
/* Check that subtracting cs2 does not change high bits of w and low bits |
| 1007 |
* do not reveal secret information */ |
| 1008 |
polyveck_pointwise_poly_montgomery(&h, &cp, &s2); |
| 1009 |
polyveck_invntt_tomont(&h); |
| 1010 |
polyveck_sub(&w0, &w0, &h); |
| 1011 |
polyveck_reduce(&w0); |
| 1012 |
if(polyveck_chknorm(&w0, GAMMA2 - BETA)) |
| 1013 |
goto rej; |
| 1014 |
|
| 1015 |
/* Compute hints for w1 */ |
| 1016 |
polyveck_pointwise_poly_montgomery(&h, &cp, &t0); |
| 1017 |
polyveck_invntt_tomont(&h); |
| 1018 |
polyveck_reduce(&h); |
| 1019 |
if(polyveck_chknorm(&h, GAMMA2)) |
| 1020 |
goto rej; |
| 1021 |
|
| 1022 |
polyveck_add(&w0, &w0, &h); |
| 1023 |
n = polyveck_make_hint(&h, &w0, &w1); |
| 1024 |
if(n > OMEGA) |
| 1025 |
goto rej; |
| 1026 |
|
| 1027 |
/* Write signature */ |
| 1028 |
pack_sig(sig, sig, &z, &h); |
| 1029 |
*siglen = CRYPTO_BYTES; |
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/************************************************* |
| 1034 |
* Name: crypto_sign_signature |
| 1035 |
* |
| 1036 |
* Description: Computes signature. |
| 1037 |
* |
| 1038 |
* Arguments: - uint8_t *sig: pointer to output signature (of length CRYPTO_BYTES) |
| 1039 |
* - size_t *siglen: pointer to output length of signature |
| 1040 |
* - uint8_t *m: pointer to message to be signed |
| 1041 |
* - size_t mlen: length of message |
| 1042 |
* - uint8_t *ctx: pointer to contex string |
| 1043 |
* - size_t ctxlen: length of contex string |
| 1044 |
* - uint8_t *sk: pointer to bit-packed secret key |
| 1045 |
* |
| 1046 |
* Returns 0 (success) or -1 (context string too long) |
| 1047 |
**************************************************/ |
| 1048 |
#ifndef DILITHIUM_INTERNAL_API_ONLY |
| 1049 |
int crypto_sign_signature(uint8_t *sig, |
| 1050 |
size_t *siglen, |
| 1051 |
const uint8_t *m, |
| 1052 |
size_t mlen, |
| 1053 |
const uint8_t *ctx, |
| 1054 |
size_t ctxlen, |
| 1055 |
const uint8_t *sk) |
| 1056 |
{ |
| 1057 |
size_t i; |
| 1058 |
uint8_t pre[257]; |
| 1059 |
uint8_t rnd[RNDBYTES]; |
| 1060 |
|
| 1061 |
if(ctxlen > 255) |
| 1062 |
return -1; |
| 1063 |
|
| 1064 |
/* Prepare pre = (0, ctxlen, ctx) */ |
| 1065 |
pre[0] = 0; |
| 1066 |
pre[1] = ctxlen; |
| 1067 |
for(i = 0; i < ctxlen; i++) |
| 1068 |
pre[2 + i] = ctx[i]; |
| 1069 |
|
| 1070 |
#ifdef DILITHIUM_RANDOMIZED_SIGNING |
| 1071 |
randombytes(rnd, RNDBYTES); |
| 1072 |
#else |
| 1073 |
for(i=0;i<RNDBYTES;i++) |
| 1074 |
rnd[i] = 0; |
| 1075 |
#endif |
| 1076 |
|
| 1077 |
crypto_sign_signature_internal(sig,siglen,m,mlen,pre,2+ctxlen,rnd,sk); |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
#endif |
| 1081 |
|
| 1082 |
/************************************************* |
| 1083 |
* Name: crypto_sign |
| 1084 |
* |
| 1085 |
* Description: Compute signed message. |
| 1086 |
* |
| 1087 |
* Arguments: - uint8_t *sm: pointer to output signed message (allocated |
| 1088 |
* array with CRYPTO_BYTES + mlen bytes), |
| 1089 |
* can be equal to m |
| 1090 |
* - size_t *smlen: pointer to output length of signed |
| 1091 |
* message |
| 1092 |
* - const uint8_t *m: pointer to message to be signed |
| 1093 |
* - size_t mlen: length of message |
| 1094 |
* - const uint8_t *ctx: pointer to context string |
| 1095 |
* - size_t ctxlen: length of context string |
| 1096 |
* - const uint8_t *sk: pointer to bit-packed secret key |
| 1097 |
* |
| 1098 |
* Returns 0 (success) or -1 (context string too long) |
| 1099 |
**************************************************/ |
| 1100 |
#ifndef DILITHIUM_INTERNAL_API_ONLY |
| 1101 |
int crypto_sign(uint8_t *sm, |
| 1102 |
size_t *smlen, |
| 1103 |
const uint8_t *m, |
| 1104 |
size_t mlen, |
| 1105 |
const uint8_t *ctx, |
| 1106 |
size_t ctxlen, |
| 1107 |
const uint8_t *sk) |
| 1108 |
{ |
| 1109 |
int ret; |
| 1110 |
size_t i; |
| 1111 |
|
| 1112 |
for(i = 0; i < mlen; ++i) |
| 1113 |
sm[CRYPTO_BYTES + mlen - 1 - i] = m[mlen - 1 - i]; |
| 1114 |
ret = crypto_sign_signature(sm, smlen, sm + CRYPTO_BYTES, mlen, ctx, ctxlen, sk); |
| 1115 |
*smlen += mlen; |
| 1116 |
return ret; |
| 1117 |
} |
| 1118 |
#endif |
| 1119 |
|
| 1120 |
/************************************************* |
| 1121 |
* Name: crypto_sign_verify_internal |
| 1122 |
* |
| 1123 |
* Description: Verifies signature. Internal API. |
| 1124 |
* |
| 1125 |
* Arguments: - uint8_t *m: pointer to input signature |
| 1126 |
* - size_t siglen: length of signature |
| 1127 |
* - const uint8_t *m: pointer to message |
| 1128 |
* - size_t mlen: length of message |
| 1129 |
* - const uint8_t *pre: pointer to prefix string |
| 1130 |
* - size_t prelen: length of prefix string |
| 1131 |
* - const uint8_t *pk: pointer to bit-packed public key |
| 1132 |
* |
| 1133 |
* Returns 0 if signature could be verified correctly and -1 otherwise |
| 1134 |
**************************************************/ |
| 1135 |
int crypto_sign_verify_internal(const uint8_t *sig, |
| 1136 |
size_t siglen, |
| 1137 |
const uint8_t *m, |
| 1138 |
size_t mlen, |
| 1139 |
const uint8_t *pre, |
| 1140 |
size_t prelen, |
| 1141 |
const uint8_t *pk) |
| 1142 |
{ |
| 1143 |
unsigned int i; |
| 1144 |
uint8_t buf[K*POLYW1_PACKEDBYTES]; |
| 1145 |
uint8_t rho[SEEDBYTES]; |
| 1146 |
uint8_t mu[CRHBYTES]; |
| 1147 |
uint8_t c[CTILDEBYTES]; |
| 1148 |
uint8_t c2[CTILDEBYTES]; |
| 1149 |
poly cp; |
| 1150 |
polyvecl mat[K], z; |
| 1151 |
polyveck t1, w1, h; |
| 1152 |
keccak_state state; |
| 1153 |
|
| 1154 |
if(siglen != CRYPTO_BYTES) |
| 1155 |
return -1; |
| 1156 |
|
| 1157 |
unpack_pk(rho, &t1, pk); |
| 1158 |
if(unpack_sig(c, &z, &h, sig)) |
| 1159 |
return -1; |
| 1160 |
if(polyvecl_chknorm(&z, GAMMA1 - BETA)) |
| 1161 |
return -1; |
| 1162 |
|
| 1163 |
/* Compute CRH(H(rho, t1), pre, msg) */ |
| 1164 |
shake256(mu, TRBYTES, pk, CRYPTO_PUBLICKEYBYTES); |
| 1165 |
shake256_init(&state); |
| 1166 |
shake256_absorb(&state, mu, TRBYTES); |
| 1167 |
shake256_absorb(&state, pre, prelen); |
| 1168 |
shake256_absorb(&state, m, mlen); |
| 1169 |
shake256_finalize(&state); |
| 1170 |
shake256_squeeze(mu, CRHBYTES, &state); |
| 1171 |
shake256_close(&state); |
| 1172 |
|
| 1173 |
/* Matrix-vector multiplication; compute Az - c2^dt1 */ |
| 1174 |
poly_challenge(&cp, c); |
| 1175 |
polyvec_matrix_expand(mat, rho); |
| 1176 |
|
| 1177 |
polyvecl_ntt(&z); |
| 1178 |
polyvec_matrix_pointwise_montgomery(&w1, mat, &z); |
| 1179 |
|
| 1180 |
poly_ntt(&cp); |
| 1181 |
polyveck_shiftl(&t1); |
| 1182 |
polyveck_ntt(&t1); |
| 1183 |
polyveck_pointwise_poly_montgomery(&t1, &cp, &t1); |
| 1184 |
|
| 1185 |
polyveck_sub(&w1, &w1, &t1); |
| 1186 |
polyveck_reduce(&w1); |
| 1187 |
polyveck_invntt_tomont(&w1); |
| 1188 |
|
| 1189 |
/* Reconstruct w1 */ |
| 1190 |
polyveck_caddq(&w1); |
| 1191 |
polyveck_use_hint(&w1, &w1, &h); |
| 1192 |
polyveck_pack_w1(buf, &w1); |
| 1193 |
|
| 1194 |
/* Call random oracle and verify challenge */ |
| 1195 |
shake256_init(&state); |
| 1196 |
shake256_absorb(&state, mu, CRHBYTES); |
| 1197 |
shake256_absorb(&state, buf, K*POLYW1_PACKEDBYTES); |
| 1198 |
shake256_finalize(&state); |
| 1199 |
shake256_squeeze(c2, CTILDEBYTES, &state); |
| 1200 |
shake256_close(&state); |
| 1201 |
for(i = 0; i < CTILDEBYTES; ++i) |
| 1202 |
if(c[i] != c2[i]) |
| 1203 |
return -1; |
| 1204 |
|
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
|
| 1208 |
/************************************************* |
| 1209 |
* Name: crypto_sign_verify |
| 1210 |
* |
| 1211 |
* Description: Verifies signature. |
| 1212 |
* |
| 1213 |
* Arguments: - uint8_t *m: pointer to input signature |
| 1214 |
* - size_t siglen: length of signature |
| 1215 |
* - const uint8_t *m: pointer to message |
| 1216 |
* - size_t mlen: length of message |
| 1217 |
* - const uint8_t *ctx: pointer to context string |
| 1218 |
* - size_t ctxlen: length of context string |
| 1219 |
* - const uint8_t *pk: pointer to bit-packed public key |
| 1220 |
* |
| 1221 |
* Returns 0 if signature could be verified correctly and -1 otherwise |
| 1222 |
**************************************************/ |
| 1223 |
#ifndef DILITHIUM_INTERNAL_API_ONLY |
| 1224 |
int crypto_sign_verify(const uint8_t *sig, |
| 1225 |
size_t siglen, |
| 1226 |
const uint8_t *m, |
| 1227 |
size_t mlen, |
| 1228 |
const uint8_t *ctx, |
| 1229 |
size_t ctxlen, |
| 1230 |
const uint8_t *pk) |
| 1231 |
{ |
| 1232 |
size_t i; |
| 1233 |
uint8_t pre[257]; |
| 1234 |
|
| 1235 |
if(ctxlen > 255) |
| 1236 |
return -1; |
| 1237 |
|
| 1238 |
pre[0] = 0; |
| 1239 |
pre[1] = ctxlen; |
| 1240 |
for(i = 0; i < ctxlen; i++) |
| 1241 |
pre[2 + i] = ctx[i]; |
| 1242 |
|
| 1243 |
return crypto_sign_verify_internal(sig,siglen,m,mlen,pre,2+ctxlen,pk); |
| 1244 |
} |
| 1245 |
#endif |
| 1246 |
|
| 1247 |
/************************************************* |
| 1248 |
* Name: crypto_sign_open |
| 1249 |
* |
| 1250 |
* Description: Verify signed message. |
| 1251 |
* |
| 1252 |
* Arguments: - uint8_t *m: pointer to output message (allocated |
| 1253 |
* array with smlen bytes), can be equal to sm |
| 1254 |
* - size_t *mlen: pointer to output length of message |
| 1255 |
* - const uint8_t *sm: pointer to signed message |
| 1256 |
* - size_t smlen: length of signed message |
| 1257 |
* - const uint8_t *ctx: pointer to context tring |
| 1258 |
* - size_t ctxlen: length of context string |
| 1259 |
* - const uint8_t *pk: pointer to bit-packed public key |
| 1260 |
* |
| 1261 |
* Returns 0 if signed message could be verified correctly and -1 otherwise |
| 1262 |
**************************************************/ |
| 1263 |
#ifndef DILITHIUM_INTERNAL_API_ONLY |
| 1264 |
int crypto_sign_open(uint8_t *m, |
| 1265 |
size_t *mlen, |
| 1266 |
const uint8_t *sm, |
| 1267 |
size_t smlen, |
| 1268 |
const uint8_t *ctx, |
| 1269 |
size_t ctxlen, |
| 1270 |
const uint8_t *pk) |
| 1271 |
{ |
| 1272 |
size_t i; |
| 1273 |
|
| 1274 |
if(smlen < CRYPTO_BYTES) |
| 1275 |
goto badsig; |
| 1276 |
|
| 1277 |
*mlen = smlen - CRYPTO_BYTES; |
| 1278 |
if(crypto_sign_verify(sm, CRYPTO_BYTES, sm + CRYPTO_BYTES, *mlen, ctx, ctxlen, pk)) |
| 1279 |
goto badsig; |
| 1280 |
else { |
| 1281 |
/* All good, copy msg, return 0 */ |
| 1282 |
for(i = 0; i < *mlen; ++i) |
| 1283 |
m[i] = sm[CRYPTO_BYTES + i]; |
| 1284 |
return 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
badsig: |
| 1288 |
/* Signature verification failed */ |
| 1289 |
*mlen = 0; |
| 1290 |
for(i = 0; i < smlen; ++i) |
| 1291 |
m[i] = 0; |
| 1292 |
|
| 1293 |
return -1; |
| 1294 |
} |
| 1295 |
#endif |
| 1296 |
|
| 1297 |
#undef DILITHIUM_MODE |
| 1298 |
|
| 1299 |
#undef CRYPTO_PUBLICKEYBYTES |
| 1300 |
#undef CRYPTO_SECRETKEYBYTES |
| 1301 |
#undef CRYPTO_BYTES |
| 1302 |
#undef POLYZ_PACKEDBYTES |
| 1303 |
#undef POLYW1_PACKEDBYTES |
| 1304 |
#undef POLYETA_PACKEDBYTES |
| 1305 |
|
| 1306 |
#undef CRYPTO_ALGNAME |
| 1307 |
#undef K |
| 1308 |
#undef L |
| 1309 |
#undef ETA |
| 1310 |
#undef TAU |
| 1311 |
#undef BETA |
| 1312 |
#undef GAMMA1 |
| 1313 |
#undef GAMMA2 |
| 1314 |
#undef OMEGA |
| 1315 |
#undef CTILDEBYTES |
| 1316 |
|
| 1317 |
#undef poly_decompose |
| 1318 |
#undef poly_make_hint |
| 1319 |
#undef poly_use_hint |
| 1320 |
#undef poly_uniform_eta |
| 1321 |
#undef poly_uniform_gamma1 |
| 1322 |
#undef polyz_pack |
| 1323 |
#undef polyz_unpack |
| 1324 |
#undef polyeta_pack |
| 1325 |
#undef polyeta_unpack |
| 1326 |
#undef polyw1_pack |
| 1327 |
|
| 1328 |
#undef polyvecl |
| 1329 |
#undef polyveck |
| 1330 |
#undef pack_pk |
| 1331 |
#undef unpack_pk |
| 1332 |
#undef pack_sk |
| 1333 |
#undef unpack_sk |
| 1334 |
#undef pack_sig |
| 1335 |
#undef unpack_sig |
| 1336 |
#undef poly_challenge |
| 1337 |
#undef polyvec_matrix_expand |
| 1338 |
#undef polyvec_matrix_pointwise_montgomery |
| 1339 |
#undef polyveck_power2round |
| 1340 |
#undef polyveck_make_hint |
| 1341 |
#undef polyveck_use_hint |
| 1342 |
#undef polyvecl_uniform_eta |
| 1343 |
#undef polyvecl_uniform_gamma1 |
| 1344 |
#undef polyvecl_reduce |
| 1345 |
#undef polyvecl_add |
| 1346 |
#undef polyvecl_ntt |
| 1347 |
#undef polyvecl_invntt_tomont |
| 1348 |
#undef polyvecl_pointwise_poly_montgomery |
| 1349 |
#undef polyvecl_pointwise_acc_montgomery |
| 1350 |
#undef polyvecl_chknorm |
| 1351 |
#undef polyveck_uniform_eta |
| 1352 |
#undef polyveck_reduce |
| 1353 |
#undef polyveck_caddq |
| 1354 |
#undef polyveck_add |
| 1355 |
#undef polyveck_sub |
| 1356 |
#undef polyveck_shiftl |
| 1357 |
#undef polyveck_ntt |
| 1358 |
#undef polyveck_invntt_tomont |
| 1359 |
#undef polyveck_pointwise_poly_montgomery |
| 1360 |
#undef polyveck_chknorm |
| 1361 |
#undef polyveck_pack_w1 |
| 1362 |
#undef polyveck_decompose |
| 1363 |
#undef crypto_sign_keypair |
| 1364 |
#undef crypto_sign_keypair_internal |
| 1365 |
#undef crypto_sign_signature_internal |
| 1366 |
#undef crypto_sign_signature |
| 1367 |
#undef crypto_sign |
| 1368 |
#undef crypto_sign_verify_internal |
| 1369 |
#undef crypto_sign_verify |
| 1370 |
#undef crypto_sign_open |