From 7460c93171e663a905cda4422e3c45a05250804e Mon Sep 17 00:00:00 2001
From: "a.stevan" <antoine.stevan@isae-supaero.fr>
Date: Tue, 10 Sep 2024 13:28:05 +0200
Subject: [PATCH] WIP

---
 Cargo.toml |  2 ++
 src/fri.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib.rs |  2 ++
 3 files changed, 96 insertions(+)
 create mode 100644 src/fri.rs

diff --git a/Cargo.toml b/Cargo.toml
index 69c51fb1..690060b9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,6 +25,7 @@ thiserror = "1.0.50"
 tracing = "0.1.40"
 tracing-subscriber = "0.3.17"
 ark-poly-commit = { git = "https://gitlab.isae-supaero.fr/a.stevan/poly-commit", version = "0.4.0", rev = "19fc0d4", optional = true }
+fri = { path = "../fri", optional = true}
 
 [workspace]
 members = [
@@ -41,6 +42,7 @@ rand = "0.8.5"
 [features]
 kzg = ["dep:ark-poly-commit"]
 aplonk = ["dep:ark-poly-commit"]
+fri = ["dep:fri"]
 fs = []
 
 [package.metadata.docs.rs]
diff --git a/src/fri.rs b/src/fri.rs
new file mode 100644
index 00000000..2fd2c118
--- /dev/null
+++ b/src/fri.rs
@@ -0,0 +1,92 @@
+use ark_ff::PrimeField;
+use ark_poly::DenseUVPolynomial;
+use ark_std::{
+    ops::Div,
+    rand::{thread_rng, Rng},
+};
+use rs_merkle::Hasher;
+use tracing::{debug, info};
+
+use crate::{algebra, error::KomodoError, fec};
+use fri::{
+    frida::{FridaBuilder, FridaCommitment},
+    rng::FriChallenger,
+    utils::{to_evaluations, MerkleProof},
+};
+
+/// representation of a block of proven data.
+///
+/// this is a wrapper around a [`fec::Shard`] with some additional cryptographic
+/// information that allows to prove the integrity of said shard.
+#[derive(Clone, PartialEq)]
+pub struct Block<F: PrimeField, H: Hasher> {
+    pub shard: fec::Shard<F>,
+    proof: MerkleProof<H>,
+    commit: FridaCommitment<F, H>,
+}
+
+pub fn prove<const N: usize, F: PrimeField, H: Hasher, P>(
+    bytes: &[u8],
+    shards: Vec<fec::Shard<F>>,
+    k: usize,
+    domain_size: usize,
+    blowup_factor: usize,
+    remainder_plus_one: usize,
+    nb_queries: usize,
+) -> Result<Vec<Block<F, H>>, KomodoError>
+where
+    P: DenseUVPolynomial<F>,
+    for<'a, 'b> &'a P: Div<&'b P, Output = P>,
+    <H as rs_merkle::Hasher>::Hash: AsRef<[u8]>,
+{
+    info!("encoding and proving {} bytes", bytes.len());
+
+    debug!("splitting bytes into rows");
+    let elements: Vec<F> = algebra::split_data_into_field_elements(bytes, k);
+    let rows = elements.chunks(k).map(|c| c.to_vec()).collect::<Vec<_>>();
+    info!(
+        "data is composed of {} rows and {} elements",
+        rows.len(),
+        elements.len()
+    );
+
+    let evaluations = rows
+        .into_iter()
+        .map(|r| to_evaluations(r, domain_size))
+        .collect::<Vec<_>>();
+
+    let builder = FridaBuilder::<F, H>::new::<N, _>(
+        &evaluations,
+        FriChallenger::<H>::default(),
+        blowup_factor,
+        remainder_plus_one,
+        nb_queries,
+    );
+
+    let mut rng = thread_rng();
+    let position = rng.gen_range(0..domain_size);
+
+    let proof = builder.prove_shards(&[position]);
+
+    //let commit = FridaCommitment::from(builder);
+    //
+    //commit
+    //    .verify::<N, _>(
+    //        FriChallenger::<H>::default(),
+    //        params.nb_queries,
+    //        params.nb_coeffs,
+    //        params.domain_size,
+    //    )
+    //    .unwrap();
+    //
+    //assert!(proof.verify(
+    //    commit.tree_root(),
+    //    &[position],
+    //    &[H::hash_item(
+    //        &nth_evaluations(&evaluations, position).collect::<Vec<_>>()
+    //    )],
+    //    params.domain_size
+    //));
+
+    Ok(vec![])
+}
diff --git a/src/lib.rs b/src/lib.rs
index 8b69d6a2..c4c0690e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -51,6 +51,8 @@ pub mod aplonk;
 mod conversions;
 pub mod error;
 pub mod fec;
+#[cfg(feature = "fri")]
+pub mod fri;
 #[cfg(feature = "fs")]
 pub mod fs;
 #[cfg(feature = "kzg")]
-- 
GitLab