mirror of
https://github.com/logos-storage/dynamic-data-experiments.git
synced 2026-01-05 22:53:11 +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 {
|
mod tests {
|
||||||
use crate::byte_data::{Data, Params};
|
use crate::byte_data::{Data, Params};
|
||||||
use ark_poly::{EvaluationDomain};
|
use ark_poly::{EvaluationDomain};
|
||||||
use crate::kzg::{F, KZGPolyComm};
|
use crate::kzg10::{E, F, get_vk, KZG10PolyComm};
|
||||||
use crate::field_matrix::Matrix;
|
use crate::field_matrix::Matrix;
|
||||||
use ark_poly_commit::{Polynomial};
|
use ark_poly_commit::kzg10::Commitment;
|
||||||
use crate::encoder::{BLSEncoder, BLSFieldEncoder, G8Encoder};
|
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]
|
#[test]
|
||||||
fn test_encode_columns() {
|
fn test_encode_columns() {
|
||||||
@ -165,32 +166,22 @@ mod tests {
|
|||||||
// make a random n×m matrix
|
// make a random n×m matrix
|
||||||
let matrix = Matrix::from_data(&data);
|
let matrix = Matrix::from_data(&data);
|
||||||
|
|
||||||
// new kzg
|
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||||
let kzg = KZGPolyComm::new(params);
|
let degree = m;
|
||||||
// setup kzg
|
// 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
|
// 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) =
|
// check that each polynomial is really the original rows
|
||||||
kzg_comm.get_refs();
|
for i in 0..m {
|
||||||
|
|
||||||
// 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() {
|
|
||||||
let row = matrix.get_row(i).unwrap();
|
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();
|
||||||
let evals: Vec<_> = srs
|
|
||||||
.ploycommit_domain
|
|
||||||
.elements()
|
|
||||||
.map(|x| poly.polynomial().evaluate(&x))
|
|
||||||
.collect();
|
|
||||||
assert_eq!(evals, row);
|
assert_eq!(evals, row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -214,22 +205,29 @@ mod tests {
|
|||||||
// make a random n×m matrix
|
// make a random n×m matrix
|
||||||
let matrix = Matrix::from_data(&data);
|
let matrix = Matrix::from_data(&data);
|
||||||
|
|
||||||
// new kzg
|
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||||
let kzg = KZGPolyComm::new(params);
|
let degree = m;
|
||||||
// setup kzg
|
// 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
|
// 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");
|
||||||
// check all cells
|
|
||||||
|
// 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 row in 0..n {
|
||||||
for col in 0..m {
|
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");
|
.expect("open should succeed");
|
||||||
let expected: F = matrix.elms[row][col].clone();
|
let expected: F = matrix.elms[row][col].clone();
|
||||||
|
let point = srs.get_domain_element(col);
|
||||||
assert!(
|
assert!(
|
||||||
KZGPolyComm::verify(&kzg_comm, &srs, row, col, expected, &proof)
|
C::verify(&vk, &verifier_comms[row], point, expected, &proof)
|
||||||
.expect("verify should succeed"),
|
.expect("verify should succeed"),
|
||||||
"KZG open/verify failed for row={}, col={}",
|
"KZG open/verify failed for row={}, col={}",
|
||||||
row,
|
row,
|
||||||
@ -265,7 +263,7 @@ mod tests {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// apply update
|
// apply update
|
||||||
data.update_col(c, &new_col);
|
data.update_col(c, &new_col).expect("update col");
|
||||||
println!("data after update:");
|
println!("data after update:");
|
||||||
data.pretty_print();
|
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:");
|
println!("data after encoding update:");
|
||||||
data.pretty_print();
|
data.pretty_print();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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
|
// dimensions: 8 rows (4 parity), 8 columns
|
||||||
let n = 8;
|
let n = 8;
|
||||||
let k = 4;
|
let k = 4;
|
||||||
@ -302,56 +351,57 @@ mod tests {
|
|||||||
|
|
||||||
// Build a matrix where entry (i,j) = i * m + j
|
// Build a matrix where entry (i,j) = i * m + j
|
||||||
let mut matrix = Matrix::<F>::from_data(&data);
|
let mut matrix = Matrix::<F>::from_data(&data);
|
||||||
|
println!("---------- original ------------");
|
||||||
matrix.pretty_print();
|
matrix.pretty_print();
|
||||||
|
|
||||||
// new kzg
|
// degree is the size of each row (the number of cells in a row) which equals the number of columns
|
||||||
let kzg = KZGPolyComm::new(params);
|
let degree = m;
|
||||||
// setup kzg
|
// 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
|
// 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
|
// a column to update
|
||||||
let row_idx = 1;
|
let col_idx = 1;
|
||||||
let old_row = matrix.get_row(row_idx)?;
|
let old_col = matrix.get_col(col_idx).expect("get old col");
|
||||||
|
|
||||||
// a new row by adding a constant to each element
|
let new_col_data: Vec<_> = old_col
|
||||||
let new_row: Vec<_> = old_row.iter()
|
.iter()
|
||||||
.map(|v| *v + F::from(10u64))
|
.take(k) // only look at the first k entries
|
||||||
|
.map(|v| *v + F::from(10u64)) // then do your +10
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Apply the change to the in-memory matrix
|
matrix.update_col(col_idx, &new_col_data).expect("update col");
|
||||||
{
|
println!("---------- updated ------------");
|
||||||
let row_slice = matrix.get_row_mut(row_idx)?;
|
|
||||||
for (j, val) in new_row.iter().enumerate() {
|
|
||||||
row_slice[j] = *val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
matrix.pretty_print();
|
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
|
// 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
|
// Verify that each row polynomial now evaluates to the updated matrix
|
||||||
for (i, poly) in kzg_comm.get_refs().0.iter().enumerate() {
|
for i in 0..m {
|
||||||
let evals: Vec<F> = srs.ploycommit_domain
|
let row = matrix.get_row(i).unwrap();
|
||||||
.elements()
|
// evaluate poly at each domain point and collect
|
||||||
.map(|x| poly.polynomial().evaluate(&x))
|
let evals: Vec<_> = kzg_comm.get_poly(i).coeffs.clone();
|
||||||
.collect();
|
assert_eq!(evals, row);
|
||||||
assert_eq!(evals, matrix.get_row(i)?);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === new fresh commit on updated matrix ===
|
// === 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
|
// Compare each row commitment
|
||||||
for (i, old_lbl_comm) in kzg_comm.get_refs().1.iter().enumerate() {
|
for (i, old_lbl_comm) in kzg_comm.comm_output.iter().enumerate() {
|
||||||
let updated_comm = old_lbl_comm.commitment();
|
let updated_comm = old_lbl_comm.get_comm();
|
||||||
let fresh_comm = kzg_comm_fresh.get_refs().1[i].commitment();
|
let fresh_comm = kzg_comm_fresh.get_comm(i);
|
||||||
assert_eq!(updated_comm, fresh_comm, "Row commitment mismatch at row {}", i);
|
assert_eq!(updated_comm, fresh_comm, "Row commitment mismatch at row {}", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user