From b813e37cc5bbff2c8abf52f61da1efa318ca2508 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Tue, 4 Jun 2024 11:41:33 +0300 Subject: [PATCH 1/4] work on transcript #247 transcript specialization for points #247 Added support of field and curve elements to transcript #247 Add specialization for poseidon #247 Added minmal tests for transcript #247 --- .../crypto3/zk/commitments/polynomial/kzg.hpp | 61 ++----------------- .../zk/commitments/polynomial/kzg_v2.hpp | 61 +++---------------- .../nil/crypto3/zk/transcript/fiat_shamir.hpp | 38 +++++++++++- .../nil/crypto3/zk/transcript/transcript.hpp | 2 + .../test/transcript/transcript.cpp | 50 +++++++++++++++ 5 files changed, 101 insertions(+), 111 deletions(-) diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp index 5d79eb33..526c4a68 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp @@ -332,43 +332,17 @@ namespace nil { /* The procedure of updating the transcript is subject to review and change * #295 */ - nil::marshalling::status_type status; - for (const auto &commit : public_key.commits) { - std::vector byteblob = - nil::marshalling::pack(commit, status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZG::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(commit); } for (const auto &S : public_key.S) { for (const auto &s : S) { - std::vector byteblob = - nil::marshalling::pack(s, status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZG::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(s); } } for (const auto &r : public_key.r) { for (std::size_t i = 0; i < r.size(); ++i) { - std::vector byteblob = - nil::marshalling::pack(r[i], status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZG::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(r[i]); } } } @@ -697,26 +671,12 @@ namespace nil { * #295 */ // Push commitments to transcript - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(_commitments[batch_ind]) - >(_commitments[batch_ind]) - ); + transcript(_commitments[batch_ind]); // Push evaluation points to transcript for(std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) { for(std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) { - nil::marshalling::status_type status; - std::vector byteblob = - nil::marshalling::pack(this->_z.get(batch_ind, i, j), status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(this->_z.get(batch_ind, i, j)); } } @@ -724,16 +684,7 @@ namespace nil { for (std::size_t i = 0; i < this->_points[batch_ind].size(); i++) { auto poly = this->get_U(batch_ind, i); for (std::size_t j = 0; j < poly.size(); ++j) { - nil::marshalling::status_type status; - std::vector byteblob = - nil::marshalling::pack(poly[j], status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(poly[j]); } } } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp index 8d260b15..92fe4833 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp @@ -151,26 +151,12 @@ namespace nil { * #295 */ // Push commitments to transcript - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(_commitments[batch_ind]) - >(_commitments[batch_ind]) - ); + transcript(_commitments[batch_ind]); // Push evaluation points to transcript for( std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++){ for( std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++ ) { - nil::marshalling::status_type status; - std::vector byteblob = - nil::marshalling::pack(this->_z.get(batch_ind, i, j), status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(this->_z.get(batch_ind, i, j)); } } @@ -178,16 +164,7 @@ namespace nil { for (std::size_t i = 0; i < this->_points[batch_ind].size(); i++) { auto poly = this->get_U(batch_ind, i); for (std::size_t j = 0; j < poly.size(); ++j) { - nil::marshalling::status_type status; - std::vector byteblob = - nil::marshalling::pack(poly[j], status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + transcript(poly[j]); } } } @@ -279,16 +256,7 @@ namespace nil { typename KZGScheme::single_commitment_type pi_1 = nil::crypto3::zk::algorithms::commit_one(_params, f); - nil::marshalling::status_type status; - std::vector pi1_byteblob = nil::marshalling::pack(pi_1, status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(pi1_byteblob) - >(pi1_byteblob) - ); + transcript(pi_1); auto theta_2 = transcript.template challenge(); math::polynomial theta_2_vanish = {{ -theta_2, KZGScheme::scalar_value_type::one() }}; @@ -319,14 +287,7 @@ namespace nil { typename KZGScheme::single_commitment_type pi_2 = nil::crypto3::zk::algorithms::commit_one(_params, L); /* TODO: Review the necessity of sending pi_2 to transcript */ - std::vector pi2_byteblob = nil::marshalling::pack(pi_2, status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(pi2_byteblob) - >(pi2_byteblob) - ); + transcript(pi_2); return {this->_z, pi_1, pi_2}; } @@ -346,15 +307,9 @@ namespace nil { } auto theta = transcript.template challenge(); - nil::marshalling::status_type status; - std::vector byteblob = nil::marshalling::pack(proof.pi_1, status); - BOOST_ASSERT(status == nil::marshalling::status_type::success); - transcript( - ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< - typename KZGScheme::transcript_hash_type::word_type, - decltype(byteblob) - >(byteblob) - ); + + transcript(proof.pi_1); + auto theta_2 = transcript.template challenge(); auto theta_i = KZGScheme::scalar_value_type::one(); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp b/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp index e5392a67..35b98a3f 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -149,7 +150,10 @@ namespace nil { } template - void operator()(const InputRange &r) { + typename std::enable_if_t< + !algebra::is_group_element::value && + !algebra::is_field_element::value> + operator()(const InputRange &r) { auto acc_convertible = hash(state); state = accumulators::extract::hash( hash(r, static_cast &>(acc_convertible))); @@ -162,6 +166,21 @@ namespace nil { hash(first, last, static_cast &>(acc_convertible))); } + template + typename std::enable_if_t< + algebra::is_group_element::value || + algebra::is_field_element::value + > + operator()(element const& data) { + nil::marshalling::status_type status; + std::vector byte_data = + nil::marshalling::pack(data, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + auto acc_convertible = hash(state); + state = accumulators::extract::hash( + hash(byte_data, static_cast &>(acc_convertible))); + } + template // typename std::enable_if<(Hash::digest_bits >= Field::modulus_bits), // typename Field::value_type>::type @@ -257,15 +276,28 @@ namespace nil { sponge.absorb(hash(first, last)); } - void operator()(const typename hash_type::digest_type input) { + void operator()(const typename hash_type::digest_type &input) { sponge.absorb(input); } template - void operator()(const InputRange &r) { + typename std::enable_if_t< + !algebra::is_group_element::value + > + operator()(const InputRange &r) { sponge.absorb(static_cast(hash(r))); } + template + typename std::enable_if_t< + algebra::is_group_element::value + > + operator()(element const& data) { + auto affine = data.to_affine(); + sponge.absorb(affine.X); + sponge.absorb(affine.Y); + } + template void operator()(InputIterator first, InputIterator last) { sponge.absorb(hash(first, last)); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/transcript/transcript.hpp b/libs/parallel-zk/include/nil/crypto3/zk/transcript/transcript.hpp index 7a9c2696..03654db4 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/transcript/transcript.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/transcript/transcript.hpp @@ -26,6 +26,8 @@ #ifndef CRYPTO3_ZK_TRANSCRIPT_HPP #define CRYPTO3_ZK_TRANSCRIPT_HPP +#include + namespace nil { namespace crypto3 { namespace zk { diff --git a/libs/parallel-zk/test/transcript/transcript.cpp b/libs/parallel-zk/test/transcript/transcript.cpp index 47a8e9ac..1b124c21 100644 --- a/libs/parallel-zk/test/transcript/transcript.cpp +++ b/libs/parallel-zk/test/transcript/transcript.cpp @@ -35,6 +35,9 @@ #include #include #include +#include +#include +#include #include #include @@ -113,3 +116,50 @@ BOOST_AUTO_TEST_CASE(zk_poseidon_transcript_no_init_test) { } BOOST_AUTO_TEST_SUITE_END() + +/* TODO: Write more elaborate tests for transcript of curve elements */ +BOOST_AUTO_TEST_SUITE(transcript_test_curves) + +template +void test_transcript(typename curve_type::base_field_type::value_type const& expected_value) +{ + using field_type = typename curve_type::base_field_type; + using g1_type = typename curve_type::template g1_type<>; + + transcript::fiat_shamir_heuristic_sequential transcript; + + transcript(g1_type::value_type::one()); + auto challenge = transcript.template challenge(); + + BOOST_CHECK_EQUAL(challenge, expected_value); +} + +BOOST_AUTO_TEST_CASE(mnt4_keccak) { + test_transcript> + (0xb985b0419fda7e26db3867b38cbb55465717e8d3ff208768cac6949bd68c2b7_cppui_modular298); +} + +BOOST_AUTO_TEST_CASE(mnt6_keccak) { + test_transcript> + (0x56d23a0a6f75fe3a7670906b341b29cdde80696fc418771e3c84910217546ef1_cppui_modular298); +} + +BOOST_AUTO_TEST_CASE(bls12_keccak) { + test_transcript> + (0x7cc24317960f68f067e0a1cfe610fe3db024d52b064ff2115ea0f594602f0784_cppui_modular381); + /* TODO: no marshalling for bls12-377 curve + test_transcript> + (0x0_cppui_modular377); + */ +} + +BOOST_AUTO_TEST_CASE(pallas_poseidon) { + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using hash_type = hashes::poseidon>; + + test_transcript + (0xb4a4cca5ad2d998a81ce64953c1fe0b16e27e4d298808165644421eebd2bc3a_cppui_modular256); +} + +BOOST_AUTO_TEST_SUITE_END() From 06ecaf0b0bc40a55e6794aed276ea5c43355995f Mon Sep 17 00:00:00 2001 From: Mikhail Komarov Date: Fri, 7 Jun 2024 08:13:08 +0300 Subject: [PATCH 2/4] Initial patch #254 --- flake.lock | 65 +- flake.nix | 2 +- .../zk/commitments/batched_commitment.hpp | 6 +- .../polynomial/kimchi_pedersen.hpp | 11 +- .../crypto3/zk/commitments/polynomial/kzg.hpp | 725 +++++++----- .../zk/commitments/polynomial/kzg_v2.hpp | 226 ++-- .../crypto3/zk/commitments/polynomial/lpc.hpp | 6 +- .../zk/commitments/polynomial/pedersen.hpp | 2 +- .../crypto3/zk/snark/routing/as_waksman.hpp | 92 +- .../zk/snark/systems/plonk/pickles/detail.hpp | 30 +- .../plonk/pickles/detail/kimchi_functions.hpp | 17 +- .../systems/plonk/pickles/detail/mapping.hpp | 50 +- .../snark/systems/plonk/pickles/oracles.hpp | 162 +-- .../snark/systems/plonk/pickles/verifier.hpp | 241 ++-- .../systems/plonk/pickles/verifier_index.hpp | 17 +- .../systems/ppzksnark/r1cs_gg_ppzksnark.hpp | 5 +- .../r1cs_gg_ppzksnark/marshalling.hpp | 12 +- .../ppzksnark/r1cs_gg_ppzksnark/prover.hpp | 68 +- .../ppzksnark/r1cs_ppzksnark/prover.hpp | 6 +- .../nil/crypto3/zk/transcript/fiat_shamir.hpp | 4 +- .../zk/transcript/kimchi_transcript.hpp | 218 ++-- libs/parallel-zk/test/CMakeLists.txt | 4 +- .../test/commitment/kimchi_pedersen.cpp | 8 +- libs/parallel-zk/test/commitment/kzg.cpp | 1049 +++++++++-------- .../test/commitment/type_traits.cpp | 41 +- libs/parallel-zk/test/math/expression.cpp | 210 ++-- .../test/relations/numeric/qap.cpp | 46 +- .../test/relations/numeric/sap.cpp | 47 +- .../test/relations/numeric/ssp.cpp | 40 +- .../test_routing_algorithms.cpp | 16 +- .../test/systems/plonk/pickles/kimchi.cpp | 439 +++---- .../test/systems/plonk/pickles/pickles.cpp | 5 +- .../placeholder/placeholder_circuits.cpp | 186 +-- .../plonk/placeholder/placeholder_curves.cpp | 41 +- .../placeholder/placeholder_gate_argument.cpp | 188 +-- .../plonk/placeholder/placeholder_hashes.cpp | 43 +- .../plonk/placeholder/placeholder_kzg.cpp | 60 +- .../placeholder_lookup_argument.cpp | 534 +++++---- .../placeholder_permutation_argument.cpp | 347 +++--- ...placeholder_quotient_polynomial_chunks.cpp | 35 +- .../placeholder/placeholder_test_runner.hpp | 165 ++- .../run_r1cs_gg_ppzksnark_tvm_marshalling.hpp | 183 +-- .../test/transcript/kimchi_transcript.cpp | 748 ++++++------ 43 files changed, 3423 insertions(+), 2977 deletions(-) diff --git a/flake.lock b/flake.lock index 6a7d210f..bcce5a64 100644 --- a/flake.lock +++ b/flake.lock @@ -2,27 +2,67 @@ "nodes": { "crypto3": { "inputs": { + "nix-3rdparty": "nix-3rdparty", "nixpkgs": [ "nixpkgs" ] }, "locked": { - "lastModified": 1717589484, - "narHash": "sha256-rGz9U/PJML8zHmuefCM2n1OP443EuXKRMaYRpvuxanw=", + "lastModified": 1718024088, + "narHash": "sha256-CMuiZC9ReLMz+RRyJKhfmATPklQxZaadZPN/i1RGPYY=", "ref": "refs/heads/master", - "rev": "887b4dbcc1adfe57d7236620f01d079537245b75", - "revCount": 9116, + "rev": "89246c5ba1db9f47a13e826ab5bcc840110fb390", + "revCount": 9140, "submodules": true, "type": "git", "url": "https://github.com/NilFoundation/crypto3" }, "original": { - "rev": "887b4dbcc1adfe57d7236620f01d079537245b75", "submodules": true, "type": "git", "url": "https://github.com/NilFoundation/crypto3" } }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nix-3rdparty": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": [ + "crypto3", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1717519917, + "narHash": "sha256-GqzEqEW4Uz9Z7uDZwers0t9seWRNbRWPNE3OJnjE1Uw=", + "owner": "NilFoundation", + "repo": "nix-3rdparty", + "rev": "a2e45429aa25a4a6e8e362ef17df6f197312f224", + "type": "github" + }, + "original": { + "owner": "NilFoundation", + "repo": "nix-3rdparty", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1717530100, @@ -44,6 +84,21 @@ "crypto3": "crypto3", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 1d69f47f..1250f828 100644 --- a/flake.nix +++ b/flake.nix @@ -5,7 +5,7 @@ nixpkgs.url = github:NixOS/nixpkgs/nixos-23.11; crypto3 = { url = - "git+https://github.com/NilFoundation/crypto3?submodules=1&rev=887b4dbcc1adfe57d7236620f01d079537245b75"; + "git+https://github.com/NilFoundation/crypto3?submodules=1"; inputs.nixpkgs.follows = "nixpkgs"; }; }; diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/batched_commitment.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/batched_commitment.hpp index e1ef4879..8955f4a8 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/batched_commitment.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/batched_commitment.hpp @@ -64,14 +64,14 @@ namespace nil { using commitment_type = typename ParamsType::commitment_type; using field_type = typename ParamsType::field_type; using transcript_type = TranscriptType; - using poly_type = PolynomialType; + using polynomial_type = PolynomialType; eval_storage _z; polys_evaluator() = default; protected: - std::map> _polys; + std::map> _polys; std::map _locked; // _locked[batch] is true after it is commited std::map>> _points; @@ -205,7 +205,7 @@ namespace nil { return root; } - void append_to_batch(std::size_t index, const poly_type& poly){ + void append_to_batch(std::size_t index, const polynomial_type& poly){ if( _locked.find(index) == _locked.end() ) _locked[index] = false; BOOST_ASSERT(!_locked[index]); // We cannot modify batch after commitment _polys[index].push_back(poly); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kimchi_pedersen.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kimchi_pedersen.hpp index ac37b545..e4556086 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kimchi_pedersen.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kimchi_pedersen.hpp @@ -250,7 +250,7 @@ namespace nil { typedef poly_comm blinding_type; typedef std::tuple blinded_commitment_type; - struct poly_type_single { + struct polynomial_type_single { // polynomial itself math::polynomial coeffs; // optional degree bound - poly degree must not exceed it @@ -258,11 +258,12 @@ namespace nil { // chunked commitment blinding_type commit; - poly_type_single(math::polynomial &coeffs, int bound, - blinding_type &commit) : coeffs(coeffs), bound(bound), commit(commit) {} + polynomial_type_single(math::polynomial &coeffs, + int bound, + blinding_type &commit) : coeffs(coeffs), bound(bound), commit(commit) {} }; - typedef std::vector poly_type; + typedef std::vector polynomial_type; struct proof_type { std::vector> lr; @@ -383,7 +384,7 @@ namespace nil { } static proof_type proof_eval(const params_type ¶ms, group_map_type &group_map, - const poly_type &plms, + const polynomial_type &plms, const std::vector &elm, const typename scalar_field_type::value_type &polyscale, const typename scalar_field_type::value_type &evalscale, diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp index 526c4a68..b25fe40a 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp @@ -68,8 +68,7 @@ namespace nil { * @brief The KZG Polynomial Commitment with Fiat-Shamir heuristic. * * References: - * "Constant-Size Commitments to Polynomials and - * Their Applications", + * "Constant-Size Commitments to Polynomials and Their Applications", * Aniket Kate, Gregory M. Zaverucha, and Ian Goldberg, * */ @@ -97,6 +96,7 @@ namespace nil { verification_key_type verification_key; params_type() {} + params_type(std::size_t d) { auto alpha = algebra::random_element(); verification_key = verification_key_type::one() * alpha; @@ -104,20 +104,22 @@ namespace nil { auto alpha_com = commitment_type::one(); for (std::size_t i = 0; i < d; i++) { commitment_key[i] = alpha_com; - alpha_com = alpha*alpha_com; + alpha_com = alpha * alpha_com; } } + params_type(std::size_t d, scalar_value_type alpha) { verification_key = verification_key_type::one() * alpha; commitment_key.resize(d); auto alpha_com = commitment_type::one(); for (std::size_t i = 0; i < d; i++) { commitment_key[i] = alpha_com; - alpha_com = alpha *alpha_com; + alpha_com = alpha * alpha_com; } } + params_type(single_commitment_type ck, verification_key_type vk) : - commitment_key(ck), verification_key(vk) {} + commitment_key(ck), verification_key(vk) {} }; struct public_key_type { @@ -126,41 +128,47 @@ namespace nil { scalar_value_type eval; public_key_type() = default; + public_key_type(commitment_type c, scalar_value_type z, scalar_value_type e) - : commit(c), z(z), eval(e) {} + : commit(c), z(z), eval(e) {} - public_key_type& operator=(const public_key_type &other) = default; + public_key_type &operator=(const public_key_type &other) = default; }; }; } // namespace commitments namespace algorithms { - template, KZG>::value, - bool>::type = true> - static typename KZG::commitment_type commit(const typename KZG::params_type ¶ms, - const typename math::polynomial &f) { + template, CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::commitment_type + commit(const typename CommitmentSchemeType::params_type ¶ms, + const typename math::polynomial &f) { BOOST_ASSERT(f.size() <= params.commitment_key.size()); - return algebra::multiexp(params.commitment_key.begin(), - params.commitment_key.begin() + f.size(), f.begin(), f.end(), 1); + return algebra::multiexp( + params.commitment_key.begin(), + params.commitment_key.begin() + f.size(), + f.begin(), f.end(), 1); } - template, KZG>::value, - bool>::type = true> - static typename KZG::proof_type proof_eval(typename KZG::params_type params, - const typename math::polynomial &f, - typename KZG::scalar_value_type z) { + template, CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::proof_type + proof_eval(typename CommitmentSchemeType::params_type params, + const typename math::polynomial &f, + typename CommitmentSchemeType::scalar_value_type z) { // We need two scopes on the next line to force it to use the initializer list version, // not another constructor with 2 params. - const typename math::polynomial denominator_polynom = {{-z, KZG::scalar_value_type::one()}}; + const typename math::polynomial denominator_polynom = { + {-z, CommitmentSchemeType::scalar_value_type::one()}}; - typename math::polynomial q(f); + typename math::polynomial q(f); q[0] -= f.evaluate(z); auto r = q % denominator_polynom; if (!r.is_zero()) { @@ -168,41 +176,49 @@ namespace nil { } q /= denominator_polynom; - return commit(params, q); + return commit(params, q); } - template, KZG>::value, - bool>::type = true> - static typename KZG::proof_type proof_eval(typename KZG::params_type params, - const typename math::polynomial &f, - typename KZG::public_key_type &pk) { - - return proof_eval(params, f, pk.z); + template, CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::proof_type + proof_eval(typename CommitmentSchemeType::params_type params, + const typename math::polynomial &f, + typename CommitmentSchemeType::public_key_type &pk) { + + return proof_eval(params, f, pk.z); } - template, KZG>::value, - bool>::type = true> - static bool verify_eval(const typename KZG::params_type ¶ms, - const typename KZG::proof_type &proof, - const typename KZG::public_key_type &public_key) { - - auto A_1 = algebra::precompute_g1(proof); - auto A_2 = algebra::precompute_g2(params.verification_key - - public_key.z * KZG::curve_type::template g2_type<>::value_type::one()); - auto B_1 = algebra::precompute_g1(public_key.eval * KZG::curve_type::template g1_type<>::value_type::one() - - public_key.commit); - auto B_2 = algebra::precompute_g2(KZG::curve_type::template g2_type<>::value_type::one()); - - typename KZG::gt_value_type gt3 = algebra::double_miller_loop(A_1, A_2, B_1, B_2); - typename KZG::gt_value_type gt_4 = algebra::final_exponentiation(gt3); - - return gt_4 == KZG::gt_value_type::one(); + template, CommitmentSchemeType>::value, + bool>::type = true> + static bool verify_eval(const typename CommitmentSchemeType::params_type ¶ms, + const typename CommitmentSchemeType::proof_type &proof, + const typename CommitmentSchemeType::public_key_type &public_key) { + + auto A_1 = algebra::precompute_g1(proof); + auto A_2 = algebra::precompute_g2( + params.verification_key - + public_key.z * + CommitmentSchemeType::curve_type::template g2_type<>::value_type::one()); + auto B_1 = algebra::precompute_g1( + public_key.eval * CommitmentSchemeType::curve_type::template g1_type<>::value_type::one() - + public_key.commit); + auto B_2 = algebra::precompute_g2( + CommitmentSchemeType::curve_type::template g2_type<>::value_type::one()); + + typename CommitmentSchemeType::gt_value_type gt3 = algebra::double_miller_loop( + A_1, A_2, + B_1, B_2); + typename CommitmentSchemeType::gt_value_type gt_4 = algebra::final_exponentiation( + gt3); + + return gt_4 == CommitmentSchemeType::gt_value_type::one(); } } // namespace algorithms @@ -216,10 +232,8 @@ namespace nil { * Dan Boneh, Justin Drake, Ben Fisch, * */ - template< - typename CurveType, typename TranscriptHashType, - typename PolynomialType = math::polynomial_dfs - > + template> struct batched_kzg { constexpr static bool is_kzg = true; @@ -233,8 +247,8 @@ namespace nil { using scalar_value_type = typename curve_type::scalar_field_type::value_type; using single_commitment_type = typename curve_type::template g1_type<>::value_type; using verification_key_type = typename curve_type::template g2_type<>::value_type; - using poly_type = PolynomialType; - using batch_of_polynomials_type = std::vector; + using polynomial_type = PolynomialType; + using batch_of_polynomials_type = std::vector; using evals_type = std::vector>; using transcript_type = transcript::fiat_shamir_heuristic_sequential; using serializer = typename nil::marshalling::curve_element_serializer; @@ -246,7 +260,7 @@ namespace nil { struct proof_type { eval_storage_type z; - single_commitment_type kzg_proof; + single_commitment_type kzg_proof; }; struct params_type { @@ -274,6 +288,7 @@ namespace nil { alpha_ver *= alpha; } } + params_type(std::size_t d, std::size_t t, scalar_value_type alpha) { commitment_key.resize(d); verification_key.resize(t + 1); @@ -288,8 +303,11 @@ namespace nil { alpha_ver = alpha * alpha_ver; } } - params_type(std::vector commitment_key, std::vector verification_key) : - commitment_key(commitment_key), verification_key(verification_key) {}; + + params_type(std::vector commitment_key, + std::vector verification_key) : + commitment_key(commitment_key), verification_key(verification_key) {}; + params_type operator=(const params_type &other) { commitment_key = other.commitment_key; verification_key = other.verification_key; @@ -301,13 +319,15 @@ namespace nil { std::vector commits; std::vector T; // merged eval points std::vector> S; // eval points - std::vector r; // U polynomials + std::vector r; // U polynomials public_key_type() {}; - public_key_type(std::vector commits, - std::vector T, - std::vector> S, - std::vector r) : - commits(commits), T(T), S(S), r(r) {}; + + public_key_type(const std::vector &commits, + const std::vector &T, + const std::vector> &S, + const std::vector &r) : + commits(commits), T(T), S(S), r(r) {}; + public_key_type operator=(const public_key_type &other) { commits = other.commits; T = other.T; @@ -320,49 +340,74 @@ namespace nil { } // namespace commitments namespace algorithms { - template, - KZG>::value, - bool>::type = true> - static void update_transcript(const typename KZG::public_key_type &public_key, - typename KZG::transcript_type &transcript) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static void update_transcript(const typename CommitmentSchemeType::public_key_type &public_key, + typename CommitmentSchemeType::transcript_type &transcript) { /* The procedure of updating the transcript is subject to review and change * #295 */ - for (const auto &commit : public_key.commits) { - transcript(commit); + nil::marshalling::status_type status; + + for (const auto &commit: public_key.commits) { + std::vector byteblob = + nil::marshalling::pack(commit, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } - for (const auto &S : public_key.S) { - for (const auto &s : S) { - transcript(s); + for (const auto &S: public_key.S) { + for (const auto &s: S) { + std::vector byteblob = + nil::marshalling::pack(s, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } - for (const auto &r : public_key.r) { + for (const auto &r: public_key.r) { for (std::size_t i = 0; i < r.size(); ++i) { - transcript(r[i]); + std::vector byteblob = + nil::marshalling::pack(r[i], status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } } // Duplicates get_U functions logic - template, KZG>::value, - bool - >::type = true + template, CommitmentSchemeType>::value, + bool + >::type = true > - static std::vector create_evals_polys( - const typename KZG::batch_of_polynomials_type &polys, - const std::vector> S - ) { + static std::vector + create_evals_polys(const typename CommitmentSchemeType::batch_of_polynomials_type &polys, + const std::vector> S) { BOOST_ASSERT(polys.size() == S.size()); - std::vector rs(polys.size()); + std::vector rs(polys.size()); for (std::size_t i = 0; i < polys.size(); ++i) { - typename std::vector> evals; - for (auto s : S[i]) { + typename std::vector> evals; + for (auto s: S[i]) { evals.push_back(std::make_pair(s, polys[i].evaluate(s))); } rs[i] = math::lagrange_interpolation(evals); @@ -370,184 +415,188 @@ namespace nil { return rs; } - template, - KZG>::value, - bool>::type = true> - static typename KZG::single_commitment_type commit_one( - const typename KZG::params_type ¶ms, - const typename math::polynomial &poly - ) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::single_commitment_type + commit_one(const typename CommitmentSchemeType::params_type ¶ms, + const typename math::polynomial &poly) { BOOST_ASSERT(poly.size() <= params.commitment_key.size()); - return algebra::multiexp( - params.commitment_key.begin(), - params.commitment_key.begin() + poly.size(), - poly.begin(), poly.end(), 1 + return algebra::multiexp( + params.commitment_key.begin(), + params.commitment_key.begin() + poly.size(), + poly.begin(), poly.end(), 1 ); } - template, - KZG>::value, - bool>::type = true> - static typename KZG::single_commitment_type commit_one( - const typename KZG::params_type ¶ms, - const typename math::polynomial_dfs &poly - ) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::single_commitment_type commit_one( + const typename CommitmentSchemeType::params_type ¶ms, + const typename math::polynomial_dfs &poly) { auto poly_normal = poly.coefficients(); BOOST_ASSERT(poly_normal.size() <= params.commitment_key.size()); - return algebra::multiexp(params.commitment_key.begin(), - params.commitment_key.begin() + poly_normal.size(), poly_normal.begin(), poly_normal.end(), 1); + return algebra::multiexp( + params.commitment_key.begin(), + params.commitment_key.begin() + + poly_normal.size(), poly_normal.begin(), + poly_normal.end(), 1); } - template>, - KZG>::value, - bool>::type = true> - static typename KZG::multi_commitment_type - commit(const typename KZG::params_type ¶ms, const std::vector> &polys ) { - typename KZG::multi_commitment_type commitments; + template>, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::multi_commitment_type + commit(const typename CommitmentSchemeType::params_type ¶ms, + const std::vector> &polys) { + typename CommitmentSchemeType::multi_commitment_type commitments; commitments.resize(polys.size()); for (std::size_t i = 0; i < polys.size(); ++i) { BOOST_ASSERT(polys[i].size() <= params.commitment_key.size()); - commitments[i] = commit_one(params, polys[i]); + commitments[i] = commit_one(params, polys[i]); } return commitments; } - template, - KZG>::value, - bool>::type = true> - static typename KZG::multi_commitment_type - commit(const typename KZG::params_type ¶ms, const std::vector> &polys ) { - typename KZG::multi_commitment_type commitments; + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::multi_commitment_type + commit(const typename CommitmentSchemeType::params_type ¶ms, + const std::vector> &polys) { + typename CommitmentSchemeType::multi_commitment_type commitments; commitments.resize(polys.size()); for (std::size_t i = 0; i < polys.size(); ++i) { BOOST_ASSERT(polys[i].size() <= params.commitment_key.size()); - commitments[i] = commit_one(params, polys[i]); + commitments[i] = commit_one(params, polys[i]); } return commitments; } - template, - KZG>::value, - bool>::type = true> - static std::vector - merge_eval_points(std::vector> S) { - std::set result; + template, + CommitmentSchemeType>::value, + bool>::type = true> + static std::vector + merge_eval_points(std::vector> S) { + std::set result; for (std::size_t i = 0; i < S.size(); ++i) { result.insert(S[i].begin(), S[i].end()); } - return std::vector(result.begin(), result.end()); + return std::vector(result.begin(), result.end()); } - template, - KZG>::value, - bool>::type = true> - static typename KZG::verification_key_type commit_g2( - typename KZG::params_type ¶ms, - typename math::polynomial poly - ) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::verification_key_type commit_g2( + typename CommitmentSchemeType::params_type ¶ms, + typename math::polynomial poly) { BOOST_ASSERT(poly.size() <= params.verification_key.size()); - typename KZG::verification_key_type result; + typename CommitmentSchemeType::verification_key_type result; auto it1 = params.verification_key.begin(); auto it2 = params.verification_key.begin() + poly.size(); - result = algebra::multiexp( - it1, it2, - poly.begin(), poly.end(), 1 + result = algebra::multiexp( + it1, it2, + poly.begin(), poly.end(), 1 ); return result; } - template, - KZG>::value, - bool>::type = true> - static typename math::polynomial - create_polynom_by_zeros(const std::vector S) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename math::polynomial + create_polynom_by_zeros(const std::vector S) { assert(S.size() > 0); - typename math::polynomial Z = {{-S[0], KZG::scalar_value_type::one()}}; + typename math::polynomial Z = { + {-S[0], CommitmentSchemeType::scalar_value_type::one()}}; for (std::size_t i = 1; i < S.size(); ++i) { - Z *= typename math::polynomial({-S[i], KZG::scalar_value_type::one()}); + Z *= typename math::polynomial( + {-S[i], CommitmentSchemeType::scalar_value_type::one()}); } return Z; } - template, - KZG>::value, - bool>::type = true> - static typename math::polynomial - set_difference_polynom(std::vector T, - std::vector S) { + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename math::polynomial + set_difference_polynom(std::vector T, + std::vector S) { std::sort(T.begin(), T.end()); std::sort(S.begin(), S.end()); - std::vector result; + std::vector result; std::set_difference(T.begin(), T.end(), S.begin(), S.end(), std::back_inserter(result)); if (result.size() == 0) { - return typename math::polynomial( - {{KZG::scalar_value_type::one()}}); + return typename math::polynomial( + {{CommitmentSchemeType::scalar_value_type::one()}}); } - return create_polynom_by_zeros(result); + return create_polynom_by_zeros(result); } - template, - KZG>::value, - bool>::type = true> - static typename KZG::single_commitment_type - proof_eval(const typename KZG::params_type ¶ms, - const typename KZG::batch_of_polynomials_type &polys, - typename KZG::public_key_type &public_key, - typename KZG::transcript_type &transcript - ) { - update_transcript(public_key, transcript); - - auto gamma = transcript.template challenge(); - auto factor = KZG::scalar_value_type::one(); - typename KZG::poly_type accum; + template, + CommitmentSchemeType>::value, + bool>::type = true> + static typename CommitmentSchemeType::single_commitment_type + proof_eval(const typename CommitmentSchemeType::params_type ¶ms, + const typename CommitmentSchemeType::batch_of_polynomials_type &polys, + typename CommitmentSchemeType::public_key_type &public_key, + typename CommitmentSchemeType::transcript_type &transcript) { + update_transcript(public_key, transcript); + + auto gamma = transcript.template challenge(); + auto factor = CommitmentSchemeType::scalar_value_type::one(); + typename CommitmentSchemeType::polynomial_type accum; for (std::size_t i = 0; i < polys.size(); ++i) { auto spare_poly = polys[i] - public_key.r[i]; - auto denom = create_polynom_by_zeros(public_key.S[i]); - for (auto s : public_key.S[i]) { + auto denom = create_polynom_by_zeros(public_key.S[i]); + for (auto s: public_key.S[i]) { assert(spare_poly.evaluate(s).is_zero()); assert(denom.evaluate(s).is_zero()); } - assert(spare_poly % denom == typename math::polynomial( - {{KZG::scalar_value_type::zero()}})); + assert(spare_poly % denom == + typename math::polynomial( + {{CommitmentSchemeType::scalar_value_type::zero()}})); spare_poly /= denom; accum += spare_poly * factor; factor *= gamma; @@ -556,127 +605,149 @@ namespace nil { //verify without pairing /* { - typename math::polynomial right_side({{0}}); - factor = KZG::scalar_value_type::one(); + typename math::polynomial right_side({{0}}); + factor = CommitmentSchemeType::scalar_value_type::one(); for (std::size_t i = 0; i < polys.size(); ++i) { - right_side = right_side + factor * (polys[i] - public_key.r[i]) * set_difference_polynom(public_key.T, public_key.S[i]); + right_side = right_side + factor * (polys[i] - public_key.r[i]) * set_difference_polynom(public_key.T, public_key.S[i]); factor = factor * gamma; } - assert(accum * create_polynom_by_zeros(public_key.T) == right_side); + assert(accum * create_polynom_by_zeros(public_key.T) == right_side); }*/ - return commit_one(params, accum); + return commit_one(params, accum); } - template, - KZG>::value, - bool>::type = true> - static bool verify_eval(typename KZG::params_type params, - const typename KZG::single_commitment_type &proof, - const typename KZG::public_key_type &public_key, - typename KZG::transcript_type &transcript) { - update_transcript(public_key, transcript); - - auto gamma = transcript.template challenge(); - auto factor = KZG::scalar_value_type::one(); - auto left_side_pairing = KZG::gt_value_type::one(); + template, + CommitmentSchemeType>::value, + bool>::type = true> + static bool verify_eval(typename CommitmentSchemeType::params_type params, + const typename CommitmentSchemeType::single_commitment_type &proof, + const typename CommitmentSchemeType::public_key_type &public_key, + typename CommitmentSchemeType::transcript_type &transcript) { + update_transcript(public_key, transcript); + + auto gamma = transcript.template challenge(); + auto factor = CommitmentSchemeType::scalar_value_type::one(); + auto left_side_pairing = CommitmentSchemeType::gt_value_type::one(); for (std::size_t i = 0; i < public_key.commits.size(); ++i) { - auto r_commit = commit_one(params, public_key.r[i]); + auto r_commit = commit_one(params, public_key.r[i]); auto left = factor * (public_key.commits[i] - r_commit); - auto right = commit_g2(params, set_difference_polynom(public_key.T, public_key.S[i])); + auto right = commit_g2(params, + set_difference_polynom( + public_key.T, public_key.S[i])); if (public_key.commits.size() == 1) { - assert(right == KZG::verification_key_type::one()); + assert(right == CommitmentSchemeType::verification_key_type::one()); } - left_side_pairing = left_side_pairing * algebra::pair_reduced(left, right); + left_side_pairing = + left_side_pairing * + algebra::pair_reduced(left, right); factor = factor * gamma; } - auto right = commit_g2(params, create_polynom_by_zeros(public_key.T)); - auto right_side_pairing = algebra::pair_reduced(proof, right); + auto right = commit_g2(params, create_polynom_by_zeros( + public_key.T)); + auto right_side_pairing = algebra::pair_reduced(proof, + right); return left_side_pairing == right_side_pairing; } } // namespace algorithms - namespace commitments{ + namespace commitments { // Placeholder-friendly class - template + template class kzg_commitment_scheme : - public polys_evaluator< - typename KZGScheme::params_type, - typename KZGScheme::commitment_type, - typename KZGScheme::poly_type> { + public polys_evaluator< + typename CommitmentSchemeType::params_type, + typename CommitmentSchemeType::commitment_type, + typename CommitmentSchemeType::polynomial_type> { public: - using curve_type = typename KZGScheme::curve_type; - using field_type = typename KZGScheme::field_type; - using params_type = typename KZGScheme::params_type; + using curve_type = typename CommitmentSchemeType::curve_type; + using field_type = typename CommitmentSchemeType::field_type; + using params_type = typename CommitmentSchemeType::params_type; // This should be marshallable and transcriptable type - using commitment_type = typename KZGScheme::commitment_type; - using transcript_type = typename KZGScheme::transcript_type; - using transcript_hash_type = typename KZGScheme::transcript_hash_type; - using poly_type = typename KZGScheme::poly_type; - using proof_type = typename KZGScheme::proof_type; + using commitment_type = typename CommitmentSchemeType::commitment_type; + using transcript_type = typename CommitmentSchemeType::transcript_type; + using transcript_hash_type = typename CommitmentSchemeType::transcript_hash_type; + using polynomial_type = typename CommitmentSchemeType::polynomial_type; + using proof_type = typename CommitmentSchemeType::proof_type; using endianness = nil::marshalling::option::big_endian; private: params_type _params; std::map _commitments; - std::map> _ind_commitments; - std::vector _merged_points; + std::map> _ind_commitments; + std::vector _merged_points; protected: - typename KZGScheme::verification_key_type commit_g2(typename math::polynomial poly) { + typename CommitmentSchemeType::verification_key_type + commit_g2(typename math::polynomial poly) { BOOST_ASSERT(poly.size() <= _params.verification_key.size()); - auto result = algebra::multiexp(_params.verification_key.begin(), - _params.verification_key.begin() + poly.size(), poly.begin(), poly.end(), 1); + auto result = algebra::multiexp( + _params.verification_key.begin(), + _params.verification_key.begin() + poly.size(), poly.begin(), poly.end(), 1); return result; } // Differs from static one by input parameters - void merge_eval_points(){ - std::set set; - for( auto const &it:this->_points){ + void merge_eval_points() { + std::set set; + for (auto const &it: this->_points) { auto k = it.first; for (std::size_t i = 0; i < this->_points[k].size(); ++i) { set.insert(this->_points[k][i].begin(), this->_points[k][i].end()); } } - _merged_points = std::vector(set.begin(), set.end()); + _merged_points = std::vector(set.begin(), + set.end()); } - typename math::polynomial + typename math::polynomial set_difference_polynom( - std::vector merged_points, - std::vector points - ) { + std::vector merged_points, + std::vector points) { std::sort(merged_points.begin(), merged_points.end()); std::sort(points.begin(), points.end()); - std::vector result; - std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), std::back_inserter(result)); + std::vector result; + std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), + std::back_inserter(result)); if (result.size() == 0) { - return typename math::polynomial( - {{KZGScheme::scalar_value_type::one()}}); + return typename math::polynomial( + {{CommitmentSchemeType::scalar_value_type::one()}}); } BOOST_ASSERT(this->get_V(result) * this->get_V(points) == this->get_V(merged_points)); return this->get_V(result); } - void update_transcript(std::size_t batch_ind, typename KZGScheme::transcript_type &transcript) { + void update_transcript(std::size_t batch_ind, + typename CommitmentSchemeType::transcript_type &transcript) { /* The procedure of updating the transcript is subject to review and change * #295 */ // Push commitments to transcript - transcript(_commitments[batch_ind]); + transcript(::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(_commitments[batch_ind]) + >(_commitments[batch_ind])); // Push evaluation points to transcript - for(std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) { - for(std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) { - transcript(this->_z.get(batch_ind, i, j)); + for (std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) { + for (std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) { + nil::marshalling::status_type status; + std::vector byteblob = + nil::marshalling::pack(this->_z.get(batch_ind, i, j), status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } @@ -684,10 +755,20 @@ namespace nil { for (std::size_t i = 0; i < this->_points[batch_ind].size(); i++) { auto poly = this->get_U(batch_ind, i); for (std::size_t j = 0; j < poly.size(); ++j) { - transcript(poly[j]); + nil::marshalling::status_type status; + std::vector byteblob = + nil::marshalling::pack(poly[j], status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } } + public: // Interface function. Isn't useful here. void mark_batch_as_fixed(std::size_t index) { @@ -696,18 +777,20 @@ namespace nil { kzg_commitment_scheme(params_type kzg_params) : _params(kzg_params) {} // Differs from static, because we pack the result into byte blob. - commitment_type commit(std::size_t index){ + commitment_type commit(std::size_t index) { this->_ind_commitments[index] = {}; this->state_commited(index); std::vector result; for (std::size_t i = 0; i < this->_polys[index].size(); ++i) { BOOST_ASSERT(this->_polys[index][i].degree() <= _params.commitment_key.size()); - auto single_commitment = nil::crypto3::zk::algorithms::commit_one(_params, this->_polys[index][i]); + auto single_commitment = nil::crypto3::zk::algorithms::commit_one( + _params, + this->_polys[index][i]); this->_ind_commitments[index].push_back(single_commitment); nil::marshalling::status_type status; std::vector single_commitment_bytes = - nil::marshalling::pack(single_commitment, status); + nil::marshalling::pack(single_commitment, status); BOOST_ASSERT(status == nil::marshalling::status_type::success); result.insert(result.end(), single_commitment_bytes.begin(), single_commitment_bytes.end()); } @@ -716,32 +799,35 @@ namespace nil { } using preprocessed_data_type = bool; - preprocessed_data_type preprocess(transcript_type& transcript) const{ + + preprocessed_data_type preprocess(transcript_type &transcript) const { return true; } - void setup(transcript_type& transcript, preprocessed_data_type b = true) { + void setup(transcript_type &transcript, preprocessed_data_type b = true) { // Nothing to be done here. } - proof_type proof_eval(transcript_type &transcript){ + proof_type proof_eval(transcript_type &transcript) { this->eval_polys(); this->merge_eval_points(); - for( auto const &it: this->_commitments ){ + for (auto const &it: this->_commitments) { auto k = it.first; update_transcript(k, transcript); } - auto gamma = transcript.template challenge(); - auto factor = KZGScheme::scalar_value_type::one(); - typename math::polynomial accum = - {{KZGScheme::scalar_value_type::zero()}}; + auto gamma = transcript.template challenge(); + auto factor = CommitmentSchemeType::scalar_value_type::one(); + typename math::polynomial accum = + {{CommitmentSchemeType::scalar_value_type::zero()}}; - for( auto const &it: this->_polys ){ + for (auto const &it: this->_polys) { auto k = it.first; for (std::size_t i = 0; i < this->_z.get_batch_size(k); ++i) { - accum += factor * ( math::polynomial( this->_polys[k][i].coefficients()) - this->get_U(k, i)) / this->get_V(this->_points[k][i]); + accum += factor * (math::polynomial( + this->_polys[k][i].coefficients()) - this->get_U(k, i)) / + this->get_V(this->_points[k][i]); factor *= gamma; } } @@ -750,26 +836,25 @@ namespace nil { //if something goes wrong, it may be useful to place here verification with pairings /* { - typename math::polynomial right_side({{0}}); - factor = KZGScheme::scalar_value_type::one(); + typename math::polynomial right_side({{0}}); + factor = CommitmentSchemeType::scalar_value_type::one(); for( auto const &it: this->_polys ){ auto k = it.first; for (std::size_t i = 0; i < this->_points[k].size(); ++i) { - right_side = right_side + (factor * (math::polynomial(this->_polys[k][i].coefficients()) - this->get_U(k, i)) * + right_side = right_side + (factor * (math::polynomial(this->_polys[k][i].coefficients()) - this->get_U(k, i)) * set_difference_polynom(this->_merged_points, this->_points[k][i])); factor = factor * gamma; } } assert(accum * this->get_V(this->_merged_points) == right_side); }*/ - return {this->_z, nil::crypto3::zk::algorithms::commit_one(_params, accum)}; + return {this->_z, + nil::crypto3::zk::algorithms::commit_one(_params, accum)}; } - bool verify_eval( - const proof_type &proof, - const std::map &commitments, - transcript_type &transcript - ) { + bool verify_eval(const proof_type &proof, + const std::map &commitments, + transcript_type &transcript) { this->merge_eval_points(); this->_commitments = commitments; this->_z = proof.z; @@ -779,9 +864,9 @@ namespace nil { update_transcript(k, transcript); } - auto gamma = transcript.template challenge(); - auto factor = KZGScheme::scalar_value_type::one(); - auto left_side_accum = KZGScheme::gt_value_type::one(); + auto gamma = transcript.template challenge(); + auto factor = CommitmentSchemeType::scalar_value_type::one(); + auto left_side_accum = CommitmentSchemeType::gt_value_type::one(); for (const auto &it: this->_commitments) { auto k = it.first; @@ -794,33 +879,35 @@ namespace nil { } nil::marshalling::status_type status; typename curve_type::template g1_type<>::value_type - i_th_commitment = nil::marshalling::pack(byteblob, status); + i_th_commitment = nil::marshalling::pack(byteblob, status); BOOST_ASSERT(status == nil::marshalling::status_type::success); - auto U_commit = nil::crypto3::zk::algorithms::commit_one(_params, this->get_U(k,i)); + auto U_commit = nil::crypto3::zk::algorithms::commit_one(_params, + this->get_U( + k, + i)); auto diffpoly = set_difference_polynom(_merged_points, this->_points.at(k)[i]); auto diffpoly_commitment = commit_g2(diffpoly); auto left_side_pairing = nil::crypto3::algebra::pair_reduced( - factor*(i_th_commitment - U_commit), diffpoly_commitment); + factor * (i_th_commitment - U_commit), diffpoly_commitment); left_side_accum = left_side_accum * left_side_pairing; factor *= gamma; } } - auto right_side_pairing = algebra::pair_reduced( - proof.kzg_proof, - commit_g2(this->get_V(this->_merged_points)) + auto right_side_pairing = algebra::pair_reduced( + proof.kzg_proof, + commit_g2(this->get_V(this->_merged_points)) ); return left_side_accum == right_side_pairing; } - const params_type& get_commitment_params() const { + const params_type &get_commitment_params() const { return _params; } - }; } // namespace commitments } // namespace zk diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp index 92fe4833..e7bd5299 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp @@ -67,44 +67,46 @@ namespace nil { // Placeholder-friendly class, KZGv2 /** * References: - * "Efficient polynomial commitment schemes for - * multiple points and polynomials", + * "Efficient polynomial commitment schemes for multiple points and polynomials", * Dan Boneh, Justin Drake, Ben Fisch, * */ - template + template class kzg_commitment_scheme_v2 : - public polys_evaluator< - typename KZGScheme::params_type, - typename KZGScheme::commitment_type, - typename KZGScheme::poly_type> { + public polys_evaluator< + typename CommitmentSchemeType::params_type, + typename CommitmentSchemeType::commitment_type, + typename CommitmentSchemeType::polynomial_type> { public: - static constexpr bool is_kzg(){ return true; } + static constexpr bool is_kzg() { return true; } - using curve_type = typename KZGScheme::curve_type; - using field_type = typename KZGScheme::field_type; - using params_type = typename KZGScheme::params_type; + using curve_type = typename CommitmentSchemeType::curve_type; + using field_type = typename CommitmentSchemeType::field_type; + using params_type = typename CommitmentSchemeType::params_type; // This should be marshallable and transcriptable type - using commitment_type = typename KZGScheme::commitment_type; + using commitment_type = typename CommitmentSchemeType::commitment_type; using verification_key_type = typename curve_type::template g2_type<>::value_type; - using transcript_type = typename KZGScheme::transcript_type; - using transcript_hash_type = typename KZGScheme::transcript_hash_type; - using poly_type = typename KZGScheme::poly_type; + using transcript_type = typename CommitmentSchemeType::transcript_type; + using transcript_hash_type = typename CommitmentSchemeType::transcript_hash_type; + using polynomial_type = typename CommitmentSchemeType::polynomial_type; using eval_storage_type = eval_storage; - using single_commitment_type = typename KZGScheme::single_commitment_type; + using single_commitment_type = typename CommitmentSchemeType::single_commitment_type; struct proof_type { eval_storage_type z; single_commitment_type pi_1, pi_2; - bool operator==(proof_type const& other) const { + + bool operator==(proof_type const &other) const { return (z == other.z) && (pi_1 == other.pi_1) && (pi_2 == other.pi_2); } - bool operator!=(proof_type const& other) const { + + bool operator!=(proof_type const &other) const { return !(*this == other); } }; + using endianness = nil::marshalling::option::big_endian; using field_element_type = nil::crypto3::marshalling::types::field_element< nil::marshalling::field_type, @@ -113,50 +115,66 @@ namespace nil { private: params_type _params; std::map _commitments; - std::map> _ind_commitments; - std::vector _merged_points; + std::map> _ind_commitments; + std::vector _merged_points; protected: // Differs from static one by input parameters void merge_eval_points() { - std::set set; - for( auto const &it:this->_points){ + std::set set; + for (auto const &it: this->_points) { auto k = it.first; for (std::size_t i = 0; i < this->_points[k].size(); ++i) { set.insert(this->_points[k][i].begin(), this->_points[k][i].end()); } } - _merged_points = std::vector(set.begin(), set.end()); + _merged_points = std::vector(set.begin(), + set.end()); } - typename math::polynomial + typename math::polynomial set_difference_polynom( - std::vector merged_points, - std::vector points - ) { + std::vector merged_points, + std::vector points) { std::sort(merged_points.begin(), merged_points.end()); std::sort(points.begin(), points.end()); - std::vector result; - std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), std::back_inserter(result)); + std::vector result; + std::set_difference(merged_points.begin(), merged_points.end(), points.begin(), points.end(), + std::back_inserter(result)); if (result.size() == 0) { - return typename math::polynomial( - {{KZGScheme::scalar_value_type::one()}}); + return typename math::polynomial( + {{CommitmentSchemeType::scalar_value_type::one()}}); } BOOST_ASSERT(this->get_V(result) * this->get_V(points) == this->get_V(merged_points)); return this->get_V(result); } - void update_transcript(std::size_t batch_ind, typename KZGScheme::transcript_type &transcript) { + void update_transcript(std::size_t batch_ind, + typename CommitmentSchemeType::transcript_type &transcript) { /* The procedure of updating the transcript is subject to review and change * #295 */ // Push commitments to transcript - transcript(_commitments[batch_ind]); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(_commitments[batch_ind]) + >(_commitments[batch_ind]) + ); // Push evaluation points to transcript - for( std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++){ - for( std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++ ) { - transcript(this->_z.get(batch_ind, i, j)); + for (std::size_t i = 0; i < this->_z.get_batch_size(batch_ind); i++) { + for (std::size_t j = 0; j < this->_z.get_poly_points_number(batch_ind, i); j++) { + nil::marshalling::status_type status; + std::vector byteblob = + nil::marshalling::pack(this->_z.get(batch_ind, i, j), status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } @@ -164,39 +182,52 @@ namespace nil { for (std::size_t i = 0; i < this->_points[batch_ind].size(); i++) { auto poly = this->get_U(batch_ind, i); for (std::size_t j = 0; j < poly.size(); ++j) { - transcript(poly[j]); + nil::marshalling::status_type status; + std::vector byteblob = + nil::marshalling::pack(poly[j], status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob) + ); } } } + public: // Interface function. Isn't useful here. void mark_batch_as_fixed(std::size_t index) { } - static params_type create_params(std::size_t d, typename KZGScheme::scalar_value_type alpha) { + static params_type + create_params(std::size_t d, typename CommitmentSchemeType::scalar_value_type alpha) { return params_type(d, 1, alpha); } kzg_commitment_scheme_v2(params_type kzg_params) : _params(kzg_params) { - BOOST_ASSERT( kzg_params.verification_key.size() == 2); + BOOST_ASSERT(kzg_params.verification_key.size() == 2); } // Differs from static, because we pack the result into byte blob. - commitment_type commit(std::size_t index){ + commitment_type commit(std::size_t index) { this->_ind_commitments[index] = {}; this->_ind_commitments[index].resize(this->_polys[index].size()); this->state_commited(index); nil::crypto3::parallel_for(0, this->_polys[index].size(), [index, this](std::size_t i) { BOOST_ASSERT(this->_polys[index][i].degree() <= _params.commitment_key.size()); - auto single_commitment = nil::crypto3::zk::algorithms::commit_one(_params, this->_polys[index][i]); + auto single_commitment = nil::crypto3::zk::algorithms::commit_one( + _params, + this->_polys[index][i]); this->_ind_commitments[index][i] = single_commitment; }); std::vector result; for (const auto& single_commitment : this->_ind_commitments[index]) { nil::marshalling::status_type status; std::vector single_commitment_bytes = - nil::marshalling::pack(single_commitment, status); + nil::marshalling::pack(single_commitment, status); BOOST_ASSERT(status == nil::marshalling::status_type::success); result.insert(result.end(), single_commitment_bytes.begin(), single_commitment_bytes.end()); } @@ -205,24 +236,25 @@ namespace nil { } using preprocessed_data_type = bool; - preprocessed_data_type preprocess(transcript_type& transcript) const{ + + preprocessed_data_type preprocess(transcript_type &transcript) const { return true; } - void setup(transcript_type& transcript, preprocessed_data_type b = true) { + void setup(transcript_type &transcript, preprocessed_data_type b = true) { // Nothing to be done here. } - proof_type proof_eval(transcript_type &transcript){ + proof_type proof_eval(transcript_type &transcript) { this->eval_polys(); this->merge_eval_points(); - for( auto const &it: this->_commitments ){ + for (auto const &it: this->_commitments) { auto k = it.first; update_transcript(k, transcript); } - auto theta = transcript.template challenge(); + auto theta = transcript.template challenge(); std::vector theta_i_pows; theta_i_pows.push_back(0); @@ -230,9 +262,9 @@ namespace nil { auto k = it.first; theta_i_pows.push_back(theta_i_pows[theta_i_pows.size() - 1] + this->_z.get_batch_size(k)); } - std::vector> addends( + std::vector> addends( this->_polys.size(), - math::polynomial::zero() + math::polynomial::zero() ); parallel_for(0, this->_polys.size(), [this, &theta, &theta_i_pows, &addends](std::size_t polys_idx) { auto it = std::next(this->_polys.begin(), polys_idx); @@ -240,28 +272,39 @@ namespace nil { auto theta_i = theta.pow(theta_i_pows[polys_idx]); for (std::size_t i = 0; i < this->_z.get_batch_size(k); ++i) { auto diffpoly = set_difference_polynom(_merged_points, this->_points.at(k)[i]); - auto f_i = math::polynomial( this->_polys[k][i].coefficients()); + auto f_i = math::polynomial( + this->_polys[k][i].coefficients()); addends[polys_idx] += theta_i * (f_i - this->get_U(k, i)) * diffpoly; theta_i *= theta; } }, ThreadPool::PoolLevel::HIGH); - auto f = math::polynomial::zero(); + auto f = math::polynomial::zero(); for (const auto& addend : addends) { f += addend; } - BOOST_ASSERT( f % this->get_V(_merged_points) == math::polynomial::zero()); + BOOST_ASSERT(f % this->get_V(_merged_points) == + math::polynomial::zero()); f /= this->get_V(_merged_points); - typename KZGScheme::single_commitment_type pi_1 = nil::crypto3::zk::algorithms::commit_one(_params, f); + typename CommitmentSchemeType::single_commitment_type pi_1 = nil::crypto3::zk::algorithms::commit_one( + _params, f); - transcript(pi_1); + nil::marshalling::status_type status; + std::vector pi1_byteblob = nil::marshalling::pack(pi_1, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + + transcript(::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(pi1_byteblob) + >(pi1_byteblob)); auto theta_2 = transcript.template challenge(); - math::polynomial theta_2_vanish = {{ -theta_2, KZGScheme::scalar_value_type::one() }}; + math::polynomial theta_2_vanish = { + {-theta_2, CommitmentSchemeType::scalar_value_type::one()}}; - addends.assign(this->_polys.size(), math::polynomial::zero()); + addends.assign(this->_polys.size(), math::polynomial::zero()); parallel_for(0, this->_polys.size(), [this, &theta, &theta_i_pows, &addends, &theta_2](std::size_t polys_idx) { auto it = std::next(this->_polys.begin(), polys_idx); auto k = it->first; @@ -269,34 +312,38 @@ namespace nil { for (std::size_t i = 0; i < this->_z.get_batch_size(k); ++i) { auto diffpoly = set_difference_polynom(_merged_points, this->_points.at(k)[i]); auto Z_T_S_i = diffpoly.evaluate(theta_2); - auto f_i = math::polynomial(this->_polys[k][i].coefficients()); + auto f_i = math::polynomial(this->_polys[k][i].coefficients()); addends[polys_idx] += theta_i * Z_T_S_i * (f_i - this->get_U(k, i).evaluate(theta_2)); theta_i *= theta; } }, ThreadPool::PoolLevel::HIGH); - auto L = math::polynomial::zero(); + auto L = math::polynomial::zero(); for (const auto& addend : addends) { L += addend; } L -= this->get_V(_merged_points).evaluate(theta_2) * f; - BOOST_ASSERT( L.evaluate(theta_2) == KZGScheme::scalar_value_type::zero() ); + BOOST_ASSERT(L.evaluate(theta_2) == CommitmentSchemeType::scalar_value_type::zero()); L /= theta_2_vanish; - typename KZGScheme::single_commitment_type pi_2 = nil::crypto3::zk::algorithms::commit_one(_params, L); + typename CommitmentSchemeType::single_commitment_type pi_2 = nil::crypto3::zk::algorithms::commit_one( + _params, L); /* TODO: Review the necessity of sending pi_2 to transcript */ - transcript(pi_2); + std::vector pi2_byteblob = nil::marshalling::pack(pi_2, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript(::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(pi2_byteblob) + >(pi2_byteblob)); return {this->_z, pi_1, pi_2}; } - bool verify_eval( - const proof_type &proof, - const std::map &commitments, - transcript_type &transcript - ) { + bool verify_eval(const proof_type &proof, + const std::map &commitments, + transcript_type &transcript) { this->merge_eval_points(); this->_commitments = commitments; this->_z = proof.z; @@ -306,30 +353,35 @@ namespace nil { update_transcript(k, transcript); } - auto theta = transcript.template challenge(); - - transcript(proof.pi_1); - - auto theta_2 = transcript.template challenge(); - auto theta_i = KZGScheme::scalar_value_type::one(); - - auto F = KZGScheme::single_commitment_type::zero(); - auto rsum = KZGScheme::scalar_value_type::zero(); + auto theta = transcript.template challenge(); + nil::marshalling::status_type status; + std::vector byteblob = nil::marshalling::pack(proof.pi_1, status); + BOOST_ASSERT(status == nil::marshalling::status_type::success); + transcript( + ::nil::crypto3::hashes::conditional_block_to_field_elements_wrapper< + typename CommitmentSchemeType::transcript_hash_type::word_type, + decltype(byteblob) + >(byteblob)); + auto theta_2 = transcript.template challenge(); + auto theta_i = CommitmentSchemeType::scalar_value_type::one(); + + auto F = CommitmentSchemeType::single_commitment_type::zero(); + auto rsum = CommitmentSchemeType::scalar_value_type::zero(); for (const auto &it: this->_commitments) { auto k = it.first; - std::size_t blob_size = this->_commitments.at(k).size() / this->_points.at(k).size(); + std::size_t blob_size = this->_commitments[k].size() / this->_points.at(k).size(); std::vector byteblob(blob_size); - for (std::size_t i = 0; i < this->_points.at(k).size(); ++i) { + for (std::size_t i = 0; i < this->_points[k].size(); ++i) { for (std::size_t j = 0; j < blob_size; j++) { - byteblob[j] = this->_commitments.at(k)[i * blob_size + j]; + byteblob[j] = this->_commitments[k][i * blob_size + j]; } - nil::marshalling::status_type status; typename curve_type::template g1_type<>::value_type - cm_i = nil::marshalling::pack(byteblob, status); + cm_i = nil::marshalling::pack(byteblob, status); BOOST_ASSERT(status == nil::marshalling::status_type::success); - auto Z_T_S_i = set_difference_polynom(_merged_points, this->_points.at(k)[i]).evaluate(theta_2); + auto Z_T_S_i = set_difference_polynom(_merged_points, this->_points.at(k)[i]).evaluate( + theta_2); F += theta_i * Z_T_S_i * cm_i; rsum += theta_i * Z_T_S_i * this->get_U(k, i).evaluate(theta_2); @@ -337,19 +389,19 @@ namespace nil { } } - F -= rsum * KZGScheme::single_commitment_type::one(); + F -= rsum * CommitmentSchemeType::single_commitment_type::one(); F -= this->get_V(_merged_points).evaluate(theta_2) * proof.pi_1; - auto left_side_pairing = nil::crypto3::algebra::pair_reduced - ( F + theta_2 * proof.pi_2, verification_key_type::one() ); + auto left_side_pairing = nil::crypto3::algebra::pair_reduced + (F + theta_2 * proof.pi_2, verification_key_type::one()); - auto right_side_pairing = nil::crypto3::algebra::pair_reduced - ( proof.pi_2, _params.verification_key[1] ); + auto right_side_pairing = nil::crypto3::algebra::pair_reduced + (proof.pi_2, _params.verification_key[1]); return left_side_pairing == right_side_pairing; } - const params_type& get_commitment_params() const { + const params_type &get_commitment_params() const { return _params; } }; diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp index 3d1326dc..f9161e05 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp @@ -65,7 +65,7 @@ namespace nil { using proof_type = typename LPCScheme::proof_type; using transcript_type = typename LPCScheme::transcript_type; using transcript_hash_type = typename LPCScheme::transcript_hash_type; - using poly_type = PolynomialType; + using polynomial_type = PolynomialType; using lpc = LPCScheme; using eval_storage_type = typename LPCScheme::eval_storage_type; using preprocessed_data_type = std::map>; @@ -126,7 +126,7 @@ namespace nil { // Prepare z-s and combined_Q; auto theta = transcript.template challenge(); - poly_type combined_Q; + polynomial_type combined_Q; auto points = this->get_unique_points(); math::polynomial combined_Q_normal; @@ -226,7 +226,7 @@ namespace nil { ); typename fri_type::proof_type fri_proof = nil::crypto3::zk::algorithms::proof_eval< - fri_type, poly_type + fri_type, polynomial_type >( this->_polys, combined_Q, diff --git a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/pedersen.hpp b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/pedersen.hpp index 395167a5..33d0029f 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/pedersen.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/commitments/polynomial/pedersen.hpp @@ -43,7 +43,7 @@ namespace nil { class pedersen { public: typedef typename CurveType::scalar_field_type field_type; - typedef typename CurveType::template g1_type group_type; + typedef typename CurveType::template g1_type group_type; typedef typename field_type::value_type evaluation_type; typedef typename group_type::value_type commitment_type; diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/routing/as_waksman.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/routing/as_waksman.hpp index 2783614d..6c5b5f88 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/routing/as_waksman.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/routing/as_waksman.hpp @@ -149,7 +149,7 @@ namespace nil { /** * Return the height of the AS-Waksman network's top sub-network. */ - inline std::size_t as_waksman_top_height(std::size_t num_packets) const { + inline std::size_t as_waksman_top_height(std::size_t num_packets) { return num_packets / 2; } @@ -164,8 +164,9 @@ namespace nil { * * If top = true, return the top wire, otherwise return bottom wire. */ - inline std::size_t as_waksman_switch_output(size_t num_packets, std::size_t row_offset, std::size_t row_idx, - bool use_top) const{ + inline std::size_t + as_waksman_switch_output(size_t num_packets, std::size_t row_offset, std::size_t row_idx, + bool use_top) { std::size_t relpos = row_idx - row_offset; assert(relpos % 2 == 0 && relpos + 1 < num_packets); return row_offset + (relpos / 2) + (use_top ? 0 : as_waksman_top_height(num_packets)); @@ -177,8 +178,9 @@ namespace nil { * * This function is analogous to as_waksman_switch_output above. */ - inline std::size_t as_waksman_switch_input(size_t num_packets, std::size_t row_offset, std::size_t row_idx, - bool use_top) { + inline std::size_t + as_waksman_switch_input(size_t num_packets, std::size_t row_offset, std::size_t row_idx, + bool use_top) { /* Due to symmetry, this function equals as_waksman_switch_output. */ return as_waksman_switch_output(num_packets, row_offset, row_idx, use_top); } @@ -200,11 +202,11 @@ namespace nil { * This function fills out neighbors[left] and neighbors[right-1]. */ inline void construct_as_waksman_inner(size_t left, - std::size_t right, - std::size_t lo, - std::size_t hi, - const std::vector &rhs_dests, - as_waksman_topology &neighbors) { + std::size_t right, + std::size_t lo, + std::size_t hi, + const std::vector &rhs_dests, + as_waksman_topology &neighbors) { if (left > right) { return; } @@ -222,7 +224,7 @@ namespace nil { for (std::size_t packet_idx = lo; packet_idx <= hi; ++packet_idx) { neighbors[left][packet_idx].first = neighbors[left][packet_idx].second = packet_idx; neighbors[right][packet_idx].first = neighbors[right][packet_idx].second = - rhs_dests[packet_idx - lo]; + rhs_dests[packet_idx - lo]; } std::vector new_rhs_dests(subnetwork_size, -1); @@ -252,18 +254,18 @@ namespace nil { for (std::size_t row_idx = lo; row_idx < (subnetwork_size % 2 == 1 ? hi : hi + 1); row_idx += 2) { neighbors[left][row_idx].first = neighbors[left][row_idx + 1].second = - as_waksman_switch_output(subnetwork_size, lo, row_idx, true); + as_waksman_switch_output(subnetwork_size, lo, row_idx, true); neighbors[left][row_idx].second = neighbors[left][row_idx + 1].first = - as_waksman_switch_output(subnetwork_size, lo, row_idx, false); + as_waksman_switch_output(subnetwork_size, lo, row_idx, false); new_rhs_dests[as_waksman_switch_input(subnetwork_size, lo, row_idx, true) - lo] = row_idx; new_rhs_dests[as_waksman_switch_input(subnetwork_size, lo, row_idx, false) - lo] = - row_idx + 1; + row_idx + 1; neighbors[right][row_idx].first = neighbors[right][row_idx + 1].second = - rhs_dests[row_idx - lo]; + rhs_dests[row_idx - lo]; neighbors[right][row_idx].second = neighbors[right][row_idx + 1].first = - rhs_dests[row_idx + 1 - lo]; + rhs_dests[row_idx + 1 - lo]; } if (subnetwork_size % 2 == 1) { @@ -302,7 +304,8 @@ namespace nil { as_waksman_topology neighbors(width, std::vector>( - num_packets, std::make_pair(-1, -1))); + num_packets, + std::make_pair(-1, -1))); std::vector rhs_dests(num_packets); for (std::size_t packet_idx = 0; packet_idx < num_packets; ++packet_idx) { @@ -337,8 +340,8 @@ namespace nil { * - the output position for the RHS switches. */ inline bool as_waksman_get_switch_setting_from_top_bottom_decision(size_t row_offset, - std::size_t packet_idx, - bool use_top) { + std::size_t packet_idx, + bool use_top) { const std::size_t row_idx = as_waksman_get_canonical_row_idx(row_offset, packet_idx); return (packet_idx == row_idx) ^ use_top; } @@ -354,8 +357,8 @@ namespace nil { * - the output position for the LHS switches. */ inline bool as_waksman_get_top_bottom_decision_from_switch_setting(size_t row_offset, - std::size_t packet_idx, - bool switch_setting) { + std::size_t packet_idx, + bool switch_setting) { const std::size_t row_idx = as_waksman_get_canonical_row_idx(row_offset, packet_idx); return (row_idx == packet_idx) ^ switch_setting; } @@ -392,12 +395,12 @@ namespace nil { * NOTE: due to offsets, neither pi or piinv are instances of integer_permutation. */ inline void as_waksman_route_inner(size_t left, - std::size_t right, - std::size_t lo, - std::size_t hi, - const math::integer_permutation &permutation, - const math::integer_permutation &permutation_inv, - as_waksman_routing &routing) { + std::size_t right, + std::size_t lo, + std::size_t hi, + const math::integer_permutation &permutation, + const math::integer_permutation &permutation_inv, + as_waksman_routing &routing) { if (left > right) { return; } @@ -462,9 +465,9 @@ namespace nil { * using the lower subnetwork. */ const std::size_t rhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation.get(hi)); + as_waksman_get_canonical_row_idx(lo, permutation.get(hi)); const bool rhs_switch_setting = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation.get(hi), false); + lo, permutation.get(hi), false); routing[right][rhs_switch] = rhs_switch_setting; std::size_t tprime = as_waksman_switch_input(subnetwork_size, lo, rhs_switch, false); new_permutation.set(hi, tprime); @@ -500,9 +503,9 @@ namespace nil { } const bool lhs_switch_setting = routing[left][lhs_switch]; const bool use_top = as_waksman_get_top_bottom_decision_from_switch_setting( - lo, to_route, lhs_switch_setting); + lo, to_route, lhs_switch_setting); const std::size_t t = - as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); + as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); if (permutation.get(to_route) == hi) { /** * We have routed to the straight wire for the odd case, @@ -515,16 +518,16 @@ namespace nil { route_left = true; } else { const std::size_t rhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation.get(to_route)); + as_waksman_get_canonical_row_idx(lo, permutation.get(to_route)); /** * We know that the corresponding switch on the right-hand side * cannot be set, so we set it according to the incoming wire. */ assert(routing[right].find(rhs_switch) == routing[right].end()); routing[right][rhs_switch] = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation.get(to_route), use_top); + lo, permutation.get(to_route), use_top); const std::size_t tprime = - as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); + as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); new_permutation.set(t, tprime); new_permutation_inv.set(tprime, t); @@ -539,13 +542,13 @@ namespace nil { */ const std::size_t rhs_switch = as_waksman_get_canonical_row_idx(lo, to_route); const std::size_t lhs_switch = - as_waksman_get_canonical_row_idx(lo, permutation_inv.get(to_route)); + as_waksman_get_canonical_row_idx(lo, permutation_inv.get(to_route)); assert(routing[right].find(rhs_switch) != routing[right].end()); const bool rhs_switch_setting = routing[right][rhs_switch]; const bool use_top = as_waksman_get_top_bottom_decision_from_switch_setting( - lo, to_route, rhs_switch_setting); + lo, to_route, rhs_switch_setting); const bool lhs_switch_setting = as_waksman_get_switch_setting_from_top_bottom_decision( - lo, permutation_inv.get(to_route), use_top); + lo, permutation_inv.get(to_route), use_top); /* The value on the left-hand side is either the same or not set. */ auto it = routing[left].find(lhs_switch); @@ -554,7 +557,7 @@ namespace nil { const std::size_t t = as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top); const std::size_t tprime = - as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); + as_waksman_switch_output(subnetwork_size, lo, lhs_switch, use_top); new_permutation.set(tprime, t); new_permutation_inv.set(t, tprime); @@ -594,8 +597,11 @@ namespace nil { const math::integer_permutation new_permutation_upper = new_permutation.slice(lo, lo + d - 1); const math::integer_permutation new_permutation_lower = new_permutation.slice(lo + d, hi); - const math::integer_permutation new_permutation_inv_upper = new_permutation_inv.slice(lo, lo + d - 1); - const math::integer_permutation new_permutation_inv_lower = new_permutation_inv.slice(lo + d, hi); + const math::integer_permutation new_permutation_inv_upper = new_permutation_inv.slice(lo, + lo + d - + 1); + const math::integer_permutation new_permutation_inv_lower = new_permutation_inv.slice(lo + d, + hi); as_waksman_route_inner(left + 1, right - 1, @@ -620,7 +626,7 @@ namespace nil { } inline bool valid_as_waksman_routing(const math::integer_permutation &permutation, - const as_waksman_routing &routing) { + const as_waksman_routing &routing) { const std::size_t num_packets = permutation.size(); const std::size_t width = as_waksman_num_columns(num_packets); as_waksman_topology neighbors = generate_as_waksman_topology(num_packets); @@ -638,10 +644,10 @@ namespace nil { auto it2 = routing[column_idx].find(packet_idx - 1); assert((it != routing[column_idx].end()) ^ (it2 != routing[column_idx].end())); const bool switch_setting = - (it != routing[column_idx].end() ? it->second : it2->second); + (it != routing[column_idx].end() ? it->second : it2->second); routed_packet_idx = (switch_setting ? neighbors[column_idx][packet_idx].second : - neighbors[column_idx][packet_idx].first); + neighbors[column_idx][packet_idx].first); } nextperm.set(routed_packet_idx, curperm.get(packet_idx)); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp index 63f2ca14..c3268450 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail.hpp @@ -54,7 +54,7 @@ namespace nil { /// The endomorphism coefficient typename FieldType::value_type endo_coefficient; /// The MDS matrix - std::array, 3> mds; + typename hashes::detail::poseidon_constants>::mds_matrix_type mds; }; enum gate_type { @@ -108,8 +108,8 @@ namespace nil { template struct arithmetic_sponge_params { - std::vector> round_constants; - std::array,3> mds; + std::vector> round_constants; + typename hashes::detail::poseidon_constants>::mds_matrix_type mds; }; struct Column; @@ -126,38 +126,40 @@ namespace nil { typedef typename commitment_scheme::commitment_type commitment_type; typedef typename CurveType::scalar_field_type scalar_field_type; - enum lookups_used { Single, Joint } lookup_used; + enum lookups_used { + Single, Joint + } lookup_used; std::vector lookup_table; std::vector lookup_selectors; commitment_type table_ids; std::size_t max_joint_size; - + commitment_type runtime_tables_selector; bool runtime_tables_selector_is_used; - - static commitment_type combine_table(std::vector& columns, - typename scalar_field_type::value_type column_combiner, - typename scalar_field_type::value_type table_id_combiner, - commitment_type& table_id_vector, - commitment_type& runtime_vector){ + + static commitment_type combine_table(std::vector &columns, + typename scalar_field_type::value_type column_combiner, + typename scalar_field_type::value_type table_id_combiner, + commitment_type &table_id_vector, + commitment_type &runtime_vector) { typename scalar_field_type::value_type j = scalar_field_type::value_type::one(); std::vector scalars; std::vector commitments; - for(auto &comm : columns){ + for (auto &comm: columns) { scalars.push_back(j); commitments.push_back(comm); j *= column_combiner; } - if(table_id_vector.unshifted.size() != 0){ + if (table_id_vector.unshifted.size() != 0) { scalars.push_back(table_id_combiner); commitments.push_back(table_id_vector); } - if(runtime_vector.unshifted.size() != 0){ + if (runtime_vector.unshifted.size() != 0) { scalars.push_back(column_combiner); commitments.push_back(runtime_vector); } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp index ea4eed91..68b97fc4 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/kimchi_functions.hpp @@ -1,21 +1,22 @@ #ifndef CRYPTO3_ZK_KIMCHI_FUNCTIONS #define CRYPTO3_ZK_KIMCHI_FUNCTIONS -namespace nil{ - namespace crypto3{ - namespace zk{ - namespace snark{ - template +namespace nil { + namespace crypto3 { + namespace zk { + namespace snark { + template struct kimchi_functions { typedef typename CurveType::scalar_field_type scalar_field_type; typedef typename CurveType::base_field_type base_field_type; - static typename scalar_field_type::value_type shift_scalar(const typename scalar_field_type::value_type& x){ + static typename scalar_field_type::value_type + shift_scalar(const typename scalar_field_type::value_type &x) { typename scalar_field_type::value_type two = typename scalar_field_type::value_type(2); typename scalar_field_type::value_type two_pow = two.pow(scalar_field_type::modulus_bits); - if(scalar_field_type::modulus < base_field_type::modulus){ + if (scalar_field_type::modulus < base_field_type::modulus) { return (x - (two_pow + scalar_field_type::value_type::one())) / two; - } else{ + } else { return x - two_pow; } } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp index b41f3537..6613e536 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/detail/mapping.hpp @@ -1,14 +1,16 @@ #ifndef CRYPTO3_ZK_SNARK_PICKLES_TO_GROUP_MAP #define CRYPTO3_ZK_SNARK_PICKLES_TO_GROUP_MAP +#include + #include -namespace nil{ - namespace crypto3{ - namespace zk{ - namespace snark{ - template - struct group_map{ +namespace nil { + namespace crypto3 { + namespace zk { + namespace snark { + template + struct group_map { typedef typename CurveType::scalar_field_type scalar_field_type; typedef typename CurveType::base_field_type base_field_type; typedef typename CurveType::template g1_type group_type; @@ -22,7 +24,7 @@ namespace nil{ value_type sqrt_neg_three_u_squared; value_type inv_three_u_squared; - static value_type curve_eqn(value_type x){ + static value_type curve_eqn(value_type x) { value_type res = x; res *= x; res += a; @@ -33,49 +35,50 @@ namespace nil{ group_map() { u = value_type(1); - while(true){ + while (true) { fu = curve_eqn(u); - if(!fu.is_zero()){ + if (!fu.is_zero()) { break; - } - else{ + } else { ++u; } } - + value_type three_u_squared = value_type(3) * u.squared(); inv_three_u_squared = three_u_squared.inversed(); sqrt_neg_three_u_squared = (-three_u_squared).sqrt(); - sqrt_neg_three_u_squared_minus_u_over_2 = (sqrt_neg_three_u_squared - u) * (value_type(2)).inversed(); + sqrt_neg_three_u_squared_minus_u_over_2 = + (sqrt_neg_three_u_squared - u) * (value_type(2)).inversed(); } - std::array potential_xs_helper(value_type& t2, value_type& alpha){ - value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 - t2.squared() * alpha * sqrt_neg_three_u_squared; + std::array potential_xs_helper(value_type &t2, value_type &alpha) { + value_type x1 = sqrt_neg_three_u_squared_minus_u_over_2 - + t2.squared() * alpha * sqrt_neg_three_u_squared; value_type x2 = -u - x1; value_type t2_plus_fu = t2 + fu; value_type x3 = u - t2_plus_fu.squared() * alpha * t2_plus_fu * inv_three_u_squared; return std::array({x1, x2, x3}); } - std::array potential_xs(value_type& t){ + std::array potential_xs(value_type &t) { value_type t2 = t.squared(); value_type alpha = ((t2 + fu) * t2).inversed(); return potential_xs_helper(t2, alpha); } - typename group_type::value_type get_xy(value_type& t){ + typename group_type::value_type get_xy(value_type &t) { std::array xvec = potential_xs(t); - for(auto &x : xvec){ + for (auto &x: xvec) { value_type y = curve_eqn(x).sqrt(); - if(y.squared() == x.pow(3) + a * x + b){ + if (y.squared() == x.pow(3) + a * x + b) { return typename group_type::value_type(x, y); } } return typename group_type::value_type(); } - typename group_type::value_type to_group(value_type t){ + typename group_type::value_type to_group(value_type t) { return get_xy(t); } }; @@ -96,14 +99,14 @@ namespace nil{ a = a.doubled(); b = b.doubled(); - bool r_2i = multiprecision::bit_test(rep, 2 * i); + bool r_2i = boost::multiprecision::bit_test(rep, 2 * i); typename FieldType::value_type s; if (r_2i) { s = one; } else { s = neg_one; } - if (multiprecision::bit_test(rep, 2 * i + 1) == 0) { + if (boost::multiprecision::bit_test(rep, 2 * i + 1) == 0) { b += s; } else { a += s; @@ -113,13 +116,14 @@ namespace nil{ return a * endo_coeff + b; } - typename FieldType::value_type value(){ + typename FieldType::value_type value() { return _val; } ScalarChallenge(typename FieldType::value_type _val) : _val(_val) {} ScalarChallenge() = default; + typename FieldType::value_type _val; }; } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp index ac8ffac6..d848ce40 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/oracles.hpp @@ -78,7 +78,7 @@ namespace nil { /// zeta^n and (zeta * omega)^n std::array powers_of_eval_points_for_chunks; /// ? - std::vector < std::tuple < commitment_type, std::vector>>> polys; + std::vector>>> polys; /// pre-computed zeta^n typename scalar_field_type::value_type zeta1; /// The evaluation f(zeta) - t(zeta) * Z_H(zeta) @@ -87,45 +87,46 @@ namespace nil { }; template> - std::vector>> prev_chal_evals( - proof_type proof, - VerifierIndexType index, - std::vector evaluation_points, - std::array powers_of_eval_points_for_chunks){ + std::vector>> + prev_chal_evals( + proof_type proof, + VerifierIndexType index, + std::vector evaluation_points, + std::array powers_of_eval_points_for_chunks) { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename CurveType::scalar_field_type scalar_field_type; // Fr typedef typename CurveType::base_field_type base_field_type; // Fq std::vector>> prev_chal_evals; - for(auto &[chals, comm] : proof.prev_challenges){ + for (auto &[chals, comm]: proof.prev_challenges) { std::size_t b_len = 1 << chals.size(); std::vector b; prev_chal_evals.push_back(std::vector>()); - for(int i = 0; i < evaluation_points.size(); ++i){ + for (int i = 0; i < evaluation_points.size(); ++i) { // prev_chal_evals.back().push_back(std::vector()); - typename scalar_field_type::value_type full = commitment_scheme::b_poly(chals, evaluation_points[i]); - if(index.max_poly_size == b_len){ + typename scalar_field_type::value_type full = commitment_scheme::b_poly(chals, + evaluation_points[i]); + if (index.max_poly_size == b_len) { std::vector vec_full = {full}; prev_chal_evals.back().emplace_back(vec_full); - } - else{ + } else { typename scalar_field_type::value_type betaacc = scalar_field_type::value_type::one(); typename scalar_field_type::value_type diff; - for(std::size_t j = index.max_poly_size; j < b_len; ++j){ + for (std::size_t j = index.max_poly_size; j < b_len; ++j) { typename scalar_field_type::value_type b_j; - if(b.empty()){ + if (b.empty()) { b = commitment_scheme::b_poly_coefficents(chals); } b_j = b[j]; - + diff += betaacc * b[j]; betaacc *= evaluation_points[i]; } std::vector tmp_vec = { - full - (diff * powers_of_eval_points_for_chunks[i]), diff, + full - (diff * powers_of_eval_points_for_chunks[i]), diff, }; prev_chal_evals.back().emplace_back(tmp_vec); @@ -139,8 +140,8 @@ namespace nil { /// This function runs the random oracle argument template> OraclesResult oracles(proof_type proof, - VerifierIndexType index, - typename commitments::kimchi_pedersen::commitment_type p_comm) { + VerifierIndexType index, + typename commitments::kimchi_pedersen::commitment_type p_comm) { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename commitment_scheme::commitment_type commitment_type; typedef typename commitment_scheme::evaluation_type evaluation_type; @@ -160,33 +161,35 @@ namespace nil { fq_sponge.absorb_g(p_comm.unshifted); //~ 3. Absorb the commitments to the registers / witness columns with the Fq-Sponge. - for (auto &commit : proof.commitments.w_comm) { + for (auto &commit: proof.commitments.w_comm) { fq_sponge.absorb_g(commit.unshifted); } std::tuple, - typename CurveType::scalar_field_type::value_type> + typename CurveType::scalar_field_type::value_type> joint_combiner; - if(index.lookup_index_is_used) { - BOOST_ASSERT_MSG(proof.commitments.lookup_is_used, "lookup should be in proof commitments"); + if (index.lookup_index_is_used) { + BOOST_ASSERT_MSG(proof.commitments.lookup_is_used, "lookup should be in proof commitments"); - if(index.lookup_index.runtime_tables_selector_is_used){ - BOOST_ASSERT_MSG(proof.commitments.lookup.runtime_is_used, "lookup runtime should be in proof commitments"); + if (index.lookup_index.runtime_tables_selector_is_used) { + BOOST_ASSERT_MSG(proof.commitments.lookup.runtime_is_used, + "lookup runtime should be in proof commitments"); fq_sponge.absorb_g(proof.commitments.lookup.runtime.unshifted); } ScalarChallenge s; if (index.lookup_index.lookup_used == lookup_verifier_index::lookups_used::Single) { - s = ScalarChallenge(CurveType::scalar_field_type::value_type::zero()); - } - else if (index.lookup_index.lookup_used == lookup_verifier_index::lookups_used::Joint) { + s = ScalarChallenge( + CurveType::scalar_field_type::value_type::zero()); + } else if (index.lookup_index.lookup_used == + lookup_verifier_index::lookups_used::Joint) { s = ScalarChallenge(fq_sponge.challenge()); } joint_combiner = std::make_tuple(s, s.to_field(index.srs.endo_r)); - for(auto &commit : proof.commitments.lookup.sorted){ + for (auto &commit: proof.commitments.lookup.sorted) { fq_sponge.absorb_g(commit.unshifted); } } @@ -209,7 +212,7 @@ namespace nil { // proof.commitments.lookup[i] = fq_sponge.absorb_g(proof.commitments.lookup[i].aggreg.unshifted); // } - if(proof.commitments.lookup_is_used){ + if (proof.commitments.lookup_is_used) { fq_sponge.absorb_g(proof.commitments.lookup.aggreg.unshifted); } @@ -218,19 +221,21 @@ namespace nil { fq_sponge.absorb_g(proof.commitments.z_comm.unshifted); //~ 10. Sample $\alpha'$ with the Fq-Sponge. - ScalarChallenge alpha_chal = ScalarChallenge(fq_sponge.challenge()); + ScalarChallenge alpha_chal = ScalarChallenge( + fq_sponge.challenge()); //~ 11. Derive $\alpha$ from $\alpha'$ using the endomorphism (TODO: details). typename scalar_field_type::value_type alpha = alpha_chal.to_field(index.srs.endo_r); //~ 12. Enforce that the length of the $t$ commitment is of size `PERMUTS`. BOOST_ASSERT_MSG(proof.commitments.t_comm.unshifted.size() == kimchi_constant::PERMUTES, - "IncorrectCommitmentLength(t)"); + "IncorrectCommitmentLength(t)"); //~ 13. Absorb the commitment to the quotient polynomial $t$ into the argument. fq_sponge.absorb_g(proof.commitments.t_comm.unshifted); //~ 14. Sample $\zeta'$ with the Fq-Sponge. - ScalarChallenge zeta_chal = ScalarChallenge(fq_sponge.challenge()); + ScalarChallenge zeta_chal = ScalarChallenge( + fq_sponge.challenge()); //~ 15. Derive $\zeta$ from $\zeta'$ using the endomorphism (TODO: specify). typename scalar_field_type::value_type zeta = zeta_chal.to_field(index.srs.endo_r); @@ -251,21 +256,21 @@ namespace nil { std::vector w; w.reserve(proof.public_input.size()); - if(proof.public_input.size() > 0){ + if (proof.public_input.size() > 0) { w.push_back(scalar_field_type::value_type::one()); } Alphas all_alphas = index.powers_of_alpha; - + all_alphas.instantiate(alpha); - - for(int i = 0; i < proof.public_input.size(); ++i){ + + for (int i = 0; i < proof.public_input.size(); ++i) { w.push_back(w.back() * index.domain.omega); } // compute Lagrange base evaluation denominators std::vector zeta_minus_x; - for (auto &i : w) { + for (auto &i: w) { zeta_minus_x.push_back(zeta - i); } @@ -278,7 +283,7 @@ namespace nil { // ark_ff::fields::batch_inversion::>(&mut zeta_minus_x); // zeta_minus_x = zeta_minus_x.inverse() * Fr::one(); - std::transform(zeta_minus_x.begin(), zeta_minus_x.end(), zeta_minus_x.begin(), [](auto &element){ + std::transform(zeta_minus_x.begin(), zeta_minus_x.end(), zeta_minus_x.begin(), [](auto &element) { return element.inversed(); }); //~ 18. Evaluate the negated public polynomial (if present) at $\zeta$ and $\zeta\omega$. @@ -288,16 +293,17 @@ namespace nil { if (!proof.public_input.empty()) { typename scalar_field_type::value_type tmp; std::size_t iter_size = std::min({proof.public_input.size(), zeta_minus_x.size(), w.size()}); - - for(int i = 0; i < iter_size; ++i){ + + for (int i = 0; i < iter_size; ++i) { tmp -= proof.public_input[i] * zeta_minus_x[i] * w[i]; } - typename scalar_field_type::value_type size_inv = typename scalar_field_type::value_type(index.domain.size()).inversed(); + typename scalar_field_type::value_type size_inv = typename scalar_field_type::value_type( + index.domain.size()).inversed(); p_eval[0].push_back(tmp * (zeta1 - scalar_field_type::value_type::one()) * size_inv); p_eval[1].push_back(tmp * (zetaw.pow(n) - scalar_field_type::value_type::one()) * size_inv); - } else{ + } else { p_eval.resize(2); } @@ -330,8 +336,8 @@ namespace nil { //~ 25. Create a list of all polynomials that have an evaluation proof. std::vector evaluation_points = {zeta, zetaw}; std::array powers_of_eval_points_for_chunks = { - zeta.pow(index.max_poly_size), - zetaw.pow(index.max_poly_size) + zeta.pow(index.max_poly_size), + zetaw.pow(index.max_poly_size) }; // let polys : Vec<(PolyComm, _)> = @@ -343,37 +349,38 @@ namespace nil { std::vector>>> polys; std::vector>> prev_chal_evals_vec = prev_chal_evals( - proof, index, evaluation_points, powers_of_eval_points_for_chunks + proof, index, evaluation_points, powers_of_eval_points_for_chunks ); - for(int i = 0; i < proof.prev_challenges.size(); ++i){ + for (int i = 0; i < proof.prev_challenges.size(); ++i) { polys.emplace_back(std::get<1>(proof.prev_challenges[i]), prev_chal_evals_vec[i]); } std::vector> evals = { - proof.evals[0].combine(powers_of_eval_points_for_chunks[0]), - proof.evals[1].combine(powers_of_eval_points_for_chunks[1]) + proof.evals[0].combine(powers_of_eval_points_for_chunks[0]), + proof.evals[1].combine(powers_of_eval_points_for_chunks[1]) }; //~ 26. Compute the evaluation of $ft(\zeta)$. typename scalar_field_type::value_type zkp = index.zkpm.evaluate(zeta); typename scalar_field_type::value_type zeta1m1 = zeta1 - scalar_field_type::value_type::one(); - std::vector alpha_powers = all_alphas.get_alphas(argument_type::Permutation, kimchi_constant::CONSTRAINTS); + std::vector alpha_powers = all_alphas.get_alphas( + argument_type::Permutation, kimchi_constant::CONSTRAINTS); typename scalar_field_type::value_type alpha0 = alpha_powers[0]; typename scalar_field_type::value_type alpha1 = alpha_powers[1]; typename scalar_field_type::value_type alpha2 = alpha_powers[2]; - typename scalar_field_type::value_type ft_eval0 = (evals[0].w[kimchi_constant::PERMUTES - 1] + gamma) * evals[1].z * alpha0 * zkp; + typename scalar_field_type::value_type ft_eval0 = + (evals[0].w[kimchi_constant::PERMUTES - 1] + gamma) * evals[1].z * alpha0 * zkp; for (size_t i = 0; i < evals[0].s.size(); ++i) { ft_eval0 *= (beta * evals[0].s[i]) + evals[0].w[i] + gamma; } if (!p_eval.empty() && !p_eval[0].empty()) { ft_eval0 -= p_eval[0][0]; - } - else { // ?????????????? + } else { // ?????????????? ft_eval0 -= scalar_field_type::value_type::zero(); } @@ -384,35 +391,42 @@ namespace nil { ft_eval0 -= tmp; - typename scalar_field_type::value_type numerator = ((zeta1m1 * alpha1 * (zeta - index.w)) + - (zeta1m1 * alpha2 * (zeta - scalar_field_type::value_type::one()))) * (scalar_field_type::value_type::one() - evals[0].z); + typename scalar_field_type::value_type numerator = ((zeta1m1 * alpha1 * (zeta - index.w)) + + (zeta1m1 * alpha2 * (zeta - + scalar_field_type::value_type::one()))) * + (scalar_field_type::value_type::one() - + evals[0].z); - typename scalar_field_type::value_type denominator = (zeta - index.w) * (zeta - scalar_field_type::value_type::one()); + typename scalar_field_type::value_type denominator = + (zeta - index.w) * (zeta - scalar_field_type::value_type::one()); denominator = denominator.inversed(); ft_eval0 += numerator * denominator; - Constants cs{alpha, beta, gamma, std::get<1>(joint_combiner), index.endo, index.fr_sponge_params.mds}; + Constants cs{alpha, beta, gamma, std::get<1>(joint_combiner), index.endo, + index.fr_sponge_params.mds_matrix}; ft_eval0 -= - PolishToken::evaluate(index.linearization.constant_term, index.domain, zeta, evals, cs); + PolishToken::evaluate(index.linearization.constant_term, index.domain, + zeta, evals, cs); std::vector> es; - for(auto &poly : polys){ + for (auto &poly: polys) { evaluation_type eval(commitment_type(), std::get<1>(poly), -1); es.emplace_back(eval, -1); } es.emplace_back(evaluation_type(commitment_type(), p_eval, -1), -1); - std::vector> ft_eval = {{ft_eval0}, {proof.ft_eval1}}; + std::vector> ft_eval = {{ft_eval0}, + {proof.ft_eval1}}; es.emplace_back(evaluation_type(commitment_type(), ft_eval, -1), -1); std::vector> z; std::vector> generic_selector; std::vector> poseidon_selector; - for(auto &eval : proof.evals){ + for (auto &eval: proof.evals) { z.push_back(eval.z); generic_selector.push_back(eval.generic_selector); poseidon_selector.push_back(eval.poseidon_selector); @@ -421,32 +435,34 @@ namespace nil { es.emplace_back(evaluation_type(commitment_type(), generic_selector, -1), -1); es.emplace_back(evaluation_type(commitment_type(), poseidon_selector, -1), -1); - for(int i = 0; i < proof.evals[0].w.size(); ++i){ - std::vector> w_copy = {proof.evals[0].w[i], proof.evals[1].w[i]}; + for (int i = 0; i < proof.evals[0].w.size(); ++i) { + std::vector> w_copy = {proof.evals[0].w[i], + proof.evals[1].w[i]}; es.emplace_back(evaluation_type(commitment_type(), w_copy, -1), -1); } - for(int i = 0; i < proof.evals[0].s.size(); ++i){ - std::vector> s_copy = {proof.evals[0].s[i], proof.evals[1].s[i]}; + for (int i = 0; i < proof.evals[0].s.size(); ++i) { + std::vector> s_copy = {proof.evals[0].s[i], + proof.evals[1].s[i]}; es.emplace_back(evaluation_type(commitment_type(), s_copy, -1), -1); } typename scalar_field_type::value_type combined_inner_product0 = commitment_scheme::combined_inner_product( - evaluation_points, - v, - u, - es, - index.srs.g.size() + evaluation_points, + v, + u, + es, + index.srs.g.size() ); RandomOracles oracles = { - joint_combiner, beta, gamma, alpha_chal, alpha, zeta, v, u, zeta_chal, v_chal, u_chal + joint_combiner, beta, gamma, alpha_chal, alpha, zeta, v, u, zeta_chal, v_chal, u_chal }; - return OraclesResult{fq_sponge, digest, oracles, - all_alphas, p_eval, powers_of_eval_points_for_chunks, - polys, zeta1, ft_eval0, combined_inner_product0}; + return OraclesResult{fq_sponge, digest, oracles, + all_alphas, p_eval, powers_of_eval_points_for_chunks, + polys, zeta1, ft_eval0, combined_inner_product0}; } } // namespace snark } // namespace zk diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp index 0f558b1c..f95c77cd 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier.hpp @@ -49,7 +49,7 @@ namespace nil { namespace snark { template> - struct verifier{ + struct verifier { typedef commitments::kimchi_pedersen commitment_scheme; typedef typename commitment_scheme::commitment_type commitment_type; typedef typename commitment_scheme::evaluation_type evaluation_type; @@ -75,7 +75,9 @@ namespace nil { //~ //~ 1. Commit to the negated public input polynomial. - BOOST_ASSERT_MSG(index.srs.lagrange_bases.find(index.domain.size()) != index.srs.lagrange_bases.end(), "pre-computed committed lagrange bases not found"); + BOOST_ASSERT_MSG( + index.srs.lagrange_bases.find(index.domain.size()) != index.srs.lagrange_bases.end(), + "pre-computed committed lagrange bases not found"); std::vector lgr_comm = index.srs.lagrange_bases[index.domain.size()]; // calculate lgr_comm BOOST_ASSERT(lgr_comm.size() == 512); // ?? std::vector com; @@ -87,14 +89,15 @@ namespace nil { } // std::vector *com_ref = &com; std::vector elm; - for (auto &i : proof.public_input) { + for (auto &i: proof.public_input) { elm.push_back(-i); } commitment_type p_comm = commitment_type::multi_scalar_mul(com, elm); //~ 2. Run the [Fiat-Shamir argument](#fiat-shamir-argument). - OraclesResult oracles_res = oracles(proof, index, p_comm); + OraclesResult oracles_res = oracles( + proof, index, p_comm); // fq_sponge, // oracles, // all_alphas, @@ -112,82 +115,84 @@ namespace nil { // Calculate polynoms in pointers powers_of_eval_points_for_chunks[0] and // powers_of_eval_points_for_chunks[1] std::vector> evals = { - proof.evals[0].combine(oracles_res.powers_of_eval_points_for_chunks[0]), - proof.evals[1].combine(oracles_res.powers_of_eval_points_for_chunks[1]) + proof.evals[0].combine(oracles_res.powers_of_eval_points_for_chunks[0]), + proof.evals[1].combine(oracles_res.powers_of_eval_points_for_chunks[1]) }; //~ 4. Compute the commitment to the linearized polynomial $f$. // permutation typename scalar_field_type::value_type zkp = index.zkpm.evaluate(oracles_res.oracles.zeta); - std::vector alphas = oracles_res.all_alphas.get_alphas(argument_type::Permutation, kimchi_constant::CONSTRAINTS); + std::vector alphas = oracles_res.all_alphas.get_alphas( + argument_type::Permutation, kimchi_constant::CONSTRAINTS); std::vector commitments = {index.sigma_comm[PERMUTES - 1]}; - std::vector scalars = {ConstraintSystem::perm_scalars(evals, oracles_res.oracles.beta, - oracles_res.oracles.gamma, alphas, zkp)}; + std::vector scalars = { + ConstraintSystem::perm_scalars(evals, oracles_res.oracles.beta, + oracles_res.oracles.gamma, alphas, + zkp)}; // generic - + std::vector generic_scalars = - ConstraintSystem::gnrc_scalars(alphas, evals[0].w, evals[0].generic_selector); + ConstraintSystem::gnrc_scalars(alphas, evals[0].w, + evals[0].generic_selector); std::vector generic_com( - index.coefficients_comm.begin(), index.coefficients_comm.begin() + generic_scalars.size()); + index.coefficients_comm.begin(), + index.coefficients_comm.begin() + generic_scalars.size()); BOOST_ASSERT(generic_scalars.size() == generic_com.size()); scalars.insert(scalars.end(), generic_scalars.begin(), generic_scalars.end()); commitments.insert(commitments.end(), generic_com.begin(), generic_com.end()); - + // other gates are implemented using the expression framework { // TODO: Reuse constants from oracles function Constants constants = { - oracles_res.oracles.alpha, - oracles_res.oracles.beta, - oracles_res.oracles.gamma, - std::get<1>(oracles_res.oracles.joint_combiner), - index.endo, - index.fr_sponge_params.mds - }; - - for (auto i : index.linearization.index_term) { + oracles_res.oracles.alpha, + oracles_res.oracles.beta, + oracles_res.oracles.gamma, + std::get<1>(oracles_res.oracles.joint_combiner), + index.endo, + index.fr_sponge_params.mds_matrix + }; + + for (auto i: index.linearization.index_term) { auto col = std::get<0>(i); auto tokens = std::get<1>(i); auto scalar = - PolishToken::evaluate(tokens, index.domain, oracles_res.oracles.zeta, evals, constants); + PolishToken::evaluate(tokens, index.domain, + oracles_res.oracles.zeta, evals, + constants); auto l = proof.commitments.lookup; if (col.column == column_type::Witness) { scalars.push_back(scalar); commitments.push_back(proof.commitments.w_comm[col.witness_value]); - } - else if (col.column == column_type::Coefficient) { + } else if (col.column == column_type::Coefficient) { scalars.push_back(scalar); commitments.push_back(index.coefficients_comm[col.coefficient_value]); - } - else if (col.column == column_type::Z) { + } else if (col.column == column_type::Z) { scalars.push_back(scalar); commitments.push_back(proof.commitments.z_comm); - } - else if (col.column == column_type::LookupSorted) { + } else if (col.column == column_type::LookupSorted) { scalars.push_back(scalar); commitments.push_back(l.sorted[col.lookup_sorted_value]); - } - else if (col.column == column_type::LookupAggreg) { + } else if (col.column == column_type::LookupAggreg) { scalars.push_back(scalar); commitments.push_back(l.aggreg); - } - else if (col.column == column_type::LookupKindIndex) { + } else if (col.column == column_type::LookupKindIndex) { if (index.lookup_index_is_used) { // assert("Attempted to use, but no lookup index was given"); } else { scalars.push_back(scalar); - commitments.push_back(index.lookup_index.lookup_selectors[col.lookup_kind_index_value]); + commitments.push_back( + index.lookup_index.lookup_selectors[col.lookup_kind_index_value]); } - } - else if (col.column == column_type::LookupTable) { + } else if (col.column == column_type::LookupTable) { if (index.lookup_index_is_used) { // assert("Attempted to use, but no lookup index was given"); } else { @@ -200,46 +205,35 @@ namespace nil { commitments.push_back(index.lookup_index.lookup_table[k]); } } - } - else if (col.column == column_type::Index) { + } else if (col.column == column_type::Index) { commitment_type c; - if(col.index_value == gate_type::Zero || col.index_value == gate_type::Generic || col.index_value == gate_type::Lookup){ + if (col.index_value == gate_type::Zero || col.index_value == gate_type::Generic || + col.index_value == gate_type::Lookup) { std::cout << "Selector for {:?} not defined\n"; - } - else if(col.index_value == gate_type::CompleteAdd){ + } else if (col.index_value == gate_type::CompleteAdd) { c = index.complete_add_comm; - } - else if(col.index_value == gate_type::VarBaseMul){ + } else if (col.index_value == gate_type::VarBaseMul) { c = index.mul_comm; - } - else if(col.index_value == gate_type::EndoMul){ + } else if (col.index_value == gate_type::EndoMul) { c = index.emul_comm; - } - else if(col.index_value == gate_type::EndoMulScalar){ + } else if (col.index_value == gate_type::EndoMulScalar) { c = index.endomul_scalar_comm; - } - else if(col.index_value == gate_type::Poseidon){ + } else if (col.index_value == gate_type::Poseidon) { c = index.psm_comm; - } - else if(col.index_value == gate_type::ChaCha0){ + } else if (col.index_value == gate_type::ChaCha0) { c = index.chacha_comm[0]; - } - else if(col.index_value == gate_type::ChaCha1){ + } else if (col.index_value == gate_type::ChaCha1) { c = index.chacha_comm[1]; - } - else if(col.index_value == gate_type::ChaCha2){ + } else if (col.index_value == gate_type::ChaCha2) { c = index.chacha_comm[2]; - } - else if(col.index_value == gate_type::ChaChaFinal){ + } else if (col.index_value == gate_type::ChaChaFinal) { c = index.chacha_comm[3]; - } - else if(col.index_value == gate_type::RangeCheck0){ + } else if (col.index_value == gate_type::RangeCheck0) { c = index.range_check_comm[0]; - } - else if(col.index_value == gate_type::RangeCheck1){ + } else if (col.index_value == gate_type::RangeCheck1) { c = index.range_check_comm[1]; } - + scalars.push_back(scalar); commitments.push_back(c); } @@ -249,19 +243,21 @@ namespace nil { // MSM commitment_type f_comm = commitment_type::multi_scalar_mul(commitments, scalars); - //~ 5. Compute the (chuncked) commitment of $ft$ - //~ (see [Maller's optimization](../crypto/plonk/maller_15.html)). - typename scalar_field_type::value_type zeta_to_srs_len = oracles_res.oracles.zeta.pow(index.max_poly_size); + //~ 5. Compute the (chuncked) commitment of $ft$ + //~ (see [Maller's optimization](../crypto/plonk/maller_15.html)). + typename scalar_field_type::value_type zeta_to_srs_len = oracles_res.oracles.zeta.pow( + index.max_poly_size); commitment_type chunked_f_comm = f_comm.chunk_commitment(zeta_to_srs_len); commitment_type chunked_t_comm = proof.commitments.t_comm.chunk_commitment(zeta_to_srs_len); - commitment_type ft_comm = chunked_f_comm - chunked_t_comm.scale(oracles_res.zeta1 - scalar_field_type::value_type::one()); + commitment_type ft_comm = chunked_f_comm - chunked_t_comm.scale( + oracles_res.zeta1 - scalar_field_type::value_type::one()); //~ 6. List the polynomial commitments, and their associated evaluations, //~ that are associated to the aggregated evaluation proof in the proof: std::vector evaluations; //~ - recursion - for (auto i : oracles_res.polys) { + for (auto i: oracles_res.polys) { evaluations.emplace_back(std::get<0>(i), std::get<1>(i), -1); } @@ -269,108 +265,117 @@ namespace nil { evaluations.emplace_back(p_comm, oracles_res.p_eval, -1); //~ - ft commitment (chunks of it) - std::vector> ft_comm_evals = {{oracles_res.ft_eval0}, {proof.ft_eval1}}; + std::vector> ft_comm_evals = {{oracles_res.ft_eval0}, + {proof.ft_eval1}}; evaluations.emplace_back(ft_comm, ft_comm_evals, -1); //~ - permutation commitment std::vector> tmp_evals; - for (auto &i : proof.evals) { + for (auto &i: proof.evals) { tmp_evals.push_back(i.z); } evaluations.emplace_back(proof.commitments.z_comm, tmp_evals, -1); //~ - index commitments that use the coefficients tmp_evals.clear(); - for (auto i : proof.evals) { + for (auto i: proof.evals) { tmp_evals.push_back(i.generic_selector); } evaluations.emplace_back(index.generic_comm, tmp_evals, -1); tmp_evals.clear(); - for (auto i : proof.evals) { + for (auto i: proof.evals) { tmp_evals.push_back(i.poseidon_selector); } evaluations.emplace_back(index.psm_comm, tmp_evals, -1); //~ - witness commitments for (size_t i = 0; i < COLUMNS; ++i) { - std::vector> witness_comm_evals = {proof.evals[0].w[i], proof.evals[1].w[i]}; + std::vector> witness_comm_evals = { + proof.evals[0].w[i], proof.evals[1].w[i]}; evaluations.emplace_back(proof.commitments.w_comm[i], witness_comm_evals, -1); } //~ - sigma commitments for (size_t i = 0; i < PERMUTES - 1; ++i) { - std::vector> sigma_comm_evals = {proof.evals[0].s[i], proof.evals[1].s[i]}; + std::vector> sigma_comm_evals = { + proof.evals[0].s[i], proof.evals[1].s[i]}; evaluations.emplace_back(index.sigma_comm[i], sigma_comm_evals, -1); } - if(index.lookup_index_is_used){ + if (index.lookup_index_is_used) { std::size_t lookup_len = std::min({ - proof.commitments.lookup.sorted.size(), - proof.evals[0].lookup.sorted.size(), - proof.evals[1].lookup.sorted.size(), - }); - - for(int i = 0; i < lookup_len; ++i){ - std::vector> lookup_sorted_comm_evals = {proof.evals[0].lookup.sorted[i], proof.evals[1].lookup.sorted[i]}; + proof.commitments.lookup.sorted.size(), + proof.evals[0].lookup.sorted.size(), + proof.evals[1].lookup.sorted.size(), + }); + + for (int i = 0; i < lookup_len; ++i) { + std::vector> lookup_sorted_comm_evals = { + proof.evals[0].lookup.sorted[i], proof.evals[1].lookup.sorted[i]}; evaluations.emplace_back( - proof.commitments.lookup.sorted[i], - lookup_sorted_comm_evals, - -1 + proof.commitments.lookup.sorted[i], + lookup_sorted_comm_evals, + -1 ); } - std::vector> lookup_aggreg_comm_evals = {proof.evals[0].lookup.aggreg, proof.evals[1].lookup.aggreg}; + std::vector> lookup_aggreg_comm_evals = { + proof.evals[0].lookup.aggreg, proof.evals[1].lookup.aggreg}; evaluations.emplace_back( - proof.commitments.lookup.aggreg, - lookup_aggreg_comm_evals, - -1 + proof.commitments.lookup.aggreg, + lookup_aggreg_comm_evals, + -1 ); commitment_type table_comm = lookup_verifier_index::combine_table( - index.lookup_index.lookup_table, - std::get<1>(oracles_res.oracles.joint_combiner), - std::get<1>(oracles_res.oracles.joint_combiner).pow(index.lookup_index.max_joint_size), - index.lookup_index.table_ids, - proof.commitments.lookup.runtime + index.lookup_index.lookup_table, + std::get<1>(oracles_res.oracles.joint_combiner), + std::get<1>(oracles_res.oracles.joint_combiner).pow( + index.lookup_index.max_joint_size), + index.lookup_index.table_ids, + proof.commitments.lookup.runtime ); - std::vector> lookup_table_comm_evals = {proof.evals[0].lookup.table, proof.evals[1].lookup.table}; + std::vector> lookup_table_comm_evals = { + proof.evals[0].lookup.table, proof.evals[1].lookup.table}; evaluations.emplace_back( - table_comm, - lookup_table_comm_evals, - -1 + table_comm, + lookup_table_comm_evals, + -1 ); - if(index.lookup_index.runtime_tables_selector_is_used){ - std::vector> lookup_runtime_comm_evals = {proof.evals[0].lookup.runtime, proof.evals[1].lookup.runtime}; + if (index.lookup_index.runtime_tables_selector_is_used) { + std::vector> lookup_runtime_comm_evals = { + proof.evals[0].lookup.runtime, proof.evals[1].lookup.runtime}; evaluations.emplace_back( - index.lookup_index.runtime_tables_selector, - lookup_runtime_comm_evals, - -1 + index.lookup_index.runtime_tables_selector, + lookup_runtime_comm_evals, + -1 ); } } // prepare for the opening proof verification - std::vector evaluation_points = {oracles_res.oracles.zeta, - oracles_res.oracles.zeta * index.domain.omega}; + std::vector evaluation_points = { + oracles_res.oracles.zeta, + oracles_res.oracles.zeta * index.domain.omega}; return batchproof_type({ - oracles_res.fq_sponge, - evaluations, - evaluation_points, - oracles_res.oracles.v, - oracles_res.oracles.u, - proof.proof - }); + oracles_res.fq_sponge, + evaluations, + evaluation_points, + oracles_res.oracles.v, + oracles_res.oracles.u, + proof.proof + }); } - - static bool batch_verify(group_map& g_map, - proofs_type& proofs){ + + static bool batch_verify(group_map &g_map, + proofs_type &proofs) { std::vector batch; - + typename commitment_scheme::params_type &srs = std::get<0>(proofs.front()).srs; - for(auto &[index, proof] : proofs){ + for (auto &[index, proof]: proofs) { batch.push_back(to_batch(index, proof)); } @@ -378,8 +383,8 @@ namespace nil { } static bool verify(group_map &g_map, - VerifierIndexType &index, - proof_type &proof){ + VerifierIndexType &index, + proof_type &proof) { proofs_type proofs; proofs.emplace_back(index, proof); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp index 0cacae35..e4cbca1f 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/pickles/verifier_index.hpp @@ -50,12 +50,11 @@ namespace nil { // arithmetic_sponge_params fq_sponge_params; // hashes::detail::poseidon_constants_kimchi fr_sponge_params; // hashes::detail::poseidon_constants_kimchi fq_sponge_params; - template< - typename CurveType, - typename PoseidonKimchiScalarConstants = hashes::detail::poseidon_constants>, - typename PoseidonKimchiBaseConstants = hashes::detail::poseidon_constants>, - std::size_t WiresAmount = kimchi_constant::COLUMNS, - std::size_t Permuts = kimchi_constant::PERMUTES + template>, + typename PoseidonKimchiBaseConstants = hashes::detail::poseidon_constants>, + std::size_t WiresAmount = kimchi_constant::COLUMNS, + std::size_t Permuts = kimchi_constant::PERMUTES > struct verifier_index { typedef commitments::kimchi_pedersen commitment_scheme; @@ -74,7 +73,7 @@ namespace nil { commitment_type generic_comm; commitment_type psm_comm; - + commitment_type complete_add_comm; commitment_type mul_comm; commitment_type emul_comm; @@ -96,8 +95,8 @@ namespace nil { // linearization; // TODO: // Linearization>>> Alphas powers_of_alpha; - PoseidonKimchiScalarConstants fr_sponge_params; - PoseidonKimchiBaseConstants fq_sponge_params; + PoseidonKimchiScalarConstants fr_sponge_params; + PoseidonKimchiBaseConstants fq_sponge_params; verifier_index() : domain(2) {} }; diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp index 1f20e157..1b5266b9 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark.hpp @@ -127,9 +127,8 @@ namespace nil { typedef typename policy_type::keypair_type keypair_type; typedef typename policy_type::proof_type proof_type; - template - static inline KeyPairType generate(const constraint_system_type &constraint_system) { - return Generator::template process(constraint_system); + static inline keypair_type generate(const constraint_system_type &constraint_system) { + return Generator::template process(constraint_system); } static inline proof_type prove(const proving_key_type &pk, diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/marshalling.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/marshalling.hpp index 107590ad..2a6dace6 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/marshalling.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/marshalling.hpp @@ -62,8 +62,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -227,7 +227,7 @@ namespace nil { } template - static inline linear_combination + static inline linear_combination linear_combination_process(InputIterator read_iter_begin, InputIterator read_iter_end, status_type &processingStatus) { @@ -394,7 +394,7 @@ namespace nil { static inline crypto3::zk::commitments::knowledge_commitment_vector, typename CurveType::template g1_type<>> g2g1_knowledge_commitment_vector_process(InputIterator read_iter_begin, InputIterator read_iter_end, - status_type &processingStatus) { + status_type &processingStatus) { using T = crypto3::zk::commitments::knowledge_commitment, typename CurveType::template g1_type<>>; @@ -921,7 +921,7 @@ namespace nil { field_type_process(typename FieldType::value_type input_fp, typename std::vector::iterator &write_iter) { - typedef nil::crypto3::multiprecision::number> + typedef boost::multiprecision::number> integral_type; constexpr const std::size_t modulus_bits = FieldType::modulus_bits; @@ -929,7 +929,7 @@ namespace nil { constexpr const std::size_t modulus_chunks = modulus_bits / chunk_size + (modulus_bits % chunk_size ? 1 : 0); - nil::crypto3::multiprecision::export_bits(integral_type(input_fp.data), write_iter, chunk_size, false); + boost::multiprecision::export_bits(integral_type(input_fp.data), write_iter, chunk_size, false); write_iter += modulus_chunks; } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/prover.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/prover.hpp index 2119224b..c2e39de8 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/prover.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_gg_ppzksnark/prover.hpp @@ -77,10 +77,10 @@ namespace nil { BOOST_ASSERT(proving_key.constraint_system.is_satisfied(primary_input, auxiliary_input)); const qap_witness qap_wit = - reductions::r1cs_to_qap::witness_map( - proving_key.constraint_system, primary_input, auxiliary_input, - scalar_field_type::value_type::zero(), scalar_field_type::value_type::zero(), - scalar_field_type::value_type::zero()); + reductions::r1cs_to_qap::witness_map( + proving_key.constraint_system, primary_input, auxiliary_input, + scalar_field_type::value_type::zero(), scalar_field_type::value_type::zero(), + scalar_field_type::value_type::zero()); /* We are dividing degree 2(d-1) polynomial by degree d polynomial and not adding a PGHR-style ZK-patch, so our H is degree d-2 */ @@ -100,59 +100,59 @@ namespace nil { // TODO: sort out indexing std::vector const_padded_assignment( - 1, scalar_field_type::value_type::one()); + 1, scalar_field_type::value_type::one()); const_padded_assignment.insert(const_padded_assignment.end(), qap_wit.coefficients_for_ABCs.begin(), qap_wit.coefficients_for_ABCs.end()); typename g1_type::value_type evaluation_At = - algebra::multiexp_with_mixed_addition( - proving_key.A_query.begin(), - proving_key.A_query.begin() + qap_wit.num_variables + 1, - const_padded_assignment.begin(), - const_padded_assignment.begin() + qap_wit.num_variables + 1, - chunks); + algebra::multiexp_with_mixed_addition( + proving_key.A_query.begin(), + proving_key.A_query.begin() + qap_wit.num_variables + 1, + const_padded_assignment.begin(), + const_padded_assignment.begin() + qap_wit.num_variables + 1, + chunks); typename commitments::knowledge_commitment::value_type evaluation_Bt = - commitments::kc_multiexp_with_mixed_addition( - proving_key.B_query, - 0, - qap_wit.num_variables + 1, - const_padded_assignment.begin(), - const_padded_assignment.begin() + qap_wit.num_variables + 1, - chunks); + commitments::kc_multiexp_with_mixed_addition( + proving_key.B_query, + 0, + qap_wit.num_variables + 1, + const_padded_assignment.begin(), + const_padded_assignment.begin() + qap_wit.num_variables + 1, + chunks); typename g1_type::value_type evaluation_Ht = - algebra::multiexp( - proving_key.H_query.begin(), - proving_key.H_query.begin() + (qap_wit.degree - 1), - qap_wit.coefficients_for_H.begin(), - qap_wit.coefficients_for_H.begin() + (qap_wit.degree - 1), - chunks); + algebra::multiexp( + proving_key.H_query.begin(), + proving_key.H_query.begin() + (qap_wit.degree - 1), + qap_wit.coefficients_for_H.begin(), + qap_wit.coefficients_for_H.begin() + (qap_wit.degree - 1), + chunks); typename g1_type::value_type evaluation_Lt = - algebra::multiexp_with_mixed_addition( - proving_key.L_query.begin(), - proving_key.L_query.end(), - const_padded_assignment.begin() + qap_wit.num_inputs + 1, - const_padded_assignment.begin() + qap_wit.num_variables + 1, - chunks); + algebra::multiexp_with_mixed_addition( + proving_key.L_query.begin(), + proving_key.L_query.end(), + const_padded_assignment.begin() + qap_wit.num_inputs + 1, + const_padded_assignment.begin() + qap_wit.num_variables + 1, + chunks); /* A = alpha + sum_i(a_i*A_i(t)) + r*delta */ typename g1_type::value_type g1_A = - proving_key.alpha_g1 + evaluation_At + r * proving_key.delta_g1; + proving_key.alpha_g1 + evaluation_At + r * proving_key.delta_g1; /* B = beta + sum_i(a_i*B_i(t)) + s*delta */ typename g1_type::value_type g1_B = - proving_key.beta_g1 + evaluation_Bt.h + s * proving_key.delta_g1; + proving_key.beta_g1 + evaluation_Bt.h + s * proving_key.delta_g1; typename g2_type::value_type g2_B = - proving_key.beta_g2 + evaluation_Bt.g + s * proving_key.delta_g2; + proving_key.beta_g2 + evaluation_Bt.g + s * proving_key.delta_g2; /* C = sum_i(a_i*((beta*A_i(t) + alpha*B_i(t) + C_i(t)) + H(t)*Z(t))/delta) + A*s + r*b - * r*s*delta */ typename g1_type::value_type g1_C = - evaluation_Ht + evaluation_Lt + s * g1_A + r * g1_B - (r * s) * proving_key.delta_g1; + evaluation_Ht + evaluation_Lt + s * g1_A + r * g1_B - (r * s) * proving_key.delta_g1; return proof_type(std::move(g1_A), std::move(g2_B), std::move(g1_C)); } diff --git a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/prover.hpp b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/prover.hpp index bdecd8d6..aa8c7990 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/prover.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/ppzksnark/r1cs_ppzksnark/prover.hpp @@ -101,17 +101,17 @@ namespace nil { const std::size_t chunks = 1; #endif - g_A = g_A + kc_multiexp_with_mixed_addition( + g_A = g_A + commitments::kc_multiexp_with_mixed_addition( proving_key.A_query, 1, 1 + qap_wit.num_variables, qap_wit.coefficients_for_ABCs.begin(), qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks); - g_B = g_B + kc_multiexp_with_mixed_addition( + g_B = g_B + commitments::kc_multiexp_with_mixed_addition( proving_key.B_query, 1, 1 + qap_wit.num_variables, qap_wit.coefficients_for_ABCs.begin(), qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks); - g_C = g_C + kc_multiexp_with_mixed_addition( + g_C = g_C + commitments::kc_multiexp_with_mixed_addition( proving_key.C_query, 1, 1 + qap_wit.num_variables, qap_wit.coefficients_for_ABCs.begin(), qap_wit.coefficients_for_ABCs.begin() + qap_wit.num_variables + 1, chunks); diff --git a/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp b/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp index 35b98a3f..d945c291 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/transcript/fiat_shamir.hpp @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include @@ -342,7 +342,7 @@ namespace nil { } public: - hashes::detail::nil_poseidon_sponge_construction sponge; + hashes::detail::poseidon_sponge_construction_custom sponge; }; } // namespace transcript diff --git a/libs/parallel-zk/include/nil/crypto3/zk/transcript/kimchi_transcript.hpp b/libs/parallel-zk/include/nil/crypto3/zk/transcript/kimchi_transcript.hpp index 3b078875..1a88e0a5 100644 --- a/libs/parallel-zk/include/nil/crypto3/zk/transcript/kimchi_transcript.hpp +++ b/libs/parallel-zk/include/nil/crypto3/zk/transcript/kimchi_transcript.hpp @@ -7,6 +7,9 @@ #include #include +#include +#include + #include #include @@ -16,13 +19,10 @@ #include // #include -#include -#include - namespace nil { namespace crypto3 { namespace zk { - namespace snark{ + namespace snark { template struct proof_evaluation_type; } @@ -30,10 +30,12 @@ namespace nil { constexpr static const int CHALLENGE_LENGTH_IN_LIMBS = 2; constexpr static const int HIGH_ENTROPY_LIMBS = 2; - template - integral_type pack(std::vector limbs_lsb){ + template + integral_type pack(std::vector limbs_lsb) { nil::marshalling::status_type status; - std::size_t byte_size = nil::crypto3::multiprecision::backends::max_precision::value / CHAR_BIT; + std::size_t byte_size = + boost::multiprecision::backends::max_precision::value / + CHAR_BIT; std::size_t size = byte_size / sizeof(uint64_t) + (byte_size % sizeof(uint64_t) ? 1 : 0); limbs_lsb.resize(size); std::reverse(limbs_lsb.begin(), limbs_lsb.end()); @@ -43,11 +45,12 @@ namespace nil { return res; } - template - std::vector unpack(value_type& value){ + template + std::vector unpack(value_type &value) { nil::marshalling::status_type status; integral_type scalar_value = integral_type(value.data); - std::vector limbs_lsb = nil::marshalling::pack(scalar_value, status); + std::vector limbs_lsb = nil::marshalling::pack( + scalar_value, status); std::reverse(limbs_lsb.begin(), limbs_lsb.end()); limbs_lsb.resize(CHALLENGE_LENGTH_IN_LIMBS); @@ -55,54 +58,57 @@ namespace nil { return limbs_lsb; } - template - struct BaseSponge{ + template + struct BaseSponge { typedef typename CurveType::template g1_type group_type; typedef typename CurveType::base_field_type base_field_type; typedef typename CurveType::scalar_field_type scalar_field_type; typedef nil::crypto3::hashes::detail::mina_poseidon_policy policy_type; - + constexpr static const int CHALLENGE_LENGTH_IN_LIMBS = 2; constexpr static const int HIGH_ENTROPY_LIMBS = 2; - + typedef snark::ScalarChallenge scalar_challenge_type; typedef std::uint64_t limb_type; - typename nil::crypto3::hashes::detail::poseidon_sponge_construction sponge; + typename nil::crypto3::hashes::detail::poseidon_sponge_construction_custom sponge; std::vector last_squeezed; }; - template + template struct DefaultFrSponge : public BaseSponge { typedef typename BaseSponge::group_type group_type; typedef typename BaseSponge::scalar_field_type scalar_field_type; // typedef typename BaseSponge::scalar_field_type scalar_field_type; typedef typename BaseSponge::limb_type limb_type; // typedef typename BaseSponge::scalar_challenge_type scalar_challenge_type; - + typedef nil::crypto3::hashes::detail::mina_poseidon_policy policy_type; - typename nil::crypto3::hashes::detail::poseidon_sponge_construction sponge; + typename nil::crypto3::hashes::detail::poseidon_sponge_construction_custom sponge; typedef snark::ScalarChallenge scalar_challenge_type; constexpr static const int CHALLENGE_LENGTH_IN_LIMBS = BaseSponge::CHALLENGE_LENGTH_IN_LIMBS; constexpr static const int HIGH_ENTROPY_LIMBS = BaseSponge::HIGH_ENTROPY_LIMBS; - typename scalar_field_type::value_type squeeze(std::size_t num_limbs){ - if(this->last_squeezed.size() >= num_limbs){ - std::vector limbs(this->last_squeezed.begin(), this->last_squeezed.begin() + num_limbs); - std::vector remaining(this->last_squeezed.begin() + num_limbs, this->last_squeezed.end()); + typename scalar_field_type::value_type squeeze(std::size_t num_limbs) { + if (this->last_squeezed.size() >= num_limbs) { + std::vector limbs(this->last_squeezed.begin(), + this->last_squeezed.begin() + num_limbs); + std::vector remaining(this->last_squeezed.begin() + num_limbs, + this->last_squeezed.end()); this->last_squeezed = remaining; - - return typename scalar_field_type::value_type(pack(limbs)); - } - else{ + + return typename scalar_field_type::value_type( + pack(limbs)); + } else { auto sq = this->sponge.squeeze(); nil::marshalling::status_type status; - std::vector x = unpack(sq); + std::vector x = unpack( + sq); - for(int i = 0; i < HIGH_ENTROPY_LIMBS; ++i){ + for (int i = 0; i < HIGH_ENTROPY_LIMBS; ++i) { this->last_squeezed.push_back(x[i]); } @@ -110,54 +116,75 @@ namespace nil { } } - void absorb(typename scalar_field_type::value_type x){ + void absorb(typename scalar_field_type::value_type x) { this->last_squeezed.clear(); this->sponge.absorb(x); } - scalar_challenge_type challenge(){ + scalar_challenge_type challenge() { return scalar_challenge_type(squeeze(CHALLENGE_LENGTH_IN_LIMBS)); } - void absorb_evaluations(std::vector& p, - snark::proof_evaluation_type>& e){ + void absorb_evaluations(std::vector &p, + snark::proof_evaluation_type> &e) { this->last_squeezed.clear(); - this->sponge.absorb(p); + for (const typename scalar_field_type::value_type &v: p) { + this->sponge.absorb(v); + } std::vector> points = { - e.z, - e.generic_selector, - e.poseidon_selector - }; - this->sponge.absorb(e.z); - this->sponge.absorb(e.generic_selector); - this->sponge.absorb(e.poseidon_selector); - - for(auto &w_iter : e.w){ - this->sponge.absorb(w_iter); + e.z, + e.generic_selector, + e.poseidon_selector + }; + for (const auto &v: e.z) { + this->sponge.absorb(v); + } + + for (const auto &v: e.generic_selector) { + this->sponge.absorb(v); + } + + for (const auto &v: e.poseidon_selector) { + this->sponge.absorb(v); } - for(auto &s_iter : e.s){ - this->sponge.absorb(s_iter); + for (auto &w_iter: e.w) { + for (const auto &v: w_iter) { + this->sponge.absorb(v); + } + } + + for (auto &s_iter: e.s) { + for (const auto &v: s_iter) { + this->sponge.absorb(v); + } } - if(e.lookup_is_used){ - for(auto &s : e.lookup.sorted){ - this->sponge.absorb(s); + if (e.lookup_is_used) { + for (auto &s: e.lookup.sorted) { + for (const auto &v: s) { + this->sponge.absorb(v); + } } - this->sponge.absorb(e.lookup.aggreg); - this->sponge.absorb(e.lookup.table); + for (const auto &v: e.lookup.aggreg) { + this->sponge.absorb(v); + } + for (const auto &v: e.lookup.table) { + this->sponge.absorb(v); + } - if(e.lookup.runtime_is_used){ - this->sponge.absorb(e.lookup.runtime); + if (e.lookup.runtime_is_used) { + for (const auto &v : e.lookup.runtime) { + this->sponge.absorb(v); + } } } } - }; - template + template struct DefaultFqSponge : public BaseSponge { typedef typename BaseSponge::group_type group_type; typedef typename BaseSponge::base_field_type base_field_type; @@ -168,20 +195,22 @@ namespace nil { constexpr static const int CHALLENGE_LENGTH_IN_LIMBS = BaseSponge::CHALLENGE_LENGTH_IN_LIMBS; constexpr static const int HIGH_ENTROPY_LIMBS = BaseSponge::HIGH_ENTROPY_LIMBS; - std::vector squeeze_limbs(std::size_t num_limbs){ - if(this->last_squeezed.size() >= num_limbs){ - std::vector limbs(this->last_squeezed.begin(), this->last_squeezed.begin() + num_limbs); - std::vector remaining(this->last_squeezed.begin() + num_limbs, this->last_squeezed.end()); + std::vector squeeze_limbs(std::size_t num_limbs) { + if (this->last_squeezed.size() >= num_limbs) { + std::vector limbs(this->last_squeezed.begin(), + this->last_squeezed.begin() + num_limbs); + std::vector remaining(this->last_squeezed.begin() + num_limbs, + this->last_squeezed.end()); this->last_squeezed = remaining; return limbs; - } - else{ + } else { auto sq = this->sponge.squeeze(); nil::marshalling::status_type status; - std::vector x = unpack(sq); + std::vector x = unpack( + sq); - for(int i = 0; i < HIGH_ENTROPY_LIMBS; ++i){ + for (int i = 0; i < HIGH_ENTROPY_LIMBS; ++i) { this->last_squeezed.push_back(x[i]); } @@ -189,64 +218,79 @@ namespace nil { } } - typename base_field_type::value_type squeeze_field(){ + typename base_field_type::value_type squeeze_field() { this->last_squeezed.clear(); return this->sponge.squeeze(); } - typename scalar_field_type::value_type squeeze(std::size_t num_limbs){ + typename scalar_field_type::value_type squeeze(std::size_t num_limbs) { auto limbs = this->squeeze_limbs(num_limbs); nil::marshalling::status_type status; auto first_value = pack(limbs); - typename scalar_field_type::value_type res = typename scalar_field_type::value_type(pack(limbs)); + typename scalar_field_type::value_type res = typename scalar_field_type::value_type( + pack(limbs)); return res; } - void absorb_g(std::vector& gs){ + void absorb_g(std::vector &gs) { this->last_squeezed.clear(); - for(auto &g : gs){ + for (auto &g: gs) { absorb_g(g); } } - void absorb_g(typename group_type::value_type g){ - if(!this->last_squeezed.empty()) + void absorb_g(typename group_type::value_type g) { + if (!this->last_squeezed.empty()) this->last_squeezed.clear(); this->sponge.absorb(g.X); this->sponge.absorb(g.Y); } - void absorb_fr(typename scalar_field_type::value_type f){ - if(this->last_squeezed.empty()) + void absorb_fr(typename scalar_field_type::value_type f) { + if (this->last_squeezed.empty()) this->last_squeezed.clear(); - if(scalar_field_type::modulus < base_field_type::modulus){ - typename base_field_type::value_type casted_to_base_value = typename base_field_type::value_type(typename base_field_type::integral_type(f.data)); + if (scalar_field_type::modulus < base_field_type::modulus) { + typename base_field_type::value_type casted_to_base_value = typename base_field_type::value_type( + typename base_field_type::integral_type(f.data)); this->sponge.absorb(casted_to_base_value); - } else{ + } else { nil::marshalling::status_type status; typename scalar_field_type::integral_type scalar_f(f.data); - std::vector bits = nil::marshalling::pack(scalar_f, status); - +#if defined(BOOST_OS_MACOS) && defined(BOOST_LIB_STD_CXX) + boost::container::vector bits = nil::marshalling::pack( + scalar_f, status); + boost::container::vector shifted_bits(bits.size(), false); +#else + std::vector bits = nil::marshalling::pack( + scalar_f, status); std::vector shifted_bits(bits.size(), false); +#endif + std::copy(bits.begin(), bits.end() - 1, shifted_bits.begin() + 1); - typename base_field_type::integral_type low_bit = bits.back() ? - typename base_field_type::integral_type(1) : typename base_field_type::integral_type(0); - typename base_field_type::integral_type high_bits = nil::marshalling::pack(shifted_bits, status); + typename base_field_type::integral_type low_bit = bits.back() ? + typename base_field_type::integral_type(1) + : typename base_field_type::integral_type( + 0); + typename base_field_type::integral_type high_bits = nil::marshalling::pack( + shifted_bits, status); - typename base_field_type::value_type high_bits_field = typename base_field_type::value_type(high_bits); - typename base_field_type::value_type low_bit_field = typename base_field_type::value_type(low_bit); + typename base_field_type::value_type high_bits_field = typename base_field_type::value_type( + high_bits); + typename base_field_type::value_type low_bit_field = typename base_field_type::value_type( + low_bit); this->sponge.absorb(high_bits_field); this->sponge.absorb(low_bit_field); } } - void absorb_fr(const std::vector& fs){ + + void absorb_fr(const std::vector &fs) { this->last_squeezed.clear(); - for(auto f : fs){ + for (auto f: fs) { absorb_fr(f); } } @@ -263,12 +307,14 @@ namespace nil { return scalar_challenge_type(challenge()); } - typename scalar_field_type::value_type squeeze_challenge(typename scalar_field_type::value_type endo_r) { + typename scalar_field_type::value_type + squeeze_challenge(typename scalar_field_type::value_type endo_r) { return squeeze_prechallenge().to_field(endo_r); } - typename scalar_field_type::value_type digest(){ - return typename scalar_field_type::value_type(typename scalar_field_type::integral_type(this->squeeze_field().data)); + typename scalar_field_type::value_type digest() { + return typename scalar_field_type::value_type( + typename scalar_field_type::integral_type(this->squeeze_field().data)); } }; } diff --git a/libs/parallel-zk/test/CMakeLists.txt b/libs/parallel-zk/test/CMakeLists.txt index 243032c5..d97824ad 100644 --- a/libs/parallel-zk/test/CMakeLists.txt +++ b/libs/parallel-zk/test/CMakeLists.txt @@ -119,8 +119,8 @@ set(TESTS_NAMES # "systems/ppzksnark/bacs_ppzksnark/bacs_ppzksnark" "systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark" - "systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark_marshalling" - "systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark_tvm_marshalling" +# "systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark_marshalling" +# "systems/ppzksnark/r1cs_gg_ppzksnark/r1cs_gg_ppzksnark_tvm_marshalling" "systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark" # "systems/ppzksnark/r1cs_se_ppzksnark/r1cs_se_ppzksnark" # "systems/ppzksnark/ram_ppzksnark/ram_ppzksnark" diff --git a/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp b/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp index b3bb55c6..0259665a 100644 --- a/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp +++ b/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp @@ -33,8 +33,8 @@ using batchproof_type = kimchi_pedersen::batchproof_type; using commitment_type = kimchi_pedersen::commitment_type; using blinding_type = kimchi_pedersen::blinding_type; using proof_type = kimchi_pedersen::proof_type; -using poly_type = kimchi_pedersen::poly_type; -using poly_type_single = kimchi_pedersen::poly_type_single; +using polynomial_type = kimchi_pedersen::polynomial_type; +using polynomial_type_single = kimchi_pedersen::polynomial_type_single; using evaluation_type = kimchi_pedersen::evaluation_type; using blinded_commitment_type = kimchi_pedersen::blinded_commitment_type; @@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE(kimchi_commitment_test_opening_proof) { scalar_value_type u = algebra::random_element(); scalar_value_type v = algebra::random_element(); - poly_type polys {{poly1, -1, std::get<1>(commitment)}, + polynomial_type polys {{poly1, -1, std::get<1>(commitment)}, {poly2, static_cast(poly2.degree() + 1), std::get<1>(bounded_commitment)}}; std::vector elm {algebra::random_element(), @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(kimchi_commitment_test_case) { commitments.emplace_back(CommitmentAndSecrets({eval_commit, poly, std::get<1>(blinded_commitment)})); } - poly_type polynomials; + polynomial_type polynomials; for (auto &c : commitments) { polynomials.emplace_back(c.poly, c.eval_commit.commit.bound, c.chunked_blinding); diff --git a/libs/parallel-zk/test/commitment/kzg.cpp b/libs/parallel-zk/test/commitment/kzg.cpp index bd23466b..79a51245 100644 --- a/libs/parallel-zk/test/commitment/kzg.cpp +++ b/libs/parallel-zk/test/commitment/kzg.cpp @@ -62,9 +62,9 @@ using namespace nil::crypto3; using namespace nil::crypto3::math; -void dump_vector(std::vector const& x, std::string label = "") { +void dump_vector(std::vector const &x, std::string label = "") { std::cout << label << "[" << std::dec << x.size() << "] "; - for(auto v: x) { + for (auto v: x) { std::cout << std::hex << std::setw(2) << std::setfill('0') << int(v); } std::cout << "" << std::endl; @@ -72,223 +72,227 @@ void dump_vector(std::vector const& x, std::string label = "") { BOOST_AUTO_TEST_SUITE(kzg_test_suite) -BOOST_AUTO_TEST_CASE(kzg_basic_test) { + BOOST_AUTO_TEST_CASE(kzg_basic_test) { - typedef algebra::curves::mnt6_298 curve_type; - //typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef algebra::curves::mnt6_298 curve_type; + //typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef zk::commitments::kzg kzg_type; + typedef zk::commitments::kzg kzg_type; - scalar_value_type alpha = 10u; - std::size_t n = 16; - scalar_value_type z = 2u; - const polynomial f = {{scalar_value_type::modulus - 1u, 1u, 2u, 3u}}; + scalar_value_type alpha = 10u; + std::size_t n = 16; + scalar_value_type z = 2u; + const polynomial f = {{scalar_value_type::modulus - 1u, 1u, 2u, 3u}}; - auto params = typename kzg_type::params_type(n, alpha); - BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); - BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); - BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); - BOOST_CHECK(alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); - BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); + auto params = typename kzg_type::params_type(n, alpha); + BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); + BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); + BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); + BOOST_CHECK( + alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); + BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); - auto commit = zk::algorithms::commit(params, f); + auto commit = zk::algorithms::commit(params, f); - BOOST_CHECK_EQUAL(3209u * curve_type::template g1_type<>::value_type::one(), commit); + BOOST_CHECK_EQUAL(3209u * curve_type::template g1_type<>::value_type::one(), commit); - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); -} + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + } -BOOST_AUTO_TEST_CASE(kzg_basic_test_mnt6) { + BOOST_AUTO_TEST_CASE(kzg_basic_test_mnt6) { - typedef algebra::curves::mnt6_298 curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef algebra::curves::mnt6_298 curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef zk::commitments::kzg kzg_type; + typedef zk::commitments::kzg kzg_type; - scalar_value_type alpha = 10u; - std::size_t n = 16; - scalar_value_type z = 2u; - const polynomial f = {scalar_value_type::modulus - 1u, 1u, 2u, 3u}; + scalar_value_type alpha = 10u; + std::size_t n = 16; + scalar_value_type z = 2u; + const polynomial f = {scalar_value_type::modulus - 1u, 1u, 2u, 3u}; - auto params = typename kzg_type::params_type(n, alpha); - BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); - BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); - BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); - BOOST_CHECK(alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); - BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); + auto params = typename kzg_type::params_type(n, alpha); + BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); + BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); + BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); + BOOST_CHECK( + alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); + BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); - auto commit = zk::algorithms::commit(params, f); - BOOST_CHECK(3209 * curve_type::template g1_type<>::value_type::one() == commit); + auto commit = zk::algorithms::commit(params, f); + BOOST_CHECK(3209 * curve_type::template g1_type<>::value_type::one() == commit); - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); -} + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + } -BOOST_AUTO_TEST_CASE(kzg_test_mnt6_accumulated) { + BOOST_AUTO_TEST_CASE(kzg_test_mnt6_accumulated) { - typedef algebra::curves::mnt6_298 curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef algebra::curves::mnt6_298 curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef zk::commitments::kzg kzg_type; - - scalar_value_type alpha = 7u; - std::size_t n = 8; - scalar_value_type z = 2u; - const polynomial f = { - 0x0ed6fb07f52c1f1ef7952250702368474f20fd7af906ba3a5842cdb7946c69b603852bf1069_cppui_modular298, - 0x14db9efba58de09f8ccb1d73fefce45393856e6a7509006561fe67ea354ec69d791b44c1476_cppui_modular298, - 0x0e9fa83a6f8891bc7e6aa1afae85e11dd80cdef32dfcef7cedc12792cf74141c899c8fb1f98_cppui_modular298, - 0x101cc0b43782ca40ae5bf96aabf461e1a623ab9284acac3bb6d55bff4429356dad714ee0bd0_cppui_modular298, - 0x1310586a4d1ed251d1e4c95711fb9346a2b233649f5ce32fe1cf3aea423d131787187a13799_cppui_modular298, - 0x0d9ed064a24e83ac6134de7cca08bdc3e31ffd4db0a004b63039f76821ec2cc53b7e6a74735_cppui_modular298, - 0x2839e48822f55b4e487b817ddf06a6e32e0dcc0c2ced1e738d38fec15bd4717d7680dda90ec_cppui_modular298, - }; - - auto f_eval = f.evaluate(alpha); - - auto params = typename kzg_type::params_type(n, alpha); - auto commit = zk::algorithms::commit(params, f); - nil::marshalling::status_type status; - using endianness = nil::marshalling::option::big_endian; - std::vector single_commitment_bytes = - nil::marshalling::pack(commit, status); - dump_vector(single_commitment_bytes, "commitment"); - - BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); - BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); - BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); - BOOST_CHECK(alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); - BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); - - BOOST_CHECK(f_eval * curve_type::template g1_type<>::value_type::one() == commit); - - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); + typedef zk::commitments::kzg kzg_type; -// std::cout << "proof:" << proof; + scalar_value_type alpha = 7u; + std::size_t n = 8; + scalar_value_type z = 2u; + const polynomial f = { + 0x0ed6fb07f52c1f1ef7952250702368474f20fd7af906ba3a5842cdb7946c69b603852bf1069_cppui_modular298, + 0x14db9efba58de09f8ccb1d73fefce45393856e6a7509006561fe67ea354ec69d791b44c1476_cppui_modular298, + 0x0e9fa83a6f8891bc7e6aa1afae85e11dd80cdef32dfcef7cedc12792cf74141c899c8fb1f98_cppui_modular298, + 0x101cc0b43782ca40ae5bf96aabf461e1a623ab9284acac3bb6d55bff4429356dad714ee0bd0_cppui_modular298, + 0x1310586a4d1ed251d1e4c95711fb9346a2b233649f5ce32fe1cf3aea423d131787187a13799_cppui_modular298, + 0x0d9ed064a24e83ac6134de7cca08bdc3e31ffd4db0a004b63039f76821ec2cc53b7e6a74735_cppui_modular298, + 0x2839e48822f55b4e487b817ddf06a6e32e0dcc0c2ced1e738d38fec15bd4717d7680dda90ec_cppui_modular298, + }; - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); -} + auto f_eval = f.evaluate(alpha); + auto params = typename kzg_type::params_type(n, alpha); + auto commit = zk::algorithms::commit(params, f); + nil::marshalling::status_type status; + using endianness = nil::marshalling::option::big_endian; + std::vector single_commitment_bytes = + nil::marshalling::pack(commit, status); + dump_vector(single_commitment_bytes, "commitment"); -BOOST_AUTO_TEST_CASE(kzg_basic_test_mnt4) { + BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); + BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); + BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); + BOOST_CHECK( + alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); + BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); - typedef algebra::curves::mnt4_298 curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + BOOST_CHECK(f_eval * curve_type::template g1_type<>::value_type::one() == commit); - typedef zk::commitments::kzg kzg_type; + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); - scalar_value_type alpha = 10u; - std::size_t n = 16; - scalar_value_type z = 2u; - const polynomial f = {scalar_value_type::modulus - 1u, 1u, 2u, 3u}; +// std::cout << "proof:" << proof; - auto params = typename kzg_type::params_type(n, alpha); - BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); - BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); - BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); - BOOST_CHECK(alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); - BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + } - auto commit = zk::algorithms::commit(params, f); - BOOST_CHECK(3209 * curve_type::template g1_type<>::value_type::one() == commit); - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); + BOOST_AUTO_TEST_CASE(kzg_basic_test_mnt4) { - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); -} + typedef algebra::curves::mnt4_298 curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef zk::commitments::kzg kzg_type; -BOOST_AUTO_TEST_CASE(kzg_random_test) { + scalar_value_type alpha = 10u; + std::size_t n = 16; + scalar_value_type z = 2u; + const polynomial f = {scalar_value_type::modulus - 1u, 1u, 2u, 3u}; - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type scalar_field_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + auto params = typename kzg_type::params_type(n, alpha); + BOOST_CHECK(curve_type::template g1_type<>::value_type::one() == params.commitment_key[0]); + BOOST_CHECK(alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[1]); + BOOST_CHECK(alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[2]); + BOOST_CHECK( + alpha * alpha * alpha * curve_type::template g1_type<>::value_type::one() == params.commitment_key[3]); + BOOST_CHECK(alpha * curve_type::template g2_type<>::value_type::one() == params.verification_key); - typedef zk::commitments::kzg kzg_type; + auto commit = zk::algorithms::commit(params, f); + BOOST_CHECK(3209 * curve_type::template g1_type<>::value_type::one() == commit); - std::size_t n = 298; - scalar_value_type z = algebra::random_element(); - const polynomial f = { - scalar_value_type::modulus - 1u, 1u, 2u, 3u, 5u, scalar_value_type::modulus - 15u}; + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); - auto params = typename kzg_type::params_type(n); - auto commit = zk::algorithms::commit(params, f); + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + } - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); -} + BOOST_AUTO_TEST_CASE(kzg_random_test) { -BOOST_AUTO_TEST_CASE(kzg_false_test) { + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type scalar_field_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef zk::commitments::kzg kzg_type; - typedef zk::commitments::kzg kzg_type; + std::size_t n = 298; + scalar_value_type z = algebra::random_element(); + const polynomial f = { + scalar_value_type::modulus - 1u, 1u, 2u, 3u, 5u, scalar_value_type::modulus - 15u}; - scalar_value_type alpha = 10u; - std::size_t n = 16; - scalar_value_type z = 5u; - const polynomial f = {100u, 1u, 2u, 3u}; + auto params = typename kzg_type::params_type(n); + auto commit = zk::algorithms::commit(params, f); - auto params = typename kzg_type::params_type(n, alpha); + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); - auto commit = zk::algorithms::commit(params, f); + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + } - typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; - auto proof = zk::algorithms::proof_eval(params, f, pk); + BOOST_AUTO_TEST_CASE(kzg_false_test) { - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - // wrong params - auto ck2 = params.commitment_key; - ck2[0] = ck2[0] * 2; - auto params2 = kzg_type::params_type(ck2, params.verification_key * 2u); - BOOST_CHECK(!zk::algorithms::verify_eval(params2, proof, pk)); + typedef zk::commitments::kzg kzg_type; - // wrong commit - auto pk2 = pk; - pk2.commit = pk2.commit * 2u; - BOOST_CHECK(!zk::algorithms::verify_eval(params, proof, pk2)); + scalar_value_type alpha = 10u; + std::size_t n = 16; + scalar_value_type z = 5u; + const polynomial f = {100u, 1u, 2u, 3u}; - // wrong eval - pk2 = pk; - pk2.eval *= 2u; - BOOST_CHECK(!zk::algorithms::verify_eval(params, proof, pk2)); + auto params = typename kzg_type::params_type(n, alpha); - // wrong proof - { - // wrong params - typename kzg_type::proof_type proof2; - bool exception = false; - try {auto proof2 = zk::algorithms::proof_eval(params2, f, pk);} - catch (std::runtime_error& e) {exception = true;} - if (!exception) { - BOOST_CHECK(proof2 != proof); - BOOST_CHECK_MESSAGE(!zk::algorithms::verify_eval(params, proof2, pk), "wrong params"); - } + auto commit = zk::algorithms::commit(params, f); + + typename kzg_type::public_key_type pk = {commit, z, f.evaluate(z)}; + auto proof = zk::algorithms::proof_eval(params, f, pk); + + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk)); - // wrong transcript - exception = false; - try {auto proof2 = zk::algorithms::proof_eval(params, f, pk2);} - catch (std::runtime_error& e) {exception = true;} - if (!exception) { - BOOST_CHECK(proof2 != proof); - BOOST_CHECK_MESSAGE(!zk::algorithms::verify_eval(params, proof2, pk), "wrong transcript"); + // wrong params + auto ck2 = params.commitment_key; + ck2[0] = ck2[0] * 2; + auto params2 = kzg_type::params_type(ck2, params.verification_key * 2u); + BOOST_CHECK(!zk::algorithms::verify_eval(params2, proof, pk)); + + // wrong commit + auto pk2 = pk; + pk2.commit = pk2.commit * 2u; + BOOST_CHECK(!zk::algorithms::verify_eval(params, proof, pk2)); + + // wrong eval + pk2 = pk; + pk2.eval *= 2u; + BOOST_CHECK(!zk::algorithms::verify_eval(params, proof, pk2)); + + // wrong proof + { + // wrong params + typename kzg_type::proof_type proof2; + bool exception = false; + try { auto proof2 = zk::algorithms::proof_eval(params2, f, pk); } + catch (std::runtime_error &e) { exception = true; } + if (!exception) { + BOOST_CHECK(proof2 != proof); + BOOST_CHECK_MESSAGE(!zk::algorithms::verify_eval(params, proof2, pk), "wrong params"); + } + + // wrong transcript + exception = false; + try { auto proof2 = zk::algorithms::proof_eval(params, f, pk2); } + catch (std::runtime_error &e) { exception = true; } + if (!exception) { + BOOST_CHECK(proof2 != proof); + BOOST_CHECK_MESSAGE(!zk::algorithms::verify_eval(params, proof2, pk), "wrong transcript"); + } } + auto proof2 = proof * 2u; + BOOST_CHECK(!zk::algorithms::verify_eval(params, proof2, pk)); } - auto proof2 = proof * 2u; - BOOST_CHECK(!zk::algorithms::verify_eval(params, proof2, pk)); -} BOOST_AUTO_TEST_SUITE_END() @@ -532,87 +536,89 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(batched_kzg_test_suite) -BOOST_AUTO_TEST_CASE(batched_kzg_basic_test) { + BOOST_AUTO_TEST_CASE(batched_kzg_basic_test) { - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef hashes::sha2<256> transcript_hash_type; - const std::size_t batch_size = 1; - typedef zk::commitments::batched_kzg> kzg_type; - typedef typename kzg_type::transcript_type transcript_type; + typedef hashes::sha2<256> transcript_hash_type; + const std::size_t batch_size = 1; + typedef zk::commitments::batched_kzg> kzg_type; + typedef typename kzg_type::transcript_type transcript_type; - typename kzg_type::batch_of_polynomials_type polys = {{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}}; + typename kzg_type::batch_of_polynomials_type polys = {{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}}; - scalar_value_type alpha = 7u; - std::size_t d = 8; - std::size_t t = 8; - auto params = typename kzg_type::params_type(d, t, alpha); + scalar_value_type alpha = 7u; + std::size_t d = 8; + std::size_t t = 8; + auto params = typename kzg_type::params_type(d, t, alpha); - std::vector> eval_points = {{{101u, 2u, 3u},}}; - std::vector merged_eval_points = zk::algorithms::merge_eval_points(eval_points); - auto rs = zk::algorithms::create_evals_polys(polys, eval_points); + std::vector> eval_points = {{{101u, 2u, 3u},}}; + std::vector merged_eval_points = zk::algorithms::merge_eval_points(eval_points); + std::vector rs = zk::algorithms::create_evals_polys(polys, + eval_points); - BOOST_CHECK(rs.size() == batch_size); - for (std::size_t i = 0; i < batch_size; ++i) { - for (auto s : eval_points[i]) { - BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); - } + BOOST_CHECK(rs.size() == batch_size); + for (std::size_t i = 0; i < batch_size; ++i) { + for (const auto &s: eval_points[i]) { + BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); + } - } - auto commits = zk::algorithms::commit(params, polys); - auto pk = typename kzg_type::public_key_type(commits, merged_eval_points, eval_points, rs); + } + auto commits = zk::algorithms::commit(params, polys); + auto pk = typename kzg_type::public_key_type(commits, merged_eval_points, eval_points, rs); - transcript_type transcript; - auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); + transcript_type transcript; + auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); - transcript_type transcript_verification; + transcript_type transcript_verification; - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); -} + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); + } -BOOST_AUTO_TEST_CASE(batched_kzg_bigger_basic_test) { + BOOST_AUTO_TEST_CASE(batched_kzg_bigger_basic_test) { // typedef algebra::curves::bls12<381> curve_type; - typedef algebra::curves::mnt6_298 curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + typedef algebra::curves::mnt6_298 curve_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - typedef hashes::keccak_1600<256> transcript_hash_type; + typedef hashes::keccak_1600<256> transcript_hash_type; // typedef hashes::sha2<256> transcript_hash_type; - typedef zk::commitments::batched_kzg> kzg_type; - typedef typename kzg_type::transcript_type transcript_type; + typedef zk::commitments::batched_kzg> kzg_type; + typedef typename kzg_type::transcript_type transcript_type; - scalar_value_type alpha = 7u; - typename kzg_type::batch_of_polynomials_type polys = {{{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}, - {{11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u}}, - {{21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u}}, - {{31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u}}}}; + scalar_value_type alpha = 7u; + typename kzg_type::batch_of_polynomials_type polys = {{{{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}, + {{11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u}}, + {{21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u}}, + {{31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u}}}}; + + auto params = typename kzg_type::params_type(8, 8, alpha); + + std::vector> S = {{{101u, 2u, 3u}, {102u, 2u, 3u}, {1u, 3u}, {101u, 4u}}}; + std::vector T = zk::algorithms::merge_eval_points(S); + { + std::vector T_check = {1u, 2u, 3u, 4u, 101u, 102u}; + std::sort(T.begin(), T.end()); + BOOST_CHECK(T == T_check); + } + auto rs = zk::algorithms::create_evals_polys(polys, S); + BOOST_CHECK(rs.size() == polys.size()); + for (std::size_t i = 0; i < polys.size(); ++i) { + BOOST_CHECK(rs[i].degree() < polys[i].degree()); + for (auto s: S[i]) { + BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); + } + } + auto commits = zk::algorithms::commit(params, polys); + auto pk = typename kzg_type::public_key_type(commits, T, S, rs); - auto params = typename kzg_type::params_type(8, 8, alpha); + transcript_type transcript; + auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); - std::vector> S = {{{101u, 2u, 3u}, {102u, 2u, 3u}, {1u, 3u}, {101u, 4u}}}; - std::vector T = zk::algorithms::merge_eval_points(S); - { - std::vector T_check = {1u, 2u, 3u, 4u, 101u, 102u}; - std::sort(T.begin(), T.end()); - BOOST_CHECK(T == T_check); - } - auto rs = zk::algorithms::create_evals_polys(polys, S); - BOOST_CHECK(rs.size() == polys.size()); - for (std::size_t i = 0; i < polys.size(); ++i) { - BOOST_CHECK(rs[i].degree() < polys[i].degree()); - for (auto s : S[i]) { - BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); - } + transcript_type transcript_verification; + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); } - auto commits = zk::algorithms::commit(params, polys); - auto pk = typename kzg_type::public_key_type(commits, T, S, rs); - - transcript_type transcript; - auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); - transcript_type transcript_verification; - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); -} /* BOOST_AUTO_TEST_CASE(batched_kzg_bigger_basic_test_mnt6) { typedef algebra::curves::mnt6_298 curve_type; @@ -656,291 +662,320 @@ BOOST_AUTO_TEST_CASE(batched_kzg_bigger_basic_test_mnt6) { } */ -template -typename kzg_type::params_type create_kzg_params(std::size_t degree_log) { - // TODO: what cases t != d? - typename kzg_type::field_type::value_type alpha (7u); - std::size_t d = 1 << degree_log; + template + typename kzg_type::params_type create_kzg_params(std::size_t degree_log) { + // TODO: what cases t != d? + typename kzg_type::field_type::value_type alpha(7u); + std::size_t d = 1 << degree_log; - typename kzg_type::params_type params(d, d, alpha); - return params; -} + typename kzg_type::params_type params(d, d, alpha); + return params; + } -BOOST_AUTO_TEST_CASE(batched_kzg_placeholder_repr) { - typedef algebra::curves::mnt6_298 curve_type; + BOOST_AUTO_TEST_CASE(batched_kzg_placeholder_repr) { + typedef algebra::curves::mnt6_298 curve_type; // typedef algebra::curves::bls12_381 curve_type; - typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - - typedef hashes::keccak_1600<256> transcript_hash_type; - typedef zk::commitments::batched_kzg> kzg_type; - typedef typename kzg_type::transcript_type transcript_type; + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; - scalar_value_type alpha = 7u; - std::vector> polys_dfs = {{ - //~-~-~-~ commiting to batch: 0~-~-~-~ - {8, { - 0x1u, - 0x29ab55a4b34e699f13959ce2c174be01985b7a0c88268d41489977b2219cd8a8a4e33032230_cppui_modular298, - 0x00f73779fe09916dfdcc2fd1f968d534beb17daf7518cd9fae5c1f7bdcf94dd5d7def6980c4_cppui_modular298, - 0x0078fe16f00d3d46d50e74ed550e57c9dda4ca5bc69da7a1820913abb7f1f371dd044f1a9c9_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a71660000_cppui_modular298, - 0x1224262893ebbcc33644de228777f0eafdda5726867d8d5ced4b9a4ebf8fb824c0c3e62ddd1_cppui_modular298, - 0x3ad84453493094f44c0e4b334f83d9b7d7845383998b4cfe8788f285043342f78dc81fc7f3d_cppui_modular298, - 0x3b567db6572ce91b74cc0617f3de5722b89106d7480672fcb3dbfe55293a9d5b88a2c745638_cppui_modular298, - }}, - - {8, { - 0x11u, - 0x32765e1dd8b55d57208c21d4b69519f0a9c31da369823c8981592cca8e802a5f94e83d34525_cppui_modular298, - 0x106aaf19dea2a84dda8f2cf18ff62880a9c958a6c6a5a79a941e1739ac8e2b3355ce6018d04_cppui_modular298, - 0x0808df85f0e111b425f5c3c2a5f3d467b7f17018307821b9a29a4e6737112a8fad4940c4659_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fff0_cppui_modular298, - 0x09591daf6e84c90b294e5930925794fbec72b38fa521de14b48be53652ac666dd0bed92badc_cppui_modular298, - 0x2b64ccb368977e146f4b4e13b8f6866bec6c788c47fe7303a1c6fac7349e659a0fd8b6472fd_cppui_modular298, - 0x33c69c47565914ae23e4b742a2f8da84de44611ade2bf8e4934ac399aa1b663db85dd59b9a8_cppui_modular298, - }}, - {8, { - 0x121u, - 0x14837ac17edd19691f5b84d622f5280b0f03870f34ac907aa464fd672612e51d5448d739767_cppui_modular298, - 0x27d7b182abe493a25c180ff56ba5f4d8ed879e46f66fb6cafe6b42d0f0be9b331c180825d40_cppui_modular298, - 0x10f7e04a707de031f19d09e27357bd0a0a9ccf351ab20817607510d8e5cab1efb68f204abe7_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fee0_cppui_modular298, - 0x274c010bc85d0cf92a7ef62f25f786e187324a23d9f78a2391801499bb19abb0115e3f2689a_cppui_modular298, - 0x13f7ca4a9b5592bfedc26b0fdd46ba13a8ae32ec183463d33779cf2ff06df59a498f0e3a2c1_cppui_modular298, - 0x2ad79b82d6bc4630583d7122d594f1e28b9901fdf3f21286d5700127fb61deddaf17f61541a_cppui_modular298, - }}, - - {8, { - 0x1331u, - 0x31adbbd7088bf00fa3cf6b1de5a83e1d102ee2033641130ddd3b79d5216262ef9c92daf0dd2_cppui_modular298, - 0x136877db5aae278ef135c61203d9be3d51b18584bc5dfeae9447a9d64fbe15917f6a9463135_cppui_modular298, - 0x3137f5bc5b7349c7e403bbf48520d1f85b927dba8b421f149031d663bdc38db588e4cb76a53_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165ecd0_cppui_modular298, - 0x0a21bff63eae3652a60b0fe7634470cf8606ef2fd863079058a9982bbfca2dddc9143b6f22f_cppui_modular298, - 0x286703f1ec8bfed358a4b4f34512f0af44844bae52461befa19d682a916e7b3be63c81fcecc_cppui_modular298, - 0x0a978610ebc6dc9a65d6bf10c3cbdcf43aa353788361fb89a5b33b9d23690317dcc24ae95ae_cppui_modular298, - }}, - - {8, { - 0x1u, - 0x29ab55a4b34e699f13959ce2c174be01985b7a0c88268d41489977b2219cd8a8a4e33032230_cppui_modular298, - 0x00f73779fe09916dfdcc2fd1f968d534beb17daf7518cd9fae5c1f7bdcf94dd5d7def6980c4_cppui_modular298, - 0x0078fe16f00d3d46d50e74ed550e57c9dda4ca5bc69da7a1820913abb7f1f371dd044f1a9c9_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a71660000_cppui_modular298, - 0x1224262893ebbcc33644de228777f0eafdda5726867d8d5ced4b9a4ebf8fb824c0c3e62ddd1_cppui_modular298, - 0x3ad84453493094f44c0e4b334f83d9b7d7845383998b4cfe8788f285043342f78dc81fc7f3d_cppui_modular298, - 0x3b567db6572ce91b74cc0617f3de5722b89106d7480672fcb3dbfe55293a9d5b88a2c745638_cppui_modular298, - }}, - - {8, { - 0x11u, - 0x32765e1dd8b55d57208c21d4b69519f0a9c31da369823c8981592cca8e802a5f94e83d34525_cppui_modular298, - 0x106aaf19dea2a84dda8f2cf18ff62880a9c958a6c6a5a79a941e1739ac8e2b3355ce6018d04_cppui_modular298, - 0x0808df85f0e111b425f5c3c2a5f3d467b7f17018307821b9a29a4e6737112a8fad4940c4659_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fff0_cppui_modular298, - 0x09591daf6e84c90b294e5930925794fbec72b38fa521de14b48be53652ac666dd0bed92badc_cppui_modular298, - 0x2b64ccb368977e146f4b4e13b8f6866bec6c788c47fe7303a1c6fac7349e659a0fd8b6472fd_cppui_modular298, - 0x33c69c47565914ae23e4b742a2f8da84de44611ade2bf8e4934ac399aa1b663db85dd59b9a8_cppui_modular298, - }}, - - {8, { - 0x121u, - 0x14837ac17edd19691f5b84d622f5280b0f03870f34ac907aa464fd672612e51d5448d739767_cppui_modular298, - 0x27d7b182abe493a25c180ff56ba5f4d8ed879e46f66fb6cafe6b42d0f0be9b331c180825d40_cppui_modular298, - 0x10f7e04a707de031f19d09e27357bd0a0a9ccf351ab20817607510d8e5cab1efb68f204abe7_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fee0_cppui_modular298, - 0x274c010bc85d0cf92a7ef62f25f786e187324a23d9f78a2391801499bb19abb0115e3f2689a_cppui_modular298, - 0x13f7ca4a9b5592bfedc26b0fdd46ba13a8ae32ec183463d33779cf2ff06df59a498f0e3a2c1_cppui_modular298, - 0x2ad79b82d6bc4630583d7122d594f1e28b9901fdf3f21286d5700127fb61deddaf17f61541a_cppui_modular298, - }}, - - {8, { - 0x1331u, - 0x31adbbd7088bf00fa3cf6b1de5a83e1d102ee2033641130ddd3b79d5216262ef9c92daf0dd2_cppui_modular298, - 0x136877db5aae278ef135c61203d9be3d51b18584bc5dfeae9447a9d64fbe15917f6a9463135_cppui_modular298, - 0x3137f5bc5b7349c7e403bbf48520d1f85b927dba8b421f149031d663bdc38db588e4cb76a53_cppui_modular298, - 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165ecd0_cppui_modular298, - 0x0a21bff63eae3652a60b0fe7634470cf8606ef2fd863079058a9982bbfca2dddc9143b6f22f_cppui_modular298, - 0x286703f1ec8bfed358a4b4f34512f0af44844bae52461befa19d682a916e7b3be63c81fcecc_cppui_modular298, - 0x0a978610ebc6dc9a65d6bf10c3cbdcf43aa353788361fb89a5b33b9d23690317dcc24ae95ae_cppui_modular298, - }}, - - {8, { 0x0u, 0x0u, 0x0u, 0x0u, 0x0u, 0x1u, 0x0u, 0x0u, }}, - - {8, { 0x0u, 0x0u, 0x0u, 0x0u, 0x0u, 0x0u, 0x1u, 0x1u, }}, - - {8, { - 0x0u, - 0x1u, - 0x1u, - 0x0u, - 0x0u, - 0x1f8915cc2533543f2bc6164e6238fc23a81c0f463c4646f1d40c1d7dfd0ae08ab78492cbef1_cppui_modular298, - 0x39bef1b52e65b396fbac77780f097c34e4287e259355a4ea31e0dcfacd0677a359e136b2fdd_cppui_modular298, - 0x173564dab75ba19b463030c03996325d30e7829fc226518b459919e6d64278946b02141888b_cppui_modular298, - }}, - - {8, { - 0x0u, - 0x0u, - 0x0u, - 0x1u, - 0x1u, - 0x0722a67f49f9ecfe9f0874df295dcd87a484fabc9ed6fa56696cb563b4ded702bbe2984c787_cppui_modular298, - 0x3b1bf86dcd7b7526048b0705c8287a3b97ca771ba445718a3614352160278d229349a1b7d08_cppui_modular298, - 0x1e127023ee88eeab382e9d07a328168599c3a9e3c0fe99eadb31575515db872426d7356b1bb_cppui_modular298, - }}, - - //~-~-~-~ commiting to batch: 1~-~-~-~ - {8, { - 0x39ef702ef59ff1816e4f51f2ae7fe2d78108c006d5f3039cd1a474ba8c48c16a62518f86863_cppui_modular298, - 0x17dadc1965bae6d9426ef1a2e6d3640ac4cd96089c55c7dc3800924668fcc450cbaa7de9f4c_cppui_modular298, - 0x1202bd2e4122c826d8ba7cd66346c0df0326468fd6e7989c8eebe3dedfcbd9b0ecdc1fb41c2_cppui_modular298, - 0x3b718dda0c9262c55640bd1e364df577ec246e46cb05109733008263282cc1a8959b4bf6fa7_cppui_modular298, - 0x27b08d175547d973e48f341c081c3851eee512d6e73200bfa47b1e049e1d268409ad2ce21c9_cppui_modular298, - 0x1872fd6e208095436bfcb92388e0d1c8509c3f8e89235d0430c61add0ab203ac30370518ce6_cppui_modular298, - 0x304c1332568ebbe7347b598eef6cb41f198a574c4ff7cd151337211efea753ec6fc7d61330b_cppui_modular298, - 0x1b41e76a1c5a4daa01029a0ec27b5f0b06ca7b480b600b8b573ae00feaab4ad9f1146a99459_cppui_modular298, - }}, - - {8, { - 0x11cccdf2e5ccc50aa597c4194181c1fe652f508e4aafb2a0137f878c4b3b9d09511285954a1_cppui_modular298, - 0x1e2f5a14babe0e0d4adcace1969a3c78807ea6da4ae1cca797a6bf88c3101397d8d2452a9dc_cppui_modular298, - 0x360a362e2078f4e68d4b9e847d6da083454c3ce2e7379483cfa751cf2c0cd7e8a47cc314928_cppui_modular298, - 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, - 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, - 0x011394bbd52cee496c395d41b68e0732c88572384d492e195f8f5b1c7a1c61f6ed67f94c950_cppui_modular298, - 0x194e4123c5669a48341b2f6b127f0a8b109818666a3d2229f23414de9c5d23d2d63c05309be_cppui_modular298, - 0x30641ec0f843aeb8202263821cac300d11b237ce42e2876763c8c16513494b993aaf5941f61_cppui_modular298, - }}, - - {8, { - 0x1e2f5a14babe0e0d4adcace1969a3c78807ea6da4ae1cca797a6bf88c3101397d8d2452a9dc_cppui_modular298, - 0x360a362e2078f4e68d4b9e847d6da083454c3ce2e7379483cfa751cf2c0cd7e8a47cc314928_cppui_modular298, - 0x0c3d778f1a6196ab1c2ba05597c7b275b23cb23faf7b128228ae23ad2aac20cc2bb1cc68ae9_cppui_modular298, - 0x1d871330c3db0fc34493247dc5f22570c08e3c4d3019e89ccadb340ddf48317d9dda6bf5cd9_cppui_modular298, - 0x114ac4e3bcbc6bf412878efb87080a493920fdbdb54535e797af6c6f15cacfa5a93c46626f0_cppui_modular298, - 0x0cfede4389503774cda3e57a7034cc1c54ad074f86f551b54a44118a30afd0fc06ad7393ee6_cppui_modular298, - 0x3b079297527c765d71f9db51a85f47c081d4047080ad9352f6a325410e1e8490ddc59988939_cppui_modular298, - 0x299eacd3439bb98b27f8cbaafb3983162a895d3de16cb29360ad4b12f5f114dee4f5a065b97_cppui_modular298, - }}, - - {8, { - 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, - 0x0u, 0x1u, 0x0u, 0x0u, 0x0u, 0x0u, 0x0u, - }}, - - //~-~-~-~ commiting to batch: 2~-~-~-~ - {8, { 0x1u, 0x1u, 0x1u, 0x1u, 0x1u, 0x1u, 0x1u, 0x1u, }}, - - //~-~-~-~ commiting to batch: 3~-~-~-~ - {8, { - 0x2783a8a7c5cf7e94e4d1fdc4aa6eb807ea4eddbf81ea87939f040dc851e9212b9dca604ac9a_cppui_modular298, - 0x13230785fb96c79b65251354a51866632384c4dc7ceff4e48dc2fac8f09db1ce7367e20608b_cppui_modular298, - 0x2ccbbf5a905e4515c62fede907c2625d90bfda58027217f7e58155b67d5851fb4cf46f04364_cppui_modular298, - 0x17adaf6b5019e118bc7ac6213b0dc84cf1a9cada9cc620471384b7a191db27251337ec3d3b7_cppui_modular298, - 0x05b19c26a34901d91528679eeac2c7f311aa3f5f0fa669855b10522373949671df3f1e23c38_cppui_modular298, - 0x37421ad4e9cf2ccadc50246390593aa253e4ca3ba5767e931130a2f905a49443e0e02fc0ce8_cppui_modular298, - 0x2a2814a40ce271f86b0369793c4c79d31686212ad02a382f6288ef94cabe1e2cff80ce74bd5_cppui_modular298, - 0x383fcb086d115688ba77b1449bd46480f3bd7cbb070242833338005e60dcaa9ba238c801961_cppui_modular298, - }}, - - {8, { - 0x0710f09328ac0442d2d93a61f4eda9b265a27ea0570484e3a1cf1aaa249974ea1a99377a11c_cppui_modular298, - 0x2bb0eec490c8ac0bbe164c6ee7072a8989e33a7006d8f222b1476b15c2ef0386b49b7d6bc28_cppui_modular298, - 0x3552ef5f48bc3702e4e9f8fc7b236de25d1a78e256d8417ff106bbc75b7cbfc36c8977b2896_cppui_modular298, - 0x3871e84395a7af9c0fdd19321af6b742815a982bb5f59bcf7be6793caa98f4a919032d2969d_cppui_modular298, - 0x153bd600c1074537112d1df7afd22932c713cc84c08d3c197cbdd9d84b675ab9c62e78d36a0_cppui_modular298, - 0x12d86d35994854ef3606ae63e5114209bec8dbb0d3ebb1bb9a786fd27ced58870d3779d3d7a_cppui_modular298, - 0x2e0895904268862017c64e0a495813bf84b1d2137a53102097557bd90c2aac21c0802fc1787_cppui_modular298, - 0x0742ee092a59ae6b7169ac51e7339c52adc1dc74471e0d207a3d29dd37d60ea9bc9438e5c15_cppui_modular298, - }}, - math::polynomial_dfs::zero(), - math::polynomial_dfs::zero(), - math::polynomial_dfs::zero(), - math::polynomial_dfs::zero(), - }}; - - std::vector> polys; - for(auto const& p_dfs: polys_dfs) { - auto p = math::polynomial(p_dfs.coefficients()); - polys.push_back(p); - } + typedef hashes::keccak_1600<256> transcript_hash_type; + typedef zk::commitments::batched_kzg> kzg_type; + typedef typename kzg_type::transcript_type transcript_type; - // auto params = typename kzg_type::params_type(8, 8, alpha); - auto params = create_kzg_params(3 /*degree_log*/); - auto commits = zk::algorithms::commit(params, polys); - using endianness = nil::marshalling::option::big_endian; - for(auto &c: commits) { - nil::marshalling::status_type status; - std::vector single_commitment_bytes = - nil::marshalling::pack(c, status); - dump_vector(single_commitment_bytes, "commitment"); - } + scalar_value_type alpha = 7u; + std::vector> polys_dfs = {{ + //~-~-~-~ commiting to batch: 0~-~-~-~ + {8, { + 0x1u, + 0x29ab55a4b34e699f13959ce2c174be01985b7a0c88268d41489977b2219cd8a8a4e33032230_cppui_modular298, + 0x00f73779fe09916dfdcc2fd1f968d534beb17daf7518cd9fae5c1f7bdcf94dd5d7def6980c4_cppui_modular298, + 0x0078fe16f00d3d46d50e74ed550e57c9dda4ca5bc69da7a1820913abb7f1f371dd044f1a9c9_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a71660000_cppui_modular298, + 0x1224262893ebbcc33644de228777f0eafdda5726867d8d5ced4b9a4ebf8fb824c0c3e62ddd1_cppui_modular298, + 0x3ad84453493094f44c0e4b334f83d9b7d7845383998b4cfe8788f285043342f78dc81fc7f3d_cppui_modular298, + 0x3b567db6572ce91b74cc0617f3de5722b89106d7480672fcb3dbfe55293a9d5b88a2c745638_cppui_modular298, + }}, + + {8, { + 0x11u, + 0x32765e1dd8b55d57208c21d4b69519f0a9c31da369823c8981592cca8e802a5f94e83d34525_cppui_modular298, + 0x106aaf19dea2a84dda8f2cf18ff62880a9c958a6c6a5a79a941e1739ac8e2b3355ce6018d04_cppui_modular298, + 0x0808df85f0e111b425f5c3c2a5f3d467b7f17018307821b9a29a4e6737112a8fad4940c4659_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fff0_cppui_modular298, + 0x09591daf6e84c90b294e5930925794fbec72b38fa521de14b48be53652ac666dd0bed92badc_cppui_modular298, + 0x2b64ccb368977e146f4b4e13b8f6866bec6c788c47fe7303a1c6fac7349e659a0fd8b6472fd_cppui_modular298, + 0x33c69c47565914ae23e4b742a2f8da84de44611ade2bf8e4934ac399aa1b663db85dd59b9a8_cppui_modular298, + }}, + {8, { + 0x121u, + 0x14837ac17edd19691f5b84d622f5280b0f03870f34ac907aa464fd672612e51d5448d739767_cppui_modular298, + 0x27d7b182abe493a25c180ff56ba5f4d8ed879e46f66fb6cafe6b42d0f0be9b331c180825d40_cppui_modular298, + 0x10f7e04a707de031f19d09e27357bd0a0a9ccf351ab20817607510d8e5cab1efb68f204abe7_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fee0_cppui_modular298, + 0x274c010bc85d0cf92a7ef62f25f786e187324a23d9f78a2391801499bb19abb0115e3f2689a_cppui_modular298, + 0x13f7ca4a9b5592bfedc26b0fdd46ba13a8ae32ec183463d33779cf2ff06df59a498f0e3a2c1_cppui_modular298, + 0x2ad79b82d6bc4630583d7122d594f1e28b9901fdf3f21286d5700127fb61deddaf17f61541a_cppui_modular298, + }}, + + {8, { + 0x1331u, + 0x31adbbd7088bf00fa3cf6b1de5a83e1d102ee2033641130ddd3b79d5216262ef9c92daf0dd2_cppui_modular298, + 0x136877db5aae278ef135c61203d9be3d51b18584bc5dfeae9447a9d64fbe15917f6a9463135_cppui_modular298, + 0x3137f5bc5b7349c7e403bbf48520d1f85b927dba8b421f149031d663bdc38db588e4cb76a53_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165ecd0_cppui_modular298, + 0x0a21bff63eae3652a60b0fe7634470cf8606ef2fd863079058a9982bbfca2dddc9143b6f22f_cppui_modular298, + 0x286703f1ec8bfed358a4b4f34512f0af44844bae52461befa19d682a916e7b3be63c81fcecc_cppui_modular298, + 0x0a978610ebc6dc9a65d6bf10c3cbdcf43aa353788361fb89a5b33b9d23690317dcc24ae95ae_cppui_modular298, + }}, + + {8, { + 0x1u, + 0x29ab55a4b34e699f13959ce2c174be01985b7a0c88268d41489977b2219cd8a8a4e33032230_cppui_modular298, + 0x00f73779fe09916dfdcc2fd1f968d534beb17daf7518cd9fae5c1f7bdcf94dd5d7def6980c4_cppui_modular298, + 0x0078fe16f00d3d46d50e74ed550e57c9dda4ca5bc69da7a1820913abb7f1f371dd044f1a9c9_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a71660000_cppui_modular298, + 0x1224262893ebbcc33644de228777f0eafdda5726867d8d5ced4b9a4ebf8fb824c0c3e62ddd1_cppui_modular298, + 0x3ad84453493094f44c0e4b334f83d9b7d7845383998b4cfe8788f285043342f78dc81fc7f3d_cppui_modular298, + 0x3b567db6572ce91b74cc0617f3de5722b89106d7480672fcb3dbfe55293a9d5b88a2c745638_cppui_modular298, + }}, + + {8, { + 0x11u, + 0x32765e1dd8b55d57208c21d4b69519f0a9c31da369823c8981592cca8e802a5f94e83d34525_cppui_modular298, + 0x106aaf19dea2a84dda8f2cf18ff62880a9c958a6c6a5a79a941e1739ac8e2b3355ce6018d04_cppui_modular298, + 0x0808df85f0e111b425f5c3c2a5f3d467b7f17018307821b9a29a4e6737112a8fad4940c4659_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fff0_cppui_modular298, + 0x09591daf6e84c90b294e5930925794fbec72b38fa521de14b48be53652ac666dd0bed92badc_cppui_modular298, + 0x2b64ccb368977e146f4b4e13b8f6866bec6c788c47fe7303a1c6fac7349e659a0fd8b6472fd_cppui_modular298, + 0x33c69c47565914ae23e4b742a2f8da84de44611ade2bf8e4934ac399aa1b663db85dd59b9a8_cppui_modular298, + }}, + + {8, { + 0x121u, + 0x14837ac17edd19691f5b84d622f5280b0f03870f34ac907aa464fd672612e51d5448d739767_cppui_modular298, + 0x27d7b182abe493a25c180ff56ba5f4d8ed879e46f66fb6cafe6b42d0f0be9b331c180825d40_cppui_modular298, + 0x10f7e04a707de031f19d09e27357bd0a0a9ccf351ab20817607510d8e5cab1efb68f204abe7_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165fee0_cppui_modular298, + 0x274c010bc85d0cf92a7ef62f25f786e187324a23d9f78a2391801499bb19abb0115e3f2689a_cppui_modular298, + 0x13f7ca4a9b5592bfedc26b0fdd46ba13a8ae32ec183463d33779cf2ff06df59a498f0e3a2c1_cppui_modular298, + 0x2ad79b82d6bc4630583d7122d594f1e28b9901fdf3f21286d5700127fb61deddaf17f61541a_cppui_modular298, + }}, + + {8, { + 0x1331u, + 0x31adbbd7088bf00fa3cf6b1de5a83e1d102ee2033641130ddd3b79d5216262ef9c92daf0dd2_cppui_modular298, + 0x136877db5aae278ef135c61203d9be3d51b18584bc5dfeae9447a9d64fbe15917f6a9463135_cppui_modular298, + 0x3137f5bc5b7349c7e403bbf48520d1f85b927dba8b421f149031d663bdc38db588e4cb76a53_cppui_modular298, + 0x3bcf7bcd473a266249da7b0548ecaeec9635d1330ea41a9e35e51200e12c90cd65a7165ecd0_cppui_modular298, + 0x0a21bff63eae3652a60b0fe7634470cf8606ef2fd863079058a9982bbfca2dddc9143b6f22f_cppui_modular298, + 0x286703f1ec8bfed358a4b4f34512f0af44844bae52461befa19d682a916e7b3be63c81fcecc_cppui_modular298, + 0x0a978610ebc6dc9a65d6bf10c3cbdcf43aa353788361fb89a5b33b9d23690317dcc24ae95ae_cppui_modular298, + }}, + + {8, + {0x0u, 0x0u, 0x0u, 0x0u, 0x0u, 0x1u, + 0x0u, 0x0u,}}, + + {8, + {0x0u, 0x0u, 0x0u, 0x0u, 0x0u, 0x0u, + 0x1u, 0x1u,}}, + + {8, { + 0x0u, + 0x1u, + 0x1u, + 0x0u, + 0x0u, + 0x1f8915cc2533543f2bc6164e6238fc23a81c0f463c4646f1d40c1d7dfd0ae08ab78492cbef1_cppui_modular298, + 0x39bef1b52e65b396fbac77780f097c34e4287e259355a4ea31e0dcfacd0677a359e136b2fdd_cppui_modular298, + 0x173564dab75ba19b463030c03996325d30e7829fc226518b459919e6d64278946b02141888b_cppui_modular298, + }}, + + {8, { + 0x0u, + 0x0u, + 0x0u, + 0x1u, + 0x1u, + 0x0722a67f49f9ecfe9f0874df295dcd87a484fabc9ed6fa56696cb563b4ded702bbe2984c787_cppui_modular298, + 0x3b1bf86dcd7b7526048b0705c8287a3b97ca771ba445718a3614352160278d229349a1b7d08_cppui_modular298, + 0x1e127023ee88eeab382e9d07a328168599c3a9e3c0fe99eadb31575515db872426d7356b1bb_cppui_modular298, + }}, + + //~-~-~-~ commiting to batch: 1~-~-~-~ + {8, { + 0x39ef702ef59ff1816e4f51f2ae7fe2d78108c006d5f3039cd1a474ba8c48c16a62518f86863_cppui_modular298, + 0x17dadc1965bae6d9426ef1a2e6d3640ac4cd96089c55c7dc3800924668fcc450cbaa7de9f4c_cppui_modular298, + 0x1202bd2e4122c826d8ba7cd66346c0df0326468fd6e7989c8eebe3dedfcbd9b0ecdc1fb41c2_cppui_modular298, + 0x3b718dda0c9262c55640bd1e364df577ec246e46cb05109733008263282cc1a8959b4bf6fa7_cppui_modular298, + 0x27b08d175547d973e48f341c081c3851eee512d6e73200bfa47b1e049e1d268409ad2ce21c9_cppui_modular298, + 0x1872fd6e208095436bfcb92388e0d1c8509c3f8e89235d0430c61add0ab203ac30370518ce6_cppui_modular298, + 0x304c1332568ebbe7347b598eef6cb41f198a574c4ff7cd151337211efea753ec6fc7d61330b_cppui_modular298, + 0x1b41e76a1c5a4daa01029a0ec27b5f0b06ca7b480b600b8b573ae00feaab4ad9f1146a99459_cppui_modular298, + }}, + + {8, { + 0x11cccdf2e5ccc50aa597c4194181c1fe652f508e4aafb2a0137f878c4b3b9d09511285954a1_cppui_modular298, + 0x1e2f5a14babe0e0d4adcace1969a3c78807ea6da4ae1cca797a6bf88c3101397d8d2452a9dc_cppui_modular298, + 0x360a362e2078f4e68d4b9e847d6da083454c3ce2e7379483cfa751cf2c0cd7e8a47cc314928_cppui_modular298, + 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, + 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, + 0x011394bbd52cee496c395d41b68e0732c88572384d492e195f8f5b1c7a1c61f6ed67f94c950_cppui_modular298, + 0x194e4123c5669a48341b2f6b127f0a8b109818666a3d2229f23414de9c5d23d2d63c05309be_cppui_modular298, + 0x30641ec0f843aeb8202263821cac300d11b237ce42e2876763c8c16513494b993aaf5941f61_cppui_modular298, + }}, + + {8, { + 0x1e2f5a14babe0e0d4adcace1969a3c78807ea6da4ae1cca797a6bf88c3101397d8d2452a9dc_cppui_modular298, + 0x360a362e2078f4e68d4b9e847d6da083454c3ce2e7379483cfa751cf2c0cd7e8a47cc314928_cppui_modular298, + 0x0c3d778f1a6196ab1c2ba05597c7b275b23cb23faf7b128228ae23ad2aac20cc2bb1cc68ae9_cppui_modular298, + 0x1d871330c3db0fc34493247dc5f22570c08e3c4d3019e89ccadb340ddf48317d9dda6bf5cd9_cppui_modular298, + 0x114ac4e3bcbc6bf412878efb87080a493920fdbdb54535e797af6c6f15cacfa5a93c46626f0_cppui_modular298, + 0x0cfede4389503774cda3e57a7034cc1c54ad074f86f551b54a44118a30afd0fc06ad7393ee6_cppui_modular298, + 0x3b079297527c765d71f9db51a85f47c081d4047080ad9352f6a325410e1e8490ddc59988939_cppui_modular298, + 0x299eacd3439bb98b27f8cbaafb3983162a895d3de16cb29360ad4b12f5f114dee4f5a065b97_cppui_modular298, + }}, + + {8, { + 0x126a1e24bba3895afe1e9d30005f807b7df2082352cd5c31f79e7e1faee22ae9ef6d091bb5c_cppui_modular298, + 0x0u, 0x1u, 0x0u, 0x0u, 0x0u, + 0x0u, 0x0u, + }}, + + //~-~-~-~ commiting to batch: 2~-~-~-~ + {8, + {0x1u, 0x1u, 0x1u, 0x1u, 0x1u, 0x1u, + 0x1u, 0x1u,}}, + + //~-~-~-~ commiting to batch: 3~-~-~-~ + {8, { + 0x2783a8a7c5cf7e94e4d1fdc4aa6eb807ea4eddbf81ea87939f040dc851e9212b9dca604ac9a_cppui_modular298, + 0x13230785fb96c79b65251354a51866632384c4dc7ceff4e48dc2fac8f09db1ce7367e20608b_cppui_modular298, + 0x2ccbbf5a905e4515c62fede907c2625d90bfda58027217f7e58155b67d5851fb4cf46f04364_cppui_modular298, + 0x17adaf6b5019e118bc7ac6213b0dc84cf1a9cada9cc620471384b7a191db27251337ec3d3b7_cppui_modular298, + 0x05b19c26a34901d91528679eeac2c7f311aa3f5f0fa669855b10522373949671df3f1e23c38_cppui_modular298, + 0x37421ad4e9cf2ccadc50246390593aa253e4ca3ba5767e931130a2f905a49443e0e02fc0ce8_cppui_modular298, + 0x2a2814a40ce271f86b0369793c4c79d31686212ad02a382f6288ef94cabe1e2cff80ce74bd5_cppui_modular298, + 0x383fcb086d115688ba77b1449bd46480f3bd7cbb070242833338005e60dcaa9ba238c801961_cppui_modular298, + }}, + + {8, { + 0x0710f09328ac0442d2d93a61f4eda9b265a27ea0570484e3a1cf1aaa249974ea1a99377a11c_cppui_modular298, + 0x2bb0eec490c8ac0bbe164c6ee7072a8989e33a7006d8f222b1476b15c2ef0386b49b7d6bc28_cppui_modular298, + 0x3552ef5f48bc3702e4e9f8fc7b236de25d1a78e256d8417ff106bbc75b7cbfc36c8977b2896_cppui_modular298, + 0x3871e84395a7af9c0fdd19321af6b742815a982bb5f59bcf7be6793caa98f4a919032d2969d_cppui_modular298, + 0x153bd600c1074537112d1df7afd22932c713cc84c08d3c197cbdd9d84b675ab9c62e78d36a0_cppui_modular298, + 0x12d86d35994854ef3606ae63e5114209bec8dbb0d3ebb1bb9a786fd27ced58870d3779d3d7a_cppui_modular298, + 0x2e0895904268862017c64e0a495813bf84b1d2137a53102097557bd90c2aac21c0802fc1787_cppui_modular298, + 0x0742ee092a59ae6b7169ac51e7339c52adc1dc74471e0d207a3d29dd37d60ea9bc9438e5c15_cppui_modular298, + }}, + math::polynomial_dfs::zero(), + math::polynomial_dfs::zero(), + math::polynomial_dfs::zero(), + math::polynomial_dfs::zero(), + }}; + + std::vector> polys; + for (auto const &p_dfs: polys_dfs) { + auto p = math::polynomial(p_dfs.coefficients()); + polys.push_back(p); + } - std::vector> S = { - /* points_k_i:0,0: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,1: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,2: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,3: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,4: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,5: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,6: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,7: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,8: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, - 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298, }, - /* points_k_i:0,9: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, - 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298, }, - /* points_k_i:0,10:*/ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:0,11:*/ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:1,0: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:1,1: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:1,2: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:1,3: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:2,0: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, - 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298, }, - /* points_k_i:3,0: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:3,1: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:3,2: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:3,3: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:3,4: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - /* points_k_i:3,5: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, }, - }; + // auto params = typename kzg_type::params_type(8, 8, alpha); + auto params = create_kzg_params(3 /*degree_log*/); + auto commits = zk::algorithms::commit(params, polys); + using endianness = nil::marshalling::option::big_endian; + for (auto &c: commits) { + nil::marshalling::status_type status; + std::vector single_commitment_bytes = + nil::marshalling::pack(c, status); + dump_vector(single_commitment_bytes, "commitment"); + } - std::vector T = zk::algorithms::merge_eval_points(S); - { - std::vector T_check = { - 0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, - 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298, + std::vector> S = { + /* points_k_i:0,0: */ {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,1: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,2: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,3: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,4: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,5: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,6: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,7: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,8: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, + 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298,}, + /* points_k_i:0,9: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, + 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298,}, + /* points_k_i:0,10:*/ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:0,11:*/ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:1,0: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:1,1: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:1,2: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:1,3: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:2,0: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, + 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298,}, + /* points_k_i:3,0: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:3,1: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:3,2: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:3,3: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:3,4: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, + /* points_k_i:3,5: */ + {0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298,}, }; - std::sort(T.begin(), T.end()); - BOOST_CHECK(T == T_check); - } - auto rs = zk::algorithms::create_evals_polys(polys, S); - BOOST_CHECK(rs.size() == polys.size()); - for (std::size_t i = 0; i < polys.size(); ++i) { - BOOST_CHECK(polys[i].degree() == 0 || rs[i].degree() < polys[i].degree()); - for (auto s : S[i]) { - BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); + + std::vector T = zk::algorithms::merge_eval_points(S); + { + std::vector T_check = { + 0x3a3eeb9eda157d043c7a56f0bb263b4d1bc21dc74cfb1b5e9a80f65a461c3916_cppui_modular298, + 0x3afff0e9becdc8be161a77a403b466aa7d696ebe365418763ba1157a5aa27fd000e04d44b99_cppui_modular298, + }; + std::sort(T.begin(), T.end()); + BOOST_CHECK(T == T_check); } - } - auto pk = typename kzg_type::public_key_type(commits, T, S, rs); + auto rs = zk::algorithms::create_evals_polys(polys, S); + BOOST_CHECK(rs.size() == polys.size()); + for (std::size_t i = 0; i < polys.size(); ++i) { + BOOST_CHECK(polys[i].degree() == 0 || rs[i].degree() < polys[i].degree()); + for (auto s: S[i]) { + BOOST_CHECK(polys[i].evaluate(s) == rs[i].evaluate(s)); + } + } + auto pk = typename kzg_type::public_key_type(commits, T, S, rs); - transcript_type transcript; - auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); + transcript_type transcript; + auto proof = zk::algorithms::proof_eval(params, polys, pk, transcript); - transcript_type transcript_verification; - BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); -} + transcript_type transcript_verification; + BOOST_CHECK(zk::algorithms::verify_eval(params, proof, pk, transcript_verification)); + } BOOST_AUTO_TEST_SUITE_END() template< - typename curve_type, - typename transcript_hash_type - > + typename curve_type, + typename transcript_hash_type +> struct placeholder_class_test_initializer { bool run_test() { typedef typename curve_type::scalar_field_type::value_type scalar_value_type; @@ -955,7 +990,7 @@ struct placeholder_class_test_initializer { typename kzg_type::batch_of_polynomials_type polys(4); - polys[0].template from_coefficients>({{ 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}); + polys[0].template from_coefficients>({{1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u}}); polys[1].template from_coefficients>({{11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u}}); polys[2].template from_coefficients>({{21u, 22u, 23u, 24u, 25u, 26u, 27u, 28u}}); polys[3].template from_coefficients>({{31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u}}); @@ -969,7 +1004,7 @@ struct placeholder_class_test_initializer { std::set points_0 = {101u, 2u, 3u}; std::set points_1 = {102u, 2u, 3u}; - std::set points_2 = { 1u, 2u, 3u}; + std::set points_2 = {1u, 2u, 3u}; std::set points_3 = {104u, 2u, 3u}; kzg.append_eval_points(batch_id, 0, points_0); kzg.append_eval_points(batch_id, 1, points_1); @@ -988,14 +1023,14 @@ struct placeholder_class_test_initializer { BOOST_AUTO_TEST_SUITE(placeholder_class) using TestFixtures = boost::mpl::list< - placeholder_class_test_initializer< algebra::curves::bls12_381, hashes::keccak_1600<256> >, - placeholder_class_test_initializer< algebra::curves::mnt4_298, hashes::keccak_1600<256> >, - placeholder_class_test_initializer< algebra::curves::mnt6_298, hashes::keccak_1600<256> > - >; - -BOOST_AUTO_TEST_CASE_TEMPLATE(placeholder_class_test, F, TestFixtures) { - F fixture; - BOOST_CHECK(fixture.run_test()); -} + placeholder_class_test_initializer >, + placeholder_class_test_initializer >, + placeholder_class_test_initializer > + >; + + BOOST_AUTO_TEST_CASE_TEMPLATE(placeholder_class_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/type_traits.cpp b/libs/parallel-zk/test/commitment/type_traits.cpp index e472c536..f4f855d2 100644 --- a/libs/parallel-zk/test/commitment/type_traits.cpp +++ b/libs/parallel-zk/test/commitment/type_traits.cpp @@ -44,28 +44,29 @@ using namespace nil::crypto3; BOOST_AUTO_TEST_SUITE(commitments_type_traits_test_suite) -BOOST_AUTO_TEST_CASE(commitments_type_traits_basic_test) { + BOOST_AUTO_TEST_CASE(commitments_type_traits_basic_test) { - typedef algebra::curves::bls12<381> curve_type; - typedef curve_type::base_field_type field_type; + typedef algebra::curves::bls12<381> curve_type; + typedef curve_type::base_field_type field_type; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; - // static_assert(zk::is_commitment< - // zk::commitments::kzg>::value); - static_assert(zk::is_commitment>::value); - static_assert( - zk::is_commitment>::value); - static_assert( - zk::is_commitment>>::value); - static_assert( - zk::is_commitment>>::value); - static_assert(zk::is_commitment>::value); - static_assert(zk::is_commitment>::value); -} + constexpr static const std::size_t m = 2; + + // static_assert(zk::is_commitment< + // zk::commitments::kzg>::value); + static_assert( + zk::is_commitment>::value); + static_assert( + zk::is_commitment>>::value); + static_assert( + zk::is_commitment>>::value); + static_assert(zk::is_commitment>::value); + static_assert(zk::is_commitment>::value); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/math/expression.cpp b/libs/parallel-zk/test/math/expression.cpp index 53bec92a..86f539e9 100644 --- a/libs/parallel-zk/test/math/expression.cpp +++ b/libs/parallel-zk/test/math/expression.cpp @@ -46,110 +46,110 @@ using namespace nil::crypto3::math; BOOST_AUTO_TEST_SUITE(expression_tests_suite) -BOOST_AUTO_TEST_CASE(expression_to_non_linear_combination_test) { - - // setup - using curve_type = algebra::curves::pallas; - using FieldType = typename curve_type::base_field_type; - using variable_type = typename nil::crypto3::zk::snark::plonk_variable; - - variable_type w0(0, 0, variable_type::column_type::witness); - variable_type w1(3, -1, variable_type::column_type::public_input); - variable_type w2(4, 1, variable_type::column_type::public_input); - variable_type w3(6, 2, variable_type::column_type::constant); - - expression expr = (w0 + w1) * (w2 + w3) - w1 * (w2 + w0); - - expression_to_non_linear_combination_visitor visitor; - non_linear_combination result = visitor.convert(expr); - non_linear_combination expected({w0 * w2, w0 * w3, w1 * w3, -w1 * w0}); - - // We may get the terms in a different order due to changes in the code, and that's fine. - BOOST_CHECK_EQUAL(result, expected); -} - -BOOST_AUTO_TEST_CASE(expression_evaluation_test) { - - // setup - using curve_type = algebra::curves::pallas; - using FieldType = typename curve_type::base_field_type; - using variable_type = typename nil::crypto3::zk::snark::plonk_variable; - - variable_type w0(0, 0, variable_type::column_type::witness); - variable_type w1(3, -1, variable_type::column_type::public_input); - variable_type w2(4, 1, variable_type::column_type::public_input); - variable_type w3(6, 2, variable_type::column_type::constant); - - expression expr = (w0 + w1) * (w2 + w3); - - expression_evaluator evaluator( - expr, - [&w0, &w1, &w2, &w3](const variable_type& var) { - if (var == w0) return variable_type::assignment_type(1u); - if (var == w1) return variable_type::assignment_type(2u); - if (var == w2) return variable_type::assignment_type(3u); - if (var == w3) return variable_type::assignment_type(4u); - return variable_type::assignment_type::zero(); - } - ); - - BOOST_CHECK(evaluator.evaluate() == variable_type::assignment_type((1u + 2u) * (3u + 4u))); -} - -BOOST_AUTO_TEST_CASE(expression_max_degree_visitor_test) { - - // setup - using curve_type = algebra::curves::pallas; - using FieldType = typename curve_type::base_field_type; - using variable_type = typename nil::crypto3::zk::snark::plonk_variable; - - variable_type w0(0, 0, variable_type::column_type::witness); - variable_type w1(3, -1, variable_type::column_type::public_input); - variable_type w2(4, 1, variable_type::column_type::public_input); - variable_type w3(6, 2, variable_type::column_type::constant); - - expression expr = (w0 + w1) * (w2 + w3) + w0 * w1 * (w2 + w3); - - expression_max_degree_visitor visitor; - - BOOST_CHECK_EQUAL(visitor.compute_max_degree(expr), 3); -} - -BOOST_AUTO_TEST_CASE(expression_for_each_variable_visitor_test) { - - // setup - using curve_type = algebra::curves::pallas; - using FieldType = typename curve_type::base_field_type; - using variable_type = typename nil::crypto3::zk::snark::plonk_variable; - - variable_type w0(0, 0, variable_type::column_type::witness); - variable_type w1(3, -1, variable_type::column_type::public_input); - variable_type w2(4, 1, variable_type::column_type::public_input); - variable_type w3(6, 2, variable_type::column_type::constant); - - expression expr = (w0 + w1) * (w2 + w3) + w0 * w1 * (w2 + w3); - - std::set variable_indices; - std::set variable_rotations; - - expression_for_each_variable_visitor visitor( - [&variable_indices, &variable_rotations](const variable_type& var) { - variable_indices.insert(var.index); - variable_rotations.insert(var.rotation); - } - ); - - visitor.visit(expr); - - std::set expected_indices = {0, 3, 4, 6}; - std::set expected_rotations = {0, -1, 1, 2}; - - BOOST_CHECK_EQUAL_COLLECTIONS( - variable_indices.begin(), variable_indices.end(), - expected_indices.begin(), expected_indices.end()); - BOOST_CHECK_EQUAL_COLLECTIONS( - variable_rotations.begin(), variable_rotations.end(), - expected_rotations.begin(), expected_rotations.end()); -} + BOOST_AUTO_TEST_CASE(expression_to_non_linear_combination_test) { + + // setup + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using variable_type = typename nil::crypto3::zk::snark::plonk_variable; + + variable_type w0(0, 0, variable_type::column_type::witness); + variable_type w1(3, -1, variable_type::column_type::public_input); + variable_type w2(4, 1, variable_type::column_type::public_input); + variable_type w3(6, 2, variable_type::column_type::constant); + + expression expr = (w0 + w1) * (w2 + w3) - w1 * (w2 + w0); + + expression_to_non_linear_combination_visitor visitor; + non_linear_combination result = visitor.convert(expr); + non_linear_combination expected({w0 * w2, w0 * w3, w1 * w3, -w1 * w0}); + + // We may get the terms in a different order due to changes in the code, and that's fine. + BOOST_CHECK_EQUAL(result, expected); + } + + BOOST_AUTO_TEST_CASE(expression_evaluation_test) { + + // setup + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using variable_type = typename nil::crypto3::zk::snark::plonk_variable; + + variable_type w0(0, 0, variable_type::column_type::witness); + variable_type w1(3, -1, variable_type::column_type::public_input); + variable_type w2(4, 1, variable_type::column_type::public_input); + variable_type w3(6, 2, variable_type::column_type::constant); + + expression expr = (w0 + w1) * (w2 + w3); + + expression_evaluator evaluator( + expr, + [&w0, &w1, &w2, &w3](const variable_type &var) { + if (var == w0) return variable_type::assignment_type(1u); + if (var == w1) return variable_type::assignment_type(2u); + if (var == w2) return variable_type::assignment_type(3u); + if (var == w3) return variable_type::assignment_type(4u); + return variable_type::assignment_type::zero(); + } + ); + + BOOST_CHECK(evaluator.evaluate() == variable_type::assignment_type((1u + 2u) * (3u + 4u))); + } + + BOOST_AUTO_TEST_CASE(expression_max_degree_visitor_test) { + + // setup + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using variable_type = typename nil::crypto3::zk::snark::plonk_variable; + + variable_type w0(0, 0, variable_type::column_type::witness); + variable_type w1(3, -1, variable_type::column_type::public_input); + variable_type w2(4, 1, variable_type::column_type::public_input); + variable_type w3(6, 2, variable_type::column_type::constant); + + expression expr = (w0 + w1) * (w2 + w3) + w0 * w1 * (w2 + w3); + + expression_max_degree_visitor visitor; + + BOOST_CHECK_EQUAL(visitor.compute_max_degree(expr), 3); + } + + BOOST_AUTO_TEST_CASE(expression_for_each_variable_visitor_test) { + + // setup + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using variable_type = typename nil::crypto3::zk::snark::plonk_variable; + + variable_type w0(0, 0, variable_type::column_type::witness); + variable_type w1(3, -1, variable_type::column_type::public_input); + variable_type w2(4, 1, variable_type::column_type::public_input); + variable_type w3(6, 2, variable_type::column_type::constant); + + expression expr = (w0 + w1) * (w2 + w3) + w0 * w1 * (w2 + w3); + + std::set variable_indices; + std::set variable_rotations; + + expression_for_each_variable_visitor visitor( + [&variable_indices, &variable_rotations](const variable_type &var) { + variable_indices.insert(var.index); + variable_rotations.insert(var.rotation); + } + ); + + visitor.visit(expr); + + std::set expected_indices = {0, 3, 4, 6}; + std::set expected_rotations = {0, -1, 1, 2}; + + BOOST_CHECK_EQUAL_COLLECTIONS( + variable_indices.begin(), variable_indices.end(), + expected_indices.begin(), expected_indices.end()); + BOOST_CHECK_EQUAL_COLLECTIONS( + variable_rotations.begin(), variable_rotations.end(), + expected_rotations.begin(), expected_rotations.end()); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/relations/numeric/qap.cpp b/libs/parallel-zk/test/relations/numeric/qap.cpp index d4a94daa..134e384e 100644 --- a/libs/parallel-zk/test/relations/numeric/qap.cpp +++ b/libs/parallel-zk/test/relations/numeric/qap.cpp @@ -73,7 +73,7 @@ void test_qap(const std::size_t qap_degree, const std::size_t num_inputs, const auto begin = std::chrono::high_resolution_clock::now(); - r1cs_example example; + r1cs_example example; if (binary_input) { example = generate_r1cs_example_with_binary_input(num_constraints, num_inputs); } else { @@ -90,9 +90,9 @@ void test_qap(const std::size_t qap_degree, const std::size_t num_inputs, const std::cout << "Constraint system satisfied" << std::endl; const typename FieldType::value_type t = random_element(), - d1 = random_element(), - d2 = random_element(), - d3 = random_element(); + d1 = random_element(), + d2 = random_element(), + d3 = random_element(); begin = std::chrono::high_resolution_clock::now(); qap_instance qap_inst_1 = reductions::r1cs_to_qap::instance_map(example.constraint_system); @@ -105,7 +105,7 @@ void test_qap(const std::size_t qap_degree, const std::size_t num_inputs, const begin = std::chrono::high_resolution_clock::now(); qap_instance_evaluation qap_inst_2 = - reductions::r1cs_to_qap::instance_map_with_evaluation(example.constraint_system, t); + reductions::r1cs_to_qap::instance_map_with_evaluation(example.constraint_system, t); end = std::chrono::high_resolution_clock::now(); elapsed = std::chrono::duration_cast(end - begin); @@ -115,7 +115,8 @@ void test_qap(const std::size_t qap_degree, const std::size_t num_inputs, const begin = std::chrono::high_resolution_clock::now(); qap_witness qap_wit = - reductions::r1cs_to_qap::witness_map(example.constraint_system, example.primary_input, example.auxiliary_input, d1, d2, d3); + reductions::r1cs_to_qap::witness_map(example.constraint_system, example.primary_input, + example.auxiliary_input, d1, d2, d3); end = std::chrono::high_resolution_clock::now(); elapsed = std::chrono::duration_cast(end - begin); @@ -128,25 +129,26 @@ void test_qap(const std::size_t qap_degree, const std::size_t num_inputs, const BOOST_AUTO_TEST_SUITE(qap_test_suite) -BOOST_AUTO_TEST_CASE(qap_test_case) { - const std::size_t num_inputs = 10; + BOOST_AUTO_TEST_CASE(qap_test_case) { + const std::size_t num_inputs = 10; - using basic_curve_type = curves::mnt6<298>; + using basic_curve_type = curves::mnt6<298>; - const std::size_t basic_domain_size = 1ul << fields::arithmetic_params::s; - const std::size_t step_domain_size = (1ul << 10) + (1ul << 8); - const std::size_t extended_domain_size = 1ul << (fields::arithmetic_params::s + 1); - const std::size_t extended_domain_size_special = extended_domain_size - 1; + const std::size_t basic_domain_size = 1ul << fields::arithmetic_params::s; + const std::size_t step_domain_size = (1ul << 10) + (1ul << 8); + const std::size_t extended_domain_size = + 1ul << (fields::arithmetic_params::s + 1); + const std::size_t extended_domain_size_special = extended_domain_size - 1; - test_qap(basic_domain_size, num_inputs, true); - test_qap(step_domain_size, num_inputs, true); - test_qap(extended_domain_size, num_inputs, true); - test_qap(extended_domain_size_special, num_inputs, true); + test_qap(basic_domain_size, num_inputs, true); + test_qap(step_domain_size, num_inputs, true); + test_qap(extended_domain_size, num_inputs, true); + test_qap(extended_domain_size_special, num_inputs, true); - test_qap(basic_domain_size, num_inputs, false); - test_qap(step_domain_size, num_inputs, false); - test_qap(extended_domain_size, num_inputs, false); - test_qap(extended_domain_size_special, num_inputs, false); -} + test_qap(basic_domain_size, num_inputs, false); + test_qap(step_domain_size, num_inputs, false); + test_qap(extended_domain_size, num_inputs, false); + test_qap(extended_domain_size_special, num_inputs, false); + } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/libs/parallel-zk/test/relations/numeric/sap.cpp b/libs/parallel-zk/test/relations/numeric/sap.cpp index ccf9f1a4..833c7b74 100644 --- a/libs/parallel-zk/test/relations/numeric/sap.cpp +++ b/libs/parallel-zk/test/relations/numeric/sap.cpp @@ -67,7 +67,7 @@ void test_sap(const std::size_t sap_degree, const std::size_t num_inputs, const const std::size_t num_constraints = (sap_degree - 1) / 2 - num_inputs; BOOST_CHECK(num_constraints >= 1); - r1cs_example example; + r1cs_example example; if (binary_input) { example = generate_r1cs_example_with_binary_input(num_constraints, num_inputs); } else { @@ -76,16 +76,17 @@ void test_sap(const std::size_t sap_degree, const std::size_t num_inputs, const BOOST_CHECK(example.constraint_system.is_satisfied(example.primary_input, example.auxiliary_input)); const typename FieldType::value_type t = random_element(), - d1 = random_element(), - d2 = random_element(); + d1 = random_element(), + d2 = random_element(); sap_instance sap_inst_1 = reductions::r1cs_to_sap::instance_map(example.constraint_system); sap_instance_evaluation sap_inst_2 = - reductions::r1cs_to_sap::instance_map_with_evaluation(example.constraint_system, t); + reductions::r1cs_to_sap::instance_map_with_evaluation(example.constraint_system, t); sap_witness sap_wit = - reductions::r1cs_to_sap::witness_map(example.constraint_system, example.primary_input, example.auxiliary_input, d1, d2); + reductions::r1cs_to_sap::witness_map(example.constraint_system, example.primary_input, + example.auxiliary_input, d1, d2); BOOST_CHECK(sap_inst_1.is_satisfied(sap_wit)); BOOST_CHECK(sap_inst_2.is_satisfied(sap_wit)); @@ -93,27 +94,29 @@ void test_sap(const std::size_t sap_degree, const std::size_t num_inputs, const BOOST_AUTO_TEST_SUITE(sap_test_suite) -BOOST_AUTO_TEST_CASE(sap_test) { - const std::size_t num_inputs = 10; + BOOST_AUTO_TEST_CASE(sap_test) { + const std::size_t num_inputs = 10; - /** - * due to the specifics of our reduction, we can only get SAPs with odd - * degrees, so we can only test "special" versions of the domains - */ + /** + * due to the specifics of our reduction, we can only get SAPs with odd + * degrees, so we can only test "special" versions of the domains + */ - using basic_curve_type = curves::mnt6<298>; + using basic_curve_type = curves::mnt6<298>; - const std::size_t basic_domain_size_special = (1ul << fields::arithmetic_params::s) - 1ul; - const std::size_t step_domain_size_special = (1ul << 10) + (1ul << 8) - 1ul; - const std::size_t extended_domain_size_special = (1ul << (fields::arithmetic_params::s + 1)) - 1ul; + const std::size_t basic_domain_size_special = + (1ul << fields::arithmetic_params::s) - 1ul; + const std::size_t step_domain_size_special = (1ul << 10) + (1ul << 8) - 1ul; + const std::size_t extended_domain_size_special = + (1ul << (fields::arithmetic_params::s + 1)) - 1ul; - test_sap(basic_domain_size_special, num_inputs, true); - test_sap(step_domain_size_special, num_inputs, true); - test_sap(extended_domain_size_special, num_inputs, true); + test_sap(basic_domain_size_special, num_inputs, true); + test_sap(step_domain_size_special, num_inputs, true); + test_sap(extended_domain_size_special, num_inputs, true); - test_sap(basic_domain_size_special, num_inputs, false); - test_sap(step_domain_size_special, num_inputs, false); - test_sap(extended_domain_size_special, num_inputs, false); -} + test_sap(basic_domain_size_special, num_inputs, false); + test_sap(step_domain_size_special, num_inputs, false); + test_sap(extended_domain_size_special, num_inputs, false); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/relations/numeric/ssp.cpp b/libs/parallel-zk/test/relations/numeric/ssp.cpp index 482d4ae9..ebf19905 100644 --- a/libs/parallel-zk/test/relations/numeric/ssp.cpp +++ b/libs/parallel-zk/test/relations/numeric/ssp.cpp @@ -67,15 +67,16 @@ void test_ssp(const std::size_t num_constraints, const std::size_t num_inputs, c BOOST_CHECK(example.constraint_system.is_satisfied(example.primary_input, example.auxiliary_input)); const typename FieldType::value_type t = random_element(), - d = random_element(); + d = random_element(); ssp_instance ssp_inst_1 = reductions::uscs_to_ssp::instance_map(example.constraint_system); ssp_instance_evaluation ssp_inst_2 = - reductions::uscs_to_ssp::instance_map_with_evaluation(example.constraint_system, t); + reductions::uscs_to_ssp::instance_map_with_evaluation(example.constraint_system, t); ssp_witness ssp_wit = - reductions::uscs_to_ssp::witness_map(example.constraint_system, example.primary_input, example.auxiliary_input, d); + reductions::uscs_to_ssp::witness_map(example.constraint_system, example.primary_input, + example.auxiliary_input, d); BOOST_CHECK(ssp_inst_1.is_satisfied(ssp_wit)); BOOST_CHECK(ssp_inst_2.is_satisfied(ssp_wit)); @@ -83,25 +84,26 @@ void test_ssp(const std::size_t num_constraints, const std::size_t num_inputs, c BOOST_AUTO_TEST_SUITE(ssp_test_suite) -BOOST_AUTO_TEST_CASE(ssp_test) { - const std::size_t num_inputs = 10; + BOOST_AUTO_TEST_CASE(ssp_test) { + const std::size_t num_inputs = 10; - using basic_curve_type = curves::mnt6<298>; + using basic_curve_type = curves::mnt6<298>; - const std::size_t basic_domain_size = 1ul << fields::arithmetic_params::s; - const std::size_t step_domain_size = (1ul << 10) + (1ul << 8); - const std::size_t extended_domain_size = 1ul << (fields::arithmetic_params::s + 1); - const std::size_t extended_domain_size_special = extended_domain_size - 1; + const std::size_t basic_domain_size = 1ul << fields::arithmetic_params::s; + const std::size_t step_domain_size = (1ul << 10) + (1ul << 8); + const std::size_t extended_domain_size = + 1ul << (fields::arithmetic_params::s + 1); + const std::size_t extended_domain_size_special = extended_domain_size - 1; - test_ssp(basic_domain_size, num_inputs, true); - test_ssp(step_domain_size, num_inputs, true); - test_ssp(extended_domain_size, num_inputs, true); - test_ssp(extended_domain_size_special, num_inputs, true); + test_ssp(basic_domain_size, num_inputs, true); + test_ssp(step_domain_size, num_inputs, true); + test_ssp(extended_domain_size, num_inputs, true); + test_ssp(extended_domain_size_special, num_inputs, true); - test_ssp(basic_domain_size, num_inputs, false); - test_ssp(step_domain_size, num_inputs, false); - test_ssp(extended_domain_size, num_inputs, false); - test_ssp(extended_domain_size_special, num_inputs, false); -} + test_ssp(basic_domain_size, num_inputs, false); + test_ssp(step_domain_size, num_inputs, false); + test_ssp(extended_domain_size, num_inputs, false); + test_ssp(extended_domain_size_special, num_inputs, false); + } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/libs/parallel-zk/test/routing_algorithms/test_routing_algorithms.cpp b/libs/parallel-zk/test/routing_algorithms/test_routing_algorithms.cpp index bcb6403a..b3ba305f 100644 --- a/libs/parallel-zk/test/routing_algorithms/test_routing_algorithms.cpp +++ b/libs/parallel-zk/test/routing_algorithms/test_routing_algorithms.cpp @@ -62,15 +62,15 @@ void test_as_waksman(const std::size_t N) { BOOST_AUTO_TEST_SUITE(routing_algorithms_test_suite) -BOOST_AUTO_TEST_CASE(routing_algorithms_test) { - std::size_t bn_size = 8; - printf("* for all permutations on %zu elements\n", bn_size); - test_benes(bn_size); + BOOST_AUTO_TEST_CASE(routing_algorithms_test) { + std::size_t bn_size = 8; + printf("* for all permutations on %zu elements\n", bn_size); + test_benes(bn_size); - std::size_t asw_max_size = 9; - for (std::size_t i = 2; i <= asw_max_size; ++i) { - test_as_waksman(i); + std::size_t asw_max_size = 9; + for (std::size_t i = 2; i <= asw_max_size; ++i) { + test_as_waksman(i); + } } -} BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/libs/parallel-zk/test/systems/plonk/pickles/kimchi.cpp b/libs/parallel-zk/test/systems/plonk/pickles/kimchi.cpp index a1b1b809..0f2bc627 100644 --- a/libs/parallel-zk/test/systems/plonk/pickles/kimchi.cpp +++ b/libs/parallel-zk/test/systems/plonk/pickles/kimchi.cpp @@ -31,19 +31,19 @@ #include #include +#include + #include #include #include -#include +#include #include #include #include #include -#include - #include #include #include @@ -54,243 +54,252 @@ using namespace nil::crypto3::zk::snark; BOOST_AUTO_TEST_SUITE(kimchi_proof_struct_test_suite) -using curve_type = nil::crypto3::algebra::curves::vesta; -using vesta_verifier_index_type = zk::snark::verifier_index< - curve_type, - nil::crypto3::zk::snark::arithmetic_sponge_params, - nil::crypto3::zk::snark::arithmetic_sponge_params, - nil::crypto3::zk::snark::kimchi_constant::COLUMNS, - nil::crypto3::zk::snark::kimchi_constant::PERMUTES ->; - -template -nil::crypto3::multiprecision::cpp_int get_cppui_modular256(Iterator it) { - BOOST_ASSERT(it->second.template get_value() != ""); - return nil::crypto3::multiprecision::cpp_int(it->second.template get_value()); -} + using curve_type = nil::crypto3::algebra::curves::vesta; + using vesta_verifier_index_type = zk::snark::verifier_index< + curve_type, + nil::crypto3::zk::snark::arithmetic_sponge_params, + nil::crypto3::zk::snark::arithmetic_sponge_params, + nil::crypto3::zk::snark::kimchi_constant::COLUMNS, + nil::crypto3::zk::snark::kimchi_constant::PERMUTES + >; + + template + boost::multiprecision::uint256_modular_t get_cppui_modular256(Iterator it) { + BOOST_ASSERT(it->second.template get_value() != ""); + return boost::multiprecision::uint256_modular_t(it->second.template get_value()); + } // make_proof function name is similar to crypto3/marshalling naming style -zk::snark::proof_type make_proof(boost::property_tree::ptree root) { - typename zk::snark::proof_type proof; - size_t i = 0; - std::string base_path = "protocolStateProof.json.proof."; - - auto best_chain = *root.get_child("data.bestChain").begin(); - i = 0; - for (auto &row : best_chain.second.get_child(base_path + "messages.w_comm")) { - auto it = row.second.get_child("").begin()->second.get_child("").begin(); - proof.commitments.w_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - ++i; - } - auto it = best_chain.second.get_child(base_path + "messages.z_comm").begin()->second.get_child("").begin(); - proof.commitments.z_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - - it = best_chain.second.get_child(base_path + "messages.t_comm").begin()->second.get_child("").begin(); - proof.commitments.t_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - // proof.commitments.lookup; // TODO: where it is? - - i = 0; - for (auto &row : best_chain.second.get_child(base_path + "openings.proof.lr")) { - auto it0 = row.second.begin()->second.get_child("").begin(); - auto it1 = row.second.begin(); - it1++; - it1 = it1->second.begin(); - proof.proof.lr.push_back({{get_cppui_modular256(it0++), get_cppui_modular256(it0)}, {get_cppui_modular256(it1++), get_cppui_modular256(it1)}}); - ++i; - } - it = best_chain.second.get_child(base_path + "openings.proof.delta").begin(); - proof.proof.delta = {get_cppui_modular256(it++), get_cppui_modular256(it)}; - it = best_chain.second.get_child(base_path + "openings.proof.sg").begin(); - proof.proof.sg = {get_cppui_modular256(it++), get_cppui_modular256(it)}; + zk::snark::proof_type make_proof(boost::property_tree::ptree root) { + typename zk::snark::proof_type proof; + size_t i = 0; + std::string base_path = "protocolStateProof.json.proof."; - proof.proof.z1 = multiprecision::cpp_int(best_chain.second.get(base_path + "openings.proof.z_1")); - proof.proof.z2 = multiprecision::cpp_int(best_chain.second.get(base_path + "openings.proof.z_2")); + auto best_chain = *root.get_child("data.bestChain").begin(); + i = 0; + for (auto &row: best_chain.second.get_child(base_path + "messages.w_comm")) { + auto it = row.second.get_child("").begin()->second.get_child("").begin(); + proof.commitments.w_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + ++i; + } + auto it = best_chain.second.get_child(base_path + "messages.z_comm").begin()->second.get_child("").begin(); + proof.commitments.z_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - std::size_t ev_i = 0; - for (auto &evals_it : best_chain.second.get_child(base_path + "openings.evals")) { + it = best_chain.second.get_child(base_path + "messages.t_comm").begin()->second.get_child("").begin(); + proof.commitments.t_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + // proof.commitments.lookup; // TODO: where it is? i = 0; - for (auto &row : evals_it.second.get_child("w")) { - for (auto &cell : row.second) { - proof.evals[ev_i].w[i].push_back(get_cppui_modular256(&cell)); - } - i++; + for (auto &row: best_chain.second.get_child(base_path + "openings.proof.lr")) { + auto it0 = row.second.begin()->second.get_child("").begin(); + auto it1 = row.second.begin(); + it1++; + it1 = it1->second.begin(); + proof.proof.lr.push_back({{get_cppui_modular256(it0++), get_cppui_modular256(it0)}, + {get_cppui_modular256(it1++), get_cppui_modular256(it1)}}); + ++i; } + it = best_chain.second.get_child(base_path + "openings.proof.delta").begin(); + proof.proof.delta = {get_cppui_modular256(it++), get_cppui_modular256(it)}; + it = best_chain.second.get_child(base_path + "openings.proof.sg").begin(); + proof.proof.sg = {get_cppui_modular256(it++), get_cppui_modular256(it)}; + + proof.proof.z1 = boost::multiprecision::uint256_modular_t( + best_chain.second.get(base_path + "openings.proof.z_1")); + proof.proof.z2 = boost::multiprecision::uint256_modular_t( + best_chain.second.get(base_path + "openings.proof.z_2")); + + std::size_t ev_i = 0; + for (auto &evals_it: best_chain.second.get_child(base_path + "openings.evals")) { + + i = 0; + for (auto &row: evals_it.second.get_child("w")) { + for (auto &cell: row.second) { + proof.evals[ev_i].w[i].emplace_back(get_cppui_modular256(&cell)); + } + i++; + } - //proof.evals[ev_i].z.size();= get_cppui_modular256(evals_it.second.get_child("z").begin()); - for( auto z_it : evals_it.second.get_child("z") ){ - proof.evals[ev_i].z.push_back(get_cppui_modular256(&z_it)); - } - - i = 0; - for (auto &row : evals_it.second.get_child("s")) { - for (auto &cell : row.second) { - proof.evals[ev_i].s[i].push_back(get_cppui_modular256(&cell)); + //proof.evals[ev_i].z.size();= get_cppui_modular256(evals_it.second.get_child("z").begin()); + for (auto z_it: evals_it.second.get_child("z")) { + proof.evals[ev_i].z.emplace_back(get_cppui_modular256(&z_it)); + } + + i = 0; + for (auto &row: evals_it.second.get_child("s")) { + for (auto &cell: row.second) { + proof.evals[ev_i].s[i].emplace_back(get_cppui_modular256(&cell)); + } + i++; } - i++; - } // proof.evals[ev_i].generic_selector = get_cppui_modular256(evals_it.second.get_child("generic_selector").begin()); - for( auto s_it : evals_it.second.get_child("generic_selector") ){ - proof.evals[ev_i].generic_selector.push_back(get_cppui_modular256(&s_it)); - } + for (auto s_it: evals_it.second.get_child("generic_selector")) { + proof.evals[ev_i].generic_selector.emplace_back(get_cppui_modular256(&s_it)); + } // proof.evals[ev_i].poseidon_selector = get_cppui_modular256(evals_it.second.get_child("poseidon_selector").begin()); - for( auto p_it : evals_it.second.get_child("poseidon_selector") ){ - proof.evals[ev_i].poseidon_selector.push_back(get_cppui_modular256(&p_it)); + for (auto p_it: evals_it.second.get_child("poseidon_selector")) { + proof.evals[ev_i].poseidon_selector.emplace_back(get_cppui_modular256(&p_it)); + } + ev_i++; } - ev_i++; - } - proof.ft_eval1 = multiprecision::cpp_int(best_chain.second.get(base_path + "openings.ft_eval1")); - // // public - // std::vector public_p; // TODO: where it is? - // - // // Previous challenges - // std::vector< - // std::tuple, commitment_scheme>> - // prev_challenges; // TODO: where it is? - return proof; -} - -vesta_verifier_index_type make_verify_index(boost::property_tree::ptree root, boost::property_tree::ptree const_root) { - using curve_type = typename nil::crypto3::algebra::curves::vesta; - - vesta_verifier_index_type ver_index; - size_t i = 0; - - // TODO Is it right? Is it a good way to set domain generator? - // We need to assert, need to check that the input is indeed the root of unity - - auto d_gen = multiprecision::cpp_int(const_root.get("verify_index.domain.group_gen")); - auto d_size = const_root.get("verify_index.domain.log_size_of_group"); - // std::cout << d_gen << " " << d_size << std::endl; - ver_index.domain = nil::crypto3::math::basic_radix2_domain(d_size+1); - // std::cout << ver_index.domain.omega.data << std::endl; - ver_index.domain.omega = d_gen; - - - ver_index.max_poly_size = root.get("data.blockchainVerificationKey.index.max_poly_size"); - ver_index.max_quot_size = root.get("data.blockchainVerificationKey.index.max_quot_size"); - // ver_index.srs = root.get("data.blockchainVerificationKey.index.srs"); // TODO: null - i = 0; - for (auto &row : root.get_child("data.blockchainVerificationKey.commitments.sigma_comm")) { - auto it = row.second.begin(); - ver_index.sigma_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - ++i; + proof.ft_eval1 = boost::multiprecision::uint256_modular_t( + best_chain.second.get(base_path + "openings.ft_eval1")); + // // public + // std::vector public_p; // TODO: where it is? + // + // // Previous challenges + // std::vector< + // std::tuple, commitment_scheme>> + // prev_challenges; // TODO: where it is? + return proof; } - i = 0; - for (auto &row : root.get_child("data.blockchainVerificationKey.commitments.coefficients_comm")) { - auto it = row.second.begin(); - ver_index.coefficients_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - ++i; - } - auto it = root.get_child("data.blockchainVerificationKey.commitments.generic_comm").begin(); - ver_index.generic_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - - it = root.get_child("data.blockchainVerificationKey.commitments.psm_comm").begin(); - ver_index.psm_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - it = root.get_child("data.blockchainVerificationKey.commitments.complete_add_comm").begin(); - ver_index.complete_add_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - it = root.get_child("data.blockchainVerificationKey.commitments.mul_comm").begin(); - ver_index.mul_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - it = root.get_child("data.blockchainVerificationKey.commitments.emul_comm").begin(); - ver_index.emul_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - it = root.get_child("data.blockchainVerificationKey.commitments.endomul_scalar_comm").begin(); - ver_index.endomul_scalar_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - - // TODO: null in example - // i = 0; - // for (auto &row : root.get_child("data.blockchainVerificationKey.commitments.chacha_comm")) { - // auto it = row.second.begin(); - // ver_index.chacha_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); - // ++i; - // } - //i = 0; - // No member shifts - //for (auto &row : root.get_child("data.blockchainVerificationKey.index.shifts")) { - // ver_index.shifts[i] = multiprecision::cpp_int(row.second.get_value()); - // ++i; - //} - - // Polynomial in coefficients form - // Const - ver_index.zkpm = {0x2C46205451F6C3BBEA4BABACBEE609ECF1039A903C42BFF639EDC5BA33356332_cppui_modular256, - 0x1764D9CB4C64EBA9A150920807637D458919CB6948821F4D15EB1994EADF9CE3_cppui_modular256, - 0x0140117C8BBC4CE4644A58F7007148577782213065BB9699BF5C391FBE1B3E6D_cppui_modular256, - 0x0000000000000000000000000000000000000000000000000000000000000001_cppui_modular256}; - ver_index.w = multiprecision::cpp_int(const_root.get("verify_index.w")); - ver_index.endo = multiprecision::cpp_int(const_root.get("verify_index.endo")); - - //ver_index.lookup_index = root.get_child("data.blockchainVerificationKey.index.lookup_index"); // TODO: null - //ver_index.linearization; // TODO: where it is? - ver_index.powers_of_alpha.next_power = 24; - - i = 0; - ver_index.fr_sponge_params.round_constants.resize(const_root.get_child("verify_index.fr_sponge_params.round_constants").size()); - for (auto &row : const_root.get_child("verify_index.fr_sponge_params.round_constants")) { - size_t j = 0; - for (auto cell : row.second){ - ver_index.fr_sponge_params.round_constants[i].push_back(get_cppui_modular256(&cell)); - j++; + vesta_verifier_index_type + make_verify_index(boost::property_tree::ptree root, boost::property_tree::ptree const_root) { + using curve_type = typename nil::crypto3::algebra::curves::vesta; + + vesta_verifier_index_type ver_index; + size_t i = 0; + + // TODO Is it right? Is it a good way to set domain generator? + // We need to assert, need to check that the input is indeed the root of unity + + auto d_gen = boost::multiprecision::uint256_modular_t( + const_root.get("verify_index.domain.group_gen")); + auto d_size = const_root.get("verify_index.domain.log_size_of_group"); + // std::cout << d_gen << " " << d_size << std::endl; + ver_index.domain = nil::crypto3::math::basic_radix2_domain(d_size + 1); + // std::cout << ver_index.domain.omega.data << std::endl; + ver_index.domain.omega = d_gen; + + + ver_index.max_poly_size = root.get("data.blockchainVerificationKey.index.max_poly_size"); + ver_index.max_quot_size = root.get("data.blockchainVerificationKey.index.max_quot_size"); + // ver_index.srs = root.get("data.blockchainVerificationKey.index.srs"); // TODO: null + i = 0; + for (auto &row: root.get_child("data.blockchainVerificationKey.commitments.sigma_comm")) { + auto it = row.second.begin(); + ver_index.sigma_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + ++i; } - i++; - } - i = 0; - for (auto &row : const_root.get_child("verify_index.fr_sponge_params.mds")) { - size_t j = 0; - for (auto cell : row.second){ - ver_index.fr_sponge_params.mds[i][j] = get_cppui_modular256(&cell); - j++; + i = 0; + for (auto &row: root.get_child("data.blockchainVerificationKey.commitments.coefficients_comm")) { + auto it = row.second.begin(); + ver_index.coefficients_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + ++i; } - i++; - } + auto it = root.get_child("data.blockchainVerificationKey.commitments.generic_comm").begin(); + ver_index.generic_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + + it = root.get_child("data.blockchainVerificationKey.commitments.psm_comm").begin(); + ver_index.psm_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + it = root.get_child("data.blockchainVerificationKey.commitments.complete_add_comm").begin(); + ver_index.complete_add_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + it = root.get_child("data.blockchainVerificationKey.commitments.mul_comm").begin(); + ver_index.mul_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + it = root.get_child("data.blockchainVerificationKey.commitments.emul_comm").begin(); + ver_index.emul_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + it = root.get_child("data.blockchainVerificationKey.commitments.endomul_scalar_comm").begin(); + ver_index.endomul_scalar_comm.unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + + // TODO: null in example + // i = 0; + // for (auto &row : root.get_child("data.blockchainVerificationKey.commitments.chacha_comm")) { + // auto it = row.second.begin(); + // ver_index.chacha_comm[i].unshifted.emplace_back(get_cppui_modular256(it++), get_cppui_modular256(it)); + // ++i; + // } + //i = 0; + // No member shifts + //for (auto &row : root.get_child("data.blockchainVerificationKey.index.shifts")) { + // ver_index.shifts[i] = boost::multiprecision::cpp_int(row.second.get_value()); + // ++i; + //} + + // Polynomial in coefficients form + // Const + ver_index.zkpm = {0x2C46205451F6C3BBEA4BABACBEE609ECF1039A903C42BFF639EDC5BA33356332_cppui_modular256, + 0x1764D9CB4C64EBA9A150920807637D458919CB6948821F4D15EB1994EADF9CE3_cppui_modular256, + 0x0140117C8BBC4CE4644A58F7007148577782213065BB9699BF5C391FBE1B3E6D_cppui_modular256, + 0x0000000000000000000000000000000000000000000000000000000000000001_cppui_modular256}; + ver_index.w = boost::multiprecision::uint256_modular_t(const_root.get("verify_index.w")); + ver_index.endo = boost::multiprecision::uint256_modular_t(const_root.get("verify_index.endo")); + + //ver_index.lookup_index = root.get_child("data.blockchainVerificationKey.index.lookup_index"); // TODO: null + //ver_index.linearization; // TODO: where it is? + ver_index.powers_of_alpha.next_power = 24; - i = 0; - ver_index.fq_sponge_params.round_constants.resize(const_root.get_child("verify_index.fq_sponge_params.round_constants").size()); - for (auto &row : const_root.get_child("verify_index.fq_sponge_params.round_constants")) { - size_t j = 0; - for (auto cell : row.second){ - ver_index.fq_sponge_params.round_constants[i].push_back(get_cppui_modular256(&cell)); - j++; + i = 0; + ver_index.fr_sponge_params.round_constants.resize( + const_root.get_child("verify_index.fr_sponge_params.round_constants").size()); + for (auto &row: const_root.get_child("verify_index.fr_sponge_params.round_constants")) { + size_t j = 0; + for (auto cell: row.second) { + ver_index.fr_sponge_params.round_constants[i].emplace_back(get_cppui_modular256(&cell)); + j++; + } + i++; + } + + i = 0; + for (auto &row: const_root.get_child("verify_index.fr_sponge_params.mds")) { + size_t j = 0; + for (auto cell: row.second) { + ver_index.fr_sponge_params.mds[i][j] = get_cppui_modular256(&cell); + j++; + } + i++; } - i++; - } - i = 0; - for (auto &row : const_root.get_child("verify_index.fq_sponge_params.mds")) { - size_t j = 0; - for (auto cell : row.second){ - ver_index.fr_sponge_params.mds[i][j] = get_cppui_modular256(&cell); - j++; + i = 0; + ver_index.fq_sponge_params.round_constants.resize( + const_root.get_child("verify_index.fq_sponge_params.round_constants").size()); + for (auto &row: const_root.get_child("verify_index.fq_sponge_params.round_constants")) { + size_t j = 0; + for (auto cell: row.second) { + ver_index.fq_sponge_params.round_constants[i].emplace_back(get_cppui_modular256(&cell)); + j++; + } + i++; } - i++; + + i = 0; + for (auto &row: const_root.get_child("verify_index.fq_sponge_params.mds")) { + size_t j = 0; + for (auto cell: row.second) { + ver_index.fr_sponge_params.mds[i][j] = get_cppui_modular256(&cell); + j++; + } + i++; + } + + // TODO: Add assertions about right size of + // fr_sponge_params.mds, + // fr_sponge_params.round_constants, + + return ver_index; + } + + BOOST_AUTO_TEST_CASE(pickles_proof_struct_test_suite) { + boost::property_tree::ptree root; + boost::property_tree::ptree const_root; + // Load the json file in this ptree + std::string test_data = TEST_DATA; + boost::property_tree::read_json(test_data + ".json", root); + boost::property_tree::read_json(test_data + "_const.json", const_root); + + zk::snark::proof_type proof = make_proof(root); + vesta_verifier_index_type ver_index = make_verify_index(root, const_root); + + //group_map g_map; + //BOOST_CHECK(true); + // TODO :: verifier should work correctly + //verifier::verify(g_map, ver_index, proof); } - // TODO: Add assertions about right size of - // fr_sponge_params.mds, - // fr_sponge_params.round_constants, - - return ver_index; -} - -BOOST_AUTO_TEST_CASE(pickles_proof_struct_test_suite) { - boost::property_tree::ptree root; - boost::property_tree::ptree const_root; - // Load the json file in this ptree - std::string test_data = TEST_DATA; - boost::property_tree::read_json(test_data + ".json", root); - boost::property_tree::read_json(test_data + "_const.json", const_root); - - zk::snark::proof_type proof = make_proof(root); - vesta_verifier_index_type ver_index = make_verify_index(root, const_root); - - //group_map g_map; - //BOOST_CHECK(true); - // TODO :: verifier should work correctly - //verifier::verify(g_map, ver_index, proof); -} BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/libs/parallel-zk/test/systems/plonk/pickles/pickles.cpp b/libs/parallel-zk/test/systems/plonk/pickles/pickles.cpp index 152fd12f..391c0bbe 100644 --- a/libs/parallel-zk/test/systems/plonk/pickles/pickles.cpp +++ b/libs/parallel-zk/test/systems/plonk/pickles/pickles.cpp @@ -31,18 +31,19 @@ #include #include -#include +#include #include #include +#include + #include #include #include #include #include -#include #include diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_circuits.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_circuits.cpp index c7230d69..a7671f07 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_circuits.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_circuits.cpp @@ -45,99 +45,99 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuits) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using hash_type = hashes::poseidon>; -using test_runner_type = placeholder_test_runner; - -BOOST_AUTO_TEST_CASE(circuit1) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_1( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit2) -{ - test_tools::random_test_initializer random_test_initializer; - auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit3) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_3( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit4) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_4( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit5) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_5( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit6) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_6( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit7) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_7( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_CASE(circuit_fib) -{ - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_fib( - random_test_initializer.alg_random_engines.template get_alg_engine() - ); - test_runner_type test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using hash_type = hashes::poseidon>; + using test_runner_type = placeholder_test_runner; + + BOOST_AUTO_TEST_CASE(circuit1) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit2) + { + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit3) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_3( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit4) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_4( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit5) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_5( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit6) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_6( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit7) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_7( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + + BOOST_AUTO_TEST_CASE(circuit_fib) + { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_fib( + random_test_initializer.alg_random_engines.template get_alg_engine() + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_curves.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_curves.cpp index 1402474f..182b0591 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_curves.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_curves.cpp @@ -65,27 +65,28 @@ using namespace nil::crypto3::zk::snark; BOOST_AUTO_TEST_SUITE(placeholder_curves_test) -using hash_type = hashes::keccak_1600<256>; + using hash_type = hashes::keccak_1600<256>; -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner ->; + using TestRunners = boost::mpl::list< + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner + >; + + BOOST_AUTO_TEST_CASE_TEMPLATE(curve_test, TestRunner, TestRunners) { + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } -BOOST_AUTO_TEST_CASE_TEMPLATE(curve_test, TestRunner, TestRunners) { - using field_type = typename TestRunner::field_type; - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_1( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp index a829c920..213a1274 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp @@ -77,9 +77,9 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) using policy_type = zk::snark::detail::placeholder_policy; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -90,121 +90,127 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) using kzg_scheme_type = typename commitments::kzg_commitment_scheme; using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; -BOOST_FIXTURE_TEST_CASE(placeholder_gate_argument_test, test_tools::random_test_initializer) { - auto pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - - plonk_table_description desc( - circuit.table.witnesses().size(), - circuit.table.public_inputs().size(), - circuit.table.constants().size(), - circuit.table.selectors().size(), - circuit.usable_rows, - circuit.table_rows); - - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, circuit.copy_constraints, circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type transcript(init_blob); - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme + BOOST_FIXTURE_TEST_CASE(placeholder_gate_argument_test, test_tools::random_test_initializer) { + auto pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine ); - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type transcript(init_blob); + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme ); - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); - transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; - transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); - math::polynomial_dfs mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, - typename field_type::value_type(1) - ); - mask_polynomial -= preprocessed_public_data.q_last; - mask_polynomial -= preprocessed_public_data.q_blind; + transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; + transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; - std::array, 1> prover_res = - placeholder_gates_argument::prove_eval( - constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); + math::polynomial_dfs mask_polynomial( + 0, preprocessed_public_data.common_data.basic_domain->m, + typename field_type::value_type(1) + ); + mask_polynomial -= preprocessed_public_data.q_last; + mask_polynomial -= preprocessed_public_data.q_blind; - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + std::array, 1> prover_res = + placeholder_gates_argument::prove_eval( + constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, + preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < desc.witness_columns; i++) { + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element( + 1); - std::size_t i_global_index = i; + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + std::size_t i_global_index = i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + desc.public_input_columns; i++) { + for (std::size_t i = 0; i < 0 + desc.public_input_columns; i++) { - std::size_t i_global_index = desc.witness_columns + i; + std::size_t i_global_index = desc.witness_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::public_input); + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::public_input); - columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { - std::size_t i_global_index = - desc.witness_columns + desc.public_input_columns + i; + std::size_t i_global_index = + desc.witness_columns + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < desc.selector_columns; i++) { + for (std::size_t i = 0; i < desc.selector_columns; i++) { - std::size_t i_global_index = desc.witness_columns + desc.constant_columns + desc.public_input_columns + i; + std::size_t i_global_index = desc.witness_columns + desc.constant_columns + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - } - auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - - preprocessed_public_data.q_blind.evaluate(y); - std::array verifier_res = - placeholder_gates_argument::verify_eval( - constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); + auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - + preprocessed_public_data.q_blind.evaluate(y); + std::array verifier_res = + placeholder_gates_argument::verify_eval( + constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); -} + BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_hashes.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_hashes.cpp index 99adacc8..41537975 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_hashes.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_hashes.cpp @@ -56,28 +56,29 @@ using namespace nil::crypto3::zk::snark; BOOST_AUTO_TEST_SUITE(placeholder_hashes_test) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; -using keccak_256_type = hashes::keccak_1600<256>; -using keccak_512_type = hashes::keccak_1600<512>; -using sha2_256_type = hashes::sha2<256>; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + using keccak_256_type = hashes::keccak_1600<256>; + using keccak_512_type = hashes::keccak_1600<512>; + using sha2_256_type = hashes::sha2<256>; -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner, - placeholder_test_runner ->; + using TestRunners = boost::mpl::list< + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner + >; + + BOOST_AUTO_TEST_CASE_TEMPLATE(hash_test, TestRunner, TestRunners) { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } -BOOST_AUTO_TEST_CASE_TEMPLATE(hash_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_1( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_kzg.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_kzg.cpp index 27d301c8..d9d3c5ce 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_kzg.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_kzg.cpp @@ -76,43 +76,43 @@ using hash_type = hashes::keccak_1600<256>; BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg) using TestRunners = boost::mpl::list< - placeholder_kzg_test_runner, - placeholder_kzg_test_runner + placeholder_kzg_test_runner, + placeholder_kzg_test_runner >; -BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_test, TestRunner, TestRunners) -{ - using field_type = typename TestRunner::field_type; - test_tools::random_test_initializer random_test_initializer; - auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} + BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_test, TestRunner, TestRunners) { + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) using TestRunners = boost::mpl::list< - placeholder_kzg_test_runner_v2, - placeholder_kzg_test_runner_v2 + placeholder_kzg_test_runner_v2, + placeholder_kzg_test_runner_v2 >; -BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_v2_test, TestRunner, TestRunners) -{ - using field_type = typename TestRunner::field_type; - test_tools::random_test_initializer random_test_initializer; - auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} + BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_v2_test, TestRunner, TestRunners) { + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp index 52788ecb..26a59e93 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp @@ -85,9 +85,9 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) using circuit_params = placeholder_circuit_params; using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -95,145 +95,156 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; using policy_type = zk::snark::detail::placeholder_policy; -BOOST_FIXTURE_TEST_CASE(lookup_test, test_tools::random_test_initializer) { - auto circuit = circuit_test_3( - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - constexpr std::size_t argument_size = 4; - - plonk_table_description desc( - circuit.table.witnesses().size(), - circuit.table.public_inputs().size(), - circuit.table.constants().size(), - circuit.table.selectors().size(), - circuit.usable_rows, - circuit.table_rows); - - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, - circuit.copy_constraints, - circuit.lookup_gates, - circuit.lookup_tables - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, true); - lpc_scheme_type lpc_scheme(fri_params); - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table - ); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover lookup_prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = lookup_prover.prove_eval(); - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < desc.witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + BOOST_FIXTURE_TEST_CASE(lookup_test, test_tools::random_test_initializer) { + auto circuit = circuit_test_3( + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + constexpr std::size_t argument_size = 4; + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, + circuit.copy_constraints, + circuit.lookup_gates, + circuit.lookup_tables + ); + typename policy_type::variable_assignment_type assignments = circuit.table; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, + true); + lpc_scheme_type lpc_scheme(fri_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table + ); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover lookup_prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = lookup_prover.prove_eval(); + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { - std::size_t i_global_index = desc.witness_columns + - desc.public_input_columns + i; + std::size_t i_global_index = desc.witness_columns + + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < desc.selector_columns; i++) { + for (std::size_t i = 0; i < desc.selector_columns; i++) { - std::size_t i_global_index = desc.witness_columns + - desc.constant_columns + - desc.public_input_columns + i; + std::size_t i_global_index = desc.witness_columns + + desc.constant_columns + + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); - } - } - - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, y * omega); - - transcript_type transcript; - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted and V_L values - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier lookup_verifier; - std::array verifier_res = lookup_verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (int i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - if( prover_res.F_dfs[i].evaluate(y) != verifier_res[i] ){ - std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, + y * omega); + + transcript_type transcript; + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted and V_L values + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier lookup_verifier; + std::array verifier_res = lookup_verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (int i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + if (prover_res.F_dfs[i].evaluate(y) != verifier_res[i]) { + std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; + } + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if (prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + field_type::value_type::zero()) { + std::cout << "![" << i << "][" << j << "]" << std::endl; + + } + BOOST_CHECK(prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero()); } - BOOST_CHECK(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } -} + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) @@ -251,9 +262,9 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) using circuit_params = placeholder_circuit_params; using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -261,148 +272,155 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; using policy_type = zk::snark::detail::placeholder_policy; -BOOST_FIXTURE_TEST_CASE(lookup_test, test_tools::random_test_initializer) { - auto circuit = circuit_test_4( - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - constexpr std::size_t argument_size = 4; - - plonk_table_description desc( - circuit.table.witnesses().size(), - circuit.table.public_inputs().size(), - circuit.table.constants().size(), - circuit.table.selectors().size(), - circuit.usable_rows, - circuit.table_rows); - - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, - circuit.copy_constraints, - circuit.lookup_gates, - circuit.lookup_tables - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - transcript_type transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = prover.prove_eval(); - - // Challenge phase - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < desc.witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + BOOST_FIXTURE_TEST_CASE(lookup_test, test_tools::random_test_initializer) { + auto circuit = circuit_test_4( + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + constexpr std::size_t argument_size = 4; + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, + circuit.copy_constraints, + circuit.lookup_gates, + circuit.lookup_tables + ); + typename policy_type::variable_assignment_type assignments = circuit.table; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + transcript_type transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = prover.prove_eval(); + + // Challenge phase + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { - std::size_t i_global_index = desc.witness_columns + - desc.public_input_columns + i; + std::size_t i_global_index = desc.witness_columns + + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::constant); - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } } - } - for (std::size_t i = 0; i < desc.selector_columns; i++) { + for (std::size_t i = 0; i < desc.selector_columns; i++) { - std::size_t i_global_index = desc.witness_columns + - desc.constant_columns + - desc.public_input_columns + i; + std::size_t i_global_index = desc.witness_columns + + desc.constant_columns + + desc.public_input_columns + i; - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, + plonk_variable::column_type::selector); - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } } - } - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); - - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted, V_L and V_S values. - - auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + - preprocessed_public_data.q_blind.evaluate(y))); - auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); - - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier verifier; - std::array verifier_res = verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, 0), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if (prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != - field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); + + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted, V_L and V_S values. + + auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + + preprocessed_public_data.q_blind.evaluate(y))); + auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); + + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier verifier; + std::array verifier_res = verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, 0), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if (prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + field_type::value_type::zero()) { + std::cout << "![" << i << "][" << j << "]" << std::endl; + } + BOOST_CHECK( + prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero()); } - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == - field_type::value_type::zero()); } } -} + BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp index 64d72f9b..2190e08d 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp @@ -89,9 +89,9 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) using policy_type = zk::snark::detail::placeholder_policy; using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -102,203 +102,212 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) using kzg_scheme_type = typename commitments::kzg_commitment_scheme; using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; -BOOST_FIXTURE_TEST_CASE(permutation_polynomials_test, test_tools::random_test_initializer) { - typename field_type::value_type pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - - plonk_table_description desc( - circuit.table.witnesses().size(), - circuit.table.public_inputs().size(), - circuit.table.constants().size(), - circuit.table.selectors().size(), - circuit.usable_rows, - circuit.table_rows); - - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, - circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; - - typename lpc_type::fri_type::params_type fri_params(1,table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - transcript_type lpc_transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme + BOOST_FIXTURE_TEST_CASE(permutation_polynomials_test, test_tools::random_test_initializer) { + typename field_type::value_type pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine ); - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); - auto polynomial_table = - plonk_polynomial_dfs_table( - lpc_preprocessed_private_data.private_polynomial_table, lpc_preprocessed_public_data.public_polynomial_table); + std::size_t table_rows_log = std::log2(desc.rows_amount); - std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; - typename field_type::value_type id_res = field_type::value_type::one(); - typename field_type::value_type sigma_res = field_type::value_type::one(); - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (auto &identity_polynomial : lpc_preprocessed_public_data.identity_polynomials) { - id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); - } + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; - for (auto &permutation_polynomial : lpc_preprocessed_public_data.permutation_polynomials) { - sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); - } - } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + transcript_type lpc_transcript; - typename field_type::value_type beta = algebra::random_element(); - typename field_type::value_type gamma = algebra::random_element(); + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme + ); - id_res = field_type::value_type::one(); - sigma_res = field_type::value_type::one(); - const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc + ); - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { - id_res = id_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); + auto polynomial_table = + plonk_polynomial_dfs_table( + lpc_preprocessed_private_data.private_polynomial_table, + lpc_preprocessed_public_data.public_polynomial_table); + + std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; + typename field_type::value_type id_res = field_type::value_type::one(); + typename field_type::value_type sigma_res = field_type::value_type::one(); + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (auto &identity_polynomial: lpc_preprocessed_public_data.identity_polynomials) { + id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); + } + + for (auto &permutation_polynomial: lpc_preprocessed_public_data.permutation_polynomials) { + sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); + } } - - for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { - sigma_res = - sigma_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); + + typename field_type::value_type beta = algebra::random_element(); + typename field_type::value_type gamma = algebra::random_element(); + + id_res = field_type::value_type::one(); + sigma_res = field_type::value_type::one(); + const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; + + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { + id_res = id_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * + lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + + gamma); + } + + for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { + sigma_res = + sigma_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate( + domain->get_domain_element(i)) + + gamma); + } } + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); -} -BOOST_FIXTURE_TEST_CASE(placeholder_split_polynomial_test, test_tools::random_test_initializer) { - math::polynomial f = {1, 3, 4, 1, 5, 6, 7, 2, 8, 7, 5, 6, 1, 2, 1, 1}; - std::size_t expected_size = 4; - std::size_t max_degree = 3; + BOOST_FIXTURE_TEST_CASE(placeholder_split_polynomial_test, test_tools::random_test_initializer) { + math::polynomial f = {1, 3, 4, 1, 5, 6, 7, 2, 8, 7, 5, 6, 1, 2, 1, 1}; + std::size_t expected_size = 4; + std::size_t max_degree = 3; - std::vector> f_splitted = - zk::snark::detail::split_polynomial(f, max_degree); + std::vector> f_splitted = + zk::snark::detail::split_polynomial(f, max_degree); - BOOST_CHECK(f_splitted.size() == expected_size); + BOOST_CHECK(f_splitted.size() == expected_size); - typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); + typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); - typename field_type::value_type f_at_y = f.evaluate(y); - typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); - for (std::size_t i = 0; i < f_splitted.size(); i++) { - f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); - } - - BOOST_CHECK(f_at_y == f_splitted_at_y); -} + typename field_type::value_type f_at_y = f.evaluate(y); + typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); + for (std::size_t i = 0; i < f_splitted.size(); i++) { + f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); + } -BOOST_FIXTURE_TEST_CASE(permutation_argument_test, test_tools::random_test_initializer) { - auto pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); + BOOST_CHECK(f_at_y == f_splitted_at_y); + } - plonk_table_description desc( - circuit.table.witnesses().size(), - circuit.table.public_inputs().size(), - circuit.table.constants().size(), - circuit.table.selectors().size(), - circuit.usable_rows, - circuit.table_rows); + BOOST_FIXTURE_TEST_CASE(permutation_argument_test, test_tools::random_test_initializer) { + auto pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); - std::size_t table_rows_log = std::log2(desc.rows_amount); + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); - const std::size_t argument_size = 3; + std::size_t table_rows_log = std::log2(desc.rows_amount); - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); + const std::size_t argument_size = 3; - typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, - circuit.lookup_gates); - typename policy_type::variable_assignment_type assignments = circuit.table; + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); - transcript_type transcript; + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, + circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme - ); + transcript_type transcript; - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme ); - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript::fiat_shamir_heuristic_sequential prover_transcript(init_blob); - transcript::fiat_shamir_heuristic_sequential verifier_transcript(init_blob); - - typename placeholder_permutation_argument::prover_result_type prover_res = - placeholder_permutation_argument::prove_eval( - constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, prover_transcript); - - // Challenge phase - const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; - - typename field_type::value_type y = algebra::random_element(); - std::vector f_at_y(permuted_columns.size()); - std::vector S_id(permuted_columns.size()); - std::vector S_sigma(permuted_columns.size()); - for (std::size_t i = 0; i < permuted_columns.size(); i++) { - f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); - S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); - S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); - } - - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); - typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); - - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - - auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); - std::array verifier_res = - placeholder_permutation_argument::verify_eval( - preprocessed_public_data.common_data, - S_id, S_sigma, - special_selector_values, - y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc ); - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, + preprocessed_public_data.public_polynomial_table); + + std::vector init_blob{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript::fiat_shamir_heuristic_sequential prover_transcript( + init_blob); + transcript::fiat_shamir_heuristic_sequential verifier_transcript( + init_blob); + + typename placeholder_permutation_argument::prover_result_type prover_res = + placeholder_permutation_argument::prove_eval( + constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, + prover_transcript); + + // Challenge phase + const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; + + typename field_type::value_type y = algebra::random_element(); + std::vector f_at_y(permuted_columns.size()); + std::vector S_id(permuted_columns.size()); + std::vector S_sigma(permuted_columns.size()); + for (std::size_t i = 0; i < permuted_columns.size(); i++) { + f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); + S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); + S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); + } - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero() - ); + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); + typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); + + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + + auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); + std::array verifier_res = + placeholder_permutation_argument::verify_eval( + preprocessed_public_data.common_data, + S_id, S_sigma, + special_selector_values, + y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + BOOST_CHECK( + prover_res.F_dfs[i].evaluate( + preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero() + ); + } } } -} BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp index 4a28f880..0cb79711 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp @@ -51,24 +51,25 @@ using namespace nil::crypto3::zk::snark; BOOST_AUTO_TEST_SUITE(placeholder_quotient_polynomial_chunks) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using hash_type = hashes::keccak_1600<256>; + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using hash_type = hashes::keccak_1600<256>; -using TestRunners = boost::mpl::list< - placeholder_test_runner< field_type, hash_type, hash_type, true, 8>, - placeholder_test_runner< field_type, hash_type, hash_type, true, 10>, - placeholder_test_runner< field_type, hash_type, hash_type, true, 30>, - placeholder_test_runner< field_type, hash_type, hash_type, true, 50> + using TestRunners = boost::mpl::list< + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner >; -BOOST_AUTO_TEST_CASE_TEMPLATE(quotient_polynomial_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_7( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit); - BOOST_CHECK(test_runner.run_test()); -} + BOOST_AUTO_TEST_CASE_TEMPLATE(quotient_polynomial_test, TestRunner, TestRunners) { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_7( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); + } + BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp index 47837fdd..bae48452 100644 --- a/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp +++ b/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp @@ -49,12 +49,11 @@ using namespace nil::crypto3; using namespace nil::crypto3::zk; using namespace nil::crypto3::zk::snark; -template< - typename FieldType, - typename merkle_hash_type, - typename transcript_hash_type, - bool UseGrinding = false, - std::size_t max_quotient_poly_chunks = 0> +template struct placeholder_test_runner { using field_type = FieldType; @@ -67,9 +66,9 @@ struct placeholder_test_runner { using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; using lpc_params_type = commitments::list_polynomial_commitment_params< - merkle_hash_type, - transcript_hash_type, - placeholder_test_params::m + merkle_hash_type, + transcript_hash_type, + placeholder_test_params::m >; using lpc_type = commitments::list_polynomial_commitment; @@ -78,41 +77,35 @@ struct placeholder_test_runner { using policy_type = zk::snark::detail::placeholder_policy; using circuit_type = circuit_description>; - placeholder_test_runner(const circuit_type& circuit_in) - : circuit(circuit_in) - , desc( circuit_in.table.witnesses().size(), - circuit_in.table.public_inputs().size(), - circuit_in.table.constants().size(), - circuit_in.table.selectors().size(), - circuit_in.usable_rows, - circuit_in.table_rows) - , constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables) - , assignments(circuit.table) - , table_rows_log(std::log2(circuit_in.table_rows)) - , fri_params(1,table_rows_log, placeholder_test_params::lambda, 4) - { + placeholder_test_runner(const circuit_type &circuit_in) + : circuit(circuit_in), desc(circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows), + constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables), + assignments(circuit.table), table_rows_log(std::log2(circuit_in.table_rows)), + fri_params(1, table_rows_log, placeholder_test_params::lambda, 4) { } bool run_test() { lpc_scheme_type lpc_scheme(fri_params); typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks - ); + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks); typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc); auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme - ); + lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, + lpc_scheme); bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme - ); + lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme); return verifier_res; } @@ -124,10 +117,9 @@ struct placeholder_test_runner { typename lpc_type::fri_type::params_type fri_params; }; -template< - typename curve_type, - typename transcript_hash_type, - bool UseGrinding = false> +template struct placeholder_kzg_test_runner { using field_type = typename curve_type::scalar_field_type; @@ -142,26 +134,25 @@ struct placeholder_kzg_test_runner { using policy_type = zk::snark::detail::placeholder_policy; using circuit_type = - circuit_description >; - - placeholder_kzg_test_runner(const circuit_type& circuit_in) - : circuit(circuit_in), - desc(circuit_in.table.witnesses().size(), - circuit_in.table.public_inputs().size(), - circuit_in.table.constants().size(), - circuit_in.table.selectors().size(), - circuit_in.usable_rows, - circuit_in.table_rows) - { + circuit_description >; + + placeholder_kzg_test_runner(const circuit_type &circuit_in) + : circuit(circuit_in), + desc(circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows) { } bool run_test() { typename policy_type::constraint_system_type - constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); typename policy_type::variable_assignment_type - assignments = circuit.table; + assignments = circuit.table; bool verifier_res; @@ -171,23 +162,20 @@ struct placeholder_kzg_test_runner { kzg_scheme_type kzg_scheme(kzg_params); typename placeholder_public_preprocessor::preprocessed_data_type - kzg_preprocessed_public_data = - placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, kzg_scheme - ); + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme); typename placeholder_private_preprocessor::preprocessed_data_type - kzg_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); auto kzg_proof = placeholder_prover::process( - kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme - ); + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, + kzg_scheme); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } @@ -195,10 +183,9 @@ struct placeholder_kzg_test_runner { plonk_table_description desc; }; -template< - typename curve_type, - typename transcript_hash_type, - bool UseGrinding = false> +template struct placeholder_kzg_test_runner_v2 { using field_type = typename curve_type::scalar_field_type; @@ -213,51 +200,47 @@ struct placeholder_kzg_test_runner_v2 { using policy_type = zk::snark::detail::placeholder_policy; using circuit_type = - circuit_description>; - - placeholder_kzg_test_runner_v2(const circuit_type& circuit_in) - : circuit(circuit_in), - desc(circuit_in.table.witnesses().size(), - circuit_in.table.public_inputs().size(), - circuit_in.table.constants().size(), - circuit_in.table.selectors().size(), - circuit_in.usable_rows, - circuit_in.table_rows) - { + circuit_description>; + + placeholder_kzg_test_runner_v2(const circuit_type &circuit_in) + : circuit(circuit_in), + desc(circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows) { } bool run_test() { typename policy_type::constraint_system_type - constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); typename policy_type::variable_assignment_type - assignments = circuit.table; + assignments = circuit.table; bool verifier_res; - typename kzg_type::field_type::value_type alpha (7); + typename kzg_type::field_type::value_type alpha(7); auto kzg_params = kzg_scheme_type::create_params(circuit.table_rows, alpha); kzg_scheme_type kzg_scheme(kzg_params); typename placeholder_public_preprocessor::preprocessed_data_type - kzg_preprocessed_public_data = - placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, kzg_scheme - ); + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme); typename placeholder_private_preprocessor::preprocessed_data_type - kzg_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); auto kzg_proof = placeholder_prover::process( - kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme - ); + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, + kzg_scheme); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } diff --git a/libs/parallel-zk/test/systems/ppzksnark/r1cs_gg_ppzksnark/run_r1cs_gg_ppzksnark_tvm_marshalling.hpp b/libs/parallel-zk/test/systems/ppzksnark/r1cs_gg_ppzksnark/run_r1cs_gg_ppzksnark_tvm_marshalling.hpp index ea3ba72b..a05f657f 100644 --- a/libs/parallel-zk/test/systems/ppzksnark/r1cs_gg_ppzksnark/run_r1cs_gg_ppzksnark_tvm_marshalling.hpp +++ b/libs/parallel-zk/test/systems/ppzksnark/r1cs_gg_ppzksnark/run_r1cs_gg_ppzksnark_tvm_marshalling.hpp @@ -59,37 +59,49 @@ namespace nil { * a primary input for CS, and a proof. */ template - bool run_r1cs_gg_ppzksnark_tvm_marshalling(const r1cs_example &example); + bool run_r1cs_gg_ppzksnark_tvm_marshalling( + const r1cs_example &example); template - void print_proving_key(r1cs_gg_ppzksnark_proving_key pk){ - std::cout << "alpha_g1: " - << pk.alpha_g1.to_affine().X.data << " " << pk.alpha_g1.to_affine().Y.data << " " << pk.alpha_g1.to_affine().Z.data << std::endl - << "beta_g1: " - << pk.beta_g1.to_affine().X.data << " " << pk.beta_g1.to_affine().Y.data << " " << pk.beta_g1.to_affine().Z.data << std::endl - << "beta_g2: " - << pk.beta_g2.to_affine().X.data[0].data << " " << pk.beta_g2.to_affine().Y.data[0].data << " " << pk.beta_g2.to_affine().Z.data[0].data << std::endl - << pk.beta_g2.to_affine().X.data[1].data << " " << pk.beta_g2.to_affine().Y.data[1].data << " " << pk.beta_g2.to_affine().Z.data[1].data << std::endl - << "delta_g1: " - << pk.delta_g1.to_affine().X.data << " " << pk.delta_g1.to_affine().Y.data << " " << pk.delta_g1.to_affine().Z.data << std::endl - << "delta_g2: " - << pk.delta_g2.to_affine().X.data[0].data << " " << pk.delta_g2.to_affine().Y.data[0].data << " " << pk.delta_g2.to_affine().Z.data[0].data << std::endl - << pk.delta_g2.to_affine().X.data[1].data << " " << pk.delta_g2.to_affine().Y.data[1].data << " " << pk.delta_g2.to_affine().Z.data[1].data << std::endl; + void print_proving_key(r1cs_gg_ppzksnark_proving_key pk) { + std::cout << "alpha_g1: " + << pk.alpha_g1.to_affine().X.data << " " << pk.alpha_g1.to_affine().Y.data << " " + << pk.alpha_g1.to_affine().Z.data << std::endl + << "beta_g1: " + << pk.beta_g1.to_affine().X.data << " " << pk.beta_g1.to_affine().Y.data << " " + << pk.beta_g1.to_affine().Z.data << std::endl + << "beta_g2: " + << pk.beta_g2.to_affine().X.data[0].data << " " << pk.beta_g2.to_affine().Y.data[0].data + << " " << pk.beta_g2.to_affine().Z.data[0].data << std::endl + << pk.beta_g2.to_affine().X.data[1].data << " " << pk.beta_g2.to_affine().Y.data[1].data + << " " << pk.beta_g2.to_affine().Z.data[1].data << std::endl + << "delta_g1: " + << pk.delta_g1.to_affine().X.data << " " << pk.delta_g1.to_affine().Y.data << " " + << pk.delta_g1.to_affine().Z.data << std::endl + << "delta_g2: " + << pk.delta_g2.to_affine().X.data[0].data << " " << pk.delta_g2.to_affine().Y.data[0].data + << " " << pk.delta_g2.to_affine().Z.data[0].data << std::endl + << pk.delta_g2.to_affine().X.data[1].data << " " << pk.delta_g2.to_affine().Y.data[1].data + << " " << pk.delta_g2.to_affine().Z.data[1].data << std::endl; } template - void print_verification_key(r1cs_gg_ppzksnark_verification_key vk){ - std::cout << "gamma_g2: " - << vk.gamma_g2.to_affine().X.data[0].data << " " << vk.gamma_g2.to_affine().Y.data[0].data << " " << vk.gamma_g2.to_affine().Z.data[0].data << std::endl - << vk.gamma_g2.to_affine().X.data[1].data << " " << vk.gamma_g2.to_affine().Y.data[1].data << " " << vk.gamma_g2.to_affine().Z.data[1].data << std::endl - << "delta_g2: " - << vk.delta_g2.to_affine().X.data[0].data << " " << vk.delta_g2.to_affine().Y.data[0].data << " " << vk.delta_g2.to_affine().Z.data[0].data << std::endl - << vk.delta_g2.to_affine().X.data[1].data << " " << vk.delta_g2.to_affine().Y.data[1].data << " " << vk.delta_g2.to_affine().Z.data[1].data << std::endl; + void print_verification_key(r1cs_gg_ppzksnark_verification_key vk) { + std::cout << "gamma_g2: " + << vk.gamma_g2.to_affine().X.data[0].data << " " << vk.gamma_g2.to_affine().Y.data[0].data + << " " << vk.gamma_g2.to_affine().Z.data[0].data << std::endl + << vk.gamma_g2.to_affine().X.data[1].data << " " << vk.gamma_g2.to_affine().Y.data[1].data + << " " << vk.gamma_g2.to_affine().Z.data[1].data << std::endl + << "delta_g2: " + << vk.delta_g2.to_affine().X.data[0].data << " " << vk.delta_g2.to_affine().Y.data[0].data + << " " << vk.delta_g2.to_affine().Z.data[0].data << std::endl + << vk.delta_g2.to_affine().X.data[1].data << " " << vk.delta_g2.to_affine().Y.data[1].data + << " " << vk.delta_g2.to_affine().Z.data[1].data << std::endl; } template<> bool run_r1cs_gg_ppzksnark_tvm_marshalling>( - const r1cs_example::scalar_field_type> &example) { + const r1cs_example::scalar_field_type> &example) { using CurveType = typename nil::crypto3::algebra::curves::bls12<381>; @@ -98,17 +110,21 @@ namespace nil { std::cout << "Starting generator" << std::endl; typename scheme_type::keypair_type keypair = - generate(example.constraint_system); + generate(example.constraint_system); std::cout << "Starting prover" << std::endl; typename scheme_type::proof_type proof = - prove(keypair.first, example.primary_input, example.auxiliary_input); + prove(keypair.first, example.primary_input, example.auxiliary_input); - std::cout << std::hex << "Obtained proof: " << proof.g_A.to_affine().X.data << " " << proof.g_A.to_affine().Y.data << " " << proof.g_A.to_affine().Z.data << std::endl - << proof.g_B.to_affine().X.data[0].data << " " << proof.g_B.to_affine().X.data[1].data << " " << proof.g_B.to_affine().Y.data[0].data << std::endl - << proof.g_B.to_affine().Y.data[1].data << " " << proof.g_B.to_affine().Z.data[0].data << " " << proof.g_B.to_affine().Z.data[1].data << std::endl - << proof.g_C.to_affine().X.data << " " << proof.g_C.to_affine().Y.data << " " << proof.g_C.to_affine().Z.data << std::endl; + std::cout << std::hex << "Obtained proof: " << proof.g_A.to_affine().X.data << " " + << proof.g_A.to_affine().Y.data << " " << proof.g_A.to_affine().Z.data << std::endl + << proof.g_B.to_affine().X.data[0].data << " " << proof.g_B.to_affine().X.data[1].data + << " " << proof.g_B.to_affine().Y.data[0].data << std::endl + << proof.g_B.to_affine().Y.data[1].data << " " << proof.g_B.to_affine().Z.data[0].data + << " " << proof.g_B.to_affine().Z.data[1].data << std::endl + << proof.g_C.to_affine().X.data << " " << proof.g_C.to_affine().Y.data << " " + << proof.g_C.to_affine().Z.data << std::endl; std::cout << std::hex << "Obtained proving key: " << std::endl; print_proving_key(keypair.first); @@ -118,21 +134,21 @@ namespace nil { std::cout << std::hex << "Obtained primary input: " << std::endl; - for (auto it = example.primary_input.begin(); it != example.primary_input.end(); it++){ - std::cout << std::hex << it->data << " " ; + for (auto it = example.primary_input.begin(); it != example.primary_input.end(); it++) { + std::cout << std::hex << it->data << " "; } std::cout << std::endl; std::vector proving_key_byteblob = nil::marshalling::verifier_input_serializer_tvm::process( - keypair.first); + keypair.first); marshalling::status_type provingProcessingStatus = marshalling::status_type::success; - typename scheme_type::proving_key_type other = - nil::marshalling::verifier_input_deserializer_tvm::proving_key_process( - proving_key_byteblob.cbegin(), - proving_key_byteblob.cend(), - provingProcessingStatus); + typename scheme_type::proving_key_type other = + nil::marshalling::verifier_input_deserializer_tvm::proving_key_process( + proving_key_byteblob.cbegin(), + proving_key_byteblob.cend(), + provingProcessingStatus); std::cout << "Decoded proving key:" << std::endl; print_proving_key(other); @@ -144,56 +160,62 @@ namespace nil { BOOST_CHECK(keypair.first.B_query == other.B_query && keypair.first.H_query == other.H_query); BOOST_CHECK(keypair.first.L_query == other.L_query); BOOST_CHECK(keypair.first.constraint_system == other.constraint_system); - BOOST_CHECK(keypair.first.constraint_system.primary_input_size == other.constraint_system.primary_input_size); - BOOST_CHECK(keypair.first.constraint_system.auxiliary_input_size == other.constraint_system.auxiliary_input_size); - BOOST_CHECK(keypair.first.constraint_system.constraints.size() == other.constraint_system.constraints.size()); - - for (std::size_t i = 0; i < keypair.first.constraint_system.constraints.size(); i++){ + BOOST_CHECK(keypair.first.constraint_system.primary_input_size == + other.constraint_system.primary_input_size); + BOOST_CHECK(keypair.first.constraint_system.auxiliary_input_size == + other.constraint_system.auxiliary_input_size); + BOOST_CHECK(keypair.first.constraint_system.constraints.size() == + other.constraint_system.constraints.size()); + + for (std::size_t i = 0; i < keypair.first.constraint_system.constraints.size(); i++) { std::cout << std::endl << "i:" << i << std::endl; - BOOST_CHECK(keypair.first.constraint_system.constraints[i] == other.constraint_system.constraints[i]); + BOOST_CHECK(keypair.first.constraint_system.constraints[i] == + other.constraint_system.constraints[i]); } std::vector verification_key_byteblob = nil::marshalling::verifier_input_serializer_tvm::process( - keypair.second); + keypair.second); std::vector primary_input_byteblob = nil::marshalling::verifier_input_serializer_tvm::process( - example.primary_input); + example.primary_input); std::vector proof_byteblob = nil::marshalling::verifier_input_serializer_tvm::process( - proof); + proof); - std::cout << "Verification key byteblob, size " << std::dec << verification_key_byteblob.size() << std::endl; + std::cout << "Verification key byteblob, size " << std::dec << verification_key_byteblob.size() + << std::endl; - for (auto it = verification_key_byteblob.begin(); it != verification_key_byteblob.end(); ++it){ - std::cout << std::hex << std::size_t(*it) << " " ; + for (auto it = verification_key_byteblob.begin(); it != verification_key_byteblob.end(); ++it) { + std::cout << std::hex << std::size_t(*it) << " "; } std::cout << std::endl; - std::cout << "Primary input byteblob, size " << std::dec << primary_input_byteblob.size() << std::endl; + std::cout << "Primary input byteblob, size " << std::dec << primary_input_byteblob.size() + << std::endl; - for (auto it = primary_input_byteblob.begin(); it != primary_input_byteblob.end(); ++it){ - std::cout << std::hex << std::size_t(*it) << " " ; + for (auto it = primary_input_byteblob.begin(); it != primary_input_byteblob.end(); ++it) { + std::cout << std::hex << std::size_t(*it) << " "; } std::cout << std::endl; std::cout << "Proof byteblob, size " << std::dec << proof_byteblob.size() << std::endl; - for (auto it = proof_byteblob.begin(); it != proof_byteblob.end(); ++it){ - std::cout << std::hex << std::size_t(*it) << " " ; + for (auto it = proof_byteblob.begin(); it != proof_byteblob.end(); ++it) { + std::cout << std::hex << std::size_t(*it) << " "; } std::cout << std::endl; std::vector byteblob; - byteblob.insert (byteblob.end(), proof_byteblob.begin(), proof_byteblob.end()); - byteblob.insert (byteblob.end(), primary_input_byteblob.begin(), primary_input_byteblob.end()); - byteblob.insert (byteblob.end(), verification_key_byteblob.begin(), verification_key_byteblob.end()); + byteblob.insert(byteblob.end(), proof_byteblob.begin(), proof_byteblob.end()); + byteblob.insert(byteblob.end(), primary_input_byteblob.begin(), primary_input_byteblob.end()); + byteblob.insert(byteblob.end(), verification_key_byteblob.begin(), verification_key_byteblob.end()); std::cout << "Data converted to byte blob" << std::endl; - for (auto it = byteblob.begin(); it != byteblob.end(); ++it){ - std::cout << std::hex << std::size_t(*it) << " " ; + for (auto it = byteblob.begin(); it != byteblob.end(); ++it) { + std::cout << std::hex << std::size_t(*it) << " "; } std::cout << std::endl; @@ -205,12 +227,13 @@ namespace nil { std::cout << "Verifier with plain input finished, result: " << ans << std::endl; marshalling::status_type processingStatus = marshalling::status_type::success; - - auto tup = nil::marshalling::verifier_input_deserializer_tvm::verifier_input_process(byteblob.cbegin(), + + auto tup = nil::marshalling::verifier_input_deserializer_tvm::verifier_input_process( + byteblob.cbegin(), byteblob.cend(), processingStatus); - if (processingStatus != marshalling::status_type::success){ + if (processingStatus != marshalling::status_type::success) { std::cout << "Incorrect datablob!" << std::endl; return false; @@ -226,28 +249,40 @@ namespace nil { // typename scheme_type::primary_input_type de_pi = nil::marshalling::verifier_input_deserializer_tvm::primary_input_process(primary_input_byteblob.cbegin(), primary_input_byteblob.cend()); // typename scheme_type::verification_key_type de_vk = nil::marshalling::verifier_input_deserializer_tvm::verification_key_process(verification_key_byteblob.cbegin(), verification_key_byteblob.cend()); - std::cout << std::hex << "Decoded proof: " << de_prf.g_A.to_affine().X.data << " " << de_prf.g_A.to_affine().Y.data << " " << de_prf.g_A.to_affine().Z.data << std::endl - << de_prf.g_B.to_affine().X.data[0].data << " " << de_prf.g_B.to_affine().X.data[1].data << " " << de_prf.g_B.to_affine().Y.data[0].data << std::endl - << de_prf.g_B.to_affine().Y.data[1].data << " " << de_prf.g_B.to_affine().Z.data[0].data << " " << de_prf.g_B.to_affine().Z.data[1].data << std::endl - << de_prf.g_C.to_affine().X.data << " " << de_prf.g_C.to_affine().Y.data << " " << de_prf.g_C.to_affine().Z.data << std::endl; + std::cout << std::hex << "Decoded proof: " << de_prf.g_A.to_affine().X.data << " " + << de_prf.g_A.to_affine().Y.data << " " << de_prf.g_A.to_affine().Z.data << std::endl + << de_prf.g_B.to_affine().X.data[0].data << " " << de_prf.g_B.to_affine().X.data[1].data + << " " << de_prf.g_B.to_affine().Y.data[0].data << std::endl + << de_prf.g_B.to_affine().Y.data[1].data << " " << de_prf.g_B.to_affine().Z.data[0].data + << " " << de_prf.g_B.to_affine().Z.data[1].data << std::endl + << de_prf.g_C.to_affine().X.data << " " << de_prf.g_C.to_affine().Y.data << " " + << de_prf.g_C.to_affine().Z.data << std::endl; assert (de_prf == proof); std::cout << std::hex << "Decoded primary input: " << std::endl; - for (auto it = de_pi.begin(); it != de_pi.end(); it++){ - std::cout << std::hex << it->data << " " ; + for (auto it = de_pi.begin(); it != de_pi.end(); it++) { + std::cout << std::hex << it->data << " "; } std::cout << std::endl; // assert (de_pi == example.primary_input); - std::cout << std::hex << "Decoded verification key: " << "gamma_g2: " - << de_vk.gamma_g2.to_affine().X.data[0].data << " " << de_vk.gamma_g2.to_affine().Y.data[0].data << " " << de_vk.gamma_g2.to_affine().Z.data[0].data << std::endl - << de_vk.gamma_g2.to_affine().X.data[1].data << " " << de_vk.gamma_g2.to_affine().Y.data[1].data << " " << de_vk.gamma_g2.to_affine().Z.data[1].data << std::endl - << "delta_g2: " - << de_vk.delta_g2.to_affine().X.data[0].data << " " << de_vk.delta_g2.to_affine().Y.data[0].data << " " << de_vk.delta_g2.to_affine().Z.data[0].data << std::endl - << de_vk.delta_g2.to_affine().X.data[1].data << " " << de_vk.delta_g2.to_affine().Y.data[1].data << " " << de_vk.delta_g2.to_affine().Z.data[1].data << std::endl; + std::cout << std::hex << "Decoded verification key: " << "gamma_g2: " + << de_vk.gamma_g2.to_affine().X.data[0].data << " " + << de_vk.gamma_g2.to_affine().Y.data[0].data << " " + << de_vk.gamma_g2.to_affine().Z.data[0].data << std::endl + << de_vk.gamma_g2.to_affine().X.data[1].data << " " + << de_vk.gamma_g2.to_affine().Y.data[1].data << " " + << de_vk.gamma_g2.to_affine().Z.data[1].data << std::endl + << "delta_g2: " + << de_vk.delta_g2.to_affine().X.data[0].data << " " + << de_vk.delta_g2.to_affine().Y.data[0].data << " " + << de_vk.delta_g2.to_affine().Z.data[0].data << std::endl + << de_vk.delta_g2.to_affine().X.data[1].data << " " + << de_vk.delta_g2.to_affine().Y.data[1].data << " " + << de_vk.delta_g2.to_affine().Z.data[1].data << std::endl; assert (de_vk == keypair.second); @@ -256,7 +291,7 @@ namespace nil { ans = verify(de_vk, de_pi, de_prf); std::cout << "Verifier with decoded input finished, result: " << ans << std::endl; - + return ans; } } // namespace snark diff --git a/libs/parallel-zk/test/transcript/kimchi_transcript.cpp b/libs/parallel-zk/test/transcript/kimchi_transcript.cpp index 96ea9c65..8295b544 100644 --- a/libs/parallel-zk/test/transcript/kimchi_transcript.cpp +++ b/libs/parallel-zk/test/transcript/kimchi_transcript.cpp @@ -61,359 +61,425 @@ using fr_sponge_type = zk::transcript::DefaultFrSponge; BOOST_AUTO_TEST_SUITE(zk_sponge_test_suite) -BOOST_AUTO_TEST_CASE(zk_sponge_test_0) { - fq_sponge_type spng; - group_type::value_type g[15]; - - g[0] = group_type::value_type(0x1CF10D1482EB88632AEFED15C16082007B38DDC528626195CF6B040E2C7D5914_cppui_modular256, - 0x15A406A92FA16DB6E24D125C8EC5365D76DD8BB188106C0063BA9EC51E0FB8E7_cppui_modular256); - g[1] = group_type::value_type(0x3B38AC47170B2DB158AE7C02E939B2877139040D240171F6A6BB01183902566E_cppui_modular256, - 0x05AAC7FD92471BBFF23D5E4F9AD0B64783467A4809940FEBB7BD6C91A9E9E1C0_cppui_modular256); - g[2] = group_type::value_type(0x281BD2B891CF0795B1439B3AB149ED2A535B8E08C4430112D7D4BF53F3789BEF_cppui_modular256, - 0x10B2FA452CAC5D11CC8040D5DD504222A2621FC378EFD7D08A01BAB3A3DE28DF_cppui_modular256); - g[3] = group_type::value_type(0x0158FEA0E6586A75F36FB621E9C9FC7A38970812F0F1753D3BB716655E3B9D79_cppui_modular256, - 0x2A9688F370DCC43130D38AB7AD2B3FF2A925791F587B55AD138B1F067E874C59_cppui_modular256); - g[4] = group_type::value_type(0x0CA7898337AB528838EAD23D7CBCD4861F1E5E2E5D3B1BD3B733A832C7931547_cppui_modular256, - 0x351C82EC1D20E977ABFC632BBA2330AF61270A00BC2D32B6F2E1DA93AA0D51F1_cppui_modular256); - g[5] = group_type::value_type(0x00DCE7DC20642A850002731F9B3820327CF5856B1D8C3B0EE6BD7BC03BC85FFD_cppui_modular256, - 0x3B1BCBA06B0D33F08123EDD6DF725CC1F8CD2213EA867FF4020C2D18619BB2DB_cppui_modular256); - g[6] = group_type::value_type(0x0F7C2FF92D8F0776629F87BBF25702CEAA45B1893617F7C9AC10AACB080B6E10_cppui_modular256, - 0x16E7207D6596C7FAFF46FB335E14DC57E08E150AB7F692607F3B8DCC9E6CDA93_cppui_modular256); - g[7] = group_type::value_type(0x2CD748E8C8806196ABE34DF032864491CADCF205AF70CB9152507BD16B912BEC_cppui_modular256, - 0x2219EC3C1873373A6717E7BFA24827AD89BF949B0F240D7B9D8981C2006E400F_cppui_modular256); - g[8] = group_type::value_type(0x027E878BD478FC5DE36CA783CB60297C5F75CB638C71615A04714C52E9B15E8E_cppui_modular256, - 0x2CCE580022C7D44E72BA8E7E608C3733A3F3EDC0304566097C07D6CCA172A1B4_cppui_modular256); - g[9] = group_type::value_type(0x0DC7C8FE3A9007F09283D29C5BE99AACEB9DA6996CD691BBAC5D075BDD6DA223_cppui_modular256, - 0x1FA4B95451090B8A36D503BFDBF086D4462745626B4BA4490AF42A7A6B5FD449_cppui_modular256); - g[10] = group_type::value_type(0x20254A64C61A3C1882EC3E9FCA0ABAE814B0EB0477C3396E562C1006054347F3_cppui_modular256, - 0x23CDCBDE9DCBD33AD86BF48181B1616FC76D24A18711A3953D184E772D936418_cppui_modular256); - g[11] = group_type::value_type(0x00DB22BCFC9A1D1A10A53716A7E7D4022DBF101B8767B68E78837CB8263BE097_cppui_modular256, - 0x3E283D2F0D90CAC87B3FCD95E7A8933FB2B2B43EF07FA577CA566527481AB6C9_cppui_modular256); - g[12] = group_type::value_type(0x0D24814B6FE1C8C42FC05834B95212E473B76C8B9588D1272BFAE8FA0E2B9384_cppui_modular256, - 0x11C75275709440AC01B74C4E64E2606F7826294F868F6B0265008E758C148369_cppui_modular256); - g[13] = group_type::value_type(0x007997CB753B919B586243FCAF6E5886676F180C2220BAC055AE9739CA4A1B4B_cppui_modular256, - 0x166859AE2ECE3520D33C2D146F6DBCFC819779C288E9D81C3F7369DF5642EF31_cppui_modular256); - g[14] = group_type::value_type(0x04E774B3DE1A78D6C9408D7B10D9E4614FC8AE4DFE4BFE6762278EE72BB9E25D_cppui_modular256, - 0x178AC19F836752BAF356D9E9C3C35470F27A52C16B7572EEF2C61A43B4D0499B_cppui_modular256); - - for (int i = 0; i < 15; ++i) { - std::vector input({g[i]}); - spng.absorb_g(input); + BOOST_AUTO_TEST_CASE(zk_sponge_test_0) { + fq_sponge_type spng; + group_type::value_type g[15]; + + g[0] = group_type::value_type( + 0x1CF10D1482EB88632AEFED15C16082007B38DDC528626195CF6B040E2C7D5914_cppui_modular256, + 0x15A406A92FA16DB6E24D125C8EC5365D76DD8BB188106C0063BA9EC51E0FB8E7_cppui_modular256); + g[1] = group_type::value_type( + 0x3B38AC47170B2DB158AE7C02E939B2877139040D240171F6A6BB01183902566E_cppui_modular256, + 0x05AAC7FD92471BBFF23D5E4F9AD0B64783467A4809940FEBB7BD6C91A9E9E1C0_cppui_modular256); + g[2] = group_type::value_type( + 0x281BD2B891CF0795B1439B3AB149ED2A535B8E08C4430112D7D4BF53F3789BEF_cppui_modular256, + 0x10B2FA452CAC5D11CC8040D5DD504222A2621FC378EFD7D08A01BAB3A3DE28DF_cppui_modular256); + g[3] = group_type::value_type( + 0x0158FEA0E6586A75F36FB621E9C9FC7A38970812F0F1753D3BB716655E3B9D79_cppui_modular256, + 0x2A9688F370DCC43130D38AB7AD2B3FF2A925791F587B55AD138B1F067E874C59_cppui_modular256); + g[4] = group_type::value_type( + 0x0CA7898337AB528838EAD23D7CBCD4861F1E5E2E5D3B1BD3B733A832C7931547_cppui_modular256, + 0x351C82EC1D20E977ABFC632BBA2330AF61270A00BC2D32B6F2E1DA93AA0D51F1_cppui_modular256); + g[5] = group_type::value_type( + 0x00DCE7DC20642A850002731F9B3820327CF5856B1D8C3B0EE6BD7BC03BC85FFD_cppui_modular256, + 0x3B1BCBA06B0D33F08123EDD6DF725CC1F8CD2213EA867FF4020C2D18619BB2DB_cppui_modular256); + g[6] = group_type::value_type( + 0x0F7C2FF92D8F0776629F87BBF25702CEAA45B1893617F7C9AC10AACB080B6E10_cppui_modular256, + 0x16E7207D6596C7FAFF46FB335E14DC57E08E150AB7F692607F3B8DCC9E6CDA93_cppui_modular256); + g[7] = group_type::value_type( + 0x2CD748E8C8806196ABE34DF032864491CADCF205AF70CB9152507BD16B912BEC_cppui_modular256, + 0x2219EC3C1873373A6717E7BFA24827AD89BF949B0F240D7B9D8981C2006E400F_cppui_modular256); + g[8] = group_type::value_type( + 0x027E878BD478FC5DE36CA783CB60297C5F75CB638C71615A04714C52E9B15E8E_cppui_modular256, + 0x2CCE580022C7D44E72BA8E7E608C3733A3F3EDC0304566097C07D6CCA172A1B4_cppui_modular256); + g[9] = group_type::value_type( + 0x0DC7C8FE3A9007F09283D29C5BE99AACEB9DA6996CD691BBAC5D075BDD6DA223_cppui_modular256, + 0x1FA4B95451090B8A36D503BFDBF086D4462745626B4BA4490AF42A7A6B5FD449_cppui_modular256); + g[10] = group_type::value_type( + 0x20254A64C61A3C1882EC3E9FCA0ABAE814B0EB0477C3396E562C1006054347F3_cppui_modular256, + 0x23CDCBDE9DCBD33AD86BF48181B1616FC76D24A18711A3953D184E772D936418_cppui_modular256); + g[11] = group_type::value_type( + 0x00DB22BCFC9A1D1A10A53716A7E7D4022DBF101B8767B68E78837CB8263BE097_cppui_modular256, + 0x3E283D2F0D90CAC87B3FCD95E7A8933FB2B2B43EF07FA577CA566527481AB6C9_cppui_modular256); + g[12] = group_type::value_type( + 0x0D24814B6FE1C8C42FC05834B95212E473B76C8B9588D1272BFAE8FA0E2B9384_cppui_modular256, + 0x11C75275709440AC01B74C4E64E2606F7826294F868F6B0265008E758C148369_cppui_modular256); + g[13] = group_type::value_type( + 0x007997CB753B919B586243FCAF6E5886676F180C2220BAC055AE9739CA4A1B4B_cppui_modular256, + 0x166859AE2ECE3520D33C2D146F6DBCFC819779C288E9D81C3F7369DF5642EF31_cppui_modular256); + g[14] = group_type::value_type( + 0x04E774B3DE1A78D6C9408D7B10D9E4614FC8AE4DFE4BFE6762278EE72BB9E25D_cppui_modular256, + 0x178AC19F836752BAF356D9E9C3C35470F27A52C16B7572EEF2C61A43B4D0499B_cppui_modular256); + + for (int i = 0; i < 15; ++i) { + std::vector input({g[i]}); + spng.absorb_g(input); + } + + auto check1 = spng.challenge(); + scalar_field_type::value_type chal1 = 0x0000000000000000000000000000000006906F18EE1C02C944C3186D54A8D03E_cppui_modular256; + BOOST_CHECK(check1 == chal1); } - auto check1 = spng.challenge(); - scalar_field_type::value_type chal1 = 0x0000000000000000000000000000000006906F18EE1C02C944C3186D54A8D03E_cppui_modular256; - BOOST_CHECK(check1 == chal1); -} - -BOOST_AUTO_TEST_CASE(zk_sponge_test_1) { - fq_sponge_type spng; - - group_type::value_type g[15]; - g[0] = group_type::value_type(0x0A0C2BD5D6D122644F29A3AD675F1EB7BA01AE9D9EBC323086E3BDED095987D4_cppui_modular256, - 0x273211F773739D39B20CCD4D5EB77479115769B4742F2FB03A4F3ED1A1EC22D4_cppui_modular256); - g[1] = group_type::value_type(0x220593B5A19D0A67847BBF80DB81F49D6CF8F9591B9E8C9A2F32670DDA1D8AC6_cppui_modular256, - 0x07211D7F83661CB78042848839E2DBD04B101F157A7DA31B30A3DB8886E9B7B0_cppui_modular256); - g[2] = group_type::value_type(0x02D77AF331102A224026E24CA18EE6B7B0D5D7709527D066F1FE5FABE4CE85D1_cppui_modular256, - 0x0845660D92C66C0462CF57ADA39E558E0AD9ECC2359F0332901B1D2FE98A10D6_cppui_modular256); - g[3] = group_type::value_type(0x0639954FFC6E0B2CCD1929139BE3105E15252BC2FA0A36024455525E659B7D88_cppui_modular256, - 0x15A3400CC3658E630DDBAA76288D06ADFC22DA21C84575D444D633F8DA6DC932_cppui_modular256); - g[4] = group_type::value_type(0x2CB8A1CA2F4340039B7912FED931EF963E442CE9920FB0A3126E25BD83E598B6_cppui_modular256, - 0x37651EF464571CEF0E7F6DC731365AFDEF8216C6D253DD2BA93A2AFB676C04E2_cppui_modular256); - g[5] = group_type::value_type(0x3A427D5DBECF18AB05809180B1F80E7509BCD963FBA6217E2E9A3292513F2049_cppui_modular256, - 0x1E66FA970D768A59A53D6349E03A4C8D7A93316D08572A133CDE18C2FE64AA03_cppui_modular256); - g[6] = group_type::value_type(0x31525F1FEC6FB330DAB5CEFEC348E7221108CF82F543DA0A8E512F3A390AE5E1_cppui_modular256, - 0x1CFCABDD222DBFD199A208CFCF97FA22627E03B878AE4FD1284AF1CCDFE48384_cppui_modular256); - g[7] = group_type::value_type(0x20BCF66A9DBB51680B5976759A9B9BABC07153E95978C3571B85DAE260326428_cppui_modular256, - 0x0FC9D105713B9FD6CC8587E84D1521BBF3737834FE5384AD76D98051619F7813_cppui_modular256); - g[8] = group_type::value_type(0x31975A5C9BD269C5B7B115205B63A9F82C9E5FAFB789FC0E3A1D9F6D1698FCFF_cppui_modular256, - 0x2B3B885F8AB568D02E0DE2BCADDBD2D9B6EDE04AA3DEC5ABB8B53913F6EE0E35_cppui_modular256); - g[9] = group_type::value_type(0x380C43392B70A7370D69266BFFC5D21825D80F34EB6DD165BF300618E0871AE7_cppui_modular256, - 0x267301AFABFE44E37DD209B0C38D662582F7CF13EE036BD77576767A70D07B88_cppui_modular256); - g[10] = group_type::value_type(0x1458C0D03E1240A352ACDF0B8858993ACF8F4D4EC4091E695C69A1F5CE30D939_cppui_modular256, - 0x1EDB145A8D6C3EDF28A85B0B7CCD77792C06AD3B787394BD0D98B3B453CE634A_cppui_modular256); - g[11] = group_type::value_type(0x20E6522DB98F94CD90E75B74AE0F038C245BD8FACDF94652B4220B139F9A8E36_cppui_modular256, - 0x133B8A97B147D68805619B1579DD49A6B898F713DC63EBCDBD311C1B99F4CED9_cppui_modular256); - g[12] = group_type::value_type(0x00203F56944A4BA0454C150B4922711F88B429B8304DE1D7AFA3AF26BBAB84A5_cppui_modular256, - 0x1371005325482F9B36105AD5C548CB5B16B94B55876F22AE1B396744C76AA548_cppui_modular256); - g[13] = group_type::value_type(0x27E71C246DEB9222E939D9994A010A063231BA76DAE041FB38C0DEF3E5A8E92D_cppui_modular256, - 0x17A6FF821B3E498999B1500CE2C3CDFB1B44691B990585B0522B6548BDDAAA83_cppui_modular256); - g[14] = group_type::value_type(0x0E86C27EF127EFDF0374B82A92D675AF7A0A02EC8A8D0EF2F9BA888427E6CFAB_cppui_modular256, - 0x2A14FA11389648694F68426BDA965E8380C1B155B277467B0C96C5EC73CE17EB_cppui_modular256); - - for (int i = 0; i < 15; ++i) { - std::vector input({g[i]}); - spng.absorb_g(input); + BOOST_AUTO_TEST_CASE(zk_sponge_test_1) { + fq_sponge_type spng; + + group_type::value_type g[15]; + g[0] = group_type::value_type( + 0x0A0C2BD5D6D122644F29A3AD675F1EB7BA01AE9D9EBC323086E3BDED095987D4_cppui_modular256, + 0x273211F773739D39B20CCD4D5EB77479115769B4742F2FB03A4F3ED1A1EC22D4_cppui_modular256); + g[1] = group_type::value_type( + 0x220593B5A19D0A67847BBF80DB81F49D6CF8F9591B9E8C9A2F32670DDA1D8AC6_cppui_modular256, + 0x07211D7F83661CB78042848839E2DBD04B101F157A7DA31B30A3DB8886E9B7B0_cppui_modular256); + g[2] = group_type::value_type( + 0x02D77AF331102A224026E24CA18EE6B7B0D5D7709527D066F1FE5FABE4CE85D1_cppui_modular256, + 0x0845660D92C66C0462CF57ADA39E558E0AD9ECC2359F0332901B1D2FE98A10D6_cppui_modular256); + g[3] = group_type::value_type( + 0x0639954FFC6E0B2CCD1929139BE3105E15252BC2FA0A36024455525E659B7D88_cppui_modular256, + 0x15A3400CC3658E630DDBAA76288D06ADFC22DA21C84575D444D633F8DA6DC932_cppui_modular256); + g[4] = group_type::value_type( + 0x2CB8A1CA2F4340039B7912FED931EF963E442CE9920FB0A3126E25BD83E598B6_cppui_modular256, + 0x37651EF464571CEF0E7F6DC731365AFDEF8216C6D253DD2BA93A2AFB676C04E2_cppui_modular256); + g[5] = group_type::value_type( + 0x3A427D5DBECF18AB05809180B1F80E7509BCD963FBA6217E2E9A3292513F2049_cppui_modular256, + 0x1E66FA970D768A59A53D6349E03A4C8D7A93316D08572A133CDE18C2FE64AA03_cppui_modular256); + g[6] = group_type::value_type( + 0x31525F1FEC6FB330DAB5CEFEC348E7221108CF82F543DA0A8E512F3A390AE5E1_cppui_modular256, + 0x1CFCABDD222DBFD199A208CFCF97FA22627E03B878AE4FD1284AF1CCDFE48384_cppui_modular256); + g[7] = group_type::value_type( + 0x20BCF66A9DBB51680B5976759A9B9BABC07153E95978C3571B85DAE260326428_cppui_modular256, + 0x0FC9D105713B9FD6CC8587E84D1521BBF3737834FE5384AD76D98051619F7813_cppui_modular256); + g[8] = group_type::value_type( + 0x31975A5C9BD269C5B7B115205B63A9F82C9E5FAFB789FC0E3A1D9F6D1698FCFF_cppui_modular256, + 0x2B3B885F8AB568D02E0DE2BCADDBD2D9B6EDE04AA3DEC5ABB8B53913F6EE0E35_cppui_modular256); + g[9] = group_type::value_type( + 0x380C43392B70A7370D69266BFFC5D21825D80F34EB6DD165BF300618E0871AE7_cppui_modular256, + 0x267301AFABFE44E37DD209B0C38D662582F7CF13EE036BD77576767A70D07B88_cppui_modular256); + g[10] = group_type::value_type( + 0x1458C0D03E1240A352ACDF0B8858993ACF8F4D4EC4091E695C69A1F5CE30D939_cppui_modular256, + 0x1EDB145A8D6C3EDF28A85B0B7CCD77792C06AD3B787394BD0D98B3B453CE634A_cppui_modular256); + g[11] = group_type::value_type( + 0x20E6522DB98F94CD90E75B74AE0F038C245BD8FACDF94652B4220B139F9A8E36_cppui_modular256, + 0x133B8A97B147D68805619B1579DD49A6B898F713DC63EBCDBD311C1B99F4CED9_cppui_modular256); + g[12] = group_type::value_type( + 0x00203F56944A4BA0454C150B4922711F88B429B8304DE1D7AFA3AF26BBAB84A5_cppui_modular256, + 0x1371005325482F9B36105AD5C548CB5B16B94B55876F22AE1B396744C76AA548_cppui_modular256); + g[13] = group_type::value_type( + 0x27E71C246DEB9222E939D9994A010A063231BA76DAE041FB38C0DEF3E5A8E92D_cppui_modular256, + 0x17A6FF821B3E498999B1500CE2C3CDFB1B44691B990585B0522B6548BDDAAA83_cppui_modular256); + g[14] = group_type::value_type( + 0x0E86C27EF127EFDF0374B82A92D675AF7A0A02EC8A8D0EF2F9BA888427E6CFAB_cppui_modular256, + 0x2A14FA11389648694F68426BDA965E8380C1B155B277467B0C96C5EC73CE17EB_cppui_modular256); + + for (int i = 0; i < 15; ++i) { + std::vector input({g[i]}); + spng.absorb_g(input); + } + + typename scalar_field_type::value_type chal1( + 0x000000000000000000000000000000006586A4AF99E47C9EA8C7AD23A9E42247_cppui_modular256); + BOOST_CHECK(spng.challenge() == chal1); } - typename scalar_field_type::value_type chal1( - 0x000000000000000000000000000000006586A4AF99E47C9EA8C7AD23A9E42247_cppui_modular256); - BOOST_CHECK(spng.challenge() == chal1); -} - -BOOST_AUTO_TEST_CASE(zk_sponge_test_2) { - fq_sponge_type spng; - - group_type::value_type g[23]; - g[0] = group_type::value_type(0x27CCCDE41398B04DD09FAEEFB7B78767B51BB2AFC6587838D0F5A43C43A4A218_cppui_modular256, - 0x1728B31CFD99AD2D3DA948387D407DC6F61CD630758344996E05C21B89B4FF7E_cppui_modular256); - g[1] = group_type::value_type(0x2AEF45CFCF1C7A3AB0437D11B00089226C1EAD9F65D17B02EBFBEEDD6ED8FFFB_cppui_modular256, - 0x354C1269DA294634A5E6E3C6D85F5068044BE97B9A41E47CFFD096AA3991D61F_cppui_modular256); - g[2] = group_type::value_type(0x01B21F2FBE8FADFD3CF5ADA9332F17B86DE9AF278982CC4E1E5CEDBC6ADFB5C5_cppui_modular256, - 0x11FFAFCFDE4766A966DCB1083C422E8A863BB1871EA5657E79149504BDF4C3F7_cppui_modular256); - g[3] = group_type::value_type(0x10B268CA02F1CCC0A9179B4DC8678E0E5E63B04D8149E8A85515B81529AD04F5_cppui_modular256, - 0x1DD587777C89876DFAF9135F7F58D4A1B37DE6F6B610043C231E23BEAAA84CF6_cppui_modular256); - g[4] = group_type::value_type(0x13C0DC5466D51C6852266A549093DA4F59C1B06B278ABEBB85519F07D75972ED_cppui_modular256, - 0x109EA2EA135BC41544A6892C17E0F4202C6E8EA4E75E0202436104F1D2DD0AEC_cppui_modular256); - g[5] = group_type::value_type(0x30F72723AC67E979D3956C3CDFE0662D0A515C3EA6D257F24BD292DB5E0450F4_cppui_modular256, - 0x1E021A1B6BF8A365E8251A51B430C39F3F1B9E08E3A13B5DCC641094EEA10A0E_cppui_modular256); - g[6] = group_type::value_type(0x04F8423216ED528B6ACB493F9E122F8E88BB173037489185ADED577FB35DD4C8_cppui_modular256, - 0x0568CBE5D3AF7A1DC593E160088A1438B8570A87E1A40AFF548F19AB88246186_cppui_modular256); - g[7] = group_type::value_type(0x3C276D9F50711A0FADC6C9215B7FE55772D8854ED88055B61D624D50A46BF2D1_cppui_modular256, - 0x064EC8440A5C9EE94D2E495DD697031A0FFAB03AD8AF8653B083B024EC2E0947_cppui_modular256); - g[8] = group_type::value_type(0x37F0A0BFF99A231FBEB6FA8BF1BAC5D27D681D96A3BFE439526E8E36DC3F456B_cppui_modular256, - 0x05438AD52E7ED1CADD62053822F42888E4F4027A009541D26BBB8A3277747690_cppui_modular256); - g[9] = group_type::value_type(0x21EF274FA84AD809DDE6858504CB06764F23349E9AD698AB06C7C88AB9938F10_cppui_modular256, - 0x22BB862C8B9C96A349540FF9127984EDEB425FA7727A86A33F0521A36F385567_cppui_modular256); - g[10] = group_type::value_type(0x339487A2AE045A6CE9B13B548CAF8C9580B1216EC279026A9EA12E5685745822_cppui_modular256, - 0x019050F98DE9302849A3C7F024544DD6D73FC5A174D0A1D3D7FED3E8612C0BDF_cppui_modular256); - g[11] = group_type::value_type(0x11FD07718FACABBFA2B0FDB6D559EECE04406643C11AE03228A6A55C0199B78F_cppui_modular256, - 0x1B3F5E6BDB2CFC18947322EB26A6A89E1CE0B3BCB79EE30245FCA702CE174390_cppui_modular256); - g[12] = group_type::value_type(0x3393E80C17EB7DF85CA4FAD7F6043A084773ACD3B5ED712030E4B2000FF5086F_cppui_modular256, - 0x033B8C65937B8EE3B1DC600E4BA1FD9EBD851D29590C16379BEEFB93A6F6226E_cppui_modular256); - g[13] = group_type::value_type(0x15E069E1CFD96C634513360C4BED63D8D22D32947BC156649D447BFA415D9D25_cppui_modular256, - 0x3F183AD1D896978D1AB0568AF4AA91CF413A2E011352B7692E17E5C0157619EF_cppui_modular256); - g[14] = group_type::value_type(0x1D42ED837696F2A777E7C1FF0436D46E96878B624ECDE039732E37AFCD409C88_cppui_modular256, - 0x1DD9078FBA2CE4F2ECE3D8374E805A0494D5F6FF85B7B1A0F255F91C79F08929_cppui_modular256); - g[15] = group_type::value_type(0x0A4020BF547A53FAF4DA99584BBAC1FD5D878D264A99BDF19710748597362B9B_cppui_modular256, - 0x179AFDC1C16BD21205B6B9E487799A032BE077512F18F1DD3215250F0C67FC64_cppui_modular256); - g[16] = group_type::value_type(0x363AA1A805E5BAFF2DB4BDD817E60CCC546DE456367677C5ECD48CAC2675E21F_cppui_modular256, - 0x1638CDF842DB7274F494B0DC06D9CD0B2565666F7C9E4330A21412F216227FEC_cppui_modular256); - g[17] = group_type::value_type(0x39A5B4737045A5192E159C13092B428E3AED966EE0A4DDA365F54AA14D17674E_cppui_modular256, - 0x052FBAB263CAC3E2B7C701B154F8F621A9BE5390D5795A3D7622C57536A30ACB_cppui_modular256); - g[18] = group_type::value_type(0x3A90A56D28DC7E01F9384207FAE64E783C7A628048F155C8D14ECB1CD53C93A8_cppui_modular256, - 0x17A22DB9090348E8C363B687BF96EC25CCA79BA366F28F176097BF5474D8C32C_cppui_modular256); - g[19] = group_type::value_type(0x02790E33CB485684C1D1CB250DBA08DE299C27F4CD340AC03720983FBF9441BE_cppui_modular256, - 0x30022C5A33C30487C1B375625E5317A351C2E5498C27B39FB0DF2DE8E95036FA_cppui_modular256); - g[20] = group_type::value_type(0x1E98D2E550D673BF3B2CEEC098C5319494441382C9427F71557B82187A1F6E72_cppui_modular256, - 0x0D336D94B3CE0D0C30073BDE4C3044F458A598B6B50A1FD1944D29C9F681EB60_cppui_modular256); - g[21] = group_type::value_type(0x0E826DABA538B6DFDFBC0133093600E5FB812F513D0FCC04106CB4BD3F32FAD3_cppui_modular256, - 0x282038D2B42F1EEF395753E663C6C62523F3C22857CFD6BCFF83B1B0F130B320_cppui_modular256); - g[22] = group_type::value_type(0x0557F05F4FE835D81BF63FFA8E35B5C014E2A4828AC3E5AEE216F11F4662D8F3_cppui_modular256, - 0x22083FE3C84501B1C06B8BF9EDC10783523E080B10B41106555D3994B42DE333_cppui_modular256); - - for (int i = 0; i < 15; ++i) { - std::vector input({g[i]}); + BOOST_AUTO_TEST_CASE(zk_sponge_test_2) { + fq_sponge_type spng; + + group_type::value_type g[23]; + g[0] = group_type::value_type( + 0x27CCCDE41398B04DD09FAEEFB7B78767B51BB2AFC6587838D0F5A43C43A4A218_cppui_modular256, + 0x1728B31CFD99AD2D3DA948387D407DC6F61CD630758344996E05C21B89B4FF7E_cppui_modular256); + g[1] = group_type::value_type( + 0x2AEF45CFCF1C7A3AB0437D11B00089226C1EAD9F65D17B02EBFBEEDD6ED8FFFB_cppui_modular256, + 0x354C1269DA294634A5E6E3C6D85F5068044BE97B9A41E47CFFD096AA3991D61F_cppui_modular256); + g[2] = group_type::value_type( + 0x01B21F2FBE8FADFD3CF5ADA9332F17B86DE9AF278982CC4E1E5CEDBC6ADFB5C5_cppui_modular256, + 0x11FFAFCFDE4766A966DCB1083C422E8A863BB1871EA5657E79149504BDF4C3F7_cppui_modular256); + g[3] = group_type::value_type( + 0x10B268CA02F1CCC0A9179B4DC8678E0E5E63B04D8149E8A85515B81529AD04F5_cppui_modular256, + 0x1DD587777C89876DFAF9135F7F58D4A1B37DE6F6B610043C231E23BEAAA84CF6_cppui_modular256); + g[4] = group_type::value_type( + 0x13C0DC5466D51C6852266A549093DA4F59C1B06B278ABEBB85519F07D75972ED_cppui_modular256, + 0x109EA2EA135BC41544A6892C17E0F4202C6E8EA4E75E0202436104F1D2DD0AEC_cppui_modular256); + g[5] = group_type::value_type( + 0x30F72723AC67E979D3956C3CDFE0662D0A515C3EA6D257F24BD292DB5E0450F4_cppui_modular256, + 0x1E021A1B6BF8A365E8251A51B430C39F3F1B9E08E3A13B5DCC641094EEA10A0E_cppui_modular256); + g[6] = group_type::value_type( + 0x04F8423216ED528B6ACB493F9E122F8E88BB173037489185ADED577FB35DD4C8_cppui_modular256, + 0x0568CBE5D3AF7A1DC593E160088A1438B8570A87E1A40AFF548F19AB88246186_cppui_modular256); + g[7] = group_type::value_type( + 0x3C276D9F50711A0FADC6C9215B7FE55772D8854ED88055B61D624D50A46BF2D1_cppui_modular256, + 0x064EC8440A5C9EE94D2E495DD697031A0FFAB03AD8AF8653B083B024EC2E0947_cppui_modular256); + g[8] = group_type::value_type( + 0x37F0A0BFF99A231FBEB6FA8BF1BAC5D27D681D96A3BFE439526E8E36DC3F456B_cppui_modular256, + 0x05438AD52E7ED1CADD62053822F42888E4F4027A009541D26BBB8A3277747690_cppui_modular256); + g[9] = group_type::value_type( + 0x21EF274FA84AD809DDE6858504CB06764F23349E9AD698AB06C7C88AB9938F10_cppui_modular256, + 0x22BB862C8B9C96A349540FF9127984EDEB425FA7727A86A33F0521A36F385567_cppui_modular256); + g[10] = group_type::value_type( + 0x339487A2AE045A6CE9B13B548CAF8C9580B1216EC279026A9EA12E5685745822_cppui_modular256, + 0x019050F98DE9302849A3C7F024544DD6D73FC5A174D0A1D3D7FED3E8612C0BDF_cppui_modular256); + g[11] = group_type::value_type( + 0x11FD07718FACABBFA2B0FDB6D559EECE04406643C11AE03228A6A55C0199B78F_cppui_modular256, + 0x1B3F5E6BDB2CFC18947322EB26A6A89E1CE0B3BCB79EE30245FCA702CE174390_cppui_modular256); + g[12] = group_type::value_type( + 0x3393E80C17EB7DF85CA4FAD7F6043A084773ACD3B5ED712030E4B2000FF5086F_cppui_modular256, + 0x033B8C65937B8EE3B1DC600E4BA1FD9EBD851D29590C16379BEEFB93A6F6226E_cppui_modular256); + g[13] = group_type::value_type( + 0x15E069E1CFD96C634513360C4BED63D8D22D32947BC156649D447BFA415D9D25_cppui_modular256, + 0x3F183AD1D896978D1AB0568AF4AA91CF413A2E011352B7692E17E5C0157619EF_cppui_modular256); + g[14] = group_type::value_type( + 0x1D42ED837696F2A777E7C1FF0436D46E96878B624ECDE039732E37AFCD409C88_cppui_modular256, + 0x1DD9078FBA2CE4F2ECE3D8374E805A0494D5F6FF85B7B1A0F255F91C79F08929_cppui_modular256); + g[15] = group_type::value_type( + 0x0A4020BF547A53FAF4DA99584BBAC1FD5D878D264A99BDF19710748597362B9B_cppui_modular256, + 0x179AFDC1C16BD21205B6B9E487799A032BE077512F18F1DD3215250F0C67FC64_cppui_modular256); + g[16] = group_type::value_type( + 0x363AA1A805E5BAFF2DB4BDD817E60CCC546DE456367677C5ECD48CAC2675E21F_cppui_modular256, + 0x1638CDF842DB7274F494B0DC06D9CD0B2565666F7C9E4330A21412F216227FEC_cppui_modular256); + g[17] = group_type::value_type( + 0x39A5B4737045A5192E159C13092B428E3AED966EE0A4DDA365F54AA14D17674E_cppui_modular256, + 0x052FBAB263CAC3E2B7C701B154F8F621A9BE5390D5795A3D7622C57536A30ACB_cppui_modular256); + g[18] = group_type::value_type( + 0x3A90A56D28DC7E01F9384207FAE64E783C7A628048F155C8D14ECB1CD53C93A8_cppui_modular256, + 0x17A22DB9090348E8C363B687BF96EC25CCA79BA366F28F176097BF5474D8C32C_cppui_modular256); + g[19] = group_type::value_type( + 0x02790E33CB485684C1D1CB250DBA08DE299C27F4CD340AC03720983FBF9441BE_cppui_modular256, + 0x30022C5A33C30487C1B375625E5317A351C2E5498C27B39FB0DF2DE8E95036FA_cppui_modular256); + g[20] = group_type::value_type( + 0x1E98D2E550D673BF3B2CEEC098C5319494441382C9427F71557B82187A1F6E72_cppui_modular256, + 0x0D336D94B3CE0D0C30073BDE4C3044F458A598B6B50A1FD1944D29C9F681EB60_cppui_modular256); + g[21] = group_type::value_type( + 0x0E826DABA538B6DFDFBC0133093600E5FB812F513D0FCC04106CB4BD3F32FAD3_cppui_modular256, + 0x282038D2B42F1EEF395753E663C6C62523F3C22857CFD6BCFF83B1B0F130B320_cppui_modular256); + g[22] = group_type::value_type( + 0x0557F05F4FE835D81BF63FFA8E35B5C014E2A4828AC3E5AEE216F11F4662D8F3_cppui_modular256, + 0x22083FE3C84501B1C06B8BF9EDC10783523E080B10B41106555D3994B42DE333_cppui_modular256); + + for (int i = 0; i < 15; ++i) { + std::vector input({g[i]}); + spng.absorb_g(input); + } + + typename scalar_field_type::value_type chal1( + 0x000000000000000000000000000000005D6E02ED382BBF4A9FF5C2C13A1F0E3D_cppui_modular256); + BOOST_CHECK(spng.challenge() == chal1); + typename scalar_field_type::value_type chal2( + 0x0000000000000000000000000000000058C638E4FE632BB34E9D712D10953688_cppui_modular256); + BOOST_CHECK(spng.challenge() == chal2); + + std::vector input({g[15]}); spng.absorb_g(input); + + typename scalar_field_type::value_type chal3( + 0x0000000000000000000000000000000072D58D72518968134276BCBD15848A54_cppui_modular256); + BOOST_CHECK(spng.challenge() == chal3); + + for (int i = 16; i < 23; ++i) { + std::vector input({g[i]}); + spng.absorb_g(input); + } + typename scalar_field_type::value_type chal4( + 0x00000000000000000000000000000000126C2E2D31FBDDB543E4FE174987EDBC_cppui_modular256); + BOOST_CHECK(spng.challenge() == chal4); } - typename scalar_field_type::value_type chal1( - 0x000000000000000000000000000000005D6E02ED382BBF4A9FF5C2C13A1F0E3D_cppui_modular256); - BOOST_CHECK(spng.challenge() == chal1); - typename scalar_field_type::value_type chal2( - 0x0000000000000000000000000000000058C638E4FE632BB34E9D712D10953688_cppui_modular256); - BOOST_CHECK(spng.challenge() == chal2); + BOOST_AUTO_TEST_CASE(zk_sponge_test_for_absorb_fr) { + fq_sponge_type spng; + // fq_sponge_type2 spng2; + scalar_field_type::value_type g[30] = { + 0x1CF10D1482EB88632AEFED15C16082007B38DDC528626195CF6B040E2C7D5914_cppui_modular256, + 0x15A406A92FA16DB6E24D125C8EC5365D76DD8BB188106C0063BA9EC51E0FB8E7_cppui_modular256, + 0x3B38AC47170B2DB158AE7C02E939B2877139040D240171F6A6BB01183902566E_cppui_modular256, + 0x05AAC7FD92471BBFF23D5E4F9AD0B64783467A4809940FEBB7BD6C91A9E9E1C0_cppui_modular256, + 0x281BD2B891CF0795B1439B3AB149ED2A535B8E08C4430112D7D4BF53F3789BEF_cppui_modular256, + 0x10B2FA452CAC5D11CC8040D5DD504222A2621FC378EFD7D08A01BAB3A3DE28DF_cppui_modular256, + 0x0158FEA0E6586A75F36FB621E9C9FC7A38970812F0F1753D3BB716655E3B9D79_cppui_modular256, + 0x2A9688F370DCC43130D38AB7AD2B3FF2A925791F587B55AD138B1F067E874C59_cppui_modular256, + 0x0CA7898337AB528838EAD23D7CBCD4861F1E5E2E5D3B1BD3B733A832C7931547_cppui_modular256, + 0x351C82EC1D20E977ABFC632BBA2330AF61270A00BC2D32B6F2E1DA93AA0D51F1_cppui_modular256, + 0x00DCE7DC20642A850002731F9B3820327CF5856B1D8C3B0EE6BD7BC03BC85FFD_cppui_modular256, + 0x3B1BCBA06B0D33F08123EDD6DF725CC1F8CD2213EA867FF4020C2D18619BB2DB_cppui_modular256, + 0x0F7C2FF92D8F0776629F87BBF25702CEAA45B1893617F7C9AC10AACB080B6E10_cppui_modular256, + 0x16E7207D6596C7FAFF46FB335E14DC57E08E150AB7F692607F3B8DCC9E6CDA93_cppui_modular256, + 0x2CD748E8C8806196ABE34DF032864491CADCF205AF70CB9152507BD16B912BEC_cppui_modular256, + 0x2219EC3C1873373A6717E7BFA24827AD89BF949B0F240D7B9D8981C2006E400F_cppui_modular256, + 0x027E878BD478FC5DE36CA783CB60297C5F75CB638C71615A04714C52E9B15E8E_cppui_modular256, + 0x2CCE580022C7D44E72BA8E7E608C3733A3F3EDC0304566097C07D6CCA172A1B4_cppui_modular256, + 0x0DC7C8FE3A9007F09283D29C5BE99AACEB9DA6996CD691BBAC5D075BDD6DA223_cppui_modular256, + 0x1FA4B95451090B8A36D503BFDBF086D4462745626B4BA4490AF42A7A6B5FD449_cppui_modular256, + 0x20254A64C61A3C1882EC3E9FCA0ABAE814B0EB0477C3396E562C1006054347F3_cppui_modular256, + 0x23CDCBDE9DCBD33AD86BF48181B1616FC76D24A18711A3953D184E772D936418_cppui_modular256, + 0x00DB22BCFC9A1D1A10A53716A7E7D4022DBF101B8767B68E78837CB8263BE097_cppui_modular256, + 0x3E283D2F0D90CAC87B3FCD95E7A8933FB2B2B43EF07FA577CA566527481AB6C9_cppui_modular256, + 0x0D24814B6FE1C8C42FC05834B95212E473B76C8B9588D1272BFAE8FA0E2B9384_cppui_modular256, + 0x11C75275709440AC01B74C4E64E2606F7826294F868F6B0265008E758C148369_cppui_modular256, + 0x007997CB753B919B586243FCAF6E5886676F180C2220BAC055AE9739CA4A1B4B_cppui_modular256, + 0x166859AE2ECE3520D33C2D146F6DBCFC819779C288E9D81C3F7369DF5642EF31_cppui_modular256, + 0x04E774B3DE1A78D6C9408D7B10D9E4614FC8AE4DFE4BFE6762278EE72BB9E25D_cppui_modular256, + 0x178AC19F836752BAF356D9E9C3C35470F27A52C16B7572EEF2C61A43B4D0499B_cppui_modular256}; + + for (int i = 0; i < 30; ++i) { + std::vector input({g[i]}); + spng.absorb_fr(input); + } + + auto check1 = spng.challenge(); + scalar_field_type::value_type chal1 = 0x0000000000000000000000000000000006906F18EE1C02C944C3186D54A8D03E_cppui_modular256; + BOOST_CHECK(check1 == chal1); + } - std::vector input({g[15]}); - spng.absorb_g(input); + BOOST_AUTO_TEST_CASE(zk_sponge_test_for_absorb_fr_2) { + using namespace nil::crypto3; + using curve_type = algebra::curves::pallas; + using group_type = typename curve_type::template g1_type<>; + using scalar_field_type = typename curve_type::scalar_field_type; + using base_field_type = typename curve_type::base_field_type; - typename scalar_field_type::value_type chal3( - 0x0000000000000000000000000000000072D58D72518968134276BCBD15848A54_cppui_modular256); - BOOST_CHECK(spng.challenge() == chal3); + using fq_sponge_type = zk::transcript::DefaultFqSponge; - for (int i = 16; i < 23; ++i) { - std::vector input({g[i]}); - spng.absorb_g(input); + std::vector input_values = { + 0x3AA52C0B2BC507CEC6CEEDBFD2C02B9C74CFA1043847011BA789D6F871201A52_cppui_modular256}; + fq_sponge_type spng; + spng.absorb_fr(input_values); + base_field_type::value_type real_value = spng.challenge_fq(); + + algebra::fields::detail::element_fp> real_inputs( + 0x1D52960595E283E7636776DFE96015CE3A67D0821C23808DD3C4EB7C38900D29_cppui_modular256); + fq_sponge_type spng_real; + spng_real.sponge.absorb(real_inputs); + base_field_type::value_type expected_value = spng_real.challenge_fq(); + + BOOST_CHECK(real_value == expected_value); } - typename scalar_field_type::value_type chal4( - 0x00000000000000000000000000000000126C2E2D31FBDDB543E4FE174987EDBC_cppui_modular256); - BOOST_CHECK(spng.challenge() == chal4); -} - -BOOST_AUTO_TEST_CASE(zk_sponge_test_for_absorb_fr) { - fq_sponge_type spng; - // fq_sponge_type2 spng2; - scalar_field_type::value_type g[30] = {0x1CF10D1482EB88632AEFED15C16082007B38DDC528626195CF6B040E2C7D5914_cppui_modular256, - 0x15A406A92FA16DB6E24D125C8EC5365D76DD8BB188106C0063BA9EC51E0FB8E7_cppui_modular256, - 0x3B38AC47170B2DB158AE7C02E939B2877139040D240171F6A6BB01183902566E_cppui_modular256, - 0x05AAC7FD92471BBFF23D5E4F9AD0B64783467A4809940FEBB7BD6C91A9E9E1C0_cppui_modular256, - 0x281BD2B891CF0795B1439B3AB149ED2A535B8E08C4430112D7D4BF53F3789BEF_cppui_modular256, - 0x10B2FA452CAC5D11CC8040D5DD504222A2621FC378EFD7D08A01BAB3A3DE28DF_cppui_modular256, - 0x0158FEA0E6586A75F36FB621E9C9FC7A38970812F0F1753D3BB716655E3B9D79_cppui_modular256, - 0x2A9688F370DCC43130D38AB7AD2B3FF2A925791F587B55AD138B1F067E874C59_cppui_modular256, - 0x0CA7898337AB528838EAD23D7CBCD4861F1E5E2E5D3B1BD3B733A832C7931547_cppui_modular256, - 0x351C82EC1D20E977ABFC632BBA2330AF61270A00BC2D32B6F2E1DA93AA0D51F1_cppui_modular256, - 0x00DCE7DC20642A850002731F9B3820327CF5856B1D8C3B0EE6BD7BC03BC85FFD_cppui_modular256, - 0x3B1BCBA06B0D33F08123EDD6DF725CC1F8CD2213EA867FF4020C2D18619BB2DB_cppui_modular256, - 0x0F7C2FF92D8F0776629F87BBF25702CEAA45B1893617F7C9AC10AACB080B6E10_cppui_modular256, - 0x16E7207D6596C7FAFF46FB335E14DC57E08E150AB7F692607F3B8DCC9E6CDA93_cppui_modular256, - 0x2CD748E8C8806196ABE34DF032864491CADCF205AF70CB9152507BD16B912BEC_cppui_modular256, - 0x2219EC3C1873373A6717E7BFA24827AD89BF949B0F240D7B9D8981C2006E400F_cppui_modular256, - 0x027E878BD478FC5DE36CA783CB60297C5F75CB638C71615A04714C52E9B15E8E_cppui_modular256, - 0x2CCE580022C7D44E72BA8E7E608C3733A3F3EDC0304566097C07D6CCA172A1B4_cppui_modular256, - 0x0DC7C8FE3A9007F09283D29C5BE99AACEB9DA6996CD691BBAC5D075BDD6DA223_cppui_modular256, - 0x1FA4B95451090B8A36D503BFDBF086D4462745626B4BA4490AF42A7A6B5FD449_cppui_modular256, - 0x20254A64C61A3C1882EC3E9FCA0ABAE814B0EB0477C3396E562C1006054347F3_cppui_modular256, - 0x23CDCBDE9DCBD33AD86BF48181B1616FC76D24A18711A3953D184E772D936418_cppui_modular256, - 0x00DB22BCFC9A1D1A10A53716A7E7D4022DBF101B8767B68E78837CB8263BE097_cppui_modular256, - 0x3E283D2F0D90CAC87B3FCD95E7A8933FB2B2B43EF07FA577CA566527481AB6C9_cppui_modular256, - 0x0D24814B6FE1C8C42FC05834B95212E473B76C8B9588D1272BFAE8FA0E2B9384_cppui_modular256, - 0x11C75275709440AC01B74C4E64E2606F7826294F868F6B0265008E758C148369_cppui_modular256, - 0x007997CB753B919B586243FCAF6E5886676F180C2220BAC055AE9739CA4A1B4B_cppui_modular256, - 0x166859AE2ECE3520D33C2D146F6DBCFC819779C288E9D81C3F7369DF5642EF31_cppui_modular256, - 0x04E774B3DE1A78D6C9408D7B10D9E4614FC8AE4DFE4BFE6762278EE72BB9E25D_cppui_modular256, - 0x178AC19F836752BAF356D9E9C3C35470F27A52C16B7572EEF2C61A43B4D0499B_cppui_modular256}; - - for (int i = 0; i < 30; ++i) { - std::vector input({g[i]}); - spng.absorb_fr(input); + + BOOST_AUTO_TEST_CASE(zk_sponge_test_real_case) { + fq_sponge_type spng; + spng.absorb_fr(value_type(0x1B76B0452DBEE0301162D6D04350DDC0361222FEF7467C285DB383D51E043D83_cppui_modular256)); + BOOST_CHECK( + spng.challenge_fq() == + base_field_type::value_type( + 0x23A5199486C064AC4CB9D8BBD59B20EB2A2B1A3CA77DFA6E9DAB7C387D270E23_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x1757CFBC6F79F5DA18CAD5BFE889D8BB11A04BEFD2F5F4ECA71CDF1541FD6A10_cppui_modular256, + 0x3440D97DA37051ACEA71310B6A9519E8989E86DE57D324745616A3BA065F2272_cppui_modular256)); + spng.absorb_g(group_type::value_type( + 0x2D47BB4464D0A3788F10C5D70FC35BF750246155649C6B6690F657D372CCE6FF_cppui_modular256, + 0x1A86D626C558F0BC02FA5F89A591DD8392DA153EB457611BA1B3A40AE3E68BD8_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x000000000000000000000000000000005A694EDCBC5D63D83F6E14016563BD69_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x3FCEA348F31BE8357433DBC714AAB303EE1477D6BCFFEC816D45199D004EA0EB_cppui_modular256, + 0x37851472B67A5A35A8F54A001659CB995C7C501CEFF4BE5448D7250508A14FBC_cppui_modular256)); + spng.absorb_g(group_type::value_type( + 0x32ACB0BD9286BDE74B949BEFA7224706890479EF2B3AE5BD11B977910D1B7478_cppui_modular256, + 0x2FB122CE2BFC57B5FF1D2CDB673D3328B6770C065E8253E90980A283AF8EBB15_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x0000000000000000000000000000000063597BF8593B53D1BEA983CAE5AA0140_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x1012AEEE4AB904D4A3B47AACDD04BD8D119B3A8015F76DF7F24AD13B7C06BB90_cppui_modular256, + 0x03428FD045BAA622B9F7EEB54E8321D96EE1CC04CEFB26F1AEB3D6D40A4DED20_cppui_modular256)); + spng.absorb_g(group_type::value_type( + 0x184F8EA7DFF1970F52E5A75BC74D8F0FAF76BEB2C56A42D44CB96CCCA895870A_cppui_modular256, + 0x110904EE3F6325E6D3D591FC6D1EAE61D43ACDB54357A7F76AD4FD6DFA59FCB8_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x0000000000000000000000000000000027ABB6B27E12348F52A181F17FE29F41_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x22F5F9CD40DE3BA2268208BAC69D839A0EFF28C445FFC36C21EA64FCC62A7FC5_cppui_modular256, + 0x35FF2C0CAB2901382134A1862322C212A143237419EE758AE4CB6B02FF1BA141_cppui_modular256)); + spng.absorb_g(group_type::value_type( + 0x1E0518490D0DD242420C8786B744E444FF09E2ECB44217B3FC2323BF739BE8A0_cppui_modular256, + 0x00A9F106114217ECA4313604AA123351DEC9EE0B6C42624270CA9390D34DD0F1_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x000000000000000000000000000000000B79378485D93E3A44F4E3EAEF25D3DA_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x14309CF280C4C82C856B17A808A2E5A583DA7EEB1C385F3B4EB12039EA76AE83_cppui_modular256, + 0x241285C939BD25A6CA34D6F347596BD110FE55AF173A0EBBFB659630A4C96B99_cppui_modular256)); + spng.absorb_g(group_type::value_type( + 0x09DA297567A8850406E640FF07BC76CCB9A4FA5C11F328EF03708414CBC1F4B9_cppui_modular256, + 0x3BE674505BBBD0F9ED806218258E029084BAECD51ABE6F9264E80C1899307ED6_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x000000000000000000000000000000003CBCF6C44411CE451A33BAB179026502_cppui_modular256)); + + spng.absorb_g(group_type::value_type( + 0x1DA94235A1E998434A93578409C4EB18BD82A01EC5A25E5D7C7C7C5E001F18FB_cppui_modular256, + 0x04A566923D712C1CEBF2DBE553DD185F8FDACF604E50948925264D3A49C037EA_cppui_modular256)); + BOOST_CHECK(spng.challenge() == + value_type(0x0000000000000000000000000000000072FF5A26FAF972660330A5D0CC5C4700_cppui_modular256)); } - auto check1 = spng.challenge(); - scalar_field_type::value_type chal1 = 0x0000000000000000000000000000000006906F18EE1C02C944C3186D54A8D03E_cppui_modular256; - BOOST_CHECK(check1 == chal1); -} - -BOOST_AUTO_TEST_CASE(zk_sponge_test_for_absorb_fr_2) { - using namespace nil::crypto3; - using curve_type = algebra::curves::pallas; - using group_type = typename curve_type::template g1_type<>; - using scalar_field_type = typename curve_type::scalar_field_type; - using base_field_type = typename curve_type::base_field_type; - - using fq_sponge_type = zk::transcript::DefaultFqSponge; - - std::vector input_values = { - 0x3AA52C0B2BC507CEC6CEEDBFD2C02B9C74CFA1043847011BA789D6F871201A52_cppui_modular256}; - fq_sponge_type spng; - spng.absorb_fr(input_values); - base_field_type::value_type real_value = spng.challenge_fq(); - - std::vector real_inputs = { - 0x1D52960595E283E7636776DFE96015CE3A67D0821C23808DD3C4EB7C38900D29_cppui_modular256, 0x0_cppui_modular256}; - fq_sponge_type spng_real; - spng_real.sponge.absorb(real_inputs); - base_field_type::value_type expected_value = spng_real.challenge_fq(); - - BOOST_CHECK(real_value == expected_value); -} - -BOOST_AUTO_TEST_CASE(zk_sponge_test_real_case) { - fq_sponge_type spng; - spng.absorb_fr(value_type(0x1B76B0452DBEE0301162D6D04350DDC0361222FEF7467C285DB383D51E043D83_cppui_modular256)); - BOOST_CHECK( - spng.challenge_fq() == - base_field_type::value_type(0x23A5199486C064AC4CB9D8BBD59B20EB2A2B1A3CA77DFA6E9DAB7C387D270E23_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x1757CFBC6F79F5DA18CAD5BFE889D8BB11A04BEFD2F5F4ECA71CDF1541FD6A10_cppui_modular256, - 0x3440D97DA37051ACEA71310B6A9519E8989E86DE57D324745616A3BA065F2272_cppui_modular256)); - spng.absorb_g(group_type::value_type(0x2D47BB4464D0A3788F10C5D70FC35BF750246155649C6B6690F657D372CCE6FF_cppui_modular256, - 0x1A86D626C558F0BC02FA5F89A591DD8392DA153EB457611BA1B3A40AE3E68BD8_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x000000000000000000000000000000005A694EDCBC5D63D83F6E14016563BD69_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x3FCEA348F31BE8357433DBC714AAB303EE1477D6BCFFEC816D45199D004EA0EB_cppui_modular256, - 0x37851472B67A5A35A8F54A001659CB995C7C501CEFF4BE5448D7250508A14FBC_cppui_modular256)); - spng.absorb_g(group_type::value_type(0x32ACB0BD9286BDE74B949BEFA7224706890479EF2B3AE5BD11B977910D1B7478_cppui_modular256, - 0x2FB122CE2BFC57B5FF1D2CDB673D3328B6770C065E8253E90980A283AF8EBB15_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x0000000000000000000000000000000063597BF8593B53D1BEA983CAE5AA0140_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x1012AEEE4AB904D4A3B47AACDD04BD8D119B3A8015F76DF7F24AD13B7C06BB90_cppui_modular256, - 0x03428FD045BAA622B9F7EEB54E8321D96EE1CC04CEFB26F1AEB3D6D40A4DED20_cppui_modular256)); - spng.absorb_g(group_type::value_type(0x184F8EA7DFF1970F52E5A75BC74D8F0FAF76BEB2C56A42D44CB96CCCA895870A_cppui_modular256, - 0x110904EE3F6325E6D3D591FC6D1EAE61D43ACDB54357A7F76AD4FD6DFA59FCB8_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x0000000000000000000000000000000027ABB6B27E12348F52A181F17FE29F41_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x22F5F9CD40DE3BA2268208BAC69D839A0EFF28C445FFC36C21EA64FCC62A7FC5_cppui_modular256, - 0x35FF2C0CAB2901382134A1862322C212A143237419EE758AE4CB6B02FF1BA141_cppui_modular256)); - spng.absorb_g(group_type::value_type(0x1E0518490D0DD242420C8786B744E444FF09E2ECB44217B3FC2323BF739BE8A0_cppui_modular256, - 0x00A9F106114217ECA4313604AA123351DEC9EE0B6C42624270CA9390D34DD0F1_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x000000000000000000000000000000000B79378485D93E3A44F4E3EAEF25D3DA_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x14309CF280C4C82C856B17A808A2E5A583DA7EEB1C385F3B4EB12039EA76AE83_cppui_modular256, - 0x241285C939BD25A6CA34D6F347596BD110FE55AF173A0EBBFB659630A4C96B99_cppui_modular256)); - spng.absorb_g(group_type::value_type(0x09DA297567A8850406E640FF07BC76CCB9A4FA5C11F328EF03708414CBC1F4B9_cppui_modular256, - 0x3BE674505BBBD0F9ED806218258E029084BAECD51ABE6F9264E80C1899307ED6_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x000000000000000000000000000000003CBCF6C44411CE451A33BAB179026502_cppui_modular256)); - - spng.absorb_g(group_type::value_type(0x1DA94235A1E998434A93578409C4EB18BD82A01EC5A25E5D7C7C7C5E001F18FB_cppui_modular256, - 0x04A566923D712C1CEBF2DBE553DD185F8FDACF604E50948925264D3A49C037EA_cppui_modular256)); - BOOST_CHECK(spng.challenge() == - value_type(0x0000000000000000000000000000000072FF5A26FAF972660330A5D0CC5C4700_cppui_modular256)); -} - -BOOST_AUTO_TEST_CASE(zk_fr_sponge_test_real_case) { - // using value_type = base_field_type::value_type; - fr_sponge_type spng; - spng.absorb(value_type(0x0ACB65E0765F80498D643313EAAEBFBC7899766A4A337EAF61261344E8C2C551_cppui_modular256)); - - spng.absorb(value_type(0x1480D3E4FD095CEC3688F88B105EE6F2365DCFAAA28CCB6B87DAB7E71E58010B_cppui_modular256)); - spng.absorb(value_type(0x0C2F522FB163AE4A8D2890C57ABF95E55EF7DDD27A928EFAD0D3FA447D40BC29_cppui_modular256)); - spng.absorb(value_type(0x3F0169364239FF2352BFFEF6D2A206A6DC8FAA526C51EB51FC7610F6E73DFAE5_cppui_modular256)); - spng.absorb(value_type(0x2BCBED001BA14933A1766C68E09BF19C133AB20B87A9D0DB68321A99C4C7A157_cppui_modular256)); - spng.absorb(value_type(0x1430DC77EBF0048A4E26DDB817DD34D3F253AA9894C7D442B8BC06C7683D0188_cppui_modular256)); - spng.absorb(value_type(0x3B79EBE49FAEF6F123C168CF484296A84186EF1FB9FFFA528B0AAC0761F535AD_cppui_modular256)); - spng.absorb(value_type(0x16C6D43CFFB252215D05E1A05DBA2EEAADB3FAAF88B8AABDBD4E8860B9623530_cppui_modular256)); - spng.absorb(value_type(0x1C0801C94EA28AAD68CEA9C9524106D39DC1A3491435A23D35EEBE56DB3AB116_cppui_modular256)); - spng.absorb(value_type(0x21545E083F1282D939751D5E0D4EF173C7528C9E38349FE5E02BAB4686B542D4_cppui_modular256)); - spng.absorb(value_type(0x2E8F53F919EBB22022424A175A051F6FBDB2B57E06E1AC8A8201FBDD02CEE2FD_cppui_modular256)); - spng.absorb(value_type(0x1B5A53763A06BFAF8BAAF566FE885CD31355B2AC4F0F04B13F05610DE1EBAB5E_cppui_modular256)); - spng.absorb(value_type(0x212CC53B694BA1B3ED2D6C514B97325D62BF301F18E76B7DF94F04B7875C7E64_cppui_modular256)); - spng.absorb(value_type(0x22C1E6932B0336B13262867483DEE4C6B8E798C24F4245051254A64C61EAC604_cppui_modular256)); - spng.absorb(value_type(0x356428F289E597185A60ED494351FF93B5802480DC375E4B2C6ECAB816B69524_cppui_modular256)); - spng.absorb(value_type(0x08066B51E8C7F77F825F541E02C51A608FD217435FDF7E75AD5BBE36CB826443_cppui_modular256)); - spng.absorb(value_type(0x1AA8ADB147AA57E6AA5DBAF2C238352D8C6AA301ECD497BBC775E2A2804E3363_cppui_modular256)); - spng.absorb(value_type(0x03D8C35D2E1466E8514E20A8E658F4E2B1116AB123F7BF53F9A1C7376F788EB1_cppui_modular256)); - spng.absorb(value_type(0x05EDDC1E6C268DF398F068F06C51794D6F672E27FB800DFF6C5C35E5C3D84207_cppui_modular256)); - spng.absorb(value_type(0x1B03A1DBEA987367FDEF97CC27F7441C4845E93AD1583167DA4A1A9CCFFB1E71_cppui_modular256)); - spng.absorb(value_type(0x11347E33DF1631D59D66F6149D99DD22FD23B185D7D89CFE0909877C494D7916_cppui_modular256)); - spng.absorb(value_type(0x0E1372B72364C37883171F80BC89F2AC7043464C8C30E1D2B5D94105035A6C6E_cppui_modular256)); - spng.absorb(value_type(0x336A5683971A09A68D33D77B41947F8CAFFE3923190B51D443E515761A32889B_cppui_modular256)); - - // BOOST_CHECK(spng.challenge().value() == - // value_type(0x0000000000000000000000000000000000F9B1BCD2BB1DE25807BE9313410D43_cppui_modular256)); - - spng.absorb(value_type(0x1635A182C3B5623D5E7CF31D244F389FB478B0612B27937A39D48B473DB68931_cppui_modular256)); - spng.absorb(value_type(0x144FF7F30B8C75C60E63614EA792F9A41E41C2DBE40F816A602160960C071F56_cppui_modular256)); - spng.absorb(value_type(0x114768369E43EA7A13DE72AC855AE7D31DC52B34EB45BB96EA1BDFF54FEC4AB8_cppui_modular256)); - spng.absorb(value_type(0x006259A5F4A9A82296077396D476F9E59392BDDA93E63B9A582EF9BBA452A7A2_cppui_modular256)); - spng.absorb(value_type(0x3F9EBB3D514729A24B0C87FB434FC043F48195FA45E510BA5817F0ED05DED76B_cppui_modular256)); - spng.absorb(value_type(0x06F0CA9962E207949F85C22ADCBE8F27E632D14B843F2C65E264752B6100049E_cppui_modular256)); - spng.absorb(value_type(0x3885B6A574C4B6B89867EE499534E0F4937C7D71BA724A857F5E7F797059E879_cppui_modular256)); - spng.absorb(value_type(0x0554E97666ABA1659D7D107E3F709F546625481B1A5684BE24EFE9B3CBBC300F_cppui_modular256)); - spng.absorb(value_type(0x06C748D2C049B08C50633EBF7F7A0C68A03677CE382BF6697B7D285F30215616_cppui_modular256)); - spng.absorb(value_type(0x0B252004A6768951624E56F1D98B1DDB006B2284FE1C08B258D95B92BF40266F_cppui_modular256)); - spng.absorb(value_type(0x029236F173E5278B30CB9DAD8C87CEDE865AD1293B9BBF991F1743E8D1FD6638_cppui_modular256)); - spng.absorb(value_type(0x28C63DB702FFC629457818259603A154886B11D1D1FB7065037F51212E5BE2D3_cppui_modular256)); - spng.absorb(value_type(0x0219DC4D947F1109C90CD6C0112559A5D04528C2B264062A98DC5E7BBF85F269_cppui_modular256)); - spng.absorb(value_type(0x246CB73F3BB0A9AC5FA65DED8A1617E0CB8231146F0DF67467ED5E85242DF2B6_cppui_modular256)); - spng.absorb(value_type(0x06BF9230E2E2424EF63FE51B0306D61BA478A06A226AEDA29DD12DA188D5F302_cppui_modular256)); - spng.absorb(value_type(0x29126D228A13DAF18CD96C487BF794569FB5A8BBDF14DDEC6CE22DAAED7DF34F_cppui_modular256)); - spng.absorb(value_type(0x069DE7D0EBB1985B05DAB9E13348C12530D374BAD474C76C4AB9FAC8EB557332_cppui_modular256)); - spng.absorb(value_type(0x177B2B5F39976BE667F5D6768480F1555F52395613AF100529C99844DA28DCC9_cppui_modular256)); - spng.absorb(value_type(0x2941C2A82AC0067D3DD6A2C47EDD675D5B7BA071414A8324BA4CFAA1816B163F_cppui_modular256)); - spng.absorb(value_type(0x05EA2B93EF3D2CD3E8DDDA175F2446A8390E35219DFBA39111C8CDBFA3038FCE_cppui_modular256)); - spng.absorb(value_type(0x15C6FB1ACD775DF5E860906CDDF37C4E6B82CDC1A67F02F129DEAE98A11620D6_cppui_modular256)); - spng.absorb(value_type(0x338D629CA1F64B37674CA7B5AF91015CA50A5D335E7076E25D9F4C230C99395D_cppui_modular256)); - - // BOOST_CHECK(spng.challenge().value() == - // value_type(0x00000000000000000000000000000000578543A9BA83C66925F5301C187FBF94_cppui_modular256)); - - spng.absorb(value_type(0x16FE1AE7F56997161DB512632BE7BFA337F47F422E0D01AF06DE298DD8C429D5_cppui_modular256)); - - auto squeezed_val1 = spng.challenge().value(); - // std::cout << std::hex << squeezed_val1.data << '\n'; - BOOST_CHECK(squeezed_val1 == - value_type(0x0000000000000000000000000000000070BB1327D20E6ADBCA0F584AEC2D4D0C_cppui_modular256)); - - squeezed_val1 = spng.challenge().value(); - // std::cout << std::hex << squeezed_val1.data << '\n'; - BOOST_CHECK(squeezed_val1 == - value_type(0x0000000000000000000000000000000062BE7EB9CB6245B29DF02D68B4B51EC5_cppui_modular256)); -} + BOOST_AUTO_TEST_CASE(zk_fr_sponge_test_real_case) { + // using value_type = base_field_type::value_type; + fr_sponge_type spng; + spng.absorb(value_type(0x0ACB65E0765F80498D643313EAAEBFBC7899766A4A337EAF61261344E8C2C551_cppui_modular256)); + + spng.absorb(value_type(0x1480D3E4FD095CEC3688F88B105EE6F2365DCFAAA28CCB6B87DAB7E71E58010B_cppui_modular256)); + spng.absorb(value_type(0x0C2F522FB163AE4A8D2890C57ABF95E55EF7DDD27A928EFAD0D3FA447D40BC29_cppui_modular256)); + spng.absorb(value_type(0x3F0169364239FF2352BFFEF6D2A206A6DC8FAA526C51EB51FC7610F6E73DFAE5_cppui_modular256)); + spng.absorb(value_type(0x2BCBED001BA14933A1766C68E09BF19C133AB20B87A9D0DB68321A99C4C7A157_cppui_modular256)); + spng.absorb(value_type(0x1430DC77EBF0048A4E26DDB817DD34D3F253AA9894C7D442B8BC06C7683D0188_cppui_modular256)); + spng.absorb(value_type(0x3B79EBE49FAEF6F123C168CF484296A84186EF1FB9FFFA528B0AAC0761F535AD_cppui_modular256)); + spng.absorb(value_type(0x16C6D43CFFB252215D05E1A05DBA2EEAADB3FAAF88B8AABDBD4E8860B9623530_cppui_modular256)); + spng.absorb(value_type(0x1C0801C94EA28AAD68CEA9C9524106D39DC1A3491435A23D35EEBE56DB3AB116_cppui_modular256)); + spng.absorb(value_type(0x21545E083F1282D939751D5E0D4EF173C7528C9E38349FE5E02BAB4686B542D4_cppui_modular256)); + spng.absorb(value_type(0x2E8F53F919EBB22022424A175A051F6FBDB2B57E06E1AC8A8201FBDD02CEE2FD_cppui_modular256)); + spng.absorb(value_type(0x1B5A53763A06BFAF8BAAF566FE885CD31355B2AC4F0F04B13F05610DE1EBAB5E_cppui_modular256)); + spng.absorb(value_type(0x212CC53B694BA1B3ED2D6C514B97325D62BF301F18E76B7DF94F04B7875C7E64_cppui_modular256)); + spng.absorb(value_type(0x22C1E6932B0336B13262867483DEE4C6B8E798C24F4245051254A64C61EAC604_cppui_modular256)); + spng.absorb(value_type(0x356428F289E597185A60ED494351FF93B5802480DC375E4B2C6ECAB816B69524_cppui_modular256)); + spng.absorb(value_type(0x08066B51E8C7F77F825F541E02C51A608FD217435FDF7E75AD5BBE36CB826443_cppui_modular256)); + spng.absorb(value_type(0x1AA8ADB147AA57E6AA5DBAF2C238352D8C6AA301ECD497BBC775E2A2804E3363_cppui_modular256)); + spng.absorb(value_type(0x03D8C35D2E1466E8514E20A8E658F4E2B1116AB123F7BF53F9A1C7376F788EB1_cppui_modular256)); + spng.absorb(value_type(0x05EDDC1E6C268DF398F068F06C51794D6F672E27FB800DFF6C5C35E5C3D84207_cppui_modular256)); + spng.absorb(value_type(0x1B03A1DBEA987367FDEF97CC27F7441C4845E93AD1583167DA4A1A9CCFFB1E71_cppui_modular256)); + spng.absorb(value_type(0x11347E33DF1631D59D66F6149D99DD22FD23B185D7D89CFE0909877C494D7916_cppui_modular256)); + spng.absorb(value_type(0x0E1372B72364C37883171F80BC89F2AC7043464C8C30E1D2B5D94105035A6C6E_cppui_modular256)); + spng.absorb(value_type(0x336A5683971A09A68D33D77B41947F8CAFFE3923190B51D443E515761A32889B_cppui_modular256)); + + // BOOST_CHECK(spng.challenge().value() == + // value_type(0x0000000000000000000000000000000000F9B1BCD2BB1DE25807BE9313410D43_cppui_modular256)); + + spng.absorb(value_type(0x1635A182C3B5623D5E7CF31D244F389FB478B0612B27937A39D48B473DB68931_cppui_modular256)); + spng.absorb(value_type(0x144FF7F30B8C75C60E63614EA792F9A41E41C2DBE40F816A602160960C071F56_cppui_modular256)); + spng.absorb(value_type(0x114768369E43EA7A13DE72AC855AE7D31DC52B34EB45BB96EA1BDFF54FEC4AB8_cppui_modular256)); + spng.absorb(value_type(0x006259A5F4A9A82296077396D476F9E59392BDDA93E63B9A582EF9BBA452A7A2_cppui_modular256)); + spng.absorb(value_type(0x3F9EBB3D514729A24B0C87FB434FC043F48195FA45E510BA5817F0ED05DED76B_cppui_modular256)); + spng.absorb(value_type(0x06F0CA9962E207949F85C22ADCBE8F27E632D14B843F2C65E264752B6100049E_cppui_modular256)); + spng.absorb(value_type(0x3885B6A574C4B6B89867EE499534E0F4937C7D71BA724A857F5E7F797059E879_cppui_modular256)); + spng.absorb(value_type(0x0554E97666ABA1659D7D107E3F709F546625481B1A5684BE24EFE9B3CBBC300F_cppui_modular256)); + spng.absorb(value_type(0x06C748D2C049B08C50633EBF7F7A0C68A03677CE382BF6697B7D285F30215616_cppui_modular256)); + spng.absorb(value_type(0x0B252004A6768951624E56F1D98B1DDB006B2284FE1C08B258D95B92BF40266F_cppui_modular256)); + spng.absorb(value_type(0x029236F173E5278B30CB9DAD8C87CEDE865AD1293B9BBF991F1743E8D1FD6638_cppui_modular256)); + spng.absorb(value_type(0x28C63DB702FFC629457818259603A154886B11D1D1FB7065037F51212E5BE2D3_cppui_modular256)); + spng.absorb(value_type(0x0219DC4D947F1109C90CD6C0112559A5D04528C2B264062A98DC5E7BBF85F269_cppui_modular256)); + spng.absorb(value_type(0x246CB73F3BB0A9AC5FA65DED8A1617E0CB8231146F0DF67467ED5E85242DF2B6_cppui_modular256)); + spng.absorb(value_type(0x06BF9230E2E2424EF63FE51B0306D61BA478A06A226AEDA29DD12DA188D5F302_cppui_modular256)); + spng.absorb(value_type(0x29126D228A13DAF18CD96C487BF794569FB5A8BBDF14DDEC6CE22DAAED7DF34F_cppui_modular256)); + spng.absorb(value_type(0x069DE7D0EBB1985B05DAB9E13348C12530D374BAD474C76C4AB9FAC8EB557332_cppui_modular256)); + spng.absorb(value_type(0x177B2B5F39976BE667F5D6768480F1555F52395613AF100529C99844DA28DCC9_cppui_modular256)); + spng.absorb(value_type(0x2941C2A82AC0067D3DD6A2C47EDD675D5B7BA071414A8324BA4CFAA1816B163F_cppui_modular256)); + spng.absorb(value_type(0x05EA2B93EF3D2CD3E8DDDA175F2446A8390E35219DFBA39111C8CDBFA3038FCE_cppui_modular256)); + spng.absorb(value_type(0x15C6FB1ACD775DF5E860906CDDF37C4E6B82CDC1A67F02F129DEAE98A11620D6_cppui_modular256)); + spng.absorb(value_type(0x338D629CA1F64B37674CA7B5AF91015CA50A5D335E7076E25D9F4C230C99395D_cppui_modular256)); + + // BOOST_CHECK(spng.challenge().value() == + // value_type(0x00000000000000000000000000000000578543A9BA83C66925F5301C187FBF94_cppui_modular256)); + + spng.absorb(value_type(0x16FE1AE7F56997161DB512632BE7BFA337F47F422E0D01AF06DE298DD8C429D5_cppui_modular256)); + + auto squeezed_val1 = spng.challenge().value(); + // std::cout << std::hex << squeezed_val1.data << '\n'; + BOOST_CHECK(squeezed_val1 == + value_type(0x0000000000000000000000000000000070BB1327D20E6ADBCA0F584AEC2D4D0C_cppui_modular256)); + + squeezed_val1 = spng.challenge().value(); + // std::cout << std::hex << squeezed_val1.data << '\n'; + BOOST_CHECK(squeezed_val1 == + value_type(0x0000000000000000000000000000000062BE7EB9CB6245B29DF02D68B4B51EC5_cppui_modular256)); + } BOOST_AUTO_TEST_SUITE_END() From 55804f81d7bbe337bbfe49aebf4a0fcf05ed701c Mon Sep 17 00:00:00 2001 From: Mikhail Komarov Date: Fri, 7 Jun 2024 08:27:09 +0300 Subject: [PATCH 3/4] Minor formatting changes #254 --- .../test/commitment/fold_polynomial.cpp | 40 +- libs/parallel-zk/test/commitment/fri.cpp | 93 +- .../test/commitment/kimchi_pedersen.cpp | 181 ++-- libs/parallel-zk/test/commitment/lpc.cpp | 948 +++++++++--------- .../test/commitment/lpc_performance.cpp | 514 +++++----- .../test/commitment/powers_of_tau.cpp | 322 +++--- .../test/commitment/proof_of_knowledge.cpp | 26 +- .../test/commitment/proof_of_work.cpp | 100 +- .../test/commitment/r1cs_gg_ppzksnark_mpc.cpp | 238 ++--- 9 files changed, 1254 insertions(+), 1208 deletions(-) diff --git a/libs/parallel-zk/test/commitment/fold_polynomial.cpp b/libs/parallel-zk/test/commitment/fold_polynomial.cpp index 42b3205c..a9475c36 100644 --- a/libs/parallel-zk/test/commitment/fold_polynomial.cpp +++ b/libs/parallel-zk/test/commitment/fold_polynomial.cpp @@ -60,7 +60,7 @@ void test_fold_polynomial() { std::size_t d_log = boost::static_log2::value; std::vector>> D = - math::calculate_domain_set(d_log, 1); + math::calculate_domain_set(d_log, 1); math::polynomial f = {{1u, 3u, 4u, 3u}}; @@ -70,12 +70,12 @@ void test_fold_polynomial() { typename FieldType::value_type alpha = algebra::random_element(); math::polynomial f_next = - zk::commitments::detail::fold_polynomial(f, alpha); + zk::commitments::detail::fold_polynomial(f, alpha); BOOST_CHECK_EQUAL(f_next.degree(), f.degree() / 2); - std::vector> interpolation_points { - std::make_pair(omega, f.evaluate(omega)), - std::make_pair(-omega, f.evaluate(-omega)), + std::vector> interpolation_points{ + std::make_pair(omega, f.evaluate(omega)), + std::make_pair(-omega, f.evaluate(-omega)), }; math::polynomial interpolant = math::lagrange_interpolation(interpolation_points); @@ -94,7 +94,7 @@ void test_fold_polynomial_dfs() { std::size_t d_log = boost::static_log2::value; std::vector>> D = - math::calculate_domain_set(d_log, 2); + math::calculate_domain_set(d_log, 2); math::polynomial f = {{1u, 3u, 4u, 3u}}; @@ -114,7 +114,7 @@ void test_fold_polynomial_dfs() { } math::polynomial_dfs f_next_dfs = - zk::commitments::detail::fold_polynomial(f_dfs, alpha, D[0]); + zk::commitments::detail::fold_polynomial(f_dfs, alpha, D[0]); std::vector f_next_vector(f_next_dfs.begin(), f_next_dfs.end()); D[1]->inverse_fft(f_next_vector); math::polynomial f_next(f_next_vector.size()); @@ -123,9 +123,9 @@ void test_fold_polynomial_dfs() { } BOOST_CHECK_EQUAL(f_next.degree(), f.degree() / 2); - std::vector> interpolation_points { - std::make_pair(omega, f.evaluate(omega)), - std::make_pair(-omega, f.evaluate(-omega)), + std::vector> interpolation_points{ + std::make_pair(omega, f.evaluate(omega)), + std::make_pair(-omega, f.evaluate(-omega)), }; math::polynomial interpolant = math::lagrange_interpolation(interpolation_points); @@ -136,21 +136,21 @@ void test_fold_polynomial_dfs() { BOOST_AUTO_TEST_SUITE(fold_polynomial_test_suite) -BOOST_AUTO_TEST_CASE(fold_polynomial_test) { - test_fold_polynomial>(); + BOOST_AUTO_TEST_CASE(fold_polynomial_test) { + test_fold_polynomial>(); - test_fold_polynomial(); + test_fold_polynomial(); - test_fold_polynomial(); -} + test_fold_polynomial(); + } -BOOST_AUTO_TEST_CASE(fold_polynomial_dfs_test) { + BOOST_AUTO_TEST_CASE(fold_polynomial_dfs_test) { - test_fold_polynomial_dfs>(); + test_fold_polynomial_dfs>(); - test_fold_polynomial_dfs(); + test_fold_polynomial_dfs(); - test_fold_polynomial_dfs(); -} + test_fold_polynomial_dfs(); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/fri.cpp b/libs/parallel-zk/test/commitment/fri.cpp index 1ba05529..c505644b 100644 --- a/libs/parallel-zk/test/commitment/fri.cpp +++ b/libs/parallel-zk/test/commitment/fri.cpp @@ -80,67 +80,70 @@ inline std::vector generate_random_step_list(const std::size_t r, c BOOST_AUTO_TEST_SUITE(fri_test_suite) -BOOST_AUTO_TEST_CASE(fri_basic_test) { + BOOST_AUTO_TEST_CASE(fri_basic_test) { - // setup - using curve_type = algebra::curves::pallas; - using FieldType = typename curve_type::base_field_type; + // setup + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; - constexpr static const std::size_t d = 16; + constexpr static const std::size_t d = 16; - constexpr static const std::size_t r = boost::static_log2::value; - constexpr static const std::size_t m = 2; - constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t r = boost::static_log2::value; + constexpr static const std::size_t m = 2; + constexpr static const std::size_t lambda = 40; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); - typedef typename fri_type::proof_type proof_type; - typedef typename fri_type::params_type params_type; + typedef typename fri_type::proof_type proof_type; + typedef typename fri_type::params_type params_type; - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); - params_type params( - d - 1, // max_degree - D, - generate_random_step_list(r, 1), - 2, //expand_factor - lambda, - true, - 0xFFFFF - ); + params_type params( + d - 1, // max_degree + D, + generate_random_step_list(r, 1), + 2, //expand_factor + lambda, + true, + 0xFFFFF + ); - BOOST_CHECK(D[1]->m == D[0]->m / 2); - BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared()); + BOOST_CHECK(D[1]->m == D[0]->m / 2); + BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared()); - // commit - math::polynomial f = {{1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}}; + // commit + math::polynomial f = { + {1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}}; - typename fri_type::merkle_tree_type tree = zk::algorithms::precommit(f, params.D[0], params.step_list[0]); - auto root = zk::algorithms::commit(tree); + typename fri_type::merkle_tree_type tree = zk::algorithms::precommit(f, params.D[0], + params.step_list[0]); + auto root = zk::algorithms::commit(tree); - // eval - std::vector init_blob {0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u}; - zk::transcript::fiat_shamir_heuristic_sequential transcript(init_blob); + // eval + std::vector init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u}; + zk::transcript::fiat_shamir_heuristic_sequential transcript(init_blob); - proof_type proof = zk::algorithms::proof_eval(f, tree, params, transcript); + proof_type proof = zk::algorithms::proof_eval(f, tree, params, transcript); - // verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(init_blob); + // verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(init_blob); - BOOST_CHECK(zk::algorithms::verify_eval(proof, root, params, transcript_verifier)); + BOOST_CHECK(zk::algorithms::verify_eval(proof, root, params, transcript_verifier)); + + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp b/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp index 0259665a..6c757c18 100644 --- a/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp +++ b/libs/parallel-zk/test/commitment/kimchi_pedersen.cpp @@ -88,10 +88,11 @@ struct aggregated_evaluation_proof { batchproof_type verify_type() { std::vector coms; - for (auto &eval_com : eval_commitments) { + for (auto &eval_com: eval_commitments) { assert(eval_points.size() == eval_com.chunked_evals.size()); coms.push_back( - evaluation_type({eval_com.commit.chunked_commitment, eval_com.chunked_evals, (int)eval_com.commit.bound})); + evaluation_type( + {eval_com.commit.chunked_commitment, eval_com.chunked_evals, (int) eval_com.commit.bound})); } return batchproof_type({fq_sponge, coms, eval_points, polymask, evalmask, proof}); @@ -103,14 +104,14 @@ struct chunked_polynomial { unsigned int chunk_size; chunked_polynomial(math::polynomial &big_polynomial, unsigned int chunk_size) : - chunk_size(chunk_size) { + chunk_size(chunk_size) { unsigned int number_of_chunks = - big_polynomial.size() / chunk_size + (big_polynomial.size() % chunk_size ? 1 : 0); + big_polynomial.size() / chunk_size + (big_polynomial.size() % chunk_size ? 1 : 0); for (unsigned int i = 0; i < number_of_chunks; ++i) { auto iter_chunk_begin = big_polynomial.begin() + i * chunk_size; auto iter_chunk_end = - big_polynomial.begin() + (i == number_of_chunks - 1 ? big_polynomial.size() : (i + 1) * chunk_size); + big_polynomial.begin() + (i == number_of_chunks - 1 ? big_polynomial.size() : (i + 1) * chunk_size); chunked_polynomials.emplace_back(iter_chunk_begin, iter_chunk_end); } } @@ -118,7 +119,7 @@ struct chunked_polynomial { std::vector evaluate_chunks(scalar_value_type &point) { std::vector result; - for (auto &a : chunked_polynomials) { + for (auto &a: chunked_polynomials) { result.push_back(a.evaluate(point)); } @@ -128,122 +129,124 @@ struct chunked_polynomial { BOOST_AUTO_TEST_SUITE(kimchi_commitment_test_suite) -BOOST_AUTO_TEST_CASE(kimchi_commitment_test_opening_proof) { - snark::group_map g_map; - sponge_type fq_sponge; - params_type params = kimchi_pedersen::setup(20); + BOOST_AUTO_TEST_CASE(kimchi_commitment_test_opening_proof) { + snark::group_map g_map; + sponge_type fq_sponge; + params_type params = kimchi_pedersen::setup(20); - std::vector coeffs; - for (int i = 0; i < 10; ++i) { - coeffs.emplace_back(i); - } + std::vector coeffs; + for (int i = 0; i < 10; ++i) { + coeffs.emplace_back(i); + } - math::polynomial poly1(coeffs); - math::polynomial poly2(coeffs.begin(), coeffs.begin() + 5); + math::polynomial poly1(coeffs); + math::polynomial poly2(coeffs.begin(), coeffs.begin() + 5); - blinded_commitment_type commitment = kimchi_pedersen::commitment(params, poly1, -1); - blinded_commitment_type bounded_commitment = kimchi_pedersen::commitment(params, poly2, poly2.degree() + 1); + blinded_commitment_type commitment = kimchi_pedersen::commitment(params, poly1, -1); + blinded_commitment_type bounded_commitment = kimchi_pedersen::commitment(params, poly2, poly2.degree() + 1); - scalar_value_type u = algebra::random_element(); - scalar_value_type v = algebra::random_element(); + scalar_value_type u = algebra::random_element(); + scalar_value_type v = algebra::random_element(); - polynomial_type polys {{poly1, -1, std::get<1>(commitment)}, - {poly2, static_cast(poly2.degree() + 1), std::get<1>(bounded_commitment)}}; + polynomial_type polys{{poly1, -1, std::get<1>(commitment)}, + {poly2, static_cast(poly2.degree() + 1), std::get<1>(bounded_commitment)}}; - std::vector elm {algebra::random_element(), - algebra::random_element()}; + std::vector elm{algebra::random_element(), + algebra::random_element()}; - proof_type proof = kimchi_pedersen::proof_eval(params, g_map, polys, elm, v, u, fq_sponge); + proof_type proof = kimchi_pedersen::proof_eval(params, g_map, polys, elm, v, u, fq_sponge); - chunked_polynomial poly1_chunked(poly1, params.g.size()); - chunked_polynomial poly2_chunked(poly2, params.g.size()); + chunked_polynomial poly1_chunked(poly1, params.g.size()); + chunked_polynomial poly2_chunked(poly2, params.g.size()); - std::vector> poly1_chunked_evals = {poly1_chunked.evaluate_chunks(elm[0]), - poly1_chunked.evaluate_chunks(elm[1])}; - std::vector> poly2_chunked_evals = {poly2_chunked.evaluate_chunks(elm[0]), - poly2_chunked.evaluate_chunks(elm[1])}; + std::vector> poly1_chunked_evals = {poly1_chunked.evaluate_chunks(elm[0]), + poly1_chunked.evaluate_chunks(elm[1])}; + std::vector> poly2_chunked_evals = {poly2_chunked.evaluate_chunks(elm[0]), + poly2_chunked.evaluate_chunks(elm[1])}; - std::vector evals; - evals.emplace_back(std::get<0>(commitment), poly1_chunked_evals, -1); - evals.emplace_back(std::get<0>(bounded_commitment), poly2_chunked_evals, poly2.degree() + 1); - sponge_type new_fq_sponge; - std::vector batch; - batch.emplace_back(new_fq_sponge, evals, elm, v, u, proof); + std::vector evals; + evals.emplace_back(std::get<0>(commitment), poly1_chunked_evals, -1); + evals.emplace_back(std::get<0>(bounded_commitment), poly2_chunked_evals, poly2.degree() + 1); + sponge_type new_fq_sponge; + std::vector batch; + batch.emplace_back(new_fq_sponge, evals, elm, v, u, proof); - BOOST_CHECK(kimchi_pedersen::verify_eval(params, g_map, batch)); -} + BOOST_CHECK(kimchi_pedersen::verify_eval(params, g_map, batch)); + } -BOOST_AUTO_TEST_CASE(kimchi_commitment_test_case) { + BOOST_AUTO_TEST_CASE(kimchi_commitment_test_case) { - snark::group_map g_map; - sponge_type fq_sponge; - params_type params = kimchi_pedersen::setup(1 << 7); + snark::group_map g_map; + sponge_type fq_sponge; + params_type params = kimchi_pedersen::setup(1 << 7); + + std::vector proofs; - std::vector proofs; + std::size_t count_eval_proofs = 1; + for (std::size_t i = 0; i < count_eval_proofs; ++i) { + std::vector eval_points(7); - std::size_t count_eval_proofs = 1; - for (std::size_t i = 0; i < count_eval_proofs; ++i) { - std::vector eval_points(7); - - // Random_element is called with the default parameter, yet we need to wrap it in a lamda. - std::generate(eval_points.begin(), eval_points.end(), - [](){return algebra::random_element();}); + // Random_element is called with the default parameter, yet we need to wrap it in a lamda. + std::generate(eval_points.begin(), eval_points.end(), + []() { return algebra::random_element(); }); - std::vector commitments; + std::vector commitments; - for (unsigned int i = 0; i < 3; ++i) { - unsigned int len = std::rand() % 500; - std::vector polynom_coeffs(len); + for (unsigned int i = 0; i < 3; ++i) { + unsigned int len = std::rand() % 500; + std::vector polynom_coeffs(len); - // Random_element is called with the default parameter, yet we need to wrap it in a lamda. - std::generate(polynom_coeffs.begin(), polynom_coeffs.end(), [](){return algebra::random_element();}); - unsigned int bound = polynom_coeffs.size(); + // Random_element is called with the default parameter, yet we need to wrap it in a lamda. + std::generate(polynom_coeffs.begin(), polynom_coeffs.end(), + []() { return algebra::random_element(); }); + unsigned int bound = polynom_coeffs.size(); - math::polynomial poly(polynom_coeffs.begin(), polynom_coeffs.end()); + math::polynomial poly(polynom_coeffs.begin(), polynom_coeffs.end()); - blinded_commitment_type blinded_commitment = kimchi_pedersen::commitment(params, poly, bound); + blinded_commitment_type blinded_commitment = kimchi_pedersen::commitment(params, poly, bound); - std::vector> chunked_evals; + std::vector> chunked_evals; - chunked_polynomial chunked_poly(poly, params.g.size()); + chunked_polynomial chunked_poly(poly, params.g.size()); - for (auto &point : eval_points) { - chunked_evals.emplace_back(chunked_poly.evaluate_chunks(point)); + for (auto &point: eval_points) { + chunked_evals.emplace_back(chunked_poly.evaluate_chunks(point)); + } + + Commitment commit{std::get<0>(blinded_commitment), bound}; + evaluated_commitment eval_commit{commit, chunked_evals}; + commitments.emplace_back(CommitmentAndSecrets({eval_commit, poly, std::get<1>(blinded_commitment)})); } - Commitment commit {std::get<0>(blinded_commitment), bound}; - evaluated_commitment eval_commit {commit, chunked_evals}; - commitments.emplace_back(CommitmentAndSecrets({eval_commit, poly, std::get<1>(blinded_commitment)})); - } + polynomial_type polynomials; - polynomial_type polynomials; + for (auto &c: commitments) { + polynomials.emplace_back(c.poly, c.eval_commit.commit.bound, c.chunked_blinding); + } - for (auto &c : commitments) { - polynomials.emplace_back(c.poly, c.eval_commit.commit.bound, c.chunked_blinding); - } + scalar_value_type polymask = algebra::random_element(); + scalar_value_type evalmask = algebra::random_element(); - scalar_value_type polymask = algebra::random_element(); - scalar_value_type evalmask = algebra::random_element(); + proof_type proof = + kimchi_pedersen::proof_eval(params, g_map, polynomials, eval_points, polymask, evalmask, fq_sponge); - proof_type proof = - kimchi_pedersen::proof_eval(params, g_map, polynomials, eval_points, polymask, evalmask, fq_sponge); + std::vector eval_commitments; + for (auto &c: commitments) { + eval_commitments.emplace_back(c.eval_commit); + } - std::vector eval_commitments; - for (auto &c : commitments) { - eval_commitments.emplace_back(c.eval_commit); + sponge_type new_fq_sponge; + proofs.emplace_back( + aggregated_evaluation_proof( + {eval_points, eval_commitments, polymask, evalmask, new_fq_sponge, proof})); } - sponge_type new_fq_sponge; - proofs.emplace_back( - aggregated_evaluation_proof({eval_points, eval_commitments, polymask, evalmask, new_fq_sponge, proof})); - } + std::vector batch; + for (auto &proof: proofs) { + batch.emplace_back(proof.verify_type()); + } - std::vector batch; - for (auto &proof : proofs) { - batch.emplace_back(proof.verify_type()); + BOOST_CHECK(kimchi_pedersen::verify_eval(params, g_map, batch)); } - BOOST_CHECK(kimchi_pedersen::verify_eval(params, g_map, batch)); -} - BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/lpc.cpp b/libs/parallel-zk/test/commitment/lpc.cpp index 8ab0adc7..47e66d62 100644 --- a/libs/parallel-zk/test/commitment/lpc.cpp +++ b/libs/parallel-zk/test/commitment/lpc.cpp @@ -58,7 +58,8 @@ using namespace nil::crypto3; using dist_type = std::uniform_int_distribution; -inline std::vector generate_random_step_list(const std::size_t r, const std::size_t max_step, boost::random::mt11213b &rnd) { +inline std::vector +generate_random_step_list(const std::size_t r, const std::size_t max_step, boost::random::mt11213b &rnd) { std::vector step_list; std::size_t steps_sum = 0; while (steps_sum != r) { @@ -77,51 +78,51 @@ inline std::vector generate_random_step_list(const std::size_t r, c return step_list; } -template +template inline math::polynomial generate_random_polynomial( - std::size_t degree, - nil::crypto3::random::algebraic_engine &rnd -){ + std::size_t degree, + nil::crypto3::random::algebraic_engine &rnd +) { math::polynomial result(degree); std::generate(std::begin(result), std::end(result), [&rnd]() { return rnd(); }); return result; } -template +template inline math::polynomial_dfs generate_random_polynomial_dfs( - std::size_t degree, - nil::crypto3::random::algebraic_engine &rnd -){ + std::size_t degree, + nil::crypto3::random::algebraic_engine &rnd +) { math::polynomial data = generate_random_polynomial(degree, rnd); math::polynomial_dfs result; result.from_coefficients(data); return result; } -template +template inline std::vector> generate_random_polynomial_batch( - std::size_t batch_size, - std::size_t degree, - nil::crypto3::random::algebraic_engine &rnd -){ + std::size_t batch_size, + std::size_t degree, + nil::crypto3::random::algebraic_engine &rnd +) { std::vector> result; - for( std::size_t i = 0; i < batch_size; i++ ){ + for (std::size_t i = 0; i < batch_size; i++) { result.push_back(generate_random_polynomial(degree, rnd)); } return result; } -template +template inline std::vector> generate_random_polynomial_dfs_batch( - std::size_t batch_size, - std::size_t degree, - nil::crypto3::random::algebraic_engine &rnd -){ + std::size_t batch_size, + std::size_t degree, + nil::crypto3::random::algebraic_engine &rnd +) { auto data = generate_random_polynomial_batch(batch_size, degree, rnd); std::vector> result; - for( std::size_t i = 0; i < data.size(); i++ ){ + for (std::size_t i = 0; i < data.size(); i++) { math::polynomial_dfs dfs; dfs.from_coefficients(data[i]); result.push_back(dfs); @@ -131,514 +132,527 @@ inline std::vector> generat std::size_t test_global_seed = 0; boost::random::mt11213b test_global_rnd_engine; -template +template nil::crypto3::random::algebraic_engine test_global_alg_rnd_engine; struct test_fixture { // Enumerate all fields used in tests; using field1_type = algebra::curves::bls12<381>::scalar_field_type; - test_fixture(){ + test_fixture() { 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"){ + 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]); + 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_rnd_engine = boost::random::mt11213b(test_global_seed); test_global_alg_rnd_engine = nil::crypto3::random::algebraic_engine(test_global_seed); } - ~test_fixture(){} + + ~test_fixture() {} }; BOOST_AUTO_TEST_SUITE(lpc_math_polynomial_suite); -BOOST_FIXTURE_TEST_CASE(lpc_basic_test, test_fixture) { - // Setup types. - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; - typedef typename containers::merkle_tree merkle_tree_type; + BOOST_FIXTURE_TEST_CASE(lpc_basic_test, test_fixture) { + // Setup types. + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; + typedef typename containers::merkle_tree merkle_tree_type; - constexpr static const std::size_t lambda = 10; - constexpr static const std::size_t k = 1; + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t k = 1; - constexpr static const std::size_t d = 16; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t d = 16; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; + constexpr static const std::size_t m = 2; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - typedef zk::commitments:: + typedef zk::commitments:: list_polynomial_commitment_params - lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - static_assert(zk::is_commitment::value); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - - // Setup params - typename fri_type::params_type fri_params( - d - 1, // max_degree - D, - generate_random_step_list(r, 1, test_global_rnd_engine), - 2, //expand_factor - lambda, - true, - 0xFFF - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - // Generate polynomials - lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u, 2u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u, 3u}); - lpc_scheme_prover.append_to_batch(2, {0u}); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(4, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(9, test_global_alg_rnd_engine)); - - // Commit - std::map commitments; - commitments[0] = lpc_scheme_prover.commit(0); - commitments[1] = lpc_scheme_prover.commit(1); - commitments[2] = lpc_scheme_prover.commit(2); - commitments[3] = lpc_scheme_prover.commit(3); - - // Generate evaluation points. Choose poin1ts outside the domain - auto point = algebra::fields::arithmetic_params::multiplicative_generator; - lpc_scheme_prover.append_eval_point(0, point); - lpc_scheme_prover.append_eval_point(1, point); - lpc_scheme_prover.append_eval_point(2, point); - lpc_scheme_prover.append_eval_point(3, point); - - std::array x_data {}; - - // Prove - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - auto proof = lpc_scheme_prover.proof_eval(transcript); - - // Verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); - lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); - lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); - - lpc_scheme_verifier.append_eval_point(0, point); - lpc_scheme_verifier.append_eval_point(1, point); - lpc_scheme_verifier.append_eval_point(2, point); - lpc_scheme_verifier.append_eval_point(3, point); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - - // Check transcript state - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} + lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + static_assert(zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + + // Setup params + typename fri_type::params_type fri_params( + d - 1, // max_degree + D, + generate_random_step_list(r, 1, test_global_rnd_engine), + 2, //expand_factor + lambda, + true, + 0xFFF + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + // Generate polynomials + lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u, 2u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u, 3u}); + lpc_scheme_prover.append_to_batch(2, {0u}); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(4, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(9, test_global_alg_rnd_engine)); + + // Commit + std::map commitments; + commitments[0] = lpc_scheme_prover.commit(0); + commitments[1] = lpc_scheme_prover.commit(1); + commitments[2] = lpc_scheme_prover.commit(2); + commitments[3] = lpc_scheme_prover.commit(3); + + // Generate evaluation points. Choose poin1ts outside the domain + auto point = algebra::fields::arithmetic_params::multiplicative_generator; + lpc_scheme_prover.append_eval_point(0, point); + lpc_scheme_prover.append_eval_point(1, point); + lpc_scheme_prover.append_eval_point(2, point); + lpc_scheme_prover.append_eval_point(3, point); + + std::array x_data{}; + + // Prove + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + auto proof = lpc_scheme_prover.proof_eval(transcript); + + // Verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); + lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); + lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); + + lpc_scheme_verifier.append_eval_point(0, point); + lpc_scheme_verifier.append_eval_point(1, point); + lpc_scheme_verifier.append_eval_point(2, point); + lpc_scheme_verifier.append_eval_point(3, point); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + + // Check transcript state + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } -BOOST_FIXTURE_TEST_CASE(lpc_basic_skipping_layers_test, test_fixture) { - // Setup types - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; + BOOST_FIXTURE_TEST_CASE(lpc_basic_skipping_layers_test, test_fixture) { + // Setup types + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; - typedef typename containers::merkle_tree merkle_tree_type; + typedef typename containers::merkle_tree merkle_tree_type; - constexpr static const std::size_t lambda = 10; - constexpr static const std::size_t k = 1; + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t k = 1; - constexpr static const std::size_t d = 2048; + constexpr static const std::size_t d = 2048; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t m = 2; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - typedef zk::commitments:: + typedef zk::commitments:: list_polynomial_commitment_params - lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - static_assert(zk::is_commitment::value); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typedef zk::commitments::fri fri_type; - - // Setup params - typename fri_type::params_type fri_params( - d - 1, // max_degree - D, - generate_random_step_list(r, 5, test_global_rnd_engine), - 2, //expand_factor - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - // Generate polynomials - lpc_scheme_prover.append_to_batch(0, generate_random_polynomial_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(1, generate_random_polynomial_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(2, generate_random_polynomial_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - - std::map commitments; - commitments[0] = lpc_scheme_prover.commit(0); - commitments[1] = lpc_scheme_prover.commit(1); - commitments[2] = lpc_scheme_prover.commit(2); - commitments[3] = lpc_scheme_prover.commit(3); - - // Generate evaluation points. Choose poin1ts outside the domain - auto point = algebra::fields::arithmetic_params::multiplicative_generator; - lpc_scheme_prover.append_eval_point(0, point); - lpc_scheme_prover.append_eval_point(1, point); - lpc_scheme_prover.append_eval_point(2, point); - lpc_scheme_prover.append_eval_point(3, point); - - std::array x_data {}; - - // Prove - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - auto proof = lpc_scheme_prover.proof_eval(transcript); - - // Verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); - lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); - lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); - - lpc_scheme_verifier.append_eval_point(0, point); - lpc_scheme_verifier.append_eval_point(1, point); - lpc_scheme_verifier.append_eval_point(2, point); - lpc_scheme_verifier.append_eval_point(3, point); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - - // Check transcript state - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} + lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + static_assert(zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typedef zk::commitments::fri fri_type; + + // Setup params + typename fri_type::params_type fri_params( + d - 1, // max_degree + D, + generate_random_step_list(r, 5, test_global_rnd_engine), + 2, //expand_factor + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + // Generate polynomials + lpc_scheme_prover.append_to_batch(0, generate_random_polynomial_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(1, generate_random_polynomial_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(2, generate_random_polynomial_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + + std::map commitments; + commitments[0] = lpc_scheme_prover.commit(0); + commitments[1] = lpc_scheme_prover.commit(1); + commitments[2] = lpc_scheme_prover.commit(2); + commitments[3] = lpc_scheme_prover.commit(3); + + // Generate evaluation points. Choose poin1ts outside the domain + auto point = algebra::fields::arithmetic_params::multiplicative_generator; + lpc_scheme_prover.append_eval_point(0, point); + lpc_scheme_prover.append_eval_point(1, point); + lpc_scheme_prover.append_eval_point(2, point); + lpc_scheme_prover.append_eval_point(3, point); + + std::array x_data{}; + + // Prove + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + auto proof = lpc_scheme_prover.proof_eval(transcript); + + // Verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); + lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); + lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); + + lpc_scheme_verifier.append_eval_point(0, point); + lpc_scheme_verifier.append_eval_point(1, point); + lpc_scheme_verifier.append_eval_point(2, point); + lpc_scheme_verifier.append_eval_point(3, point); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + + // Check transcript state + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } -BOOST_FIXTURE_TEST_CASE(lpc_dfs_basic_test, test_fixture) { - // Setup types - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; + BOOST_FIXTURE_TEST_CASE(lpc_dfs_basic_test, test_fixture) { + // Setup types + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; - typedef typename containers::merkle_tree merkle_tree_type; + typedef typename containers::merkle_tree merkle_tree_type; - constexpr static const std::size_t lambda = 10; - constexpr static const std::size_t k = 1; + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t k = 1; - constexpr static const std::size_t d = 16; + constexpr static const std::size_t d = 16; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t m = 2; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - typedef zk::commitments:: + typedef zk::commitments:: list_polynomial_commitment_params - lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - static_assert(zk::is_commitment::value); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - - // Setup params - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r+1); - - // Setup params - typename fri_type::params_type fri_params( - d - 1, // max_degree - D, - generate_random_step_list(r, 1, test_global_rnd_engine), - 2, //expand_factor - lambda, - true - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - // Generate polynomials - std::array>,4> f; - lpc_scheme_prover.append_to_batch(0, generate_random_polynomial_dfs_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(1, generate_random_polynomial_dfs_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(2, generate_random_polynomial_dfs_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial_dfs_batch(dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); - - std::map commitments; - commitments[0] = lpc_scheme_prover.commit(0); - commitments[1] = lpc_scheme_prover.commit(1); - commitments[2] = lpc_scheme_prover.commit(2); - commitments[3] = lpc_scheme_prover.commit(3); - - // Generate evaluation points. Choose poin1ts outside the domain - auto point = algebra::fields::arithmetic_params::multiplicative_generator; - lpc_scheme_prover.append_eval_point(0, point); - lpc_scheme_prover.append_eval_point(1, point); - lpc_scheme_prover.append_eval_point(2, point); - lpc_scheme_prover.append_eval_point(3, point); - - std::array x_data {}; - - // Prove - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - auto proof = lpc_scheme_prover.proof_eval(transcript); - - // Verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); - lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); - lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); - - lpc_scheme_verifier.append_eval_point(0, point); - lpc_scheme_verifier.append_eval_point(1, point); - lpc_scheme_verifier.append_eval_point(2, point); - lpc_scheme_verifier.append_eval_point(3, point); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - - // Check transcript state - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} + lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + static_assert(zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + + // Setup params + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r + 1); + + // Setup params + typename fri_type::params_type fri_params( + d - 1, // max_degree + D, + generate_random_step_list(r, 1, test_global_rnd_engine), + 2, //expand_factor + lambda, + true + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + // Generate polynomials + std::array>, 4> f; + lpc_scheme_prover.append_to_batch(0, generate_random_polynomial_dfs_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(1, generate_random_polynomial_dfs_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(2, generate_random_polynomial_dfs_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial_dfs_batch( + dist_type(1, 10)(test_global_rnd_engine), d, test_global_alg_rnd_engine)); + + std::map commitments; + commitments[0] = lpc_scheme_prover.commit(0); + commitments[1] = lpc_scheme_prover.commit(1); + commitments[2] = lpc_scheme_prover.commit(2); + commitments[3] = lpc_scheme_prover.commit(3); + + // Generate evaluation points. Choose poin1ts outside the domain + auto point = algebra::fields::arithmetic_params::multiplicative_generator; + lpc_scheme_prover.append_eval_point(0, point); + lpc_scheme_prover.append_eval_point(1, point); + lpc_scheme_prover.append_eval_point(2, point); + lpc_scheme_prover.append_eval_point(3, point); + + std::array x_data{}; + + // Prove + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + auto proof = lpc_scheme_prover.proof_eval(transcript); + + // Verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); + lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); + lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); + + lpc_scheme_verifier.append_eval_point(0, point); + lpc_scheme_verifier.append_eval_point(1, point); + lpc_scheme_verifier.append_eval_point(2, point); + lpc_scheme_verifier.append_eval_point(3, point); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + + // Check transcript state + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(lpc_params_test_suite) -BOOST_FIXTURE_TEST_CASE(lpc_batches_num_3_test, test_fixture){ - // Setup types. - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; - typedef typename containers::merkle_tree merkle_tree_type; - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; + BOOST_FIXTURE_TEST_CASE(lpc_batches_num_3_test, test_fixture) { + // Setup types. + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + typedef hashes::sha2<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; + typedef typename containers::merkle_tree merkle_tree_type; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t k = 1; - constexpr static const std::size_t d = 16; + constexpr static const std::size_t d = 16; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t m = 2; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - typedef zk::commitments:: + typedef zk::commitments:: list_polynomial_commitment_params - lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - static_assert(zk::is_commitment::value); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, // max_degree - D, - generate_random_step_list(r, 1, test_global_rnd_engine), - 2, //expand_factor - lambda, - true, - 0xFF - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - // Generate polynomials - lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); - lpc_scheme_prover.append_to_batch(2, {0u, 1u}); - lpc_scheme_prover.append_to_batch(2, {0u, 1u, 2u}); - lpc_scheme_prover.append_to_batch(2, {0u, 1u, 3u}); - lpc_scheme_prover.append_to_batch(3, {0u}); - - // Commit - std::map commitments; - commitments[0] = lpc_scheme_prover.commit(0); - commitments[2] = lpc_scheme_prover.commit(2); - commitments[3] = lpc_scheme_prover.commit(3); - - // Generate evaluation points. Generate points outside of the basic domain - // Generate evaluation points. Choose poin1ts outside the domain - auto point = algebra::fields::arithmetic_params::multiplicative_generator; - lpc_scheme_prover.append_eval_point(0, point); - lpc_scheme_prover.append_eval_point(2, point); - lpc_scheme_prover.append_eval_point(3, point); - - std::array x_data {}; - - // Prove - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - auto proof = lpc_scheme_prover.proof_eval(transcript); - - // Verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); - lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); - - lpc_scheme_verifier.append_eval_point(0, point); - lpc_scheme_verifier.append_eval_point(2, point); - lpc_scheme_verifier.append_eval_point(3, point); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - - // Check transcript state - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} + lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + static_assert(zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, // max_degree + D, + generate_random_step_list(r, 1, test_global_rnd_engine), + 2, //expand_factor + lambda, + true, + 0xFF + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + // Generate polynomials + lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); + lpc_scheme_prover.append_to_batch(2, {0u, 1u}); + lpc_scheme_prover.append_to_batch(2, {0u, 1u, 2u}); + lpc_scheme_prover.append_to_batch(2, {0u, 1u, 3u}); + lpc_scheme_prover.append_to_batch(3, {0u}); + + // Commit + std::map commitments; + commitments[0] = lpc_scheme_prover.commit(0); + commitments[2] = lpc_scheme_prover.commit(2); + commitments[3] = lpc_scheme_prover.commit(3); + + // Generate evaluation points. Generate points outside of the basic domain + // Generate evaluation points. Choose poin1ts outside the domain + auto point = algebra::fields::arithmetic_params::multiplicative_generator; + lpc_scheme_prover.append_eval_point(0, point); + lpc_scheme_prover.append_eval_point(2, point); + lpc_scheme_prover.append_eval_point(3, point); + + std::array x_data{}; + + // Prove + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + auto proof = lpc_scheme_prover.proof_eval(transcript); + + // Verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); + lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); + + lpc_scheme_verifier.append_eval_point(0, point); + lpc_scheme_verifier.append_eval_point(2, point); + lpc_scheme_verifier.append_eval_point(3, point); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + + // Check transcript state + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } -BOOST_FIXTURE_TEST_CASE(lpc_different_hash_types_test, test_fixture) { - // Setup types. - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; - typedef typename containers::merkle_tree merkle_tree_type; + BOOST_FIXTURE_TEST_CASE(lpc_different_hash_types_test, test_fixture) { + // Setup types. + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + typedef hashes::keccak_1600<256> merkle_hash_type; + typedef hashes::sha2<256> transcript_hash_type; + typedef typename containers::merkle_tree merkle_tree_type; - constexpr static const std::size_t lambda = 10; - constexpr static const std::size_t k = 1; + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t k = 1; - constexpr static const std::size_t d = 16; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t d = 16; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; + constexpr static const std::size_t m = 2; - typedef zk::commitments::fri fri_type; + typedef zk::commitments::fri fri_type; - typedef zk::commitments:: + typedef zk::commitments:: list_polynomial_commitment_params - lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - static_assert(zk::is_commitment::value); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - static_assert(!zk::is_commitment::value); - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, // max_degree - D, - generate_random_step_list(r, 1, test_global_rnd_engine), - 2, //expand_factor - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - // Generate polynomials - lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u, 2u}); - lpc_scheme_prover.append_to_batch(1, {0u, 1u, 3u}); - lpc_scheme_prover.append_to_batch(2, {0u}); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(4, test_global_alg_rnd_engine)); - lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(9, test_global_alg_rnd_engine)); - - // Commit - std::map commitments; - commitments[0] = lpc_scheme_prover.commit(0); - commitments[1] = lpc_scheme_prover.commit(1); - commitments[2] = lpc_scheme_prover.commit(2); - commitments[3] = lpc_scheme_prover.commit(3); - - // Generate evaluation points. Choose poin1ts outside the domain - auto point = algebra::fields::arithmetic_params::multiplicative_generator; - lpc_scheme_prover.append_eval_point(0, point); - lpc_scheme_prover.append_eval_point(1, point); - lpc_scheme_prover.append_eval_point(2, point); - lpc_scheme_prover.append_eval_point(3, point); - - std::array x_data {}; - - // Prove - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - auto proof = lpc_scheme_prover.proof_eval(transcript); - - // Verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); - lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); - lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); - - lpc_scheme_verifier.append_eval_point(0, point); - lpc_scheme_verifier.append_eval_point(1, point); - lpc_scheme_verifier.append_eval_point(2, point); - lpc_scheme_verifier.append_eval_point(3, point); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); - - // Check transcript state - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); -} + lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + static_assert(zk::is_commitment::value); + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + static_assert(!zk::is_commitment::value); + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, // max_degree + D, + generate_random_step_list(r, 1, test_global_rnd_engine), + 2, //expand_factor + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + // Generate polynomials + lpc_scheme_prover.append_to_batch(0, {1u, 13u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u, 2u}); + lpc_scheme_prover.append_to_batch(1, {0u, 1u, 3u}); + lpc_scheme_prover.append_to_batch(2, {0u}); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(4, test_global_alg_rnd_engine)); + lpc_scheme_prover.append_to_batch(3, generate_random_polynomial(9, test_global_alg_rnd_engine)); + + // Commit + std::map commitments; + commitments[0] = lpc_scheme_prover.commit(0); + commitments[1] = lpc_scheme_prover.commit(1); + commitments[2] = lpc_scheme_prover.commit(2); + commitments[3] = lpc_scheme_prover.commit(3); + + // Generate evaluation points. Choose poin1ts outside the domain + auto point = algebra::fields::arithmetic_params::multiplicative_generator; + lpc_scheme_prover.append_eval_point(0, point); + lpc_scheme_prover.append_eval_point(1, point); + lpc_scheme_prover.append_eval_point(2, point); + lpc_scheme_prover.append_eval_point(3, point); + + std::array x_data{}; + + // Prove + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + auto proof = lpc_scheme_prover.proof_eval(transcript); + + // Verify + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + lpc_scheme_verifier.set_batch_size(1, proof.z.get_batch_size(1)); + lpc_scheme_verifier.set_batch_size(2, proof.z.get_batch_size(2)); + lpc_scheme_verifier.set_batch_size(3, proof.z.get_batch_size(3)); + + lpc_scheme_verifier.append_eval_point(0, point); + lpc_scheme_verifier.append_eval_point(1, point); + lpc_scheme_verifier.append_eval_point(2, point); + lpc_scheme_verifier.append_eval_point(3, point); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + + // Check transcript state + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + } + BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/lpc_performance.cpp b/libs/parallel-zk/test/commitment/lpc_performance.cpp index cb524025..fbee5d86 100644 --- a/libs/parallel-zk/test/commitment/lpc_performance.cpp +++ b/libs/parallel-zk/test/commitment/lpc_performance.cpp @@ -59,22 +59,26 @@ namespace boost { namespace tt_detail { template<> struct print_log_value>>>> { - void operator()(std::ostream &, - const nil::crypto3::math::polynomial>>> &) { - } - }; - } // namespace tt_detail - } // namespace test_tools + algebra::fields::detail::element_fp < algebra::fields::params < algebra::fields::bls12_base_field < + 381>>>>> { + void operator()(std::ostream &, + const nil::crypto3::math::polynomial> + + >> &) { + } + }; +} // namespace tt_detail +} // namespace test_tools } // namespace boost template std::vector> generate(NumberType degree) { typedef boost::random::independent_bits_engine - random_polynomial_generator_type; + FieldType::modulus_bits, + typename FieldType::value_type::integral_type> + random_polynomial_generator_type; std::vector> res; @@ -121,266 +125,276 @@ inline std::vector generate_random_step_list(const std::size_t r, c BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite) -BOOST_AUTO_TEST_CASE(step_list_1) { - PROFILE_PLACEHOLDER_SCOPE("LPC step list 1 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - - constexpr static const std::size_t m = 2; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 1), - r, - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } + BOOST_AUTO_TEST_CASE(step_list_1) { + PROFILE_PLACEHOLDER_SCOPE("LPC step list 1 test"); + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + + typedef hashes::keccak_1600<256> merkle_hash_type; + typedef hashes::keccak_1600<256> transcript_hash_type; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t k = 1; + + // It's important parameter + constexpr static const std::size_t d = 1 << 24; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + + constexpr static const std::size_t m = 2; + + typedef zk::commitments::fri fri_type; + typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, + D, + generate_random_step_list(r, 1), + r, + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + typedef boost::random::independent_bits_engine< + boost::random::mt19937, FieldType::modulus_bits, + typename FieldType::value_type::integral_type + > random_polynomial_generator_type; + + std::vector> res; + + // Generate polys + boost::random::random_device rd; // Will be used to obtain a seed for the random number engine + boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() + boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), + std::numeric_limits::max()); + + random_polynomial_generator_type polynomial_element_gen; + std::size_t height = 1; + res.reserve(height); + + for (int i = 0; i < height; i++) { + math::polynomial poly(fri_params.max_degree + 1); + for (int j = 0; j < fri_params.max_degree + 1; j++) { + poly[i] = typename FieldType::value_type(polynomial_element_gen()); + } - std::map commitments; - { - PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0,poly); - commitments[0] = lpc_scheme_prover.commit(0); - } + std::map commitments; + { + PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); + lpc_scheme_prover.append_to_batch(0, poly); + commitments[0] = lpc_scheme_prover.commit(0); + } - typename lpc_scheme_type::proof_type proof; - std::array x_data {}; - { - PROFILE_PLACEHOLDER_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } + typename lpc_scheme_type::proof_type proof; + std::array x_data{}; + { + PROFILE_PLACEHOLDER_SCOPE("proof generation"); + lpc_scheme_prover.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + proof = lpc_scheme_prover.proof_eval(transcript); + } - { - PROFILE_PLACEHOLDER_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + { + PROFILE_PLACEHOLDER_SCOPE("verification"); + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + lpc_scheme_verifier.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + } } } -} - -BOOST_AUTO_TEST_CASE(step_list_3) { - PROFILE_PLACEHOLDER_SCOPE("LPC step list 3 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - constexpr static const std::size_t m = 2; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 3), - r, - lambda - ); - - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } + BOOST_AUTO_TEST_CASE(step_list_3) { + PROFILE_PLACEHOLDER_SCOPE("LPC step list 3 test"); + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + + typedef hashes::keccak_1600<256> merkle_hash_type; + typedef hashes::keccak_1600<256> transcript_hash_type; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t k = 1; + + // It's important parameter + constexpr static const std::size_t d = 1 << 24; + + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + constexpr static const std::size_t m = 2; + + typedef zk::commitments::fri fri_type; + typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, + D, + generate_random_step_list(r, 3), + r, + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + typedef boost::random::independent_bits_engine< + boost::random::mt19937, FieldType::modulus_bits, + typename FieldType::value_type::integral_type + > random_polynomial_generator_type; + + std::vector> res; + + // Generate polys + boost::random::random_device rd; // Will be used to obtain a seed for the random number engine + boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() + boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), + std::numeric_limits::max()); + + random_polynomial_generator_type polynomial_element_gen; + std::size_t height = 1; + res.reserve(height); + + for (int i = 0; i < height; i++) { + math::polynomial poly(fri_params.max_degree + 1); + for (int j = 0; j < fri_params.max_degree + 1; j++) { + poly[i] = typename FieldType::value_type(polynomial_element_gen()); + } - std::map commitments; - { - PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0,poly); - commitments[0] = lpc_scheme_prover.commit(0); - } + std::map commitments; + { + PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); + lpc_scheme_prover.append_to_batch(0, poly); + commitments[0] = lpc_scheme_prover.commit(0); + } - typename lpc_scheme_type::proof_type proof; - std::array x_data {}; - { - PROFILE_PLACEHOLDER_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } + typename lpc_scheme_type::proof_type proof; + std::array x_data{}; + { + PROFILE_PLACEHOLDER_SCOPE("proof generation"); + lpc_scheme_prover.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + proof = lpc_scheme_prover.proof_eval(transcript); + } - { - PROFILE_PLACEHOLDER_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + { + PROFILE_PLACEHOLDER_SCOPE("verification"); + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + lpc_scheme_verifier.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + } } } -} - -BOOST_AUTO_TEST_CASE(step_list_5) { - PROFILE_PLACEHOLDER_SCOPE("LPC step list 5 test"); - typedef algebra::curves::bls12<381> curve_type; - typedef typename curve_type::scalar_field_type FieldType; - - typedef hashes::keccak_1600<256> merkle_hash_type; - typedef hashes::keccak_1600<256> transcript_hash_type; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t k = 1; - - // It's important parameter - constexpr static const std::size_t d = 1 << 24; - constexpr static const std::size_t m = 2; - constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; - - typedef zk::commitments::fri fri_type; - typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; - typedef zk::commitments::list_polynomial_commitment lpc_type; - - constexpr static const std::size_t d_extended = d; - std::size_t extended_log = boost::static_log2::value; - std::vector>> D = - math::calculate_domain_set(extended_log, r); - - typename fri_type::params_type fri_params( - d - 1, - D, - generate_random_step_list(r, 5), - r, - lambda - ); - using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; - lpc_scheme_type lpc_scheme_prover(fri_params); - lpc_scheme_type lpc_scheme_verifier(fri_params); - - typedef boost::random::independent_bits_engine< - boost::random::mt19937, FieldType::modulus_bits, - typename FieldType::value_type::integral_type - > random_polynomial_generator_type; - - std::vector> res; - - // Generate polys - boost::random::random_device rd; // Will be used to obtain a seed for the random number engine - boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() - boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), std::numeric_limits::max()); - - random_polynomial_generator_type polynomial_element_gen; - std::size_t height = 1; - res.reserve(height); - - for (int i = 0; i < height; i++) { - math::polynomial poly(fri_params.max_degree + 1); - for (int j = 0; j < fri_params.max_degree + 1; j++) { - poly[i] = typename FieldType::value_type(polynomial_element_gen()); - } + BOOST_AUTO_TEST_CASE(step_list_5) { + PROFILE_PLACEHOLDER_SCOPE("LPC step list 5 test"); + typedef algebra::curves::bls12<381> curve_type; + typedef typename curve_type::scalar_field_type FieldType; + + typedef hashes::keccak_1600<256> merkle_hash_type; + typedef hashes::keccak_1600<256> transcript_hash_type; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t k = 1; + + // It's important parameter + constexpr static const std::size_t d = 1 << 24; + constexpr static const std::size_t m = 2; + constexpr static const std::size_t r = boost::static_log2<(d - k)>::value; + + typedef zk::commitments::fri fri_type; + typedef zk::commitments::list_polynomial_commitment_params lpc_params_type; + typedef zk::commitments::list_polynomial_commitment lpc_type; + + constexpr static const std::size_t d_extended = d; + std::size_t extended_log = boost::static_log2::value; + std::vector>> D = + math::calculate_domain_set(extended_log, r); + + typename fri_type::params_type fri_params( + d - 1, + D, + generate_random_step_list(r, 5), + r, + lambda + ); + + using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme>; + lpc_scheme_type lpc_scheme_prover(fri_params); + lpc_scheme_type lpc_scheme_verifier(fri_params); + + typedef boost::random::independent_bits_engine< + boost::random::mt19937, FieldType::modulus_bits, + typename FieldType::value_type::integral_type + > random_polynomial_generator_type; + + std::vector> res; + + // Generate polys + boost::random::random_device rd; // Will be used to obtain a seed for the random number engine + boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd() + boost::random::uniform_int_distribution<> distrib(std::numeric_limits::min(), + std::numeric_limits::max()); + + random_polynomial_generator_type polynomial_element_gen; + std::size_t height = 1; + res.reserve(height); + + for (int i = 0; i < height; i++) { + math::polynomial poly(fri_params.max_degree + 1); + for (int j = 0; j < fri_params.max_degree + 1; j++) { + poly[i] = typename FieldType::value_type(polynomial_element_gen()); + } - std::map commitments; - { - PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); - lpc_scheme_prover.append_to_batch(0,poly); - commitments[0] = lpc_scheme_prover.commit(0); - } + std::map commitments; + { + PROFILE_PLACEHOLDER_SCOPE("polynomial commitment"); + lpc_scheme_prover.append_to_batch(0, poly); + commitments[0] = lpc_scheme_prover.commit(0); + } - typename lpc_scheme_type::proof_type proof; - std::array x_data {}; - { - PROFILE_PLACEHOLDER_SCOPE("proof generation"); - lpc_scheme_prover.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); - proof = lpc_scheme_prover.proof_eval(transcript); - } + typename lpc_scheme_type::proof_type proof; + std::array x_data{}; + { + PROFILE_PLACEHOLDER_SCOPE("proof generation"); + lpc_scheme_prover.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + zk::transcript::fiat_shamir_heuristic_sequential transcript(x_data); + proof = lpc_scheme_prover.proof_eval(transcript); + } - { - PROFILE_PLACEHOLDER_SCOPE("verification"); - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); - lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); + { + PROFILE_PLACEHOLDER_SCOPE("verification"); + zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(x_data); + lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0)); - lpc_scheme_verifier.append_eval_point(0, algebra::fields::arithmetic_params::multiplicative_generator); - BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + lpc_scheme_verifier.append_eval_point(0, + algebra::fields::arithmetic_params::multiplicative_generator); + BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier)); + } } } -} + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/libs/parallel-zk/test/commitment/powers_of_tau.cpp b/libs/parallel-zk/test/commitment/powers_of_tau.cpp index aba83f99..ec820748 100644 --- a/libs/parallel-zk/test/commitment/powers_of_tau.cpp +++ b/libs/parallel-zk/test/commitment/powers_of_tau.cpp @@ -15,167 +15,175 @@ using namespace nil::crypto3::zk::commitments; BOOST_AUTO_TEST_SUITE(powers_of_tau_test_suite) -BOOST_AUTO_TEST_CASE(powers_of_tau_result_basic_test) { - using curve_type = curves::bls12<381>; - using g1_value_type = curve_type::g1_type<>::value_type; - using g2_value_type = curve_type::g2_type<>::value_type; - using scalar_field_type = curve_type::scalar_field_type; - - constexpr const unsigned tau_powers = 1 << 5; - - using scheme_type = powers_of_tau; - - auto acc1 = scheme_type::accumulator_type(); - auto acc2 = acc1; - auto sk = scheme_type::generate_private_key(); - auto pk = scheme_type::proof_eval(sk, acc1); - acc2.transform(sk); - - BOOST_CHECK(scheme_type::verify_eval(pk, acc1, acc2)); - auto result = scheme_type::result_type::from_accumulator(acc2, tau_powers); - - auto g1_generator = g1_value_type::one(); - auto g2_generator = g2_value_type::one(); - - BOOST_CHECK(result.alpha_g1 == g1_generator * sk.alpha); - BOOST_CHECK(result.beta_g1 == g1_generator * sk.beta); - BOOST_CHECK(result.beta_g2 == g2_generator * sk.beta); - - BOOST_CHECK_EQUAL(result.coeffs_g1.size(), tau_powers); - BOOST_CHECK_EQUAL(result.coeffs_g2.size(), tau_powers); - BOOST_CHECK_EQUAL(result.alpha_coeffs_g1.size(), tau_powers); - BOOST_CHECK_EQUAL(result.beta_coeffs_g1.size(), tau_powers); - - auto domain = nil::crypto3::math::make_evaluation_domain(tau_powers); - auto u = domain->evaluate_all_lagrange_polynomials(sk.tau); - - for (std::size_t i = 0; i < domain->m; ++i) { - BOOST_CHECK_MESSAGE(result.coeffs_g1[i] == g1_generator * u[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result.coeffs_g2[i] == g2_generator * u[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u[i]), - std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result.beta_coeffs_g1[i] == g1_generator * (sk.beta * u[i]), - std::string("i=") + std::to_string(i)); + BOOST_AUTO_TEST_CASE(powers_of_tau_result_basic_test) { + using curve_type = curves::bls12<381>; + using g1_value_type = curve_type::g1_type<>::value_type; + using g2_value_type = curve_type::g2_type<>::value_type; + using scalar_field_type = curve_type::scalar_field_type; + + constexpr const unsigned tau_powers = 1 << 5; + + using scheme_type = powers_of_tau; + + auto acc1 = scheme_type::accumulator_type(); + auto acc2 = acc1; + auto sk = scheme_type::generate_private_key(); + auto pk = scheme_type::proof_eval(sk, acc1); + acc2.transform(sk); + + BOOST_CHECK(scheme_type::verify_eval(pk, acc1, acc2)); + auto result = scheme_type::result_type::from_accumulator(acc2, tau_powers); + + auto g1_generator = g1_value_type::one(); + auto g2_generator = g2_value_type::one(); + + BOOST_CHECK(result.alpha_g1 == g1_generator * sk.alpha); + BOOST_CHECK(result.beta_g1 == g1_generator * sk.beta); + BOOST_CHECK(result.beta_g2 == g2_generator * sk.beta); + + BOOST_CHECK_EQUAL(result.coeffs_g1.size(), tau_powers); + BOOST_CHECK_EQUAL(result.coeffs_g2.size(), tau_powers); + BOOST_CHECK_EQUAL(result.alpha_coeffs_g1.size(), tau_powers); + BOOST_CHECK_EQUAL(result.beta_coeffs_g1.size(), tau_powers); + + auto domain = nil::crypto3::math::make_evaluation_domain(tau_powers); + auto u = domain->evaluate_all_lagrange_polynomials(sk.tau); + + for (std::size_t i = 0; i < domain->m; ++i) { + BOOST_CHECK_MESSAGE(result.coeffs_g1[i] == g1_generator * u[i], std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result.coeffs_g2[i] == g2_generator * u[i], std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u[i]), + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result.beta_coeffs_g1[i] == g1_generator * (sk.beta * u[i]), + std::string("i=") + std::to_string(i)); + } + + BOOST_CHECK_EQUAL(result.h.size(), domain->m - 1); + auto Zt = domain->compute_vanishing_polynomial(sk.tau); + for (std::size_t i = 0; i < domain->m - 1; ++i) { + BOOST_CHECK_MESSAGE(result.h[i] == g1_generator * (sk.tau.pow(i) * Zt), + std::string("i=") + std::to_string(i)); + } + + auto result_16 = scheme_type::result_type::from_accumulator(acc2, 16); + auto domain_16 = nil::crypto3::math::make_evaluation_domain(16); + auto u_16 = domain_16->evaluate_all_lagrange_polynomials(sk.tau); + + BOOST_CHECK_EQUAL(u_16.size(), 16); + + BOOST_CHECK(result_16.alpha_g1 == g1_generator * sk.alpha); + BOOST_CHECK(result_16.beta_g1 == g1_generator * sk.beta); + BOOST_CHECK(result_16.beta_g2 == g2_generator * sk.beta); + + BOOST_CHECK_EQUAL(result_16.coeffs_g1.size(), 16); + BOOST_CHECK_EQUAL(result_16.coeffs_g2.size(), 16); + BOOST_CHECK_EQUAL(result_16.alpha_coeffs_g1.size(), 16); + BOOST_CHECK_EQUAL(result_16.beta_coeffs_g1.size(), 16); + + for (std::size_t i = 0; i < domain_16->m; ++i) { + BOOST_CHECK_MESSAGE(result_16.coeffs_g1[i] == g1_generator * u_16[i], + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_16.coeffs_g2[i] == g2_generator * u_16[i], + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_16.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u_16[i]), + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_16.beta_coeffs_g1[i] == g1_generator * (sk.beta * u_16[i]), + std::string("i=") + std::to_string(i)); + } + + BOOST_CHECK_EQUAL(result_16.h.size(), domain_16->m - 1); + auto Zt_16 = domain_16->compute_vanishing_polynomial(sk.tau); + for (std::size_t i = 0; i < domain_16->m - 1; ++i) { + BOOST_CHECK_MESSAGE(result_16.h[i] == g1_generator * (sk.tau.pow(i) * Zt_16), + std::string("i=") + std::to_string(i)); + } + + auto result_24 = scheme_type::result_type::from_accumulator(acc2, 24); + auto domain_24 = nil::crypto3::math::make_evaluation_domain(24); + auto u_24 = domain_24->evaluate_all_lagrange_polynomials(sk.tau); + + BOOST_CHECK_EQUAL(u_24.size(), 24); + + BOOST_CHECK(result_24.alpha_g1 == g1_generator * sk.alpha); + BOOST_CHECK(result_24.beta_g1 == g1_generator * sk.beta); + BOOST_CHECK(result_24.beta_g2 == g2_generator * sk.beta); + + BOOST_CHECK_EQUAL(result_24.coeffs_g1.size(), 24); + BOOST_CHECK_EQUAL(result_24.coeffs_g2.size(), 24); + BOOST_CHECK_EQUAL(result_24.alpha_coeffs_g1.size(), 24); + BOOST_CHECK_EQUAL(result_24.beta_coeffs_g1.size(), 24); + + for (std::size_t i = 0; i < domain_24->m; ++i) { + BOOST_CHECK_MESSAGE(result_24.coeffs_g1[i] == g1_generator * u_24[i], + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_24.coeffs_g2[i] == g2_generator * u_24[i], + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_24.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u_24[i]), + std::string("i=") + std::to_string(i)); + BOOST_CHECK_MESSAGE(result_24.beta_coeffs_g1[i] == g1_generator * (sk.beta * u_24[i]), + std::string("i=") + std::to_string(i)); + } + + BOOST_CHECK_EQUAL(result_24.h.size(), domain_24->m - 1); + auto Zt_24 = domain_24->compute_vanishing_polynomial(sk.tau); + for (std::size_t i = 0; i < domain_24->m - 1; ++i) { + BOOST_CHECK_MESSAGE(result_24.h[i] == g1_generator * (sk.tau.pow(i) * Zt_24), + std::string("i=") + std::to_string(i)); + } } - BOOST_CHECK_EQUAL(result.h.size(), domain->m - 1); - auto Zt = domain->compute_vanishing_polynomial(sk.tau); - for (std::size_t i = 0; i < domain->m - 1; ++i) { - BOOST_CHECK_MESSAGE(result.h[i] == g1_generator * (sk.tau.pow(i) * Zt), std::string("i=") + std::to_string(i)); + BOOST_AUTO_TEST_CASE(powers_of_tau_basic_test) { + using curve_type = curves::bls12<381>; + using scheme_type = powers_of_tau; + auto acc1 = scheme_type::accumulator_type(); + auto acc2 = acc1; + auto sk = scheme_type::generate_private_key(); + auto pubkey = scheme_type::proof_eval(sk, acc2); + acc2.transform(sk); + + BOOST_CHECK(acc1.tau_powers_g1[0] == acc2.tau_powers_g1[0]); + BOOST_CHECK(scheme_type::verify_eval(pubkey, acc1, acc2)); + auto acc3 = acc2; + std::vector beacon{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + auto rng = scheme_type::rng_from_beacon(beacon); + auto beacon_sk = scheme_type::generate_private_key(rng); + auto beacon_pubkey = scheme_type::proof_eval(beacon_sk, acc2, rng); + acc3.transform(beacon_sk); + BOOST_CHECK(scheme_type::verify_eval(beacon_pubkey, acc2, acc3)); + + // Check reproducibility + auto rng_reproduced = scheme_type::rng_from_beacon(beacon); + auto beacon_sk_reproduced = scheme_type::generate_private_key(rng_reproduced); + BOOST_CHECK(beacon_sk.tau == beacon_sk_reproduced.tau); + BOOST_CHECK(beacon_sk.alpha == beacon_sk_reproduced.alpha); + BOOST_CHECK(beacon_sk.beta == beacon_sk_reproduced.beta); + auto beacon_pubkey_reproduced = scheme_type::proof_eval(beacon_sk_reproduced, acc2, rng_reproduced); + BOOST_CHECK(scheme_type::verify_eval(beacon_pubkey_reproduced, acc2, acc3)); + + auto result = scheme_type::result_type::from_accumulator(acc3, 32); } - auto result_16 = scheme_type::result_type::from_accumulator(acc2, 16); - auto domain_16 = nil::crypto3::math::make_evaluation_domain(16); - auto u_16 = domain_16->evaluate_all_lagrange_polynomials(sk.tau); - - BOOST_CHECK_EQUAL(u_16.size(), 16); - - BOOST_CHECK(result_16.alpha_g1 == g1_generator * sk.alpha); - BOOST_CHECK(result_16.beta_g1 == g1_generator * sk.beta); - BOOST_CHECK(result_16.beta_g2 == g2_generator * sk.beta); - - BOOST_CHECK_EQUAL(result_16.coeffs_g1.size(), 16); - BOOST_CHECK_EQUAL(result_16.coeffs_g2.size(), 16); - BOOST_CHECK_EQUAL(result_16.alpha_coeffs_g1.size(), 16); - BOOST_CHECK_EQUAL(result_16.beta_coeffs_g1.size(), 16); - - for (std::size_t i = 0; i < domain_16->m; ++i) { - BOOST_CHECK_MESSAGE(result_16.coeffs_g1[i] == g1_generator * u_16[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_16.coeffs_g2[i] == g2_generator * u_16[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_16.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u_16[i]), - std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_16.beta_coeffs_g1[i] == g1_generator * (sk.beta * u_16[i]), - std::string("i=") + std::to_string(i)); - } - - BOOST_CHECK_EQUAL(result_16.h.size(), domain_16->m - 1); - auto Zt_16 = domain_16->compute_vanishing_polynomial(sk.tau); - for (std::size_t i = 0; i < domain_16->m - 1; ++i) { - BOOST_CHECK_MESSAGE(result_16.h[i] == g1_generator * (sk.tau.pow(i) * Zt_16), - std::string("i=") + std::to_string(i)); - } - - auto result_24 = scheme_type::result_type::from_accumulator(acc2, 24); - auto domain_24 = nil::crypto3::math::make_evaluation_domain(24); - auto u_24 = domain_24->evaluate_all_lagrange_polynomials(sk.tau); - - BOOST_CHECK_EQUAL(u_24.size(), 24); - - BOOST_CHECK(result_24.alpha_g1 == g1_generator * sk.alpha); - BOOST_CHECK(result_24.beta_g1 == g1_generator * sk.beta); - BOOST_CHECK(result_24.beta_g2 == g2_generator * sk.beta); - - BOOST_CHECK_EQUAL(result_24.coeffs_g1.size(), 24); - BOOST_CHECK_EQUAL(result_24.coeffs_g2.size(), 24); - BOOST_CHECK_EQUAL(result_24.alpha_coeffs_g1.size(), 24); - BOOST_CHECK_EQUAL(result_24.beta_coeffs_g1.size(), 24); - - for (std::size_t i = 0; i < domain_24->m; ++i) { - BOOST_CHECK_MESSAGE(result_24.coeffs_g1[i] == g1_generator * u_24[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_24.coeffs_g2[i] == g2_generator * u_24[i], std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_24.alpha_coeffs_g1[i] == g1_generator * (sk.alpha * u_24[i]), - std::string("i=") + std::to_string(i)); - BOOST_CHECK_MESSAGE(result_24.beta_coeffs_g1[i] == g1_generator * (sk.beta * u_24[i]), - std::string("i=") + std::to_string(i)); - } - - BOOST_CHECK_EQUAL(result_24.h.size(), domain_24->m - 1); - auto Zt_24 = domain_24->compute_vanishing_polynomial(sk.tau); - for (std::size_t i = 0; i < domain_24->m - 1; ++i) { - BOOST_CHECK_MESSAGE(result_24.h[i] == g1_generator * (sk.tau.pow(i) * Zt_24), - std::string("i=") + std::to_string(i)); + BOOST_AUTO_TEST_CASE(keypair_generation_basic_test) { + using curve_type = curves::bls12<381>; + using scheme_type = powers_of_tau; + auto sk = scheme_type::generate_private_key(); + auto acc = scheme_type::accumulator_type(); + + std::vector transcript = scheme_type::compute_transcript(acc); + + auto pubkey = scheme_type::proof_eval(sk, acc); + auto tau_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.tau_pok.g1_s, + pubkey.tau_pok.g1_s_x, + transcript, 0); + BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.tau_pok, tau_gs2)); + auto alpha_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.alpha_pok.g1_s, + pubkey.alpha_pok.g1_s_x, transcript, + 1); + BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.alpha_pok, alpha_gs2)); + auto beta_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.beta_pok.g1_s, + pubkey.beta_pok.g1_s_x, transcript, + 2); + BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.beta_pok, beta_gs2)); } -} - -BOOST_AUTO_TEST_CASE(powers_of_tau_basic_test) { - using curve_type = curves::bls12<381>; - using scheme_type = powers_of_tau; - auto acc1 = scheme_type::accumulator_type(); - auto acc2 = acc1; - auto sk = scheme_type::generate_private_key(); - auto pubkey = scheme_type::proof_eval(sk, acc2); - acc2.transform(sk); - - BOOST_CHECK(acc1.tau_powers_g1[0] == acc2.tau_powers_g1[0]); - BOOST_CHECK(scheme_type::verify_eval(pubkey, acc1, acc2)); - auto acc3 = acc2; - std::vector beacon {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - auto rng = scheme_type::rng_from_beacon(beacon); - auto beacon_sk = scheme_type::generate_private_key(rng); - auto beacon_pubkey = scheme_type::proof_eval(beacon_sk, acc2, rng); - acc3.transform(beacon_sk); - BOOST_CHECK(scheme_type::verify_eval(beacon_pubkey, acc2, acc3)); - - // Check reproducibility - auto rng_reproduced = scheme_type::rng_from_beacon(beacon); - auto beacon_sk_reproduced = scheme_type::generate_private_key(rng_reproduced); - BOOST_CHECK(beacon_sk.tau == beacon_sk_reproduced.tau); - BOOST_CHECK(beacon_sk.alpha == beacon_sk_reproduced.alpha); - BOOST_CHECK(beacon_sk.beta == beacon_sk_reproduced.beta); - auto beacon_pubkey_reproduced = scheme_type::proof_eval(beacon_sk_reproduced, acc2, rng_reproduced); - BOOST_CHECK(scheme_type::verify_eval(beacon_pubkey_reproduced, acc2, acc3)); - - auto result = scheme_type::result_type::from_accumulator(acc3, 32); -} - -BOOST_AUTO_TEST_CASE(keypair_generation_basic_test) { - using curve_type = curves::bls12<381>; - using scheme_type = powers_of_tau; - auto sk = scheme_type::generate_private_key(); - auto acc = scheme_type::accumulator_type(); - - std::vector transcript = scheme_type::compute_transcript(acc); - - auto pubkey = scheme_type::proof_eval(sk, acc); - auto tau_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.tau_pok.g1_s, pubkey.tau_pok.g1_s_x, - transcript, 0); - BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.tau_pok, tau_gs2)); - auto alpha_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.alpha_pok.g1_s, - pubkey.alpha_pok.g1_s_x, transcript, 1); - BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.alpha_pok, alpha_gs2)); - auto beta_gs2 = scheme_type::proof_of_knowledge_scheme_type::compute_g2_s(pubkey.beta_pok.g1_s, - pubkey.beta_pok.g1_s_x, transcript, 2); - BOOST_CHECK(scheme_type::proof_of_knowledge_scheme_type::verify_eval(pubkey.beta_pok, beta_gs2)); -} BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/proof_of_knowledge.cpp b/libs/parallel-zk/test/commitment/proof_of_knowledge.cpp index 48050a8a..37088095 100644 --- a/libs/parallel-zk/test/commitment/proof_of_knowledge.cpp +++ b/libs/parallel-zk/test/commitment/proof_of_knowledge.cpp @@ -15,18 +15,18 @@ using namespace nil::crypto3::zk::commitments; BOOST_AUTO_TEST_SUITE(proof_of_knowledge_test_suite) -BOOST_AUTO_TEST_CASE(pok_basic_test) { - using curve_type = curves::bls12<381>; - using scalar_field_type = curve_type::scalar_field_type; - using scheme_type = proof_of_knowledge; - std::vector transcript(64, 1); - scalar_field_type::value_type a = random_element(); - - auto a_pok = scheme_type::proof_eval(a, transcript, 0); - - auto a_gs2 = scheme_type::compute_g2_s(a_pok.g1_s, a_pok.g1_s_x, transcript, 0); - BOOST_CHECK(scheme_type::verify_eval(a_pok, a_gs2)); - BOOST_CHECK(scheme_type::verify_eval(a_pok, transcript, 0)); -} + BOOST_AUTO_TEST_CASE(pok_basic_test) { + using curve_type = curves::bls12<381>; + using scalar_field_type = curve_type::scalar_field_type; + using scheme_type = proof_of_knowledge; + std::vector transcript(64, 1); + scalar_field_type::value_type a = random_element(); + + auto a_pok = scheme_type::proof_eval(a, transcript, 0); + + auto a_gs2 = scheme_type::compute_g2_s(a_pok.g1_s, a_pok.g1_s_x, transcript, 0); + BOOST_CHECK(scheme_type::verify_eval(a_pok, a_gs2)); + BOOST_CHECK(scheme_type::verify_eval(a_pok, transcript, 0)); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/proof_of_work.cpp b/libs/parallel-zk/test/commitment/proof_of_work.cpp index b9efa0b1..ae7f51fc 100644 --- a/libs/parallel-zk/test/commitment/proof_of_work.cpp +++ b/libs/parallel-zk/test/commitment/proof_of_work.cpp @@ -43,55 +43,55 @@ using namespace nil::crypto3::zk::commitments; BOOST_AUTO_TEST_SUITE(proof_of_knowledge_test_suite) -BOOST_AUTO_TEST_CASE(pow_poseidon_basic_test) { - using curve_type = curves::pallas; - using field_type = curve_type::base_field_type; - using integral_type = typename field_type::integral_type; - using policy = nil::crypto3::hashes::detail::mina_poseidon_policy; - using poseidon = nil::crypto3::hashes::poseidon; - using pow_type = nil::crypto3::zk::commitments::field_proof_of_work; - - const integral_type expected_mask = integral_type(0xFF80000000000000) << (field_type::modulus_bits - 64); - nil::crypto3::zk::transcript::fiat_shamir_heuristic_sequential transcript; - auto old_transcript_1 = transcript, old_transcript_2 = transcript; - - auto result = pow_type::generate(transcript, 9); - BOOST_ASSERT(pow_type::verify(old_transcript_1, result, 9)); - - // manually reimplement verify to ensure that changes in implementation didn't break it - old_transcript_2(result); - auto chal = old_transcript_2.template challenge(); - BOOST_ASSERT((integral_type(chal.data) & expected_mask) == 0); - - using hard_pow_type = nil::crypto3::zk::commitments::field_proof_of_work; - // check that random stuff doesn't pass verify - BOOST_ASSERT(!hard_pow_type::verify(old_transcript_1, result, 9)); -} - -BOOST_AUTO_TEST_CASE(pow_basic_test) { - using keccak = nil::crypto3::hashes::keccak_1600<512>; - const std::uint32_t mask = 0xFFFFF000; - using pow_type = nil::crypto3::zk::commitments::proof_of_work; - - nil::crypto3::zk::transcript::fiat_shamir_heuristic_sequential transcript; - auto old_transcript_1 = transcript, old_transcript_2 = transcript; - - auto result = pow_type::generate(transcript, mask); - BOOST_ASSERT(pow_type::verify(old_transcript_1, result, mask)); - - // manually reimplement verify to ensure that changes in implementation didn't break it - std::array bytes; - bytes[0] = std::uint8_t((result & 0xFF000000) >> 24); - bytes[1] = std::uint8_t((result & 0x00FF0000) >> 16); - bytes[2] = std::uint8_t((result & 0x0000FF00) >> 8); - bytes[3] = std::uint8_t(result & 0x000000FF); - old_transcript_2(bytes); - auto chal = old_transcript_2.template int_challenge(); - BOOST_ASSERT((chal & mask) == 0); - - // check that random stuff doesn't pass verify - using hard_pow_type = nil::crypto3::zk::commitments::proof_of_work; - BOOST_ASSERT(!hard_pow_type::verify(old_transcript_1, result, mask)); -} + BOOST_AUTO_TEST_CASE(pow_poseidon_basic_test) { + using curve_type = curves::pallas; + using field_type = curve_type::base_field_type; + using integral_type = typename field_type::integral_type; + using policy = nil::crypto3::hashes::detail::mina_poseidon_policy; + using poseidon = nil::crypto3::hashes::poseidon; + using pow_type = nil::crypto3::zk::commitments::field_proof_of_work; + + const integral_type expected_mask = integral_type(0xFF80000000000000) << (field_type::modulus_bits - 64); + nil::crypto3::zk::transcript::fiat_shamir_heuristic_sequential transcript; + auto old_transcript_1 = transcript, old_transcript_2 = transcript; + + auto result = pow_type::generate(transcript, 9); + BOOST_ASSERT(pow_type::verify(old_transcript_1, result, 9)); + + // manually reimplement verify to ensure that changes in implementation didn't break it + old_transcript_2(result); + auto chal = old_transcript_2.template challenge(); + BOOST_ASSERT((integral_type(chal.data) & expected_mask) == 0); + + using hard_pow_type = nil::crypto3::zk::commitments::field_proof_of_work; + // check that random stuff doesn't pass verify + BOOST_ASSERT(!hard_pow_type::verify(old_transcript_1, result, 9)); + } + + BOOST_AUTO_TEST_CASE(pow_basic_test) { + using keccak = nil::crypto3::hashes::keccak_1600<512>; + const std::uint32_t mask = 0xFFFFF000; + using pow_type = nil::crypto3::zk::commitments::proof_of_work; + + nil::crypto3::zk::transcript::fiat_shamir_heuristic_sequential transcript; + auto old_transcript_1 = transcript, old_transcript_2 = transcript; + + auto result = pow_type::generate(transcript, mask); + BOOST_ASSERT(pow_type::verify(old_transcript_1, result, mask)); + + // manually reimplement verify to ensure that changes in implementation didn't break it + std::array bytes; + bytes[0] = std::uint8_t((result & 0xFF000000) >> 24); + bytes[1] = std::uint8_t((result & 0x00FF0000) >> 16); + bytes[2] = std::uint8_t((result & 0x0000FF00) >> 8); + bytes[3] = std::uint8_t(result & 0x000000FF); + old_transcript_2(bytes); + auto chal = old_transcript_2.template int_challenge(); + BOOST_ASSERT((chal & mask) == 0); + + // check that random stuff doesn't pass verify + using hard_pow_type = nil::crypto3::zk::commitments::proof_of_work; + BOOST_ASSERT(!hard_pow_type::verify(old_transcript_1, result, mask)); + } BOOST_AUTO_TEST_SUITE_END() diff --git a/libs/parallel-zk/test/commitment/r1cs_gg_ppzksnark_mpc.cpp b/libs/parallel-zk/test/commitment/r1cs_gg_ppzksnark_mpc.cpp index 67813352..aa17de0c 100644 --- a/libs/parallel-zk/test/commitment/r1cs_gg_ppzksnark_mpc.cpp +++ b/libs/parallel-zk/test/commitment/r1cs_gg_ppzksnark_mpc.cpp @@ -22,138 +22,142 @@ using namespace nil::crypto3::zk::commitments; BOOST_AUTO_TEST_SUITE(mpc_generator_test_suite) -BOOST_AUTO_TEST_CASE(mpc_generator_compare_keypairs_without_delta_contribution_test) { - - using curve_type = curves::bls12<381>; - using scalar_field_type = curve_type::scalar_field_type; - using scalar_field_value_type = scalar_field_type::value_type; - - using g1_value_type = curve_type::g1_type<>::value_type; - using g2_value_type = curve_type::g2_type<>::value_type; - using powers_of_tau_scheme_type = powers_of_tau; - using proving_scheme_type = r1cs_gg_ppzksnark; - using proving_scheme_generator_type = r1cs_gg_ppzksnark_generator; - - auto acc = powers_of_tau_scheme_type::accumulator_type(); - auto sk = powers_of_tau_scheme_type::generate_private_key(); - auto pk = powers_of_tau_scheme_type::proof_eval(sk, acc); - acc.transform(sk); - - auto r1cs_example = generate_r1cs_example_with_field_input(20, 3); - - std::size_t m = r1cs_example.constraint_system.num_constraints() + r1cs_example.constraint_system.num_inputs() + 1; - - auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, m); - - auto mpc_kp = - commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, result); - auto g1_generator = g1_value_type::one(); - auto g2_generator = g2_value_type::one(); - - auto [alpha_g1, beta_g1, beta_g2, delta_g1, delta_g2, gamma_g2, A_query, B_query, H_query, L_query, r1cs_copy, - alpha_g1_beta_g2, gamma_ABC_g1, gamma_g1] = - proving_scheme_generator_type::deterministic_basic_process( - r1cs_example.constraint_system, sk.tau, sk.alpha, sk.beta, scalar_field_value_type::one(), - scalar_field_value_type::one(), g1_generator, g2_generator); - - BOOST_CHECK(mpc_kp.first.alpha_g1 == alpha_g1); - BOOST_CHECK(mpc_kp.first.beta_g1 == beta_g1); - BOOST_CHECK(mpc_kp.first.beta_g2 == beta_g2); - BOOST_CHECK(mpc_kp.first.delta_g1 == delta_g1); - BOOST_CHECK(mpc_kp.first.delta_g2 == delta_g2); - - BOOST_CHECK_EQUAL(mpc_kp.first.A_query.size(), A_query.size()); - BOOST_CHECK_EQUAL(mpc_kp.first.B_query.domain_size(), B_query.domain_size()); - BOOST_CHECK_EQUAL(mpc_kp.first.H_query.size(), H_query.size()); - BOOST_CHECK_EQUAL(mpc_kp.first.L_query.size(), L_query.size()); - - for (std::size_t i = 0; i < A_query.size(); ++i) { - BOOST_CHECK_MESSAGE(mpc_kp.first.A_query[i] == A_query[i], std::string("i=") + std::to_string(i)); - } - for (std::size_t i = 0; i < B_query.domain_size(); ++i) { - BOOST_CHECK_MESSAGE(mpc_kp.first.B_query[i] == B_query[i], std::string("i=") + std::to_string(i)); - } - for (std::size_t i = 0; i < H_query.size(); ++i) { - BOOST_CHECK_MESSAGE(mpc_kp.first.H_query[i] == H_query[i], std::string("i=") + std::to_string(i)); - } - for (std::size_t i = 0; i < L_query.size(); ++i) { - BOOST_CHECK_MESSAGE(mpc_kp.first.L_query[i] == L_query[i], std::string("i=") + std::to_string(i)); + BOOST_AUTO_TEST_CASE(mpc_generator_compare_keypairs_without_delta_contribution_test) { + + using curve_type = curves::bls12<381>; + using scalar_field_type = curve_type::scalar_field_type; + using scalar_field_value_type = scalar_field_type::value_type; + + using g1_value_type = curve_type::g1_type<>::value_type; + using g2_value_type = curve_type::g2_type<>::value_type; + using powers_of_tau_scheme_type = powers_of_tau; + using proving_scheme_type = r1cs_gg_ppzksnark; + using proving_scheme_generator_type = r1cs_gg_ppzksnark_generator; + + auto acc = powers_of_tau_scheme_type::accumulator_type(); + auto sk = powers_of_tau_scheme_type::generate_private_key(); + auto pk = powers_of_tau_scheme_type::proof_eval(sk, acc); + acc.transform(sk); + + auto r1cs_example = generate_r1cs_example_with_field_input(20, 3); + + std::size_t m = + r1cs_example.constraint_system.num_constraints() + r1cs_example.constraint_system.num_inputs() + 1; + + auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, m); + + auto mpc_kp = + commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, + result); + auto g1_generator = g1_value_type::one(); + auto g2_generator = g2_value_type::one(); + + auto [alpha_g1, beta_g1, beta_g2, delta_g1, delta_g2, gamma_g2, A_query, B_query, H_query, L_query, r1cs_copy, + alpha_g1_beta_g2, gamma_ABC_g1, gamma_g1] = + proving_scheme_generator_type::deterministic_basic_process( + r1cs_example.constraint_system, sk.tau, sk.alpha, sk.beta, scalar_field_value_type::one(), + scalar_field_value_type::one(), g1_generator, g2_generator); + + BOOST_CHECK(mpc_kp.first.alpha_g1 == alpha_g1); + BOOST_CHECK(mpc_kp.first.beta_g1 == beta_g1); + BOOST_CHECK(mpc_kp.first.beta_g2 == beta_g2); + BOOST_CHECK(mpc_kp.first.delta_g1 == delta_g1); + BOOST_CHECK(mpc_kp.first.delta_g2 == delta_g2); + + BOOST_CHECK_EQUAL(mpc_kp.first.A_query.size(), A_query.size()); + BOOST_CHECK_EQUAL(mpc_kp.first.B_query.domain_size(), B_query.domain_size()); + BOOST_CHECK_EQUAL(mpc_kp.first.H_query.size(), H_query.size()); + BOOST_CHECK_EQUAL(mpc_kp.first.L_query.size(), L_query.size()); + + for (std::size_t i = 0; i < A_query.size(); ++i) { + BOOST_CHECK_MESSAGE(mpc_kp.first.A_query[i] == A_query[i], std::string("i=") + std::to_string(i)); + } + for (std::size_t i = 0; i < B_query.domain_size(); ++i) { + BOOST_CHECK_MESSAGE(mpc_kp.first.B_query[i] == B_query[i], std::string("i=") + std::to_string(i)); + } + for (std::size_t i = 0; i < H_query.size(); ++i) { + BOOST_CHECK_MESSAGE(mpc_kp.first.H_query[i] == H_query[i], std::string("i=") + std::to_string(i)); + } + for (std::size_t i = 0; i < L_query.size(); ++i) { + BOOST_CHECK_MESSAGE(mpc_kp.first.L_query[i] == L_query[i], std::string("i=") + std::to_string(i)); + } + + BOOST_CHECK(mpc_kp.second.alpha_g1_beta_g2 == alpha_g1_beta_g2); + BOOST_CHECK(mpc_kp.second.gamma_g2 == gamma_g2); + BOOST_CHECK(mpc_kp.second.delta_g2 == delta_g2); + + BOOST_CHECK(mpc_kp.second.gamma_ABC_g1.first == gamma_ABC_g1.first); + + BOOST_CHECK_EQUAL(mpc_kp.second.gamma_ABC_g1.rest.domain_size(), gamma_ABC_g1.rest.domain_size()); + + for (std::size_t i = 0; i < gamma_ABC_g1.rest.domain_size(); ++i) { + BOOST_CHECK_MESSAGE(mpc_kp.second.gamma_ABC_g1.rest[i] == gamma_ABC_g1.rest[i], + std::string("i=") + std::to_string(i)); + } + + auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); + auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); + BOOST_CHECK(verification_result); } - BOOST_CHECK(mpc_kp.second.alpha_g1_beta_g2 == alpha_g1_beta_g2); - BOOST_CHECK(mpc_kp.second.gamma_g2 == gamma_g2); - BOOST_CHECK(mpc_kp.second.delta_g2 == delta_g2); + BOOST_AUTO_TEST_CASE(mpc_generator_proof_verification_without_delta_contribution_test) { - BOOST_CHECK(mpc_kp.second.gamma_ABC_g1.first == gamma_ABC_g1.first); - - BOOST_CHECK_EQUAL(mpc_kp.second.gamma_ABC_g1.rest.domain_size(), gamma_ABC_g1.rest.domain_size()); - - for (std::size_t i = 0; i < gamma_ABC_g1.rest.domain_size(); ++i) { - BOOST_CHECK_MESSAGE(mpc_kp.second.gamma_ABC_g1.rest[i] == gamma_ABC_g1.rest[i], - std::string("i=") + std::to_string(i)); - } + using curve_type = curves::bls12<381>; + using powers_of_tau_scheme_type = powers_of_tau; + using proving_scheme_type = r1cs_gg_ppzksnark; - auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); - auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); - BOOST_CHECK(verification_result); -} + auto acc = powers_of_tau_scheme_type::accumulator_type(); + auto sk = powers_of_tau_scheme_type::generate_private_key(); + auto pk = powers_of_tau_scheme_type::proof_eval(sk, acc); + acc.transform(sk); -BOOST_AUTO_TEST_CASE(mpc_generator_proof_verification_without_delta_contribution_test) { + auto r1cs_example = generate_r1cs_example_with_field_input(20, 5); + auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, 32); - using curve_type = curves::bls12<381>; - using powers_of_tau_scheme_type = powers_of_tau; - using proving_scheme_type = r1cs_gg_ppzksnark; + auto mpc_kp = + commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, + result); - auto acc = powers_of_tau_scheme_type::accumulator_type(); - auto sk = powers_of_tau_scheme_type::generate_private_key(); - auto pk = powers_of_tau_scheme_type::proof_eval(sk, acc); - acc.transform(sk); - - auto r1cs_example = generate_r1cs_example_with_field_input(20, 5); - auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, 32); - - auto mpc_kp = - commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, result); - - auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); - auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); - BOOST_CHECK(verification_result); -} + auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); + auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); + BOOST_CHECK(verification_result); + } -BOOST_AUTO_TEST_CASE(mpc_generator_proof_verification_with_delta_contribution_test) { + BOOST_AUTO_TEST_CASE(mpc_generator_proof_verification_with_delta_contribution_test) { - using curve_type = curves::bls12<381>; - using powers_of_tau_scheme_type = powers_of_tau; - using proving_scheme_type = r1cs_gg_ppzksnark; - using crs_mpc_type = r1cs_gg_ppzksnark_mpc; - using public_key_type = crs_mpc_type::public_key_type; + using curve_type = curves::bls12<381>; + using powers_of_tau_scheme_type = powers_of_tau; + using proving_scheme_type = r1cs_gg_ppzksnark; + using crs_mpc_type = r1cs_gg_ppzksnark_mpc; + using public_key_type = crs_mpc_type::public_key_type; - auto acc = powers_of_tau_scheme_type::accumulator_type(); - auto pot_sk = powers_of_tau_scheme_type::generate_private_key(); - auto pot_pk = powers_of_tau_scheme_type::proof_eval(pot_sk, acc); - acc.transform(pot_sk); - auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, 32); + auto acc = powers_of_tau_scheme_type::accumulator_type(); + auto pot_sk = powers_of_tau_scheme_type::generate_private_key(); + auto pot_pk = powers_of_tau_scheme_type::proof_eval(pot_sk, acc); + acc.transform(pot_sk); + auto result = powers_of_tau_scheme_type::result_type::from_accumulator(acc, 32); - auto r1cs_example = generate_r1cs_example_with_field_input(20, 5); + auto r1cs_example = generate_r1cs_example_with_field_input(20, 5); - auto mpc_kp = - commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, result); + auto mpc_kp = + commitments::detail::make_r1cs_gg_ppzksnark_keypair_from_powers_of_tau(r1cs_example.constraint_system, + result); - std::vector pks; + std::vector pks; - auto mpc_sk1 = crs_mpc_type::generate_private_key(); - pks.emplace_back(crs_mpc_type::proof_eval(mpc_sk1, boost::none, mpc_kp)); - commitments::detail::transform_keypair(mpc_kp, mpc_sk1); - BOOST_CHECK(crs_mpc_type::verify_eval(mpc_kp, pks, r1cs_example.constraint_system, result)); + auto mpc_sk1 = crs_mpc_type::generate_private_key(); + pks.emplace_back(crs_mpc_type::proof_eval(mpc_sk1, boost::none, mpc_kp)); + commitments::detail::transform_keypair(mpc_kp, mpc_sk1); + BOOST_CHECK(crs_mpc_type::verify_eval(mpc_kp, pks, r1cs_example.constraint_system, result)); - auto mpc_sk2 = crs_mpc_type::generate_private_key(); - pks.emplace_back(crs_mpc_type::proof_eval(mpc_sk2, pks[0], mpc_kp)); - commitments::detail::transform_keypair(mpc_kp, mpc_sk2); - BOOST_CHECK(crs_mpc_type::verify_eval(mpc_kp, pks, r1cs_example.constraint_system, result)); + auto mpc_sk2 = crs_mpc_type::generate_private_key(); + pks.emplace_back(crs_mpc_type::proof_eval(mpc_sk2, pks[0], mpc_kp)); + commitments::detail::transform_keypair(mpc_kp, mpc_sk2); + BOOST_CHECK(crs_mpc_type::verify_eval(mpc_kp, pks, r1cs_example.constraint_system, result)); - auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); - auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); - BOOST_CHECK(verification_result); -} + auto proof = proving_scheme_type::prove(mpc_kp.first, r1cs_example.primary_input, r1cs_example.auxiliary_input); + auto verification_result = proving_scheme_type::verify(mpc_kp.second, r1cs_example.primary_input, proof); + BOOST_CHECK(verification_result); + } BOOST_AUTO_TEST_SUITE_END() From 828b994873f2e522f1e47c8382b0e939173bbfb7 Mon Sep 17 00:00:00 2001 From: Martun Karapetyan Date: Fri, 7 Jun 2024 12:38:22 +0000 Subject: [PATCH 4/4] Fix equality checks for modular numbers in multiprecision. --- libs/parallel-math/test/polynomial_dfs.cpp | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libs/parallel-math/test/polynomial_dfs.cpp b/libs/parallel-math/test/polynomial_dfs.cpp index 69db5d2c..efff0c01 100644 --- a/libs/parallel-math/test/polynomial_dfs.cpp +++ b/libs/parallel-math/test/polynomial_dfs.cpp @@ -1424,4 +1424,27 @@ BOOST_AUTO_TEST_CASE(polynomial_dfs_resize_perf_test, *boost::unit_test::disable std::cout << "Resize time: " << duration.count() << " microseconds." << std::endl; } +BOOST_AUTO_TEST_CASE(polynomial_dfs_equality_check_perf_test, *boost::unit_test::disabled()) { + size_t size = 131072 * 16; + + polynomial_dfs poly = { + size / 128, size, nil::crypto3::algebra::random_element()}; + + std::vector> poly4(64, poly); + + auto start = std::chrono::high_resolution_clock::now(); + + for (int i = 0; i < poly4.size(); ++i) { + BOOST_CHECK(poly4[i] == poly); + } + + // Record the end time + auto end = std::chrono::high_resolution_clock::now(); + + // Calculate the duration + auto duration = std::chrono::duration_cast(end - start); + + std::cout << "Equality check time: " << duration.count() << " microseconds." << std::endl; +} + BOOST_AUTO_TEST_SUITE_END()