diff --git a/fri/src/details.rs b/fri/src/details.rs index a1f3792..e79ad9f 100644 --- a/fri/src/details.rs +++ b/fri/src/details.rs @@ -4,6 +4,7 @@ use ark_ff::{FftField, PrimeField}; use ark_poly::EvaluationDomain; use channel::FSChannel; use commitment_scheme::table_utils::RowCol; +use felt::felt_252_to_hex; use sha3::Digest; use crate::stone_domain::get_field_element_at_index; @@ -26,13 +27,21 @@ pub fn second_layer_queries_to_first_layer_queries( first_layer_queries } -pub fn apply_fri_layers>( +pub fn apply_fri_layers>( elements: &[F], eval_point: Option, params: &FriParameters, layer_num: usize, mut first_element_index: usize, ) -> F { + // prints elements by looping over them + for (i, element) in elements.iter().enumerate() { + println!("elements[{i}]: {:?}", felt_252_to_hex(element)); + } + + // prints eval_point + println!("eval_point: {:?}", eval_point.map(|e| felt_252_to_hex(&e))); + let mut curr_eval_point = eval_point; let mut cumulative_fri_step = 0; for i in 0..layer_num { @@ -56,14 +65,13 @@ pub fn apply_fri_layers>( let mut next_layer = Vec::with_capacity(cur_layer.len() / 2); for j in (0..cur_layer.len()).step_by(2) { - next_layer.push( - MultiplicativeFriFolder::next_layer_element_from_two_previous_layer_elements( - &cur_layer[j], - &cur_layer[j + 1], - &curr_eval_point.unwrap(), - &get_field_element_at_index(&fft_domain, first_element_index + j), - ), + let res = MultiplicativeFriFolder::next_layer_element_from_two_previous_layer_elements( + &cur_layer[j], + &cur_layer[j + 1], + &curr_eval_point.unwrap(), + &get_field_element_at_index(&fft_domain, first_element_index + j), ); + next_layer.push(res); } cur_layer = next_layer; diff --git a/fri/src/folder.rs b/fri/src/folder.rs index 3810578..19d9641 100644 --- a/fri/src/folder.rs +++ b/fri/src/folder.rs @@ -1,20 +1,21 @@ -use ark_ff::FftField; +use ark_ff::{FftField, PrimeField}; use std::sync::Arc; pub struct MultiplicativeFriFolder; #[allow(dead_code)] impl MultiplicativeFriFolder { - pub fn next_layer_element_from_two_previous_layer_elements( + pub fn next_layer_element_from_two_previous_layer_elements( f_x: &F, f_minus_x: &F, eval_point: &F, - x_inv: &F, + x: &F, ) -> F { - Self::fold(f_x, f_minus_x, eval_point, x_inv) + let x_inv = x.inverse().unwrap(); + Self::fold(f_x, f_minus_x, eval_point, &x_inv) } - fn fold(f_x: &F, f_minus_x: &F, eval_point: &F, x_inv: &F) -> F { + fn fold(f_x: &F, f_minus_x: &F, eval_point: &F, x_inv: &F) -> F { *f_x + *f_minus_x + *eval_point * (*f_x - *f_minus_x) * *x_inv } } diff --git a/fri/src/stone_domain.rs b/fri/src/stone_domain.rs index d241ddf..151cf5d 100644 --- a/fri/src/stone_domain.rs +++ b/fri/src/stone_domain.rs @@ -21,9 +21,10 @@ pub fn get_field_element_at_index>( // change order of elements in domain pub fn change_order_of_elements_in_domain(elements: &[F]) -> Vec { - assert!(elements.len().is_power_of_two()); - let mut new_elements = vec![F::zero(); elements.len()]; - let log_len = elements.len().trailing_zeros(); + // get smallest power of two that is greater than elements.len() + let size = elements.len().next_power_of_two(); + let mut new_elements = vec![F::zero(); size]; + let log_len = size.trailing_zeros(); let mapping_vec = (0..log_len) .map(|i| (1 << (log_len - 1 - i))) .collect::>(); diff --git a/fri/src/verifier.rs b/fri/src/verifier.rs index e1d0223..25928f0 100644 --- a/fri/src/verifier.rs +++ b/fri/src/verifier.rs @@ -2,6 +2,7 @@ use std::error::Error; use ark_ff::{FftField, PrimeField}; use ark_poly::{domain::EvaluationDomain, Radix2EvaluationDomain}; +use felt::felt_252_to_hex; use randomness::Prng; use sha3::Digest; @@ -31,7 +32,7 @@ pub struct FriVerifier< channel: FSVerifierChannel, params: FriParameters>, commitment_hashes: CommitmentHashes, - first_layer_callback: FirstLayerQueriesCallback, + first_layer_callback: Box Vec>, n_layers: usize, first_eval_point: Option, eval_points: Vec, @@ -45,19 +46,22 @@ pub struct FriVerifier< impl FriVerifier { - pub fn new( + pub fn new( channel: FSVerifierChannel, params: FriParameters>, commitment_hashes: CommitmentHashes, - first_layer_callback: FirstLayerQueriesCallback, - ) -> Self { + first_layer_callback: C, + ) -> Self + where + C: Fn(&[u64]) -> Vec + 'static, + { let n_queries = params.n_queries; let n_layers = params.fri_step_list.len(); Self { channel, params, commitment_hashes, - first_layer_callback, + first_layer_callback: Box::new(first_layer_callback), n_layers, first_eval_point: None, eval_points: vec![F::zero(); n_layers - 1], @@ -73,8 +77,7 @@ impl