Merge pull request #718 from mir-protocol/fix_basesumgate_numlimbs

Fix `num_limbs` in `BaseSumGate`
This commit is contained in:
wborgeaud 2022-09-29 16:32:56 +02:00 committed by GitHub
commit 58256ce052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 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;
@ -32,7 +33,8 @@ impl<const B: usize> BaseSumGate<B> {
}
pub fn new_from_config<F: Field64>(config: &CircuitConfig) -> Self {
let num_limbs = F::BITS.min(config.num_routed_wires - Self::START_LIMBS);
let num_limbs =
log_floor(F::ORDER - 1, B as u64).min(config.num_routed_wires - Self::START_LIMBS);
Self::new(num_limbs)
}

View File

@ -38,6 +38,23 @@ pub fn log2_strict(n: usize) -> usize {
res as usize
}
/// Returns the largest integer `i` such that `base**i <= n`.
pub const fn log_floor(n: u64, base: u64) -> usize {
assert!(n > 0);
assert!(base > 1);
let mut i = 0;
let mut cur: u64 = 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();