From 66c0f88f632689a35a14302c01137c4d59d5eb1b Mon Sep 17 00:00:00 2001 From: Mitchell Berry Date: Fri, 13 Oct 2023 08:06:17 +1100 Subject: [PATCH] Fix: Lints Removes unecessary borrows Adds parentheses for clarity Use subraction assignment Remove unecessary indexing --- src/kem.rs | 6 +++--- src/kex.rs | 4 ++-- src/reference/fips202.rs | 6 +++--- src/reference/indcpa.rs | 14 +++++++------- src/reference/ntt.rs | 2 +- src/reference/poly.rs | 6 ++---- src/symmetric.rs | 4 ++-- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/kem.rs b/src/kem.rs index 7173ed8..1eafbdc 100644 --- a/src/kem.rs +++ b/src/kem.rs @@ -28,7 +28,7 @@ where hash_h(&mut sk[PK_START..], pk, KYBER_PUBLICKEYBYTES); if let Some(s) = _seed { - sk[SK_START..].copy_from_slice(&s.1) + sk[SK_START..].copy_from_slice(s.1) } else { randombytes(&mut sk[SK_START..], KYBER_SYMBYTES, _rng)?; } @@ -59,7 +59,7 @@ where // Deterministic randbuf for KAT's if let Some(s) = _seed { - randbuf[..KYBER_SYMBYTES].copy_from_slice(&s); + randbuf[..KYBER_SYMBYTES].copy_from_slice(s); } else { randombytes(&mut randbuf, KYBER_SYMBYTES, _rng)?; } @@ -92,7 +92,7 @@ where /// - const [u8] sk: input private key (an already allocated array of CRYPTO_SECRETKEYBYTES bytes) /// /// On failure, ss will contain a pseudo-random value. -pub fn crypto_kem_dec(ss: &mut [u8], ct: &[u8], sk: &[u8]) -> () { +pub fn crypto_kem_dec(ss: &mut [u8], ct: &[u8], sk: &[u8]) { let mut buf = [0u8; 2 * KYBER_SYMBYTES]; let mut kr = [0u8; 2 * KYBER_SYMBYTES]; let mut cmp = [0u8; KYBER_CIPHERTEXTBYTES]; diff --git a/src/kex.rs b/src/kex.rs index 1709e27..0d9e1dc 100644 --- a/src/kex.rs +++ b/src/kex.rs @@ -358,7 +358,7 @@ where fn uake_shared_a(k: &mut [u8], recv: &[u8], tk: &[u8], sk: &[u8]) -> Result<(), KyberError> { let mut buf = [0u8; 2 * KYBER_SYMBYTES]; crypto_kem_dec(&mut buf, recv, sk); - buf[KYBER_SYMBYTES..].copy_from_slice(&tk[..]); + buf[KYBER_SYMBYTES..].copy_from_slice(tk); kdf(k, &buf, 2 * KYBER_SYMBYTES); Ok(()) } @@ -424,7 +424,7 @@ fn ake_shared_a( &recv[KYBER_CIPHERTEXTBYTES..], ska, ); - buf[2 * KYBER_SYMBYTES..].copy_from_slice(&tk[..]); + buf[2 * KYBER_SYMBYTES..].copy_from_slice(tk); kdf(k, &buf, 3 * KYBER_SYMBYTES); Ok(()) } diff --git a/src/reference/fips202.rs b/src/reference/fips202.rs index 15da459..6774814 100644 --- a/src/reference/fips202.rs +++ b/src/reference/fips202.rs @@ -416,7 +416,7 @@ pub fn sha3_512(h: &mut [u8], input: &[u8], inlen: usize) { /// - usize r: rate in bytes (e.g., 168 for SHAKE128) /// - u8 p: domain separation byte fn keccak_finalize(s: &mut [u64], pos: usize, r: usize, p: u8) { - s[pos / 8] ^= (p as u64) << 8 * (pos % 8); + s[pos / 8] ^= (p as u64) << (8 * (pos % 8)); s[r / 8 - 1] ^= 1u64 << 63; } @@ -445,9 +445,9 @@ pub fn keccak_absorb_once(s: &mut [u64], r: usize, input: &[u8], mut inlen: usiz } for i in 0..inlen { - s[i / 8] ^= (input[idx + i] as u64) << 8 * (i % 8); + s[i / 8] ^= (input[idx + i] as u64) << (8 * (i % 8)); } - s[inlen / 8] ^= (p as u64) << 8 * (inlen % 8); + s[inlen / 8] ^= (p as u64) << (8 * (inlen % 8)); s[(r - 1) / 8] ^= 1u64 << 63; } diff --git a/src/reference/indcpa.rs b/src/reference/indcpa.rs index d9a436c..0e4b5cd 100644 --- a/src/reference/indcpa.rs +++ b/src/reference/indcpa.rs @@ -10,7 +10,7 @@ use crate::{params::*, poly::*, polyvec::*, symmetric::*, CryptoRng, KyberError, /// Arguments: [u8] r: the output serialized public key /// const poly *pk: the input public-key polynomial /// const [u8] seed: the input public seed -fn pack_pk(r: &mut [u8], pk: &mut Polyvec, seed: &[u8]) { +fn pack_pk(r: &mut [u8], pk: &Polyvec, seed: &[u8]) { const END: usize = KYBER_SYMBYTES + KYBER_POLYVECBYTES; polyvec_tobytes(r, pk); r[KYBER_POLYVECBYTES..END].copy_from_slice(&seed[..KYBER_SYMBYTES]); @@ -36,7 +36,7 @@ fn unpack_pk(pk: &mut Polyvec, seed: &mut [u8], packedpk: &[u8]) { /// /// Arguments: - [u8] r: output serialized secret key /// - const Polyvec sk: input vector of polynomials (secret key) -fn pack_sk(r: &mut [u8], sk: &mut Polyvec) { +fn pack_sk(r: &mut [u8], sk: &Polyvec) { polyvec_tobytes(r, sk); } @@ -59,7 +59,7 @@ fn unpack_sk(sk: &mut Polyvec, packedsk: &[u8]) { /// Arguments: [u8] r: the output serialized ciphertext /// const poly *pk: the input vector of polynomials b /// const [u8] seed: the input polynomial v -fn pack_ciphertext(r: &mut [u8], b: &mut Polyvec, v: Poly) { +fn pack_ciphertext(r: &mut [u8], b: &Polyvec, v: Poly) { polyvec_compress(r, *b); poly_compress(&mut r[KYBER_POLYVECCOMPRESSEDBYTES..], v); } @@ -184,7 +184,7 @@ where let mut randbuf = [0u8; 2 * KYBER_SYMBYTES]; if let Some(s) = _seed { - randbuf[..KYBER_SYMBYTES].copy_from_slice(&s.0); + randbuf[..KYBER_SYMBYTES].copy_from_slice(s.0); } else { randombytes(&mut randbuf, KYBER_SYMBYTES, _rng)?; } @@ -214,8 +214,8 @@ where polyvec_add(&mut pkpv, &e); polyvec_reduce(&mut pkpv); - pack_sk(sk, &mut skpv); - pack_pk(pk, &mut pkpv, publicseed); + pack_sk(sk, &skpv); + pack_pk(pk, &pkpv, publicseed); Ok(()) } @@ -272,7 +272,7 @@ pub fn indcpa_enc(c: &mut [u8], m: &[u8], pk: &[u8], coins: &[u8]) { polyvec_reduce(&mut b); poly_reduce(&mut v); - pack_ciphertext(c, &mut b, v); + pack_ciphertext(c, &b, v); } /// Name: indcpa_dec diff --git a/src/reference/ntt.rs b/src/reference/ntt.rs index ef2657b..725205f 100644 --- a/src/reference/ntt.rs +++ b/src/reference/ntt.rs @@ -110,7 +110,7 @@ pub fn invntt(r: &mut [i16]) { while j < (start + len) { t = r[j]; r[j] = barrett_reduce(t + r[j + len]); - r[j + len] = r[j + len] - t; + r[j + len] -= t; r[j + len] = fqmul(zeta, r[j + len]); j += 1 } diff --git a/src/reference/poly.rs b/src/reference/poly.rs index 236eb3d..18791a6 100644 --- a/src/reference/poly.rs +++ b/src/reference/poly.rs @@ -79,11 +79,9 @@ pub fn poly_compress(r: &mut [u8], a: Poly) { pub fn poly_decompress(r: &mut Poly, a: &[u8]) { match KYBER_POLYCOMPRESSEDBYTES { 128 => { - let mut idx = 0usize; for i in 0..KYBER_N / 2 { - r.coeffs[2 * i + 0] = ((((a[idx] & 15) as usize * KYBER_Q) + 8) >> 4) as i16; - r.coeffs[2 * i + 1] = ((((a[idx] >> 4) as usize * KYBER_Q) + 8) >> 4) as i16; - idx += 1; + r.coeffs[2 * i + 0] = ((((a[i] & 15) as usize * KYBER_Q) + 8) >> 4) as i16; + r.coeffs[2 * i + 1] = ((((a[i] >> 4) as usize * KYBER_Q) + 8) >> 4) as i16; } } 160 => { diff --git a/src/symmetric.rs b/src/symmetric.rs index f2f7840..515e052 100644 --- a/src/symmetric.rs +++ b/src/symmetric.rs @@ -76,7 +76,7 @@ pub fn hash_g(out: &mut [u8], input: &[u8], inlen: usize) { #[cfg(not(feature = "90s"))] pub fn xof_absorb(state: &mut XofState, input: &[u8], x: u8, y: u8) { - kyber_shake128_absorb(state, &input, x, y); + kyber_shake128_absorb(state, input, x, y); } #[cfg(feature = "90s")] @@ -99,7 +99,7 @@ pub fn xof_squeezeblocks(out: &mut [u8], outblocks: usize, state: &mut XofState) #[cfg(not(feature = "90s"))] pub fn prf(out: &mut [u8], outbytes: usize, key: &[u8], nonce: u8) { - shake256_prf(out, outbytes, &key, nonce); + shake256_prf(out, outbytes, key, nonce); } #[cfg(feature = "90s")]