Skip to content

Commit 339e496

Browse files
rod-chapmanmkannwischer
authored andcommitted
CBMC: Correct contract and naming of mld_poly_chknorm_native()
- Fix ensures clause to account for all three return values (-1, 0, 1) and make the stronger postcondition conditional on non-fallback. - Use mld_ct_cmask_nonzero_u32() in mld_poly_chknorm() caller. - Rename REDUCE32_RANGE_MAX to MLD_REDUCE32_RANGE_MAX. - Fix stale comments referencing ntt.h instead of poly.h. Signed-off-by: Rod Chapman <rodchap@amazon.com>
1 parent 7693f85 commit 339e496

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

mldsa/mldsa_native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@
601601
#undef MLD_NATIVE_FUNC_FALLBACK
602602
#undef MLD_NATIVE_FUNC_SUCCESS
603603
#undef MLD_NTT_BOUND
604-
#undef REDUCE32_RANGE_MAX
604+
#undef MLD_REDUCE32_RANGE_MAX
605605
/* mldsa/src/native/meta.h */
606606
#undef MLD_NATIVE_META_H
607607
#if defined(MLD_SYS_AARCH64)

mldsa/mldsa_native_asm.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@
604604
#undef MLD_NATIVE_FUNC_FALLBACK
605605
#undef MLD_NATIVE_FUNC_SUCCESS
606606
#undef MLD_NTT_BOUND
607-
#undef REDUCE32_RANGE_MAX
607+
#undef MLD_REDUCE32_RANGE_MAX
608608
/* mldsa/src/native/meta.h */
609609
#undef MLD_NATIVE_META_H
610610
#if defined(MLD_SYS_AARCH64)

mldsa/src/native/api.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939

4040
/* Bound on absolute value of coefficients after NTT.
4141
*
42-
* NOTE: This is the same bound as in ntt.h and has to be kept
42+
* NOTE: This is the same bound as in poly.h and has to be kept
4343
* in sync. */
4444
#define MLD_NTT_BOUND (9 * MLDSA_Q)
4545

4646
/* Absolute exclusive upper bound for the output of the inverse NTT
4747
*
48-
* NOTE: This is the same bound as in ntt.h and has to be kept
48+
* NOTE: This is the same bound as in poly.h and has to be kept
4949
* in sync. */
5050
#define MLD_INTT_BOUND MLDSA_Q
5151

@@ -54,7 +54,7 @@
5454
* NOTE: This is the same bound as in reduce.h and has to be kept
5555
* in sync. */
5656
/* check-magic: 6283009 == (MLD_REDUCE32_DOMAIN_MAX - 255 * MLDSA_Q + 1) */
57-
#define REDUCE32_RANGE_MAX 6283009
57+
#define MLD_REDUCE32_RANGE_MAX 6283009
5858
/*
5959
* This is the C<->native interface allowing for the drop-in of
6060
* native code for performance critical arithmetic components of ML-DSA.
@@ -429,19 +429,28 @@ __contract__(
429429
* Assumes input coefficients were reduced by mld_reduce32().
430430
*
431431
* Arguments: - const int32_t *a: pointer to polynomial
432-
* - int32_t B: norm bound
432+
* - int32_t B: norm bound, which must be in the range
433+
* 0 .. MLDSA_Q - MLD_REDUCE32_RANGE_MAX inclusive.
433434
*
434-
* Returns 0 if the infinity norm is strictly smaller than B, and 1
435-
* otherwise. B must not be larger than MLDSA_Q - MLD_REDUCE32_RANGE_MAX.
435+
* Returns MLD_NATIVE_FUNC_FALLBACK (-1) if the target CPU cannot
436+
* support a native implementation of this function.
437+
*
438+
* If the target CPU can support this function, then
439+
* Returns MLD_NATIVE_FUNC_SUCCESS (0) if the infinity norm is strictly
440+
* smaller than B
441+
* Returns 1 otherwise
436442
**************************************************/
437443
MLD_MUST_CHECK_RETURN_VALUE
438444
static MLD_INLINE int mld_poly_chknorm_native(const int32_t *a, int32_t B)
439445
__contract__(
440446
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
441-
requires(0 <= B && B <= MLDSA_Q - REDUCE32_RANGE_MAX)
442-
requires(array_bound(a, 0, MLDSA_N, -REDUCE32_RANGE_MAX, REDUCE32_RANGE_MAX))
443-
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
444-
ensures((return_value == 0) == array_abs_bound(a, 0, MLDSA_N, B))
447+
requires(0 <= B && B <= MLDSA_Q - MLD_REDUCE32_RANGE_MAX)
448+
requires(array_bound(a, 0, MLDSA_N, -MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX))
449+
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == 0 ||
450+
return_value == 1)
451+
ensures((return_value != MLD_NATIVE_FUNC_FALLBACK) ==>
452+
((return_value == 0) == array_abs_bound(a, 0, MLDSA_N, B)))
453+
445454
);
446455
#endif /* MLD_USE_NATIVE_POLY_CHKNORM */
447456

mldsa/src/poly.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ uint32_t mld_poly_chknorm(const mld_poly *a, int32_t B)
956956
if (success)
957957
{
958958
/* Convert 0 / 1 to 0 / 0xFFFFFFFF here */
959-
return 0U - (uint32_t)ret;
959+
return mld_ct_cmask_nonzero_u32((uint32_t)ret);
960960
}
961961
#endif /* MLD_USE_NATIVE_POLY_CHKNORM */
962962
return mld_poly_chknorm_c(a, B);

proofs/cbmc/poly_chknorm_native/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PROJECT_SOURCES += $(SRCDIR)/mldsa/src/poly.c
2222
CHECK_FUNCTION_CONTRACTS=mld_poly_chknorm
2323
USE_FUNCTION_CONTRACTS=mld_poly_chknorm_native
2424
USE_FUNCTION_CONTRACTS+=mld_poly_chknorm_c
25+
USE_FUNCTION_CONTRACTS+=mld_ct_cmask_nonzero_u32
2526
APPLY_LOOP_CONTRACTS=on
2627
USE_DYNAMIC_FRAMES=1
2728

test/src/test_unit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,9 @@ static int test_native_poly_chknorm(void)
499499

500500
for (i = 0; i < NUM_RANDOM_TESTS; i++)
501501
{
502-
generate_i32_array_ranged(test_poly.coeffs, MLDSA_N, -REDUCE32_RANGE_MAX,
503-
REDUCE32_RANGE_MAX);
504-
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_Q - REDUCE32_RANGE_MAX,
502+
generate_i32_array_ranged(test_poly.coeffs, MLDSA_N,
503+
-MLD_REDUCE32_RANGE_MAX, MLD_REDUCE32_RANGE_MAX);
504+
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_Q - MLD_REDUCE32_RANGE_MAX,
505505
"poly_chknorm_MAX_B") == 0);
506506
CHECK(test_poly_chknorm_core(&test_poly, MLDSA_GAMMA1 - MLDSA_BETA,
507507
"poly_chknorm_gamma1_minus_beta") == 0);

0 commit comments

Comments
 (0)