| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* Copyright (c) The mlkem-native project authors |
| 4 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/* References |
| 8 |
* ========== |
| 9 |
* |
| 10 |
* - [FIPS204] |
| 11 |
* FIPS 204 Module-Lattice-Based Digital Signature Standard |
| 12 |
* National Institute of Standards and Technology |
| 13 |
* https://csrc.nist.gov/pubs/fips/204/final |
| 14 |
* |
| 15 |
* - [REF] |
| 16 |
* CRYSTALS-Dilithium reference implementation |
| 17 |
* Bai, Ducas, Kiltz, Lepoint, Lyubashevsky, Schwabe, Seiler, Stehlé |
| 18 |
* https://github.com/pq-crystals/dilithium/tree/master/ref |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "common.h" |
| 25 |
#include "ct.h" |
| 26 |
#include "debug.h" |
| 27 |
#include "poly.h" |
| 28 |
#include "reduce.h" |
| 29 |
#include "rounding.h" |
| 30 |
#include "symmetric.h" |
| 31 |
|
| 32 |
#if !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED) |
| 33 |
#include "zetas.inc" |
| 34 |
|
| 35 |
MLD_INTERNAL_API |
| 36 |
void mld_poly_reduce(mld_poly *a) |
| 37 |
{ |
| 38 |
unsigned int i; |
| 39 |
mld_assert_bound(a->coeffs, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX); |
| 40 |
|
| 41 |
for (i = 0; i < MLDSA_N; ++i) |
| 42 |
__loop__( |
| 43 |
invariant(i <= MLDSA_N) |
| 44 |
invariant(forall(k0, i, MLDSA_N, a->coeffs[k0] == loop_entry(*a).coeffs[k0])) |
| 45 |
invariant(array_bound(a->coeffs, 0, i, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))) |
| 46 |
{ |
| 47 |
a->coeffs[i] = mld_reduce32(a->coeffs[i]); |
| 48 |
} |
| 49 |
|
| 50 |
mld_assert_bound(a->coeffs, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, |
| 51 |
MLD_REDUCE32_RANGE_MAX); |
| 52 |
} |
| 53 |
|
| 54 |
MLD_STATIC_TESTABLE void mld_poly_caddq_c(mld_poly *a) |
| 55 |
__contract__( |
| 56 |
requires(memory_no_alias(a, sizeof(mld_poly))) |
| 57 |
requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q)) |
| 58 |
assigns(memory_slice(a, sizeof(mld_poly))) |
| 59 |
ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q)) |
| 60 |
) |
| 61 |
{ |
| 62 |
unsigned int i; |
| 63 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 64 |
|
| 65 |
for (i = 0; i < MLDSA_N; ++i) |
| 66 |
__loop__( |
| 67 |
invariant(i <= MLDSA_N) |
| 68 |
invariant(forall(k0, i, MLDSA_N, a->coeffs[k0] == loop_entry(*a).coeffs[k0])) |
| 69 |
invariant(array_bound(a->coeffs, 0, i, 0, MLDSA_Q)) |
| 70 |
) |
| 71 |
{ |
| 72 |
a->coeffs[i] = mld_caddq(a->coeffs[i]); |
| 73 |
} |
| 74 |
|
| 75 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 76 |
} |
| 77 |
|
| 78 |
MLD_INTERNAL_API |
| 79 |
void mld_poly_caddq(mld_poly *a) |
| 80 |
{ |
| 81 |
#if defined(MLD_USE_NATIVE_POLY_CADDQ) |
| 82 |
int ret; |
| 83 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 84 |
ret = mld_poly_caddq_native(a->coeffs); |
| 85 |
if (ret == MLD_NATIVE_FUNC_SUCCESS) |
| 86 |
{ |
| 87 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 88 |
return; |
| 89 |
} |
| 90 |
#endif /* MLD_USE_NATIVE_POLY_CADDQ */ |
| 91 |
mld_poly_caddq_c(a); |
| 92 |
} |
| 93 |
|
| 94 |
/* Reference: We use destructive version (output=first input) to avoid |
| 95 |
* reasoning about aliasing in the CBMC specification */ |
| 96 |
MLD_INTERNAL_API |
| 97 |
void mld_poly_add(mld_poly *r, const mld_poly *b) |
| 98 |
{ |
| 99 |
unsigned int i; |
| 100 |
for (i = 0; i < MLDSA_N; ++i) |
| 101 |
__loop__( |
| 102 |
assigns(i, memory_slice(r, sizeof(mld_poly))) |
| 103 |
invariant(i <= MLDSA_N) |
| 104 |
invariant(forall(k0, i, MLDSA_N, r->coeffs[k0] == loop_entry(*r).coeffs[k0])) |
| 105 |
invariant(forall(k1, 0, i, r->coeffs[k1] == loop_entry(*r).coeffs[k1] + b->coeffs[k1])) |
| 106 |
invariant(forall(k2, 0, i, r->coeffs[k2] < MLD_REDUCE32_DOMAIN_MAX)) |
| 107 |
invariant(forall(k2, 0, i, r->coeffs[k2] >= INT32_MIN)) |
| 108 |
) |
| 109 |
{ |
| 110 |
r->coeffs[i] = r->coeffs[i] + b->coeffs[i]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/* Reference: We use destructive version (output=first input) to avoid |
| 115 |
* reasoning about aliasing in the CBMC specification */ |
| 116 |
MLD_INTERNAL_API |
| 117 |
void mld_poly_sub(mld_poly *r, const mld_poly *b) |
| 118 |
{ |
| 119 |
unsigned int i; |
| 120 |
mld_assert_abs_bound(b->coeffs, MLDSA_N, MLDSA_Q); |
| 121 |
mld_assert_abs_bound(r->coeffs, MLDSA_N, MLDSA_Q); |
| 122 |
|
| 123 |
for (i = 0; i < MLDSA_N; ++i) |
| 124 |
__loop__( |
| 125 |
invariant(i <= MLDSA_N) |
| 126 |
invariant(array_bound(r->coeffs, 0, i, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX)) |
| 127 |
invariant(forall(k0, i, MLDSA_N, r->coeffs[k0] == loop_entry(*r).coeffs[k0])) |
| 128 |
) |
| 129 |
{ |
| 130 |
r->coeffs[i] = r->coeffs[i] - b->coeffs[i]; |
| 131 |
} |
| 132 |
|
| 133 |
mld_assert_bound(r->coeffs, MLDSA_N, INT32_MIN, MLD_REDUCE32_DOMAIN_MAX); |
| 134 |
} |
| 135 |
|
| 136 |
MLD_INTERNAL_API |
| 137 |
void mld_poly_shiftl(mld_poly *a) |
| 138 |
{ |
| 139 |
unsigned int i; |
| 140 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, 1 << 10); |
| 141 |
|
| 142 |
for (i = 0; i < MLDSA_N; i++) |
| 143 |
__loop__( |
| 144 |
invariant(i <= MLDSA_N) |
| 145 |
invariant(array_bound(a->coeffs, 0, i, 0, MLDSA_Q)) |
| 146 |
invariant(forall(k0, i, MLDSA_N, a->coeffs[k0] == loop_entry(*a).coeffs[k0]))) |
| 147 |
{ |
| 148 |
/* Reference: uses a left shift by MLDSA_D which is undefined behaviour in |
| 149 |
* C90/C99 |
| 150 |
*/ |
| 151 |
a->coeffs[i] *= (1 << MLDSA_D); |
| 152 |
} |
| 153 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static MLD_INLINE int32_t mld_fqmul(int32_t a, int32_t b) |
| 158 |
__contract__( |
| 159 |
requires(b > -MLDSA_Q_HALF && b < MLDSA_Q_HALF) |
| 160 |
ensures(return_value > -MLDSA_Q && return_value < MLDSA_Q) |
| 161 |
) |
| 162 |
{ |
| 163 |
/* Bounds: We argue in mld_montgomery_reduce() that the reult |
| 164 |
* of Montgomery reduction is < MLDSA_Q if the input is smaller |
| 165 |
* than 2^31 * MLDSA_Q in absolute value. Indeed, we have: |
| 166 |
* |
| 167 |
* |a * b| = |a| * |b| |
| 168 |
* < 2^31 * MLDSA_Q_HALF |
| 169 |
* < 2^31 * MLDSA_Q |
| 170 |
*/ |
| 171 |
return mld_montgomery_reduce((int64_t)a * (int64_t)b); |
| 172 |
} |
| 173 |
|
| 174 |
/* mld_ntt_butterfly_block() |
| 175 |
* |
| 176 |
* Computes a block CT butterflies with a fixed twiddle factor, |
| 177 |
* using Montgomery multiplication. |
| 178 |
* |
| 179 |
* Parameters: |
| 180 |
* - r: Pointer to base of polynomial (_not_ the base of butterfly block) |
| 181 |
* - zeta: Twiddle factor to use for the butterfly. This must be in |
| 182 |
* Montgomery form and signed canonical. |
| 183 |
* - start: Offset to the beginning of the butterfly block |
| 184 |
* - len: Index difference between coefficients subject to a butterfly |
| 185 |
* - bound: Ghost variable describing coefficient bound: Prior to `start`, |
| 186 |
* coefficients must be bound by `bound + MLDSA_Q`. Post `start`, |
| 187 |
* they must be bound by `bound`. |
| 188 |
* When this function returns, output coefficients in the index range |
| 189 |
* [start, start+2*len) have bound bumped to `bound + MLDSA_Q`. |
| 190 |
* Example: |
| 191 |
* - start=8, len=4 |
| 192 |
* This would compute the following four butterflies |
| 193 |
* 8 -- 12 |
| 194 |
* 9 -- 13 |
| 195 |
* 10 -- 14 |
| 196 |
* 11 -- 15 |
| 197 |
* - start=4, len=2 |
| 198 |
* This would compute the following two butterflies |
| 199 |
* 4 -- 6 |
| 200 |
* 5 -- 7 |
| 201 |
*/ |
| 202 |
|
| 203 |
/* Reference: Embedded in `ntt()` in the reference implementation @[REF]. */ |
| 204 |
static MLD_INLINE void mld_ntt_butterfly_block(int32_t r[MLDSA_N], |
| 205 |
const int32_t zeta, |
| 206 |
const unsigned start, |
| 207 |
const unsigned len, |
| 208 |
const unsigned bound) |
| 209 |
__contract__( |
| 210 |
requires(start < MLDSA_N) |
| 211 |
requires(1 <= len && len <= MLDSA_N / 2 && start + 2 * len <= MLDSA_N) |
| 212 |
requires(0 <= bound && bound < INT32_MAX - MLDSA_Q) |
| 213 |
requires(-MLDSA_Q_HALF < zeta && zeta < MLDSA_Q_HALF) |
| 214 |
requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N)) |
| 215 |
requires(array_abs_bound(r, 0, start, bound + MLDSA_Q)) |
| 216 |
requires(array_abs_bound(r, start, MLDSA_N, bound)) |
| 217 |
assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N)) |
| 218 |
ensures(array_abs_bound(r, 0, start + 2*len, bound + MLDSA_Q)) |
| 219 |
ensures(array_abs_bound(r, start + 2 * len, MLDSA_N, bound))) |
| 220 |
{ |
| 221 |
/* `bound` is a ghost variable only needed in the CBMC specification */ |
| 222 |
unsigned j; |
| 223 |
((void)bound); |
| 224 |
for (j = start; j < start + len; j++) |
| 225 |
__loop__( |
| 226 |
invariant(start <= j && j <= start + len) |
| 227 |
/* |
| 228 |
* Coefficients are updated in strided pairs, so the bounds for the |
| 229 |
* intermediate states alternate twice between the old and new bound |
| 230 |
*/ |
| 231 |
invariant(array_abs_bound(r, 0, j, bound + MLDSA_Q)) |
| 232 |
invariant(array_abs_bound(r, j, start + len, bound)) |
| 233 |
invariant(array_abs_bound(r, start + len, j + len, bound + MLDSA_Q)) |
| 234 |
invariant(array_abs_bound(r, j + len, MLDSA_N, bound))) |
| 235 |
{ |
| 236 |
int32_t t; |
| 237 |
t = mld_fqmul(r[j + len], zeta); |
| 238 |
r[j + len] = r[j] - t; |
| 239 |
r[j] = r[j] + t; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/* mld_ntt_layer() |
| 244 |
* |
| 245 |
* Compute one layer of forward NTT |
| 246 |
* |
| 247 |
* Parameters: |
| 248 |
* - r: Pointer to base of polynomial |
| 249 |
* - layer: Indicates which layer is being applied. |
| 250 |
*/ |
| 251 |
|
| 252 |
/* Reference: Embedded in `ntt()` in the reference implementation @[REF]. */ |
| 253 |
static MLD_INLINE void mld_ntt_layer(int32_t r[MLDSA_N], const unsigned layer) |
| 254 |
__contract__( |
| 255 |
requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N)) |
| 256 |
requires(1 <= layer && layer <= 8) |
| 257 |
requires(array_abs_bound(r, 0, MLDSA_N, layer * MLDSA_Q)) |
| 258 |
assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N)) |
| 259 |
ensures(array_abs_bound(r, 0, MLDSA_N, (layer + 1) * MLDSA_Q))) |
| 260 |
{ |
| 261 |
unsigned start, k, len; |
| 262 |
/* Twiddle factors for layer n are at indices 2^(n-1)..2^n-1. */ |
| 263 |
k = 1u << (layer - 1); |
| 264 |
len = (unsigned)MLDSA_N >> layer; |
| 265 |
for (start = 0; start < MLDSA_N; start += 2 * len) |
| 266 |
__loop__( |
| 267 |
invariant(start < MLDSA_N + 2 * len) |
| 268 |
invariant(k <= MLDSA_N) |
| 269 |
invariant(2 * len * k == start + MLDSA_N) |
| 270 |
invariant(array_abs_bound(r, 0, start, layer * MLDSA_Q + MLDSA_Q)) |
| 271 |
invariant(array_abs_bound(r, start, MLDSA_N, layer * MLDSA_Q))) |
| 272 |
{ |
| 273 |
int32_t zeta = mld_zetas[k++]; |
| 274 |
mld_ntt_butterfly_block(r, zeta, start, len, layer * MLDSA_Q); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
MLD_STATIC_TESTABLE void mld_poly_ntt_c(mld_poly *a) |
| 279 |
__contract__( |
| 280 |
requires(memory_no_alias(a, sizeof(mld_poly))) |
| 281 |
requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q)) |
| 282 |
assigns(memory_slice(a, sizeof(mld_poly))) |
| 283 |
ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND)) |
| 284 |
) |
| 285 |
{ |
| 286 |
unsigned int layer; |
| 287 |
int32_t *r; |
| 288 |
|
| 289 |
|
| 290 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 291 |
r = a->coeffs; |
| 292 |
|
| 293 |
for (layer = 1; layer < 9; layer++) |
| 294 |
__loop__( |
| 295 |
invariant(1 <= layer && layer <= 9) |
| 296 |
invariant(array_abs_bound(r, 0, MLDSA_N, layer * MLDSA_Q)) |
| 297 |
) |
| 298 |
{ |
| 299 |
mld_ntt_layer(r, layer); |
| 300 |
} |
| 301 |
|
| 302 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 303 |
} |
| 304 |
|
| 305 |
MLD_INTERNAL_API |
| 306 |
void mld_poly_ntt(mld_poly *a) |
| 307 |
{ |
| 308 |
#if defined(MLD_USE_NATIVE_NTT) |
| 309 |
int ret; |
| 310 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 311 |
ret = mld_ntt_native(a->coeffs); |
| 312 |
if (ret == MLD_NATIVE_FUNC_SUCCESS) |
| 313 |
{ |
| 314 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 315 |
return; |
| 316 |
} |
| 317 |
#endif /* MLD_USE_NATIVE_NTT */ |
| 318 |
mld_poly_ntt_c(a); |
| 319 |
} |
| 320 |
|
| 321 |
/************************************************* |
| 322 |
* Name: mld_fqscale |
| 323 |
* |
| 324 |
* Description: Scales a field element by mont/256 , i.e., performs Montgomery |
| 325 |
* multiplication by mont^2/256. |
| 326 |
* Input is expected to have absolute value smaller than |
| 327 |
* 256 * MLDSA_Q. |
| 328 |
* Output has absolute value smaller than MLD_INTT_BOUND. |
| 329 |
* |
| 330 |
* Arguments: - int32_t a: Field element to be scaled. |
| 331 |
**************************************************/ |
| 332 |
static MLD_INLINE int32_t mld_fqscale(int32_t a) |
| 333 |
__contract__( |
| 334 |
requires(a > -256*MLDSA_Q && a < 256*MLDSA_Q) |
| 335 |
ensures(return_value > -MLD_INTT_BOUND && return_value < MLD_INTT_BOUND) |
| 336 |
) |
| 337 |
{ |
| 338 |
/* check-magic: 41978 == pow(2,64-8,MLDSA_Q) */ |
| 339 |
const int32_t f = 41978; |
| 340 |
/* Bounds: MLD_INTT_BOUND is MLDSA_Q, so the bounds reasoning is just |
| 341 |
* a special case of that in mld_fqmul(). */ |
| 342 |
return mld_montgomery_reduce((int64_t)a * f); |
| 343 |
} |
| 344 |
|
| 345 |
/* Reference: Embedded into `invntt_tomont()` in the reference implementation |
| 346 |
* @[REF] */ |
| 347 |
static MLD_INLINE void mld_invntt_layer(int32_t r[MLDSA_N], unsigned layer) |
| 348 |
__contract__( |
| 349 |
requires(memory_no_alias(r, sizeof(int32_t) * MLDSA_N)) |
| 350 |
requires(1 <= layer && layer <= 8) |
| 351 |
requires(array_abs_bound(r, 0, MLDSA_N, (MLDSA_N >> layer) * MLDSA_Q)) |
| 352 |
assigns(memory_slice(r, sizeof(int32_t) * MLDSA_N)) |
| 353 |
ensures(array_abs_bound(r, 0, MLDSA_N, (MLDSA_N >> (layer - 1)) * MLDSA_Q))) |
| 354 |
{ |
| 355 |
unsigned start, k, len; |
| 356 |
len = (unsigned)MLDSA_N >> layer; |
| 357 |
k = (1u << layer) - 1; |
| 358 |
for (start = 0; start < MLDSA_N; start += 2 * len) |
| 359 |
__loop__( |
| 360 |
invariant(start <= MLDSA_N && k <= 255) |
| 361 |
invariant(2 * len * k + start == 2 * MLDSA_N - 2 * len) |
| 362 |
invariant(array_abs_bound(r, 0, start, (MLDSA_N >> (layer - 1)) * MLDSA_Q)) |
| 363 |
invariant(array_abs_bound(r, start, MLDSA_N, (MLDSA_N >> layer) * MLDSA_Q))) |
| 364 |
{ |
| 365 |
unsigned j; |
| 366 |
int32_t zeta = -mld_zetas[k--]; |
| 367 |
|
| 368 |
for (j = start; j < start + len; j++) |
| 369 |
__loop__( |
| 370 |
invariant(start <= j && j <= start + len) |
| 371 |
invariant(array_abs_bound(r, 0, start, (MLDSA_N >> (layer - 1)) * MLDSA_Q)) |
| 372 |
invariant(array_abs_bound(r, start, j, (MLDSA_N >> (layer - 1)) * MLDSA_Q)) |
| 373 |
invariant(array_abs_bound(r, j, start + len, (MLDSA_N >> layer) * MLDSA_Q)) |
| 374 |
invariant(array_abs_bound(r, start + len, MLDSA_N, (MLDSA_N >> layer) * MLDSA_Q))) |
| 375 |
{ |
| 376 |
int32_t t = r[j]; |
| 377 |
r[j] = t + r[j + len]; |
| 378 |
r[j + len] = t - r[j + len]; |
| 379 |
r[j + len] = mld_fqmul(r[j + len], zeta); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
MLD_STATIC_TESTABLE void mld_poly_invntt_tomont_c(mld_poly *a) |
| 385 |
__contract__( |
| 386 |
requires(memory_no_alias(a, sizeof(mld_poly))) |
| 387 |
requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLDSA_Q)) |
| 388 |
assigns(memory_slice(a, sizeof(mld_poly))) |
| 389 |
ensures(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_INTT_BOUND)) |
| 390 |
) |
| 391 |
{ |
| 392 |
unsigned int layer, j; |
| 393 |
int32_t *r; |
| 394 |
|
| 395 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 396 |
|
| 397 |
r = a->coeffs; |
| 398 |
for (layer = 8; layer >= 1; layer--) |
| 399 |
__loop__( |
| 400 |
invariant(layer <= 8) |
| 401 |
/* Absolute bounds increase from 1Q before layer 8 */ |
| 402 |
/* up to 256Q after layer 1 */ |
| 403 |
invariant(array_abs_bound(r, 0, MLDSA_N, (MLDSA_N >> layer) * MLDSA_Q))) |
| 404 |
{ |
| 405 |
mld_invntt_layer(r, layer); |
| 406 |
} |
| 407 |
|
| 408 |
/* Coefficient bounds are now at 256Q. We now scale by mont / 256, |
| 409 |
* i.e., compute the Montgomery multiplication by mont^2 / 256. |
| 410 |
* mont corrects the mont^-1 factor introduced in the basemul. |
| 411 |
* 1/256 performs that scaling of the inverse NTT. |
| 412 |
* The reduced value is bounded by MLD_INTT_BOUND in absolute |
| 413 |
* value.*/ |
| 414 |
for (j = 0; j < MLDSA_N; ++j) |
| 415 |
__loop__( |
| 416 |
invariant(j <= MLDSA_N) |
| 417 |
invariant(array_abs_bound(r, 0, j, MLD_INTT_BOUND)) |
| 418 |
invariant(array_abs_bound(r, j, MLDSA_N, MLDSA_N * MLDSA_Q)) |
| 419 |
) |
| 420 |
{ |
| 421 |
r[j] = mld_fqscale(r[j]); |
| 422 |
} |
| 423 |
|
| 424 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_INTT_BOUND); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
MLD_INTERNAL_API |
| 429 |
void mld_poly_invntt_tomont(mld_poly *a) |
| 430 |
{ |
| 431 |
#if defined(MLD_USE_NATIVE_INTT) |
| 432 |
int ret; |
| 433 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLDSA_Q); |
| 434 |
ret = mld_intt_native(a->coeffs); |
| 435 |
if (ret == MLD_NATIVE_FUNC_SUCCESS) |
| 436 |
{ |
| 437 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_INTT_BOUND); |
| 438 |
return; |
| 439 |
} |
| 440 |
#endif /* MLD_USE_NATIVE_INTT */ |
| 441 |
mld_poly_invntt_tomont_c(a); |
| 442 |
} |
| 443 |
|
| 444 |
MLD_STATIC_TESTABLE void mld_poly_pointwise_montgomery_c(mld_poly *c, |
| 445 |
const mld_poly *a, |
| 446 |
const mld_poly *b) |
| 447 |
__contract__( |
| 448 |
requires(memory_no_alias(a, sizeof(mld_poly))) |
| 449 |
requires(memory_no_alias(b, sizeof(mld_poly))) |
| 450 |
requires(memory_no_alias(c, sizeof(mld_poly))) |
| 451 |
requires(array_abs_bound(a->coeffs, 0, MLDSA_N, MLD_NTT_BOUND)) |
| 452 |
requires(array_abs_bound(b->coeffs, 0, MLDSA_N, MLD_NTT_BOUND)) |
| 453 |
assigns(memory_slice(c, sizeof(mld_poly))) |
| 454 |
ensures(array_abs_bound(c->coeffs, 0, MLDSA_N, MLDSA_Q)) |
| 455 |
) |
| 456 |
{ |
| 457 |
unsigned int i; |
| 458 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 459 |
mld_assert_abs_bound(b->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 460 |
|
| 461 |
for (i = 0; i < MLDSA_N; ++i) |
| 462 |
__loop__( |
| 463 |
invariant(i <= MLDSA_N) |
| 464 |
invariant(array_abs_bound(c->coeffs, 0, i, MLDSA_Q)) |
| 465 |
) |
| 466 |
{ |
| 467 |
c->coeffs[i] = mld_montgomery_reduce((int64_t)a->coeffs[i] * b->coeffs[i]); |
| 468 |
} |
| 469 |
mld_assert_abs_bound(c->coeffs, MLDSA_N, MLDSA_Q); |
| 470 |
} |
| 471 |
|
| 472 |
MLD_INTERNAL_API |
| 473 |
void mld_poly_pointwise_montgomery(mld_poly *c, const mld_poly *a, |
| 474 |
const mld_poly *b) |
| 475 |
{ |
| 476 |
#if defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY) |
| 477 |
int ret; |
| 478 |
mld_assert_abs_bound(a->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 479 |
mld_assert_abs_bound(b->coeffs, MLDSA_N, MLD_NTT_BOUND); |
| 480 |
ret = mld_poly_pointwise_montgomery_native(c->coeffs, a->coeffs, b->coeffs); |
| 481 |
if (ret == MLD_NATIVE_FUNC_SUCCESS) |
| 482 |
{ |
| 483 |
mld_assert_abs_bound(c->coeffs, MLDSA_N, MLDSA_Q); |
| 484 |
return; |
| 485 |
} |
| 486 |
#endif /* MLD_USE_NATIVE_POINTWISE_MONTGOMERY */ |
| 487 |
mld_poly_pointwise_montgomery_c(c, a, b); |
| 488 |
} |
| 489 |
|
| 490 |
MLD_INTERNAL_API |
| 491 |
void mld_poly_power2round(mld_poly *a1, mld_poly *a0, const mld_poly *a) |
| 492 |
{ |
| 493 |
unsigned int i; |
| 494 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 495 |
|
| 496 |
for (i = 0; i < MLDSA_N; ++i) |
| 497 |
__loop__( |
| 498 |
assigns(i, memory_slice(a0, sizeof(mld_poly)), memory_slice(a1, sizeof(mld_poly))) |
| 499 |
invariant(i <= MLDSA_N) |
| 500 |
invariant(array_bound(a0->coeffs, 0, i, -(MLD_2_POW_D/2)+1, (MLD_2_POW_D/2)+1)) |
| 501 |
invariant(array_bound(a1->coeffs, 0, i, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1)) |
| 502 |
) |
| 503 |
{ |
| 504 |
mld_power2round(&a0->coeffs[i], &a1->coeffs[i], a->coeffs[i]); |
| 505 |
} |
| 506 |
|
| 507 |
mld_assert_bound(a0->coeffs, MLDSA_N, -(MLD_2_POW_D / 2) + 1, |
| 508 |
(MLD_2_POW_D / 2) + 1); |
| 509 |
mld_assert_bound(a1->coeffs, MLDSA_N, 0, ((MLDSA_Q - 1) / MLD_2_POW_D) + 1); |
| 510 |
} |
| 511 |
|
| 512 |
#ifndef MLD_POLY_UNIFORM_NBLOCKS |
| 513 |
#define MLD_POLY_UNIFORM_NBLOCKS \ |
| 514 |
((768 + MLD_STREAM128_BLOCKBYTES - 1) / MLD_STREAM128_BLOCKBYTES) |
| 515 |
#endif |
| 516 |
/* Reference: `mld_rej_uniform()` in the reference implementation @[REF]. |
| 517 |
* - Our signature differs from the reference implementation |
| 518 |
* in that it adds the offset and always expects the base of the |
| 519 |
* target buffer. This avoids shifting the buffer base in the |
| 520 |
* caller, which appears tricky to reason about. */ |
| 521 |
MLD_STATIC_TESTABLE unsigned int mld_rej_uniform_c(int32_t *a, |
| 522 |
unsigned int target, |
| 523 |
unsigned int offset, |
| 524 |
const uint8_t *buf, |
| 525 |
unsigned int buflen) |
| 526 |
__contract__( |
| 527 |
requires(offset <= target && target <= MLDSA_N) |
| 528 |
requires(buflen <= (MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES) && buflen % 3 == 0) |
| 529 |
requires(memory_no_alias(a, sizeof(int32_t) * target)) |
| 530 |
requires(memory_no_alias(buf, buflen)) |
| 531 |
requires(array_bound(a, 0, offset, 0, MLDSA_Q)) |
| 532 |
assigns(memory_slice(a, sizeof(int32_t) * target)) |
| 533 |
ensures(offset <= return_value && return_value <= target) |
| 534 |
ensures(array_bound(a, 0, return_value, 0, MLDSA_Q)) |
| 535 |
) |
| 536 |
{ |
| 537 |
unsigned int ctr, pos; |
| 538 |
uint32_t t; |
| 539 |
mld_assert_bound(a, offset, 0, MLDSA_Q); |
| 540 |
|
| 541 |
ctr = offset; |
| 542 |
pos = 0; |
| 543 |
/* pos + 3 cannot overflow due to the assumption |
| 544 |
buflen <= (MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES) */ |
| 545 |
while (ctr < target && pos + 3 <= buflen) |
| 546 |
__loop__( |
| 547 |
invariant(offset <= ctr && ctr <= target && pos <= buflen) |
| 548 |
invariant(array_bound(a, 0, ctr, 0, MLDSA_Q))) |
| 549 |
{ |
| 550 |
t = buf[pos++]; |
| 551 |
t |= (uint32_t)buf[pos++] << 8; |
| 552 |
t |= (uint32_t)buf[pos++] << 16; |
| 553 |
t &= 0x7FFFFF; |
| 554 |
|
| 555 |
if (t < MLDSA_Q) |
| 556 |
{ |
| 557 |
a[ctr++] = (int32_t)t; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
mld_assert_bound(a, ctr, 0, MLDSA_Q); |
| 562 |
|
| 563 |
return ctr; |
| 564 |
} |
| 565 |
/************************************************* |
| 566 |
* Name: mld_rej_uniform |
| 567 |
* |
| 568 |
* Description: Sample uniformly random coefficients in [0, MLDSA_Q-1] by |
| 569 |
* performing rejection sampling on array of random bytes. |
| 570 |
* |
| 571 |
* Arguments: - int32_t *a: pointer to output array (allocated) |
| 572 |
* - unsigned int target: requested number of coefficients to |
| 573 |
*sample |
| 574 |
* - unsigned int offset: number of coefficients already sampled |
| 575 |
* - const uint8_t *buf: array of random bytes to sample from |
| 576 |
* - unsigned int buflen: length of array of random bytes (must be |
| 577 |
* multiple of 3) |
| 578 |
* |
| 579 |
* Returns number of sampled coefficients. Can be smaller than len if not enough |
| 580 |
* random bytes were given. |
| 581 |
**************************************************/ |
| 582 |
|
| 583 |
/* Reference: `mld_rej_uniform()` in the reference implementation @[REF]. |
| 584 |
* - Our signature differs from the reference implementation |
| 585 |
* in that it adds the offset and always expects the base of the |
| 586 |
* target buffer. This avoids shifting the buffer base in the |
| 587 |
* caller, which appears tricky to reason about. */ |
| 588 |
static unsigned int mld_rej_uniform(int32_t *a, unsigned int target, |
| 589 |
unsigned int offset, const uint8_t *buf, |
| 590 |
unsigned int buflen) |
| 591 |
__contract__( |
| 592 |
requires(offset <= target && target <= MLDSA_N) |
| 593 |
requires(buflen <= (MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES) && buflen % 3 == 0) |
| 594 |
requires(memory_no_alias(a, sizeof(int32_t) * target)) |
| 595 |
requires(memory_no_alias(buf, buflen)) |
| 596 |
requires(array_bound(a, 0, offset, 0, MLDSA_Q)) |
| 597 |
assigns(memory_slice(a, sizeof(int32_t) * target)) |
| 598 |
ensures(offset <= return_value && return_value <= target) |
| 599 |
ensures(array_bound(a, 0, return_value, 0, MLDSA_Q)) |
| 600 |
) |
| 601 |
{ |
| 602 |
#if defined(MLD_USE_NATIVE_REJ_UNIFORM) |
| 603 |
int ret; |
| 604 |
mld_assert_bound(a, offset, 0, MLDSA_Q); |
| 605 |
if (offset == 0) |
| 606 |
{ |
| 607 |
ret = mld_rej_uniform_native(a, target, buf, buflen); |
| 608 |
if (ret != MLD_NATIVE_FUNC_FALLBACK) |
| 609 |
{ |
| 610 |
unsigned res = (unsigned)ret; |
| 611 |
mld_assert_bound(a, res, 0, MLDSA_Q); |
| 612 |
return res; |
| 613 |
} |
| 614 |
} |
| 615 |
#endif /* MLD_USE_NATIVE_REJ_UNIFORM */ |
| 616 |
|
| 617 |
return mld_rej_uniform_c(a, target, offset, buf, buflen); |
| 618 |
} |
| 619 |
|
| 620 |
/* Reference: poly_uniform() in the reference implementation @[REF]. |
| 621 |
* - Simplified from reference by removing buffer tail handling |
| 622 |
* since buflen % 3 = 0 always holds true (MLD_STREAM128_BLOCKBYTES |
| 623 |
* = 168). |
| 624 |
* - Modified rej_uniform interface to track offset directly. |
| 625 |
* - Pass nonce packed in the extended seed array instead of a third |
| 626 |
* argument. |
| 627 |
* */ |
| 628 |
MLD_INTERNAL_API |
| 629 |
void mld_poly_uniform(mld_poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2]) |
| 630 |
{ |
| 631 |
unsigned int ctr; |
| 632 |
unsigned int buflen = MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES; |
| 633 |
MLD_ALIGN uint8_t buf[MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES]; |
| 634 |
mld_xof128_ctx state; |
| 635 |
|
| 636 |
mld_xof128_init(&state); |
| 637 |
mld_xof128_absorb_once(&state, seed, MLDSA_SEEDBYTES + 2); |
| 638 |
mld_xof128_squeezeblocks(buf, MLD_POLY_UNIFORM_NBLOCKS, &state); |
| 639 |
|
| 640 |
ctr = mld_rej_uniform(a->coeffs, MLDSA_N, 0, buf, buflen); |
| 641 |
buflen = MLD_STREAM128_BLOCKBYTES; |
| 642 |
while (ctr < MLDSA_N) |
| 643 |
__loop__( |
| 644 |
assigns(ctr, state, memory_slice(a, sizeof(mld_poly)), object_whole(buf)) |
| 645 |
invariant(ctr <= MLDSA_N) |
| 646 |
invariant(array_bound(a->coeffs, 0, ctr, 0, MLDSA_Q)) |
| 647 |
invariant(state.pos <= SHAKE128_RATE) |
| 648 |
) |
| 649 |
{ |
| 650 |
mld_xof128_squeezeblocks(buf, 1, &state); |
| 651 |
ctr = mld_rej_uniform(a->coeffs, MLDSA_N, ctr, buf, buflen); |
| 652 |
} |
| 653 |
mld_xof128_release(&state); |
| 654 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 655 |
|
| 656 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 657 |
mld_zeroize(buf, sizeof(buf)); |
| 658 |
} |
| 659 |
|
| 660 |
#if !defined(MLD_CONFIG_SERIAL_FIPS202_ONLY) && !defined(MLD_CONFIG_REDUCE_RAM) |
| 661 |
MLD_INTERNAL_API |
| 662 |
void mld_poly_uniform_4x(mld_poly *vec0, mld_poly *vec1, mld_poly *vec2, |
| 663 |
mld_poly *vec3, |
| 664 |
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)]) |
| 665 |
{ |
| 666 |
/* Temporary buffers for XOF output before rejection sampling */ |
| 667 |
MLD_ALIGN uint8_t |
| 668 |
buf[4][MLD_ALIGN_UP(MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES)]; |
| 669 |
|
| 670 |
/* Tracks the number of coefficients we have already sampled */ |
| 671 |
unsigned ctr[4]; |
| 672 |
mld_xof128_x4_ctx state; |
| 673 |
unsigned buflen; |
| 674 |
|
| 675 |
mld_xof128_x4_init(&state); |
| 676 |
mld_xof128_x4_absorb(&state, seed, MLDSA_SEEDBYTES + 2); |
| 677 |
|
| 678 |
/* |
| 679 |
* Initially, squeeze heuristic number of MLD_POLY_UNIFORM_NBLOCKS. |
| 680 |
* This should generate the matrix entries with high probability. |
| 681 |
*/ |
| 682 |
|
| 683 |
mld_xof128_x4_squeezeblocks(buf, MLD_POLY_UNIFORM_NBLOCKS, &state); |
| 684 |
buflen = MLD_POLY_UNIFORM_NBLOCKS * MLD_STREAM128_BLOCKBYTES; |
| 685 |
ctr[0] = mld_rej_uniform(vec0->coeffs, MLDSA_N, 0, buf[0], buflen); |
| 686 |
ctr[1] = mld_rej_uniform(vec1->coeffs, MLDSA_N, 0, buf[1], buflen); |
| 687 |
ctr[2] = mld_rej_uniform(vec2->coeffs, MLDSA_N, 0, buf[2], buflen); |
| 688 |
ctr[3] = mld_rej_uniform(vec3->coeffs, MLDSA_N, 0, buf[3], buflen); |
| 689 |
|
| 690 |
/* |
| 691 |
* So long as not all matrix entries have been generated, squeeze |
| 692 |
* one more block a time until we're done. |
| 693 |
*/ |
| 694 |
buflen = MLD_STREAM128_BLOCKBYTES; |
| 695 |
while (ctr[0] < MLDSA_N || ctr[1] < MLDSA_N || ctr[2] < MLDSA_N || |
| 696 |
ctr[3] < MLDSA_N) |
| 697 |
__loop__( |
| 698 |
assigns(ctr, state, object_whole(buf), |
| 699 |
memory_slice(vec0, sizeof(mld_poly)), memory_slice(vec1, sizeof(mld_poly)), |
| 700 |
memory_slice(vec2, sizeof(mld_poly)), memory_slice(vec3, sizeof(mld_poly))) |
| 701 |
invariant(ctr[0] <= MLDSA_N && ctr[1] <= MLDSA_N) |
| 702 |
invariant(ctr[2] <= MLDSA_N && ctr[3] <= MLDSA_N) |
| 703 |
invariant(array_bound(vec0->coeffs, 0, ctr[0], 0, MLDSA_Q)) |
| 704 |
invariant(array_bound(vec1->coeffs, 0, ctr[1], 0, MLDSA_Q)) |
| 705 |
invariant(array_bound(vec2->coeffs, 0, ctr[2], 0, MLDSA_Q)) |
| 706 |
invariant(array_bound(vec3->coeffs, 0, ctr[3], 0, MLDSA_Q))) |
| 707 |
{ |
| 708 |
mld_xof128_x4_squeezeblocks(buf, 1, &state); |
| 709 |
ctr[0] = mld_rej_uniform(vec0->coeffs, MLDSA_N, ctr[0], buf[0], buflen); |
| 710 |
ctr[1] = mld_rej_uniform(vec1->coeffs, MLDSA_N, ctr[1], buf[1], buflen); |
| 711 |
ctr[2] = mld_rej_uniform(vec2->coeffs, MLDSA_N, ctr[2], buf[2], buflen); |
| 712 |
ctr[3] = mld_rej_uniform(vec3->coeffs, MLDSA_N, ctr[3], buf[3], buflen); |
| 713 |
} |
| 714 |
mld_xof128_x4_release(&state); |
| 715 |
|
| 716 |
mld_assert_bound(vec0->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 717 |
mld_assert_bound(vec1->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 718 |
mld_assert_bound(vec2->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 719 |
mld_assert_bound(vec3->coeffs, MLDSA_N, 0, MLDSA_Q); |
| 720 |
|
| 721 |
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */ |
| 722 |
mld_zeroize(buf, sizeof(buf)); |
| 723 |
} |
| 724 |
|
| 725 |
#endif /* !MLD_CONFIG_SERIAL_FIPS202_ONLY && !MLD_CONFIG_REDUCE_RAM */ |
| 726 |
|
| 727 |
MLD_INTERNAL_API |
| 728 |
void mld_polyt1_pack(uint8_t r[MLDSA_POLYT1_PACKEDBYTES], const mld_poly *a) |
| 729 |
{ |
| 730 |
unsigned int i; |
| 731 |
mld_assert_bound(a->coeffs, MLDSA_N, 0, 1 << 10); |
| 732 |
|
| 733 |
for (i = 0; i < MLDSA_N / 4; ++i) |
| 734 |
__loop__( |
| 735 |
invariant(i <= MLDSA_N/4)) |
| 736 |
{ |
| 737 |
r[5 * i + 0] = (uint8_t)((a->coeffs[4 * i + 0] >> 0) & 0xFF); |
| 738 |
r[5 * i + 1] = |
| 739 |
(uint8_t)(((a->coeffs[4 * i + 0] >> 8) | (a->coeffs[4 * i + 1] << 2)) & |
| 740 |
0xFF); |
| 741 |
r[5 * i + 2] = |
| 742 |
(uint8_t)(((a->coeffs[4 * i + 1] >> 6) | (a->coeffs[4 * i + 2] << 4)) & |
| 743 |
0xFF); |
| 744 |
r[5 * i + 3] = |
| 745 |
(uint8_t)(((a->coeffs[4 * i + 2] >> 4) | (a->coeffs[4 * i + 3] << 6)) & |
| 746 |
0xFF); |
| 747 |
r[5 * i + 4] = (uint8_t)((a->coeffs[4 * i + 3] >> 2) & 0xFF); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
MLD_INTERNAL_API |
| 752 |
void mld_polyt1_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT1_PACKEDBYTES]) |
| 753 |
{ |
| 754 |
unsigned int i; |
| 755 |
|
| 756 |
for (i = 0; i < MLDSA_N / 4; ++i) |
| 757 |
__loop__( |
| 758 |
invariant(i <= MLDSA_N/4) |
| 759 |
invariant(array_bound(r->coeffs, 0, i*4, 0, 1 << 10))) |
| 760 |
{ |
| 761 |
r->coeffs[4 * i + 0] = |
| 762 |
((a[5 * i + 0] >> 0) | ((int32_t)a[5 * i + 1] << 8)) & 0x3FF; |
| 763 |
r->coeffs[4 * i + 1] = |
| 764 |
((a[5 * i + 1] >> 2) | ((int32_t)a[5 * i + 2] << 6)) & 0x3FF; |
| 765 |
r->coeffs[4 * i + 2] = |
| 766 |
((a[5 * i + 2] >> 4) | ((int32_t)a[5 * i + 3] << 4)) & 0x3FF; |
| 767 |
r->coeffs[4 * i + 3] = |
| 768 |
((a[5 * i + 3] >> 6) | ((int32_t)a[5 * i + 4] << 2)) & 0x3FF; |
| 769 |
} |
| 770 |
|
| 771 |
mld_assert_bound(r->coeffs, MLDSA_N, 0, 1 << 10); |
| 772 |
} |
| 773 |
|
| 774 |
MLD_INTERNAL_API |
| 775 |
void mld_polyt0_pack(uint8_t r[MLDSA_POLYT0_PACKEDBYTES], const mld_poly *a) |
| 776 |
{ |
| 777 |
unsigned int i; |
| 778 |
uint32_t t[8]; |
| 779 |
|
| 780 |
mld_assert_bound(a->coeffs, MLDSA_N, -(1 << (MLDSA_D - 1)) + 1, |
| 781 |
(1 << (MLDSA_D - 1)) + 1); |
| 782 |
|
| 783 |
for (i = 0; i < MLDSA_N / 8; ++i) |
| 784 |
__loop__( |
| 785 |
invariant(i <= MLDSA_N/8)) |
| 786 |
{ |
| 787 |
/* Safety: a->coeffs[i] <= (1 << (MLDSA_D - 1) as they are output of |
| 788 |
* power2round, hence, these casts are safe. */ |
| 789 |
t[0] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 0]); |
| 790 |
t[1] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 1]); |
| 791 |
t[2] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 2]); |
| 792 |
t[3] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 3]); |
| 793 |
t[4] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 4]); |
| 794 |
t[5] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 5]); |
| 795 |
t[6] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 6]); |
| 796 |
t[7] = (uint32_t)((1 << (MLDSA_D - 1)) - a->coeffs[8 * i + 7]); |
| 797 |
|
| 798 |
r[13 * i + 0] = (uint8_t)((t[0]) & 0xFF); |
| 799 |
r[13 * i + 1] = (uint8_t)((t[0] >> 8) & 0xFF); |
| 800 |
r[13 * i + 1] |= (uint8_t)((t[1] << 5) & 0xFF); |
| 801 |
r[13 * i + 2] = (uint8_t)((t[1] >> 3) & 0xFF); |
| 802 |
r[13 * i + 3] = (uint8_t)((t[1] >> 11) & 0xFF); |
| 803 |
r[13 * i + 3] |= (uint8_t)((t[2] << 2) & 0xFF); |
| 804 |
r[13 * i + 4] = (uint8_t)((t[2] >> 6) & 0xFF); |
| 805 |
r[13 * i + 4] |= (uint8_t)((t[3] << 7) & 0xFF); |
| 806 |
r[13 * i + 5] = (uint8_t)((t[3] >> 1) & 0xFF); |
| 807 |
r[13 * i + 6] = (uint8_t)((t[3] >> 9) & 0xFF); |
| 808 |
r[13 * i + 6] |= (uint8_t)((t[4] << 4) & 0xFF); |
| 809 |
r[13 * i + 7] = (uint8_t)((t[4] >> 4) & 0xFF); |
| 810 |
r[13 * i + 8] = (uint8_t)((t[4] >> 12) & 0xFF); |
| 811 |
r[13 * i + 8] |= (uint8_t)((t[5] << 1) & 0xFF); |
| 812 |
r[13 * i + 9] = (uint8_t)((t[5] >> 7) & 0xFF); |
| 813 |
r[13 * i + 9] |= (uint8_t)((t[6] << 6) & 0xFF); |
| 814 |
r[13 * i + 10] = (uint8_t)((t[6] >> 2) & 0xFF); |
| 815 |
r[13 * i + 11] = (uint8_t)((t[6] >> 10) & 0xFF); |
| 816 |
r[13 * i + 11] |= (uint8_t)((t[7] << 3) & 0xFF); |
| 817 |
r[13 * i + 12] = (uint8_t)((t[7] >> 5) & 0xFF); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
MLD_INTERNAL_API |
| 822 |
void mld_polyt0_unpack(mld_poly *r, const uint8_t a[MLDSA_POLYT0_PACKEDBYTES]) |
| 823 |
{ |
| 824 |
unsigned int i; |
| 825 |
|
| 826 |
for (i = 0; i < MLDSA_N / 8; ++i) |
| 827 |
__loop__( |
| 828 |
invariant(i <= MLDSA_N/8) |
| 829 |
invariant(array_bound(r->coeffs, 0, i*8, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))) |
| 830 |
{ |
| 831 |
r->coeffs[8 * i + 0] = a[13 * i + 0]; |
| 832 |
r->coeffs[8 * i + 0] |= (int32_t)a[13 * i + 1] << 8; |
| 833 |
r->coeffs[8 * i + 0] &= 0x1FFF; |
| 834 |
|
| 835 |
r->coeffs[8 * i + 1] = a[13 * i + 1] >> 5; |
| 836 |
r->coeffs[8 * i + 1] |= (int32_t)a[13 * i + 2] << 3; |
| 837 |
r->coeffs[8 * i + 1] |= (int32_t)a[13 * i + 3] << 11; |
| 838 |
r->coeffs[8 * i + 1] &= 0x1FFF; |
| 839 |
|
| 840 |
r->coeffs[8 * i + 2] = a[13 * i + 3] >> 2; |
| 841 |
r->coeffs[8 * i + 2] |= (int32_t)a[13 * i + 4] << 6; |
| 842 |
r->coeffs[8 * i + 2] &= 0x1FFF; |
| 843 |
|
| 844 |
r->coeffs[8 * i + 3] = a[13 * i + 4] >> 7; |
| 845 |
r->coeffs[8 * i + 3] |= (int32_t)a[13 * i + 5] << 1; |
| 846 |
r->coeffs[8 * i + 3] |= (int32_t)a[13 * i + 6] << 9; |
| 847 |
r->coeffs[8 * i + 3] &= 0x1FFF; |
| 848 |
|
| 849 |
r->coeffs[8 * i + 4] = a[13 * i + 6] >> 4; |
| 850 |
r->coeffs[8 * i + 4] |= (int32_t)a[13 * i + 7] << 4; |
| 851 |
r->coeffs[8 * i + 4] |= (int32_t)a[13 * i + 8] << 12; |
| 852 |
r->coeffs[8 * i + 4] &= 0x1FFF; |
| 853 |
|
| 854 |
r->coeffs[8 * i + 5] = a[13 * i + 8] >> 1; |
| 855 |
r->coeffs[8 * i + 5] |= (int32_t)a[13 * i + 9] << 7; |
| 856 |
r->coeffs[8 * i + 5] &= 0x1FFF; |
| 857 |
|
| 858 |
r->coeffs[8 * i + 6] = a[13 * i + 9] >> 6; |
| 859 |
r->coeffs[8 * i + 6] |= (int32_t)a[13 * i + 10] << 2; |
| 860 |
r->coeffs[8 * i + 6] |= (int32_t)a[13 * i + 11] << 10; |
| 861 |
r->coeffs[8 * i + 6] &= 0x1FFF; |
| 862 |
|
| 863 |
r->coeffs[8 * i + 7] = a[13 * i + 11] >> 3; |
| 864 |
r->coeffs[8 * i + 7] |= (int32_t)a[13 * i + 12] << 5; |
| 865 |
r->coeffs[8 * i + 7] &= 0x1FFF; |
| 866 |
|
| 867 |
r->coeffs[8 * i + 0] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 0]; |
| 868 |
r->coeffs[8 * i + 1] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 1]; |
| 869 |
r->coeffs[8 * i + 2] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 2]; |
| 870 |
r->coeffs[8 * i + 3] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 3]; |
| 871 |
r->coeffs[8 * i + 4] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 4]; |
| 872 |
r->coeffs[8 * i + 5] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 5]; |
| 873 |
r->coeffs[8 * i + 6] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 6]; |
| 874 |
r->coeffs[8 * i + 7] = (1 << (MLDSA_D - 1)) - r->coeffs[8 * i + 7]; |
| 875 |
} |
| 876 |
|
| 877 |
mld_assert_bound(r->coeffs, MLDSA_N, -(1 << (MLDSA_D - 1)) + 1, |
| 878 |
(1 << (MLDSA_D - 1)) + 1); |
| 879 |
} |
| 880 |
|
| 881 |
MLD_STATIC_TESTABLE uint32_t mld_poly_chknorm_c(const mld_poly *a, int32_t B) |
| 882 |
__contract__( |
| 883 |
requires(memory_no_alias(a, sizeof(mld_poly))) |
| 884 |
requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX) |
| 885 |
requires(array_bound(a->coeffs, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX)) |
| 886 |
ensures(return_value == 0 || return_value == 0xFFFFFFFF) |
| 887 |
ensures((return_value == 0) == array_abs_bound(a->coeffs, 0, MLDSA_N, B)) |
| 888 |
) |
| 889 |
{ |
| 890 |
unsigned int i; |
| 891 |
uint32_t t = 0; |
| 892 |
mld_assert_bound(a->coeffs, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, |
| 893 |
MLD_REDUCE32_RANGE_MAX); |
| 894 |
for (i = 0; i < MLDSA_N; ++i) |
| 895 |
__loop__( |
| 896 |
invariant(i <= MLDSA_N) |
| 897 |
invariant(t == 0 || t == 0xFFFFFFFF) |
| 898 |
invariant((t == 0) == array_abs_bound(a->coeffs, 0, i, B)) |
| 899 |
) |
| 900 |
{ |
| 901 |
/* |
| 902 |
* Since we know that -MLD_REDUCE32_RANGE_MAX <= a < MLD_REDUCE32_RANGE_MAX, |
| 903 |
* and B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX, to check if |
| 904 |
* -B < (a mod± MLDSA_Q) < B, it suffices to check if -B < a < B. |
| 905 |
* |
| 906 |
* We prove this to be true using the following CBMC assertions. |
| 907 |
* a ==> b expressed as !a || b to also allow run-time assertion. |
| 908 |
*/ |
| 909 |
mld_assert(a->coeffs[i] < B || a->coeffs[i] - MLDSA_Q <= -B); |
| 910 |
mld_assert(a->coeffs[i] > -B || a->coeffs[i] + MLDSA_Q >= B); |
| 911 |
|
| 912 |
/* Reference: Leaks which coefficient violates the bound via a conditional. |
| 913 |
* We are more conservative to reduce the number of declassifications in |
| 914 |
* constant-time testing. |
| 915 |
*/ |
| 916 |
|
| 917 |
/* if (abs(a[i]) >= B) */ |
| 918 |
t |= mld_ct_cmask_neg_i32(B - 1 - mld_ct_abs_i32(a->coeffs[i])); |
| 919 |
} |
| 920 |
|
| 921 |
return t; |
| 922 |
} |
| 923 |
|
| 924 |
/* Reference: explicitly checks the bound B to be <= (MLDSA_Q - 1) / 8). |
| 925 |
* This is unnecessary as it's always a compile-time constant. |
| 926 |
* We instead model it as a precondition. |
| 927 |
* Checking the bound is performed using a conditional arguing |
| 928 |
* that it is okay to leak which coefficient violates the bound (while the |
| 929 |
* coefficient itself must remain secret). |
| 930 |
* We instead perform everything in constant-time. |
| 931 |
* Also it is sufficient to check that it is smaller than |
| 932 |
* MLDSA_Q - MLD_REDUCE32_RANGE_MAX > (MLDSA_Q - 1) / 8). |
| 933 |
*/ |
| 934 |
MLD_INTERNAL_API |
| 935 |
uint32_t mld_poly_chknorm(const mld_poly *a, int32_t B) |
| 936 |
{ |
| 937 |
#if defined(MLD_USE_NATIVE_POLY_CHKNORM) |
| 938 |
int ret; |
| 939 |
int success; |
| 940 |
mld_assert_bound(a->coeffs, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, |
| 941 |
MLD_REDUCE32_RANGE_MAX); |
| 942 |
/* The native backend returns 0 if all coefficients are within the bound, |
| 943 |
* 1 if at least one coefficient exceeds the bound, and |
| 944 |
* -1 (MLD_NATIVE_FUNC_FALLBACK) if the platform does not have the |
| 945 |
* required capabilities to run the native function. |
| 946 |
*/ |
| 947 |
ret = mld_poly_chknorm_native(a->coeffs, B); |
| 948 |
|
| 949 |
success = (ret != MLD_NATIVE_FUNC_FALLBACK); |
| 950 |
/* Constant-time: It would be fine to leak the return value of chknorm |
| 951 |
* entirely (as it is fine to leak if any coefficient exceeded the bound or |
| 952 |
* not). However, it is cleaner to perform declassification in sign.c. |
| 953 |
* Hence, here we only declassify if the native function returned |
| 954 |
* MLD_NATIVE_FUNC_FALLBACK or not (which solely depends on system |
| 955 |
* capabilities). |
| 956 |
*/ |
| 957 |
MLD_CT_TESTING_DECLASSIFY(&success, sizeof(int)); |
| 958 |
if (success) |
| 959 |
{ |
| 960 |
/* Convert 0 / 1 to 0 / 0xFFFFFFFF here */ |
| 961 |
return 0U - (uint32_t)ret; |
| 962 |
} |
| 963 |
#endif /* MLD_USE_NATIVE_POLY_CHKNORM */ |
| 964 |
return mld_poly_chknorm_c(a, B); |
| 965 |
} |
| 966 |
|
| 967 |
#else /* !MLD_CONFIG_MULTILEVEL_NO_SHARED */ |
| 968 |
MLD_EMPTY_CU(mld_poly) |
| 969 |
#endif /* MLD_CONFIG_MULTILEVEL_NO_SHARED */ |
| 970 |
|
| 971 |
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros. |
| 972 |
* Don't modify by hand -- this is auto-generated by scripts/autogen. */ |
| 973 |
#undef MLD_POLY_UNIFORM_NBLOCKS |