Skip to content

Commit

Permalink
chore: rename get_padded functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 11, 2024
1 parent 429512f commit 8d21ee6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions crates/precompile/src/modexp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
primitives::U256,
utilities::{get_right_padded, get_right_padded_vec, left_pad, left_pad_vec},
utilities::{left_pad, left_pad_vec, right_pad_with_offset, right_pad_with_offset_vec},
Error, Precompile, PrecompileResult, PrecompileWithAddress,
};
use alloc::vec::Vec;
Expand Down Expand Up @@ -59,9 +59,9 @@ where
const HEADER_LENGTH: usize = 96;

// Extract the header.
let base_len = U256::from_be_bytes(get_right_padded::<32>(input, 0).into_owned());
let exp_len = U256::from_be_bytes(get_right_padded::<32>(input, 32).into_owned());
let mod_len = U256::from_be_bytes(get_right_padded::<32>(input, 64).into_owned());
let base_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 0).into_owned());
let exp_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 32).into_owned());
let mod_len = U256::from_be_bytes(right_pad_with_offset::<32>(input, 64).into_owned());

// cast base and modulus to usize, it does not make sense to handle larger values
let Ok(base_len) = usize::try_from(base_len) else {
Expand Down Expand Up @@ -94,7 +94,7 @@ where

let exp_highp = {
// get right padded bytes so if data.len is less then exp_len we will get right padded zeroes.
let right_padded_highp = get_right_padded::<32>(input, base_len);
let right_padded_highp = right_pad_with_offset::<32>(input, base_len);
// If exp_len is less then 32 bytes get only exp_len bytes and do left padding.
let out = left_pad::<32>(&right_padded_highp[..exp_highp_len]);
U256::from_be_bytes(out.into_owned())
Expand All @@ -108,9 +108,9 @@ where
}

// Padding is needed if the input does not contain all 3 values.
let base = get_right_padded_vec(input, 0, base_len);
let exponent = get_right_padded_vec(input, base_len, exp_len);
let modulus = get_right_padded_vec(input, base_len.saturating_add(exp_len), mod_len);
let base = right_pad_with_offset_vec(input, 0, base_len);
let exponent = right_pad_with_offset_vec(input, base_len, exp_len);
let modulus = right_pad_with_offset_vec(input, base_len.saturating_add(exp_len), mod_len);

// Call the modexp.
let output = modexp(&base, &exponent, &modulus);
Expand Down
16 changes: 8 additions & 8 deletions crates/precompile/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use core::cmp::min;
///
/// Returns the first `LEN` bytes if it does not need padding.
#[inline(always)]
pub fn get_right_padded<const LEN: usize>(data: &[u8], offset: usize) -> Cow<'_, [u8; LEN]> {
pub fn right_pad_with_offset<const LEN: usize>(data: &[u8], offset: usize) -> Cow<'_, [u8; LEN]> {
right_pad(data.get(offset..).unwrap_or_default())
}

/// Right-pads the given slice at `offset` with zeroes until `len`.
///
/// Returns the first `len` bytes if it does not need padding.
#[inline(always)]
pub fn get_right_padded_vec(data: &[u8], offset: usize, len: usize) -> Cow<'_, [u8]> {
pub fn right_pad_with_offset_vec(data: &[u8], offset: usize, len: usize) -> Cow<'_, [u8]> {
right_pad_vec(data.get(offset..).unwrap_or_default(), len)
}

Expand Down Expand Up @@ -84,26 +84,26 @@ mod tests {
#[test]
fn get_with_right_padding() {
let data = [1, 2, 3, 4];
let padded = get_right_padded::<8>(&data, 4);
let padded = right_pad_with_offset::<8>(&data, 4);
assert!(matches!(padded, Cow::Owned(_)));
assert_eq!(padded[..], [0, 0, 0, 0, 0, 0, 0, 0]);
let padded = get_right_padded_vec(&data, 4, 8);
let padded = right_pad_with_offset_vec(&data, 4, 8);
assert!(matches!(padded, Cow::Owned(_)));
assert_eq!(padded[..], [0, 0, 0, 0, 0, 0, 0, 0]);

let data = [1, 2, 3, 4, 5, 6, 7, 8];
let padded = get_right_padded::<8>(&data, 0);
let padded = right_pad_with_offset::<8>(&data, 0);
assert!(matches!(padded, Cow::Borrowed(_)));
assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);
let padded = get_right_padded_vec(&data, 0, 8);
let padded = right_pad_with_offset_vec(&data, 0, 8);
assert!(matches!(padded, Cow::Borrowed(_)));
assert_eq!(padded[..], [1, 2, 3, 4, 5, 6, 7, 8]);

let data = [1, 2, 3, 4, 5, 6, 7, 8];
let padded = get_right_padded::<8>(&data, 4);
let padded = right_pad_with_offset::<8>(&data, 4);
assert!(matches!(padded, Cow::Owned(_)));
assert_eq!(padded[..], [5, 6, 7, 8, 0, 0, 0, 0]);
let padded = get_right_padded_vec(&data, 4, 8);
let padded = right_pad_with_offset_vec(&data, 4, 8);
assert!(matches!(padded, Cow::Owned(_)));
assert_eq!(padded[..], [5, 6, 7, 8, 0, 0, 0, 0]);
}
Expand Down

0 comments on commit 8d21ee6

Please sign in to comment.