| 1 |
/* dilithium-common.c - the Dilithium (common 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/ntt.h */ |
| 47 |
static void ntt(int32_t a[N]); |
| 48 |
|
| 49 |
static void invntt_tomont(int32_t a[N]); |
| 50 |
|
| 51 |
/*************** dilithium/ref/ntt.c */ |
| 52 |
|
| 53 |
static const int32_t zetas[N] = { |
| 54 |
0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, |
| 55 |
1826347, 2353451, -359251, -2091905, 3119733, -2884855, 3111497, 2680103, |
| 56 |
2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, |
| 57 |
-2118186, -3859737, -1399561, -3277672, 1757237, -19422, 4010497, 280005, |
| 58 |
2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516, 3915439, |
| 59 |
-3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, |
| 60 |
-1699267, -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596, |
| 61 |
811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779, |
| 62 |
-3930395, -1528703, -3677745, -3041255, -1452451, 3475950, 2176455, -1585221, |
| 63 |
-1257611, 1939314, -4083598, -1000202, -3190144, -3157330, -3632928, 126922, |
| 64 |
3412210, -983419, 2147896, 2715295, -2967645, -3693493, -411027, -2477047, |
| 65 |
-671102, -1228525, -22981, -1308169, -381987, 1349076, 1852771, -1430430, |
| 66 |
-3343383, 264944, 508951, 3097992, 44288, -1100098, 904516, 3958618, |
| 67 |
-3724342, -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856, |
| 68 |
189548, -3553272, 3159746, -1851402, -2409325, -177440, 1315589, 1341330, |
| 69 |
1285669, -1584928, -812732, -1439742, -3019102, -3881060, -3628969, 3839961, |
| 70 |
2091667, 3407706, 2316500, 3817976, -3342478, 2244091, -2446433, -3562462, |
| 71 |
266997, 2434439, -1235728, 3513181, -3520352, -3759364, -1197226, -3193378, |
| 72 |
900702, 1859098, 909542, 819034, 495491, -1613174, -43260, -522500, |
| 73 |
-655327, -3122442, 2031748, 3207046, -3556995, -525098, -768622, -3595838, |
| 74 |
342297, 286988, -2437823, 4108315, 3437287, -3342277, 1735879, 203044, |
| 75 |
2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353, 1595974, |
| 76 |
-3767016, 1250494, 2635921, -3548272, -2994039, 1869119, 1903435, -1050970, |
| 77 |
-1333058, 1237275, -3318210, -1430225, -451100, 1312455, 3306115, -1962642, |
| 78 |
-1279661, 1917081, -2546312, -1374803, 1500165, 777191, 2235880, 3406031, |
| 79 |
-542412, -2831860, -1671176, -1846953, -2584293, -3724270, 594136, -3776993, |
| 80 |
-2013608, 2432395, 2454455, -164721, 1957272, 3369112, 185531, -1207385, |
| 81 |
-3183426, 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107, |
| 82 |
-3038916, 3523897, 3866901, 269760, 2213111, -975884, 1717735, 472078, |
| 83 |
-426683, 1723600, -1803090, 1910376, -1667432, -1104333, -260646, -3833893, |
| 84 |
-2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, |
| 85 |
-554416, 3919660, -48306, -1362209, 3937738, 1400424, -846154, 1976782 |
| 86 |
}; |
| 87 |
|
| 88 |
/************************************************* |
| 89 |
* Name: ntt |
| 90 |
* |
| 91 |
* Description: Forward NTT, in-place. No modular reduction is performed after |
| 92 |
* additions or subtractions. Output vector is in bitreversed order. |
| 93 |
* |
| 94 |
* Arguments: - uint32_t p[N]: input/output coefficient array |
| 95 |
**************************************************/ |
| 96 |
void ntt(int32_t a[N]) { |
| 97 |
unsigned int len, start, j, k; |
| 98 |
int32_t zeta, t; |
| 99 |
|
| 100 |
k = 0; |
| 101 |
for(len = 128; len > 0; len >>= 1) { |
| 102 |
for(start = 0; start < N; start = j + len) { |
| 103 |
zeta = zetas[++k]; |
| 104 |
for(j = start; j < start + len; ++j) { |
| 105 |
t = montgomery_reduce((int64_t)zeta * a[j + len]); |
| 106 |
a[j + len] = a[j] - t; |
| 107 |
a[j] = a[j] + t; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/************************************************* |
| 114 |
* Name: invntt_tomont |
| 115 |
* |
| 116 |
* Description: Inverse NTT and multiplication by Montgomery factor 2^32. |
| 117 |
* In-place. No modular reductions after additions or |
| 118 |
* subtractions; input coefficients need to be smaller than |
| 119 |
* Q in absolute value. Output coefficient are smaller than Q in |
| 120 |
* absolute value. |
| 121 |
* |
| 122 |
* Arguments: - uint32_t p[N]: input/output coefficient array |
| 123 |
**************************************************/ |
| 124 |
void invntt_tomont(int32_t a[N]) { |
| 125 |
unsigned int start, len, j, k; |
| 126 |
int32_t t, zeta; |
| 127 |
const int32_t f = 41978; /* mont^2/256 */ |
| 128 |
|
| 129 |
k = 256; |
| 130 |
for(len = 1; len < N; len <<= 1) { |
| 131 |
for(start = 0; start < N; start = j + len) { |
| 132 |
zeta = -zetas[--k]; |
| 133 |
for(j = start; j < start + len; ++j) { |
| 134 |
t = a[j]; |
| 135 |
a[j] = t + a[j + len]; |
| 136 |
a[j + len] = t - a[j + len]; |
| 137 |
a[j + len] = montgomery_reduce((int64_t)zeta * a[j + len]); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
for(j = 0; j < N; ++j) { |
| 143 |
a[j] = montgomery_reduce((int64_t)f * a[j]); |
| 144 |
} |
| 145 |
} |
| 146 |
/*************** dilithium/ref/rounding.h */ |
| 147 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 148 |
static int32_t decompose_88(int32_t *a0, int32_t a); |
| 149 |
#endif |
| 150 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 151 |
static int32_t decompose_32(int32_t *a0, int32_t a); |
| 152 |
#endif |
| 153 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 154 |
static unsigned int make_hint_88(int32_t a0, int32_t a1); |
| 155 |
#endif |
| 156 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 157 |
static unsigned int make_hint_32(int32_t a0, int32_t a1); |
| 158 |
#endif |
| 159 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 160 |
static int32_t use_hint_88(int32_t a, unsigned int hint); |
| 161 |
#endif |
| 162 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 163 |
static int32_t use_hint_32(int32_t a, unsigned int hint); |
| 164 |
#endif |
| 165 |
/*************** dilithium/ref/rounding.c */ |
| 166 |
|
| 167 |
/************************************************* |
| 168 |
* Name: power2round |
| 169 |
* |
| 170 |
* Description: For finite field element a, compute a0, a1 such that |
| 171 |
* a mod^+ Q = a1*2^D + a0 with -2^{D-1} < a0 <= 2^{D-1}. |
| 172 |
* Assumes a to be standard representative. |
| 173 |
* |
| 174 |
* Arguments: - int32_t a: input element |
| 175 |
* - int32_t *a0: pointer to output element a0 |
| 176 |
* |
| 177 |
* Returns a1. |
| 178 |
**************************************************/ |
| 179 |
int32_t power2round(int32_t *a0, int32_t a) { |
| 180 |
int32_t a1; |
| 181 |
|
| 182 |
a1 = (a + (1 << (D-1)) - 1) >> D; |
| 183 |
*a0 = a - (a1 << D); |
| 184 |
return a1; |
| 185 |
} |
| 186 |
|
| 187 |
/************************************************* |
| 188 |
* Name: decompose |
| 189 |
* |
| 190 |
* Description: For finite field element a, compute high and low bits a0, a1 such |
| 191 |
* that a mod^+ Q = a1*ALPHA + a0 with -ALPHA/2 < a0 <= ALPHA/2 except |
| 192 |
* if a1 = (Q-1)/ALPHA where we set a1 = 0 and |
| 193 |
* -ALPHA/2 <= a0 = a mod^+ Q - Q < 0. Assumes a to be standard |
| 194 |
* representative. |
| 195 |
* |
| 196 |
* Arguments: - int32_t a: input element |
| 197 |
* - int32_t *a0: pointer to output element a0 |
| 198 |
* |
| 199 |
* Returns a1. |
| 200 |
**************************************************/ |
| 201 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 202 |
int32_t decompose_88(int32_t *a0, int32_t a) { |
| 203 |
int32_t a1; |
| 204 |
|
| 205 |
a1 = (a + 127) >> 7; |
| 206 |
a1 = (a1*11275 + (1 << 23)) >> 24; |
| 207 |
a1 ^= ((43 - a1) >> 31) & a1; |
| 208 |
|
| 209 |
*a0 = a - a1*2*GAMMA2_88; |
| 210 |
*a0 -= (((Q-1)/2 - *a0) >> 31) & Q; |
| 211 |
return a1; |
| 212 |
} |
| 213 |
#endif |
| 214 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 215 |
int32_t decompose_32(int32_t *a0, int32_t a) { |
| 216 |
int32_t a1; |
| 217 |
|
| 218 |
a1 = (a + 127) >> 7; |
| 219 |
a1 = (a1*1025 + (1 << 21)) >> 22; |
| 220 |
a1 &= 15; |
| 221 |
|
| 222 |
*a0 = a - a1*2*GAMMA2_32; |
| 223 |
*a0 -= (((Q-1)/2 - *a0) >> 31) & Q; |
| 224 |
return a1; |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
/************************************************* |
| 229 |
* Name: make_hint |
| 230 |
* |
| 231 |
* Description: Compute hint bit indicating whether the low bits of the |
| 232 |
* input element overflow into the high bits. |
| 233 |
* |
| 234 |
* Arguments: - int32_t a0: low bits of input element |
| 235 |
* - int32_t a1: high bits of input element |
| 236 |
* |
| 237 |
* Returns 1 if overflow. |
| 238 |
**************************************************/ |
| 239 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 240 |
unsigned int make_hint_88(int32_t a0, int32_t a1) { |
| 241 |
if(a0 > GAMMA2_88 || a0 < -GAMMA2_88 || (a0 == -GAMMA2_88 && a1 != 0)) |
| 242 |
return 1; |
| 243 |
|
| 244 |
return 0; |
| 245 |
} |
| 246 |
#endif |
| 247 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 248 |
unsigned int make_hint_32(int32_t a0, int32_t a1) { |
| 249 |
if(a0 > GAMMA2_32 || a0 < -GAMMA2_32 || (a0 == -GAMMA2_32 && a1 != 0)) |
| 250 |
return 1; |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
#endif |
| 255 |
|
| 256 |
/************************************************* |
| 257 |
* Name: use_hint |
| 258 |
* |
| 259 |
* Description: Correct high bits according to hint. |
| 260 |
* |
| 261 |
* Arguments: - int32_t a: input element |
| 262 |
* - unsigned int hint: hint bit |
| 263 |
* |
| 264 |
* Returns corrected high bits. |
| 265 |
**************************************************/ |
| 266 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 267 |
int32_t use_hint_88(int32_t a, unsigned int hint) { |
| 268 |
int32_t a0, a1; |
| 269 |
|
| 270 |
a1 = decompose_88(&a0, a); |
| 271 |
if(hint == 0) |
| 272 |
return a1; |
| 273 |
|
| 274 |
if(a0 > 0) |
| 275 |
return (a1 == 43) ? 0 : a1 + 1; |
| 276 |
else |
| 277 |
return (a1 == 0) ? 43 : a1 - 1; |
| 278 |
} |
| 279 |
#endif |
| 280 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 281 |
int32_t use_hint_32(int32_t a, unsigned int hint) { |
| 282 |
int32_t a0, a1; |
| 283 |
|
| 284 |
a1 = decompose_32(&a0, a); |
| 285 |
if(hint == 0) |
| 286 |
return a1; |
| 287 |
|
| 288 |
if(a0 > 0) |
| 289 |
return (a1 + 1) & 15; |
| 290 |
else |
| 291 |
return (a1 - 1) & 15; |
| 292 |
} |
| 293 |
#endif |
| 294 |
/*************** dilithium/ref/poly.c */ |
| 295 |
|
| 296 |
#ifdef DBENCH |
| 297 |
extern const uint64_t timing_overhead; |
| 298 |
extern uint64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack; |
| 299 |
#define DBENCH_START() uint64_t time = cpucycles() |
| 300 |
#define DBENCH_STOP(t) t += cpucycles() - time - timing_overhead |
| 301 |
#else |
| 302 |
#define DBENCH_START() |
| 303 |
#define DBENCH_STOP(t) |
| 304 |
#endif |
| 305 |
|
| 306 |
/************************************************* |
| 307 |
* Name: poly_reduce |
| 308 |
* |
| 309 |
* Description: Inplace reduction of all coefficients of polynomial to |
| 310 |
* representative in [-6283008,6283008]. |
| 311 |
* |
| 312 |
* Arguments: - poly *a: pointer to input/output polynomial |
| 313 |
**************************************************/ |
| 314 |
void poly_reduce(poly *a) { |
| 315 |
unsigned int i; |
| 316 |
DBENCH_START(); |
| 317 |
|
| 318 |
for(i = 0; i < N; ++i) |
| 319 |
a->coeffs[i] = reduce32(a->coeffs[i]); |
| 320 |
|
| 321 |
DBENCH_STOP(*tred); |
| 322 |
} |
| 323 |
|
| 324 |
/************************************************* |
| 325 |
* Name: poly_caddq |
| 326 |
* |
| 327 |
* Description: For all coefficients of in/out polynomial add Q if |
| 328 |
* coefficient is negative. |
| 329 |
* |
| 330 |
* Arguments: - poly *a: pointer to input/output polynomial |
| 331 |
**************************************************/ |
| 332 |
void poly_caddq(poly *a) { |
| 333 |
unsigned int i; |
| 334 |
DBENCH_START(); |
| 335 |
|
| 336 |
for(i = 0; i < N; ++i) |
| 337 |
a->coeffs[i] = caddq(a->coeffs[i]); |
| 338 |
|
| 339 |
DBENCH_STOP(*tred); |
| 340 |
} |
| 341 |
|
| 342 |
/************************************************* |
| 343 |
* Name: poly_add |
| 344 |
* |
| 345 |
* Description: Add polynomials. No modular reduction is performed. |
| 346 |
* |
| 347 |
* Arguments: - poly *c: pointer to output polynomial |
| 348 |
* - const poly *a: pointer to first summand |
| 349 |
* - const poly *b: pointer to second summand |
| 350 |
**************************************************/ |
| 351 |
void poly_add(poly *c, const poly *a, const poly *b) { |
| 352 |
unsigned int i; |
| 353 |
DBENCH_START(); |
| 354 |
|
| 355 |
for(i = 0; i < N; ++i) |
| 356 |
c->coeffs[i] = a->coeffs[i] + b->coeffs[i]; |
| 357 |
|
| 358 |
DBENCH_STOP(*tadd); |
| 359 |
} |
| 360 |
|
| 361 |
/************************************************* |
| 362 |
* Name: poly_sub |
| 363 |
* |
| 364 |
* Description: Subtract polynomials. No modular reduction is |
| 365 |
* performed. |
| 366 |
* |
| 367 |
* Arguments: - poly *c: pointer to output polynomial |
| 368 |
* - const poly *a: pointer to first input polynomial |
| 369 |
* - const poly *b: pointer to second input polynomial to be |
| 370 |
* subtraced from first input polynomial |
| 371 |
**************************************************/ |
| 372 |
void poly_sub(poly *c, const poly *a, const poly *b) { |
| 373 |
unsigned int i; |
| 374 |
DBENCH_START(); |
| 375 |
|
| 376 |
for(i = 0; i < N; ++i) |
| 377 |
c->coeffs[i] = a->coeffs[i] - b->coeffs[i]; |
| 378 |
|
| 379 |
DBENCH_STOP(*tadd); |
| 380 |
} |
| 381 |
|
| 382 |
/************************************************* |
| 383 |
* Name: poly_shiftl |
| 384 |
* |
| 385 |
* Description: Multiply polynomial by 2^D without modular reduction. Assumes |
| 386 |
* input coefficients to be less than 2^{31-D} in absolute value. |
| 387 |
* |
| 388 |
* Arguments: - poly *a: pointer to input/output polynomial |
| 389 |
**************************************************/ |
| 390 |
void poly_shiftl(poly *a) { |
| 391 |
unsigned int i; |
| 392 |
DBENCH_START(); |
| 393 |
|
| 394 |
for(i = 0; i < N; ++i) |
| 395 |
a->coeffs[i] <<= D; |
| 396 |
|
| 397 |
DBENCH_STOP(*tmul); |
| 398 |
} |
| 399 |
|
| 400 |
/************************************************* |
| 401 |
* Name: poly_ntt |
| 402 |
* |
| 403 |
* Description: Inplace forward NTT. Coefficients can grow by |
| 404 |
* 8*Q in absolute value. |
| 405 |
* |
| 406 |
* Arguments: - poly *a: pointer to input/output polynomial |
| 407 |
**************************************************/ |
| 408 |
void poly_ntt(poly *a) { |
| 409 |
DBENCH_START(); |
| 410 |
|
| 411 |
ntt(a->coeffs); |
| 412 |
|
| 413 |
DBENCH_STOP(*tmul); |
| 414 |
} |
| 415 |
|
| 416 |
/************************************************* |
| 417 |
* Name: poly_invntt_tomont |
| 418 |
* |
| 419 |
* Description: Inplace inverse NTT and multiplication by 2^{32}. |
| 420 |
* Input coefficients need to be less than Q in absolute |
| 421 |
* value and output coefficients are again bounded by Q. |
| 422 |
* |
| 423 |
* Arguments: - poly *a: pointer to input/output polynomial |
| 424 |
**************************************************/ |
| 425 |
void poly_invntt_tomont(poly *a) { |
| 426 |
DBENCH_START(); |
| 427 |
|
| 428 |
invntt_tomont(a->coeffs); |
| 429 |
|
| 430 |
DBENCH_STOP(*tmul); |
| 431 |
} |
| 432 |
|
| 433 |
/************************************************* |
| 434 |
* Name: poly_pointwise_montgomery |
| 435 |
* |
| 436 |
* Description: Pointwise multiplication of polynomials in NTT domain |
| 437 |
* representation and multiplication of resulting polynomial |
| 438 |
* by 2^{-32}. |
| 439 |
* |
| 440 |
* Arguments: - poly *c: pointer to output polynomial |
| 441 |
* - const poly *a: pointer to first input polynomial |
| 442 |
* - const poly *b: pointer to second input polynomial |
| 443 |
**************************************************/ |
| 444 |
void poly_pointwise_montgomery(poly *c, const poly *a, const poly *b) { |
| 445 |
unsigned int i; |
| 446 |
DBENCH_START(); |
| 447 |
|
| 448 |
for(i = 0; i < N; ++i) |
| 449 |
c->coeffs[i] = montgomery_reduce((int64_t)a->coeffs[i] * b->coeffs[i]); |
| 450 |
|
| 451 |
DBENCH_STOP(*tmul); |
| 452 |
} |
| 453 |
|
| 454 |
/************************************************* |
| 455 |
* Name: poly_power2round |
| 456 |
* |
| 457 |
* Description: For all coefficients c of the input polynomial, |
| 458 |
* compute c0, c1 such that c mod Q = c1*2^D + c0 |
| 459 |
* with -2^{D-1} < c0 <= 2^{D-1}. Assumes coefficients to be |
| 460 |
* standard representatives. |
| 461 |
* |
| 462 |
* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 |
| 463 |
* - poly *a0: pointer to output polynomial with coefficients c0 |
| 464 |
* - const poly *a: pointer to input polynomial |
| 465 |
**************************************************/ |
| 466 |
void poly_power2round(poly *a1, poly *a0, const poly *a) { |
| 467 |
unsigned int i; |
| 468 |
DBENCH_START(); |
| 469 |
|
| 470 |
for(i = 0; i < N; ++i) |
| 471 |
a1->coeffs[i] = power2round(&a0->coeffs[i], a->coeffs[i]); |
| 472 |
|
| 473 |
DBENCH_STOP(*tround); |
| 474 |
} |
| 475 |
|
| 476 |
/************************************************* |
| 477 |
* Name: poly_decompose |
| 478 |
* |
| 479 |
* Description: For all coefficients c of the input polynomial, |
| 480 |
* compute high and low bits c0, c1 such c mod Q = c1*ALPHA + c0 |
| 481 |
* with -ALPHA/2 < c0 <= ALPHA/2 except c1 = (Q-1)/ALPHA where we |
| 482 |
* set c1 = 0 and -ALPHA/2 <= c0 = c mod Q - Q < 0. |
| 483 |
* Assumes coefficients to be standard representatives. |
| 484 |
* |
| 485 |
* Arguments: - poly *a1: pointer to output polynomial with coefficients c1 |
| 486 |
* - poly *a0: pointer to output polynomial with coefficients c0 |
| 487 |
* - const poly *a: pointer to input polynomial |
| 488 |
**************************************************/ |
| 489 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 490 |
static |
| 491 |
void poly_decompose_88(poly *a1, poly *a0, const poly *a) { |
| 492 |
unsigned int i; |
| 493 |
DBENCH_START(); |
| 494 |
|
| 495 |
for(i = 0; i < N; ++i) |
| 496 |
a1->coeffs[i] = decompose_88(&a0->coeffs[i], a->coeffs[i]); |
| 497 |
|
| 498 |
DBENCH_STOP(*tround); |
| 499 |
} |
| 500 |
#endif |
| 501 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 502 |
static |
| 503 |
void poly_decompose_32(poly *a1, poly *a0, const poly *a) { |
| 504 |
unsigned int i; |
| 505 |
DBENCH_START(); |
| 506 |
|
| 507 |
for(i = 0; i < N; ++i) |
| 508 |
a1->coeffs[i] = decompose_32(&a0->coeffs[i], a->coeffs[i]); |
| 509 |
|
| 510 |
DBENCH_STOP(*tround); |
| 511 |
} |
| 512 |
#endif |
| 513 |
|
| 514 |
/************************************************* |
| 515 |
* Name: poly_make_hint |
| 516 |
* |
| 517 |
* Description: Compute hint polynomial. The coefficients of which indicate |
| 518 |
* whether the low bits of the corresponding coefficient of |
| 519 |
* the input polynomial overflow into the high bits. |
| 520 |
* |
| 521 |
* Arguments: - poly *h: pointer to output hint polynomial |
| 522 |
* - const poly *a0: pointer to low part of input polynomial |
| 523 |
* - const poly *a1: pointer to high part of input polynomial |
| 524 |
* |
| 525 |
* Returns number of 1 bits. |
| 526 |
**************************************************/ |
| 527 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 528 |
static |
| 529 |
unsigned int poly_make_hint_88(poly *h, const poly *a0, const poly *a1) { |
| 530 |
unsigned int i, s = 0; |
| 531 |
DBENCH_START(); |
| 532 |
|
| 533 |
for(i = 0; i < N; ++i) { |
| 534 |
h->coeffs[i] = make_hint_88(a0->coeffs[i], a1->coeffs[i]); |
| 535 |
s += h->coeffs[i]; |
| 536 |
} |
| 537 |
|
| 538 |
DBENCH_STOP(*tround); |
| 539 |
return s; |
| 540 |
} |
| 541 |
#endif |
| 542 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 543 |
static |
| 544 |
unsigned int poly_make_hint_32(poly *h, const poly *a0, const poly *a1) { |
| 545 |
unsigned int i, s = 0; |
| 546 |
DBENCH_START(); |
| 547 |
|
| 548 |
for(i = 0; i < N; ++i) { |
| 549 |
h->coeffs[i] = make_hint_32(a0->coeffs[i], a1->coeffs[i]); |
| 550 |
s += h->coeffs[i]; |
| 551 |
} |
| 552 |
|
| 553 |
DBENCH_STOP(*tround); |
| 554 |
return s; |
| 555 |
} |
| 556 |
#endif |
| 557 |
|
| 558 |
/************************************************* |
| 559 |
* Name: poly_use_hint |
| 560 |
* |
| 561 |
* Description: Use hint polynomial to correct the high bits of a polynomial. |
| 562 |
* |
| 563 |
* Arguments: - poly *b: pointer to output polynomial with corrected high bits |
| 564 |
* - const poly *a: pointer to input polynomial |
| 565 |
* - const poly *h: pointer to input hint polynomial |
| 566 |
**************************************************/ |
| 567 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 568 |
static |
| 569 |
void poly_use_hint_88(poly *b, const poly *a, const poly *h) { |
| 570 |
unsigned int i; |
| 571 |
DBENCH_START(); |
| 572 |
|
| 573 |
for(i = 0; i < N; ++i) |
| 574 |
b->coeffs[i] = use_hint_88(a->coeffs[i], h->coeffs[i]); |
| 575 |
|
| 576 |
DBENCH_STOP(*tround); |
| 577 |
} |
| 578 |
#endif |
| 579 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 580 |
static |
| 581 |
void poly_use_hint_32(poly *b, const poly *a, const poly *h) { |
| 582 |
unsigned int i; |
| 583 |
DBENCH_START(); |
| 584 |
|
| 585 |
for(i = 0; i < N; ++i) |
| 586 |
b->coeffs[i] = use_hint_32(a->coeffs[i], h->coeffs[i]); |
| 587 |
|
| 588 |
DBENCH_STOP(*tround); |
| 589 |
} |
| 590 |
#endif |
| 591 |
|
| 592 |
/************************************************* |
| 593 |
* Name: poly_chknorm |
| 594 |
* |
| 595 |
* Description: Check infinity norm of polynomial against given bound. |
| 596 |
* Assumes input coefficients were reduced by reduce32(). |
| 597 |
* |
| 598 |
* Arguments: - const poly *a: pointer to polynomial |
| 599 |
* - int32_t B: norm bound |
| 600 |
* |
| 601 |
* Returns 0 if norm is strictly smaller than B <= (Q-1)/8 and 1 otherwise. |
| 602 |
**************************************************/ |
| 603 |
int poly_chknorm(const poly *a, int32_t B) { |
| 604 |
unsigned int i; |
| 605 |
int32_t t; |
| 606 |
DBENCH_START(); |
| 607 |
|
| 608 |
if(B > (Q-1)/8) |
| 609 |
return 1; |
| 610 |
|
| 611 |
/* It is ok to leak which coefficient violates the bound since |
| 612 |
the probability for each coefficient is independent of secret |
| 613 |
data but we must not leak the sign of the centralized representative. */ |
| 614 |
for(i = 0; i < N; ++i) { |
| 615 |
/* Absolute value */ |
| 616 |
t = a->coeffs[i] >> 31; |
| 617 |
t = a->coeffs[i] - (t & 2*a->coeffs[i]); |
| 618 |
|
| 619 |
if(t >= B) { |
| 620 |
DBENCH_STOP(*tsample); |
| 621 |
return 1; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
DBENCH_STOP(*tsample); |
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
/************************************************* |
| 630 |
* Name: rej_uniform |
| 631 |
* |
| 632 |
* Description: Sample uniformly random coefficients in [0, Q-1] by |
| 633 |
* performing rejection sampling on array of random bytes. |
| 634 |
* |
| 635 |
* Arguments: - int32_t *a: pointer to output array (allocated) |
| 636 |
* - unsigned int len: number of coefficients to be sampled |
| 637 |
* - const uint8_t *buf: array of random bytes |
| 638 |
* - unsigned int buflen: length of array of random bytes |
| 639 |
* |
| 640 |
* Returns number of sampled coefficients. Can be smaller than len if not enough |
| 641 |
* random bytes were given. |
| 642 |
**************************************************/ |
| 643 |
static unsigned int rej_uniform(int32_t *a, |
| 644 |
unsigned int len, |
| 645 |
const uint8_t *buf, |
| 646 |
unsigned int buflen) |
| 647 |
{ |
| 648 |
unsigned int ctr, pos; |
| 649 |
uint32_t t; |
| 650 |
DBENCH_START(); |
| 651 |
|
| 652 |
ctr = pos = 0; |
| 653 |
while(ctr < len && pos + 3 <= buflen) { |
| 654 |
t = buf[pos++]; |
| 655 |
t |= (uint32_t)buf[pos++] << 8; |
| 656 |
t |= (uint32_t)buf[pos++] << 16; |
| 657 |
t &= 0x7FFFFF; |
| 658 |
|
| 659 |
if(t < Q) |
| 660 |
a[ctr++] = t; |
| 661 |
} |
| 662 |
|
| 663 |
DBENCH_STOP(*tsample); |
| 664 |
return ctr; |
| 665 |
} |
| 666 |
|
| 667 |
/************************************************* |
| 668 |
* Name: poly_uniform |
| 669 |
* |
| 670 |
* Description: Sample polynomial with uniformly random coefficients |
| 671 |
* in [0,Q-1] by performing rejection sampling on the |
| 672 |
* output stream of SHAKE128(seed|nonce) |
| 673 |
* |
| 674 |
* Arguments: - poly *a: pointer to output polynomial |
| 675 |
* - const uint8_t seed[]: byte array with seed of length SEEDBYTES |
| 676 |
* - uint16_t nonce: 2-byte nonce |
| 677 |
**************************************************/ |
| 678 |
#define POLY_UNIFORM_NBLOCKS ((768 + STREAM128_BLOCKBYTES - 1)/STREAM128_BLOCKBYTES) |
| 679 |
void poly_uniform(poly *a, |
| 680 |
const uint8_t seed[SEEDBYTES], |
| 681 |
uint16_t nonce) |
| 682 |
{ |
| 683 |
unsigned int i, ctr, off; |
| 684 |
unsigned int buflen = POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES; |
| 685 |
uint8_t buf[POLY_UNIFORM_NBLOCKS*STREAM128_BLOCKBYTES + 2]; |
| 686 |
stream128_state state; |
| 687 |
|
| 688 |
stream128_init(&state, seed, nonce); |
| 689 |
stream128_squeezeblocks(buf, POLY_UNIFORM_NBLOCKS, &state); |
| 690 |
|
| 691 |
ctr = rej_uniform(a->coeffs, N, buf, buflen); |
| 692 |
|
| 693 |
while(ctr < N) { |
| 694 |
off = buflen % 3; |
| 695 |
for(i = 0; i < off; ++i) |
| 696 |
buf[i] = buf[buflen - off + i]; |
| 697 |
|
| 698 |
stream128_squeezeblocks(buf + off, 1, &state); |
| 699 |
buflen = STREAM128_BLOCKBYTES + off; |
| 700 |
ctr += rej_uniform(a->coeffs + ctr, N - ctr, buf, buflen); |
| 701 |
} |
| 702 |
stream128_close(&state); |
| 703 |
} |
| 704 |
|
| 705 |
/************************************************* |
| 706 |
* Name: rej_eta |
| 707 |
* |
| 708 |
* Description: Sample uniformly random coefficients in [-ETA, ETA] by |
| 709 |
* performing rejection sampling on array of random bytes. |
| 710 |
* |
| 711 |
* Arguments: - int32_t *a: pointer to output array (allocated) |
| 712 |
* - unsigned int len: number of coefficients to be sampled |
| 713 |
* - const uint8_t *buf: array of random bytes |
| 714 |
* - unsigned int buflen: length of array of random bytes |
| 715 |
* |
| 716 |
* Returns number of sampled coefficients. Can be smaller than len if not enough |
| 717 |
* random bytes were given. |
| 718 |
**************************************************/ |
| 719 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 || DILITHIUM_MODE == 5 |
| 720 |
static unsigned int rej_eta_2(int32_t *a, |
| 721 |
unsigned int len, |
| 722 |
const uint8_t *buf, |
| 723 |
unsigned int buflen) |
| 724 |
{ |
| 725 |
unsigned int ctr, pos; |
| 726 |
uint32_t t0, t1; |
| 727 |
DBENCH_START(); |
| 728 |
|
| 729 |
ctr = pos = 0; |
| 730 |
while(ctr < len && pos < buflen) { |
| 731 |
t0 = buf[pos] & 0x0F; |
| 732 |
t1 = buf[pos++] >> 4; |
| 733 |
|
| 734 |
if(t0 < 15) { |
| 735 |
t0 = t0 - (205*t0 >> 10)*5; |
| 736 |
a[ctr++] = 2 - t0; |
| 737 |
} |
| 738 |
if(t1 < 15 && ctr < len) { |
| 739 |
t1 = t1 - (205*t1 >> 10)*5; |
| 740 |
a[ctr++] = 2 - t1; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
DBENCH_STOP(*tsample); |
| 745 |
return ctr; |
| 746 |
} |
| 747 |
#endif |
| 748 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 |
| 749 |
static unsigned int rej_eta_4(int32_t *a, |
| 750 |
unsigned int len, |
| 751 |
const uint8_t *buf, |
| 752 |
unsigned int buflen) |
| 753 |
{ |
| 754 |
unsigned int ctr, pos; |
| 755 |
uint32_t t0, t1; |
| 756 |
DBENCH_START(); |
| 757 |
|
| 758 |
ctr = pos = 0; |
| 759 |
while(ctr < len && pos < buflen) { |
| 760 |
t0 = buf[pos] & 0x0F; |
| 761 |
t1 = buf[pos++] >> 4; |
| 762 |
|
| 763 |
if(t0 < 9) |
| 764 |
a[ctr++] = 4 - t0; |
| 765 |
if(t1 < 9 && ctr < len) |
| 766 |
a[ctr++] = 4 - t1; |
| 767 |
} |
| 768 |
|
| 769 |
DBENCH_STOP(*tsample); |
| 770 |
return ctr; |
| 771 |
} |
| 772 |
#endif |
| 773 |
|
| 774 |
/************************************************* |
| 775 |
* Name: poly_uniform_eta |
| 776 |
* |
| 777 |
* Description: Sample polynomial with uniformly random coefficients |
| 778 |
* in [-ETA,ETA] by performing rejection sampling on the |
| 779 |
* output stream from SHAKE256(seed|nonce) |
| 780 |
* |
| 781 |
* Arguments: - poly *a: pointer to output polynomial |
| 782 |
* - const uint8_t seed[]: byte array with seed of length CRHBYTES |
| 783 |
* - uint16_t nonce: 2-byte nonce |
| 784 |
**************************************************/ |
| 785 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 || DILITHIUM_MODE == 5 |
| 786 |
#define POLY_UNIFORM_ETA_NBLOCKS_2 ((136 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) |
| 787 |
static |
| 788 |
void poly_uniform_eta_2(poly *a, |
| 789 |
const uint8_t seed[CRHBYTES], |
| 790 |
uint16_t nonce) |
| 791 |
{ |
| 792 |
unsigned int ctr; |
| 793 |
unsigned int buflen = POLY_UNIFORM_ETA_NBLOCKS_2*STREAM256_BLOCKBYTES; |
| 794 |
uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS_2*STREAM256_BLOCKBYTES]; |
| 795 |
stream256_state state; |
| 796 |
|
| 797 |
stream256_init(&state, seed, nonce); |
| 798 |
stream256_squeezeblocks(buf, POLY_UNIFORM_ETA_NBLOCKS_2, &state); |
| 799 |
|
| 800 |
ctr = rej_eta_2(a->coeffs, N, buf, buflen); |
| 801 |
|
| 802 |
while(ctr < N) { |
| 803 |
stream256_squeezeblocks(buf, 1, &state); |
| 804 |
ctr += rej_eta_2(a->coeffs + ctr, N - ctr, buf, STREAM256_BLOCKBYTES); |
| 805 |
} |
| 806 |
stream256_close(&state); |
| 807 |
} |
| 808 |
#endif |
| 809 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 |
| 810 |
#define POLY_UNIFORM_ETA_NBLOCKS_4 ((227 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) |
| 811 |
static |
| 812 |
void poly_uniform_eta_4(poly *a, |
| 813 |
const uint8_t seed[CRHBYTES], |
| 814 |
uint16_t nonce) |
| 815 |
{ |
| 816 |
unsigned int ctr; |
| 817 |
unsigned int buflen = POLY_UNIFORM_ETA_NBLOCKS_4*STREAM256_BLOCKBYTES; |
| 818 |
uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS_4*STREAM256_BLOCKBYTES]; |
| 819 |
stream256_state state; |
| 820 |
|
| 821 |
stream256_init(&state, seed, nonce); |
| 822 |
stream256_squeezeblocks(buf, POLY_UNIFORM_ETA_NBLOCKS_4, &state); |
| 823 |
|
| 824 |
ctr = rej_eta_4(a->coeffs, N, buf, buflen); |
| 825 |
|
| 826 |
while(ctr < N) { |
| 827 |
stream256_squeezeblocks(buf, 1, &state); |
| 828 |
ctr += rej_eta_4(a->coeffs + ctr, N - ctr, buf, STREAM256_BLOCKBYTES); |
| 829 |
} |
| 830 |
stream256_close(&state); |
| 831 |
} |
| 832 |
#endif |
| 833 |
|
| 834 |
/************************************************* |
| 835 |
* Name: poly_uniform_gamma1m1 |
| 836 |
* |
| 837 |
* Description: Sample polynomial with uniformly random coefficients |
| 838 |
* in [-(GAMMA1 - 1), GAMMA1] by unpacking output stream |
| 839 |
* of SHAKE256(seed|nonce) |
| 840 |
* |
| 841 |
* Arguments: - poly *a: pointer to output polynomial |
| 842 |
* - const uint8_t seed[]: byte array with seed of length CRHBYTES |
| 843 |
* - uint16_t nonce: 16-bit nonce |
| 844 |
**************************************************/ |
| 845 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 846 |
#define POLY_UNIFORM_GAMMA1_NBLOCKS_17 ((POLYZ_PACKEDBYTES_17 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) |
| 847 |
static void polyz_unpack_17(poly *r, const uint8_t *a);/* Forward declarations */ |
| 848 |
static |
| 849 |
void poly_uniform_gamma1_17(poly *a, |
| 850 |
const uint8_t seed[CRHBYTES], |
| 851 |
uint16_t nonce) |
| 852 |
{ |
| 853 |
uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS_17*STREAM256_BLOCKBYTES]; |
| 854 |
stream256_state state; |
| 855 |
|
| 856 |
stream256_init(&state, seed, nonce); |
| 857 |
stream256_squeezeblocks(buf, POLY_UNIFORM_GAMMA1_NBLOCKS_17, &state); |
| 858 |
polyz_unpack_17(a, buf); |
| 859 |
stream256_close(&state); |
| 860 |
} |
| 861 |
#endif |
| 862 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 863 |
#define POLY_UNIFORM_GAMMA1_NBLOCKS_19 ((POLYZ_PACKEDBYTES_19 + STREAM256_BLOCKBYTES - 1)/STREAM256_BLOCKBYTES) |
| 864 |
static void polyz_unpack_19(poly *r, const uint8_t *a);/* Forward declarations */ |
| 865 |
static |
| 866 |
void poly_uniform_gamma1_19(poly *a, |
| 867 |
const uint8_t seed[CRHBYTES], |
| 868 |
uint16_t nonce) |
| 869 |
{ |
| 870 |
uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS_19*STREAM256_BLOCKBYTES]; |
| 871 |
stream256_state state; |
| 872 |
|
| 873 |
stream256_init(&state, seed, nonce); |
| 874 |
stream256_squeezeblocks(buf, POLY_UNIFORM_GAMMA1_NBLOCKS_19, &state); |
| 875 |
polyz_unpack_19(a, buf); |
| 876 |
stream256_close(&state); |
| 877 |
} |
| 878 |
#endif |
| 879 |
|
| 880 |
/************************************************* |
| 881 |
* Name: polyeta_pack |
| 882 |
* |
| 883 |
* Description: Bit-pack polynomial with coefficients in [-ETA,ETA]. |
| 884 |
* |
| 885 |
* Arguments: - uint8_t *r: pointer to output byte array with at least |
| 886 |
* POLYETA_PACKEDBYTES bytes |
| 887 |
* - const poly *a: pointer to input polynomial |
| 888 |
**************************************************/ |
| 889 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 || DILITHIUM_MODE == 5 |
| 890 |
static |
| 891 |
void polyeta_pack_2(uint8_t *r, const poly *a) { |
| 892 |
unsigned int i; |
| 893 |
uint8_t t[8]; |
| 894 |
DBENCH_START(); |
| 895 |
|
| 896 |
for(i = 0; i < N/8; ++i) { |
| 897 |
t[0] = ETA2 - a->coeffs[8*i+0]; |
| 898 |
t[1] = ETA2 - a->coeffs[8*i+1]; |
| 899 |
t[2] = ETA2 - a->coeffs[8*i+2]; |
| 900 |
t[3] = ETA2 - a->coeffs[8*i+3]; |
| 901 |
t[4] = ETA2 - a->coeffs[8*i+4]; |
| 902 |
t[5] = ETA2 - a->coeffs[8*i+5]; |
| 903 |
t[6] = ETA2 - a->coeffs[8*i+6]; |
| 904 |
t[7] = ETA2 - a->coeffs[8*i+7]; |
| 905 |
|
| 906 |
r[3*i+0] = (t[0] >> 0) | (t[1] << 3) | (t[2] << 6); |
| 907 |
r[3*i+1] = (t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7); |
| 908 |
r[3*i+2] = (t[5] >> 1) | (t[6] << 2) | (t[7] << 5); |
| 909 |
} |
| 910 |
|
| 911 |
DBENCH_STOP(*tpack); |
| 912 |
} |
| 913 |
#endif |
| 914 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 |
| 915 |
static |
| 916 |
void polyeta_pack_4(uint8_t *r, const poly *a) { |
| 917 |
unsigned int i; |
| 918 |
uint8_t t[8]; |
| 919 |
DBENCH_START(); |
| 920 |
|
| 921 |
for(i = 0; i < N/2; ++i) { |
| 922 |
t[0] = ETA4 - a->coeffs[2*i+0]; |
| 923 |
t[1] = ETA4 - a->coeffs[2*i+1]; |
| 924 |
r[i] = t[0] | (t[1] << 4); |
| 925 |
} |
| 926 |
|
| 927 |
DBENCH_STOP(*tpack); |
| 928 |
} |
| 929 |
#endif |
| 930 |
|
| 931 |
/************************************************* |
| 932 |
* Name: polyeta_unpack |
| 933 |
* |
| 934 |
* Description: Unpack polynomial with coefficients in [-ETA,ETA]. |
| 935 |
* |
| 936 |
* Arguments: - poly *r: pointer to output polynomial |
| 937 |
* - const uint8_t *a: byte array with bit-packed polynomial |
| 938 |
**************************************************/ |
| 939 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 || DILITHIUM_MODE == 5 |
| 940 |
static |
| 941 |
void polyeta_unpack_2(poly *r, const uint8_t *a) { |
| 942 |
unsigned int i; |
| 943 |
DBENCH_START(); |
| 944 |
|
| 945 |
for(i = 0; i < N/8; ++i) { |
| 946 |
r->coeffs[8*i+0] = (a[3*i+0] >> 0) & 7; |
| 947 |
r->coeffs[8*i+1] = (a[3*i+0] >> 3) & 7; |
| 948 |
r->coeffs[8*i+2] = ((a[3*i+0] >> 6) | (a[3*i+1] << 2)) & 7; |
| 949 |
r->coeffs[8*i+3] = (a[3*i+1] >> 1) & 7; |
| 950 |
r->coeffs[8*i+4] = (a[3*i+1] >> 4) & 7; |
| 951 |
r->coeffs[8*i+5] = ((a[3*i+1] >> 7) | (a[3*i+2] << 1)) & 7; |
| 952 |
r->coeffs[8*i+6] = (a[3*i+2] >> 2) & 7; |
| 953 |
r->coeffs[8*i+7] = (a[3*i+2] >> 5) & 7; |
| 954 |
|
| 955 |
r->coeffs[8*i+0] = ETA2 - r->coeffs[8*i+0]; |
| 956 |
r->coeffs[8*i+1] = ETA2 - r->coeffs[8*i+1]; |
| 957 |
r->coeffs[8*i+2] = ETA2 - r->coeffs[8*i+2]; |
| 958 |
r->coeffs[8*i+3] = ETA2 - r->coeffs[8*i+3]; |
| 959 |
r->coeffs[8*i+4] = ETA2 - r->coeffs[8*i+4]; |
| 960 |
r->coeffs[8*i+5] = ETA2 - r->coeffs[8*i+5]; |
| 961 |
r->coeffs[8*i+6] = ETA2 - r->coeffs[8*i+6]; |
| 962 |
r->coeffs[8*i+7] = ETA2 - r->coeffs[8*i+7]; |
| 963 |
} |
| 964 |
|
| 965 |
DBENCH_STOP(*tpack); |
| 966 |
} |
| 967 |
#endif |
| 968 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 |
| 969 |
static |
| 970 |
void polyeta_unpack_4(poly *r, const uint8_t *a) { |
| 971 |
unsigned int i; |
| 972 |
DBENCH_START(); |
| 973 |
|
| 974 |
for(i = 0; i < N/2; ++i) { |
| 975 |
r->coeffs[2*i+0] = a[i] & 0x0F; |
| 976 |
r->coeffs[2*i+1] = a[i] >> 4; |
| 977 |
r->coeffs[2*i+0] = ETA4 - r->coeffs[2*i+0]; |
| 978 |
r->coeffs[2*i+1] = ETA4 - r->coeffs[2*i+1]; |
| 979 |
} |
| 980 |
|
| 981 |
DBENCH_STOP(*tpack); |
| 982 |
} |
| 983 |
#endif |
| 984 |
|
| 985 |
/************************************************* |
| 986 |
* Name: polyt1_pack |
| 987 |
* |
| 988 |
* Description: Bit-pack polynomial t1 with coefficients fitting in 10 bits. |
| 989 |
* Input coefficients are assumed to be standard representatives. |
| 990 |
* |
| 991 |
* Arguments: - uint8_t *r: pointer to output byte array with at least |
| 992 |
* POLYT1_PACKEDBYTES bytes |
| 993 |
* - const poly *a: pointer to input polynomial |
| 994 |
**************************************************/ |
| 995 |
void polyt1_pack(uint8_t *r, const poly *a) { |
| 996 |
unsigned int i; |
| 997 |
DBENCH_START(); |
| 998 |
|
| 999 |
for(i = 0; i < N/4; ++i) { |
| 1000 |
r[5*i+0] = (a->coeffs[4*i+0] >> 0); |
| 1001 |
r[5*i+1] = (a->coeffs[4*i+0] >> 8) | (a->coeffs[4*i+1] << 2); |
| 1002 |
r[5*i+2] = (a->coeffs[4*i+1] >> 6) | (a->coeffs[4*i+2] << 4); |
| 1003 |
r[5*i+3] = (a->coeffs[4*i+2] >> 4) | (a->coeffs[4*i+3] << 6); |
| 1004 |
r[5*i+4] = (a->coeffs[4*i+3] >> 2); |
| 1005 |
} |
| 1006 |
|
| 1007 |
DBENCH_STOP(*tpack); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/************************************************* |
| 1011 |
* Name: polyt1_unpack |
| 1012 |
* |
| 1013 |
* Description: Unpack polynomial t1 with 10-bit coefficients. |
| 1014 |
* Output coefficients are standard representatives. |
| 1015 |
* |
| 1016 |
* Arguments: - poly *r: pointer to output polynomial |
| 1017 |
* - const uint8_t *a: byte array with bit-packed polynomial |
| 1018 |
**************************************************/ |
| 1019 |
void polyt1_unpack(poly *r, const uint8_t *a) { |
| 1020 |
unsigned int i; |
| 1021 |
DBENCH_START(); |
| 1022 |
|
| 1023 |
for(i = 0; i < N/4; ++i) { |
| 1024 |
r->coeffs[4*i+0] = ((a[5*i+0] >> 0) | ((uint32_t)a[5*i+1] << 8)) & 0x3FF; |
| 1025 |
r->coeffs[4*i+1] = ((a[5*i+1] >> 2) | ((uint32_t)a[5*i+2] << 6)) & 0x3FF; |
| 1026 |
r->coeffs[4*i+2] = ((a[5*i+2] >> 4) | ((uint32_t)a[5*i+3] << 4)) & 0x3FF; |
| 1027 |
r->coeffs[4*i+3] = ((a[5*i+3] >> 6) | ((uint32_t)a[5*i+4] << 2)) & 0x3FF; |
| 1028 |
} |
| 1029 |
|
| 1030 |
DBENCH_STOP(*tpack); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/************************************************* |
| 1034 |
* Name: polyt0_pack |
| 1035 |
* |
| 1036 |
* Description: Bit-pack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. |
| 1037 |
* |
| 1038 |
* Arguments: - uint8_t *r: pointer to output byte array with at least |
| 1039 |
* POLYT0_PACKEDBYTES bytes |
| 1040 |
* - const poly *a: pointer to input polynomial |
| 1041 |
**************************************************/ |
| 1042 |
void polyt0_pack(uint8_t *r, const poly *a) { |
| 1043 |
unsigned int i; |
| 1044 |
uint32_t t[8]; |
| 1045 |
DBENCH_START(); |
| 1046 |
|
| 1047 |
for(i = 0; i < N/8; ++i) { |
| 1048 |
t[0] = (1 << (D-1)) - a->coeffs[8*i+0]; |
| 1049 |
t[1] = (1 << (D-1)) - a->coeffs[8*i+1]; |
| 1050 |
t[2] = (1 << (D-1)) - a->coeffs[8*i+2]; |
| 1051 |
t[3] = (1 << (D-1)) - a->coeffs[8*i+3]; |
| 1052 |
t[4] = (1 << (D-1)) - a->coeffs[8*i+4]; |
| 1053 |
t[5] = (1 << (D-1)) - a->coeffs[8*i+5]; |
| 1054 |
t[6] = (1 << (D-1)) - a->coeffs[8*i+6]; |
| 1055 |
t[7] = (1 << (D-1)) - a->coeffs[8*i+7]; |
| 1056 |
|
| 1057 |
r[13*i+ 0] = t[0]; |
| 1058 |
r[13*i+ 1] = t[0] >> 8; |
| 1059 |
r[13*i+ 1] |= t[1] << 5; |
| 1060 |
r[13*i+ 2] = t[1] >> 3; |
| 1061 |
r[13*i+ 3] = t[1] >> 11; |
| 1062 |
r[13*i+ 3] |= t[2] << 2; |
| 1063 |
r[13*i+ 4] = t[2] >> 6; |
| 1064 |
r[13*i+ 4] |= t[3] << 7; |
| 1065 |
r[13*i+ 5] = t[3] >> 1; |
| 1066 |
r[13*i+ 6] = t[3] >> 9; |
| 1067 |
r[13*i+ 6] |= t[4] << 4; |
| 1068 |
r[13*i+ 7] = t[4] >> 4; |
| 1069 |
r[13*i+ 8] = t[4] >> 12; |
| 1070 |
r[13*i+ 8] |= t[5] << 1; |
| 1071 |
r[13*i+ 9] = t[5] >> 7; |
| 1072 |
r[13*i+ 9] |= t[6] << 6; |
| 1073 |
r[13*i+10] = t[6] >> 2; |
| 1074 |
r[13*i+11] = t[6] >> 10; |
| 1075 |
r[13*i+11] |= t[7] << 3; |
| 1076 |
r[13*i+12] = t[7] >> 5; |
| 1077 |
} |
| 1078 |
|
| 1079 |
DBENCH_STOP(*tpack); |
| 1080 |
} |
| 1081 |
|
| 1082 |
/************************************************* |
| 1083 |
* Name: polyt0_unpack |
| 1084 |
* |
| 1085 |
* Description: Unpack polynomial t0 with coefficients in ]-2^{D-1}, 2^{D-1}]. |
| 1086 |
* |
| 1087 |
* Arguments: - poly *r: pointer to output polynomial |
| 1088 |
* - const uint8_t *a: byte array with bit-packed polynomial |
| 1089 |
**************************************************/ |
| 1090 |
void polyt0_unpack(poly *r, const uint8_t *a) { |
| 1091 |
unsigned int i; |
| 1092 |
DBENCH_START(); |
| 1093 |
|
| 1094 |
for(i = 0; i < N/8; ++i) { |
| 1095 |
r->coeffs[8*i+0] = a[13*i+0]; |
| 1096 |
r->coeffs[8*i+0] |= (uint32_t)a[13*i+1] << 8; |
| 1097 |
r->coeffs[8*i+0] &= 0x1FFF; |
| 1098 |
|
| 1099 |
r->coeffs[8*i+1] = a[13*i+1] >> 5; |
| 1100 |
r->coeffs[8*i+1] |= (uint32_t)a[13*i+2] << 3; |
| 1101 |
r->coeffs[8*i+1] |= (uint32_t)a[13*i+3] << 11; |
| 1102 |
r->coeffs[8*i+1] &= 0x1FFF; |
| 1103 |
|
| 1104 |
r->coeffs[8*i+2] = a[13*i+3] >> 2; |
| 1105 |
r->coeffs[8*i+2] |= (uint32_t)a[13*i+4] << 6; |
| 1106 |
r->coeffs[8*i+2] &= 0x1FFF; |
| 1107 |
|
| 1108 |
r->coeffs[8*i+3] = a[13*i+4] >> 7; |
| 1109 |
r->coeffs[8*i+3] |= (uint32_t)a[13*i+5] << 1; |
| 1110 |
r->coeffs[8*i+3] |= (uint32_t)a[13*i+6] << 9; |
| 1111 |
r->coeffs[8*i+3] &= 0x1FFF; |
| 1112 |
|
| 1113 |
r->coeffs[8*i+4] = a[13*i+6] >> 4; |
| 1114 |
r->coeffs[8*i+4] |= (uint32_t)a[13*i+7] << 4; |
| 1115 |
r->coeffs[8*i+4] |= (uint32_t)a[13*i+8] << 12; |
| 1116 |
r->coeffs[8*i+4] &= 0x1FFF; |
| 1117 |
|
| 1118 |
r->coeffs[8*i+5] = a[13*i+8] >> 1; |
| 1119 |
r->coeffs[8*i+5] |= (uint32_t)a[13*i+9] << 7; |
| 1120 |
r->coeffs[8*i+5] &= 0x1FFF; |
| 1121 |
|
| 1122 |
r->coeffs[8*i+6] = a[13*i+9] >> 6; |
| 1123 |
r->coeffs[8*i+6] |= (uint32_t)a[13*i+10] << 2; |
| 1124 |
r->coeffs[8*i+6] |= (uint32_t)a[13*i+11] << 10; |
| 1125 |
r->coeffs[8*i+6] &= 0x1FFF; |
| 1126 |
|
| 1127 |
r->coeffs[8*i+7] = a[13*i+11] >> 3; |
| 1128 |
r->coeffs[8*i+7] |= (uint32_t)a[13*i+12] << 5; |
| 1129 |
r->coeffs[8*i+7] &= 0x1FFF; |
| 1130 |
|
| 1131 |
r->coeffs[8*i+0] = (1 << (D-1)) - r->coeffs[8*i+0]; |
| 1132 |
r->coeffs[8*i+1] = (1 << (D-1)) - r->coeffs[8*i+1]; |
| 1133 |
r->coeffs[8*i+2] = (1 << (D-1)) - r->coeffs[8*i+2]; |
| 1134 |
r->coeffs[8*i+3] = (1 << (D-1)) - r->coeffs[8*i+3]; |
| 1135 |
r->coeffs[8*i+4] = (1 << (D-1)) - r->coeffs[8*i+4]; |
| 1136 |
r->coeffs[8*i+5] = (1 << (D-1)) - r->coeffs[8*i+5]; |
| 1137 |
r->coeffs[8*i+6] = (1 << (D-1)) - r->coeffs[8*i+6]; |
| 1138 |
r->coeffs[8*i+7] = (1 << (D-1)) - r->coeffs[8*i+7]; |
| 1139 |
} |
| 1140 |
|
| 1141 |
DBENCH_STOP(*tpack); |
| 1142 |
} |
| 1143 |
|
| 1144 |
/************************************************* |
| 1145 |
* Name: polyz_pack |
| 1146 |
* |
| 1147 |
* Description: Bit-pack polynomial with coefficients |
| 1148 |
* in [-(GAMMA1 - 1), GAMMA1]. |
| 1149 |
* |
| 1150 |
* Arguments: - uint8_t *r: pointer to output byte array with at least |
| 1151 |
* POLYZ_PACKEDBYTES bytes |
| 1152 |
* - const poly *a: pointer to input polynomial |
| 1153 |
**************************************************/ |
| 1154 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 1155 |
static |
| 1156 |
void polyz_pack_17(uint8_t *r, const poly *a) { |
| 1157 |
unsigned int i; |
| 1158 |
uint32_t t[4]; |
| 1159 |
DBENCH_START(); |
| 1160 |
|
| 1161 |
for(i = 0; i < N/4; ++i) { |
| 1162 |
t[0] = GAMMA1_17 - a->coeffs[4*i+0]; |
| 1163 |
t[1] = GAMMA1_17 - a->coeffs[4*i+1]; |
| 1164 |
t[2] = GAMMA1_17 - a->coeffs[4*i+2]; |
| 1165 |
t[3] = GAMMA1_17 - a->coeffs[4*i+3]; |
| 1166 |
|
| 1167 |
r[9*i+0] = t[0]; |
| 1168 |
r[9*i+1] = t[0] >> 8; |
| 1169 |
r[9*i+2] = t[0] >> 16; |
| 1170 |
r[9*i+2] |= t[1] << 2; |
| 1171 |
r[9*i+3] = t[1] >> 6; |
| 1172 |
r[9*i+4] = t[1] >> 14; |
| 1173 |
r[9*i+4] |= t[2] << 4; |
| 1174 |
r[9*i+5] = t[2] >> 4; |
| 1175 |
r[9*i+6] = t[2] >> 12; |
| 1176 |
r[9*i+6] |= t[3] << 6; |
| 1177 |
r[9*i+7] = t[3] >> 2; |
| 1178 |
r[9*i+8] = t[3] >> 10; |
| 1179 |
} |
| 1180 |
|
| 1181 |
DBENCH_STOP(*tpack); |
| 1182 |
} |
| 1183 |
#endif |
| 1184 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 1185 |
static |
| 1186 |
void polyz_pack_19(uint8_t *r, const poly *a) { |
| 1187 |
unsigned int i; |
| 1188 |
uint32_t t[4]; |
| 1189 |
DBENCH_START(); |
| 1190 |
|
| 1191 |
for(i = 0; i < N/2; ++i) { |
| 1192 |
t[0] = GAMMA1_19 - a->coeffs[2*i+0]; |
| 1193 |
t[1] = GAMMA1_19 - a->coeffs[2*i+1]; |
| 1194 |
|
| 1195 |
r[5*i+0] = t[0]; |
| 1196 |
r[5*i+1] = t[0] >> 8; |
| 1197 |
r[5*i+2] = t[0] >> 16; |
| 1198 |
r[5*i+2] |= t[1] << 4; |
| 1199 |
r[5*i+3] = t[1] >> 4; |
| 1200 |
r[5*i+4] = t[1] >> 12; |
| 1201 |
} |
| 1202 |
|
| 1203 |
DBENCH_STOP(*tpack); |
| 1204 |
} |
| 1205 |
#endif |
| 1206 |
|
| 1207 |
/************************************************* |
| 1208 |
* Name: polyz_unpack |
| 1209 |
* |
| 1210 |
* Description: Unpack polynomial z with coefficients |
| 1211 |
* in [-(GAMMA1 - 1), GAMMA1]. |
| 1212 |
* |
| 1213 |
* Arguments: - poly *r: pointer to output polynomial |
| 1214 |
* - const uint8_t *a: byte array with bit-packed polynomial |
| 1215 |
**************************************************/ |
| 1216 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 1217 |
static |
| 1218 |
void polyz_unpack_17(poly *r, const uint8_t *a) { |
| 1219 |
unsigned int i; |
| 1220 |
DBENCH_START(); |
| 1221 |
|
| 1222 |
for(i = 0; i < N/4; ++i) { |
| 1223 |
r->coeffs[4*i+0] = a[9*i+0]; |
| 1224 |
r->coeffs[4*i+0] |= (uint32_t)a[9*i+1] << 8; |
| 1225 |
r->coeffs[4*i+0] |= (uint32_t)a[9*i+2] << 16; |
| 1226 |
r->coeffs[4*i+0] &= 0x3FFFF; |
| 1227 |
|
| 1228 |
r->coeffs[4*i+1] = a[9*i+2] >> 2; |
| 1229 |
r->coeffs[4*i+1] |= (uint32_t)a[9*i+3] << 6; |
| 1230 |
r->coeffs[4*i+1] |= (uint32_t)a[9*i+4] << 14; |
| 1231 |
r->coeffs[4*i+1] &= 0x3FFFF; |
| 1232 |
|
| 1233 |
r->coeffs[4*i+2] = a[9*i+4] >> 4; |
| 1234 |
r->coeffs[4*i+2] |= (uint32_t)a[9*i+5] << 4; |
| 1235 |
r->coeffs[4*i+2] |= (uint32_t)a[9*i+6] << 12; |
| 1236 |
r->coeffs[4*i+2] &= 0x3FFFF; |
| 1237 |
|
| 1238 |
r->coeffs[4*i+3] = a[9*i+6] >> 6; |
| 1239 |
r->coeffs[4*i+3] |= (uint32_t)a[9*i+7] << 2; |
| 1240 |
r->coeffs[4*i+3] |= (uint32_t)a[9*i+8] << 10; |
| 1241 |
r->coeffs[4*i+3] &= 0x3FFFF; |
| 1242 |
|
| 1243 |
r->coeffs[4*i+0] = GAMMA1_17 - r->coeffs[4*i+0]; |
| 1244 |
r->coeffs[4*i+1] = GAMMA1_17 - r->coeffs[4*i+1]; |
| 1245 |
r->coeffs[4*i+2] = GAMMA1_17 - r->coeffs[4*i+2]; |
| 1246 |
r->coeffs[4*i+3] = GAMMA1_17 - r->coeffs[4*i+3]; |
| 1247 |
} |
| 1248 |
|
| 1249 |
DBENCH_STOP(*tpack); |
| 1250 |
} |
| 1251 |
#endif |
| 1252 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 1253 |
static |
| 1254 |
void polyz_unpack_19(poly *r, const uint8_t *a) { |
| 1255 |
unsigned int i; |
| 1256 |
DBENCH_START(); |
| 1257 |
|
| 1258 |
for(i = 0; i < N/2; ++i) { |
| 1259 |
r->coeffs[2*i+0] = a[5*i+0]; |
| 1260 |
r->coeffs[2*i+0] |= (uint32_t)a[5*i+1] << 8; |
| 1261 |
r->coeffs[2*i+0] |= (uint32_t)a[5*i+2] << 16; |
| 1262 |
r->coeffs[2*i+0] &= 0xFFFFF; |
| 1263 |
|
| 1264 |
r->coeffs[2*i+1] = a[5*i+2] >> 4; |
| 1265 |
r->coeffs[2*i+1] |= (uint32_t)a[5*i+3] << 4; |
| 1266 |
r->coeffs[2*i+1] |= (uint32_t)a[5*i+4] << 12; |
| 1267 |
/* r->coeffs[2*i+1] &= 0xFFFFF; */ /* No effect, since we're anyway at 20 bits */ |
| 1268 |
|
| 1269 |
r->coeffs[2*i+0] = GAMMA1_19 - r->coeffs[2*i+0]; |
| 1270 |
r->coeffs[2*i+1] = GAMMA1_19 - r->coeffs[2*i+1]; |
| 1271 |
} |
| 1272 |
|
| 1273 |
DBENCH_STOP(*tpack); |
| 1274 |
} |
| 1275 |
#endif |
| 1276 |
|
| 1277 |
/************************************************* |
| 1278 |
* Name: polyw1_pack |
| 1279 |
* |
| 1280 |
* Description: Bit-pack polynomial w1 with coefficients in [0,15] or [0,43]. |
| 1281 |
* Input coefficients are assumed to be standard representatives. |
| 1282 |
* |
| 1283 |
* Arguments: - uint8_t *r: pointer to output byte array with at least |
| 1284 |
* POLYW1_PACKEDBYTES bytes |
| 1285 |
* - const poly *a: pointer to input polynomial |
| 1286 |
**************************************************/ |
| 1287 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 2 |
| 1288 |
static |
| 1289 |
void polyw1_pack_88(uint8_t *r, const poly *a) { |
| 1290 |
unsigned int i; |
| 1291 |
DBENCH_START(); |
| 1292 |
|
| 1293 |
for(i = 0; i < N/4; ++i) { |
| 1294 |
r[3*i+0] = a->coeffs[4*i+0]; |
| 1295 |
r[3*i+0] |= a->coeffs[4*i+1] << 6; |
| 1296 |
r[3*i+1] = a->coeffs[4*i+1] >> 2; |
| 1297 |
r[3*i+1] |= a->coeffs[4*i+2] << 4; |
| 1298 |
r[3*i+2] = a->coeffs[4*i+2] >> 4; |
| 1299 |
r[3*i+2] |= a->coeffs[4*i+3] << 2; |
| 1300 |
} |
| 1301 |
|
| 1302 |
DBENCH_STOP(*tpack); |
| 1303 |
} |
| 1304 |
#endif |
| 1305 |
#if !defined(DILITHIUM_MODE) || DILITHIUM_MODE == 3 || DILITHIUM_MODE == 5 |
| 1306 |
static |
| 1307 |
void polyw1_pack_32(uint8_t *r, const poly *a) { |
| 1308 |
unsigned int i; |
| 1309 |
DBENCH_START(); |
| 1310 |
|
| 1311 |
for(i = 0; i < N/2; ++i) |
| 1312 |
r[i] = a->coeffs[2*i+0] | (a->coeffs[2*i+1] << 4); |
| 1313 |
|
| 1314 |
DBENCH_STOP(*tpack); |
| 1315 |
} |
| 1316 |
#endif |
| 1317 |
/*************** dilithium/ref/reduce.c */ |
| 1318 |
|
| 1319 |
/************************************************* |
| 1320 |
* Name: montgomery_reduce |
| 1321 |
* |
| 1322 |
* Description: For finite field element a with -2^{31}Q <= a <= Q*2^31, |
| 1323 |
* compute r \equiv a*2^{-32} (mod Q) such that -Q < r < Q. |
| 1324 |
* |
| 1325 |
* Arguments: - int64_t: finite field element a |
| 1326 |
* |
| 1327 |
* Returns r. |
| 1328 |
**************************************************/ |
| 1329 |
int32_t montgomery_reduce(int64_t a) { |
| 1330 |
int32_t t; |
| 1331 |
|
| 1332 |
t = (int64_t)(int32_t)a*QINV; |
| 1333 |
t = (a - (int64_t)t*Q) >> 32; |
| 1334 |
return t; |
| 1335 |
} |
| 1336 |
|
| 1337 |
/************************************************* |
| 1338 |
* Name: reduce32 |
| 1339 |
* |
| 1340 |
* Description: For finite field element a with a <= 2^{31} - 2^{22} - 1, |
| 1341 |
* compute r \equiv a (mod Q) such that -6283008 <= r <= 6283008. |
| 1342 |
* |
| 1343 |
* Arguments: - int32_t: finite field element a |
| 1344 |
* |
| 1345 |
* Returns r. |
| 1346 |
**************************************************/ |
| 1347 |
int32_t reduce32(int32_t a) { |
| 1348 |
int32_t t; |
| 1349 |
|
| 1350 |
t = (a + (1 << 22)) >> 23; |
| 1351 |
t = a - t*Q; |
| 1352 |
return t; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/************************************************* |
| 1356 |
* Name: caddq |
| 1357 |
* |
| 1358 |
* Description: Add Q if input coefficient is negative. |
| 1359 |
* |
| 1360 |
* Arguments: - int32_t: finite field element a |
| 1361 |
* |
| 1362 |
* Returns r. |
| 1363 |
**************************************************/ |
| 1364 |
int32_t caddq(int32_t a) { |
| 1365 |
a += (a >> 31) & Q; |
| 1366 |
return a; |
| 1367 |
} |
| 1368 |
|
| 1369 |
#if 0 /* Not used */ |
| 1370 |
/************************************************* |
| 1371 |
* Name: freeze |
| 1372 |
* |
| 1373 |
* Description: For finite field element a, compute standard |
| 1374 |
* representative r = a mod^+ Q. |
| 1375 |
* |
| 1376 |
* Arguments: - int32_t: finite field element a |
| 1377 |
* |
| 1378 |
* Returns r. |
| 1379 |
**************************************************/ |
| 1380 |
int32_t freeze(int32_t a) { |
| 1381 |
a = reduce32(a); |
| 1382 |
a = caddq(a); |
| 1383 |
return a; |
| 1384 |
} |
| 1385 |
#endif |