Skip to content

Commit dec77c3

Browse files
authored
Merge pull request #244 from pq-code-package/poly_uniform-consolidate
Consolidate poly_uniform and poly_uniform_4x
2 parents 7fb4de0 + df4a447 commit dec77c3

File tree

9 files changed

+43
-125
lines changed

9 files changed

+43
-125
lines changed

mldsa/poly.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,20 @@ __contract__(
323323
* - Simplified from reference by removing buffer tail handling
324324
* since buflen % 3 = 0 always holds true (STREAM128_BLOCKBYTES =
325325
* 168).
326-
* - Modified rej_uniform interface to track offset directly. */
327-
void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES], uint16_t nonce)
326+
* - Modified rej_uniform interface to track offset directly.
327+
* - Pass nonce packed in the extended seed array instead of a third
328+
* argument.
329+
* */
330+
void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2])
328331
{
329332
unsigned int ctr;
330333
unsigned int buflen = POLY_UNIFORM_NBLOCKS * STREAM128_BLOCKBYTES;
331-
uint8_t buf[POLY_UNIFORM_NBLOCKS * STREAM128_BLOCKBYTES];
332-
stream128_state state;
334+
MLD_ALIGN uint8_t buf[POLY_UNIFORM_NBLOCKS * STREAM128_BLOCKBYTES];
335+
mld_xof_ctx state;
333336

334-
stream128_init(&state, seed, nonce);
335-
stream128_squeezeblocks(buf, POLY_UNIFORM_NBLOCKS, &state);
337+
mld_xof_init(&state);
338+
mld_xof_absorb(&state, seed, MLDSA_SEEDBYTES + 2);
339+
mld_xof_squeezeblocks(buf, POLY_UNIFORM_NBLOCKS, &state);
336340

337341
ctr = rej_uniform(a->coeffs, MLDSA_N, 0, buf, buflen);
338342
buflen = STREAM128_BLOCKBYTES;
@@ -343,13 +347,13 @@ void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES], uint16_t nonce)
343347
invariant((&state)->pos <= SHAKE128_RATE)
344348
invariant(array_bound(a->coeffs, 0, ctr, 0, MLDSA_Q)))
345349
{
346-
stream128_squeezeblocks(buf, 1, &state);
350+
mld_xof_squeezeblocks(buf, 1, &state);
347351
ctr = rej_uniform(a->coeffs, MLDSA_N, ctr, buf, buflen);
348352
}
349353
}
350354

