Skip to content

Commit

Permalink
Merge pull request #24 from varunthakore/master
Browse files Browse the repository at this point in the history
Implement layers and FRI Prover
  • Loading branch information
varunthakore authored Sep 30, 2024
2 parents 598cad3 + 3a26bc8 commit 1d89996
Show file tree
Hide file tree
Showing 7 changed files with 1,116 additions and 18 deletions.
1 change: 1 addition & 0 deletions fri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde_json.workspace = true
serde.workspace = true
commitment_scheme = { path = "../commitment_scheme" }
anyhow.workspace = true
num-bigint.workspace = true

[dev-dependencies]
rand.workspace = true
101 changes: 101 additions & 0 deletions fri/src/committed_layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::{
details::next_layer_data_and_integrity_queries, layers::FriLayer, parameters::FriParameters,
};
use anyhow::Ok;
use ark_ff::PrimeField;
use ark_poly::EvaluationDomain;
use channel::fs_prover_channel::FSProverChannel;
use commitment_scheme::table_prover::TableProver;
use commitment_scheme::CommitmentHashes;
use randomness::Prng;
use sha3::Digest;
use std::sync::Arc;

#[allow(dead_code)]
pub struct FriCommittedLayer<F: PrimeField, E: EvaluationDomain<F>, P: Prng, W: Digest> {
fri_step: usize,
layer: Arc<dyn FriLayer<F, E>>,
params: FriParameters<F, E>,
layer_num: usize,
table_prover: TableProver<F, P, W>,
}

#[allow(dead_code)]
impl<
F: PrimeField,
E: EvaluationDomain<F>,
P: Prng + Clone + 'static,
W: Digest + Clone + 'static,
> FriCommittedLayer<F, E, P, W>
{
#[allow(clippy::too_many_arguments)]
pub fn new(
fri_step: usize,
layer: Arc<dyn FriLayer<F, E>>,
params: FriParameters<F, E>,
layer_num: usize,
field_element_size_in_bytes: usize,
n_verifier_friendly_commitment_layers: usize,
commitment_hashes: CommitmentHashes,
channel: &mut FSProverChannel<F, P, W>,
mont_r: F,
) -> Self {
let layer_size = layer.get_layer_size();

let mut table_prover = TableProver::new(
1,
layer_size / (1 << fri_step),
1 << fri_step,
field_element_size_in_bytes,
n_verifier_friendly_commitment_layers,
commitment_hashes,
mont_r,
);

let segment = layer.get_layer().unwrap();
table_prover.add_segment_for_commitment(&[segment], 0, 1 << fri_step);
table_prover.commit(channel).unwrap();

Self {
fri_step,
layer,
params,
layer_num,
table_prover,
}
}

pub fn eval_at_points(&self, required_row_indices: &[usize]) -> Vec<Vec<F>> {
let coset_size = 1 << self.params.fri_step_list[self.layer_num];
let mut elements_data_vectors = Vec::with_capacity(coset_size);

for col in 0..coset_size {
let mut required_indices: Vec<usize> = Vec::new();

for &row in required_row_indices {
required_indices.push(row * coset_size + col);
}

let eval_result = self.layer.eval_at_points(&required_indices);
elements_data_vectors.push(eval_result);
}

elements_data_vectors
}

pub fn decommit(
&mut self,
queries: &[u64],
channel: &mut FSProverChannel<F, P, W>,
) -> Result<(), anyhow::Error> {
let (data_queries, integrity_queries) =
next_layer_data_and_integrity_queries(&self.params, queries, self.layer_num);
let required_row_indices = self
.table_prover
.start_decommitment_phase(data_queries.clone(), integrity_queries.clone());

let elements_data = self.eval_at_points(&required_row_indices);
self.table_prover.decommit(channel, &elements_data)?;
Ok(())
}
}
8 changes: 4 additions & 4 deletions fri/src/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::stone_domain::get_field_element_at_index;
use anyhow::Error;
use ark_ff::fields::batch_inversion;
use ark_ff::{FftField, PrimeField};
use ark_poly::Radix2EvaluationDomain;
use ark_poly::EvaluationDomain;
use std::sync::Arc;

pub struct MultiplicativeFriFolder;
Expand All @@ -11,13 +11,13 @@ pub struct MultiplicativeFriFolder;
impl MultiplicativeFriFolder {
// Computes the values of the next FRI layer given the values and domain of the current layer.
pub fn compute_next_fri_layer<F: FftField + PrimeField>(
domain: Radix2EvaluationDomain<F>,
domain: impl EvaluationDomain<F>,
input_layer: &[F],
eval_point: F,
) -> Result<Vec<F>, Error> {
assert_eq!(input_layer.len(), domain.size as usize);
assert_eq!(input_layer.len(), domain.size());

let mut elements: Vec<F> = (0..domain.size as usize)
let mut elements: Vec<F> = (0..domain.size())
.map(|i| get_field_element_at_index(&domain, i))
.collect();
batch_inversion(&mut elements);
Expand Down
Loading

0 comments on commit 1d89996

Please sign in to comment.