Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ff1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Radix {
let log_radix = 31 - radix.leading_zeros();
Radix::PowerTwo {
radix,
min_len: cmp::max((MIN_RADIX_2_NS_LEN + log_radix - 1) / log_radix, MIN_NS_LEN),
min_len: cmp::max(MIN_RADIX_2_NS_LEN.div_ceil(log_radix), MIN_NS_LEN),
log_radix: u8::try_from(log_radix).unwrap(),
}
} else {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Radix {
use libm::{ceil, log2};
match *self {
Radix::Any { radix, .. } => ceil(v as f64 * log2(f64::from(radix)) / 8f64) as usize,
Radix::PowerTwo { log_radix, .. } => ((v * log_radix as usize) + 7) / 8,
Radix::PowerTwo { log_radix, .. } => (v * log_radix as usize).div_ceil(8),
}
}

Expand Down Expand Up @@ -204,7 +204,7 @@ fn generate_s<'a, CIPH: BlockEncrypt>(
) -> impl Iterator<Item = u8> + 'a {
r.clone()
.into_iter()
.chain((1..((d + 15) / 16) as u128).flat_map(move |j| {
.chain((1..d.div_ceil(16) as u128).flat_map(move |j| {
let mut block = r.clone();
for (b, j) in block.iter_mut().zip(j.to_be_bytes().iter()) {
*b ^= j;
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> FF1<CIPH> {
let b = self.radix.calculate_b(v);

// 4. Let d = 4 * ceil(b / 4) + 4.
let d = 4 * ((b + 3) / 4) + 4;
let d = 4 * b.div_ceil(4) + 4;

// 5. Let P = [1, 2, 1] || [radix] || [10] || [u mod 256] || [n] || [t].
let mut p = [1, 2, 1, 0, 0, 0, 10, u as u8, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<CIPH: BlockCipher + BlockEncrypt + Clone> FF1<CIPH> {
let b = self.radix.calculate_b(v);

// 4. Let d = 4 * ceil(b / 4) + 4.
let d = 4 * ((b + 3) / 4) + 4;
let d = 4 * b.div_ceil(4) + 4;

// 5. Let P = [1, 2, 1] || [radix] || [10] || [u mod 256] || [n] || [t].
let mut p = [1, 2, 1, 0, 0, 0, 10, u as u8, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down
14 changes: 6 additions & 8 deletions src/ff1/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! FF1 NumeralString implementations that require a global allocator.

use core::iter;

use alloc::{vec, vec::Vec};

use num_bigint::{BigInt, BigUint, Sign};
Expand Down Expand Up @@ -55,7 +53,7 @@ impl Numeral for BigUint {
let mut bytes = self.to_bytes_le();
let padding = b - bytes.len();
bytes.reserve_exact(padding);
bytes.extend(iter::repeat(0).take(padding));
bytes.extend(std::iter::repeat_n(0, padding));
bytes.reverse();
bytes
}
Expand Down Expand Up @@ -97,7 +95,7 @@ impl NumeralString for FlexibleNumeralString {
type Ops = Self;

fn is_valid(&self, radix: u32) -> bool {
self.0.iter().all(|n| (u32::from(*n) < radix))
self.0.iter().all(|n| u32::from(*n) < radix)
}

fn numeral_count(&self) -> usize {
Expand Down Expand Up @@ -194,7 +192,7 @@ impl NumeralString for BinaryNumeralString {
let n = self.numeral_count();
let u = n / 2;
let v = n - u;
let a_end = (u + 7) / 8;
let a_end = u.div_ceil(8);
let b_start = u / 8;

// FF1 processes the two halves of a numeral string as big-endian integers in the
Expand Down Expand Up @@ -242,7 +240,7 @@ impl NumeralString for BinaryNumeralString {
let a_subslice = self.0[..a_end].iter();
let b_subslice = self.0[b_start..].iter();

let (a, b) = if u % 8 == 0 {
let (a, b) = if u.is_multiple_of(8) {
// Simple case: no shifting necessary, just splitting and reversing.
assert_eq!(a_end, b_start);

Expand Down Expand Up @@ -297,7 +295,7 @@ impl NumeralString for BinaryNumeralString {
// Simple case: no shifting necessary, just reversing and joining.
b.data
.into_iter()
.chain(a.data.into_iter())
.chain(a.data)
.map(|b| b.reverse_bits())
.rev()
.collect()
Expand Down Expand Up @@ -384,7 +382,7 @@ impl Operations for BinaryOps {

impl BinaryOps {
fn new(data: Vec<u8>, num_bits: usize) -> Self {
assert_eq!(data.len(), (num_bits + 7) / 8);
assert_eq!(data.len(), num_bits.div_ceil(8));
BinaryOps { data, num_bits }
}

Expand Down
2 changes: 1 addition & 1 deletion src/ff1/proptests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ proptest! {
key in prop::array::uniform32(prop::num::u8::ANY),
(radix, _, _) in valid_radix(),
) {
assert!(matches!(FF1::<Aes256>::new(&key, radix), Ok(_)));
assert!(FF1::<Aes256>::new(&key, radix).is_ok());
}

#[test]
Expand Down
Loading