fix: clippy warnings

This commit is contained in:
2025-12-18 18:42:50 +02:00
parent 9869036bdf
commit 0687fe0431
5 changed files with 43 additions and 39 deletions

View File

@@ -6,33 +6,31 @@ pub trait SboxLookup: Sized {
}
macro_rules! impl_sbox_lookup {
($ty:ty, $bytes:expr) => {
impl SboxLookup for $ty {
fn sbox_lookup(self) -> Self {
(0..$bytes).fold(0, |acc, idx| {
let shift = ($bytes - 1 - idx) * 8;
let byte = ((self >> shift) & 0xFF) as u8;
let row = (byte >> 4) as usize;
let col = (byte & 0xF) as usize;
acc | Self::from(S_BOXES[row][col]) << shift
})
}
($($ty:ty),*) => {
$(
impl SboxLookup for $ty {
fn sbox_lookup(self) -> Self {
let mut bytes = self.to_le_bytes();
for b in bytes.iter_mut() {
let row = (*b >> 4) as usize;
let col = (*b & 0x0F) as usize;
*b = S_BOXES[row][col];
}
Self::from_le_bytes(bytes)
}
fn inv_sbox_lookup(self) -> Self {
(0..$bytes).fold(0, |acc, idx| {
let shift = ($bytes - 1 - idx) * 8;
let byte = ((self >> shift) & 0xFF) as u8;
let row = (byte >> 4) as usize;
let col = (byte & 0xF) as usize;
acc | Self::from(INV_S_BOXES[row][col]) << shift
})
fn inv_sbox_lookup(self) -> Self {
let mut bytes = self.to_le_bytes();
for b in bytes.iter_mut() {
let row = (*b >> 4) as usize;
let col = (*b & 0x0F) as usize;
*b = INV_S_BOXES[row][col];
}
Self::from_le_bytes(bytes)
}
}
}
)*
};
}
impl_sbox_lookup!(u8, 1);
impl_sbox_lookup!(u16, 2);
impl_sbox_lookup!(u32, 4);
impl_sbox_lookup!(u64, 8);
impl_sbox_lookup!(u128, 16);
impl_sbox_lookup!(u8, u16, u32, u64, u128);

View File

@@ -519,7 +519,8 @@ fn encrypt_decrypt_roundtrip(
let ciphertext = aes
.encrypt(&plaintext.to_be_bytes())
.expect("Encryption failed");
let ciphertext_u128 = u128::from_be_bytes(ciphertext.as_slice().try_into().unwrap());
let ciphertext_u128 =
u128::from_be_bytes(ciphertext.as_slice().try_into().expect("ciphertext"));
assert_eq!(
ciphertext_u128, expected_ciphertext,
@@ -528,7 +529,12 @@ fn encrypt_decrypt_roundtrip(
// Decrypt
let decrypted = aes.decrypt(&ciphertext).expect("Decryption failed");
let decrypted_u128 = u128::from_be_bytes(decrypted.as_slice().try_into().unwrap());
let decrypted_u128 = u128::from_be_bytes(
decrypted
.as_slice()
.try_into()
.expect("decrypted plaintext"),
);
assert_eq!(
decrypted_u128, plaintext,