Skip to content

Commit

Permalink
Stack optimized challenge generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dop-amin committed Apr 8, 2024
1 parent aabf059 commit a3e34fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crypto_sign/dilithium3/m4fstack/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ int crypto_sign_verify(const uint8_t *sig,
shake256_inc_absorb(&s256, mu, CRHBYTES);

/* Matrix-vector multiplication; compute Az - c2^dt1 */
poly_challenge(&p, sig);
poly_challenge_stack(&p, sig);
poly_challenge_compress(ccomp, &p);

for (size_t k_idx = 0; k_idx < K; k_idx++) {
Expand Down
46 changes: 46 additions & 0 deletions crypto_sign/dilithium3/m4fstack/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,50 @@ void pack_sk_tr(unsigned char sk[CRYPTO_SECRETKEYBYTES],
for (unsigned int i = 0; i < TRBYTES; ++i) {
sk[i] = tr[i];
}
}

/*************************************************
* Name: challenge
*
* Description: Implementation of H. Samples polynomial with TAU nonzero
* coefficients in {-1,1} using the output stream of
* SHAKE256(seed). Stack optimized.
*
* Arguments: - poly *c: pointer to output polynomial
* - const uint8_t mu[]: byte array containing seed of length SEEDBYTES
**************************************************/
#define CHALLENGE_STACK_BUF_SIZE 8
void poly_challenge_stack(poly *c, const uint8_t seed[SEEDBYTES]) {
unsigned int i, b, pos;
uint64_t signs;
uint8_t buf[CHALLENGE_STACK_BUF_SIZE];
shake256incctx state;

shake256_inc_init(&state);
shake256_inc_absorb(&state, seed, SEEDBYTES);
shake256_inc_finalize(&state);
shake256_inc_squeeze(buf, CHALLENGE_STACK_BUF_SIZE, &state);
signs = 0;
for(i = 0; i < 8; ++i)
{
signs |= (uint64_t)buf[i] << 8*i;
}
pos = 8;

for(i = 0; i < N; ++i)
c->coeffs[i] = 0;
for(i = N-TAU; i < N; ++i) {
do {
if(pos >= CHALLENGE_STACK_BUF_SIZE) {
shake256_inc_squeeze(buf, CHALLENGE_STACK_BUF_SIZE, &state);
pos = 0;
}

b = buf[pos++];
} while(b > i);

c->coeffs[i] = c->coeffs[b];
c->coeffs[b] = 1 - 2*(signs & 1);
signs >>= 1;
}
}
1 change: 1 addition & 0 deletions crypto_sign/dilithium3/m4fstack/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void unpack_sk_s2(smallpoly *a, const uint8_t *sk, size_t idx);
void poly_uniform_pointwise_montgomery_polywadd_stack(uint8_t wcomp[3*N], poly *b, const uint8_t seed[SEEDBYTES], uint16_t nonce, shake128incctx *state);
void poly_uniform_gamma1_stack(poly *a, const uint8_t seed[CRHBYTES], uint16_t nonce, shake256incctx *state);
void poly_uniform_gamma1_add_stack(poly *a, poly *b, const uint8_t seed[CRHBYTES], uint16_t nonce, shake256incctx *state);
void poly_challenge_stack(poly *c, const uint8_t seed[SEEDBYTES]);

size_t poly_make_hint_stack(poly *a, poly *t, uint8_t w[768]);
int unpack_sig_h_indices(uint8_t h_i[OMEGA], unsigned int * number_of_hints, unsigned int idx, const unsigned char sig[CRYPTO_BYTES]);
Expand Down

0 comments on commit a3e34fd

Please sign in to comment.