From ebe6646ec9a1d4587e2da95b3c11b3722288c3cd Mon Sep 17 00:00:00 2001 From: Luka Borkovic Date: Mon, 21 Nov 2022 16:40:48 +0100 Subject: [PATCH] Fix zero degree poly commitments and add appropriate tests. --- Cargo.lock | 2 +- kate/Cargo.toml | 2 +- kate/proof/Cargo.toml | 2 +- kate/recovery/Cargo.toml | 2 +- kate/src/com.rs | 59 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 731e9575..e3306dd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -881,7 +881,7 @@ dependencies = [ [[package]] name = "dusk-plonk" version = "0.12.0" -source = "git+https://github.com/maticnetwork/plonk.git?tag=v0.12.0-polygon-1#d8cdbd1787405ecceb494b9d1438254b1c1de53b" +source = "git+https://github.com/maticnetwork/plonk.git?tag=v0.12.0-polygon-2#6ada57a82b6e3a92a677b9f6263f9f3c9b501b23" dependencies = [ "cfg-if", "dusk-bls12_381", diff --git a/kate/Cargo.toml b/kate/Cargo.toml index e294c5cf..294ea01d 100644 --- a/kate/Cargo.toml +++ b/kate/Cargo.toml @@ -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 } diff --git a/kate/proof/Cargo.toml b/kate/proof/Cargo.toml index 58246b6e..a5a76b51 100644 --- a/kate/proof/Cargo.toml +++ b/kate/proof/Cargo.toml @@ -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" diff --git a/kate/recovery/Cargo.toml b/kate/recovery/Cargo.toml index 10e4799e..fd8a1e7c 100644 --- a/kate/recovery/Cargo.toml +++ b/kate/recovery/Cargo.toml @@ -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" diff --git a/kate/src/com.rs b/kate/src/com.rs index 163bf046..8ca16b68 100644 --- a/kate/src/com.rs +++ b/kate/src/com.rs @@ -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()); } } } @@ -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. @@ -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) { + // 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::>(); + + assert_eq!(row.len(), len as usize); + let mut result_bytes: Vec = 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::>(); + + 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" )]