Skip to content

Commit a1bb750

Browse files
committed
Guard tests & add CHECK macro
1 parent 79878f1 commit a1bb750

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

test/test_unit.c

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#define STATE_BYTES (MLD_KECCAK_LANES * sizeof(uint64_t))
5050

51+
#if defined(MLD_USE_FIPS202_X1_XOR_NATIVE) || defined(MLD_USE_FIPS202_X4_XOR_NATIVE)
5152
/* Bit interleave/deinterleave helpers as specified */
5253
static uint64_t test_priv_bit_interleave(uint64_t in)
5354
{
@@ -92,7 +93,9 @@ static void deinterleave_state(uint64_t out[MLD_KECCAK_LANES], const uint64_t in
9293
{
9394
for (int i = 0; i < MLD_KECCAK_LANES; i++) out[i] = test_priv_bit_deinterleave(in[i]);
9495
}
96+
#endif /* MLD_USE_FIPS202_X1_XOR_NATIVE || MLD_USE_FIPS202_X4_XOR_NATIVE */
9597

98+
#if defined(MLD_USE_FIPS202_X4_NATIVE) && defined(MLD_USE_FIPS202_X4_XOR_NATIVE)
9699
/* X4 native packing: 800 bytes total: [even half(400)] [odd half(400)]
97100
* Each half: 25 lanes x 16 bytes; each lane holds 4x uint32 (channel 0..3)
98101
* Value per lane comes from bit-interleaved 64-bit: low32=even, high32=odd. */
@@ -150,7 +153,9 @@ static void unpack_x4_native(uint64_t ch_interleaved_out[MLD_KECCAK_WAY][MLD_KEC
150153
}
151154
}
152155
}
156+
#endif /* MLD_USE_FIPS202_X4_NATIVE && MLD_USE_FIPS202_X4_XOR_NATIVE */
153157

158+
#if defined(MLD_USE_FIPS202_X4_NATIVE)
154159
static int test_x4_xor_bytes(void)
155160
{
156161
int fails = 0;
@@ -181,18 +186,12 @@ static int test_x4_xor_bytes(void)
181186
for (int ch = 0; ch < 4; ch++) {
182187
uint64_t norm[MLD_KECCAK_LANES];
183188
deinterleave_state(norm, ch_inter[ch]);
184-
if (memcmp(norm, ref[ch], sizeof norm) != 0) {
185-
fprintf(stderr, "x4_xor_bytes mismatch ch=%d off=%u len=%u\n", ch, off, len);
186-
return ++fails;
187-
}
189+
CHECK(memcmp(norm, ref[ch], sizeof norm) == 0);
188190
}
189191
#else
190192
for (int ch = 0; ch < 4; ch++) {
191193
uint64_t *ch_state = ((uint64_t*)x4buf) + (size_t)ch * MLD_KECCAK_LANES;
192-
if (memcmp(ch_state, ref[ch], sizeof(ref[ch])) != 0) {
193-
fprintf(stderr, "x4_xor_bytes mismatch (fallback) ch=%d off=%u len=%u\n", ch, off, len);
194-
return ++fails;
195-
}
194+
CHECK(memcmp(ch_state, ref[ch], sizeof(ref[ch])) == 0);
196195
}
197196
#endif
198197
}
@@ -236,10 +235,7 @@ static int test_x4_extract_bytes(void)
236235
test_priv_keccakf1600_extract_bytes(ref[3], out_ref[3], off, len);
237236
mld_keccakf1600x4_extract_bytes(x4buf, out_nat[0], out_nat[1], out_nat[2], out_nat[3], off, len);
238237
for (int ch = 0; ch < 4; ch++) {
239-
if (memcmp(out_ref[ch], out_nat[ch], len) != 0) {
240-
fprintf(stderr, "x4_extract_bytes mismatch ch=%d off=%u len=%u\n", ch, off, len);
241-
return ++fails;
242-
}
238+
CHECK(memcmp(out_ref[ch], out_nat[ch], len) == 0);
243239
}
244240
}
245241
}
@@ -276,20 +272,14 @@ static int test_x4_permute(void)
276272
for (int ch = 0; ch < 4; ch++) {
277273
uint64_t norm[MLD_KECCAK_LANES];
278274
deinterleave_state(norm, inter_out[ch]);
279-
if (memcmp(norm, ref[ch], sizeof norm) != 0) {
280-
fprintf(stderr, "x4_permute mismatch (case1) ch=%d\n", ch);
281-
return ++fails;
282-
}
275+
CHECK(memcmp(norm, ref[ch], sizeof norm) == 0);
283276
}
284277
#else
285278
for (int ch = 0; ch < 4; ch++) memcpy(((uint64_t*)x4buf) + (size_t)ch * MLD_KECCAK_LANES, init[ch], sizeof(init[ch]));
286279
mld_keccakf1600x4_permute(x4buf);
287280
for (int ch = 0; ch < 4; ch++) {
288281
uint64_t *ch_state = ((uint64_t*)x4buf) + (size_t)ch * MLD_KECCAK_LANES;
289-
if (memcmp(ch_state, ref[ch], sizeof(ref[ch])) != 0) {
290-
fprintf(stderr, "x4_permute mismatch (case1,fallback) ch=%d\n", ch);
291-
return ++fails;
292-
}
282+
CHECK(memcmp(ch_state, ref[ch], sizeof(ref[ch])) == 0);
293283
}
294284
#endif
295285
}
@@ -323,27 +313,23 @@ static int test_x4_permute(void)
323313
for (int ch = 0; ch < 4; ch++) {
324314
uint64_t norm[MLD_KECCAK_LANES];
325315
deinterleave_state(norm, inter_out[ch]);
326-
if (memcmp(norm, ref[ch], sizeof norm) != 0) {
327-
fprintf(stderr, "x4_permute mismatch (case2) ch=%d\n", ch);
328-
return ++fails;
329-
}
316+
CHECK(memcmp(norm, ref[ch], sizeof norm) == 0);
330317
}
331318
#else
332319
for (int ch = 0; ch < 4; ch++) memcpy(((uint64_t*)x4buf) + (size_t)ch * MLD_KECCAK_LANES, init[ch], sizeof(init[ch]));
333320
mld_keccakf1600x4_permute(x4buf);
334321
for (int ch = 0; ch < 4; ch++) {
335322
uint64_t *ch_state = ((uint64_t*)x4buf) + (size_t)ch * MLD_KECCAK_LANES;
336-
if (memcmp(ch_state, ref[ch], sizeof(ref[ch])) != 0) {
337-
fprintf(stderr, "x4_permute mismatch (case2,fallback) ch=%d\n", ch);
338-
return ++fails;
339-
}
323+
CHECK(memcmp(ch_state, ref[ch], sizeof(ref[ch])) == 0);
340324
}
341325
#endif
342326
}
343327

