Skip to content

Commit

Permalink
Merge pull request #58 from availproject/toufeeq/srs
Browse files Browse the repository at this point in the history
  • Loading branch information
ToufeeqP authored Oct 30, 2023
2 parents b19a8bb + c26660d commit b7097da
Show file tree
Hide file tree
Showing 7 changed files with 1,195 additions and 15 deletions.
3 changes: 3 additions & 0 deletions kate/recovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub mod sparse_slice_read;

#[cfg(feature = "std")]
pub mod testnet;

#[cfg(feature = "std")]
pub mod testnet_v2;
5 changes: 1 addition & 4 deletions kate/recovery/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,5 @@ pub fn verify(
.nth(cell.position.col.into())
.ok_or(Error::InvalidPositionInDomain)?;

public_parameters
.trim(cols)
.map(|(_, verifier_key)| verifier_key.check(point, proof))
.map_err(|_| Error::InvalidDegree)
Ok(public_parameters.opening_key().check(point, proof))
}
7 changes: 7 additions & 0 deletions kate/recovery/src/testnet_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use dusk_plonk::commitment_scheme::kzg10::PublicParameters;

pub fn public_params() -> PublicParameters {
let pp_bytes = include_bytes!("../../src/pp_1024.data");
PublicParameters::from_slice(pp_bytes)
.expect("Deserialising of public parameters should work for serialised pp")
}
1,028 changes: 1,028 additions & 0 deletions kate/src/g1_g2_1024.txt

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions kate/src/gridgen/tests/commitments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::{gridgen::*, testnet, Seed};
use crate::{gridgen::*, testnet, testnet_v2, Seed};
use avail_core::{AppExtrinsic, AppId, BlockLengthColumns, BlockLengthRows};
use hex_literal::hex;
use kate_recovery::{
Expand Down Expand Up @@ -86,7 +86,7 @@ proptest! {
.map(|c| c.to_bytes().unwrap())
.collect::<Vec<_>>();

let public_params = testnet::public_params(BlockLengthColumns(g_cols as u32));
let public_params = testnet::public_params( BlockLengthColumns(g_cols.into()));

for xt in exts.iter() {
let rows = grid.app_rows(xt.app_id, Some(orig_dims)).unwrap().unwrap();
Expand Down Expand Up @@ -152,8 +152,9 @@ fn test_zero_deg_poly_commit(row_values: Vec<u8>) {
println!("Row: {:?}", ev.evals);

let pg = ev.make_polynomial_grid().unwrap();
let pmp = testnet_v2::multiproof_params();
println!("Poly: {:?}", pg.inner[0]);
let commitment = pg.commitment(&*PMP, 0).unwrap().to_bytes().unwrap();
let commitment = pg.commitment(&pmp, 0).unwrap().to_bytes().unwrap();

for x in 0..len {
// Randomly chosen cell to prove, probably should test all of them
Expand All @@ -162,7 +163,7 @@ fn test_zero_deg_poly_commit(row_values: Vec<u8>) {
row: BlockLengthRows(0),
};

let proof = pg.proof(&PMP, &cell).unwrap();
let proof = pg.proof(&pmp, &cell).unwrap();

let proof_bytes = proof.to_bytes().unwrap();
let cell_bytes = ev.get(0usize, x).unwrap().to_bytes().unwrap();
Expand All @@ -175,12 +176,8 @@ fn test_zero_deg_poly_commit(row_values: Vec<u8>) {
},
content: content.try_into().unwrap(),
};
let verification = kate_recovery::proof::verify(
&kate_recovery::testnet::public_params(256),
dims,
&commitment,
&cell,
);
let verification =
kate_recovery::proof::verify(&testnet_v2::public_params(), dims, &commitment, &cell);
assert!(verification.is_ok());
assert!(verification.unwrap())
}
Expand Down
150 changes: 149 additions & 1 deletion kate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub mod testnet {
static SRS_DATA: Lazy<Mutex<HashMap<u32, PublicParameters>>> =
Lazy::new(|| Mutex::new(HashMap::new()));

/// constructs public parameters for a given degree
pub fn public_params(max_degree: BlockLengthColumns) -> PublicParameters {
let max_degree: u32 = max_degree.into();
let mut srs_data_locked = SRS_DATA.lock().unwrap();
Expand Down Expand Up @@ -165,6 +166,153 @@ pub mod testnet {
}
}

// TODO: load pp for both dusk & arkworks from same file
// To be used for incentivised testnet
#[cfg(feature = "std")]
pub mod testnet_v2 {
use super::*;
use poly_multiproof::ark_serialize::CanonicalDeserialize;
use poly_multiproof::m1_blst;
use poly_multiproof::m1_blst::{G1, G2};

/// Constructs public parameters from pre-generated points for degree upto 1024
pub fn public_params() -> PublicParameters {
// We can also use the raw data to make deserilization faster at the cost of size of the data
let pp_bytes = include_bytes!("pp_1024.data");
PublicParameters::from_slice(pp_bytes).expect("Deserialization should work")
}

// Loads the pre-generated trusted g1 & g2 from the file
fn load_trusted_g1_g2() -> (Vec<G1>, Vec<G2>) {
// for degree = 1024
let contents = include_str!("g1_g2_1024.txt");
let mut lines = contents.lines();
let g1_len: usize = lines.next().unwrap().parse().unwrap();
let g2_len: usize = lines.next().unwrap().parse().unwrap();

let g1_bytes: Vec<[u8; 48]> = lines
.by_ref()
.take(g1_len)
.map(|line| hex::decode(line).unwrap().try_into().unwrap())
.collect();

let g2_bytes: Vec<[u8; 96]> = lines
.take(g2_len)
.map(|line| hex::decode(line).unwrap().try_into().unwrap())
.collect();

let g1: Vec<G1> = g1_bytes
.iter()
.map(|bytes| G1::deserialize_compressed(&bytes[..]).unwrap())
.collect();

let g2: Vec<G2> = g2_bytes
.iter()
.map(|bytes| G2::deserialize_compressed(&bytes[..]).unwrap())
.collect();

(g1, g2)
}

/// Construct public parameters from pre-generated points for degree upto 1024
pub fn multiproof_params() -> m1_blst::M1NoPrecomp {
let (g1, g2) = load_trusted_g1_g2();
m1_blst::M1NoPrecomp::new_from_powers(g1, g2)
}

#[cfg(test)]
mod tests {
use super::*;
use dusk_plonk::{
commitment_scheme::kzg10::proof::Proof,
fft::{EvaluationDomain as DPEvaluationDomain, Evaluations},
};
use kate_recovery::{data::Cell, matrix::Position};
use pmp::{
ark_poly::{
univariate::DensePolynomial, DenseUVPolynomial, EvaluationDomain,
GeneralEvaluationDomain,
},
traits::KZGProof,
};
use poly_multiproof::{
m1_blst::Fr,
traits::{AsBytes, Committer},
};
use rand::thread_rng;

#[test]
fn test_consistent_testnet_params() {
let pmp = testnet_v2::multiproof_params();
let pmp2 = testnet_v2::public_params();

let points = DensePolynomial::<Fr>::rand(1023, &mut thread_rng()).coeffs;
let points2: Vec<_> = points
.iter()
.map(|p| BlsScalar::from_bytes(&p.to_bytes().unwrap()).unwrap())
.collect();

let dp_ev = DPEvaluationDomain::new(1024).unwrap();
let dp_poly = Evaluations::from_vec_and_domain(points2.clone(), dp_ev).interpolate();
let dp_domain_pts = dp_ev.elements().collect::<Vec<_>>();
let pmp_ev = GeneralEvaluationDomain::<Fr>::new(1024).unwrap();
let pmp_poly = pmp_ev.ifft(&points);
let pmp_domain_pts = pmp_ev.elements().collect::<Vec<_>>();

let dp_commit = pmp2.commit_key().commit(&dp_poly).unwrap();
let pmp_commit = pmp.commit(&pmp_poly).unwrap();

assert_eq!(dp_commit.0.to_bytes(), pmp_commit.to_bytes().unwrap());

let proof = pmp
.open(
pmp.compute_witness_polynomial(pmp_poly, pmp_domain_pts[1])
.unwrap(),
)
.unwrap();

let proof2 = pmp2
.commit_key()
.commit(
&pmp2
.commit_key()
.compute_single_witness(&dp_poly, &dp_domain_pts[1]),
)
.unwrap();

assert_eq!(proof.to_bytes().unwrap(), proof2.to_bytes());

let verify1 = pmp
.verify(&pmp_commit, pmp_domain_pts[1], points[1], &proof)
.unwrap();

let dp_proof_obj = Proof {
commitment_to_witness: proof2,
evaluated_point: points2[1],
commitment_to_polynomial: dp_commit,
};
assert!(pmp2.opening_key().check(dp_domain_pts[1], dp_proof_obj));

let mut content = [0u8; 80];
content[..48].copy_from_slice(&proof2.to_bytes());
content[48..].copy_from_slice(&points2[1].to_bytes());
let verify2 = kate_recovery::proof::verify(
&pmp2,
Dimensions::new(1, 1024).unwrap(),
&dp_commit.0.to_bytes(),
&Cell {
content,
position: Position { row: 0, col: 1 },
},
)
.unwrap();

assert!(verify1);
assert!(verify2);
}
}
}

pub mod metrics;

#[cfg(feature = "std")]
Expand All @@ -173,7 +321,7 @@ pub mod com;
#[cfg(feature = "std")]
pub mod gridgen;

/// Precalculate the length of padding IEC 9797 1.
/// Precalculate the g1_len of padding IEC 9797 1.
///
/// # NOTE
/// There is a unit test to ensure this formula match with the current
Expand Down
Binary file added kate/src/pp_1024.data
Binary file not shown.

0 comments on commit b7097da

Please sign in to comment.