Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move random_test_initializer to separate file [SyncWith: crypto3-zk#328] #103

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ include(CMSetupVersion)
option(BUILD_TESTS "Build unit tests" TRUE)
option(BUILD_WITH_NO_WARNINGS "Build threading warnings as errors" FALSE)

list(APPEND ${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS
include/nil/network/marshalling/version.hpp)

list(APPEND ${CURRENT_PROJECT_NAME}_UNGROUPED_SOURCES)

list(APPEND ${CURRENT_PROJECT_NAME}_HEADERS ${${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS})

list(APPEND ${CURRENT_PROJECT_NAME}_SOURCES ${${CURRENT_PROJECT_NAME}_UNGROUPED_SOURCES})

cm_setup_version(VERSION 0.1.0 PREFIX ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME})

add_library(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE)
Expand Down Expand Up @@ -73,9 +64,6 @@ cm_deploy(TARGETS ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME}
INCLUDE include
NAMESPACE ${CMAKE_WORKSPACE_NAME}::)

add_subdirectories("${CMAKE_CURRENT_LIST_DIR}/libs"
SUBMODULE_DEFINITION_HEADER "cm_project\\(")

if(BUILD_TESTS)
add_subdirectory(test)
endif()
Expand Down
85 changes: 16 additions & 69 deletions test/fri_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
#include <random>
#include <regex>

#include <nil/crypto3/random/algebraic_random_device.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>
#include <nil/crypto3/algebra/random_element.hpp>

#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/endianness.hpp>
Expand Down Expand Up @@ -71,6 +67,7 @@

#include <nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp>
#include <nil/crypto3/zk/commitments/polynomial/fri.hpp>
#include <nil/crypto3/zk/test_tools/random_test_initializer.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/fri.hpp>

using namespace nil::crypto3;
Expand Down Expand Up @@ -319,54 +316,7 @@ void test_fri_proof(typename FRI::proof_type &proof, typename nil::crypto3::mars
BOOST_CHECK(proof == constructed_val_read);
}

//
// Randomness setup
//
std::size_t test_global_seed = 0;
boost::random::mt11213b test_global_rnd_engine;
template<typename FieldType>
nil::crypto3::random::algebraic_engine<FieldType> test_global_alg_rnd_engine;

struct test_initializer {
// Enumerate all fields used in tests;
using field1_type = algebra::curves::bls12<381>::scalar_field_type;

test_initializer() {
test_global_seed = 0;

for (std::size_t i = 0; i < std::size_t(boost::unit_test::framework::master_test_suite().argc - 1); i++) {
if (std::string(boost::unit_test::framework::master_test_suite().argv[i]) == "--seed") {
if (std::string(boost::unit_test::framework::master_test_suite().argv[i + 1]) == "random") {
std::random_device rd;
test_global_seed = rd();
std::cout << "Random seed = " << test_global_seed << std::endl;
break;
}
if (std::regex_match(boost::unit_test::framework::master_test_suite().argv[i + 1],
std::regex(("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?")))) {
test_global_seed = atoi(boost::unit_test::framework::master_test_suite().argv[i + 1]);
break;
}
}
}

BOOST_TEST_MESSAGE("test_global_seed = " << test_global_seed);
test_global_rnd_engine = boost::random::mt11213b(test_global_seed);
test_global_alg_rnd_engine<field1_type> = nil::crypto3::random::algebraic_engine<field1_type>(test_global_seed);
}

void setup() {
}

void teardown() {
}

~test_initializer() {}
};

BOOST_TEST_GLOBAL_FIXTURE(test_initializer);

BOOST_AUTO_TEST_SUITE(marshalling_fri_proof_elements)
BOOST_FIXTURE_TEST_SUITE(marshalling_fri_proof_elements, zk::test_tools::random_test_initializer<algebra::curves::bls12<381>::scalar_field_type>)
static constexpr std::size_t lambda = 40;
static constexpr std::size_t m = 2;

Expand All @@ -387,24 +337,22 @@ BOOST_AUTO_TEST_SUITE(marshalling_fri_proof_elements)
auto _f = nil::crypto3::marshalling::types::make_fri_math_polynomial<Endianness, polynomial_type>(filled_polynomial);
BOOST_CHECK(f == _f);


f = generate_random_polynomial<field_type>(2048, test_global_alg_rnd_engine<field_type>);
f = generate_random_polynomial<field_type>(2048, alg_random_engines.template get_alg_engine<field_type>());
filled_polynomial = nil::crypto3::marshalling::types::fill_fri_math_polynomial<Endianness, polynomial_type>(f);

_f = nil::crypto3::marshalling::types::make_fri_math_polynomial<Endianness, polynomial_type>(filled_polynomial);
BOOST_CHECK(f == _f);
}


