Skip to content

Commit

Permalink
Evaluate and interpolate functions have non-mutable inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole authored and Nicole committed Oct 30, 2024
1 parent 3ed8ac1 commit a7161f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion math/src/circle/cfft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn icfft(
/// This function permutes the slice [0, 2, 4, 6, 7, 5, 3, 1] into [0, 1, 2, 3, 4, 5, 6, 7].
/// TODO: This can be optimized by performing in-place value swapping (WIP).
pub fn order_cfft_result_naive(
input: &mut [FieldElement<Mersenne31Field>],
input: &[FieldElement<Mersenne31Field>],
) -> Vec<FieldElement<Mersenne31Field>> {
let mut result = Vec::new();
let length = input.len();
Expand Down
19 changes: 12 additions & 7 deletions math/src/circle/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use alloc::vec::Vec;
/// Note that coeff has to be a vector with length a power of two 2^n.
#[cfg(feature = "alloc")]
pub fn evaluate_cfft(
mut coeff: Vec<FieldElement<Mersenne31Field>>,
coeff: Vec<FieldElement<Mersenne31Field>>,
) -> Vec<FieldElement<Mersenne31Field>> {
let mut coeff = coeff;

// We get the twiddles for the Evaluation.
let domain_log_2_size: u32 = coeff.len().trailing_zeros();
let coset = Coset::new_standard(domain_log_2_size);
Expand All @@ -29,19 +31,21 @@ 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.
order_cfft_result_naive(&mut coeff)
order_cfft_result_naive(&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.
/// As a result we obtain the coefficients of the polynomial in the basis: {1, y, x, xy, 2xˆ2 -1, 2xˆ2y-y, 2xˆ3-x, 2xˆ3y-xy,...}
/// Note that eval has to be a vector of length a power of two 2^n.
/// If the vector of evaluations is empty, it returns an empty vector.
#[cfg(feature = "alloc")]
pub fn interpolate_cfft(
mut eval: Vec<FieldElement<Mersenne31Field>>,
eval: Vec<FieldElement<Mersenne31Field>>,
) -> Vec<FieldElement<Mersenne31Field>> {
if eval.len() == 0 {
let mut poly = Vec::new();
poly.push(FieldElement::<Mersenne31Field>::zero());
let mut eval = eval;

if eval.is_empty() {
let poly: Vec<FieldElement<Mersenne31Field>> = Vec::new();
return poly;
}

Expand Down Expand Up @@ -135,8 +139,9 @@ mod tests {
expected_result.push(point_eval);
}

let input_vec = input.to_vec();
// We evaluate the polynomial using now the cfft.
let result = evaluate_cfft(input.to_vec());
let result = evaluate_cfft(input_vec);
let slice_result: &[FE] = &result;

assert_eq!(slice_result, expected_result);
Expand Down

0 comments on commit a7161f2

Please sign in to comment.