2021-06-17 19:40:41 +02:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
|
|
|
|
|
|
use crate::field::extension_field::Frobenius;
|
|
|
|
|
use crate::field::field::Field;
|
2021-06-17 21:34:04 +02:00
|
|
|
use crate::polynomial::polynomial::PolynomialCoeffs;
|
2021-06-17 19:40:41 +02:00
|
|
|
|
2021-06-17 21:34:04 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2021-06-17 22:06:53 +02:00
|
|
|
pub struct ReducingFactor<F: Field> {
|
2021-06-17 19:40:41 +02:00
|
|
|
base: F,
|
|
|
|
|
count: u64,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 22:06:53 +02:00
|
|
|
impl<F: Field> ReducingFactor<F> {
|
2021-06-17 19:40:41 +02:00
|
|
|
pub fn new(base: F) -> Self {
|
|
|
|
|
Self { base, count: 0 }
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 21:34:04 +02:00
|
|
|
fn mul(&mut self, x: F) -> F {
|
2021-06-17 19:40:41 +02:00
|
|
|
self.count += 1;
|
|
|
|
|
self.base * x
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 11:41:32 +02:00
|
|
|
fn mul_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
2021-06-17 21:34:04 +02:00
|
|
|
self.count += 1;
|
2021-06-23 11:41:32 +02:00
|
|
|
*p *= self.base;
|
2021-06-17 21:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 22:06:53 +02:00
|
|
|
pub fn reduce(&mut self, iter: impl DoubleEndedIterator<Item = impl Borrow<F>>) -> F {
|
2021-06-17 21:34:04 +02:00
|
|
|
iter.rev()
|
|
|
|
|
.fold(F::ZERO, |acc, x| self.mul(acc) + *x.borrow())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 22:06:53 +02:00
|
|
|
pub fn reduce_polys(
|
2021-06-17 21:34:04 +02:00
|
|
|
&mut self,
|
|
|
|
|
polys: impl DoubleEndedIterator<Item = impl Borrow<PolynomialCoeffs<F>>>,
|
|
|
|
|
) -> PolynomialCoeffs<F> {
|
2021-06-23 11:41:32 +02:00
|
|
|
polys.rev().fold(PolynomialCoeffs::empty(), |mut acc, x| {
|
|
|
|
|
self.mul_poly(&mut acc);
|
|
|
|
|
acc += x.borrow();
|
|
|
|
|
acc
|
2021-06-17 21:34:04 +02:00
|
|
|
})
|
2021-06-17 19:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn shift(&mut self, x: F) -> F {
|
|
|
|
|
let tmp = self.base.exp(self.count) * x;
|
|
|
|
|
self.count = 0;
|
|
|
|
|
tmp
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 11:41:32 +02:00
|
|
|
pub fn shift_poly(&mut self, p: &mut PolynomialCoeffs<F>) {
|
|
|
|
|
*p *= self.base.exp(self.count);
|
2021-06-17 21:34:04 +02:00
|
|
|
self.count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 19:40:41 +02:00
|
|
|
pub fn repeated_frobenius<const D: usize>(&self, count: usize) -> Self
|
|
|
|
|
where
|
|
|
|
|
F: Frobenius<D>,
|
|
|
|
|
{
|
|
|
|
|
Self {
|
|
|
|
|
base: self.base.repeated_frobenius(count),
|
|
|
|
|
count: self.count,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|