BOOST_AUTO_TEST_CASE(merkle_proof_vector_test) {
std::vector<typename FRI::merkle_proof_type> mp;
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, test_global_rnd_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));
mp.push_back(generate_random_merkle_proof<FRI>(5, generic_random_engine));

auto filled = nil::crypto3::marshalling::types::fill_merkle_proof_vector<Endianness, FRI>(mp);
auto _f = nil::crypto3::marshalling::types::make_merkle_proof_vector<Endianness, FRI>(filled);
Expand Down Expand Up @@ -442,8 +390,8 @@ BOOST_AUTO_TEST_SUITE(marshalling_fri_proof_elements)
lambda,
false,
batch_info,
test_global_alg_rnd_engine<field_type>,
test_global_rnd_engine
alg_random_engines.template get_alg_engine<field_type>(),
generic_random_engine
);
test_fri_proof<Endianness, FRI>(proof, batch_info, fri_params);
}
Expand All @@ -463,15 +411,15 @@ BOOST_AUTO_TEST_SUITE(marshalling_fri_proof_elements)
lambda,
true,
batch_info,
test_global_alg_rnd_engine<field_type>,
test_global_rnd_engine
alg_random_engines.template get_alg_engine<field_type>(),
generic_random_engine
);
test_fri_proof<Endianness, FRI>(proof, batch_info, fri_params);
}
BOOST_AUTO_TEST_SUITE_END()


BOOST_AUTO_TEST_SUITE(marshalling_real_fri_proofs)
BOOST_FIXTURE_TEST_SUITE(marshalling_real_fri_proofs, zk::test_tools::random_test_initializer<algebra::curves::pallas::base_field_type>)
using Endianness = nil::marshalling::option::big_endian;

