Skip to content

Commit

Permalink
correct mistakes, simplify, assert
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-chuang committed Feb 8, 2021
1 parent 113e7c0 commit 691d9a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
23 changes: 5 additions & 18 deletions ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,11 @@ pub trait Field:
/// Exponentiates this element by a number represented with `u64` limbs,
/// least significant limb first.
#[inline]
fn pow_with_table<S: AsRef<[u64]>>(&self, exp: S, pows_2: &[Self]) -> Self {
let exp = exp.as_ref();
let len = exp.len();
let mut max = len * 64;
for i in 0..len {
let lz = exp[len - 1 - i].leading_zeros() as usize;
max -= lz;
if lz == 64 {
break;
}
}

let mut res = Self::zero();
for i in 0..max {
let part = i / 64;
let bit = i - (64 * part);
if exp[part] & (1 << bit) > 0 {
res *= pows_2[i]
fn pow_with_table<S: AsRef<[u64]>>(pows_2: &[Self], exp: S) -> Self {
let mut res = Self::one();
for (pow, bit) in BitIteratorLE::without_trailing_zeros(exp).enumerate() {
if bit {
res *= &pows_2[pow];
}
}
res
Expand Down
20 changes: 14 additions & 6 deletions poly/src/polynomial/univariate/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,27 @@ impl<F: Field> Polynomial<F> for SparsePolynomial<F> {
let total_bits = core::mem::size_of::<usize>() * 8;
let max_pow_2 = total_bits - self.degree().leading_zeros() as usize;

let mut pows_2 = Vec::with_capacity(max_pow_2);
let mut pows_2 = Vec::<F>::with_capacity(max_pow_2);

let mut point = *point;
pows_2.push(point);
let mut p = *point;
pows_2.push(p);
for _ in 1..max_pow_2 {
point.square_in_place();
pows_2.push(point);
p.square_in_place();
pows_2.push(p);
}
// assert_eq!(pows_2.len(), max_pow_2);
// compute all coeff * point^{i} and then sum the results
let total = self
.coeffs
.iter()
.map(|(i, c)| (*c * point.pow_with_table(&[*i as u64], &pows_2[..])))
.map(|(i, c)| {
debug_assert_eq!(
F::pow_with_table(&pows_2[..], &[*i as u64]),
point.pow(&[*i as u64]),
"pows not equal"
);
F::pow_with_table(&pows_2[..], &[*i as u64])
})
.sum();
total
}
Expand Down

0 comments on commit 691d9a1

Please sign in to comment.