Skip to content

Commit

Permalink
Export linear codes and sponge test types (#70)
Browse files Browse the repository at this point in the history
* Export test_sponge under test-types feature

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Export linear PCS types

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Refactor UV Ligero tests

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Refactor ML Ligero tests

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Refactor ML Brakedown tests

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Group imports

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Run cargo fmt

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Add ahash as default hashbrown feature

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

* Add missing definition for test-types feature

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>

---------

Co-authored-by: Antonio Mejías Gil <anmegi.95@gmail.com>
  • Loading branch information
Cesar199999 and Antonio95 authored Jun 21, 2024
1 parent 16ed9b8 commit 30faef0
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 332 deletions.
2 changes: 2 additions & 0 deletions bench-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ macro_rules! bench {

/**** Auxiliary methods for linear-code-based PCSs ****/

// TODO: This is code duplicated in test_types/linear_code.rs.

/// Needed for benches and tests.
pub struct LeafIdentityHasher;

Expand Down
3 changes: 2 additions & 1 deletion poly-commit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ blake2 = { version = "0.10", default-features = false }
rand = { version = "0.8.0", optional = true }
ark-relations = { version = "^0.4.0", default-features = false, optional = true }
ark-r1cs-std = { version = "^0.4.0", default-features = false, optional = true }
hashbrown = { version = "0.14", default-features = false, optional = true }
hashbrown = { version = "0.14", default-features = false, features = [ "ahash" ] , optional = true }

digest = "0.10"
derivative = { version = "2", features = [ "use_core" ] }
Expand Down Expand Up @@ -68,3 +68,4 @@ std = [ "ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std", "ark-relation
r1cs = [ "ark-relations", "ark-r1cs-std", "hashbrown", "ark-crypto-primitives/r1cs"]
print-trace = [ "ark-std/print-trace" ]
parallel = [ "std", "ark-ff/parallel", "ark-ec/parallel", "ark-poly/parallel", "ark-std/parallel", "rayon", "rand" ]
test-types = []
2 changes: 1 addition & 1 deletion poly-commit/src/hyrax/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::test_types::test_sponge;
use crate::tests::*;
use crate::utils::test_sponge;
use crate::LabeledPolynomial;
use crate::{hyrax::HyraxPC, PolynomialCommitment};
use ark_bls12_377::G1Affine;
Expand Down
7 changes: 7 additions & 0 deletions poly-commit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ pub use data_structures::*;
/// Useful functions
pub(crate) mod utils;

/// Types used for testing polynomial commitment schemes.
#[cfg(any(test, feature = "test-types"))]
pub mod test_types;

#[cfg(feature = "test-types")]
pub use test_types::*;

/// R1CS constraints for polynomial constraints.
#[cfg(feature = "r1cs")]
mod constraints;
Expand Down
23 changes: 11 additions & 12 deletions poly-commit/src/linear_codes/brakedown.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use super::utils::SprsMat;
use super::BrakedownPCParams;
use super::LinCodeParametersInfo;
use crate::linear_codes::utils::calculate_t;
use crate::utils::ceil_div;
use crate::utils::{ceil_mul, ent};
use crate::{PCCommitterKey, PCUniversalParams, PCVerifierKey};
use super::{utils::SprsMat, BrakedownPCParams, LinCodeParametersInfo};
use crate::{
linear_codes::utils::calculate_t,
utils::{ceil_div, ceil_mul, ent},
PCCommitterKey, PCUniversalParams, PCVerifierKey,
};

use ark_crypto_primitives::crh::{CRHScheme, TwoToOneCRHScheme};
use ark_crypto_primitives::merkle_tree::{Config, LeafParam, TwoToOneParam};
use ark_crypto_primitives::{
crh::{CRHScheme, TwoToOneCRHScheme},
merkle_tree::{Config, LeafParam, TwoToOneParam},
};
use ark_ff::PrimeField;
use ark_std::log2;
use ark_std::rand::RngCore;
use ark_std::vec::Vec;
use ark_std::{log2, rand::RngCore, vec::Vec};
#[cfg(all(not(feature = "std"), target_arch = "aarch64"))]
use num_traits::Float;

Expand Down
4 changes: 1 addition & 3 deletions poly-commit/src/linear_codes/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use ark_crypto_primitives::{
};
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::marker::PhantomData;
use ark_std::rand::RngCore;
use ark_std::vec::Vec;
use ark_std::{marker::PhantomData, rand::RngCore, vec::Vec};

#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(Clone(bound = ""), Debug(bound = ""))]
Expand Down
20 changes: 11 additions & 9 deletions poly-commit/src/linear_codes/ligero.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use super::LigeroPCParams;
use super::LinCodeParametersInfo;
use crate::linear_codes::utils::calculate_t;
use crate::utils::ceil_div;
use crate::{PCCommitterKey, PCUniversalParams, PCVerifierKey};
use super::{LigeroPCParams, LinCodeParametersInfo};

use ark_crypto_primitives::crh::{CRHScheme, TwoToOneCRHScheme};
use ark_crypto_primitives::merkle_tree::{Config, LeafParam, TwoToOneParam};
use crate::{
linear_codes::utils::calculate_t, utils::ceil_div, PCCommitterKey, PCUniversalParams,
PCVerifierKey,
};

use ark_crypto_primitives::{
crh::{CRHScheme, TwoToOneCRHScheme},
merkle_tree::{Config, LeafParam, TwoToOneParam},
};
use ark_ff::PrimeField;
use ark_std::log2;
use ark_std::marker::PhantomData;
use ark_std::{log2, marker::PhantomData};
#[cfg(not(feature = "std"))]
use num_traits::Float;

Expand Down
14 changes: 6 additions & 8 deletions poly-commit/src/linear_codes/multilinear_brakedown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::Error;

use super::utils::tensor_vec;
use super::{BrakedownPCParams, LinearEncode};
use ark_crypto_primitives::crh::{CRHScheme, TwoToOneCRHScheme};
use ark_crypto_primitives::merkle_tree::Config;
use super::{utils::tensor_vec, BrakedownPCParams, LinearEncode};
use ark_crypto_primitives::{
crh::{CRHScheme, TwoToOneCRHScheme},
merkle_tree::Config,
};
use ark_ff::{Field, PrimeField};
use ark_poly::{MultilinearExtension, Polynomial};
use ark_std::log2;
use ark_std::marker::PhantomData;
use ark_std::rand::RngCore;
use ark_std::vec::Vec;
use ark_std::{log2, marker::PhantomData, rand::RngCore, vec::Vec};

mod tests;

Expand Down
88 changes: 31 additions & 57 deletions poly-commit/src/linear_codes/multilinear_brakedown/tests.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
#[cfg(test)]
mod tests {

use crate::linear_codes::LinearCodePCS;
use crate::utils::test_sponge;
use crate::PolynomialCommitment;
use crate::{
linear_codes::{utils::*, BrakedownPCParams, MultilinearBrakedown},
LabeledPolynomial,
linear_codes::BrakedownPCParams,
test_types::{
test_sponge, FieldToBytesColHasher, LeafIdentityHasher, TestMLBrakedown,
TestMerkleTreeParams,
},
LabeledPolynomial, PolynomialCommitment,
};

use ark_bls12_377::Fr;
use ark_bls12_381::Fr as Fr381;
use ark_crypto_primitives::{
crh::{sha256::Sha256, CRHScheme, TwoToOneCRHScheme},
merkle_tree::{ByteDigestConverter, Config},
};
use ark_crypto_primitives::crh::{sha256::Sha256, CRHScheme, TwoToOneCRHScheme};

use ark_ff::{Field, PrimeField};
use ark_poly::evaluations::multivariate::{MultilinearExtension, SparseMultilinearExtension};
use ark_std::test_rng;
use blake2::Blake2s256;
use rand_chacha::{rand_core::SeedableRng, ChaCha20Rng};

type LeafH = LeafIdentityHasher;
type CompressH = Sha256;
type ColHasher<F, D> = FieldToBytesColHasher<F, D>;

struct MerkleTreeParams;

impl Config for MerkleTreeParams {
type Leaf = Vec<u8>;

type LeafDigest = <LeafH as CRHScheme>::Output;
type LeafInnerDigestConverter = ByteDigestConverter<Self::LeafDigest>;
type InnerDigest = <CompressH as TwoToOneCRHScheme>::Output;

type LeafHash = LeafH;
type TwoToOneHash = CompressH;
}

type MTConfig = MerkleTreeParams;

type BrakedownPCS<F> = LinearCodePCS<
MultilinearBrakedown<F, MTConfig, SparseMultilinearExtension<F>, ColHasher<F, Blake2s256>>,
F,
SparseMultilinearExtension<F>,
MTConfig,
ColHasher<F, Blake2s256>,
>;

fn rand_poly<Fr: PrimeField>(
_: usize,
num_vars: Option<usize>,
Expand Down Expand Up @@ -77,14 +49,15 @@ mod tests {
let mut rng = &mut test_rng();
let num_vars = 11;
// just to make sure we have the right degree given the FFT domain for our field
let leaf_hash_param = <LeafH as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_hash_param = <CompressH as TwoToOneCRHScheme>::setup(&mut rng)
let leaf_hash_param = <LeafIdentityHasher as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_hash_param = <Sha256 as TwoToOneCRHScheme>::setup(&mut rng)
.unwrap()
.clone();
let col_hash_params = <ColHasher<Fr, Blake2s256> as CRHScheme>::setup(&mut rng).unwrap();
let col_hash_params =
<FieldToBytesColHasher<Fr, Blake2s256> as CRHScheme>::setup(&mut rng).unwrap();
let check_well_formedness = true;

let pp: BrakedownPCParams<Fr, MTConfig, ColHasher<Fr, Blake2s256>> =
let pp: BrakedownPCParams<Fr, TestMerkleTreeParams, FieldToBytesColHasher<Fr, Blake2s256>> =
BrakedownPCParams::default(
rng,
1 << num_vars,
Expand All @@ -94,7 +67,7 @@ mod tests {
col_hash_params,
);

let (ck, vk) = BrakedownPCS::<Fr>::trim(&pp, 0, 0, None).unwrap();
let (ck, vk) = TestMLBrakedown::<Fr>::trim(&pp, 0, 0, None).unwrap();

let rand_chacha = &mut ChaCha20Rng::from_rng(test_rng()).unwrap();
let labeled_poly = LabeledPolynomial::new(
Expand All @@ -105,13 +78,14 @@ mod tests {
);

let mut test_sponge = test_sponge::<Fr>();
let (c, states) = BrakedownPCS::<Fr>::commit(&ck, &[labeled_poly.clone()], None).unwrap();
let (c, states) =
TestMLBrakedown::<Fr>::commit(&ck, &[labeled_poly.clone()], None).unwrap();

let point = rand_point(Some(num_vars), rand_chacha);

let value = labeled_poly.evaluate(&point);

let proof = BrakedownPCS::<Fr>::open(
let proof = TestMLBrakedown::<Fr>::open(
&ck,
&[labeled_poly],
&c,
Expand All @@ -121,7 +95,7 @@ mod tests {
None,
)
.unwrap();
assert!(BrakedownPCS::<Fr>::check(
assert!(TestMLBrakedown::<Fr>::check(
&vk,
&c,
&point,
Expand All @@ -143,14 +117,14 @@ mod tests {
#[test]
fn single_poly_test() {
use crate::tests::*;
single_poly_test::<_, _, BrakedownPCS<Fr>, _>(
single_poly_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(5),
rand_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
single_poly_test::<_, _, BrakedownPCS<Fr381>, _>(
single_poly_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(10),
rand_poly::<Fr381>,
rand_point::<Fr381>,
Expand All @@ -162,14 +136,14 @@ mod tests {
#[test]
fn constant_poly_test() {
use crate::tests::*;
single_poly_test::<_, _, BrakedownPCS<Fr>, _>(
single_poly_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(10),
constant_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
single_poly_test::<_, _, BrakedownPCS<Fr381>, _>(
single_poly_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(5),
constant_poly::<Fr381>,
rand_point::<Fr381>,
Expand All @@ -181,15 +155,15 @@ mod tests {
#[test]
fn full_end_to_end_test() {
use crate::tests::*;
full_end_to_end_test::<_, _, BrakedownPCS<Fr>, _>(
full_end_to_end_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(8),
rand_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
println!("Finished bls12-377");
full_end_to_end_test::<_, _, BrakedownPCS<Fr381>, _>(
full_end_to_end_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(9),
rand_poly::<Fr381>,
rand_point::<Fr381>,
Expand All @@ -202,15 +176,15 @@ mod tests {
#[test]
fn single_equation_test() {
use crate::tests::*;
single_equation_test::<_, _, BrakedownPCS<Fr>, _>(
single_equation_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(10),
rand_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
println!("Finished bls12-377");
single_equation_test::<_, _, BrakedownPCS<Fr381>, _>(
single_equation_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(5),
rand_poly::<Fr381>,
rand_point::<Fr381>,
Expand All @@ -223,15 +197,15 @@ mod tests {
#[test]
fn two_equation_test() {
use crate::tests::*;
two_equation_test::<_, _, BrakedownPCS<Fr>, _>(
two_equation_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(5),
rand_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
println!("Finished bls12-377");
two_equation_test::<_, _, BrakedownPCS<Fr381>, _>(
two_equation_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(10),
rand_poly::<Fr381>,
rand_point::<Fr381>,
Expand All @@ -244,15 +218,15 @@ mod tests {
#[test]
fn full_end_to_end_equation_test() {
use crate::tests::*;
full_end_to_end_equation_test::<_, _, BrakedownPCS<Fr>, _>(
full_end_to_end_equation_test::<_, _, TestMLBrakedown<Fr>, _>(
Some(5),
rand_poly::<Fr>,
rand_point::<Fr>,
poseidon_sponge_for_test::<Fr>,
)
.expect("test failed for bls12-377");
println!("Finished bls12-377");
full_end_to_end_equation_test::<_, _, BrakedownPCS<Fr381>, _>(
full_end_to_end_equation_test::<_, _, TestMLBrakedown<Fr381>, _>(
Some(8),
rand_poly::<Fr381>,
rand_point::<Fr381>,
Expand Down
4 changes: 1 addition & 3 deletions poly-commit/src/linear_codes/multilinear_ligero/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use ark_crypto_primitives::{
};
use ark_ff::{FftField, PrimeField};
use ark_poly::{MultilinearExtension, Polynomial};
use ark_std::log2;
use ark_std::marker::PhantomData;
use ark_std::vec::Vec;
use ark_std::{log2, marker::PhantomData, vec::Vec};

mod tests;

Expand Down
Loading

0 comments on commit 30faef0

Please sign in to comment.