mirror of
https://github.com/logos-storage/dynamic-data-experiments.git
synced 2026-01-02 13:13:08 +00:00
add commitment tests
This commit is contained in:
parent
7242c37107
commit
2610b41834
178
src/test.rs
178
src/test.rs
@ -2,11 +2,12 @@
|
||||
mod tests {
|
||||
use crate::byte_data::{Data, Params};
|
||||
use ark_poly::{EvaluationDomain};
|
||||
use crate::kzg::{F, KZGPolyComm};
|
||||
use crate::kzg10::{E, F, get_vk, KZG10PolyComm};
|
||||
use crate::field_matrix::Matrix;
|
||||
use ark_poly_commit::{Polynomial};
|
||||
use ark_poly_commit::kzg10::Commitment;
|
||||
use crate::encoder::{BLSEncoder, BLSFieldEncoder, G8Encoder};
|
||||
use crate::traits::{DataMatrix, Encoder, PolynomialCommitmentScheme};
|
||||
use crate::matrix_commit::MatrixPolyComm;
|
||||
use crate::traits::{DataMatrix, Encoder, PolyCommScheme, MatrixPolyCommScheme, CommitOutputTrait, SRSTrait};
|
||||
|
||||
#[test]
|
||||
fn test_encode_columns() {
|
||||
@ -165,32 +166,22 @@ mod tests {
|
||||
// make a random n×m matrix
|
||||
let matrix = Matrix::from_data(&data);
|
||||
|
||||
// new kzg
|
||||
let kzg = KZGPolyComm::new(params);
|
||||
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||
let degree = m;
|
||||
// setup kzg
|
||||
let srs = kzg.setup().expect("setup should succeed");
|
||||
type P = KZG10PolyComm;
|
||||
type C = MatrixPolyComm<F,P>;
|
||||
let srs = C::setup(degree).expect("setup should succeed");
|
||||
|
||||
// commit to its rows
|
||||
let kzg_comm = kzg.commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
let kzg_comm = C::commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
|
||||
assert_eq!(kzg_comm.comm_output.len(), m);
|
||||
|
||||
let (row_polys, commitments, randomness) =
|
||||
kzg_comm.get_refs();
|
||||
|
||||
// we produced exactly one polynomial, one comm, one rand per column
|
||||
assert_eq!(row_polys.len(), m);
|
||||
assert_eq!(commitments.len(), m);
|
||||
assert_eq!(randomness.len(), m);
|
||||
|
||||
// check that each polynomial really interpolates its original rows
|
||||
for (i, poly) in row_polys.iter().enumerate() {
|
||||
// check that each polynomial is really the original rows
|
||||
for i in 0..m {
|
||||
let row = matrix.get_row(i).unwrap();
|
||||
// evaluate poly at each domain point and collect
|
||||
let evals: Vec<_> = srs
|
||||
.ploycommit_domain
|
||||
.elements()
|
||||
.map(|x| poly.polynomial().evaluate(&x))
|
||||
.collect();
|
||||
let evals: Vec<_> = kzg_comm.get_poly(i).coeffs.clone();
|
||||
assert_eq!(evals, row);
|
||||
}
|
||||
}
|
||||
@ -214,22 +205,29 @@ mod tests {
|
||||
// make a random n×m matrix
|
||||
let matrix = Matrix::from_data(&data);
|
||||
|
||||
// new kzg
|
||||
let kzg = KZGPolyComm::new(params);
|
||||
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||
let degree = m;
|
||||
// setup kzg
|
||||
let srs = kzg.setup().expect("setup should succeed");
|
||||
type P = KZG10PolyComm;
|
||||
type C = MatrixPolyComm<F,P>;
|
||||
let srs = C::setup(degree).expect("setup should succeed");
|
||||
|
||||
// commit to its rows
|
||||
let kzg_comm = kzg.commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
// check all cells
|
||||
let kzg_comm = C::commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
|
||||
// verifier Part
|
||||
let vk = get_vk(&srs.pp).unwrap();
|
||||
let verifier_comms: Vec<Commitment<E>> = kzg_comm.comm_output.iter().map(|c|c.get_comm().clone()).collect();
|
||||
|
||||
// check all domain points
|
||||
for row in 0..n {
|
||||
for col in 0..m {
|
||||
let proof = KZGPolyComm::open(&kzg_comm, &srs, row, col)
|
||||
let proof = C::open(&kzg_comm, &srs, row, col)
|
||||
.expect("open should succeed");
|
||||
let expected: F = matrix.elms[row][col].clone();
|
||||
|
||||
let point = srs.get_domain_element(col);
|
||||
assert!(
|
||||
KZGPolyComm::verify(&kzg_comm, &srs, row, col, expected, &proof)
|
||||
C::verify(&vk, &verifier_comms[row], point, expected, &proof)
|
||||
.expect("verify should succeed"),
|
||||
"KZG open/verify failed for row={}, col={}",
|
||||
row,
|
||||
@ -265,7 +263,7 @@ mod tests {
|
||||
.collect();
|
||||
|
||||
// apply update
|
||||
data.update_col(c, &new_col);
|
||||
data.update_col(c, &new_col).expect("update col");
|
||||
println!("data after update:");
|
||||
data.pretty_print();
|
||||
|
||||
@ -278,13 +276,64 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
let _coded_row = G8Encoder::encode_col(&mut data, c).unwrap();
|
||||
G8Encoder::encode_col(&mut data, c).expect("encode col");
|
||||
println!("data after encoding update:");
|
||||
data.pretty_print();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_commitments() -> anyhow::Result<()> {
|
||||
fn test_kzg10_update_commitments() {
|
||||
// dimensions: 8 rows (4 parity), 8 columns
|
||||
let n = 8;
|
||||
let k = 4;
|
||||
let m = 8;
|
||||
|
||||
// generate Data with random content
|
||||
let params = Params {
|
||||
k,
|
||||
n,
|
||||
m,
|
||||
};
|
||||
// original
|
||||
let mut data = Data::new_random(params.clone());
|
||||
G8Encoder::encode(&mut data).expect("encode failed");
|
||||
|
||||
// Build a matrix where entry (i,j) = i * m + j
|
||||
let matrix = Matrix::<F>::from_data(&data);
|
||||
|
||||
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||
let degree = m;
|
||||
// setup kzg
|
||||
type P = KZG10PolyComm;
|
||||
let srs = P::setup(degree).expect("setup should succeed");
|
||||
let mut row = matrix.get_row(0).expect("get row");
|
||||
let mut com = P::commit(&srs, row.clone()).expect("commit");
|
||||
|
||||
// Verify that row polynomial coeffs are the row data
|
||||
for i in 0..m {
|
||||
let row_elem = row[i].clone();
|
||||
let eval = com.poly.coeffs[i].clone();
|
||||
assert_eq!(eval, row_elem);
|
||||
}
|
||||
|
||||
let cell = row[0].clone();
|
||||
let new_cell = cell + F::from(10u64);
|
||||
|
||||
P::update_commitment(&srs,&mut com,cell,new_cell.clone(),0).expect("update comm");
|
||||
|
||||
let eval = com.poly.coeffs[0].clone();
|
||||
assert_eq!(eval, new_cell);
|
||||
|
||||
row[0] = new_cell;
|
||||
|
||||
let new_com = P::commit(&srs, row.clone()).expect("commit");
|
||||
|
||||
assert_eq!(com.comm, new_com.comm);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_commitments() {
|
||||
// dimensions: 8 rows (4 parity), 8 columns
|
||||
let n = 8;
|
||||
let k = 4;
|
||||
@ -302,56 +351,57 @@ mod tests {
|
||||
|
||||
// Build a matrix where entry (i,j) = i * m + j
|
||||
let mut matrix = Matrix::<F>::from_data(&data);
|
||||
println!("---------- original ------------");
|
||||
matrix.pretty_print();
|
||||
|
||||
// new kzg
|
||||
let kzg = KZGPolyComm::new(params);
|
||||
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||
let degree = m;
|
||||
// setup kzg
|
||||
let srs = kzg.setup().expect("setup should succeed");
|
||||
type P = KZG10PolyComm;
|
||||
type C = MatrixPolyComm<F,P>;
|
||||
let srs = C::setup(degree).expect("setup should succeed");
|
||||
|
||||
// commit to its rows
|
||||
let mut kzg_comm = kzg.commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
let mut kzg_comm = C::commit(&srs, &matrix).expect("commit_rows should succeed");
|
||||
|
||||
// a row to update
|
||||
let row_idx = 1;
|
||||
let old_row = matrix.get_row(row_idx)?;
|
||||
// a column to update
|
||||
let col_idx = 1;
|
||||
let old_col = matrix.get_col(col_idx).expect("get old col");
|
||||
|
||||
// a new row by adding a constant to each element
|
||||
let new_row: Vec<_> = old_row.iter()
|
||||
.map(|v| *v + F::from(10u64))
|
||||
let new_col_data: Vec<_> = old_col
|
||||
.iter()
|
||||
.take(k) // only look at the first k entries
|
||||
.map(|v| *v + F::from(10u64)) // then do your +10
|
||||
.collect();
|
||||
|
||||
// Apply the change to the in-memory matrix
|
||||
{
|
||||
let row_slice = matrix.get_row_mut(row_idx)?;
|
||||
for (j, val) in new_row.iter().enumerate() {
|
||||
row_slice[j] = *val;
|
||||
}
|
||||
}
|
||||
matrix.update_col(col_idx, &new_col_data).expect("update col");
|
||||
println!("---------- updated ------------");
|
||||
matrix.pretty_print();
|
||||
// G8Encoder::encode_col(&mut data, col_idx).expect("encode col");
|
||||
// println!("---------- encoded ------------");
|
||||
// matrix.pretty_print();
|
||||
|
||||
let encoded_new_col = matrix.get_col(col_idx).expect("get old col");
|
||||
|
||||
// do the comm update
|
||||
KZGPolyComm::update_commitments(&srs, &mut kzg_comm, row_idx, &old_row, &new_row)?;
|
||||
C::update_commitments(&srs, &mut kzg_comm, col_idx, &old_col, &encoded_new_col).expect("update comm");
|
||||
|
||||
// Verify that each row polynomial now evaluates to the updated matrix
|
||||
for (i, poly) in kzg_comm.get_refs().0.iter().enumerate() {
|
||||
let evals: Vec<F> = srs.ploycommit_domain
|
||||
.elements()
|
||||
.map(|x| poly.polynomial().evaluate(&x))
|
||||
.collect();
|
||||
assert_eq!(evals, matrix.get_row(i)?);
|
||||
for i in 0..m {
|
||||
let row = matrix.get_row(i).unwrap();
|
||||
// evaluate poly at each domain point and collect
|
||||
let evals: Vec<_> = kzg_comm.get_poly(i).coeffs.clone();
|
||||
assert_eq!(evals, row);
|
||||
}
|
||||
|
||||
// === new fresh commit on updated matrix ===
|
||||
let kzg_comm_fresh = kzg.commit(&srs, &matrix)?;
|
||||
let kzg_comm_fresh = C::commit(&srs, &matrix).expect("commit updated matrix");
|
||||
// Compare each row commitment
|
||||
for (i, old_lbl_comm) in kzg_comm.get_refs().1.iter().enumerate() {
|
||||
let updated_comm = old_lbl_comm.commitment();
|
||||
let fresh_comm = kzg_comm_fresh.get_refs().1[i].commitment();
|
||||
for (i, old_lbl_comm) in kzg_comm.comm_output.iter().enumerate() {
|
||||
let updated_comm = old_lbl_comm.get_comm();
|
||||
let fresh_comm = kzg_comm_fresh.get_comm(i);
|
||||
assert_eq!(updated_comm, fresh_comm, "Row commitment mismatch at row {}", i);
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user