Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ColoCarletti committed Oct 17, 2024
1 parent 2546ca2 commit da5fae7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 168 deletions.
79 changes: 5 additions & 74 deletions math/src/circle/cfft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ pub fn cfft(
// The cfft has n layers.
(0..log_2_size).for_each(|i| {
// In each layer i we split the current input in chunks of size 2^{i+1}.
let chunk_size = 1 << i + 1;
let chunk_size = 1 << (i + 1);
let half_chunk_size = 1 << i;
input.chunks_mut(chunk_size).for_each(|chunk| {
// We split each chunk in half, calling the first half hi_part and the second hal low_part.
let (hi_part, low_part) = chunk.split_at_mut(half_chunk_size);

// We apply the corresponding butterfly for every element j of the high and low part.
hi_part
.into_iter()
.iter_mut()
.zip(low_part)
.enumerate()
.for_each(|(j, (hi, low))| {
let temp = *low * twiddles[i as usize][j];
*low = *hi - temp;
*hi = *hi + temp;
*hi += temp
});
});
});
Expand All @@ -47,15 +47,15 @@ pub fn icfft(
// The icfft has n layers.
(0..log_2_size).for_each(|i| {
// In each layer i we split the current input in chunks of size 2^{n - i}.
let chunk_size = 1 << log_2_size - i;
let chunk_size = 1 << (log_2_size - i);
let half_chunk_size = chunk_size >> 1;
input.chunks_mut(chunk_size).for_each(|chunk| {
// We split each chunk in half, calling the first half hi_part and the second hal low_part.
let (hi_part, low_part) = chunk.split_at_mut(half_chunk_size);

// We apply the corresponding butterfly for every element j of the high and low part.
hi_part
.into_iter()
.iter_mut()
.zip(low_part)
.enumerate()
.for_each(|(j, (hi, low))| {
Expand Down Expand Up @@ -119,75 +119,6 @@ pub fn reverse_cfft_index(index: usize, length: usize) -> usize {
}
}

pub fn cfft_4(
input: &mut [FieldElement<Mersenne31Field>],
twiddles: Vec<Vec<FieldElement<Mersenne31Field>>>,
) -> Vec<FieldElement<Mersenne31Field>> {
let mut stage1: Vec<FieldElement<Mersenne31Field>> = Vec::with_capacity(4);

stage1.push(input[0] + input[1]);
stage1.push((input[0] - input[1]) * twiddles[0][0]);

stage1.push(input[2] + input[3]);
stage1.push((input[2] - input[3]) * twiddles[0][1]);

let mut stage2: Vec<FieldElement<Mersenne31Field>> = Vec::with_capacity(4);

stage2.push(stage1[0] + stage1[2]);
stage2.push(stage1[1] + stage1[3]);

stage2.push((stage1[0] - stage1[2]) * twiddles[1][0]);
stage2.push((stage1[1] - stage1[3]) * twiddles[1][0]);

let f = FieldElement::<Mersenne31Field>::from(4).inv().unwrap();
stage2.into_iter().map(|elem| elem * f).collect()
}

pub fn cfft_8(
input: &mut [FieldElement<Mersenne31Field>],
twiddles: Vec<Vec<FieldElement<Mersenne31Field>>>,
) -> Vec<FieldElement<Mersenne31Field>> {
let mut stage1: Vec<FieldElement<Mersenne31Field>> = Vec::with_capacity(8);

stage1.push(input[0] + input[4]);
stage1.push(input[1] + input[5]);
stage1.push(input[2] + input[6]);
stage1.push(input[3] + input[7]);
stage1.push((input[0] - input[4]) * twiddles[0][0]);
stage1.push((input[1] - input[5]) * twiddles[0][1]);
stage1.push((input[2] - input[6]) * twiddles[0][2]);
stage1.push((input[3] - input[7]) * twiddles[0][3]);

let mut stage2: Vec<FieldElement<Mersenne31Field>> = Vec::with_capacity(8);

stage2.push(stage1[0] + stage1[2]);
stage2.push(stage1[1] + stage1[3]);
stage2.push((stage1[0] - stage1[2]) * twiddles[1][0]);
stage2.push((stage1[1] - stage1[3]) * twiddles[1][1]);

stage2.push(stage1[4] + stage1[6]);
stage2.push(stage1[5] + stage1[7]);
stage2.push((stage1[4] - stage1[6]) * twiddles[1][0]);
stage2.push((stage1[5] - stage1[7]) * twiddles[1][1]);

let mut stage3: Vec<FieldElement<Mersenne31Field>> = Vec::with_capacity(8);

stage3.push(stage2[0] + stage2[1]);
stage3.push((stage2[0] - stage2[1]) * twiddles[2][0]);

stage3.push(stage2[2] + stage2[3]);
stage3.push((stage2[2] - stage2[3]) * twiddles[2][0]);

stage3.push(stage2[4] + stage2[5]);
stage3.push((stage2[4] - stage2[5]) * twiddles[2][0]);

stage3.push(stage2[6] + stage2[7]);
stage3.push((stage2[6] - stage2[7]) * twiddles[2][0]);

let f = FieldElement::<Mersenne31Field>::from(8).inv().unwrap();
stage3.into_iter().map(|elem| elem * f).collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
70 changes: 3 additions & 67 deletions math/src/circle/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::{
};

use super::{
cfft::{cfft, cfft_4, cfft_8, icfft, order_cfft_result_naive, order_icfft_input_naive},
cfft::{cfft, icfft, order_cfft_result_naive, order_icfft_input_naive},
cosets::Coset,
twiddles::{
get_twiddles, get_twiddles_itnerpolation_4, get_twiddles_itnerpolation_8, TwiddlesConfig,
get_twiddles, TwiddlesConfig,
},
};

Expand All @@ -28,8 +28,7 @@ pub fn evaluate_cfft(
cfft(&mut coeff, twiddles);

// The cfft returns the evaluations in a certain order, so we permute them to get the natural order.
let result = order_cfft_result_naive(&mut coeff);
result
order_cfft_result_naive(&mut coeff)
}

/// Interpolates the 2^n evaluations of a two-variables polynomial of degree 2^n - 1 on the points of the standard coset of size 2^n.
Expand Down Expand Up @@ -59,28 +58,6 @@ pub fn interpolate_cfft(
eval_ordered.iter().map(|coef| coef * factor).collect()
}

pub fn interpolate_4(
mut eval: Vec<FieldElement<Mersenne31Field>>,
) -> Vec<FieldElement<Mersenne31Field>> {
let domain_log_2_size: u32 = eval.len().trailing_zeros();
let coset = Coset::new_standard(domain_log_2_size);
let twiddles = get_twiddles_itnerpolation_4(coset);

let res = cfft_4(&mut eval, twiddles);
res
}

pub fn interpolate_8(
mut eval: Vec<FieldElement<Mersenne31Field>>,
) -> Vec<FieldElement<Mersenne31Field>> {
let domain_log_2_size: u32 = eval.len().trailing_zeros();
let coset = Coset::new_standard(domain_log_2_size);
let twiddles = get_twiddles_itnerpolation_8(coset);

let res = cfft_8(&mut eval, twiddles);
res
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -225,47 +202,6 @@ mod tests {
assert_eq!(slice_result, expected_result);
}

#[test]
fn interpolation() {
let coeff = vec![
FE::from(1),
FE::from(2),
FE::from(3),
FE::from(4),
FE::from(5),
FE::from(6),
FE::from(7),
FE::from(8),
];

let evals = evaluate_cfft(coeff.clone());

// println!("EVALS: {:?}", evals);

// EVALS: [
// FieldElement { value: 885347334 }, -> 0
// FieldElement { value: 1037382257 }, -> 1
// FieldElement { value: 714723476 }, -> 2
// FieldElement { value: 55636419 }, -> 3
// FieldElement { value: 1262332919 }, -> 4
// FieldElement { value: 1109642644 }, -> 5
// FieldElement { value: 1432563561 }, -> 6
// FieldElement { value: 2092305986 }] -> 7

let new_evals = vec![
FE::from(885347334),
FE::from(714723476),
FE::from(1262332919),
FE::from(1432563561),
FE::from(2092305986),
FE::from(1109642644),
FE::from(55636419),
FE::from(1037382257),
];

let new_coeff = interpolate_8(new_evals);
}

#[test]
fn evaluate_and_interpolate_8_points_is_identity() {
// We define the 8 coefficients of a polynomial of degree 7.
Expand Down
28 changes: 1 addition & 27 deletions math/src/circle/twiddles.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate alloc;
use crate::{
circle::{cosets::Coset, point::CirclePoint},
fft::cpu::bit_reversing::in_place_bit_reverse_permute,
circle::cosets::Coset,
field::{element::FieldElement, fields::mersenne31::field::Mersenne31Field},
};
use alloc::vec::Vec;
Expand Down Expand Up @@ -56,31 +55,6 @@ pub fn get_twiddles(
twiddles
}

pub fn get_twiddles_itnerpolation_4(domain: Coset) -> Vec<Vec<FieldElement<Mersenne31Field>>> {
let half_domain_points = Coset::get_coset_points(&Coset::half_coset(domain.clone()));
let mut twiddles: Vec<Vec<FieldElement<Mersenne31Field>>> =
vec![half_domain_points.iter().map(|p| p.y).collect()];
twiddles.push(half_domain_points.iter().take(1).map(|p| p.x).collect());
twiddles.iter_mut().for_each(|x| {
FieldElement::<Mersenne31Field>::inplace_batch_inverse(x).unwrap();
});
twiddles
}

pub fn get_twiddles_itnerpolation_8(domain: Coset) -> Vec<Vec<FieldElement<Mersenne31Field>>> {
let half_domain_points = Coset::get_coset_points(&Coset::half_coset(domain.clone()));
let mut twiddles: Vec<Vec<FieldElement<Mersenne31Field>>> =
vec![half_domain_points.iter().map(|p| p.y).collect()];
twiddles.push(half_domain_points.iter().take(2).map(|p| p.x).collect());
twiddles.push(vec![
half_domain_points[0].x.square().double() - FieldElement::<Mersenne31Field>::one(),
]);
twiddles.iter_mut().for_each(|x| {
FieldElement::<Mersenne31Field>::inplace_batch_inverse(x).unwrap();
});
twiddles
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit da5fae7

Please sign in to comment.