diff --git a/bench-templates/src/lib.rs b/bench-templates/src/lib.rs index e7abbf5d..6c67e29b 100644 --- a/bench-templates/src/lib.rs +++ b/bench-templates/src/lib.rs @@ -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; diff --git a/poly-commit/Cargo.toml b/poly-commit/Cargo.toml index 49b48083..e503458e 100644 --- a/poly-commit/Cargo.toml +++ b/poly-commit/Cargo.toml @@ -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" ] } @@ -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 = [] diff --git a/poly-commit/src/hyrax/tests.rs b/poly-commit/src/hyrax/tests.rs index 51ce2bf9..6ae822d9 100644 --- a/poly-commit/src/hyrax/tests.rs +++ b/poly-commit/src/hyrax/tests.rs @@ -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; diff --git a/poly-commit/src/lib.rs b/poly-commit/src/lib.rs index 7bd198dd..99e2da7d 100644 --- a/poly-commit/src/lib.rs +++ b/poly-commit/src/lib.rs @@ -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; diff --git a/poly-commit/src/linear_codes/brakedown.rs b/poly-commit/src/linear_codes/brakedown.rs index 722e1338..f1306bfd 100644 --- a/poly-commit/src/linear_codes/brakedown.rs +++ b/poly-commit/src/linear_codes/brakedown.rs @@ -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; diff --git a/poly-commit/src/linear_codes/data_structures.rs b/poly-commit/src/linear_codes/data_structures.rs index 8972840c..6120f7a2 100644 --- a/poly-commit/src/linear_codes/data_structures.rs +++ b/poly-commit/src/linear_codes/data_structures.rs @@ -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 = ""))] diff --git a/poly-commit/src/linear_codes/ligero.rs b/poly-commit/src/linear_codes/ligero.rs index 3eb13043..51898dd1 100644 --- a/poly-commit/src/linear_codes/ligero.rs +++ b/poly-commit/src/linear_codes/ligero.rs @@ -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; diff --git a/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs b/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs index d1002aef..433c9a18 100644 --- a/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs +++ b/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs @@ -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; diff --git a/poly-commit/src/linear_codes/multilinear_brakedown/tests.rs b/poly-commit/src/linear_codes/multilinear_brakedown/tests.rs index 77cb541d..5faae7be 100644 --- a/poly-commit/src/linear_codes/multilinear_brakedown/tests.rs +++ b/poly-commit/src/linear_codes/multilinear_brakedown/tests.rs @@ -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 = FieldToBytesColHasher; - - struct MerkleTreeParams; - - impl Config for MerkleTreeParams { - type Leaf = Vec; - - type LeafDigest = ::Output; - type LeafInnerDigestConverter = ByteDigestConverter; - type InnerDigest = ::Output; - - type LeafHash = LeafH; - type TwoToOneHash = CompressH; - } - - type MTConfig = MerkleTreeParams; - - type BrakedownPCS = LinearCodePCS< - MultilinearBrakedown, ColHasher>, - F, - SparseMultilinearExtension, - MTConfig, - ColHasher, - >; - fn rand_poly( _: usize, num_vars: Option, @@ -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 = ::setup(&mut rng).unwrap(); - let two_to_one_hash_param = ::setup(&mut rng) + let leaf_hash_param = ::setup(&mut rng).unwrap(); + let two_to_one_hash_param = ::setup(&mut rng) .unwrap() .clone(); - let col_hash_params = as CRHScheme>::setup(&mut rng).unwrap(); + let col_hash_params = + as CRHScheme>::setup(&mut rng).unwrap(); let check_well_formedness = true; - let pp: BrakedownPCParams> = + let pp: BrakedownPCParams> = BrakedownPCParams::default( rng, 1 << num_vars, @@ -94,7 +67,7 @@ mod tests { col_hash_params, ); - let (ck, vk) = BrakedownPCS::::trim(&pp, 0, 0, None).unwrap(); + let (ck, vk) = TestMLBrakedown::::trim(&pp, 0, 0, None).unwrap(); let rand_chacha = &mut ChaCha20Rng::from_rng(test_rng()).unwrap(); let labeled_poly = LabeledPolynomial::new( @@ -105,13 +78,14 @@ mod tests { ); let mut test_sponge = test_sponge::(); - let (c, states) = BrakedownPCS::::commit(&ck, &[labeled_poly.clone()], None).unwrap(); + let (c, states) = + TestMLBrakedown::::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::::open( + let proof = TestMLBrakedown::::open( &ck, &[labeled_poly], &c, @@ -121,7 +95,7 @@ mod tests { None, ) .unwrap(); - assert!(BrakedownPCS::::check( + assert!(TestMLBrakedown::::check( &vk, &c, &point, @@ -143,14 +117,14 @@ mod tests { #[test] fn single_poly_test() { use crate::tests::*; - single_poly_test::<_, _, BrakedownPCS, _>( + single_poly_test::<_, _, TestMLBrakedown, _>( Some(5), rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, BrakedownPCS, _>( + single_poly_test::<_, _, TestMLBrakedown, _>( Some(10), rand_poly::, rand_point::, @@ -162,14 +136,14 @@ mod tests { #[test] fn constant_poly_test() { use crate::tests::*; - single_poly_test::<_, _, BrakedownPCS, _>( + single_poly_test::<_, _, TestMLBrakedown, _>( Some(10), constant_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, BrakedownPCS, _>( + single_poly_test::<_, _, TestMLBrakedown, _>( Some(5), constant_poly::, rand_point::, @@ -181,7 +155,7 @@ mod tests { #[test] fn full_end_to_end_test() { use crate::tests::*; - full_end_to_end_test::<_, _, BrakedownPCS, _>( + full_end_to_end_test::<_, _, TestMLBrakedown, _>( Some(8), rand_poly::, rand_point::, @@ -189,7 +163,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_test::<_, _, BrakedownPCS, _>( + full_end_to_end_test::<_, _, TestMLBrakedown, _>( Some(9), rand_poly::, rand_point::, @@ -202,7 +176,7 @@ mod tests { #[test] fn single_equation_test() { use crate::tests::*; - single_equation_test::<_, _, BrakedownPCS, _>( + single_equation_test::<_, _, TestMLBrakedown, _>( Some(10), rand_poly::, rand_point::, @@ -210,7 +184,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - single_equation_test::<_, _, BrakedownPCS, _>( + single_equation_test::<_, _, TestMLBrakedown, _>( Some(5), rand_poly::, rand_point::, @@ -223,7 +197,7 @@ mod tests { #[test] fn two_equation_test() { use crate::tests::*; - two_equation_test::<_, _, BrakedownPCS, _>( + two_equation_test::<_, _, TestMLBrakedown, _>( Some(5), rand_poly::, rand_point::, @@ -231,7 +205,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - two_equation_test::<_, _, BrakedownPCS, _>( + two_equation_test::<_, _, TestMLBrakedown, _>( Some(10), rand_poly::, rand_point::, @@ -244,7 +218,7 @@ mod tests { #[test] fn full_end_to_end_equation_test() { use crate::tests::*; - full_end_to_end_equation_test::<_, _, BrakedownPCS, _>( + full_end_to_end_equation_test::<_, _, TestMLBrakedown, _>( Some(5), rand_poly::, rand_point::, @@ -252,7 +226,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_equation_test::<_, _, BrakedownPCS, _>( + full_end_to_end_equation_test::<_, _, TestMLBrakedown, _>( Some(8), rand_poly::, rand_point::, diff --git a/poly-commit/src/linear_codes/multilinear_ligero/mod.rs b/poly-commit/src/linear_codes/multilinear_ligero/mod.rs index 39cecf13..33c20e90 100644 --- a/poly-commit/src/linear_codes/multilinear_ligero/mod.rs +++ b/poly-commit/src/linear_codes/multilinear_ligero/mod.rs @@ -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; diff --git a/poly-commit/src/linear_codes/multilinear_ligero/tests.rs b/poly-commit/src/linear_codes/multilinear_ligero/tests.rs index 9742104f..82f183d4 100644 --- a/poly-commit/src/linear_codes/multilinear_ligero/tests.rs +++ b/poly-commit/src/linear_codes/multilinear_ligero/tests.rs @@ -1,54 +1,23 @@ #[cfg(test)] mod tests { - - use crate::linear_codes::LinearCodePCS; - use crate::utils::test_sponge; - use crate::PolynomialCommitment; use crate::{ - linear_codes::{LigeroPCParams, MultilinearLigero}, - LabeledPolynomial, + linear_codes::LigeroPCParams, + test_types::{ + test_sponge, FieldToBytesColHasher, LeafIdentityHasher, TestMLLigero, + 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}; - use ark_pcs_bench_templates::{FieldToBytesColHasher, LeafIdentityHasher}; - - type LeafH = LeafIdentityHasher; - type CompressH = Sha256; - type ColHasher = FieldToBytesColHasher; - - struct MerkleTreeParams; - - impl Config for MerkleTreeParams { - type Leaf = Vec; - - type LeafDigest = ::Output; - type LeafInnerDigestConverter = ByteDigestConverter; - type InnerDigest = ::Output; - - type LeafHash = LeafH; - type TwoToOneHash = CompressH; - } - - type MTConfig = MerkleTreeParams; - - type LigeroPCS = LinearCodePCS< - MultilinearLigero, ColHasher>, - F, - SparseMultilinearExtension, - MTConfig, - ColHasher, - >; - fn rand_poly( _: usize, num_vars: Option, @@ -80,23 +49,25 @@ mod tests { let mut rng = &mut test_rng(); let num_vars = 10; // just to make sure we have the right degree given the FFT domain for our field - let leaf_hash_param = ::setup(&mut rng).unwrap(); - let two_to_one_hash_param = ::setup(&mut rng) + let leaf_hash_param = ::setup(&mut rng).unwrap(); + let two_to_one_hash_param = ::setup(&mut rng) .unwrap() .clone(); - let col_hash_params = as CRHScheme>::setup(&mut rng).unwrap(); + let col_hash_params = + as CRHScheme>::setup(&mut rng).unwrap(); let check_well_formedness = true; - let pp: LigeroPCParams> = LigeroPCParams::new( - 128, - 4, - check_well_formedness, - leaf_hash_param, - two_to_one_hash_param, - col_hash_params, - ); + let pp: LigeroPCParams> = + LigeroPCParams::new( + 128, + 4, + check_well_formedness, + leaf_hash_param, + two_to_one_hash_param, + col_hash_params, + ); - let (ck, vk) = LigeroPCS::::trim(&pp, 0, 0, None).unwrap(); + let (ck, vk) = TestMLLigero::::trim(&pp, 0, 0, None).unwrap(); let rand_chacha = &mut ChaCha20Rng::from_rng(test_rng()).unwrap(); let labeled_poly = LabeledPolynomial::new( @@ -107,13 +78,13 @@ mod tests { ); let mut test_sponge = test_sponge::(); - let (c, rands) = LigeroPCS::::commit(&ck, &[labeled_poly.clone()], None).unwrap(); + let (c, rands) = TestMLLigero::::commit(&ck, &[labeled_poly.clone()], None).unwrap(); let point = rand_point(Some(num_vars), rand_chacha); let value = labeled_poly.evaluate(&point); - let proof = LigeroPCS::::open( + let proof = TestMLLigero::::open( &ck, &[labeled_poly], &c, @@ -123,10 +94,16 @@ mod tests { None, ) .unwrap(); - assert!( - LigeroPCS::::check(&vk, &c, &point, [value], &proof, &mut test_sponge, None) - .unwrap() - ); + assert!(TestMLLigero::::check( + &vk, + &c, + &point, + [value], + &proof, + &mut test_sponge, + None + ) + .unwrap()); } fn rand_point(num_vars: Option, rng: &mut ChaCha20Rng) -> Vec { @@ -139,14 +116,14 @@ mod tests { #[test] fn single_poly_test() { use crate::tests::*; - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestMLLigero, _>( Some(5), rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestMLLigero, _>( Some(10), rand_poly::, rand_point::, @@ -158,14 +135,14 @@ mod tests { #[test] fn constant_poly_test() { use crate::tests::*; - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestMLLigero, _>( Some(10), constant_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestMLLigero, _>( Some(5), constant_poly::, rand_point::, @@ -177,7 +154,7 @@ mod tests { #[test] fn full_end_to_end_test() { use crate::tests::*; - full_end_to_end_test::<_, _, LigeroPCS, _>( + full_end_to_end_test::<_, _, TestMLLigero, _>( Some(8), rand_poly::, rand_point::, @@ -185,7 +162,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_test::<_, _, LigeroPCS, _>( + full_end_to_end_test::<_, _, TestMLLigero, _>( Some(3), rand_poly::, rand_point::, @@ -198,7 +175,7 @@ mod tests { #[test] fn single_equation_test() { use crate::tests::*; - single_equation_test::<_, _, LigeroPCS, _>( + single_equation_test::<_, _, TestMLLigero, _>( Some(10), rand_poly::, rand_point::, @@ -206,7 +183,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - single_equation_test::<_, _, LigeroPCS, _>( + single_equation_test::<_, _, TestMLLigero, _>( Some(5), rand_poly::, rand_point::, @@ -219,7 +196,7 @@ mod tests { #[test] fn two_equation_test() { use crate::tests::*; - two_equation_test::<_, _, LigeroPCS, _>( + two_equation_test::<_, _, TestMLLigero, _>( Some(5), rand_poly::, rand_point::, @@ -227,7 +204,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - two_equation_test::<_, _, LigeroPCS, _>( + two_equation_test::<_, _, TestMLLigero, _>( Some(10), rand_poly::, rand_point::, @@ -240,7 +217,7 @@ mod tests { #[test] fn full_end_to_end_equation_test() { use crate::tests::*; - full_end_to_end_equation_test::<_, _, LigeroPCS, _>( + full_end_to_end_equation_test::<_, _, TestMLLigero, _>( Some(5), rand_poly::, rand_point::, @@ -248,7 +225,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_equation_test::<_, _, LigeroPCS, _>( + full_end_to_end_equation_test::<_, _, TestMLLigero, _>( Some(8), rand_poly::, rand_point::, diff --git a/poly-commit/src/linear_codes/univariate_ligero/mod.rs b/poly-commit/src/linear_codes/univariate_ligero/mod.rs index d3c139f1..c172dba1 100644 --- a/poly-commit/src/linear_codes/univariate_ligero/mod.rs +++ b/poly-commit/src/linear_codes/univariate_ligero/mod.rs @@ -1,13 +1,13 @@ -use super::utils::reed_solomon; -use super::{LigeroPCParams, LinearEncode}; +use super::{utils::reed_solomon, LigeroPCParams, LinearEncode}; use crate::Error; -use ark_crypto_primitives::crh::{CRHScheme, TwoToOneCRHScheme}; -use ark_crypto_primitives::merkle_tree::Config; +use ark_crypto_primitives::{ + crh::{CRHScheme, TwoToOneCRHScheme}, + merkle_tree::Config, +}; use ark_ff::PrimeField; use ark_poly::DenseUVPolynomial; -use ark_std::marker::PhantomData; -use ark_std::vec::Vec; +use ark_std::{marker::PhantomData, vec::Vec}; mod tests; diff --git a/poly-commit/src/linear_codes/univariate_ligero/tests.rs b/poly-commit/src/linear_codes/univariate_ligero/tests.rs index 375c7076..7bdd847f 100644 --- a/poly-commit/src/linear_codes/univariate_ligero/tests.rs +++ b/poly-commit/src/linear_codes/univariate_ligero/tests.rs @@ -1,63 +1,23 @@ #[cfg(test)] mod tests { - - use crate::ark_std::UniformRand; - use crate::linear_codes::LinearCodePCS; - use crate::utils::test_sponge; - use crate::PolynomialCommitment; use crate::{ - linear_codes::{LigeroPCParams, UnivariateLigero}, - LabeledPolynomial, + linear_codes::LigeroPCParams, + test_types::{ + test_sponge, FieldToBytesColHasher, LeafIdentityHasher, TestMerkleTreeParams, + TestUVLigero, + }, + 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_ff::{Field, PrimeField}; + use ark_crypto_primitives::crh::{sha256::Sha256, CRHScheme, TwoToOneCRHScheme}; + use ark_ff::{Field, PrimeField, UniformRand}; use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial}; use ark_std::test_rng; use blake2::Blake2s256; use rand_chacha::{rand_core::SeedableRng, ChaCha20Rng}; - use ark_pcs_bench_templates::{FieldToBytesColHasher, LeafIdentityHasher}; - - type LeafH = LeafIdentityHasher; - type CompressH = Sha256; - type ColHasher = FieldToBytesColHasher; - - struct MerkleTreeParams; - - impl Config for MerkleTreeParams { - type Leaf = Vec; - - type LeafDigest = ::Output; - type LeafInnerDigestConverter = ByteDigestConverter; - type InnerDigest = ::Output; - - type LeafHash = LeafH; - type TwoToOneHash = CompressH; - } - - type MTConfig = MerkleTreeParams; - - type LigeroPCS = LinearCodePCS< - UnivariateLigero, ColHasher>, - Fr, - DensePolynomial, - MTConfig, - ColHasher, - >; - - type LigeroPcsF = LinearCodePCS< - UnivariateLigero, ColHasher>, - F, - DensePolynomial, - MTConfig, - ColHasher, - >; - fn rand_poly( degree: usize, _: Option, @@ -79,23 +39,25 @@ mod tests { let degree = 4; let mut rng = &mut test_rng(); // just to make sure we have the right degree given the FFT domain for our field - let leaf_hash_param = ::setup(&mut rng).unwrap(); - let two_to_one_hash_param = ::setup(&mut rng) + let leaf_hash_param = ::setup(&mut rng).unwrap(); + let two_to_one_hash_param = ::setup(&mut rng) .unwrap() .clone(); - let col_hash_params = as CRHScheme>::setup(&mut rng).unwrap(); + let col_hash_params = + as CRHScheme>::setup(&mut rng).unwrap(); let check_well_formedness = true; - let pp: LigeroPCParams> = LigeroPCParams::new( - 128, - 4, - check_well_formedness, - leaf_hash_param, - two_to_one_hash_param, - col_hash_params, - ); + let pp: LigeroPCParams> = + LigeroPCParams::new( + 128, + 4, + check_well_formedness, + leaf_hash_param, + two_to_one_hash_param, + col_hash_params, + ); - let (ck, vk) = LigeroPCS::trim(&pp, 0, 0, None).unwrap(); + let (ck, vk) = TestUVLigero::::trim(&pp, 0, 0, None).unwrap(); let rand_chacha = &mut ChaCha20Rng::from_rng(test_rng()).unwrap(); let labeled_poly = LabeledPolynomial::new( @@ -106,13 +68,13 @@ mod tests { ); let mut test_sponge = test_sponge::(); - let (c, rands) = LigeroPCS::commit(&ck, &[labeled_poly.clone()], None).unwrap(); + let (c, rands) = TestUVLigero::::commit(&ck, &[labeled_poly.clone()], None).unwrap(); let point = Fr::rand(rand_chacha); let value = labeled_poly.evaluate(&point); - let proof = LigeroPCS::open( + let proof = TestUVLigero::::open( &ck, &[labeled_poly], &c, @@ -122,9 +84,16 @@ mod tests { None, ) .unwrap(); - assert!( - LigeroPCS::check(&vk, &c, &point, [value], &proof, &mut test_sponge, None).unwrap() - ); + assert!(TestUVLigero::::check( + &vk, + &c, + &point, + [value], + &proof, + &mut test_sponge, + None + ) + .unwrap()); } fn rand_point(_: Option, rng: &mut ChaCha20Rng) -> F { @@ -134,14 +103,14 @@ mod tests { #[test] fn single_poly_test() { use crate::tests::*; - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, LigeroPcsF, _>( + single_poly_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -153,14 +122,14 @@ mod tests { #[test] fn constant_poly_test() { use crate::tests::*; - single_poly_test::<_, _, LigeroPCS, _>( + single_poly_test::<_, _, TestUVLigero, _>( None, constant_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_test::<_, _, LigeroPcsF, _>( + single_poly_test::<_, _, TestUVLigero, _>( None, constant_poly::, rand_point::, @@ -172,13 +141,13 @@ mod tests { #[test] fn quadratic_poly_degree_bound_multiple_queries_test() { use crate::tests::*; - quadratic_poly_degree_bound_multiple_queries_test::<_, _, LigeroPCS, _>( + quadratic_poly_degree_bound_multiple_queries_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - quadratic_poly_degree_bound_multiple_queries_test::<_, _, LigeroPcsF, _>( + quadratic_poly_degree_bound_multiple_queries_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -189,13 +158,13 @@ mod tests { #[test] fn linear_poly_degree_bound_test() { use crate::tests::*; - linear_poly_degree_bound_test::<_, _, LigeroPCS, _>( + linear_poly_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - linear_poly_degree_bound_test::<_, _, LigeroPcsF, _>( + linear_poly_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -206,13 +175,13 @@ mod tests { #[test] fn single_poly_degree_bound_test() { use crate::tests::*; - single_poly_degree_bound_test::<_, _, LigeroPCS, _>( + single_poly_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_degree_bound_test::<_, _, LigeroPcsF, _>( + single_poly_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -223,13 +192,13 @@ mod tests { #[test] fn single_poly_degree_bound_multiple_queries_test() { use crate::tests::*; - single_poly_degree_bound_multiple_queries_test::<_, _, LigeroPCS, _>( + single_poly_degree_bound_multiple_queries_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - single_poly_degree_bound_multiple_queries_test::<_, _, LigeroPcsF, _>( + single_poly_degree_bound_multiple_queries_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -240,13 +209,13 @@ mod tests { #[test] fn two_polys_degree_bound_single_query_test() { use crate::tests::*; - two_polys_degree_bound_single_query_test::<_, _, LigeroPCS, _>( + two_polys_degree_bound_single_query_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); - two_polys_degree_bound_single_query_test::<_, _, LigeroPcsF, _>( + two_polys_degree_bound_single_query_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -257,7 +226,7 @@ mod tests { #[test] fn full_end_to_end_test() { use crate::tests::*; - full_end_to_end_test::<_, _, LigeroPCS, _>( + full_end_to_end_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -265,7 +234,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_test::<_, _, LigeroPcsF, _>( + full_end_to_end_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -278,7 +247,7 @@ mod tests { #[test] fn single_equation_test() { use crate::tests::*; - single_equation_test::<_, _, LigeroPCS, _>( + single_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -286,7 +255,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - single_equation_test::<_, _, LigeroPcsF, _>( + single_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -299,7 +268,7 @@ mod tests { #[test] fn two_equation_test() { use crate::tests::*; - two_equation_test::<_, _, LigeroPCS, _>( + two_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -307,7 +276,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - two_equation_test::<_, _, LigeroPcsF, _>( + two_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -320,14 +289,14 @@ mod tests { #[test] fn two_equation_degree_bound_test() { use crate::tests::*; - two_equation_degree_bound_test::<_, _, LigeroPCS, _>( + two_equation_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - two_equation_degree_bound_test::<_, _, LigeroPcsF, _>( + two_equation_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, @@ -339,7 +308,7 @@ mod tests { #[test] fn full_end_to_end_equation_test() { use crate::tests::*; - full_end_to_end_equation_test::<_, _, LigeroPCS, _>( + full_end_to_end_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -347,7 +316,7 @@ mod tests { ) .expect("test failed for bls12-377"); println!("Finished bls12-377"); - full_end_to_end_equation_test::<_, _, LigeroPcsF, _>( + full_end_to_end_equation_test::<_, _, TestUVLigero, _>( None, rand_poly::, rand_point::, @@ -362,7 +331,7 @@ mod tests { fn bad_degree_bound_test() { use crate::tests::*; use ark_bls12_381::Fq as Fq381; - bad_degree_bound_test::<_, _, LigeroPcsF, _>( + bad_degree_bound_test::<_, _, TestUVLigero, _>( rand_poly::, rand_point::, poseidon_sponge_for_test::, diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index d4cacc4f..edd8bbaa 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -11,10 +11,8 @@ use num_traits::Float; #[cfg(test)] use { - crate::to_bytes, ark_crypto_primitives::crh::CRHScheme, - ark_std::{borrow::Borrow, marker::PhantomData, rand::RngCore}, - digest::Digest, + ark_std::{borrow::Borrow, rand::RngCore}, }; /// Apply reed-solomon encoding to msg. @@ -204,39 +202,6 @@ impl CRHScheme for LeafIdentityHasher { } } -#[cfg(test)] -pub(crate) struct FieldToBytesColHasher -where - F: PrimeField + CanonicalSerialize, - D: Digest, -{ - _phantom: PhantomData<(F, D)>, -} - -#[cfg(test)] -impl CRHScheme for FieldToBytesColHasher -where - F: PrimeField + CanonicalSerialize, - D: Digest, -{ - type Input = Vec; - type Output = Vec; - type Parameters = (); - - fn setup(_rng: &mut R) -> Result { - Ok(()) - } - - fn evaluate>( - _parameters: &Self::Parameters, - input: T, - ) -> Result { - let mut dig = D::new(); - dig.update(to_bytes!(input.borrow()).unwrap()); - Ok(dig.finalize().to_vec()) - } -} - pub(crate) fn tensor_vec(values: &[F]) -> Vec { let one = F::one(); let anti_values: Vec = values.iter().map(|v| one - *v).collect(); diff --git a/poly-commit/src/test_types/linear_codes.rs b/poly-commit/src/test_types/linear_codes.rs new file mode 100644 index 00000000..728853a0 --- /dev/null +++ b/poly-commit/src/test_types/linear_codes.rs @@ -0,0 +1,119 @@ +use crate::{ + linear_codes::{LinearCodePCS, MultilinearBrakedown, MultilinearLigero, UnivariateLigero}, + to_bytes, +}; +use ark_crypto_primitives::{ + crh::{sha256::Sha256, CRHScheme, TwoToOneCRHScheme}, + merkle_tree::{ByteDigestConverter, Config}, +}; +use ark_ff::PrimeField; +use ark_poly::{univariate::DensePolynomial, SparseMultilinearExtension}; +use ark_serialize::CanonicalSerialize; +use ark_std::{borrow::Borrow, marker::PhantomData, rand::RngCore}; +use blake2::Blake2s256; +use digest::Digest; + +type LeafH = LeafIdentityHasher; +type CompressH = Sha256; +type ColHasher = FieldToBytesColHasher; + +pub struct LeafIdentityHasher; + +impl CRHScheme for LeafIdentityHasher { + type Input = Vec; + type Output = Vec; + type Parameters = (); + + fn setup(_: &mut R) -> Result { + Ok(()) + } + + fn evaluate>( + _: &Self::Parameters, + input: T, + ) -> Result { + Ok(input.borrow().to_vec().into()) + } +} + +/// Needed for benches and tests. +pub struct FieldToBytesColHasher +where + F: PrimeField + CanonicalSerialize, + D: Digest, +{ + _phantom: PhantomData<(F, D)>, +} + +impl CRHScheme for FieldToBytesColHasher +where + F: PrimeField + CanonicalSerialize, + D: Digest, +{ + type Input = Vec; + type Output = Vec; + type Parameters = (); + + fn setup(_rng: &mut R) -> Result { + Ok(()) + } + + fn evaluate>( + _parameters: &Self::Parameters, + input: T, + ) -> Result { + let mut dig = D::new(); + dig.update(to_bytes!(input.borrow()).unwrap()); + Ok(dig.finalize().to_vec()) + } +} + +pub struct TestMerkleTreeParams; + +impl Config for TestMerkleTreeParams { + type Leaf = Vec; + + type LeafDigest = ::Output; + type LeafInnerDigestConverter = ByteDigestConverter; + type InnerDigest = ::Output; + + type LeafHash = LeafH; + type TwoToOneHash = CompressH; +} + +/// Univariate Ligero +pub type TestUVLigero = LinearCodePCS< + UnivariateLigero, ColHasher>, + F, + DensePolynomial, + TestMerkleTreeParams, + ColHasher, +>; + +/// Multilinear Ligero +pub type TestMLLigero = LinearCodePCS< + MultilinearLigero< + F, + TestMerkleTreeParams, + SparseMultilinearExtension, + ColHasher, + >, + F, + SparseMultilinearExtension, + TestMerkleTreeParams, + ColHasher, +>; + +/// Multilinear Brakedown +pub type TestMLBrakedown = LinearCodePCS< + MultilinearBrakedown< + F, + TestMerkleTreeParams, + SparseMultilinearExtension, + ColHasher, + >, + F, + SparseMultilinearExtension, + TestMerkleTreeParams, + ColHasher, +>; diff --git a/poly-commit/src/test_types/mod.rs b/poly-commit/src/test_types/mod.rs new file mode 100644 index 00000000..17d8c29a --- /dev/null +++ b/poly-commit/src/test_types/mod.rs @@ -0,0 +1,8 @@ +mod linear_codes; +mod poseidon_sponge; + +pub use linear_codes::{TestMLBrakedown, TestMLLigero, TestUVLigero}; +pub use poseidon_sponge::test_sponge; + +#[cfg(test)] +pub(crate) use linear_codes::{FieldToBytesColHasher, LeafIdentityHasher, TestMerkleTreeParams}; diff --git a/poly-commit/src/test_types/poseidon_sponge.rs b/poly-commit/src/test_types/poseidon_sponge.rs new file mode 100644 index 00000000..66c6a931 --- /dev/null +++ b/poly-commit/src/test_types/poseidon_sponge.rs @@ -0,0 +1,32 @@ +// TODO: replace by https://github.com/arkworks-rs/crypto-primitives/issues/112. +use ark_crypto_primitives::sponge::poseidon::PoseidonSponge; +use ark_crypto_primitives::sponge::{poseidon::PoseidonConfig, CryptographicSponge}; +use ark_ff::PrimeField; +use ark_std::test_rng; + +/// Constructs a Poseidon sponge for testing. +pub fn test_sponge() -> PoseidonSponge { + let full_rounds = 8; + let partial_rounds = 31; + let alpha = 17; + + let mds = vec![ + vec![F::one(), F::zero(), F::one()], + vec![F::one(), F::one(), F::zero()], + vec![F::zero(), F::one(), F::one()], + ]; + + let mut v = Vec::new(); + let mut ark_rng = test_rng(); + + for _ in 0..(full_rounds + partial_rounds) { + let mut res = Vec::new(); + + for _ in 0..3 { + res.push(F::rand(&mut ark_rng)); + } + v.push(res); + } + let config = PoseidonConfig::new(full_rounds, partial_rounds, alpha, mds, v, 2, 1); + PoseidonSponge::new(&config) +} diff --git a/poly-commit/src/utils.rs b/poly-commit/src/utils.rs index d7703a0e..560b2ba4 100644 --- a/poly-commit/src/utils.rs +++ b/poly-commit/src/utils.rs @@ -174,42 +174,6 @@ pub(crate) fn to_field(v: Vec) -> Vec { v.iter().map(|x| F::from(*x)).collect::>() } -// TODO: replace by https://github.com/arkworks-rs/crypto-primitives/issues/112. -#[cfg(test)] -use ark_crypto_primitives::sponge::poseidon::PoseidonSponge; -#[cfg(test)] -use ark_ff::PrimeField; - -#[cfg(test)] -pub(crate) fn test_sponge() -> PoseidonSponge { - use ark_crypto_primitives::sponge::{poseidon::PoseidonConfig, CryptographicSponge}; - use ark_std::test_rng; - - let full_rounds = 8; - let partial_rounds = 31; - let alpha = 17; - - let mds = vec![ - vec![F::one(), F::zero(), F::one()], - vec![F::one(), F::one(), F::zero()], - vec![F::zero(), F::one(), F::one()], - ]; - - let mut v = Vec::new(); - let mut ark_rng = test_rng(); - - for _ in 0..(full_rounds + partial_rounds) { - let mut res = Vec::new(); - - for _ in 0..3 { - res.push(F::rand(&mut ark_rng)); - } - v.push(res); - } - let config = PoseidonConfig::new(full_rounds, partial_rounds, alpha, mds, v, 2, 1); - PoseidonSponge::new(&config) -} - #[cfg(test)] pub(crate) mod tests {