Skip to content

Commit

Permalink
Get rid of allow(clippy::cast_possible_truncation)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed Jun 6, 2023
1 parent 7bc388b commit d56a16c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
20 changes: 9 additions & 11 deletions algorithms/src/crypto_hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
let capacity = F::size_in_bits() - 1;
let mut dest_limbs = Vec::<F>::new();

let params = get_params(TargetField::size_in_bits(), F::size_in_bits(), ty);
let params = get_params(TargetField::size_in_bits_u32(), F::size_in_bits_u32(), ty);

let adjustment_factor_lookup_table = {
let mut table = Vec::<F>::new();
Expand All @@ -342,9 +342,9 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
let first = &src_limbs[i];
let second = if i + 1 < src_len { Some(&src_limbs[i + 1]) } else { None };

let first_max_bits_per_limb = params.bits_per_limb + crate::overhead!(first.1 + F::one());
let first_max_bits_per_limb = params.bits_per_limb as usize + crate::overhead!(first.1 + F::one());
let second_max_bits_per_limb = if let Some(second) = second {
params.bits_per_limb + crate::overhead!(second.1 + F::one())
params.bits_per_limb as usize + crate::overhead!(second.1 + F::one())
} else {
0
};
Expand Down Expand Up @@ -377,26 +377,24 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
Self::get_limbs_representations_from_big_integer::<TargetField>(&elem.to_bigint(), optimization_type)
}

// params.bits_per_limb is safe to cast as u32 because it will be less than `max_limb_size`
#[allow(clippy::cast_possible_truncation)]
/// Obtain the limbs directly from a big int
pub fn get_limbs_representations_from_big_integer<TargetField: PrimeField>(
elem: &<TargetField as PrimeField>::BigInteger,
optimization_type: OptimizationType,
) -> SmallVec<[F; 10]> {
let params = get_params(TargetField::size_in_bits(), F::size_in_bits(), optimization_type);
assert!(params.bits_per_limb <= u32::MAX as usize);
let params = get_params(TargetField::size_in_bits_u32(), F::size_in_bits_u32(), optimization_type);

// Push the lower limbs first
let mut limbs: SmallVec<[F; 10]> = SmallVec::new();
let mut cur = *elem;
for _ in 0..params.num_limbs {
let cur_bits = cur.to_bits_be(); // `to_bits` is big endian
let cur_mod_r =
<F as PrimeField>::BigInteger::from_bits_be(&cur_bits[cur_bits.len() - params.bits_per_limb..])
.unwrap(); // therefore, the lowest `bits_per_non_top_limb` bits is what we want.
let cur_mod_r = <F as PrimeField>::BigInteger::from_bits_be(
&cur_bits[cur_bits.len() - params.bits_per_limb as usize..],
)
.unwrap(); // therefore, the lowest `bits_per_non_top_limb` bits is what we want.
limbs.push(F::from_bigint(cur_mod_r).unwrap());
cur.divn(params.bits_per_limb as u32);
cur.divn(params.bits_per_limb);
}

// then we reserve, so that the limbs are ``big limb first''
Expand Down
22 changes: 11 additions & 11 deletions algorithms/src/traits/algebraic_sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ pub(crate) mod nonnative_params {
#[derive(Clone, Debug)]
pub struct NonNativeFieldParams {
/// The number of limbs (`BaseField` elements) used to represent a `TargetField` element. Highest limb first.
pub num_limbs: usize,
pub num_limbs: u32,

/// The number of bits of the limb
pub bits_per_limb: usize,
pub bits_per_limb: u32,
}

/// Obtain the parameters from a `ConstraintSystem`'s cache or generate a new one
#[must_use]
pub const fn get_params(
target_field_size: usize,
base_field_size: usize,
target_field_size: u32,
base_field_size: u32,
optimization_type: OptimizationType,
) -> NonNativeFieldParams {
let (num_of_limbs, limb_size) = find_parameters(base_field_size, target_field_size, optimization_type);
Expand All @@ -156,21 +156,21 @@ pub(crate) mod nonnative_params {

/// A function to search for parameters for nonnative field gadgets
pub const fn find_parameters(
base_field_prime_length: usize,
target_field_prime_bit_length: usize,
base_field_prime_length: u32,
target_field_prime_bit_length: u32,
optimization_type: OptimizationType,
) -> (usize, usize) {
) -> (u32, u32) {
let mut found = false;
let mut min_cost = 0usize;
let mut min_cost_limb_size = 0usize;
let mut min_cost_num_of_limbs = 0usize;
let mut min_cost = 0u32;
let mut min_cost_limb_size = 0u32;
let mut min_cost_num_of_limbs = 0u32;

let surfeit = 10;
let mut max_limb_size = (base_field_prime_length - 1 - surfeit - 1) / 2 - 1;
if max_limb_size > target_field_prime_bit_length {
max_limb_size = target_field_prime_bit_length;
}
let mut limb_size = 1;
let mut limb_size = 1u32;

while limb_size <= max_limb_size {
let num_of_limbs = (target_field_prime_bit_length + limb_size - 1) / limb_size;
Expand Down

0 comments on commit d56a16c

Please sign in to comment.