351-
void poly_rej_uniform_4x(poly *vec,
352-
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)])
355+
void poly_uniform_4x(poly *vec,
356+
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)])
353357
{
354358
/* Temporary buffers for XOF output before rejection sampling */
355359
MLD_ALIGN uint8_t
@@ -487,7 +491,7 @@ void poly_uniform_eta(poly *a, const uint8_t seed[MLDSA_CRHBYTES],
487491
{
488492
unsigned int ctr;
489493
unsigned int buflen = POLY_UNIFORM_ETA_NBLOCKS * STREAM256_BLOCKBYTES;
490-
uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS * STREAM256_BLOCKBYTES];
494+
MLD_ALIGN uint8_t buf[POLY_UNIFORM_ETA_NBLOCKS * STREAM256_BLOCKBYTES];
491495
stream256_state state;
492496

493497
stream256_init(&state, seed, nonce);
@@ -512,7 +516,7 @@ void poly_uniform_eta(poly *a, const uint8_t seed[MLDSA_CRHBYTES],
512516
void poly_uniform_gamma1(poly *a, const uint8_t seed[MLDSA_CRHBYTES],
513517
uint16_t nonce)
514518
{
515-
uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS * STREAM256_BLOCKBYTES];
519+
MLD_ALIGN uint8_t buf[POLY_UNIFORM_GAMMA1_NBLOCKS * STREAM256_BLOCKBYTES];
516520
stream256_state state;
517521

518522
stream256_init(&state, seed, nonce);
@@ -525,7 +529,7 @@ void poly_challenge(poly *c, const uint8_t seed[MLDSA_CTILDEBYTES])
525529
unsigned int i, j, pos;
526530
uint64_t signs;
527531
uint64_t offset;
528-
uint8_t buf[SHAKE256_RATE];
532+
MLD_ALIGN uint8_t buf[SHAKE256_RATE];
529533
keccak_state state;
530534

531535
shake256_init(&state);

mldsa/poly.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,18 @@ __contract__(
294294
*
295295
* Arguments: - poly *a: pointer to output polynomial
296296
* - const uint8_t seed[]: byte array with seed of length
297-
* MLDSA_SEEDBYTES
298-
* - uint16_t nonce: 2-byte nonce
297+
* MLDSA_SEEDBYTES and the packed 2-byte nonce
299298
**************************************************/
300-
void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES], uint16_t nonce)
299+
void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES + 2])
301300
__contract__(
302301
requires(memory_no_alias(a, sizeof(poly)))
303-
requires(memory_no_alias(seed, MLDSA_SEEDBYTES))
302+
requires(memory_no_alias(seed, MLDSA_SEEDBYTES + 2))
304303
assigns(memory_slice(a, sizeof(poly)))
305304
ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
306305
);
307306
#define poly_rej_uniform_4x MLD_NAMESPACE(poly_rej_uniform_4x)
308307
/*************************************************
309-
* Name: poly_rej_uniform_x4
308+
* Name: poly_uniform_x4
310309
*
311310
* Description: Generate four polynomials using rejection sampling
312311
* on (pseudo-)uniformly random bytes sampled from a seed.
@@ -318,8 +317,8 @@ __contract__(
318317
* MLDSA_SEEDBYTES + 2 each, plus padding for alignment.
319318
*
320319
**************************************************/
321-
void poly_rej_uniform_4x(poly *vec,
322-
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)]);
320+
void poly_uniform_4x(poly *vec,
321+
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)]);
323322

324323
#define poly_uniform_eta MLD_NAMESPACE(poly_uniform_eta)
325324
/*************************************************

mldsa/polyvec.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void polyvec_matrix_expand(polyvecl mat[MLDSA_K],
4848
* does not introduce any padding here. Need to refactor the data type to
4949
* polymat as in mlkem-native.
5050
*/
51-
poly_rej_uniform_4x(&mat[(i) / MLDSA_L].vec[(i) % MLDSA_L], seed_ext);
51+
poly_uniform_4x(&mat[(i) / MLDSA_L].vec[(i) % MLDSA_L], seed_ext);
5252
}
5353

5454
/* For MLDSA_K=6, MLDSA_L=5, process the last two entries individually */
@@ -58,7 +58,10 @@ void polyvec_matrix_expand(polyvecl mat[MLDSA_K],
5858
x = i / MLDSA_L;
5959
y = i % MLDSA_L;
6060

61-
poly_uniform(&mat[(i) / MLDSA_L].vec[(i) % MLDSA_L], rho, (x << 8) + y);
61+
seed_ext[0][MLDSA_SEEDBYTES + 0] = y;
62+
seed_ext[0][MLDSA_SEEDBYTES + 1] = x;
63+
64+
poly_uniform(&mat[i / MLDSA_L].vec[i % MLDSA_L], seed_ext[0]);
6265

6366
i++;
6467
}

mldsa/symmetric-shake.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@
77
#include "params.h"
88
#include "symmetric.h"
99

10-
void mldsa_shake128_stream_init(keccak_state *state,
11-
const uint8_t seed[MLDSA_SEEDBYTES],
12-
uint16_t nonce)
13-
{
14-
uint8_t t[2];
15-
16-
t[0] = nonce & 0xFF;
17-
t[1] = nonce >> 8;
18-
19-
shake128_init(state);
20-
shake128_absorb(state, seed, MLDSA_SEEDBYTES);
21-
shake128_absorb(state, t, 2);
22-
shake128_finalize(state);
23-
}
2410

