Skip to content

Commit

Permalink
refactor to new bsd style
Browse files Browse the repository at this point in the history
Signed-off-by: Duc Tri Nguyen <[email protected]>
  • Loading branch information
cothan committed Jun 17, 2024
1 parent a216720 commit edf8538
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
6 changes: 3 additions & 3 deletions fips202/fips202.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ static void keccak_absorb(uint64_t *s,
* - uint64_t *s: pointer to in/output Keccak state
* - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
**************************************************/
static void keccak_squeezeblocks(uint8_t *h, size_t nblocks,
uint64_t *s,
uint32_t r)
void keccak_squeezeblocks(uint8_t *h, size_t nblocks,
uint64_t *s,
uint32_t r)
{
while (nblocks > 0)
{
Expand Down
52 changes: 35 additions & 17 deletions fips202/fips202x.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ static void keccak_absorb_x4(uint64_t *s, uint32_t r,
const uint8_t *in2,
const uint8_t *in3,
size_t inlen,
uint8_t p) {
uint8_t p)
{

while (inlen >= r) {
while (inlen >= r)
{

KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, in0, 0, r);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, in1, 0, r);
Expand All @@ -33,20 +35,24 @@ static void keccak_absorb_x4(uint64_t *s, uint32_t r,
inlen -= r;
}

if (inlen > 0) {
if (inlen > 0)
{
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, in0, 0, inlen);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, in1, 0, inlen);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, in2, 0, inlen);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, in3, 0, inlen);
}

if (inlen == r - 1) {
if (inlen == r - 1)
{
p |= 128;
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, &p, inlen, 1);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, &p, inlen, 1);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, &p, inlen, 1);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 3, &p, inlen, 1);
} else {
}
else
{
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 0, &p, inlen, 1);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 1, &p, inlen, 1);
KeccakF1600_StateXORBytes(s + KECCAK_CTX * 2, &p, inlen, 1);
Expand All @@ -65,9 +71,11 @@ static void keccak_squeezeblocks_x4(uint8_t *out0,
uint8_t *out3,
size_t nblocks,
uint64_t *s,
uint32_t r) {
uint32_t r)
{

while (nblocks > 0) {
while (nblocks > 0)
{
KeccakF1600_StatePermute(s + KECCAK_CTX * 0);
KeccakF1600_StatePermute(s + KECCAK_CTX * 1);
KeccakF1600_StatePermute(s + KECCAK_CTX * 2);
Expand All @@ -86,8 +94,10 @@ static void keccak_squeezeblocks_x4(uint8_t *out0,
}
}

uint64_t *keccakx_get_lane_state(keccakx4_state *state, size_t index) {
if (index >= KECCAK_WAY) {
uint64_t *keccakx_get_lane_state(keccakx4_state *state, size_t index)
{
if (index >= KECCAK_WAY)
{
return NULL;
}

Expand All @@ -99,8 +109,10 @@ int shake128x4_absorb(keccakx4_state *state,
const uint8_t *in1,
const uint8_t *in2,
const uint8_t *in3,
size_t inlen) {
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL) {
size_t inlen)
{
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL)
{
return 1;
}

Expand All @@ -116,8 +128,10 @@ int shake256x4_absorb(keccakx4_state *state,
const uint8_t *in1,
const uint8_t *in2,
const uint8_t *in3,
size_t inlen) {
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL) {
size_t inlen)
{
if (state == NULL || in0 == NULL || in1 == NULL || in2 == NULL || in3 == NULL)
{
return 1;
}

Expand All @@ -134,8 +148,10 @@ int shake128x4_squeezeblocks(uint8_t *out0,
uint8_t *out2,
uint8_t *out3,
size_t nblocks,
keccakx4_state *state) {
if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL) {
keccakx4_state *state)
{
if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL)
{
return 1;
}
keccak_squeezeblocks_x4(out0, out1, out2, out3, nblocks, state->ctx, SHAKE128_RATE);
Expand All @@ -148,9 +164,11 @@ int shake256x4_squeezeblocks(uint8_t *out0,
uint8_t *out2,
uint8_t *out3,
size_t nblocks,
keccakx4_state *state) {
keccakx4_state *state)
{

if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL) {
if (state == NULL || out0 == NULL || out1 == NULL || out2 == NULL || out3 == NULL)
{
return 1;
}
keccak_squeezeblocks_x4(out0, out1, out2, out3, nblocks, state->ctx, SHAKE256_RATE);
Expand Down
3 changes: 2 additions & 1 deletion fips202/fips202x.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

#include <stdint.h>

typedef struct {
typedef struct
{
uint64_t ctx[25 * KECCAK_WAY];
} keccakx4_state;

Expand Down
23 changes: 16 additions & 7 deletions mlkem/indcpa.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ void gen_matrix(polyvec *a, const uint8_t seed[KYBER_SYMBYTES], int transposed)
memcpy(seedxy[j], seed, KYBER_SYMBYTES);
x = (i + j) / KYBER_K;
y = (i + j) % KYBER_K;
if (transposed) {
if (transposed)
{
seedxy[j][KYBER_SYMBYTES + 0] = x;
seedxy[j][KYBER_SYMBYTES + 1] = y;
} else {
}
else
{
seedxy[j][KYBER_SYMBYTES + 0] = y;
seedxy[j][KYBER_SYMBYTES + 1] = x;
}
Expand All @@ -198,15 +201,17 @@ void gen_matrix(polyvec *a, const uint8_t seed[KYBER_SYMBYTES], int transposed)
shake128x4_absorb(&statex, seedxy[0], seedxy[1], seedxy[2], seedxy[3], KYBER_SYMBYTES + 2);
shake128x4_squeezeblocks(bufx[0], bufx[1], bufx[2], bufx[3], GEN_MATRIX_NBLOCKS, &statex);

for (j = 0; j < KECCAK_WAY; j++) {
for (j = 0; j < KECCAK_WAY; j++)
{
x = (i + j) / KYBER_K;
y = (i + j) % KYBER_K;
vec = a[x].vec[y].coeffs;
buf = bufx[j];
buflen = GEN_MATRIX_NBLOCKS *XOF_BLOCKBYTES;
ctr = rej_uniform(vec, KYBER_N, buf, buflen);

while (ctr < KYBER_N ) {
while (ctr < KYBER_N )
{
uint64_t *ctx = keccakx_get_lane_state(&statex, j);
keccak_squeezeblocks(buf, 1, ctx, SHAKE128_RATE);
buflen = XOF_BLOCKBYTES;
Expand All @@ -216,15 +221,19 @@ void gen_matrix(polyvec *a, const uint8_t seed[KYBER_SYMBYTES], int transposed)
}

// For left over vector, we use single keccak.
for (; i < KYBER_K *KYBER_K; i++) {
for (; i < KYBER_K *KYBER_K; i++)
{
x = i / KYBER_K;
y = i % KYBER_K;
buf = bufx[0];
vec = a[x].vec[y].coeffs;

if (transposed) {
if (transposed)
{
xof_absorb(&state, seed, x, y);
} else {
}
else
{
xof_absorb(&state, seed, y, x);
}
xof_squeezeblocks(buf, GEN_MATRIX_NBLOCKS, &state);
Expand Down

0 comments on commit edf8538

Please sign in to comment.