BOOST_AUTO_TEST_CASE(marshalling_fri_basic_test) {
Expand Down Expand Up @@ -503,7 +451,7 @@ BOOST_AUTO_TEST_CASE(marshalling_fri_basic_test) {
params_type params(
d - 1, // max_degree
D,
generate_random_step_list(r, 3, test_global_rnd_engine),
generate_random_step_list(r, 3, generic_random_engine),
2, //expand_factor
lambda
);
Expand Down Expand Up @@ -535,4 +483,3 @@ BOOST_AUTO_TEST_CASE(marshalling_fri_basic_test) {
//BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));
}
BOOST_AUTO_TEST_SUITE_END()

12 changes: 1 addition & 11 deletions test/kzg_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,17 @@

#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <iostream>
#include <iomanip>
#include <random>
#include <regex>

#include <nil/crypto3/random/algebraic_random_device.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>
#include <nil/crypto3/algebra/random_element.hpp>

#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/field_type.hpp>
#include <nil/marshalling/endianness.hpp>

#include <nil/crypto3/multiprecision/cpp_int.hpp>
#include <nil/crypto3/multiprecision/number.hpp>

#include <nil/crypto3/algebra/random_element.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>

Expand Down Expand Up @@ -79,8 +71,6 @@
#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/keccak.hpp>

#include <nil/crypto3/random/algebraic_random_device.hpp>

#include <nil/crypto3/zk/commitments/detail/polynomial/eval_storage.hpp>
#include <nil/crypto3/zk/commitments/polynomial/kzg.hpp>
#include <nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp>
Expand Down Expand Up @@ -214,4 +204,4 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(batched_kzg_test, F, TestFixtures) {
}

BOOST_AUTO_TEST_SUITE_END()
*/
*/
58 changes: 6 additions & 52 deletions test/lpc_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

#include <nil/crypto3/zk/commitments/polynomial/fri.hpp>
#include <nil/crypto3/zk/commitments/polynomial/lpc.hpp>
#include <nil/crypto3/zk/test_tools/random_test_initializer.hpp>

using namespace nil::crypto3;

Expand Down Expand Up @@ -391,53 +392,6 @@ void test_lpc_proof(typename LPC::proof_type &proof, typename LPC::fri_type::par
}*/
}

// *******************************************************************************
// * Randomness setup
// *******************************************************************************/
using dist_type = std::uniform_int_distribution<int>;
std::size_t test_global_seed = 0;
boost::random::mt11213b test_global_rnd_engine;
template<typename FieldType>
nil::crypto3::random::algebraic_engine<FieldType> test_global_alg_rnd_engine;

struct test_initializer {
// Enumerate all fields used in tests;
using field1_type = algebra::curves::bls12<381>::scalar_field_type;

test_initializer() {
test_global_seed = 0;

for (std::size_t i = 0; i < std::size_t(boost::unit_test::framework::master_test_suite().argc - 1); i++) {
if (std::string(boost::unit_test::framework::master_test_suite().argv[i]) == "--seed") {
if (std::string(boost::unit_test::framework::master_test_suite().argv[i + 1]) == "random") {
std::random_device rd;
test_global_seed = rd();
std::cout << "Random seed = " << test_global_seed << std::endl;
break;
}
if (std::regex_match(boost::unit_test::framework::master_test_suite().argv[i + 1],
std::regex(("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?")))) {
test_global_seed = atoi(boost::unit_test::framework::master_test_suite().argv[i + 1]);
break;
}
}
}

BOOST_TEST_MESSAGE("test_global_seed = " << test_global_seed);
test_global_rnd_engine = boost::random::mt11213b(test_global_seed);
test_global_alg_rnd_engine<field1_type> = nil::crypto3::random::algebraic_engine<field1_type>(test_global_seed);
}

void setup() {
}

void teardown() {
}

~test_initializer() {
}
};

BOOST_AUTO_TEST_SUITE(marshalling_random)
// setup
static constexpr std::size_t lambda = 40;
Expand All @@ -461,7 +415,7 @@ BOOST_AUTO_TEST_SUITE(marshalling_random)
using LPC = typename nil::crypto3::zk::commitments::batched_list_polynomial_commitment<field_type, lpc_params_type>;
using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme<LPC, math::polynomial<typename field_type::value_type>>;

BOOST_FIXTURE_TEST_CASE(lpc_proof_test, test_initializer) {
BOOST_FIXTURE_TEST_CASE(lpc_proof_test, zk::test_tools::random_test_initializer<field_type>) {

typename FRI::params_type fri_params(1, r + 1, lambda, 4);

Expand All @@ -470,8 +424,8 @@ BOOST_FIXTURE_TEST_CASE(lpc_proof_test, test_initializer) {
fri_params.step_list,
lambda,
false,
test_global_alg_rnd_engine<typename LPC::basic_fri::field_type>,
test_global_rnd_engine
alg_random_engines.template get_alg_engine<field_type>(),
generic_random_engine
);
test_lpc_proof<Endianness, lpc_scheme_type>(proof, fri_params);
}
Expand All @@ -487,7 +441,7 @@ BOOST_AUTO_TEST_SUITE(marshalling_real)
using transcript_hash_type = nil::crypto3::hashes::keccak_1600<256>;
using merkle_tree_type = typename containers::merkle_tree<merkle_hash_type, 2>;

BOOST_FIXTURE_TEST_CASE(batches_num_3_test, test_initializer){
BOOST_FIXTURE_TEST_CASE(batches_num_3_test, zk::test_tools::random_test_initializer<field_type>){
// Setup types.
constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t k = 1;
Expand Down Expand Up @@ -519,7 +473,7 @@ BOOST_FIXTURE_TEST_CASE(batches_num_3_test, test_initializer){
typename fri_type::params_type fri_params(
d - 1, // max_degree
D,
generate_random_step_list(r, 1, test_global_rnd_engine),
generate_random_step_list(r, 1, generic_random_engine),
2, //expand_factor
lambda
);
Expand Down
Loading
Loading