Skip to content

Commit b5f9627

Browse files
authored
Merge pull request #479 from pq-code-package/remove_todo
Resolve or remove various small TODOs
2 parents 425bc20 + 5cdbeea commit b5f9627

File tree

11 files changed

+53
-50
lines changed

11 files changed

+53
-50
lines changed

cbmc/proofs/poly_compress_du/Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROJECT_SOURCES += $(SRCDIR)/mlkem/poly.c
2020

2121
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_compress_du
22+
USE_FUNCTION_CONTRACTS =
23+
24+
# TODO: We should be calling scalar_decompress_xxx by contract here,
25+
# but it does not seem to work yet because they are marked as static inline.
2226
# For K = 2 or 3, the code calls scalar_compress_d10, so
23-
ifeq ($(MLKEM_K),4)
24-
USE_FUNCTION_CONTRACTS = scalar_compress_d11
25-
else
26-
USE_FUNCTION_CONTRACTS = scalar_compress_d10
27-
endif
27+
# ifeq ($(MLKEM_K),4)
28+
# USE_FUNCTION_CONTRACTS = scalar_compress_d11
29+
# else
30+
# USE_FUNCTION_CONTRACTS = scalar_compress_d10
31+
# endif
2832

2933
APPLY_LOOP_CONTRACTS=on
3034
USE_DYNAMIC_FRAMES=1

cbmc/proofs/poly_compress_dv/Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROJECT_SOURCES += $(SRCDIR)/mlkem/poly.c
2020

2121
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_compress_dv
22-
ifeq ($(MLKEM_K),4)
23-
USE_FUNCTION_CONTRACTS = scalar_compress_d5
24-
else
25-
USE_FUNCTION_CONTRACTS = scalar_compress_d4
26-
endif
22+
23+
USE_FUNCTION_CONTRACTS =
24+
# TODO: We should be calling scalar_decompress_xxx by contract here,
25+
# but it does not seem to work yet because they are marked as static inline.
26+
# ifeq ($(MLKEM_K),4)
27+
# USE_FUNCTION_CONTRACTS = scalar_compress_d5
28+
# else
29+
# USE_FUNCTION_CONTRACTS = scalar_compress_d4
30+
# endif
2731
APPLY_LOOP_CONTRACTS=on
2832
USE_DYNAMIC_FRAMES=1
2933

cbmc/proofs/poly_decompress_du/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ PROJECT_SOURCES += $(SRCDIR)/mlkem/poly.c
2020

2121
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_decompress_du
2222

23+
USE_FUNCTION_CONTRACTS =
24+
# TODO: We should be calling scalar_decompress_xxx by contract here,
25+
# but it does not seem to work yet because they are marked as static inline.
2326
# For K = 2 or 3, the code calls scalar_decompress_d10, so
24-
ifeq ($(MLKEM_K),4)
25-
USE_FUNCTION_CONTRACTS = scalar_decompress_d11
26-
else
27-
USE_FUNCTION_CONTRACTS = scalar_decompress_d10
28-
endif
27+
# ifeq ($(MLKEM_K),4)
28+
# USE_FUNCTION_CONTRACTS = scalar_decompress_d11
29+
# else
30+
# USE_FUNCTION_CONTRACTS = scalar_decompress_d10
31+
# endif
2932

3033
APPLY_LOOP_CONTRACTS=on
3134
USE_DYNAMIC_FRAMES=1

cbmc/proofs/poly_decompress_dv/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ PROJECT_SOURCES += $(SRCDIR)/mlkem/poly.c
2020

2121
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_decompress_dv
2222

23+
USE_FUNCTION_CONTRACTS =
24+
# TODO: We should be calling scalar_decompress_xxx by contract here,
25+
# but it does not seem to work yet because they are marked as static inline.
2326
# For K = 2 or 3, the code calls scalar_decompress_d4, so
24-
ifeq ($(MLKEM_K),4)
25-
USE_FUNCTION_CONTRACTS = scalar_decompress_d5
26-
else
27-
USE_FUNCTION_CONTRACTS = scalar_decompress_d4
28-
endif
27+
# ifeq ($(MLKEM_K),4)
28+
# USE_FUNCTION_CONTRACTS = scalar_decompress_d5
29+
# else
30+
# USE_FUNCTION_CONTRACTS = scalar_decompress_d4
31+
# endif
2932

3033
APPLY_LOOP_CONTRACTS=on
3134
USE_DYNAMIC_FRAMES=1

mlkem/indcpa.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ static void unpack_pk(polyvec *pk, uint8_t seed[MLKEM_SYMBYTES],
6161
memcpy(seed, packedpk + MLKEM_POLYVECBYTES, MLKEM_SYMBYTES);
6262

6363
/*
64-
* TODO! pk must be subject to a "modulus check" at the top-level
65-
* crypto_kem_enc_derand(). Once that's done, the reduction is no
66-
* longer necessary here.
64+
* TODO! We know from the modulus check that this will result in an
65+
* unsigned canonical polynomial, but CBMC does not know it. We should
66+
* weaken the specification of `unpack_pk()` and all depending functions
67+
* to work with the weaker 4096-bound, so that the proofs go through
68+
* without the need of this redundant call to polyvec_reduce().
6769
*/
6870
polyvec_reduce(pk);
6971
}
@@ -291,13 +293,6 @@ void gen_matrix(polyvec *a, const uint8_t seed[MLKEM_SYMBYTES], int transposed)
291293
memcpy(seedxy[j], seed, MLKEM_SYMBYTES);
292294
}
293295

