Skip to content

Commit

Permalink
use align_num value
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Oct 28, 2024
1 parent af5ae5a commit bc84d81
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/net/ecies/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ pub struct RLPxSymmetricKeys {
pub mac_key: B256,
}

/// return num align size
#[inline]
const fn align_num(num: usize, align: usize) -> usize {
(num + (align - 1)) & !(align - 1)
}

impl ECIES {
/// Create a new client with the given static secret key, remote peer id, nonce, and ephemeral
/// secret key.
Expand Down Expand Up @@ -686,7 +692,7 @@ impl ECIES {

pub fn body_len(&self) -> usize {
let len = self.body_size.unwrap();
(if len % 16 == 0 { len } else { (len / 16 + 1) * 16 }) + 16
align_num(len, 16) + 16
}

#[cfg(test)]
Expand All @@ -697,7 +703,7 @@ impl ECIES {
}

pub fn write_body(&mut self, out: &mut BytesMut, data: &[u8]) {
let len = if data.len() % 16 == 0 { data.len() } else { (data.len() / 16 + 1) * 16 };
let len = align_num(data.len(), 16);
let old_len = out.len();
out.resize(old_len + len, 0);

Expand Down

0 comments on commit bc84d81

Please sign in to comment.