From 7d3cc0abece7ab5e07f1e6bb71907c57d65a5d1f Mon Sep 17 00:00:00 2001 From: xvzcf Date: Fri, 28 Jun 2024 17:01:36 -0400 Subject: [PATCH] Made changes suggested by cargo clippy. --- libcrux-ml-dsa/src/arithmetic.rs | 14 +++++------ libcrux-ml-dsa/src/encoding/commitment.rs | 6 ++--- libcrux-ml-dsa/src/encoding/error.rs | 10 ++++---- libcrux-ml-dsa/src/encoding/gamma1.rs | 24 +++++++++---------- libcrux-ml-dsa/src/encoding/signing_key.rs | 16 ++++++------- libcrux-ml-dsa/src/encoding/t0.rs | 10 ++++---- libcrux-ml-dsa/src/encoding/t1.rs | 2 +- .../src/encoding/verification_key.rs | 6 ++--- libcrux-ml-dsa/src/hash_functions.rs | 2 +- libcrux-ml-dsa/src/matrix.rs | 8 ++++--- libcrux-ml-dsa/src/ml_dsa_generic.rs | 11 ++++++--- libcrux-ml-dsa/src/ntt.rs | 4 ++-- libcrux-ml-dsa/src/sample.rs | 3 +++ 13 files changed, 62 insertions(+), 54 deletions(-) diff --git a/libcrux-ml-dsa/src/arithmetic.rs b/libcrux-ml-dsa/src/arithmetic.rs index 4fb7d278e..428b919dd 100644 --- a/libcrux-ml-dsa/src/arithmetic.rs +++ b/libcrux-ml-dsa/src/arithmetic.rs @@ -79,8 +79,8 @@ pub(crate) fn vector_infinity_norm_exceeds( // TODO: We can break out of this loop early if need be, but the most // straightforward way to do so (returning false) will not go through hax; // revisit if performance is impacted. - for i in 0..DIMENSION { - exceeds |= vector[i].infinity_norm_exceeds(value); + for ring_element in vector.iter() { + exceeds |= ring_element.infinity_norm_exceeds(value); } exceeds @@ -312,7 +312,7 @@ pub(crate) fn make_hint( pub(crate) fn use_hint_value(r: i32, hint: bool) -> i32 { let (r0, r1) = decompose::(r); - if hint == false { + if !hint { return r1; } @@ -324,12 +324,10 @@ pub(crate) fn use_hint_value(r: i32, hint: bool) -> i32 { } else { r1 + 1 } + } else if r1 == 0 { + 43 } else { - if r1 == 0 { - 43 - } else { - r1 - 1 - } + r1 - 1 } } diff --git a/libcrux-ml-dsa/src/encoding/commitment.rs b/libcrux-ml-dsa/src/encoding/commitment.rs index 6b9beb471..96145f946 100644 --- a/libcrux-ml-dsa/src/encoding/commitment.rs +++ b/libcrux-ml-dsa/src/encoding/commitment.rs @@ -27,7 +27,7 @@ fn serialize(re: PolynomialRingElement) -> [u8; OUTPUT let coefficient2 = coefficients[2] as u8; let coefficient3 = coefficients[3] as u8; - out[3 * i + 0] = (coefficient1 << 6) | coefficient0; + out[3 * i] = (coefficient1 << 6) | coefficient0; out[3 * i + 1] = (coefficient2 << 4) | coefficient1 >> 2; out[3 * i + 2] = (coefficient3 << 2) | coefficient2 >> 4; } @@ -50,9 +50,9 @@ pub(crate) fn serialize_vector< let mut serialized = [0u8; OUTPUT_SIZE]; let mut offset: usize = 0; - for i in 0..DIMENSION { + for ring_element in vector.iter() { serialized[offset..offset + RING_ELEMENT_SIZE] - .copy_from_slice(&serialize::(vector[i])); + .copy_from_slice(&serialize::(*ring_element)); offset += RING_ELEMENT_SIZE; } diff --git a/libcrux-ml-dsa/src/encoding/error.rs b/libcrux-ml-dsa/src/encoding/error.rs index 9862e73fb..cb01e2362 100644 --- a/libcrux-ml-dsa/src/encoding/error.rs +++ b/libcrux-ml-dsa/src/encoding/error.rs @@ -19,7 +19,7 @@ fn serialize_when_eta_is_2( let coefficient6 = (ETA - coefficients[6]) as u8; let coefficient7 = (ETA - coefficients[7]) as u8; - serialized[3 * i + 0] = (coefficient2 << 6) | (coefficient1 << 3) | coefficient0; + serialized[3 * i] = (coefficient2 << 6) | (coefficient1 << 3) | coefficient0; serialized[3 * i + 1] = (coefficient5 << 7) | (coefficient4 << 4) | (coefficient3 << 1) | (coefficient2 >> 2); serialized[3 * i + 2] = (coefficient7 << 5) | (coefficient6 << 2) | (coefficient5 >> 1); @@ -65,7 +65,7 @@ fn deserialize_when_eta_is_2(serialized: &[u8]) -> PolynomialRingElement { let byte1 = bytes[1] as i32; let byte2 = bytes[2] as i32; - re.coefficients[8 * i + 0] = (byte0 >> 0) & 7; + re.coefficients[8 * i] = byte0 & 7; re.coefficients[8 * i + 1] = (byte0 >> 3) & 7; re.coefficients[8 * i + 2] = ((byte0 >> 6) | (byte1 << 2)) & 7; re.coefficients[8 * i + 3] = (byte1 >> 1) & 7; @@ -74,7 +74,7 @@ fn deserialize_when_eta_is_2(serialized: &[u8]) -> PolynomialRingElement { re.coefficients[8 * i + 6] = (byte2 >> 2) & 7; re.coefficients[8 * i + 7] = (byte2 >> 5) & 7; - re.coefficients[8 * i + 0] = ETA - re.coefficients[8 * i + 0]; + re.coefficients[8 * i] = ETA - re.coefficients[8 * i]; re.coefficients[8 * i + 1] = ETA - re.coefficients[8 * i + 1]; re.coefficients[8 * i + 2] = ETA - re.coefficients[8 * i + 2]; re.coefficients[8 * i + 3] = ETA - re.coefficients[8 * i + 3]; @@ -92,8 +92,8 @@ fn deserialize_when_eta_is_4(serialized: &[u8]) -> PolynomialRingElement { let mut re = PolynomialRingElement::ZERO; const ETA: i32 = 4; - for (i, byte) in serialized.into_iter().enumerate() { - re.coefficients[2 * i + 0] = ETA - ((byte & 0xF) as i32); + for (i, byte) in serialized.iter().enumerate() { + re.coefficients[2 * i] = ETA - ((byte & 0xF) as i32); re.coefficients[2 * i + 1] = ETA - ((byte >> 4) as i32); } diff --git a/libcrux-ml-dsa/src/encoding/gamma1.rs b/libcrux-ml-dsa/src/encoding/gamma1.rs index f8ebbee64..2bb4b8299 100644 --- a/libcrux-ml-dsa/src/encoding/gamma1.rs +++ b/libcrux-ml-dsa/src/encoding/gamma1.rs @@ -13,7 +13,7 @@ fn serialize_when_gamma1_is_2_pow_17( let coefficient2 = GAMMA1 - coefficients[2]; let coefficient3 = GAMMA1 - coefficients[3]; - serialized[9 * i + 0] = coefficient0 as u8; + serialized[9 * i] = coefficient0 as u8; serialized[9 * i + 1] = (coefficient0 >> 8) as u8; serialized[9 * i + 2] = (coefficient0 >> 16) as u8; @@ -47,7 +47,7 @@ fn serialize_when_gamma1_is_2_pow_19( let coefficient0 = GAMMA1 - coefficients[0]; let coefficient1 = GAMMA1 - coefficients[1]; - serialized[5 * i + 0] = coefficient0 as u8; + serialized[5 * i] = coefficient0 as u8; serialized[5 * i + 1] = (coefficient0 >> 8) as u8; serialized[5 * i + 2] = (coefficient0 >> 16) as u8; @@ -79,10 +79,10 @@ fn deserialize_when_gamma1_is_2_pow_17(serialized: &[u8]) -> PolynomialRingEleme let mut re = PolynomialRingElement::ZERO; for (i, bytes) in serialized.chunks_exact(9).enumerate() { - re.coefficients[4 * i + 0] = bytes[0] as i32; - re.coefficients[4 * i + 0] |= (bytes[1] as i32) << 8; - re.coefficients[4 * i + 0] |= (bytes[2] as i32) << 16; - re.coefficients[4 * i + 0] &= GAMMA1_TIMES_2_BITMASK; + re.coefficients[4 * i] = bytes[0] as i32; + re.coefficients[4 * i] |= (bytes[1] as i32) << 8; + re.coefficients[4 * i] |= (bytes[2] as i32) << 16; + re.coefficients[4 * i] &= GAMMA1_TIMES_2_BITMASK; re.coefficients[4 * i + 1] = (bytes[2] as i32) >> 2; re.coefficients[4 * i + 1] |= (bytes[3] as i32) << 6; @@ -99,7 +99,7 @@ fn deserialize_when_gamma1_is_2_pow_17(serialized: &[u8]) -> PolynomialRingEleme re.coefficients[4 * i + 3] |= (bytes[8] as i32) << 10; re.coefficients[4 * i + 3] &= GAMMA1_TIMES_2_BITMASK; - re.coefficients[4 * i + 0] = GAMMA1 - re.coefficients[4 * i + 0]; + re.coefficients[4 * i] = GAMMA1 - re.coefficients[4 * i]; re.coefficients[4 * i + 1] = GAMMA1 - re.coefficients[4 * i + 1]; re.coefficients[4 * i + 2] = GAMMA1 - re.coefficients[4 * i + 2]; re.coefficients[4 * i + 3] = GAMMA1 - re.coefficients[4 * i + 3]; @@ -116,16 +116,16 @@ fn deserialize_when_gamma1_is_2_pow_19(serialized: &[u8]) -> PolynomialRingEleme let mut re = PolynomialRingElement::ZERO; for (i, bytes) in serialized.chunks_exact(5).enumerate() { - re.coefficients[2 * i + 0] = bytes[0] as i32; - re.coefficients[2 * i + 0] |= (bytes[1] as i32) << 8; - re.coefficients[2 * i + 0] |= (bytes[2] as i32) << 16; - re.coefficients[2 * i + 0] &= GAMMA1_TIMES_2_BITMASK; + re.coefficients[2 * i] = bytes[0] as i32; + re.coefficients[2 * i] |= (bytes[1] as i32) << 8; + re.coefficients[2 * i] |= (bytes[2] as i32) << 16; + re.coefficients[2 * i] &= GAMMA1_TIMES_2_BITMASK; re.coefficients[2 * i + 1] = (bytes[2] as i32) >> 4; re.coefficients[2 * i + 1] |= (bytes[3] as i32) << 4; re.coefficients[2 * i + 1] |= (bytes[4] as i32) << 12; - re.coefficients[2 * i + 0] = GAMMA1 - re.coefficients[2 * i + 0]; + re.coefficients[2 * i] = GAMMA1 - re.coefficients[2 * i]; re.coefficients[2 * i + 1] = GAMMA1 - re.coefficients[2 * i + 1]; } diff --git a/libcrux-ml-dsa/src/encoding/signing_key.rs b/libcrux-ml-dsa/src/encoding/signing_key.rs index 9245d8986..afb7bdb0a 100644 --- a/libcrux-ml-dsa/src/encoding/signing_key.rs +++ b/libcrux-ml-dsa/src/encoding/signing_key.rs @@ -27,11 +27,11 @@ pub(crate) fn generate_serialized< let mut signing_key_serialized = [0u8; SIGNING_KEY_SIZE]; let mut offset = 0; - signing_key_serialized[offset..offset + SEED_FOR_A_SIZE].copy_from_slice(&seed_for_A); + signing_key_serialized[offset..offset + SEED_FOR_A_SIZE].copy_from_slice(seed_for_A); offset += SEED_FOR_A_SIZE; signing_key_serialized[offset..offset + SEED_FOR_SIGNING_SIZE] - .copy_from_slice(&seed_for_signing); + .copy_from_slice(seed_for_signing); offset += SEED_FOR_SIGNING_SIZE; let verification_key_hash = H::one_shot::(verification_key); @@ -39,23 +39,23 @@ pub(crate) fn generate_serialized< .copy_from_slice(&verification_key_hash); offset += BYTES_FOR_VERIFICATION_KEY_HASH; - for i in 0..COLUMNS_IN_A { + for ring_element in s1.iter() { signing_key_serialized[offset..offset + ERROR_RING_ELEMENT_SIZE].copy_from_slice( - &encoding::error::serialize::(s1[i]), + &encoding::error::serialize::(*ring_element), ); offset += ERROR_RING_ELEMENT_SIZE; } - for i in 0..ROWS_IN_A { + for ring_element in s2.iter() { signing_key_serialized[offset..offset + ERROR_RING_ELEMENT_SIZE].copy_from_slice( - &encoding::error::serialize::(s2[i]), + &encoding::error::serialize::(*ring_element), ); offset += ERROR_RING_ELEMENT_SIZE; } - for i in 0..ROWS_IN_A { + for ring_element in t0.iter() { signing_key_serialized[offset..offset + RING_ELEMENT_OF_T0S_SIZE] - .copy_from_slice(&encoding::t0::serialize(t0[i])); + .copy_from_slice(&encoding::t0::serialize(*ring_element)); offset += RING_ELEMENT_OF_T0S_SIZE; } diff --git a/libcrux-ml-dsa/src/encoding/t0.rs b/libcrux-ml-dsa/src/encoding/t0.rs index 85910d314..3b6914bb9 100644 --- a/libcrux-ml-dsa/src/encoding/t0.rs +++ b/libcrux-ml-dsa/src/encoding/t0.rs @@ -29,7 +29,7 @@ pub(crate) fn serialize(re: PolynomialRingElement) -> [u8; RING_ELEMENT_OF_T0S_S let coefficient6 = change_t0_interval(coefficients[6]); let coefficient7 = change_t0_interval(coefficients[7]); - serialized[13 * i + 0] = coefficient0 as u8; + serialized[13 * i] = coefficient0 as u8; serialized[13 * i + 1] = (coefficient0 >> 8) as u8; serialized[13 * i + 1] |= (coefficient1 << 5) as u8; @@ -87,9 +87,9 @@ fn deserialize(serialized: &[u8]) -> PolynomialRingElement { let byte11 = bytes[11] as i32; let byte12 = bytes[12] as i32; - re.coefficients[8 * i + 0] = byte0; - re.coefficients[8 * i + 0] |= byte1 << 8; - re.coefficients[8 * i + 0] &= BITS_IN_LOWER_PART_OF_T_MASK; + re.coefficients[8 * i] = byte0; + re.coefficients[8 * i] |= byte1 << 8; + re.coefficients[8 * i] &= BITS_IN_LOWER_PART_OF_T_MASK; re.coefficients[8 * i + 1] = byte1 >> 5; re.coefficients[8 * i + 1] |= byte2 << 3; @@ -123,7 +123,7 @@ fn deserialize(serialized: &[u8]) -> PolynomialRingElement { re.coefficients[8 * i + 7] |= byte12 << 5; re.coefficients[8 * i + 7] &= BITS_IN_LOWER_PART_OF_T_MASK; - re.coefficients[8 * i + 0] = change_t0_interval(re.coefficients[8 * i + 0]); + re.coefficients[8 * i] = change_t0_interval(re.coefficients[8 * i]); re.coefficients[8 * i + 1] = change_t0_interval(re.coefficients[8 * i + 1]); re.coefficients[8 * i + 2] = change_t0_interval(re.coefficients[8 * i + 2]); re.coefficients[8 * i + 3] = change_t0_interval(re.coefficients[8 * i + 3]); diff --git a/libcrux-ml-dsa/src/encoding/t1.rs b/libcrux-ml-dsa/src/encoding/t1.rs index e393c6e12..8f3b448c2 100644 --- a/libcrux-ml-dsa/src/encoding/t1.rs +++ b/libcrux-ml-dsa/src/encoding/t1.rs @@ -36,7 +36,7 @@ pub(crate) fn deserialize(serialized: &[u8]) -> PolynomialRingElement { let byte3 = bytes[3] as i32; let byte4 = bytes[4] as i32; - out.coefficients[4 * i + 0] = ((byte0 >> 0) | (byte1 << 8)) & mask; + out.coefficients[4 * i] = (byte0 | (byte1 << 8)) & mask; out.coefficients[4 * i + 1] = ((byte1 >> 2) | (byte2 << 6)) & mask; out.coefficients[4 * i + 2] = ((byte2 >> 4) | (byte3 << 4)) & mask; out.coefficients[4 * i + 3] = ((byte3 >> 6) | (byte4 << 2)) & mask; diff --git a/libcrux-ml-dsa/src/encoding/verification_key.rs b/libcrux-ml-dsa/src/encoding/verification_key.rs index 0132f3c7e..7a11b8f80 100644 --- a/libcrux-ml-dsa/src/encoding/verification_key.rs +++ b/libcrux-ml-dsa/src/encoding/verification_key.rs @@ -11,12 +11,12 @@ pub(crate) fn generate_serialized [u8; VERIFICATION_KEY_SIZE] { let mut verification_key_serialized = [0u8; VERIFICATION_KEY_SIZE]; - verification_key_serialized[0..SEED_FOR_A_SIZE].copy_from_slice(&seed_for_A); + verification_key_serialized[0..SEED_FOR_A_SIZE].copy_from_slice(seed_for_A); - for i in 0..ROWS_IN_A { + for (i, ring_element) in t1.iter().enumerate() { let offset = SEED_FOR_A_SIZE + (i * RING_ELEMENT_OF_T1S_SIZE); verification_key_serialized[offset..offset + RING_ELEMENT_OF_T1S_SIZE] - .copy_from_slice(&t1::serialize(t1[i])); + .copy_from_slice(&t1::serialize(*ring_element)); } verification_key_serialized diff --git a/libcrux-ml-dsa/src/hash_functions.rs b/libcrux-ml-dsa/src/hash_functions.rs index 54b696791..4b165b8ea 100644 --- a/libcrux-ml-dsa/src/hash_functions.rs +++ b/libcrux-ml-dsa/src/hash_functions.rs @@ -15,7 +15,7 @@ pub(crate) mod H { #[inline(always)] pub(crate) fn new(seed: &[u8]) -> KeccakState1 { let mut state = incremental::shake256_init(); - incremental::shake256_absorb_final(&mut state, &seed); + incremental::shake256_absorb_final(&mut state, seed); state } diff --git a/libcrux-ml-dsa/src/matrix.rs b/libcrux-ml-dsa/src/matrix.rs index f461d55c8..24586bee2 100644 --- a/libcrux-ml-dsa/src/matrix.rs +++ b/libcrux-ml-dsa/src/matrix.rs @@ -12,6 +12,7 @@ pub(crate) fn expand_to_A( ) -> [[PolynomialRingElement; COLUMNS_IN_A]; ROWS_IN_A] { let mut A = [[PolynomialRingElement::ZERO; COLUMNS_IN_A]; ROWS_IN_A]; + #[allow(clippy::needless_range_loop)] for i in 0..ROWS_IN_A { for j in 0..COLUMNS_IN_A { seed[32] = j as u8; @@ -76,8 +77,9 @@ pub(crate) fn vector_times_ring_element( ) -> [PolynomialRingElement; DIMENSION] { let mut result = [PolynomialRingElement::ZERO; DIMENSION]; - for (i, vector_element) in vector.iter().enumerate() { - result[i] = invert_ntt_montgomery(ntt_multiply_montgomery(&vector_element, ring_element)); + for (i, vector_ring_element) in vector.iter().enumerate() { + result[i] = + invert_ntt_montgomery(ntt_multiply_montgomery(vector_ring_element, ring_element)); } result @@ -126,7 +128,7 @@ pub(crate) fn compute_w_approx(signature_serialized)?; + // We use if-else branches because early returns will not go through hax. if !vector_infinity_norm_exceeds::( signature.signer_response, (2 << GAMMA1_EXPONENT) - BETA, diff --git a/libcrux-ml-dsa/src/ntt.rs b/libcrux-ml-dsa/src/ntt.rs index 405de096f..abd20d1b6 100644 --- a/libcrux-ml-dsa/src/ntt.rs +++ b/libcrux-ml-dsa/src/ntt.rs @@ -54,7 +54,7 @@ fn ntt_at_layer( ZETAS_TIMES_MONTGOMERY_R[*zeta_i], ); re.coefficients[j + step] = re.coefficients[j] - t; - re.coefficients[j] = re.coefficients[j] + t; + re.coefficients[j] += t; } } @@ -93,7 +93,7 @@ fn invert_ntt_at_layer( for j in offset..offset + step { let a_minus_b = re.coefficients[j + step] - re.coefficients[j]; - re.coefficients[j] = re.coefficients[j] + re.coefficients[j + step]; + re.coefficients[j] += re.coefficients[j + step]; re.coefficients[j + step] = montgomery_multiply_fe_by_fer(a_minus_b, ZETAS_TIMES_MONTGOMERY_R[*zeta_i]); } diff --git a/libcrux-ml-dsa/src/sample.rs b/libcrux-ml-dsa/src/sample.rs index bb18f1df2..477b5f689 100644 --- a/libcrux-ml-dsa/src/sample.rs +++ b/libcrux-ml-dsa/src/sample.rs @@ -165,6 +165,8 @@ pub(crate) fn sample_error_vector( domain_separator: &mut u16, ) -> [PolynomialRingElement; DIMENSION] { let mut error = [PolynomialRingElement::ZERO; DIMENSION]; + + #[allow(clippy::needless_range_loop)] for i in 0..DIMENSION { seed[64] = *domain_separator as u8; seed[65] = (*domain_separator >> 8) as u8; @@ -192,6 +194,7 @@ pub(crate) fn sample_mask_vector [PolynomialRingElement; DIMENSION] { let mut error = [PolynomialRingElement::ZERO; DIMENSION]; + #[allow(clippy::needless_range_loop)] for i in 0..DIMENSION { seed[64] = *domain_separator as u8; seed[65] = (*domain_separator >> 8) as u8;