Skip to content

Commit

Permalink
Made changes suggested by cargo clippy.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvzcf committed Jun 28, 2024
1 parent 5fa313c commit 7d3cc0a
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 54 deletions.
14 changes: 6 additions & 8 deletions libcrux-ml-dsa/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub(crate) fn vector_infinity_norm_exceeds<const DIMENSION: usize>(
// 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
Expand Down Expand Up @@ -312,7 +312,7 @@ pub(crate) fn make_hint<const DIMENSION: usize, const GAMMA2: i32>(
pub(crate) fn use_hint_value<const GAMMA2: i32>(r: i32, hint: bool) -> i32 {
let (r0, r1) = decompose::<GAMMA2>(r);

if hint == false {
if !hint {
return r1;
}

Expand All @@ -324,12 +324,10 @@ pub(crate) fn use_hint_value<const GAMMA2: i32>(r: i32, hint: bool) -> i32 {
} else {
r1 + 1
}
} else if r1 == 0 {
43
} else {
if r1 == 0 {
43
} else {
r1 - 1
}
r1 - 1
}
}

Expand Down
6 changes: 3 additions & 3 deletions libcrux-ml-dsa/src/encoding/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn serialize<const OUTPUT_SIZE: usize>(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;
}
Expand All @@ -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::<RING_ELEMENT_SIZE>(vector[i]));
.copy_from_slice(&serialize::<RING_ELEMENT_SIZE>(*ring_element));
offset += RING_ELEMENT_SIZE;
}

Expand Down
10 changes: 5 additions & 5 deletions libcrux-ml-dsa/src/encoding/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn serialize_when_eta_is_2<const OUTPUT_SIZE: usize>(
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);
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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);
}

Expand Down
24 changes: 12 additions & 12 deletions libcrux-ml-dsa/src/encoding/gamma1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn serialize_when_gamma1_is_2_pow_17<const OUTPUT_SIZE: usize>(
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;
Expand Down Expand Up @@ -47,7 +47,7 @@ fn serialize_when_gamma1_is_2_pow_19<const OUTPUT_SIZE: usize>(
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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];
}

Expand Down
16 changes: 8 additions & 8 deletions libcrux-ml-dsa/src/encoding/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ 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::<BYTES_FOR_VERIFICATION_KEY_HASH>(verification_key);
signing_key_serialized[offset..offset + BYTES_FOR_VERIFICATION_KEY_HASH]
.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::<ETA, ERROR_RING_ELEMENT_SIZE>(s1[i]),
&encoding::error::serialize::<ETA, ERROR_RING_ELEMENT_SIZE>(*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::<ETA, ERROR_RING_ELEMENT_SIZE>(s2[i]),
&encoding::error::serialize::<ETA, ERROR_RING_ELEMENT_SIZE>(*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;
}

Expand Down
10 changes: 5 additions & 5 deletions libcrux-ml-dsa/src/encoding/t0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/src/encoding/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions libcrux-ml-dsa/src/encoding/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub(crate) fn generate_serialized<const ROWS_IN_A: usize, const VERIFICATION_KEY
t1: [PolynomialRingElement; ROWS_IN_A],
) -> [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
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/src/hash_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions libcrux-ml-dsa/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub(crate) fn expand_to_A<const ROWS_IN_A: usize, const COLUMNS_IN_A: usize>(
) -> [[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;
Expand Down Expand Up @@ -76,8 +77,9 @@ pub(crate) fn vector_times_ring_element<const DIMENSION: usize>(
) -> [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
Expand Down Expand Up @@ -126,7 +128,7 @@ pub(crate) fn compute_w_approx<const ROWS_IN_A: usize, const COLUMNS_IN_A: usize

for (i, row) in A_as_ntt.iter().enumerate() {
for (j, ring_element) in row.iter().enumerate() {
let product = ntt_multiply_montgomery(&ring_element, &ntt(signer_response[j]));
let product = ntt_multiply_montgomery(ring_element, &ntt(signer_response[j]));

result[i] = result[i].add(&product);
}
Expand Down
11 changes: 8 additions & 3 deletions libcrux-ml-dsa/src/ml_dsa_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<const COMMITMENT_HASH_SIZE: usize, const COLUMNS_IN_A: usize, const ROWS_IN

for i in 0..ROWS_IN_A {
for (j, hint) in self.hint[i].into_iter().enumerate() {
if hint == true {
if hint {
hint_serialized[true_hints_seen] = j as u8;
true_hints_seen += 1;
}
Expand Down Expand Up @@ -188,8 +188,12 @@ impl<const COMMITMENT_HASH_SIZE: usize, const COLUMNS_IN_A: usize, const ROWS_IN
previous_true_hints_seen = current_true_hints_seen;
}

for j in previous_true_hints_seen..MAX_ONES_IN_HINT {
if hint_serialized[j] != 0 {
for bit in hint_serialized
.iter()
.take(MAX_ONES_IN_HINT)
.skip(previous_true_hints_seen)
{
if *bit != 0 {
// ensures padding indices are zero
return Err(VerificationError::MalformedHintError);
}
Expand Down Expand Up @@ -373,6 +377,7 @@ pub(crate) fn verify<
SIGNATURE_SIZE,
>(signature_serialized)?;

// We use if-else branches because early returns will not go through hax.
if !vector_infinity_norm_exceeds::<COLUMNS_IN_A>(
signature.signer_response,
(2 << GAMMA1_EXPONENT) - BETA,
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-dsa/src/ntt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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]);
}
Expand Down
3 changes: 3 additions & 0 deletions libcrux-ml-dsa/src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ pub(crate) fn sample_error_vector<const DIMENSION: usize, const ETA: usize>(
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;
Expand Down Expand Up @@ -192,6 +194,7 @@ pub(crate) fn sample_mask_vector<const DIMENSION: usize, const GAMMA1_EXPONENT:
) -> [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;
Expand Down

0 comments on commit 7d3cc0a

Please sign in to comment.