Skip to content

Commit 52c0de8

Browse files
authored
Merge pull request #15 from cryptape/feature/clippy-refactor
Clippy refactor
2 parents 53f097b + 880694f commit 52c0de8

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ jobs:
2121
- stable
2222
- beta
2323
- nightly
24+
command:
25+
- build
26+
- test
27+
include:
28+
- toolchain: "1.76.0"
29+
command: clippy
2430
steps:
2531
- uses: actions/checkout@v4
2632
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
27-
- run: cargo build --verbose
28-
- run: cargo test --verbose
33+
- run: cargo ${{ matrix.command }} --verbose
34+
env:
35+
RUSTFLAGS: "-Dwarnings"
2936

3037
ci:
3138
name: Build and Test

src/lib.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl OnionPacket {
266266
if data_len > packet_data_len {
267267
return Err(SphinxError::HopDataLenTooLarge);
268268
}
269-
let hop_data = (&packet_data[0..data_len]).to_vec();
269+
let hop_data = packet_data[0..data_len].to_vec();
270270
let mut hmac = [0; 32];
271271
hmac.copy_from_slice(&packet_data[data_len..(data_len + 32)]);
272272
shift_slice_left(&mut packet_data[..], data_len + 32);
@@ -491,11 +491,7 @@ impl<'s, I, C: Signing> OnionSharedSecretIter<'s, I, C> {
491491
/// - `hops_path`: The public keys for each hop. These are _y_<sub>i</sub> in the specification.
492492
/// - `session_key`: The ephemeral secret key for the onion packet. It must be generated securely using a random process.
493493
/// This is _x_ in the specification.
494-
pub fn new(
495-
hops_path_iter: I,
496-
session_key: SecretKey,
497-
secp_ctx: &'s Secp256k1<C>,
498-
) -> OnionSharedSecretIter<I, C> {
494+
pub fn new(hops_path_iter: I, session_key: SecretKey, secp_ctx: &'s Secp256k1<C>) -> Self {
499495
OnionSharedSecretIter {
500496
hops_path_iter,
501497
secp_ctx,
@@ -511,7 +507,7 @@ impl<'s, 'i, I: Iterator<Item = &'i PublicKey>, C: Signing> Iterator
511507

512508
fn next(&mut self) -> Option<Self::Item> {
513509
self.hops_path_iter.next().map(|pk| {
514-
let shared_secret = SharedSecret::new(&pk, &self.ephemeral_secret_key);
510+
let shared_secret = SharedSecret::new(pk, &self.ephemeral_secret_key);
515511

516512
let ephemeral_public_key = self.ephemeral_secret_key.public_key(self.secp_ctx);
517513
self.ephemeral_secret_key = derive_next_hop_ephemeral_secret_key(
@@ -527,7 +523,7 @@ impl<'s, 'i, I: Iterator<Item = &'i PublicKey>, C: Signing> Iterator
527523

528524
/// Derives keys for forwarding the onion packet.
529525
fn derive_hops_forward_keys<C: Signing>(
530-
hops_path: &Vec<PublicKey>,
526+
hops_path: &[PublicKey],
531527
session_key: SecretKey,
532528
secp_ctx: &Secp256k1<C>,
533529
) -> Vec<ForwardKeys> {
@@ -541,8 +537,8 @@ fn shift_slice_right(arr: &mut [u8], amt: usize) {
541537
for i in (amt..arr.len()).rev() {
542538
arr[i] = arr[i - amt];
543539
}
544-
for i in 0..amt {
545-
arr[i] = 0;
540+
for item in arr.iter_mut().take(amt) {
541+
*item = 0;
546542
}
547543
}
548544

@@ -552,16 +548,16 @@ fn shift_slice_left(arr: &mut [u8], amt: usize) {
552548
for i in 0..pivot {
553549
arr[i] = arr[i + amt];
554550
}
555-
for i in pivot..arr.len() {
556-
arr[i] = 0;
551+
for item in arr.iter_mut().skip(pivot) {
552+
*item = 0;
557553
}
558554
}
559555

560556
/// Computes hmac of packet_data and optional associated data using the key `hmac_key`.
561557
fn compute_hmac(hmac_key: &[u8; 32], packet_data: &[u8], assoc_data: Option<&[u8]>) -> [u8; 32] {
562558
let mut hmac_engine = Hmac::<Sha256>::new_from_slice(hmac_key).expect("valid hmac key");
563-
hmac_engine.update(&packet_data);
564-
if let Some(ref assoc_data) = assoc_data {
559+
hmac_engine.update(packet_data);
560+
if let Some(assoc_data) = assoc_data {
565561
hmac_engine.update(assoc_data);
566562
}
567563
hmac_engine.finalize().into_bytes().into()
@@ -596,7 +592,7 @@ fn derive_next_hop_ephemeral_secret_key(
596592
let blinding_factor: [u8; 32] = {
597593
let mut sha = Sha256::new();
598594
sha.update(&ephemeral_public_key.serialize()[..]);
599-
sha.update(shared_secret.as_ref());
595+
sha.update(shared_secret);
600596
sha.finalize().into()
601597
};
602598

@@ -697,7 +693,7 @@ fn construct_onion_packet(
697693
for (i, (data, keys)) in hops_data.iter().zip(hops_keys.iter()).rev().enumerate() {
698694
let data_len = data.len();
699695
shift_slice_right(&mut packet_data, data_len + 32);
700-
packet_data[0..data_len].copy_from_slice(&data);
696+
packet_data[0..data_len].copy_from_slice(data);
701697
packet_data[data_len..(data_len + 32)].copy_from_slice(&hmac);
702698

703699
let mut chacha = ChaCha20::new(&keys.rho.into(), &[0u8; 12].into());

0 commit comments

Comments
 (0)