From 8b8f27b00380e82e7001aa5f12c6e73e746b517d Mon Sep 17 00:00:00 2001 From: Peter Schwabe Date: Thu, 14 Feb 2019 16:14:47 +0100 Subject: [PATCH] Make various casts in kyber explicit to make MS compiler happy with /W4 --- crypto_kem/kyber768/clean/cbd.c | 28 ++++++++++++------------- crypto_kem/kyber768/clean/indcpa.c | 8 ++++---- crypto_kem/kyber768/clean/poly.c | 32 ++++++++++++++--------------- crypto_kem/kyber768/clean/polyvec.c | 22 ++++++++++---------- crypto_kem/kyber768/clean/reduce.c | 4 ++-- crypto_kem/kyber768/clean/verify.c | 4 ++-- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/crypto_kem/kyber768/clean/cbd.c b/crypto_kem/kyber768/clean/cbd.c index b54ee1ce..55d6b6c0 100644 --- a/crypto_kem/kyber768/clean/cbd.c +++ b/crypto_kem/kyber768/clean/cbd.c @@ -37,7 +37,7 @@ void PQCLEAN_KYBER768_cbd(poly *r, const unsigned char *buf) { int i, j; for (i = 0; i < KYBER_N / 4; i++) { - t = load_littleendian(buf + 3 * i, 3); + t = (uint32_t)load_littleendian(buf + 3 * i, 3); d = 0; for (j = 0; j < 3; j++) { d += (t >> j) & 0x249249; @@ -52,17 +52,17 @@ void PQCLEAN_KYBER768_cbd(poly *r, const unsigned char *buf) { a[3] = (d >> 18) & 0x7; b[3] = (d >> 21); - r->coeffs[4 * i + 0] = a[0] + KYBER_Q - b[0]; - r->coeffs[4 * i + 1] = a[1] + KYBER_Q - b[1]; - r->coeffs[4 * i + 2] = a[2] + KYBER_Q - b[2]; - r->coeffs[4 * i + 3] = a[3] + KYBER_Q - b[3]; + r->coeffs[4 * i + 0] = (uint16_t)a[0] + KYBER_Q - b[0]; + r->coeffs[4 * i + 1] = (uint16_t)a[1] + KYBER_Q - b[1]; + r->coeffs[4 * i + 2] = (uint16_t)a[2] + KYBER_Q - b[2]; + r->coeffs[4 * i + 3] = (uint16_t)a[3] + KYBER_Q - b[3]; } #elif KYBER_ETA == 4 uint32_t t, d, a[4], b[4]; int i, j; for (i = 0; i < KYBER_N / 4; i++) { - t = load_littleendian(buf + 4 * i, 4); + t = (uint32_t)load_littleendian(buf + 4 * i, 4); d = 0; for (j = 0; j < 4; j++) { d += (t >> j) & 0x11111111; @@ -77,10 +77,10 @@ void PQCLEAN_KYBER768_cbd(poly *r, const unsigned char *buf) { a[3] = (d >> 24) & 0xf; b[3] = (d >> 28); - r->coeffs[4 * i + 0] = a[0] + KYBER_Q - b[0]; - r->coeffs[4 * i + 1] = a[1] + KYBER_Q - b[1]; - r->coeffs[4 * i + 2] = a[2] + KYBER_Q - b[2]; - r->coeffs[4 * i + 3] = a[3] + KYBER_Q - b[3]; + r->coeffs[4 * i + 0] = (uint16_t)a[0] + KYBER_Q - b[0]; + r->coeffs[4 * i + 1] = (uint16_t)a[1] + KYBER_Q - b[1]; + r->coeffs[4 * i + 2] = (uint16_t)a[2] + KYBER_Q - b[2]; + r->coeffs[4 * i + 3] = (uint16_t)a[3] + KYBER_Q - b[3]; } #elif KYBER_ETA == 5 uint64_t t, d, a[4], b[4]; @@ -102,10 +102,10 @@ void PQCLEAN_KYBER768_cbd(poly *r, const unsigned char *buf) { a[3] = (d >> 30) & 0x1f; b[3] = (d >> 35); - r->coeffs[4 * i + 0] = a[0] + KYBER_Q - b[0]; - r->coeffs[4 * i + 1] = a[1] + KYBER_Q - b[1]; - r->coeffs[4 * i + 2] = a[2] + KYBER_Q - b[2]; - r->coeffs[4 * i + 3] = a[3] + KYBER_Q - b[3]; + r->coeffs[4 * i + 0] = (uint16_t)a[0] + KYBER_Q - b[0]; + r->coeffs[4 * i + 1] = (uint16_t)a[1] + KYBER_Q - b[1]; + r->coeffs[4 * i + 2] = (uint16_t)a[2] + KYBER_Q - b[2]; + r->coeffs[4 * i + 3] = (uint16_t)a[3] + KYBER_Q - b[3]; } #else #error "poly_getnoise in poly.c only supports eta in {3,4,5}" diff --git a/crypto_kem/kyber768/clean/indcpa.c b/crypto_kem/kyber768/clean/indcpa.c index a8d531e4..046fef65 100644 --- a/crypto_kem/kyber768/clean/indcpa.c +++ b/crypto_kem/kyber768/clean/indcpa.c @@ -143,11 +143,11 @@ static void gen_matrix(polyvec *a, const unsigned char *seed, int transposed) { ctr = pos = 0; nblocks = maxnblocks; if (transposed) { - extseed[KYBER_SYMBYTES] = i; - extseed[KYBER_SYMBYTES + 1] = j; + extseed[KYBER_SYMBYTES] = (unsigned char)i; + extseed[KYBER_SYMBYTES + 1] = (unsigned char)j; } else { - extseed[KYBER_SYMBYTES] = j; - extseed[KYBER_SYMBYTES + 1] = i; + extseed[KYBER_SYMBYTES] = (unsigned char)j; + extseed[KYBER_SYMBYTES + 1] = (unsigned char)i; } shake128_absorb(state, extseed, KYBER_SYMBYTES + 2); diff --git a/crypto_kem/kyber768/clean/poly.c b/crypto_kem/kyber768/clean/poly.c index f020db96..40ef8ec3 100644 --- a/crypto_kem/kyber768/clean/poly.c +++ b/crypto_kem/kyber768/clean/poly.c @@ -23,9 +23,9 @@ void PQCLEAN_KYBER768_poly_compress(unsigned char *r, const poly *a) { t[j] = (((PQCLEAN_KYBER768_freeze(a->coeffs[i + j]) << 3) + KYBER_Q / 2) / KYBER_Q) & 7; } - r[k] = t[0] | (t[1] << 3) | (t[2] << 6); - r[k + 1] = (t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7); - r[k + 2] = (t[5] >> 1) | (t[6] << 2) | (t[7] << 5); + r[k] = (unsigned char)( t[0] | (t[1] << 3) | (t[2] << 6)); + r[k + 1] = (unsigned char)((t[2] >> 2) | (t[3] << 1) | (t[4] << 4) | (t[5] << 7)); + r[k + 2] = (unsigned char)((t[5] >> 1) | (t[6] << 2) | (t[7] << 5)); k += 3; } } @@ -71,19 +71,19 @@ void PQCLEAN_KYBER768_poly_tobytes(unsigned char *r, const poly *a) { t[j] = PQCLEAN_KYBER768_freeze(a->coeffs[8 * i + j]); } - r[13 * i + 0] = t[0] & 0xff; - r[13 * i + 1] = (t[0] >> 8) | ((t[1] & 0x07) << 5); - r[13 * i + 2] = (t[1] >> 3) & 0xff; - r[13 * i + 3] = (t[1] >> 11) | ((t[2] & 0x3f) << 2); - r[13 * i + 4] = (t[2] >> 6) | ((t[3] & 0x01) << 7); - r[13 * i + 5] = (t[3] >> 1) & 0xff; - r[13 * i + 6] = (t[3] >> 9) | ((t[4] & 0x0f) << 4); - r[13 * i + 7] = (t[4] >> 4) & 0xff; - r[13 * i + 8] = (t[4] >> 12) | ((t[5] & 0x7f) << 1); - r[13 * i + 9] = (t[5] >> 7) | ((t[6] & 0x03) << 6); - r[13 * i + 10] = (t[6] >> 2) & 0xff; - r[13 * i + 11] = (t[6] >> 10) | ((t[7] & 0x1f) << 3); - r[13 * i + 12] = (t[7] >> 5); + r[13 * i + 0] = (unsigned char)( t[0] & 0xff); + r[13 * i + 1] = (unsigned char)((t[0] >> 8) | ((t[1] & 0x07) << 5)); + r[13 * i + 2] = (unsigned char)((t[1] >> 3) & 0xff); + r[13 * i + 3] = (unsigned char)((t[1] >> 11) | ((t[2] & 0x3f) << 2)); + r[13 * i + 4] = (unsigned char)((t[2] >> 6) | ((t[3] & 0x01) << 7)); + r[13 * i + 5] = (unsigned char)((t[3] >> 1) & 0xff); + r[13 * i + 6] = (unsigned char)((t[3] >> 9) | ((t[4] & 0x0f) << 4)); + r[13 * i + 7] = (unsigned char)((t[4] >> 4) & 0xff); + r[13 * i + 8] = (unsigned char)((t[4] >> 12) | ((t[5] & 0x7f) << 1)); + r[13 * i + 9] = (unsigned char)((t[5] >> 7) | ((t[6] & 0x03) << 6)); + r[13 * i + 10] = (unsigned char)((t[6] >> 2) & 0xff); + r[13 * i + 11] = (unsigned char)((t[6] >> 10) | ((t[7] & 0x1f) << 3)); + r[13 * i + 12] = (unsigned char)((t[7] >> 5)); } } diff --git a/crypto_kem/kyber768/clean/polyvec.c b/crypto_kem/kyber768/clean/polyvec.c index f90af808..9dc92379 100644 --- a/crypto_kem/kyber768/clean/polyvec.c +++ b/crypto_kem/kyber768/clean/polyvec.c @@ -21,17 +21,17 @@ void PQCLEAN_KYBER768_polyvec_compress(unsigned char *r, const polyvec *a) { t[k] = ((((uint32_t)PQCLEAN_KYBER768_freeze(a->vec[i].coeffs[8 * j + k]) << 11) + KYBER_Q / 2) / KYBER_Q) & 0x7ff; } - r[11 * j + 0] = t[0] & 0xff; - r[11 * j + 1] = (t[0] >> 8) | ((t[1] & 0x1f) << 3); - r[11 * j + 2] = (t[1] >> 5) | ((t[2] & 0x03) << 6); - r[11 * j + 3] = (t[2] >> 2) & 0xff; - r[11 * j + 4] = (t[2] >> 10) | ((t[3] & 0x7f) << 1); - r[11 * j + 5] = (t[3] >> 7) | ((t[4] & 0x0f) << 4); - r[11 * j + 6] = (t[4] >> 4) | ((t[5] & 0x01) << 7); - r[11 * j + 7] = (t[5] >> 1) & 0xff; - r[11 * j + 8] = (t[5] >> 9) | ((t[6] & 0x3f) << 2); - r[11 * j + 9] = (t[6] >> 6) | ((t[7] & 0x07) << 5); - r[11 * j + 10] = (t[7] >> 3); + r[11 * j + 0] = (unsigned char)( t[0] & 0xff); + r[11 * j + 1] = (unsigned char)((t[0] >> 8) | ((t[1] & 0x1f) << 3)); + r[11 * j + 2] = (unsigned char)((t[1] >> 5) | ((t[2] & 0x03) << 6)); + r[11 * j + 3] = (unsigned char)((t[2] >> 2) & 0xff); + r[11 * j + 4] = (unsigned char)((t[2] >> 10) | ((t[3] & 0x7f) << 1)); + r[11 * j + 5] = (unsigned char)((t[3] >> 7) | ((t[4] & 0x0f) << 4)); + r[11 * j + 6] = (unsigned char)((t[4] >> 4) | ((t[5] & 0x01) << 7)); + r[11 * j + 7] = (unsigned char)((t[5] >> 1) & 0xff); + r[11 * j + 8] = (unsigned char)((t[5] >> 9) | ((t[6] & 0x3f) << 2)); + r[11 * j + 9] = (unsigned char)((t[6] >> 6) | ((t[7] & 0x07) << 5)); + r[11 * j + 10] = (unsigned char)((t[7] >> 3)); } r += 352; } diff --git a/crypto_kem/kyber768/clean/reduce.c b/crypto_kem/kyber768/clean/reduce.c index 8c030d7d..8e27e06a 100644 --- a/crypto_kem/kyber768/clean/reduce.c +++ b/crypto_kem/kyber768/clean/reduce.c @@ -24,7 +24,7 @@ uint16_t PQCLEAN_KYBER768_montgomery_reduce(uint32_t a) { u &= ((1 << rlog) - 1); u *= KYBER_Q; a = a + u; - return a >> rlog; + return (uint16_t)(a >> rlog); } /************************************************* @@ -38,7 +38,7 @@ uint16_t PQCLEAN_KYBER768_montgomery_reduce(uint32_t a) { * Returns: unsigned integer in {0,...,11768} congruent to a modulo q. **************************************************/ uint16_t PQCLEAN_KYBER768_barrett_reduce(uint16_t a) { - uint32_t u; + uint16_t u; u = a >> 13; //((uint32_t) a * sinv) >> 16; u *= KYBER_Q; diff --git a/crypto_kem/kyber768/clean/verify.c b/crypto_kem/kyber768/clean/verify.c index a3e14d53..b1cd0b03 100644 --- a/crypto_kem/kyber768/clean/verify.c +++ b/crypto_kem/kyber768/clean/verify.c @@ -21,8 +21,8 @@ int PQCLEAN_KYBER768_verify(const unsigned char *a, const unsigned char *b, size r |= a[i] ^ b[i]; } - r = (-r) >> 63; - return r; + r = (-(int64_t)r) >> 63; + return (int)r; } /*************************************************