diff --git a/src/ff1.rs b/src/ff1.rs index bf0500d..271bc06 100644 --- a/src/ff1.rs +++ b/src/ff1.rs @@ -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 { @@ -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), } } @@ -204,7 +204,7 @@ fn generate_s<'a, CIPH: BlockEncrypt>( ) -> impl Iterator + '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; @@ -260,7 +260,7 @@ impl FF1 { 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]; @@ -330,7 +330,7 @@ impl FF1 { 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]; diff --git a/src/ff1/alloc.rs b/src/ff1/alloc.rs index f932fc1..ec84c64 100644 --- a/src/ff1/alloc.rs +++ b/src/ff1/alloc.rs @@ -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}; @@ -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 } @@ -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 { @@ -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 @@ -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); @@ -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() @@ -384,7 +382,7 @@ impl Operations for BinaryOps { impl BinaryOps { fn new(data: Vec, 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 } } diff --git a/src/ff1/proptests.rs b/src/ff1/proptests.rs index f44da13..a6f820f 100644 --- a/src/ff1/proptests.rs +++ b/src/ff1/proptests.rs @@ -56,7 +56,7 @@ proptest! { key in prop::array::uniform32(prop::num::u8::ANY), (radix, _, _) in valid_radix(), ) { - assert!(matches!(FF1::::new(&key, radix), Ok(_))); + assert!(FF1::::new(&key, radix).is_ok()); } #[test]