Skip to content

Commit 26df8b0

Browse files
committed
Fix clippy and rustfmt errors in kmac submission
1 parent ea0ce6c commit 26df8b0

File tree

6 files changed

+253
-227
lines changed

6 files changed

+253
-227
lines changed

kmac/benches/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(test)]
22
extern crate test;
33

4+
use core::hint::black_box;
45
use kmac::{KeyInit, Kmac128, Kmac256, Mac};
56
use test::Bencher;
6-
use core::hint::black_box;
77

88
#[macro_export]
99
macro_rules! bench_full {
@@ -58,4 +58,4 @@ digest::bench_update!(
5858
kmac256_update_100 100;
5959
kmac256_update_1000 1000;
6060
kmac256_update_10000 10000;
61-
);
61+
);

kmac/src/encoding.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#[inline(always)]
44
pub(crate) fn num_encoding_size(num: u64) -> usize {
55
let bits = 64 - (num | 1).leading_zeros() as usize;
6-
let len = (bits + 7) / 8;
7-
len
6+
bits.div_ceil(8)
87
}
98

109
#[inline(always)]
@@ -55,7 +54,13 @@ mod tests {
5554
];
5655

5756
for &(num, expected_size) in &test_cases {
58-
assert_eq!(num_encoding_size(num), expected_size, "num_encoding_size({}) should return {}", num, expected_size);
57+
assert_eq!(
58+
num_encoding_size(num),
59+
expected_size,
60+
"num_encoding_size({}) should return {}",
61+
num,
62+
expected_size
63+
);
5964
}
6065
}
6166

@@ -88,9 +93,10 @@ mod tests {
8893

8994
for i in 0..usize::BITS {
9095
let x: usize = 1 << i;
91-
let mut want = std::vec::Vec::from_iter(x.to_be_bytes().iter().copied().skip_while(|&v| v == 0));
96+
let mut want =
97+
std::vec::Vec::from_iter(x.to_be_bytes().iter().copied().skip_while(|&v| v == 0));
9298
want.push(want.len() as u8);
9399
assert_eq!(right_encode(x as u64, &mut buf), want, "#{x}");
94100
}
95101
}
96-
}
102+
}

kmac/src/kmac.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::encoding::{left_encode, right_encode};
2+
use crate::traits::{CShake, EagerHash};
13
use core::fmt;
2-
use digest::block_api::{AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore, FixedOutputCore, UpdateCore, XofReaderCore};
4+
use digest::block_api::{
5+
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore,
6+
FixedOutputCore, UpdateCore, XofReaderCore,
7+
};
38
use digest::crypto_common::KeySizeUser;
49
use digest::{InvalidLength, Key, KeyInit, MacMarker, Output, OutputSizeUser};
5-
use crate::encoding::{left_encode, right_encode};
6-
use crate::traits::{CShake, EagerHash};
710

811
pub struct KmacCore<D: EagerHash> {
912
digest: D::Core,
@@ -45,27 +48,22 @@ impl<D: EagerHash> KmacCore<D> {
4548
// bytepad, left_encode(w)
4649
buffer.digest_blocks(
4750
left_encode(D::block_size() as u64, &mut encode_buffer),
48-
|blocks| digest.update_blocks(blocks)
51+
|blocks| digest.update_blocks(blocks),
4952
);
5053

5154
// encode_string(K), left_encode(len(K)) -- length is in bits
5255
buffer.digest_blocks(
5356
left_encode(8 * key.len() as u64, &mut encode_buffer),
54-
|blocks| digest.update_blocks(blocks)
57+
|blocks| digest.update_blocks(blocks),
5558
);
5659

5760
// encode_string(K) copy K into blocks
58-
buffer.digest_blocks(
59-
&key,
60-
|blocks| digest.update_blocks(blocks)
61-
);
61+
buffer.digest_blocks(key, |blocks| digest.update_blocks(blocks));
6262

6363
// bytepad, pad the key to the block size
6464
digest.update_blocks(&[buffer.pad_with_zeros()]);
6565

66-
Self {
67-
digest,
68-
}
66+
Self { digest }
6967
}
7068
}
7169