344328
return fails;
345329
}
330+
#endif /* MLD_USE_FIPS202_X4_NATIVE */
346331

332+
#if defined(MLD_USE_FIPS202_X1_NATIVE)
347333
static int test_xor_bytes(void)
348334
{
349335
int fails = 0;
@@ -364,14 +350,10 @@ static int test_xor_bytes(void)
364350
#if defined(MLD_USE_FIPS202_X1_XOR_NATIVE)
365351
uint64_t nat_s_norm[MLD_KECCAK_LANES];
366352
deinterleave_state(nat_s_norm, nat_s);
367-
if (memcmp(ref_s, nat_s_norm, sizeof ref_s) != 0) {
353+
CHECK(memcmp(ref_s, nat_s_norm, sizeof ref_s) == 0);
368354
#else
369-
if (memcmp(ref_s, nat_s, sizeof ref_s) != 0) {
355+
CHECK(memcmp(ref_s, nat_s, sizeof ref_s) == 0);
370356
#endif
371-
fprintf(stderr, "xor_bytes mismatch at off=%u len=%u\n", off, len);
372-
fails++;
373-
return fails; /* early exit for debug */
374-
}
375357
}
376358
}
377359
return fails;
@@ -401,11 +383,7 @@ static int test_extract_bytes(void)
401383
memset(out_nat, 0, len);
402384
test_priv_keccakf1600_extract_bytes(ref_s, out_ref, off, len);
403385
mld_keccakf1600_extract_bytes(nat_s, out_nat, off, len);
404-
if (memcmp(out_ref, out_nat, len) != 0) {
405-
fprintf(stderr, "extract_bytes mismatch at off=%u len=%u\n", off, len);
406-
fails++;
407-
return fails;
408-
}
386+
CHECK(memcmp(out_ref, out_nat, len) == 0);
409387
}
410388
}
411389
return fails;
@@ -427,30 +405,32 @@ static int test_permute(void)
427405
mld_keccakf1600_permute(nat_s);
428406
uint64_t nat_s_norm[MLD_KECCAK_LANES];
429407
deinterleave_state(nat_s_norm, nat_s);
430-
if (memcmp(ref_s, nat_s_norm, sizeof ref_s) != 0) {
408+
CHECK(memcmp(ref_s, nat_s_norm, sizeof ref_s) == 0);
431409
#else
432410
memcpy(nat_s, init, sizeof nat_s);
433411
mld_keccakf1600_permute(nat_s);
434-
if (memcmp(ref_s, nat_s, sizeof ref_s) != 0) {
412+
CHECK(memcmp(ref_s, nat_s, sizeof ref_s) == 0);
435413
#endif
436-
fprintf(stderr, "permute mismatch\n");
437-
fails++;
438-
}
439414
return fails;
440415
}
416+
#endif /* MLD_USE_FIPS202_X1_NATIVE */
441417

442418
int main(void)
443419
{
444420
int fails = 0;
445421

446422
randombytes_reset();
447423

424+
#if defined(MLD_USE_FIPS202_X1_NATIVE)
448425
fails += test_xor_bytes();
449426
fails += test_extract_bytes();
450427
fails += test_permute();
428+
#endif
429+
#if defined(MLD_USE_FIPS202_X4_NATIVE)
451430
fails += test_x4_xor_bytes();
452431
fails += test_x4_extract_bytes();
453432
fails += test_x4_permute();
433+
#endif
454434

455435
if (fails) {
456436
fprintf(stderr, "unit tests: FAILED (%d failure%s)\n", fails, fails==1?"":"s");

0 commit comments

Comments
 (0)