| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stddef.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
#include "../notrandombytes/notrandombytes.h" |
| 10 |
|
| 11 |
#include "../../mldsa/src/poly.h" |
| 12 |
#include "../../mldsa/src/poly_kl.h" |
| 13 |
#include "../../mldsa/src/polyvec.h" |
| 14 |
|
| 15 |
#ifndef NUM_RANDOM_TESTS |
| 16 |
#ifdef MLDSA_DEBUG |
| 17 |
#define NUM_RANDOM_TESTS 1000 |
| 18 |
#else |
| 19 |
#define NUM_RANDOM_TESTS 5000 |
| 20 |
#endif |
| 21 |
#endif |
| 22 |
|
| 23 |
#define CHECK(x) \ |
| 24 |
do \ |
| 25 |
{ \ |
| 26 |
int r; \ |
| 27 |
r = (x); \ |
| 28 |
if (!r) \ |
| 29 |
{ \ |
| 30 |
fprintf(stderr, "ERROR (%s,%d)\n", __FILE__, __LINE__); \ |
| 31 |
return 1; \ |
| 32 |
} \ |
| 33 |
} while (0) |
| 34 |
|
| 35 |
|
| 36 |
void mld_poly_ntt_c(mld_poly *a); |
| 37 |
void mld_poly_invntt_tomont_c(mld_poly *a); |
| 38 |
void mld_poly_caddq_c(mld_poly *a); |
| 39 |
void mld_poly_decompose_c(mld_poly *a1, mld_poly *a0); |
| 40 |
void mld_poly_use_hint_c(mld_poly *b, const mld_poly *a, const mld_poly *h); |
| 41 |
uint32_t mld_poly_chknorm_c(const mld_poly *a, int32_t B); |
| 42 |
void mld_poly_pointwise_montgomery_c(mld_poly *c, const mld_poly *a, |
| 43 |
const mld_poly *b); |
| 44 |
void mld_polyvecl_pointwise_acc_montgomery_c(mld_poly *w, const mld_polyvecl *u, |
| 45 |
const mld_polyvecl *v); |
| 46 |
void mld_polyz_unpack_c(mld_poly *r, const uint8_t a[MLDSA_POLYZ_PACKEDBYTES]); |
| 47 |
#if defined(MLD_USE_NATIVE_NTT) || defined(MLD_USE_NATIVE_INTT) || \ |
| 48 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32) || \ |
| 49 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88) || \ |
| 50 |
defined(MLD_USE_NATIVE_POLY_CADDQ) || \ |
| 51 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_88) || \ |
| 52 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_32) || \ |
| 53 |
defined(MLD_USE_NATIVE_POLY_CHKNORM) || \ |
| 54 |
defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY) || \ |
| 55 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4) || \ |
| 56 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5) || \ |
| 57 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7) || \ |
| 58 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_17) || \ |
| 59 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_19) |
| 60 |
|
| 61 |
static void print_i32_array(const char *label, const int32_t *array, size_t len) |
| 62 |
{ |
| 63 |
size_t i; |
| 64 |
fprintf(stderr, "%s:\n", label); |
| 65 |
for (i = 0; i < len; i++) |
| 66 |
{ |
| 67 |
if (i % 8 == 0) |
| 68 |
{ |
| 69 |
fprintf(stderr, " "); |
| 70 |
} |
| 71 |
fprintf(stderr, "%8d", array[i]); |
| 72 |
if (i % 8 == 7) |
| 73 |
{ |
| 74 |
fprintf(stderr, "\n"); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
fprintf(stderr, " "); |
| 79 |
} |
| 80 |
} |
| 81 |
if (len % 8 != 0) |
| 82 |
{ |
| 83 |
fprintf(stderr, "\n"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
static void generate_i32_array_zeros(int32_t *data, size_t len) |
| 88 |
{ |
| 89 |
memset(data, 0, len * sizeof(int32_t)); |
| 90 |
} |
| 91 |
|
| 92 |
static void generate_i32_array_single(int32_t *data, size_t len, size_t pos, |
| 93 |
int32_t value) |
| 94 |
{ |
| 95 |
memset(data, 0, len * sizeof(int32_t)); |
| 96 |
data[pos] = value; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static void generate_i32_array_ranged(int32_t *data, size_t len, int min_incl, |
| 105 |
int max_excl) |
| 106 |
{ |
| 107 |
size_t i; |
| 108 |
|
| 109 |
randombytes((uint8_t *)data, len * sizeof(int32_t)); |
| 110 |
for (i = 0; i < len; i++) |
| 111 |
{ |
| 112 |
data[i] = (int32_t)((unsigned)min_incl + |
| 113 |
((unsigned)data[i] % (unsigned)(max_excl - min_incl))); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
static int compare_i32_arrays(const int32_t *a, const int32_t *b, unsigned len, |
| 118 |
const char *test_name, const int32_t *input) |
| 119 |
{ |
| 120 |
unsigned i; |
| 121 |
for (i = 0; i < len; i++) |
| 122 |
{ |
| 123 |
if (a[i] != b[i]) |
| 124 |
{ |
| 125 |
fprintf(stderr, "FAIL: %s\n", test_name); |
| 126 |
fprintf(stderr, |
| 127 |
" First difference at index %u: native=%d, reference=%d\n", i, |
| 128 |
a[i], b[i]); |
| 129 |
if (input) |
| 130 |
{ |
| 131 |
print_i32_array("Input", input, len); |
| 132 |
} |
| 133 |
print_i32_array("Native result", a, len); |
| 134 |
print_i32_array("Reference result", b, len); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
} |
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
#ifdef MLD_USE_NATIVE_NTT |
| 142 |
static int test_ntt_core(const int32_t *input, const char *test_name) |
| 143 |
{ |
| 144 |
mld_poly test_poly, ref_poly; |
| 145 |
|
| 146 |
memcpy(test_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 147 |
memcpy(ref_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 148 |
|
| 149 |
mld_poly_ntt(&test_poly); |
| 150 |
mld_poly_ntt_c(&ref_poly); |
| 151 |
|
| 152 |
#ifdef MLD_USE_NATIVE_NTT_CUSTOM_ORDER |
| 153 |
mld_poly_permute_bitrev_to_custom(ref_poly.coeffs); |
| 154 |
#endif |
| 155 |
|
| 156 |
|
| 157 |
mld_poly_reduce(&ref_poly); |
| 158 |
mld_poly_reduce(&test_poly); |
| 159 |
|
| 160 |
mld_poly_caddq_c(&ref_poly); |
| 161 |
mld_poly_caddq_c(&test_poly); |
| 162 |
|
| 163 |
CHECK(compare_i32_arrays(test_poly.coeffs, ref_poly.coeffs, MLDSA_N, |
| 164 |
test_name, input)); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
static int test_native_ntt(void) |
| 169 |
{ |
| 170 |
int32_t test_data[MLDSA_N]; |
| 171 |
int pos, i; |
| 172 |
|
| 173 |
generate_i32_array_zeros(test_data, MLDSA_N); |
| 174 |
CHECK(test_ntt_core(test_data, "ntt_zeros") == 0); |
| 175 |
|
| 176 |
for (pos = 0; pos < MLDSA_N; pos += MLDSA_N / 8) |
| 177 |
{ |
| 178 |
generate_i32_array_single(test_data, MLDSA_N, (size_t)pos, 1); |
| 179 |
CHECK(test_ntt_core(test_data, "ntt_single") == 0); |
| 180 |
} |
| 181 |
|
| 182 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 183 |
{ |
| 184 |
generate_i32_array_ranged(test_data, MLDSA_N, -MLDSA_Q + 1, MLDSA_Q); |
| 185 |
CHECK(test_ntt_core(test_data, "ntt_random") == 0); |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
#endif |
| 191 |
|
| 192 |
#ifdef MLD_USE_NATIVE_INTT |
| 193 |
static int test_invntt_tomont_core(const int32_t *input, const char *test_name) |
| 194 |
{ |
| 195 |
mld_poly test_poly, ref_poly; |
| 196 |
|
| 197 |
memcpy(test_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 198 |
memcpy(ref_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 199 |
|
| 200 |
#ifdef MLD_USE_NATIVE_NTT_CUSTOM_ORDER |
| 201 |
mld_poly_permute_bitrev_to_custom(test_poly.coeffs); |
| 202 |
#endif |
| 203 |
|
| 204 |
mld_poly_invntt_tomont(&test_poly); |
| 205 |
mld_poly_invntt_tomont_c(&ref_poly); |
| 206 |
|
| 207 |
|
| 208 |
mld_poly_reduce(&ref_poly); |
| 209 |
mld_poly_reduce(&test_poly); |
| 210 |
|
| 211 |
mld_poly_caddq_c(&ref_poly); |
| 212 |
mld_poly_caddq_c(&test_poly); |
| 213 |
|
| 214 |
CHECK(compare_i32_arrays(test_poly.coeffs, ref_poly.coeffs, MLDSA_N, |
| 215 |
test_name, input)); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
static int test_native_invntt_tomont(void) |
| 220 |
{ |
| 221 |
int32_t test_data[MLDSA_N]; |
| 222 |
int pos, i; |
| 223 |
|
| 224 |
generate_i32_array_zeros(test_data, MLDSA_N); |
| 225 |
CHECK(test_invntt_tomont_core(test_data, "invntt_tomont_zeros") == 0); |
| 226 |
|
| 227 |
for (pos = 0; pos < MLDSA_N; pos += MLDSA_N / 8) |
| 228 |
{ |
| 229 |
generate_i32_array_single(test_data, MLDSA_N, (size_t)pos, 1); |
| 230 |
CHECK(test_invntt_tomont_core(test_data, "invntt_tomont_single") == 0); |
| 231 |
} |
| 232 |
|
| 233 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 234 |
{ |
| 235 |
generate_i32_array_ranged(test_data, MLDSA_N, -MLDSA_Q + 1, MLDSA_Q); |
| 236 |
CHECK(test_invntt_tomont_core(test_data, "invntt_tomont_random") == 0); |
| 237 |
} |
| 238 |
|
| 239 |
return 0; |
| 240 |
} |
| 241 |
#endif |
| 242 |
|
| 243 |
#if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32) || \ |
| 244 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88) |
| 245 |
static int test_poly_decompose_core(const mld_poly *input_poly, |
| 246 |
const char *test_name) |
| 247 |
{ |
| 248 |
mld_poly test_a1, test_a0, ref_a1, ref_a0; |
| 249 |
|
| 250 |
mld_memcpy(&test_a0, input_poly, sizeof(mld_poly)); |
| 251 |
mld_memcpy(&ref_a0, input_poly, sizeof(mld_poly)); |
| 252 |
|
| 253 |
mld_poly_decompose(&test_a1, &test_a0); |
| 254 |
mld_poly_decompose_c(&ref_a1, &ref_a0); |
| 255 |
|
| 256 |
CHECK(compare_i32_arrays(test_a1.coeffs, ref_a1.coeffs, MLDSA_N, test_name, |
| 257 |
input_poly->coeffs)); |
| 258 |
CHECK(compare_i32_arrays(test_a0.coeffs, ref_a0.coeffs, MLDSA_N, test_name, |
| 259 |
input_poly->coeffs)); |
| 260 |
return 0; |
| 261 |
} |
| 262 |
static int test_native_decompose(void) |
| 263 |
{ |
| 264 |
mld_poly test_poly; |
| 265 |
int i; |
| 266 |
|
| 267 |
generate_i32_array_zeros(test_poly.coeffs, MLDSA_N); |
| 268 |
CHECK(test_poly_decompose_core(&test_poly, "poly_decompose_zeros") == 0); |
| 269 |
|
| 270 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 271 |
{ |
| 272 |
generate_i32_array_ranged(test_poly.coeffs, MLDSA_N, 0, MLDSA_Q); |
| 273 |
CHECK(test_poly_decompose_core(&test_poly, "poly_decompose_random") == 0); |
| 274 |
} |
| 275 |
|
| 276 |
return 0; |
| 277 |
} |
| 278 |
#endif |
| 279 |
|
| 280 |
|
| 281 |
#if defined(MLD_USE_NATIVE_POLY_CADDQ) |
| 282 |
static int test_caddq_core(const int32_t *input, const char *test_name) |
| 283 |
{ |
| 284 |
mld_poly test_poly, ref_poly; |
| 285 |
|
| 286 |
memcpy(test_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 287 |
memcpy(ref_poly.coeffs, input, MLDSA_N * sizeof(int32_t)); |
| 288 |
|
| 289 |
mld_poly_caddq(&test_poly); |
| 290 |
mld_poly_caddq_c(&ref_poly); |
| 291 |
|
| 292 |
CHECK(compare_i32_arrays(test_poly.coeffs, ref_poly.coeffs, MLDSA_N, |
| 293 |
test_name, input)); |
| 294 |
return 0; |
| 295 |
} |
| 296 |
static int test_native_caddq(void) |
| 297 |
{ |
| 298 |
int32_t test_data[MLDSA_N]; |
| 299 |
int pos, i; |
| 300 |
|
| 301 |
generate_i32_array_zeros(test_data, MLDSA_N); |
| 302 |
CHECK(test_caddq_core(test_data, "poly_caddq_zeros") == 0); |
| 303 |
|
| 304 |
for (pos = 0; pos < MLDSA_N; pos += MLDSA_N / 8) |
| 305 |
{ |
| 306 |
generate_i32_array_single(test_data, MLDSA_N, (size_t)pos, 1); |
| 307 |
CHECK(test_caddq_core(test_data, "poly_caddq_single") == 0); |
| 308 |
} |
| 309 |
|
| 310 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 311 |
{ |
| 312 |
generate_i32_array_ranged(test_data, MLDSA_N, -MLDSA_Q + 1, MLDSA_Q); |
| 313 |
CHECK(test_caddq_core(test_data, "poly_caddq_random") == 0); |
| 314 |
} |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
#endif |
| 319 |
|
| 320 |
#if defined(MLD_USE_NATIVE_POLY_USE_HINT_88) || \ |
| 321 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_32) |
| 322 |
static int test_poly_use_hint_core(const mld_poly *poly_a, |
| 323 |
const mld_poly *poly_h, |
| 324 |
const char *test_name) |
| 325 |
{ |
| 326 |
mld_poly test_b, ref_b; |
| 327 |
|
| 328 |
mld_poly_use_hint(&test_b, poly_a, poly_h); |
| 329 |
mld_poly_use_hint_c(&ref_b, poly_a, poly_h); |
| 330 |
|
| 331 |
CHECK(compare_i32_arrays(test_b.coeffs, ref_b.coeffs, MLDSA_N, test_name, |
| 332 |
poly_a->coeffs)); |
| 333 |
return 0; |
| 334 |
} |
| 335 |
static int test_native_use_hint(void) |
| 336 |
{ |
| 337 |
mld_poly poly_a, poly_h; |
| 338 |
int i; |
| 339 |
generate_i32_array_zeros(poly_a.coeffs, MLDSA_N); |
| 340 |
generate_i32_array_zeros(poly_h.coeffs, MLDSA_N); |
| 341 |
CHECK(test_poly_use_hint_core(&poly_a, &poly_h, "poly_use_hint_zeros") == 0); |
| 342 |
|
| 343 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 344 |
{ |
| 345 |
generate_i32_array_ranged(poly_a.coeffs, MLDSA_N, 0, MLDSA_Q); |
| 346 |
generate_i32_array_ranged(poly_h.coeffs, MLDSA_N, 0, 2); |
| 347 |
CHECK(test_poly_use_hint_core(&poly_a, &poly_h, "poly_use_hint_random") == |
| 348 |
0); |
| 349 |
} |
| 350 |
|
| 351 |
return 0; |
| 352 |
} |
| 353 |
#endif |
| 354 |
|
| 355 |
|
| 356 |
#if defined(MLD_USE_NATIVE_POLY_CHKNORM) |
| 357 |
static int test_poly_chknorm_core(const mld_poly *input_poly, int32_t B, |
| 358 |
const char *test_name) |
| 359 |
{ |
| 360 |
uint32_t test_result, ref_result; |
| 361 |
|
| 362 |
test_result = mld_poly_chknorm(input_poly, B); |
| 363 |
ref_result = mld_poly_chknorm_c(input_poly, B); |
| 364 |
|
| 365 |
if (test_result != ref_result) |
| 366 |
{ |
| 367 |
fprintf(stderr, "FAIL: %s - result mismatch: native=%u, ref=%u\n", |
| 368 |
test_name, test_result, ref_result); |
| 369 |
return 1; |
| 370 |
} |
| 371 |
|
| 372 |
return 0; |
| 373 |
} |
| 374 |
|
| 375 |
static int test_native_poly_chknorm(void) |
| 376 |
{ |
| 377 |
mld_poly test_poly; |
| 378 |
int i; |
| 379 |
|
| 380 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 381 |
{ |
| 382 |
generate_i32_array_ranged(test_poly.coeffs, MLDSA_N, -REDUCE32_RANGE_MAX, |
| 383 |
REDUCE32_RANGE_MAX); |
| 384 |
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_Q - REDUCE32_RANGE_MAX, |
| 385 |
"poly_chknorm_MAX_B") == 0); |
| 386 |
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_GAMMA1 - MLDSA_BETA, |
| 387 |
"poly_chknorm_gamma1_minus_beta") == 0); |
| 388 |
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_GAMMA2 - MLDSA_BETA, |
| 389 |
"poly_chknorm_gamma2_minus_beta") == 0); |
| 390 |
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_GAMMA2, |
| 391 |
"poly_chknorm_gamma2") == 0); |
| 392 |
} |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
#endif |
| 397 |
|
| 398 |
#if defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY) |
| 399 |
static int test_poly_pointwise_montgomery_core(const mld_poly *poly_a, |
| 400 |
const mld_poly *poly_b, |
| 401 |
const char *test_name) |
| 402 |
{ |
| 403 |
mld_poly test_poly_c, ref_poly_c; |
| 404 |
|
| 405 |
mld_poly_pointwise_montgomery(&test_poly_c, poly_a, poly_b); |
| 406 |
mld_poly_pointwise_montgomery_c(&ref_poly_c, poly_a, poly_b); |
| 407 |
|
| 408 |
CHECK(compare_i32_arrays(test_poly_c.coeffs, ref_poly_c.coeffs, MLDSA_N, |
| 409 |
test_name, poly_a->coeffs)); |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
static int test_native_pointwise_montgomery(void) |
| 414 |
{ |
| 415 |
mld_poly test_poly_a, test_poly_b; |
| 416 |
int pos, i; |
| 417 |
|
| 418 |
generate_i32_array_zeros(test_poly_a.coeffs, MLDSA_N); |
| 419 |
generate_i32_array_zeros(test_poly_b.coeffs, MLDSA_N); |
| 420 |
CHECK(test_poly_pointwise_montgomery_core(&test_poly_a, &test_poly_b, |
| 421 |
"pointwise_montgomery_zeros") == 0); |
| 422 |
|
| 423 |
for (pos = 0; pos < MLDSA_N; pos += MLDSA_N / 8) |
| 424 |
{ |
| 425 |
generate_i32_array_single(test_poly_a.coeffs, MLDSA_N, (size_t)pos, 1); |
| 426 |
generate_i32_array_single(test_poly_b.coeffs, MLDSA_N, (size_t)pos, 1); |
| 427 |
CHECK(test_poly_pointwise_montgomery_core( |
| 428 |
&test_poly_a, &test_poly_b, "pointwise_montgomery_single") == 0); |
| 429 |
} |
| 430 |
|
| 431 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 432 |
{ |
| 433 |
generate_i32_array_ranged(test_poly_a.coeffs, MLDSA_N, -(MLD_NTT_BOUND - 1), |
| 434 |
MLD_NTT_BOUND); |
| 435 |
generate_i32_array_ranged(test_poly_b.coeffs, MLDSA_N, -(MLD_NTT_BOUND - 1), |
| 436 |
MLD_NTT_BOUND); |
| 437 |
CHECK(test_poly_pointwise_montgomery_core( |
| 438 |
&test_poly_a, &test_poly_b, "pointwise_montgomery_random") == 0); |
| 439 |
} |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
#endif |
| 444 |
|
| 445 |
#if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4) || \ |
| 446 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5) || \ |
| 447 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7) |
| 448 |
static int test_polyvecl_pointwise_acc_montgomery_core(const mld_polyvecl *u, |
| 449 |
const mld_polyvecl *v, |
| 450 |
const char *test_name) |
| 451 |
{ |
| 452 |
mld_poly test_w, ref_w; |
| 453 |
|
| 454 |
mld_polyvecl_pointwise_acc_montgomery(&test_w, u, v); |
| 455 |
mld_polyvecl_pointwise_acc_montgomery_c(&ref_w, u, v); |
| 456 |
|
| 457 |
CHECK(compare_i32_arrays(test_w.coeffs, ref_w.coeffs, MLDSA_N, test_name, |
| 458 |
NULL)); |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
static int test_native_polyvecl_pointwise_acc_montgomery(void) |
| 463 |
{ |
| 464 |
mld_polyvecl u, v; |
| 465 |
unsigned int i; |
| 466 |
|
| 467 |
|
| 468 |
generate_i32_array_zeros((int32_t *)&u, MLDSA_L * MLDSA_N); |
| 469 |
generate_i32_array_zeros((int32_t *)&v, MLDSA_L * MLDSA_N); |
| 470 |
CHECK(test_polyvecl_pointwise_acc_montgomery_core(&u, &v, |
| 471 |
"polyvecl_acc_zeros") == 0); |
| 472 |
|
| 473 |
|
| 474 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 475 |
{ |
| 476 |
generate_i32_array_ranged((int32_t *)&u, MLDSA_L * MLDSA_N, 0, MLDSA_Q); |
| 477 |
generate_i32_array_ranged((int32_t *)&v, MLDSA_L * MLDSA_N, |
| 478 |
-MLD_NTT_BOUND + 1, MLD_NTT_BOUND); |
| 479 |
CHECK(test_polyvecl_pointwise_acc_montgomery_core( |
| 480 |
&u, &v, "polyvecl_acc_random") == 0); |
| 481 |
} |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |
| 485 |
#endif |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
#if defined(MLD_USE_NATIVE_POLYZ_UNPACK_17) || \ |
| 491 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_19) |
| 492 |
static int test_mld_polyz_unpack_core(const uint8_t *input, |
| 493 |
const char *test_name) |
| 494 |
{ |
| 495 |
mld_poly test_poly, ref_poly; |
| 496 |
|
| 497 |
mld_polyz_unpack(&test_poly, input); |
| 498 |
mld_polyz_unpack_c(&ref_poly, input); |
| 499 |
|
| 500 |
CHECK(compare_i32_arrays(test_poly.coeffs, ref_poly.coeffs, MLDSA_N, |
| 501 |
test_name, NULL)); |
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
static int test_native_polyz_unpack(void) |
| 506 |
{ |
| 507 |
uint8_t test_bytes[MLDSA_POLYZ_PACKEDBYTES]; |
| 508 |
int i; |
| 509 |
|
| 510 |
memset(test_bytes, 0, MLDSA_POLYZ_PACKEDBYTES); |
| 511 |
CHECK(test_mld_polyz_unpack_core(test_bytes, "polyz_unpack_zeros") == 0); |
| 512 |
|
| 513 |
|
| 514 |
for (i = 0; i < NUM_RANDOM_TESTS; i++) |
| 515 |
{ |
| 516 |
randombytes(test_bytes, MLDSA_POLYZ_PACKEDBYTES); |
| 517 |
CHECK(test_mld_polyz_unpack_core(test_bytes, "polyz_unpack_random") == 0); |
| 518 |
} |
| 519 |
|
| 520 |
return 0; |
| 521 |
} |
| 522 |
#endif |
| 523 |
|
| 524 |
|
| 525 |
static int test_backend_units(void) |
| 526 |
{ |
| 527 |
|
| 528 |
randombytes_reset(); |
| 529 |
|
| 530 |
|
| 531 |
#ifdef MLD_USE_NATIVE_NTT |
| 532 |
CHECK(test_native_ntt() == 0); |
| 533 |
#endif |
| 534 |
|
| 535 |
#ifdef MLD_USE_NATIVE_INTT |
| 536 |
CHECK(test_native_invntt_tomont() == 0); |
| 537 |
#endif |
| 538 |
|
| 539 |
#if defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32) || \ |
| 540 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88) |
| 541 |
CHECK(test_native_decompose() == 0); |
| 542 |
#endif |
| 543 |
|
| 544 |
#ifdef MLD_USE_NATIVE_POLY_CADDQ |
| 545 |
CHECK(test_native_caddq() == 0); |
| 546 |
#endif |
| 547 |
|
| 548 |
#if defined(MLD_USE_NATIVE_POLY_USE_HINT_88) || \ |
| 549 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_32) |
| 550 |
CHECK(test_native_use_hint() == 0); |
| 551 |
#endif |
| 552 |
|
| 553 |
#if defined(MLD_USE_NATIVE_POLY_CHKNORM) |
| 554 |
CHECK(test_native_poly_chknorm() == 0); |
| 555 |
#endif |
| 556 |
|
| 557 |
#if defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY) |
| 558 |
CHECK(test_native_pointwise_montgomery() == 0); |
| 559 |
#endif |
| 560 |
|
| 561 |
#if defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4) || \ |
| 562 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5) || \ |
| 563 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7) |
| 564 |
CHECK(test_native_polyvecl_pointwise_acc_montgomery() == 0); |
| 565 |
#endif |
| 566 |
|
| 567 |
#if defined(MLD_USE_NATIVE_POLYZ_UNPACK_17) || \ |
| 568 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_19) |
| 569 |
CHECK(test_native_polyz_unpack() == 0); |
| 570 |
#endif |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
#endif |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
int main(void) |
| 585 |
{ |
| 586 |
|
| 587 |
|
| 588 |
randombytes_reset(); |
| 589 |
|
| 590 |
|
| 591 |
#if defined(MLD_USE_NATIVE_NTT) || defined(MLD_USE_NATIVE_INTT) || \ |
| 592 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_32) || \ |
| 593 |
defined(MLD_USE_NATIVE_POLY_DECOMPOSE_88) || \ |
| 594 |
defined(MLD_USE_NATIVE_POLY_CADDQ) || \ |
| 595 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_88) || \ |
| 596 |
defined(MLD_USE_NATIVE_POLY_USE_HINT_32) || \ |
| 597 |
defined(MLD_USE_NATIVE_POLY_CHKNORM) || \ |
| 598 |
defined(MLD_USE_NATIVE_POINTWISE_MONTGOMERY) || \ |
| 599 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L4) || \ |
| 600 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L5) || \ |
| 601 |
defined(MLD_USE_NATIVE_POLYVECL_POINTWISE_ACC_MONTGOMERY_L7) || \ |
| 602 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_17) || \ |
| 603 |
defined(MLD_USE_NATIVE_POLYZ_UNPACK_19) |
| 604 |
CHECK(test_backend_units() == 0); |
| 605 |
#endif |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
return 0; |
| 617 |
} |