Skip to content

Commit 5a3fc59

Browse files
willieyzmkannwischer
authored andcommitted
unit-test: add consistency test for rej_uniform and rej_uniform_x4
Add unit tests that verify mlk_poly_rej_uniform_x4 processing four different seeds produces the same results as four separate calls to mlk_poly_rej_uniform with those same individual seeds. Introduce NUM_RANDOM_TESTS_REJ_UNIFORM constant to prevent test timeouts on embedded platforms. The poly_rej_uniform operations are computationally intensive and require reduced iteration counts to stay within execution limits on resource-constrained targets like AVR ATmega128RFR2. Signed-off-by: willieyz <[email protected]>
1 parent d213ca8 commit 5a3fc59

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/src/test_unit.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../../mlkem/src/compress.h"
1212
#include "../../mlkem/src/poly.h"
1313
#include "../../mlkem/src/poly_k.h"
14+
#include "../../mlkem/src/sampling.h"
1415

1516
#ifndef NUM_RANDOM_TESTS
1617
#ifdef MLKEM_DEBUG
@@ -20,6 +21,14 @@
2021
#endif
2122
#endif /* !NUM_RANDOM_TESTS */
2223

24+
#ifndef NUM_RANDOM_TESTS_REJ_UNIFORM
25+
#ifdef MLKEM_DEBUG
26+
#define NUM_RANDOM_TESTS_REJ_UNIFORM 100
27+
#else
28+
#define NUM_RANDOM_TESTS_REJ_UNIFORM 1000
29+
#endif
30+
#endif /* !NUM_RANDOM_TESTS_REJ_UNIFORM */
31+
2332
/* Declarations for _c functions exposed by MLK_STATIC_TESTABLE= */
2433

2534
void mlk_poly_reduce_c(mlk_poly *r);
@@ -669,6 +678,49 @@ static int test_poly_compress_no_overflow(void)
669678
return 0;
670679
}
671680

681+
/* poly_rej_uniform and poly_rej_uniform_4x implement the same
682+
* functionality with different degrees of batching. This unit
683+
* test makes sure these functions indeed produce the same
684+
* outputs. */
685+
#if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
686+
static int test_poly_rej_uniform_consistency(void)
687+
{
688+
mlk_poly vec_x4[4], vec_x1[4];
689+
MLK_ALIGN uint8_t seed[4][MLK_ALIGN_UP(MLKEM_SYMBYTES + 2)];
690+
int i, j;
691+
692+
693+
for (i = 0; i < NUM_RANDOM_TESTS_REJ_UNIFORM; i++)
694+
{
695+
for (j = 0; j < 4; j++)
696+
{
697+
randombytes(seed[j], MLKEM_SYMBYTES + 2);
698+
}
699+
700+
/* Test x4 version */
701+
mlk_poly_rej_uniform_x4(&vec_x4[0], &vec_x4[1], &vec_x4[2], &vec_x4[3],
702+
seed);
703+
704+
/* Test x1 version with same seeds */
705+
for (j = 0; j < 4; j++)
706+
{
707+
mlk_poly_rej_uniform(&vec_x1[j], seed[j]);
708+
}
709+
710+
/* Compare results */
711+
for (j = 0; j < 4; j++)
712+
{
713+
CHECK(memcmp(vec_x4[j].coeffs, vec_x1[j].coeffs,
714+
MLKEM_N * sizeof(int16_t)) == 0);
715+
}
716+
}
717+
718+
return 0;
719+
}
720+
#endif /* !MLK_CONFIG_SERIAL_FIPS202_ONLY */
721+
722+
723+
672724
int main(void)
673725
{
674726
/* WARNING: Test-only
@@ -690,5 +742,9 @@ int main(void)
690742
/* Test poly compress no overflow */
691743
CHECK(test_poly_compress_no_overflow() == 0);
692744

745+
#if !defined(MLK_CONFIG_SERIAL_FIPS202_ONLY)
746+
CHECK(test_poly_rej_uniform_consistency() == 0);
747+
#endif
748+
693749
return 0;
694750
}

0 commit comments

Comments
 (0)