Skip to content

Commit

Permalink
Fix zero degree poly commitments and add appropriate tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
luka-ethernal committed Nov 23, 2022
1 parent c06ae22 commit ebe6646
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
da-primitives = { path = "../primitives/avail", default-features = false }
derive_more = "0.99.17"
dusk-bytes = { version = "0.1.6", default-features = false, optional = true }
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-1", optional = true }
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-2", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false }
getrandom = { version = "0.2", features = ["js"], optional = true }
hex = { version = "0.4", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion kate/proof/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.41"
dusk-bytes = "0.1.6"
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-1" }
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-2" }
merlin = "3.0"
rand = "0.8.4"
rand_chacha = "0.3"
2 changes: 1 addition & 1 deletion kate/recovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
derive_more = "0.99.17"
dusk-bytes = "0.1.6"
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-1" }
dusk-plonk = { git = "https://github.com/maticnetwork/plonk.git", tag = "v0.12.0-polygon-2" }
getrandom = { version = "0.2", features = ["js"] }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0.37"
Expand Down
59 changes: 58 additions & 1 deletion kate/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ mod tests {
let commitment = &commitments[row * 48..(row + 1) * 48];
let verification = kate_proof::kc_verify_proof(col, &proof, commitment, dims.rows.as_usize(), dims.cols.as_usize(), &public_params);
prop_assert!(verification.is_ok());
prop_assert!(verification.unwrap());
}
}
}
Expand Down Expand Up @@ -1110,7 +1111,6 @@ Let's see how this gets encoded and then reconstructed by sampling only some dat
}

#[test]
#[should_panic]
fn par_build_commitments_row_wise_constant_row() {
// Due to scale encoding, first line is not constant.
// We will use second line to ensure constant row.
Expand All @@ -1121,6 +1121,63 @@ Let's see how this gets encoded and then reconstructed by sampling only some dat
}];
par_build_commitments(BlockLengthRows(4), BlockLengthColumns(4), 32, &xts, hash).unwrap();
}
#[test_case( (&[1,1,1,1]).to_vec(); "All values are non-zero but same")]
#[test_case( (&[0,0,0,0]).to_vec(); "All values are zero")]
#[test_case( (&[0,5,2,1]).to_vec(); "All values are different")]
fn test_zero_deg_poly_commit(row_values: Vec<u8>) {
// There are two main cases that generate a zero degree polynomial. One is for data that is non-zero, but the same.
// The other is for all-zero data. They differ, as the former yields a polynomial with one coefficient, and latter generates zero coefficients.
let len = row_values.len();
let public_params = testnet::public_params(BlockLengthColumns(len as u32));
let (prover_key, _) = public_params.trim(len).map_err(Error::from).unwrap();
let row_eval_domain = EvaluationDomain::new(len).map_err(Error::from).unwrap();

let row = row_values
.iter()
.map(|val| {
let mut value = [0u8; 32];
let v = value.last_mut().unwrap();
*v = *val;
BlsScalar::from_bytes(&value).unwrap()
})
.collect::<Vec<_>>();

assert_eq!(row.len(), len as usize);
let mut result_bytes: Vec<u8> = vec![0u8; 48];
let _ = commit(&prover_key, row_eval_domain, row.clone(), &mut result_bytes).unwrap();
println!("Commitment: {result_bytes:?}");

// We artificially extend the matrix by doubling values, this is not proper erasure coding.
let ext_m = row.into_iter().flat_map(|e| vec![e, e]).collect::<Vec<_>>();

for i in 0..row_values.len() {
// Randomly chosen cell to prove, probably should test all of them
let cell = Cell {
col: BlockLengthColumns(i as u32),
row: BlockLengthRows(0),
};
let proof = build_proof(
&public_params,
BlockDimensions {
rows: BlockLengthRows(1),
cols: BlockLengthColumns(4),
chunk_size: 32,
},
&ext_m,
&[cell],
)
.unwrap();
println!("Proof: {proof:?}");

assert!(proof.len() == 80);

let commitment = &result_bytes;
let verification =
kate_proof::kc_verify_proof(i as u32, &proof, commitment, 1, 4, &public_params);
assert!(verification.is_ok());
assert!(verification.unwrap())
}
}

#[test_case( r#"{ "row": 42, "col": 99 }"# => Cell::new(42.into(),99.into()) ; "Simple" )]
#[test_case( r#"{ "row": 4294967295, "col": 99 }"# => Cell::new(4_294_967_295.into(),99.into()) ; "Max row" )]
Expand Down

0 comments on commit ebe6646

Please sign in to comment.