diff --git a/nomos-da/kzgrs-backend/Cargo.toml b/nomos-da/kzgrs-backend/Cargo.toml index a6cf4407..82dffc88 100644 --- a/nomos-da/kzgrs-backend/Cargo.toml +++ b/nomos-da/kzgrs-backend/Cargo.toml @@ -6,4 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -kzgrs = { path = "../kzgrs" } \ No newline at end of file +ark-poly = "0.4.2" +kzgrs = { path = "../kzgrs" } +rand = "0.8.5" +once_cell = "1.19" \ No newline at end of file diff --git a/nomos-da/kzgrs-backend/src/common.rs b/nomos-da/kzgrs-backend/src/common.rs index 6986fe81..5732ea04 100644 --- a/nomos-da/kzgrs-backend/src/common.rs +++ b/nomos-da/kzgrs-backend/src/common.rs @@ -45,6 +45,9 @@ impl FromIterator for Column { } impl ChunksMatrix { + pub fn rows(&self) -> impl Iterator + '_ { + self.0.iter() + } pub fn columns(&self) -> impl Iterator + '_ { let size = self.0.first().map(|r| r.0.len()).unwrap_or(0); (0..size).map(|i| { diff --git a/nomos-da/kzgrs-backend/src/encoder.rs b/nomos-da/kzgrs-backend/src/encoder.rs index 567afc45..40cb2d41 100644 --- a/nomos-da/kzgrs-backend/src/encoder.rs +++ b/nomos-da/kzgrs-backend/src/encoder.rs @@ -1,19 +1,18 @@ use crate::common::{Chunk, ChunksMatrix}; -use kzgrs::{Commitment, Proof, BYTES_PER_FIELD_ELEMENT}; - +use crate::global::{DOMAIN, GLOBAL_PARAMETERS}; +use kzgrs::{ + bytes_to_polynomial, commit_polynomial, Commitment, Polynomial, PolynomialEvaluationDomain, + Proof, BYTES_PER_FIELD_ELEMENT, +}; pub struct DaEncoderParams { column_count: usize, - bytes_per_chunk: usize, } impl DaEncoderParams { const MAX_BLS12_381_ENCODING_CHUNK_SIZE: usize = 31; const fn default_with(column_count: usize) -> Self { - Self { - column_count, - bytes_per_chunk: Self::MAX_BLS12_381_ENCODING_CHUNK_SIZE, - } + Self { column_count } } } @@ -34,18 +33,32 @@ struct DaEncoder { impl DaEncoder { pub const fn new(settings: DaEncoderParams) -> Self { - assert!(settings.bytes_per_chunk < BYTES_PER_FIELD_ELEMENT); Self { params: settings } } fn chunkify(&self, data: &[u8]) -> ChunksMatrix { - let size = self.params.column_count * self.params.bytes_per_chunk; + let size = self.params.column_count * DaEncoderParams::MAX_BLS12_381_ENCODING_CHUNK_SIZE; data.windows(size) .map(|d| { - d.windows(self.params.bytes_per_chunk) + d.windows(DaEncoderParams::MAX_BLS12_381_ENCODING_CHUNK_SIZE) .map(Chunk::from) .collect() }) .collect() } + + fn compute_kzg_row_commitments(matrix: ChunksMatrix) -> Vec<(Polynomial, Commitment)> { + matrix + .rows() + .map(|r| { + let (_, poly) = bytes_to_polynomial::< + { DaEncoderParams::MAX_BLS12_381_ENCODING_CHUNK_SIZE }, + >(r.as_bytes().as_ref(), *DOMAIN) + .unwrap(); + + let commitment = commit_polynomial(&poly, &GLOBAL_PARAMETERS).unwrap(); + (poly, commitment) + }) + .collect() + } } diff --git a/nomos-da/kzgrs-backend/src/global.rs b/nomos-da/kzgrs-backend/src/global.rs new file mode 100644 index 00000000..a0695fa6 --- /dev/null +++ b/nomos-da/kzgrs-backend/src/global.rs @@ -0,0 +1,11 @@ +use ark_poly::EvaluationDomain; +use kzgrs::{global_parameters_from_randomness, GlobalParameters, PolynomialEvaluationDomain}; +use once_cell::sync::Lazy; + +pub static GLOBAL_PARAMETERS: Lazy = Lazy::new(|| { + let mut rng = rand::thread_rng(); + global_parameters_from_randomness(&mut rng) +}); + +pub static DOMAIN: Lazy = + Lazy::new(|| PolynomialEvaluationDomain::new(8192).unwrap()); diff --git a/nomos-da/kzgrs-backend/src/lib.rs b/nomos-da/kzgrs-backend/src/lib.rs index 0b185639..ac01ae55 100644 --- a/nomos-da/kzgrs-backend/src/lib.rs +++ b/nomos-da/kzgrs-backend/src/lib.rs @@ -1,17 +1,3 @@ mod common; mod encoder; - -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +mod global; diff --git a/nomos-da/kzgrs/Cargo.toml b/nomos-da/kzgrs/Cargo.toml index de81d8e2..cbdafe9c 100644 --- a/nomos-da/kzgrs/Cargo.toml +++ b/nomos-da/kzgrs/Cargo.toml @@ -17,6 +17,4 @@ ark-serialize = { version = "0.4" } num-bigint = "0.4.4" thiserror = "1.0.58" num-traits = "0.2.18" - -[dev-dependencies] rand = "0.8.5" \ No newline at end of file diff --git a/nomos-da/kzgrs/src/global_parameters.rs b/nomos-da/kzgrs/src/global_parameters.rs new file mode 100644 index 00000000..417e4120 --- /dev/null +++ b/nomos-da/kzgrs/src/global_parameters.rs @@ -0,0 +1,9 @@ +use super::GlobalParameters; +use ark_bls12_381::{fr::Fr, Bls12_381}; +use ark_poly::polynomial::univariate::DensePolynomial; +use ark_poly_commit::kzg10::KZG10; +use rand::Rng; + +pub fn global_parameters_from_randomness(rng: &mut R) -> GlobalParameters { + KZG10::>::setup(8192, true, rng).unwrap() +} diff --git a/nomos-da/kzgrs/src/lib.rs b/nomos-da/kzgrs/src/lib.rs index 49521bf4..f4b69207 100644 --- a/nomos-da/kzgrs/src/lib.rs +++ b/nomos-da/kzgrs/src/lib.rs @@ -1,17 +1,26 @@ pub mod common; +mod global_parameters; pub mod kzg; pub mod rs; use ark_bls12_381::{Bls12_381, Fr}; +use ark_poly::univariate::DensePolynomial; +use ark_poly::GeneralEvaluationDomain; use ark_poly_commit::kzg10; +use ark_poly_commit::sonic_pc::UniversalParams; use std::mem; pub use common::{bytes_to_evaluations, bytes_to_polynomial, KzgRsError}; +pub use global_parameters::global_parameters_from_randomness; pub use kzg::{commit_polynomial, generate_element_proof, verify_element_proof}; pub use rs::{decode, encode}; pub type Commitment = kzg10::Commitment; pub type Proof = kzg10::Proof; pub type FieldElement = ark_bls12_381::Fr; +pub type Polynomial = DensePolynomial; +pub type PolynomialEvaluationDomain = GeneralEvaluationDomain; + +pub type GlobalParameters = UniversalParams; pub const BYTES_PER_FIELD_ELEMENT: usize = mem::size_of::();