Skip to content

Commit 908de0d

Browse files
committed
Implement polyvecl_uniform_gamma1 using 4-way Keccak
This commit adds poly_uniform_gamma1_4x which implements uniform sampling in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] using 4-way batched Keccak. It then implements polyvecl_uniform_gamma1 using the new batched function. L = 4 is implemented using one call to poly_uniform_gamma1_4x. L = 5 is implemented using one call to poly_uniform_gamma1_4x and one call to poly_uniform_gamma1 L = 7 is implemented using two call to poly_uniform_gamma1_4x (with one polynomial being wasted). Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 049e603 commit 908de0d

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

mldsa/poly.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,44 @@ void poly_uniform_gamma1(poly *a, const uint8_t seed[MLDSA_CRHBYTES],
561561
polyz_unpack(a, buf);
562562
}
563563

564+
void poly_uniform_gamma1_4x(poly *r0, poly *r1, poly *r2, poly *r3,
565+
const uint8_t seed[MLDSA_CRHBYTES], uint16_t nonce0,
566+
uint16_t nonce1, uint16_t nonce2, uint16_t nonce3)
567+
{
568+
/* Temporary buffers for XOF output before rejection sampling */
569+
MLD_ALIGN uint8_t
570+
buf[4][MLD_ALIGN_UP(POLY_UNIFORM_GAMMA1_NBLOCKS * STREAM256_BLOCKBYTES)];
571+
572+
MLD_ALIGN uint8_t extseed[4][MLD_ALIGN_UP(MLDSA_CRHBYTES + 2)];
573+
574+
/* Tracks the number of coefficients we have already sampled */
575+
mld_xof256_x4_ctx state;
576+
577+
memcpy(extseed[0], seed, MLDSA_CRHBYTES);
578+
memcpy(extseed[1], seed, MLDSA_CRHBYTES);
579+
memcpy(extseed[2], seed, MLDSA_CRHBYTES);
580+
memcpy(extseed[3], seed, MLDSA_CRHBYTES);
581+
extseed[0][MLDSA_CRHBYTES] = nonce0 & 0xFF;
582+
extseed[1][MLDSA_CRHBYTES] = nonce1 & 0xFF;
583+
extseed[2][MLDSA_CRHBYTES] = nonce2 & 0xFF;
584+
extseed[3][MLDSA_CRHBYTES] = nonce3 & 0xFF;
585+
extseed[0][MLDSA_CRHBYTES + 1] = nonce0 >> 8;
586+
extseed[1][MLDSA_CRHBYTES + 1] = nonce1 >> 8;
587+
extseed[2][MLDSA_CRHBYTES + 1] = nonce2 >> 8;
588+
extseed[3][MLDSA_CRHBYTES + 1] = nonce3 >> 8;
589+
590+
mld_xof256_x4_init(&state);
591+
mld_xof256_x4_absorb(&state, extseed, MLDSA_CRHBYTES + 2);
592+
mld_xof256_x4_squeezeblocks(buf, POLY_UNIFORM_GAMMA1_NBLOCKS, &state);
593+
594+
polyz_unpack(r0, buf[0]);
595+
polyz_unpack(r1, buf[1]);
596+
polyz_unpack(r2, buf[2]);
597+
polyz_unpack(r3, buf[3]);
598+
mld_xof256_x4_release(&state);
599+
}
600+
601+
564602
void poly_challenge(poly *c, const uint8_t seed[MLDSA_CTILDEBYTES])
565603
{
566604
unsigned int i, j, pos;

mldsa/poly.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void poly_uniform_eta_4x(poly *r0, poly *r1, poly *r2, poly *r3,
346346

347347
#define poly_uniform_gamma1 MLD_NAMESPACE(poly_uniform_gamma1)
348348
/*************************************************
349-
* Name: poly_uniform_gamma1m1
349+
* Name: poly_uniform_gamma1
350350
*
351351
* Description: Sample polynomial with uniformly random coefficients
352352
* in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output
@@ -366,6 +366,24 @@ __contract__(
366366
ensures(array_bound(a->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))
367367
);
368368

369+
370+
#define poly_uniform_gamma1 MLD_NAMESPACE(poly_uniform_gamma1)
371+
/*************************************************
372+
* Name: poly_uniform_gamma1_4x
373+
*
374+
* Description: Sample polynomial with uniformly random coefficients
375+
* in [-(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1] by unpacking output
376+
* stream of SHAKE256(seed|nonce)
377+
*
378+
* Arguments: - poly *a: pointer to output polynomial
379+
* - const uint8_t seed[]: byte array with seed of length
380+
* MLDSA_CRHBYTES
381+
* - uint16_t nonce: 16-bit nonce
382+
**************************************************/
383+
void poly_uniform_gamma1_4x(poly *r0, poly *r1, poly *r2, poly *r3,
384+
const uint8_t seed[MLDSA_CRHBYTES], uint16_t nonce0,
385+
uint16_t nonce1, uint16_t nonce2, uint16_t nonce3);
386+
369387
#define poly_challenge MLD_NAMESPACE(poly_challenge)
370388
/*************************************************
371389
* Name: poly_challenge

mldsa/polyvec.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ void polyvec_matrix_pointwise_montgomery(polyveck *t,
8585
void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[MLDSA_CRHBYTES],
8686
uint16_t nonce)
8787
{
88+
#if MLDSA_L == 4
89+
poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2], &v->vec[3], seed,
90+
nonce, nonce + 1, nonce + 2, nonce + 3);
91+
#elif MLDSA_L == 5
92+
poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2], &v->vec[3], seed,
93+
nonce, nonce + 1, nonce + 2, nonce + 3);
94+
poly_uniform_gamma1(&v->vec[4], seed, nonce + 4);
95+
#elif MLDSA_L == 7
96+
poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2],
97+
&v->vec[3 /* irrelevant */], seed, nonce, nonce + 1,
98+
nonce + 2, 0xFF /* irrelevant */);
99+
poly_uniform_gamma1_4x(&v->vec[3], &v->vec[4], &v->vec[5], &v->vec[6], seed,
100+
nonce + 3, nonce + 4, nonce + 5, nonce + 6);
101+
#endif
102+
103+
88104
unsigned int i;
89105

90106
for (i = 0; i < MLDSA_L; ++i)

0 commit comments

Comments
 (0)