Move to util and rename

This commit is contained in:
wborgeaud 2022-09-14 05:48:37 +02:00
parent e8eca780ae
commit 2a37aeca5d
2 changed files with 19 additions and 18 deletions

View File

@ -3,6 +3,7 @@ use std::ops::Range;
use plonky2_field::extension::Extendable;
use plonky2_field::packed::PackedField;
use plonky2_field::types::{Field, Field64};
use plonky2_util::log_floor;
use crate::gates::gate::Gate;
use crate::gates::packed_util::PackedEvaluableBase;
@ -33,7 +34,7 @@ impl<const B: usize> BaseSumGate<B> {
pub fn new_from_config<F: Field64>(config: &CircuitConfig) -> Self {
let num_limbs =
logarithm(F::ORDER as usize, B).min(config.num_routed_wires - Self::START_LIMBS);
log_floor(F::ORDER as usize, B).min(config.num_routed_wires - Self::START_LIMBS);
Self::new(num_limbs)
}
@ -193,23 +194,6 @@ impl<F: RichField, const B: usize> SimpleGenerator<F> for BaseSplitGenerator<B>
}
}
/// Returns the largest `i` such that `base**i < n`.
const fn logarithm(n: usize, base: usize) -> usize {
assert!(n > 0);
assert!(base > 1);
let mut i = 0;
let mut cur: usize = 1;
loop {
let (mul, overflow) = cur.overflowing_mul(base);
if overflow || mul >= n {
return i;
} else {
i += 1;
cur = mul;
}
}
}
#[cfg(test)]
mod tests {
use anyhow::Result;

View File

@ -38,6 +38,23 @@ pub fn log2_strict(n: usize) -> usize {
res as usize
}
/// Returns the largest `i` such that `base**i < n`.
pub const fn log_floor(n: usize, base: usize) -> usize {
assert!(n > 0);
assert!(base > 1);
let mut i = 0;
let mut cur: usize = 1;
loop {
let (mul, overflow) = cur.overflowing_mul(base);
if overflow || mul >= n {
return i;
} else {
i += 1;
cur = mul;
}
}
}
/// Permutes `arr` such that each index is mapped to its reverse in binary.
pub fn reverse_index_bits<T: Copy>(arr: &[T]) -> Vec<T> {
let n = arr.len();