2511
void mldsa_shake256_stream_init(keccak_state *state,
2612
const uint8_t seed[MLDSA_CRHBYTES],

mldsa/symmetric.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,8 @@
1111

1212
#include "fips202/fips202.h"
1313

14-
typedef keccak_state stream128_state;
1514
typedef keccak_state stream256_state;
1615

17-
#define mldsa_shake128_stream_init MLD_NAMESPACE(mldsa_shake128_stream_init)
18-
void mldsa_shake128_stream_init(keccak_state *state,
19-
const uint8_t seed[MLDSA_SEEDBYTES],
20-
uint16_t nonce)
21-
__contract__(
22-
requires(memory_no_alias(state, sizeof(keccak_state)))
23-
requires(memory_no_alias(seed, MLDSA_SEEDBYTES))
24-
assigns(memory_slice(state, sizeof(keccak_state)))
25-
ensures(state->pos <= SHAKE128_RATE)
26-
);
27-
2816
#define mldsa_shake256_stream_init MLD_NAMESPACE(mldsa_shake256_stream_init)
2917
void mldsa_shake256_stream_init(keccak_state *state,
3018
const uint8_t seed[MLDSA_CRHBYTES],
@@ -39,15 +27,24 @@ __contract__(
3927
#define STREAM128_BLOCKBYTES SHAKE128_RATE
4028
#define STREAM256_BLOCKBYTES SHAKE256_RATE
4129

42-
#define stream128_init(STATE, SEED, NONCE) \
43-
mldsa_shake128_stream_init(STATE, SEED, NONCE)
44-
#define stream128_squeezeblocks(OUT, OUTBLOCKS, STATE) \
45-
shake128_squeezeblocks(OUT, OUTBLOCKS, STATE)
4630
#define stream256_init(STATE, SEED, NONCE) \
4731
mldsa_shake256_stream_init(STATE, SEED, NONCE)
4832
#define stream256_squeezeblocks(OUT, OUTBLOCKS, STATE) \
4933
shake256_squeezeblocks(OUT, OUTBLOCKS, STATE)
5034

35+
#define mld_xof_ctx keccak_state
36+
#define mld_xof_init(CTX) shake128_init(CTX)
37+
#define mld_xof_absorb(CTX, IN, INBYTES) \
38+
do \
39+
{ \
40+
shake128_absorb(CTX, IN, INBYTES); \
41+
shake128_finalize(CTX); \
42+
} while (0)
43+
44+
45+
#define mld_xof_squeezeblocks(OUT, OUTBLOCKS, STATE) \
46+
shake128_squeezeblocks(OUT, OUTBLOCKS, STATE)
47+
5148
#define mld_xof_x4_ctx mld_shake128x4ctx
5249
#define mld_xof_x4_init(CTX) mld_shake128x4_init((CTX))
5350
#define mld_xof_x4_absorb(CTX, IN, INBYTES) \

proofs/cbmc/mldsa_shake128_stream_init/Makefile

Lines changed: 0 additions & 56 deletions
This file was deleted.

proofs/cbmc/mldsa_shake128_stream_init/mldsa_shake128_stream_init_harness.c

Lines changed: 0 additions & 14 deletions
This file was deleted.

proofs/cbmc/poly_uniform/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ REMOVE_FUNCTION_BODY +=
1717
UNWINDSET +=
1818

1919
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20-
PROJECT_SOURCES += $(SRCDIR)/mldsa/poly.c $(SRCDIR)/mldsa/symmetric-shake.c $(SRCDIR)/mldsa/fips202/fips202.c
20+
PROJECT_SOURCES += $(SRCDIR)/mldsa/poly.c
2121

2222
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform
23-
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)mldsa_shake128_stream_init $(FIPS202_NAMESPACE)shake128_squeezeblocks rej_uniform
23+
USE_FUNCTION_CONTRACTS=$(FIPS202_NAMESPACE)shake128_init $(FIPS202_NAMESPACE)shake128_absorb $(FIPS202_NAMESPACE)shake128_finalize $(FIPS202_NAMESPACE)shake128_squeezeblocks rej_uniform
2424
APPLY_LOOP_CONTRACTS=on
2525
USE_DYNAMIC_FRAMES=1
2626

proofs/cbmc/poly_uniform/poly_uniform_harness.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ void harness(void)
88
{
99
poly *a;
1010
const uint8_t *seed;
11-
uint16_t nonce;
1211

13-
poly_uniform(a, seed, nonce);
12+
poly_uniform(a, seed);
1413
}

0 commit comments

Comments
 (0)