@@ -95,7 +93,7 @@ impl<D: EagerHash> KmacCore<D> {
9593
// right_encode(L), where L = output length in bits
9694
buffer.digest_blocks(
9795
right_encode(8 * out.len() as u64, &mut [0u8; 9]),
98-
|blocks| self.update_blocks(blocks)
96+
|blocks| self.update_blocks(blocks),
9997
);
10098

10199
let mut reader = self.digest.finalize_xof_core(buffer);
@@ -126,10 +124,9 @@ impl<D: EagerHash> ExtendableOutputCore for KmacCore<D> {
126124
#[inline(always)]
127125
fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore {
128126
// right_encode(0), as L = 0 for extendable output
129-
buffer.digest_blocks(
130-
right_encode(0, &mut [0u8; 9]),
131-
|blocks| self.update_blocks(blocks)
132-
);
127+
buffer.digest_blocks(right_encode(0, &mut [0u8; 9]), |blocks| {
128+
self.update_blocks(blocks)
129+
});
133130
self.digest.finalize_xof_core(buffer)
134131
}
135132
}

kmac/src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#![forbid(unsafe_code)]
1111
#![warn(missing_docs)]
1212

13+
mod encoding;
1314
mod kmac;
1415
mod traits;
15-
mod encoding;
1616

1717
use crate::kmac::KmacCore;
1818
use digest::block_api::{Block, BlockSizeUser, Buffer, ExtendableOutputCore, XofReaderCore};
1919
use digest::block_buffer::ReadBuffer;
20-
use digest::consts::{U136, U168, U32, U64};
20+
use digest::consts::{U32, U64, U136, U168};
2121
pub use digest::{self, ExtendableOutput, KeyInit, Mac, XofReader};
2222
use digest::{InvalidLength, OutputSizeUser};
2323
use sha3::block_api::Sha3ReaderCore;
@@ -213,12 +213,18 @@ mod tests {
213213
mac.update(b"input message");
214214
let result = mac.finalize();
215215
let code_bytes = result.into_bytes();
216-
let expected = hex!("
216+
let expected = hex!(
217+
"
217218
c39a8f614f8821443599440df5402787
218219
0f67e4c47919061584f14a616f3efcf5
219-
");
220-
assert_eq!(code_bytes[..], expected[..], "Expected hex output is {}", hex::encode(&code_bytes));
221-
220+
"
221+
);
222+
assert_eq!(
223+
code_bytes[..],
224+
expected[..],
225+
"Expected hex output is {}",
226+
hex::encode(&code_bytes)
227+
);
222228

223229
let mut mac = Kmac128::new_from_slice(b"key material").unwrap();
224230
mac.update(b"input message");
@@ -232,11 +238,18 @@ mod tests {
232238
let mut output = [0u8; 32];
233239
mac.finalize_into(&mut output);
234240

235-
let expected = hex!("
241+
let expected = hex!(
242+
"
236243
85fb77da3a35e4c4b0057c3151e6cc54
237244
ee401ffe65ec2f0239f439be8896f7b6
238-
");
239-
assert_eq!(output[..], expected[..], "Expected hex output is {}", hex::encode(&output));
245+
"
246+
);
247+
assert_eq!(
248+
output[..],
249+
expected[..],
250+
"Expected hex output is {}",
251+
hex::encode(&output)
252+
);
240253
}
241254

242255
#[test]
@@ -248,10 +261,17 @@ mod tests {
248261
let mut output = [0u8; 32];
249262
reader.read(&mut output);
250263

251-
let expected = hex!("
264+
let expected = hex!(
265+
"
252266
b675b75668eab0706ab05650f34fa1b6
253267
24051a9a42b5e42cfe9970e8f903d45b
254-
");
255-
assert_eq!(output[..], expected[..], "Expected hex output is {}", hex::encode(&output));
268+
"
269+
);
270+
assert_eq!(
271+
output[..],
272+
expected[..],
273+
"Expected hex output is {}",
274+
hex::encode(&output)
275+
);
256276
}
257-
}
277+
}

kmac/src/traits.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use digest::block_api::{BlockSizeUser, BufferKindUser, CoreProxy, Eager, ExtendableOutputCore, UpdateCore};
21
use digest::HashMarker;
2+
use digest::block_api::{
3+
BlockSizeUser, BufferKindUser, CoreProxy, Eager, ExtendableOutputCore, UpdateCore,
4+
};
35

46
const FUNCTION_NAME: &[u8] = b"KMAC";
57

@@ -23,26 +25,26 @@ impl CShake for sha3::block_api::CShake256Core {
2325
pub trait EagerHash: BlockSizeUser {
2426
/// Block-level core type of the hash.
2527
type Core: HashMarker
26-
+ CShake
27-
+ UpdateCore
28-
+ ExtendableOutputCore
29-
+ BlockSizeUser<BlockSize = <Self as BlockSizeUser>::BlockSize>
30-
+ BufferKindUser<BufferKind = Eager>
31-
+ Default
32-
+ Clone;
28+
+ CShake
29+
+ UpdateCore
30+
+ ExtendableOutputCore
31+
+ BlockSizeUser<BlockSize = <Self as BlockSizeUser>::BlockSize>
32+
+ BufferKindUser<BufferKind = Eager>
33+
+ Default
34+
+ Clone;
3335
}
3436

3537
impl<T> EagerHash for T
3638
where
3739
T: CoreProxy + BlockSizeUser,
3840
<T as CoreProxy>::Core: HashMarker
39-
+ CShake
40-
+ UpdateCore
41-
+ ExtendableOutputCore
42-
+ BlockSizeUser<BlockSize = <Self as BlockSizeUser>::BlockSize>
43-
+ BufferKindUser<BufferKind = Eager>
44-
+ Default
45-
+ Clone,
41+
+ CShake
42+
+ UpdateCore
43+
+ ExtendableOutputCore
44+
+ BlockSizeUser<BlockSize = <Self as BlockSizeUser>::BlockSize>
45+
+ BufferKindUser<BufferKind = Eager>
46+
+ Default
47+
+ Clone,
4648
{
4749
type Core = T::Core;
4850
}

0 commit comments

Comments
 (0)