Implement commit row commitments

This commit is contained in:
Daniel Sanchez Quiros 2024-04-11 18:12:28 +03:00
parent 4653fa3319
commit 9b2d723fd9
8 changed files with 60 additions and 28 deletions

View File

@ -6,4 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
ark-poly = "0.4.2"
kzgrs = { path = "../kzgrs" } kzgrs = { path = "../kzgrs" }
rand = "0.8.5"
once_cell = "1.19"

View File

@ -45,6 +45,9 @@ impl FromIterator<Chunk> for Column {
} }
impl ChunksMatrix { impl ChunksMatrix {
pub fn rows(&self) -> impl Iterator<Item = &Row> + '_ {
self.0.iter()
}
pub fn columns(&self) -> impl Iterator<Item = Column> + '_ { pub fn columns(&self) -> impl Iterator<Item = Column> + '_ {
let size = self.0.first().map(|r| r.0.len()).unwrap_or(0); let size = self.0.first().map(|r| r.0.len()).unwrap_or(0);
(0..size).map(|i| { (0..size).map(|i| {

View File

@ -1,19 +1,18 @@
use crate::common::{Chunk, ChunksMatrix}; 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 { pub struct DaEncoderParams {
column_count: usize, column_count: usize,
bytes_per_chunk: usize,
} }
impl DaEncoderParams { impl DaEncoderParams {
const MAX_BLS12_381_ENCODING_CHUNK_SIZE: usize = 31; const MAX_BLS12_381_ENCODING_CHUNK_SIZE: usize = 31;
const fn default_with(column_count: usize) -> Self { const fn default_with(column_count: usize) -> Self {
Self { Self { column_count }
column_count,
bytes_per_chunk: Self::MAX_BLS12_381_ENCODING_CHUNK_SIZE,
}
} }
} }
@ -34,18 +33,32 @@ struct DaEncoder {
impl DaEncoder { impl DaEncoder {
pub const fn new(settings: DaEncoderParams) -> Self { pub const fn new(settings: DaEncoderParams) -> Self {
assert!(settings.bytes_per_chunk < BYTES_PER_FIELD_ELEMENT);
Self { params: settings } Self { params: settings }
} }
fn chunkify(&self, data: &[u8]) -> ChunksMatrix { 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) data.windows(size)
.map(|d| { .map(|d| {
d.windows(self.params.bytes_per_chunk) d.windows(DaEncoderParams::MAX_BLS12_381_ENCODING_CHUNK_SIZE)
.map(Chunk::from) .map(Chunk::from)
.collect() .collect()
}) })
.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()
}
} }

View File

@ -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<GlobalParameters> = Lazy::new(|| {
let mut rng = rand::thread_rng();
global_parameters_from_randomness(&mut rng)
});
pub static DOMAIN: Lazy<PolynomialEvaluationDomain> =
Lazy::new(|| PolynomialEvaluationDomain::new(8192).unwrap());

View File

@ -1,17 +1,3 @@
mod common; mod common;
mod encoder; mod encoder;
mod global;
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);
}
}

View File

@ -17,6 +17,4 @@ ark-serialize = { version = "0.4" }
num-bigint = "0.4.4" num-bigint = "0.4.4"
thiserror = "1.0.58" thiserror = "1.0.58"
num-traits = "0.2.18" num-traits = "0.2.18"
[dev-dependencies]
rand = "0.8.5" rand = "0.8.5"

View File

@ -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<R: Rng>(rng: &mut R) -> GlobalParameters {
KZG10::<Bls12_381, DensePolynomial<Fr>>::setup(8192, true, rng).unwrap()
}

View File

@ -1,17 +1,26 @@
pub mod common; pub mod common;
mod global_parameters;
pub mod kzg; pub mod kzg;
pub mod rs; pub mod rs;
use ark_bls12_381::{Bls12_381, Fr}; 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::kzg10;
use ark_poly_commit::sonic_pc::UniversalParams;
use std::mem; use std::mem;
pub use common::{bytes_to_evaluations, bytes_to_polynomial, KzgRsError}; 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 kzg::{commit_polynomial, generate_element_proof, verify_element_proof};
pub use rs::{decode, encode}; pub use rs::{decode, encode};
pub type Commitment = kzg10::Commitment<Bls12_381>; pub type Commitment = kzg10::Commitment<Bls12_381>;
pub type Proof = kzg10::Proof<Bls12_381>; pub type Proof = kzg10::Proof<Bls12_381>;
pub type FieldElement = ark_bls12_381::Fr; pub type FieldElement = ark_bls12_381::Fr;
pub type Polynomial = DensePolynomial<Fr>;
pub type PolynomialEvaluationDomain = GeneralEvaluationDomain<Fr>;
pub type GlobalParameters = UniversalParams<Bls12_381>;
pub const BYTES_PER_FIELD_ELEMENT: usize = mem::size_of::<Fr>(); pub const BYTES_PER_FIELD_ELEMENT: usize = mem::size_of::<Fr>();