Skip to content

Commit dd7de62

Browse files
Apply patch from Kyber reference code which updates poly_compress and… (#1381)
Co-authored-by: Brian Jarvis <92757966+brian-jarvis-aws@users.noreply.github.com>
1 parent b3dcb63 commit dd7de62

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

crypto/kyber/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ The following changes were made to the source code in `pqcrystals_kyber_ref_comm
2323
* `kem.c`: calls to `randombytes` function is replaced with calls to `pq_custom_randombytes` and the appropriate header file is included (`crypto/rand_extra/pq_custom_randombytes.h`).
2424
* `symmetric-shake.c`: unnecessary include of `fips202.h` is removed.
2525
* `api.h`, `fips202.h`, `params.h`: modified [in this PR](https://github.com/aws/aws-lc/pull/655) to support our [prefixed symbols build](https://github.com/aws/aws-lc/blob/main/BUILDING.md#building-with-prefixed-symbols).
26-
* `poly.c` was modified to remove 2 lines of comment from [this commit](https://github.com/pq-crystals/kyber/commit/dda29cc63af721981ee2c831cf00822e69be3220)
26+
* `poly.c` and 'polyvec.c' were modified to remove 6 lines of comment from these two reference commits ([dda29cc](https://github.com/pq-crystals/kyber/commit/dda29cc63af721981ee2c831cf00822e69be3220), [272125f](https://github.com/pq-crystals/kyber/commit/272125f6acc8e8b6850fd68ceb901a660ff48196))
2727

2828
**Usage.** The KEM API is defined and documented in `include/openssl/evp.h`. To see examples of how to use any KEM, including Kyber, see `crypto/kem/README.md`.

crypto/kyber/pqcrystals_kyber_ref_common/poly.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
1919
{
2020
unsigned int i,j;
2121
int16_t u;
22+
uint32_t d0;
2223
uint8_t t[8];
2324

2425
#if (KYBER_POLYCOMPRESSEDBYTES == 128)
@@ -27,7 +28,11 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
2728
// map to positive standard representatives
2829
u = a->coeffs[8*i+j];
2930
u += (u >> 15) & KYBER_Q;
30-
t[j] = ((((uint16_t)u << 4) + KYBER_Q/2)/KYBER_Q) & 15;
31+
d0 = u << 4;
32+
d0 += 1665;
33+
d0 *= 80635;
34+
d0 >>= 28;
35+
t[j] = d0 & 0xf;
3136
}
3237

3338
r[0] = t[0] | (t[1] << 4);
@@ -42,7 +47,11 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
4247
// map to positive standard representatives
4348
u = a->coeffs[8*i+j];
4449
u += (u >> 15) & KYBER_Q;
45-
t[j] = ((((uint32_t)u << 5) + KYBER_Q/2)/KYBER_Q) & 31;
50+
d0 = u << 5;
51+
d0 += 1664;
52+
d0 *= 40318;
53+
d0 >>= 27;
54+
t[j] = d0 & 0x1f;
4655
}
4756

4857
r[0] = (t[0] >> 0) | (t[1] << 5);

crypto/kyber/pqcrystals_kyber_ref_common/polyvec.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
1616
{
1717
unsigned int i,j,k;
18+
uint64_t d0;
1819

1920
#if (KYBER_POLYVECCOMPRESSEDBYTES == (KYBER_K * 352))
2021
uint16_t t[8];
@@ -23,7 +24,12 @@ void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
2324
for(k=0;k<8;k++) {
2425
t[k] = a->vec[i].coeffs[8*j+k];
2526
t[k] += ((int16_t)t[k] >> 15) & KYBER_Q;
26-
t[k] = ((((uint32_t)t[k] << 11) + KYBER_Q/2)/KYBER_Q) & 0x7ff;
27+
d0 = t[k];
28+
d0 <<= 11;
29+
d0 += 1664;
30+
d0 *= 645084;
31+
d0 >>= 31;
32+
t[k] = d0 & 0x7ff;
2733
}
2834

2935
r[ 0] = (t[0] >> 0);
@@ -47,7 +53,12 @@ void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
4753
for(k=0;k<4;k++) {
4854
t[k] = a->vec[i].coeffs[4*j+k];
4955
t[k] += ((int16_t)t[k] >> 15) & KYBER_Q;
50-
t[k] = ((((uint32_t)t[k] << 10) + KYBER_Q/2)/ KYBER_Q) & 0x3ff;
56+
d0 = t[k];
57+
d0 <<= 10;
58+
d0 += 1665;
59+
d0 *= 1290167;
60+
d0 >>= 32;
61+
t[k] = d0 & 0x3ff;
5162
}
5263

5364
r[0] = (t[0] >> 0);

0 commit comments

Comments
 (0)