Skip to content

Commit

Permalink
Reimplement bitrev() using a compiler intrinsic (#1010)
Browse files Browse the repository at this point in the history
* Add cases to polynomial multiplication benchmarks

* Implement bitrev with a compiler intrinsic

* Lower FFT_THRESHOLD to 30
  • Loading branch information
divergentdave authored Apr 23, 2024
1 parent 3cadae5 commit a6d1c0a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion benches/speed_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn dp_noise(c: &mut Criterion) {
/// for small polynomials. The result is used to pick the `FFT_THRESHOLD` constant in
/// `src/flp/gadgets.rs`.
fn poly_mul(c: &mut Criterion) {
let test_sizes = [1_usize, 30, 60, 90, 120, 150];
let test_sizes = [1_usize, 30, 60, 90, 120, 150, 255];

let mut group = c.benchmark_group("poly_mul");
for size in test_sizes {
Expand Down
16 changes: 8 additions & 8 deletions src/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ pub fn discrete_fourier_transform<F: FftFriendlyFieldElement>(
return Err(FftError::SizeInvalid);
}

for (i, outp_val) in outp[..size].iter_mut().enumerate() {
let j = bitrev(d, i);
*outp_val = if j < inp.len() { inp[j] } else { F::zero() };
if d > 0 {
for (i, outp_val) in outp[..size].iter_mut().enumerate() {
let j = bitrev(d, i);
*outp_val = if j < inp.len() { inp[j] } else { F::zero() };
}
} else {
outp[0] = inp[0];
}

let mut w: F;
Expand Down Expand Up @@ -116,11 +120,7 @@ pub(crate) fn discrete_fourier_transform_inv_finish<F: FftFriendlyFieldElement>(

// bitrev returns the first d bits of x in reverse order. (Thanks, OEIS! https://oeis.org/A030109)
fn bitrev(d: usize, x: usize) -> usize {
let mut y = 0;
for i in 0..d {
y += ((x >> i) & 1) << (d - i);
}
y >> 1
x.reverse_bits() >> (usize::BITS - d as u32)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/flp/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::marker::PhantomData;

/// For input polynomials larger than or equal to this threshold, gadgets will use FFT for
/// polynomial multiplication. Otherwise, the gadget uses direct multiplication.
const FFT_THRESHOLD: usize = 60;
const FFT_THRESHOLD: usize = 30;

/// An arity-2 gadget that multiples its inputs.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit a6d1c0a

Please sign in to comment.