294-
/*
295-
* TODO: All loops in this function should be unrolled for decent
296-
* performance.
297-
* Either add suitable pragmas, or split gen_matrix according to MLKEM_K
298-
* and unroll by hand.
299-
*/
300-
301296
for (i = 0; i < (MLKEM_K * MLKEM_K / KECCAK_WAY) * KECCAK_WAY;
302297
i += KECCAK_WAY)
303298
{

mlkem/native/arith_native.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static INLINE void poly_frombytes_native(poly *a,
240240
*
241241
* Return -1 if the native implementation does not support the input lengths.
242242
* Otherwise, returns non-negative number of sampled 16-bit integers (at most
243-
*len).
243+
* len).
244244
**************************************************/
245245
static INLINE int rej_uniform_native(int16_t *r, unsigned int len,
246246
const uint8_t *buf, unsigned int buflen);

mlkem/native/x86_64/arith_native_x86_64.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
((12 * MLKEM_N / 8 * (1 << 12) / MLKEM_Q + SHAKE128_RATE) / SHAKE128_RATE)
2121
#define REJ_UNIFORM_AVX_BUFLEN (REJ_UNIFORM_AVX_NBLOCKS * SHAKE128_RATE)
2222

23-
/* TODO: Document buffer constraints */
2423
#define rej_uniform_avx2 MLKEM_NAMESPACE(rej_uniform_avx2)
2524
unsigned int rej_uniform_avx2(int16_t *r, const uint8_t *buf);
2625

mlkem/native/x86_64/profiles/default.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ static INLINE void poly_mulcache_compute_native(poly_mulcache *x, const poly *y)
7474
{
7575
/* AVX2 backend does not use mulcache */
7676
((void)y);
77-
78-
/*
79-
* TODO! The mulcache is subject to the absolute bound < q
80-
* This needs to be dropped if the mulcache is not present.
81-
* Until that's done, memset to 0 to avoid failure.
82-
*/
83-
memset(x, 0, sizeof(poly_mulcache));
77+
((void)x);
8478
}
8579

8680
static INLINE void polyvec_basemul_acc_montgomery_cached_native(

mlkem/poly.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ void poly_basemul_montgomery_cached(poly *r, const poly *a, const poly *b,
456456
const poly_mulcache *b_cache)
457457
{
458458
int i;
459+
POLY_BOUND(b_cache, MLKEM_Q);
460+
459461
for (i = 0; i < MLKEM_N / 4; i++)
460462
__loop__(
461463
assigns(i, object_whole(r))
@@ -559,6 +561,8 @@ void poly_mulcache_compute(poly_mulcache *x, const poly *a)
559561
void poly_mulcache_compute(poly_mulcache *x, const poly *a)
560562
{
561563
poly_mulcache_compute_native(x, a);
562-
POLY_BOUND(x, MLKEM_Q);
564+
/* Omitting POLY_BOUND(x, MLKEM_Q) since native implementations may
565+
* decide not to use a mulcache. Note that the C backend implementation
566+
* of poly_basemul_montgomery_cached() does still include the check. */
563567
}
564568
#endif /* MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE */

mlkem/poly.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ __contract__(
192192
#pragma CPROVER check push
193193
#pragma CPROVER check disable "unsigned-overflow"
194194
#endif
195-
/* TODO: do the same for the other static inline functions */
196-
STATIC_INLINE_TESTABLE
197-
uint32_t scalar_compress_d10(uint16_t u)
195+
static INLINE uint32_t scalar_compress_d10(uint16_t u)
198196
__contract__(
199197
requires(u <= MLKEM_Q - 1)
200198
ensures(return_value < (1u << 10))
@@ -244,8 +242,7 @@ __contract__(
244242
#pragma CPROVER check push
245243
#pragma CPROVER check disable "unsigned-overflow"
246244
#endif
247-
STATIC_INLINE_TESTABLE
248-
uint32_t scalar_compress_d11(uint16_t u)
245+
static INLINE uint32_t scalar_compress_d11(uint16_t u)
249246
__contract__(
250247
requires(u <= MLKEM_Q - 1)
251248
ensures(return_value < (1u << 11))
@@ -270,8 +267,7 @@ __contract__(
270267
* Arguments: - u: Unsigned canonical modulus modulo 16
271268
* to be decompressed.
272269
************************************************************/
273-
STATIC_INLINE_TESTABLE
274-
uint16_t scalar_decompress_d11(uint32_t u)
270+
static INLINE uint16_t scalar_decompress_d11(uint32_t u)
275271
__contract__(
276272
requires(0 <= u && u < 2048)
277273
ensures(return_value <= (MLKEM_Q - 1))
@@ -295,8 +291,7 @@ __contract__(
295291
*
296292
* Arguments: c: signed coefficient to be converted
297293
************************************************************/
298-
STATIC_INLINE_TESTABLE
299-
uint16_t scalar_signed_to_unsigned_q(int16_t c)
294+
static INLINE uint16_t scalar_signed_to_unsigned_q(int16_t c)
300295
__contract__(
301296
requires(c >= -(MLKEM_Q - 1) && c <= (MLKEM_Q - 1))
302297
ensures(return_value >= 0 && return_value <= (MLKEM_Q - 1))

mlkem/polyvec.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ void polyvec_basemul_acc_montgomery_cached(poly *r, const polyvec *a,
128128
{
129129
POLYVEC_BOUND(a, MLKEM_Q);
130130
POLYVEC_BOUND(b, NTT_BOUND);
131-
POLYVEC_BOUND(b_cache, MLKEM_Q);
131+
/* Omitting POLYVEC_BOUND(b_cache, MLKEM_Q) since native implementations may
132+
* decide not to use a mulcache. Note that the C backend implementation
133+
* of poly_basemul_montgomery_cached() does still include the check. */
132134
polyvec_basemul_acc_montgomery_cached_native(r, a, b, b_cache);
133135
}
134136
#endif /* MLKEM_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED */

0 commit comments

Comments
 (0)