diff --git a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_node.hpp b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_node.hpp index 7a6a727888..c273ec063a 100644 --- a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_node.hpp +++ b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_node.hpp @@ -169,11 +169,16 @@ namespace nil { std::is_same::value_type>::value, bool>::type = true> - ValueType - make_merkle_node_value(const typename merkle_node_value, - ValueType>::type &filled_node_value) { + ValueType make_merkle_node_value( + const typename merkle_node_value, ValueType>::type &filled_node_value) + { ValueType node_value; - BOOST_ASSERT(node_value.size() == filled_node_value.value().size()); + if (node_value.size() != filled_node_value.value().size()) { + throw std::invalid_argument( + std::string("Invalid number of elements for merkle tree nodes. Expected: ") + + std::to_string(node_value.size()) + " got: " + + std::to_string(filled_node_value.value().size())); + } for (std::size_t i = 0; i < filled_node_value.value().size(); ++i) { node_value.at(i) = filled_node_value.value().at(i).value(); } @@ -187,7 +192,8 @@ namespace nil { GroupElementType >::value, bool>::type = true> GroupElementType make_merkle_node_value(const typename merkle_node_value< - nil::marshalling::field_type, GroupElementType>::type &filled_node_value) { + nil::marshalling::field_type, GroupElementType>::type &filled_node_value) + { return filled_node_value.value(); } @@ -198,11 +204,12 @@ namespace nil { nil::crypto3::containers::merkle_proof>::value, bool>::type = true> - typename MerkleProof::value_type - make_merkle_node_value(const typename merkle_node_value, - MerkleProof>::type &filled_node_value) { + typename MerkleProof::value_type make_merkle_node_value( + const typename merkle_node_value, MerkleProof>::type &filled_node_value) + { return make_merkle_node_value(filled_node_value); } + template>::value, bool>::type = true> - typename MerkleTree::value_type - make_merkle_node_value(const typename merkle_node_value, - MerkleTree>::type &filled_node_value) { + typename MerkleTree::value_type make_merkle_node_value( + const typename merkle_node_value, MerkleTree>::type &filled_node_value) + { return make_merkle_node_value(filled_node_value); } diff --git a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_proof.hpp b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_proof.hpp index b5af6953b5..7eb61246b3 100644 --- a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_proof.hpp +++ b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_proof.hpp @@ -109,8 +109,8 @@ namespace nil { template typename MerkleProof::path_element_type make_merkle_proof_path_element( - const typename merkle_proof_path_element, - MerkleProof>::type &filled_proof_path_element) { + const typename merkle_proof_path_element, MerkleProof>::type &filled_proof_path_element) + { typename MerkleProof::path_element_type proof_path_element; proof_path_element._position = std::get<0>(filled_proof_path_element.value()).value(); proof_path_element._hash = @@ -133,9 +133,9 @@ namespace nil { } template - typename MerkleProof::layer_type - make_merkle_proof_layer(const typename merkle_proof_layer, - MerkleProof>::type &filled_proof_layer) { + typename MerkleProof::layer_type make_merkle_proof_layer( + const typename merkle_proof_layer, MerkleProof>::type &filled_proof_layer) + { typename MerkleProof::layer_type proof_layer; for (std::size_t i = 0; i < filled_proof_layer.value().size(); ++i) { proof_layer.at(i) = @@ -158,9 +158,9 @@ namespace nil { } template - typename MerkleProof::path_type - make_merkle_proof_path(const typename merkle_proof_path, - MerkleProof>::type &filled_proof_path) { + typename MerkleProof::path_type make_merkle_proof_path( + const typename merkle_proof_path, MerkleProof>::type &filled_proof_path) + { typename MerkleProof::path_type proof_path; proof_path.reserve(filled_proof_path.value().size()); for (std::size_t i = 0; i < filled_proof_path.value().size(); ++i) { @@ -189,8 +189,8 @@ namespace nil { template MerkleProof make_merkle_proof( - const merkle_proof, MerkleProof> &filled_merkle_proof) { - + const merkle_proof, MerkleProof> &filled_merkle_proof) + { MerkleProof mp( std::get<0>(filled_merkle_proof.value()).value(), // mp._li make_merkle_node_value(std::get<1>(filled_merkle_proof.value())), // mp._root diff --git a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_tree.hpp b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_tree.hpp index f4fe5bc072..5b63c9cc28 100644 --- a/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_tree.hpp +++ b/crypto3/libs/marshalling/containers/include/nil/crypto3/marshalling/containers/types/merkle_tree.hpp @@ -68,8 +68,9 @@ namespace nil { } template - MerkleTree make_merkle_tree(const merkle_tree< - nil::marshalling::field_type, MerkleTree> &filled_merkle_tree) { + MerkleTree make_merkle_tree( + const merkle_tree, MerkleTree> &filled_merkle_tree) + { typename MerkleTree::container_type hashes; for (std::size_t i = 0; i < filled_merkle_tree.value().size(); ++i) { hashes.push_back( diff --git a/crypto3/libs/marshalling/core/include/nil/marshalling/types/array_list.hpp b/crypto3/libs/marshalling/core/include/nil/marshalling/types/array_list.hpp index e675cb2c51..d05aaf2769 100644 --- a/crypto3/libs/marshalling/core/include/nil/marshalling/types/array_list.hpp +++ b/crypto3/libs/marshalling/core/include/nil/marshalling/types/array_list.hpp @@ -438,9 +438,11 @@ namespace nil { template std::vector make_standard_array_list( - const standard_array_list& filled_array, - std::function element_de_marshalling) { + const standard_array_list& filled_array, + std::function element_de_marshalling) + { std::vector result; + result.reserve(filled_array.value().size()); for (const auto& v: filled_array.value()) { result.push_back(element_de_marshalling(v)); } @@ -470,8 +472,11 @@ namespace nil { const standard_array_list& filled_keys, const standard_array_list& filled_values, std::function key_de_marshalling, - std::function value_de_marshalling) { - assert(filled_keys.value().size() == filled_values.value().size()); + std::function value_de_marshalling) + { + if (filled_keys.value().size() != filled_values.value().size()) { + throw std::invalid_argument("Number of values and keys do not match");; + } std::map result; for (std::size_t i = 0; i < filled_keys.value().size(); ++i) { diff --git a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/expression.hpp b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/expression.hpp index 40834c05c4..4fde8d1ed0 100644 --- a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/expression.hpp +++ b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/expression.hpp @@ -180,8 +180,9 @@ namespace nil { } template - math::flat_pow_operation - make_power_operation(const typename flat_pow_operation>::type& filled_power_op) { + math::flat_pow_operation make_power_operation( + const typename flat_pow_operation>::type& filled_power_op) + { math::flat_pow_operation power_op; power_op.power = std::get<0>(filled_power_op.value()).value(); power_op.type = static_cast(std::get<1>(filled_power_op.value()).value()); @@ -190,8 +191,9 @@ namespace nil { } template - math::flat_binary_arithmetic_operation - make_binary_operation(const typename flat_binary_arithmetic_operation>::type& filled_power_op) { + math::flat_binary_arithmetic_operation make_binary_operation( + const typename flat_binary_arithmetic_operation>::type& filled_power_op) + { math::flat_binary_arithmetic_operation bin_op; bin_op.op = static_cast(std::get<0>(filled_power_op.value()).value()); bin_op.left_type = static_cast(std::get<1>(filled_power_op.value()).value()); @@ -203,9 +205,8 @@ namespace nil { template ExpressionType make_expression( - const typename expression, - ExpressionType>::type &filled_expr) { - + const typename expression, ExpressionType>::type &filled_expr) + { using ArithmeticOperatorType = typename ExpressionType::binary_arithmetic_operation_type::ArithmeticOperatorType; math::flat_expression flat_expr; diff --git a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/polynomial.hpp b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/polynomial.hpp index 5cac2eb2cb..323f587498 100644 --- a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/polynomial.hpp +++ b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/polynomial.hpp @@ -70,8 +70,7 @@ namespace nil { } template - PolynomialType - make_polynomial( + PolynomialType make_polynomial( const typename polynomial< nil::marshalling::field_type, PolynomialType, @@ -121,12 +120,12 @@ namespace nil { } template - PolynomialDFSType - make_polynomial(const typename polynomial< + PolynomialDFSType make_polynomial(const typename polynomial< nil::marshalling::field_type, PolynomialDFSType, std::enable_if_t::value - >>::type &filled_polynomial) { + >>::type &filled_polynomial) + { auto val = nil::crypto3::marshalling::types::make_field_element_vector< typename PolynomialDFSType::value_type, Endianness>(std::get<1>(filled_polynomial.value())); @@ -155,7 +154,8 @@ namespace nil { template std::vector make_polynomial_vector( - const polynomial_vector, PolynomialType> &filled_polynomial_vector) { + const polynomial_vector, PolynomialType> &filled_polynomial_vector) + { std::vector result; result.reserve(filled_polynomial_vector.value().size()); for (std::size_t i = 0; i < filled_polynomial_vector.value().size(); i++) { diff --git a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/term.hpp b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/term.hpp index fbbf0592b4..c6564061e9 100644 --- a/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/term.hpp +++ b/crypto3/libs/marshalling/math/include/nil/crypto3/marshalling/math/types/term.hpp @@ -82,9 +82,9 @@ namespace nil { } template - NonLinearTerm - make_term(const typename term, - NonLinearTerm>::type &filled_term) { + NonLinearTerm make_term( + const typename term, NonLinearTerm>::type &filled_term) + { std::vector vars; auto coeff = std::get<0>(filled_term.value()).value(); vars.reserve(std::get<1>(filled_term.value()).value().size()); diff --git a/crypto3/libs/marshalling/multiprecision/include/nil/crypto3/marshalling/multiprecision/types/integral.hpp b/crypto3/libs/marshalling/multiprecision/include/nil/crypto3/marshalling/multiprecision/types/integral.hpp index 7654b9c828..f9bb282310 100644 --- a/crypto3/libs/marshalling/multiprecision/include/nil/crypto3/marshalling/multiprecision/types/integral.hpp +++ b/crypto3/libs/marshalling/multiprecision/include/nil/crypto3/marshalling/multiprecision/types/integral.hpp @@ -427,15 +427,16 @@ namespace nil { nil::marshalling::types::standard_array_list< nil::marshalling::field_type, integral, IntegralContainer>> - integral_vector) { - - std::vector result; - std::vector, IntegralContainer>> &values = + const& integral_vector) + { + std::vector, IntegralContainer>> const& values = integral_vector.value(); std::size_t size = values.size(); + std::vector result; + result.reserve(size); for (std::size_t i = 0; i < size; i++) { - result.push_back(values[i].value()); + result.emplace_back(values[i].value()); } return result; } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/commitment_params.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/commitment_params.hpp index 085a76208d..19b2891c59 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/commitment_params.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/commitment_params.hpp @@ -72,12 +72,13 @@ namespace nil { template std::vector - make_integer_vector(const nil::marshalling::types::standard_size_t_array_list< - nil::marshalling::field_type - >& filled_vector) { + make_integer_vector( + const nil::marshalling::types::standard_size_t_array_list >& filled_vector) + { std::vector result; - for( std::size_t i = 0; i < filled_vector.value().size(); i++){ - result.push_back(filled_vector.value()[i].value()); + result.reserve(filled_vector.value().size()); + for (std::size_t i = 0; i < filled_vector.value().size(); i++) { + result.emplace_back(filled_vector.value()[i].value()); } return result; } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp index b84b3a2a6e..b84e19a86c 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp @@ -133,13 +133,16 @@ namespace nil { template EvalStorage make_eval_storage( - const eval_storage, EvalStorage> &filled_storage - ){ + const eval_storage, EvalStorage> &filled_storage) + { EvalStorage z; typename nil::crypto3::marshalling::types::batch_info_type batch_info; std::vector eval_points_num; auto filled_batch_info = std::get<1>(filled_storage.value()).value(); + if (filled_batch_info.size() % 2 != 0) { + throw std::invalid_argument("Wrong length of batch info"); + } for( std::size_t i = 0; i < filled_batch_info.size(); i+=2 ){ batch_info[filled_batch_info[i].value()] = filled_batch_info[i+1].value(); z.set_batch_size(filled_batch_info[i].value(), filled_batch_info[i+1].value()); @@ -147,8 +150,11 @@ namespace nil { auto filled_eval_points_num = std::get<2>(filled_storage.value()).value(); std::size_t cur = 0; - for( const auto &it:batch_info){ - for( std::size_t i = 0; i < it.second; i++ ){ + for (const auto &it: batch_info){ + for (std::size_t i = 0; i < it.second; i++ ) { + if (cur >= filled_eval_points_num.size()) { + throw std::invalid_argument("Not enough eval points"); + } z.set_poly_points_number(it.first, i, filled_eval_points_num[cur].value()); cur++; } @@ -156,9 +162,12 @@ namespace nil { auto filled_z = std::get<0>(filled_storage.value()).value(); cur = 0; - for( const auto &it:batch_info){ - for( std::size_t i = 0; i < it.second; i++ ){ - for( std::size_t j = 0; j < z.get_poly_points_number(it.first, i); j++ ){ + for (const auto &it: batch_info) { + for (std::size_t i = 0; i < it.second; i++ ) { + for (std::size_t j = 0; j < z.get_poly_points_number(it.first, i); j++ ) { + if (cur >= filled_z.size()) { + throw std::invalid_argument("Not enough values for Z"); + } z.set(it.first, i, j, filled_z[cur].value()); cur++; } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp index 59872536a1..667badf265 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp @@ -229,7 +229,12 @@ namespace nil { typename FRI::round_proof_type round_proof; // std::vector> y; const std::size_t size = std::get<0>(filled.value()).value().size(); - BOOST_ASSERT(size % FRI::m == 0); + if (size % FRI::m != 0) { + throw std::invalid_argument( + std::string("Number of elements should be multiple of m = ") + + std::to_string(FRI::m) + " got: " + + std::to_string(size)); + } const std::size_t coset_size = size / FRI::m; std::size_t cur = 0; round_proof.y.resize(coset_size); @@ -296,25 +301,33 @@ namespace nil { make_fri_query_proof( const fri_query_proof_type, FRI> &filled, const batch_info_type &batch_info, - const std::vector &step_list - ) { + const std::vector &step_list) + { typename FRI::query_proof_type query_proof; // std::map initial_proof; std::size_t cur = 0; std::size_t coset_size = 1 << (step_list[0] - 1); + auto const& initial_proof = std::get<0>(filled.value()).value(); for (const auto &[batch_id, batch_size] : batch_info) { + if (cur >= initial_proof.size()) { + throw std::invalid_argument("Not enough initial_proof values"); + } query_proof.initial_proof[batch_id] = make_fri_initial_proof( - std::get<0>(filled.value()).value()[cur++], batch_size, coset_size + initial_proof[cur++], batch_size, coset_size ); } // std::vector round_proofs; cur = 0; + auto const& round_proofs = std::get<1>(filled.value()).value(); for (std::size_t r = 0; r < step_list.size(); r++) { coset_size = r == step_list.size() - 1 ? 1 : (1 << (step_list[r+1]-1)); + if (cur >= round_proofs.size()) { + throw std::invalid_argument("Not enough round_proofs values"); + } query_proof.round_proofs.push_back( make_fri_round_proof( - std::get<1>(filled.value()).value()[cur++] + round_proofs[cur++] ) ); } @@ -405,14 +418,24 @@ namespace nil { auto &query_proof = proof.query_proofs[i]; for( const auto &it: query_proof.initial_proof){ auto &initial_proof = it.second; - BOOST_ASSERT(initial_proof.values.size() == batch_info.at(it.first)); + if (initial_proof.values.size() != batch_info.at(it.first)) { + throw std::invalid_argument( + std::string("Initial proof has wrong size. Expected: ") + + std::to_string(initial_proof.values.size()) + " got: " + + std::to_string(batch_info.at(it.first))); + } for( std::size_t j = 0; j < initial_proof.values.size(); j++ ){ for(std::size_t k = 0; k < initial_proof.values[j].size(); k++ ){ for( std::size_t l = 0; l < FRI::m; l++ ){ initial_val.push_back(initial_proof.values[j][k][l]); } } - BOOST_ASSERT(std::size_t(1 << (params.step_list[0] - 1)) == initial_proof.values[j].size()); + if (std::size_t(1 << (params.step_list[0] - 1)) != initial_proof.values[j].size()) { + throw std::invalid_argument( + std::string("Initial proof element has wrong size. Expected: ") + + std::to_string(std::size_t(1 << (params.step_list[0] - 1))) + " got: " + + std::to_string(initial_proof.values[j].size())); + } } } } @@ -494,18 +517,19 @@ namespace nil { template typename FRI::proof_type make_fri_proof( - const typename fri_proof, FRI>::type &filled_proof, const batch_info_type &batch_info - ){ + const typename fri_proof, FRI>::type &filled_proof, + const batch_info_type &batch_info) + { typename FRI::proof_type proof; // merkle roots - for( std::size_t i = 0; i < std::get<0>(filled_proof.value()).value().size(); i++){ + for (std::size_t i = 0; i < std::get<0>(filled_proof.value()).value().size(); i++) { proof.fri_roots.push_back( make_merkle_node_value(std::get<0>(filled_proof.value()).value()[i]) ); } // step_list std::vector step_list; - for( std::size_t i = 0; i < std::get<1>(filled_proof.value()).value().size(); i++){ + for (std::size_t i = 0; i < std::get<1>(filled_proof.value()).value().size(); i++) { auto c = std::get<1>(filled_proof.value()).value()[i].value(); step_list.push_back(c); } @@ -515,16 +539,21 @@ namespace nil { // initial_polynomials values std::size_t coset_size = 1 << (step_list[0] - 1); std::size_t cur = 0; - for( std::size_t i = 0; i < lambda; i++ ){ - for( const auto &it:batch_info){ + for (std::size_t i = 0; i < lambda; i++) { + for (const auto &it: batch_info) { proof.query_proofs[i].initial_proof[it.first] = typename FRI::initial_proof_type(); proof.query_proofs[i].initial_proof[it.first].values.resize(it.second); - for( std::size_t j = 0; j < it.second; j++ ){ + for (std::size_t j = 0; j < it.second; j++) { proof.query_proofs[i].initial_proof[it.first].values[j].resize(coset_size); - for( std::size_t k = 0; k < coset_size; k++){ - for( std::size_t l = 0; l < FRI::m; l++, cur++ ){ - BOOST_ASSERT(cur < std::get<2>(filled_proof.value()).value().size()); - proof.query_proofs[i].initial_proof[it.first].values[j][k][l] = std::get<2>(filled_proof.value()).value()[cur].value(); + for (std::size_t k = 0; k < coset_size; k++) { + for (std::size_t l = 0; l < FRI::m; l++, cur++ ) { + if (cur >= std::get<2>(filled_proof.value()).value().size()) { + throw std::invalid_argument( + std::string("Too few elements provided for initial_polynomials values: ") + + std::to_string(std::get<2>(filled_proof.value()).value().size())); + } + proof.query_proofs[i].initial_proof[it.first].values[j][k][l] = + std::get<2>(filled_proof.value()).value()[cur].value(); } } } @@ -533,35 +562,49 @@ namespace nil { // round polynomials values cur = 0; - for(std::size_t i = 0; i < lambda; i++ ){ + for (std::size_t i = 0; i < lambda; i++) { proof.query_proofs[i].round_proofs.resize(step_list.size()); - for(std::size_t r = 0; r < step_list.size(); r++ ){ + for (std::size_t r = 0; r < step_list.size(); r++ ) { coset_size = r == step_list.size() - 1? 1: (1 << (step_list[r+1]-1)); proof.query_proofs[i].round_proofs[r].y.resize(coset_size); - for( std::size_t j = 0; j < coset_size; j++){ - for( std::size_t k = 0; k < FRI::m; k++, cur++){ - BOOST_ASSERT(cur < std::get<3>(filled_proof.value()).value().size()); + for (std::size_t j = 0; j < coset_size; j++) { + for (std::size_t k = 0; k < FRI::m; k++, cur++) { + if (cur >= std::get<3>(filled_proof.value()).value().size()) { + throw std::invalid_argument( + std::string("Too few elements provided for round polynomials values: ") + + std::to_string(std::get<3>(filled_proof.value()).value().size())); + } proof.query_proofs[i].round_proofs[r].y[j][k] = std::get<3>(filled_proof.value()).value()[cur].value(); } } } } // initial merkle proofs + auto const& initial_merkle_proofs = std::get<4>(filled_proof.value()).value(); cur = 0; - for( std::size_t i = 0; i < lambda; i++ ){ - for( const auto &it:batch_info){ - proof.query_proofs[i].initial_proof[it.first].p = make_merkle_proof( - std::get<4>(filled_proof.value()).value()[cur++] + for (std::size_t i = 0; i < lambda; i++) { + for (const auto &it: batch_info) { + if (cur >= initial_merkle_proofs.size()) { + throw std::invalid_argument("Not enough initial_merkle_proof values"); + } + proof.query_proofs[i].initial_proof[it.first].p = + make_merkle_proof( + std::get<4>(filled_proof.value()).value()[cur++] ); } } // round merkle proofs + auto const& round_merkle_proofs = std::get<5>(filled_proof.value()).value(); cur = 0; - for( std::size_t i = 0; i < lambda; i++ ){ - for( std::size_t r = 0; r < step_list.size(); r++, cur++ ){ - proof.query_proofs[i].round_proofs[r].p = make_merkle_proof( - std::get<5>(filled_proof.value()).value()[cur] + for (std::size_t i = 0; i < lambda; i++ ) { + for (std::size_t r = 0; r < step_list.size(); r++, cur++ ) { + if (cur >= round_merkle_proofs.size()) { + throw std::invalid_argument("Not enough round_merkle_proof values"); + } + proof.query_proofs[i].round_proofs[r].p = + make_merkle_proof( + round_merkle_proofs[cur] ); } } @@ -570,6 +613,7 @@ namespace nil { proof.final_polynomial = make_polynomial( std::get<6>(filled_proof.value()) ); + // proof_of_work proof.proof_of_work = std::get<7>(filled_proof.value()).value(); return proof; @@ -638,8 +682,8 @@ namespace nil { template typename FRI::initial_proofs_batch_type make_initial_proofs_batch( - const initial_proofs_batch_type, FRI> &filled - ) { + const initial_proofs_batch_type, FRI> &filled) + { typename FRI::initial_proofs_batch_type initial_proofs_batch; for (const auto &batch : filled.value()) { std::map batch_initial_proofs; @@ -688,8 +732,8 @@ namespace nil { template typename FRI::round_proofs_batch_type make_round_proofs_batch( - const round_proofs_batch_type, FRI> &filled - ) { + const round_proofs_batch_type, FRI> &filled) + { typename FRI::round_proofs_batch_type round_proofs_batch; for (const auto &round_proof_vector : filled.value()) { std::vector round_proofs; diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp index 4b5db63b8b..cdd28022ce 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp @@ -146,12 +146,27 @@ namespace nil { >::type const& filled_commitment_preprocessed_data ) { typename LPCScheme::preprocessed_data_type result; - for(std::size_t i = 0; i < std::get<0>(filled_commitment_preprocessed_data.value()).value().size(); i++){ - std::size_t k = std::get<0>(filled_commitment_preprocessed_data.value()).value()[i].value(); - std::size_t size = std::get<1>(filled_commitment_preprocessed_data.value()).value()[i].value(); + auto const& vector_map = std::get<0>(filled_commitment_preprocessed_data.value()).value(); + auto const& vector_sizes = std::get<1>(filled_commitment_preprocessed_data.value()).value(); + auto const& vector_values = std::get<2>(filled_commitment_preprocessed_data.value()).value(); + if (vector_map.size() != vector_sizes.size()) { + throw std::invalid_argument("Map size does not equal vector sizes"); + } + std::size_t total_size = 0; + for(auto const& size: vector_sizes) { + total_size += size.value(); + } + if (vector_values.size() != total_size) { + throw std::invalid_argument("Map size does not equal vector sizes"); + } + + for (std::size_t i = 0; i < vector_map.size(); i++) { + std::size_t k = vector_map[i].value(); + std::size_t size = vector_sizes[i].value(); std::vector v; - for(std::size_t j = 0; j < size; j++){ - v.push_back(std::get<2>(filled_commitment_preprocessed_data.value()).value()[i*size + j].value()); + v.reserve(size); + for (std::size_t j = 0; j < size; j++){ + v.emplace_back(vector_values[i*size + j].value()); } result[k] = v; } @@ -406,8 +421,8 @@ namespace nil { template typename LPCScheme::fri_proof_type make_initial_fri_proof( - const initial_fri_proof_type, LPCScheme> &filled_proof - ) { + const initial_fri_proof_type, LPCScheme> &filled_proof) + { typename LPCScheme::fri_proof_type proof; proof.fri_round_proof = make_round_proofs_batch< @@ -454,8 +469,8 @@ namespace nil { template typename LPCScheme::lpc_proof_type make_initial_eval_proof( - const inital_eval_proof, LPCScheme> &filled_proof - ) { + const inital_eval_proof, LPCScheme> &filled_proof) + { typename LPCScheme::lpc_proof_type proof; proof.z = make_eval_storage( diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/polys_evaluator.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/polys_evaluator.hpp index 924971e588..84f8bc8764 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/polys_evaluator.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/polys_evaluator.hpp @@ -92,7 +92,7 @@ namespace nil { using size_t_marshalling_type = nil::marshalling::types::integral; using polynomial_vector_marshalling_type = polynomial_vector; - + using field_element_vector_type = field_element_vector>; using array_of_field_element_vector_type = standard_array_list; @@ -146,8 +146,8 @@ namespace nil { template PolysEvaluator make_polys_evaluator( - const polys_evaluator, PolysEvaluator>& filled_polys_evaluator - ) { + const polys_evaluator, PolysEvaluator>& filled_polys_evaluator) + { using nil::marshalling::types::make_size_t; using nil::marshalling::types::make_std_map; using nil::marshalling::types::standard_array_list; @@ -159,7 +159,7 @@ namespace nil { using size_t_marshalling_type = nil::marshalling::types::integral; using polynomial_vector_marshalling_type = polynomial_vector; - + using field_element_vector_type = field_element_vector>; using array_of_field_element_vector_type = standard_array_list; diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/accumulator.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/accumulator.hpp index 0390bc8347..c8f8986d9e 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/accumulator.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/accumulator.hpp @@ -112,9 +112,8 @@ namespace nil { template Accumulator make_powers_of_tau_accumulator( - const powers_of_tau_accumulator, Accumulator> - &filled_accumulator) { - + const powers_of_tau_accumulator, Accumulator> &filled_accumulator) + { return Accumulator( std::move( make_fast_curve_element_vector, Endianness>( diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/public_key.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/public_key.hpp index e6908eab4a..91dd2dda0b 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/public_key.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/public_key.hpp @@ -84,8 +84,8 @@ namespace nil { template PublicKey make_powers_of_tau_public_key( - const powers_of_tau_public_key, PublicKey> - &filled_public_key) { + const powers_of_tau_public_key, PublicKey> &filled_public_key) + { return PublicKey( std::move( diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/result.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/result.hpp index eaaf14b4da..22b841ac0b 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/result.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/powers_of_tau/result.hpp @@ -133,9 +133,8 @@ namespace nil { template Result make_powers_of_tau_result( - const powers_of_tau_result, Result> - &filled_result) { - + const powers_of_tau_result, Result> &filled_result) + { return Result( std::move( make_fast_curve_element, Endianness>( diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/knowledge_commitment.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/knowledge_commitment.hpp index f4c52c85b0..3c8fdca5f6 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/knowledge_commitment.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/knowledge_commitment.hpp @@ -83,11 +83,12 @@ namespace nil { template typename KnowledgeCommitment::value_type - make_knowledge_commitment(const knowledge_commitment, - KnowledgeCommitment> &filled_kc) { - - return typename KnowledgeCommitment::value_type(std::move(std::get<0>(filled_kc.value()).value()), - std::move(std::get<1>(filled_kc.value()).value())); + make_knowledge_commitment( + const knowledge_commitment, KnowledgeCommitment> &filled_kc) + { + return typename KnowledgeCommitment::value_type( + std::move(std::get<0>(filled_kc.value()).value()), + std::move(std::get<1>(filled_kc.value()).value())); } template @@ -119,16 +120,17 @@ namespace nil { const nil::marshalling::types::standard_array_list< nil::marshalling::field_type, knowledge_commitment, KnowledgeCommitment>> - &filled_kc_vector) { - - std::vector result; + &filled_kc_vector) + { const std::vector< knowledge_commitment, KnowledgeCommitment>> &values = filled_kc_vector.value(); std::size_t size = values.size(); + std::vector result; + result.reserve(size); for (std::size_t i = 0; i < size; i++) { - result.push_back(make_knowledge_commitment(values[i])); + result.emplace_back(make_knowledge_commitment(values[i])); } return result; } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp index 35e9c57059..455d30b27e 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp @@ -89,9 +89,11 @@ namespace nil { } template - PreprocessedPublicDataType make_placeholder_preprocessed_public_data(const - placeholder_preprocessed_public_data, PreprocessedPublicDataType> &filled_preprocessed_public_data - ) { + PreprocessedPublicDataType make_placeholder_preprocessed_public_data( + const placeholder_preprocessed_public_data< + nil::marshalling::field_type, + PreprocessedPublicDataType> &filled_preprocessed_public_data) + { using TTypeBase = typename nil::marshalling::field_type; using PolynomialDFSType = typename PreprocessedPublicDataType::polynomial_dfs_type; diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp index 25c256be20..d808875ff0 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp @@ -96,9 +96,8 @@ namespace nil { template typename Proof::partial_proof_type make_placeholder_partial_evaluation_proof( - const placeholder_partial_evaluation_proof< - nil::marshalling::field_type, Proof> &filled_proof - ) { + const placeholder_partial_evaluation_proof, Proof> &filled_proof) + { typename Proof::partial_proof_type partial_proof; // batch size integers @@ -151,8 +150,8 @@ namespace nil { template typename Proof::evaluation_proof make_placeholder_evaluation_proof( - const placeholder_evaluation_proof, Proof> &filled_proof) { - + const placeholder_evaluation_proof, Proof> &filled_proof) + { typename Proof::evaluation_proof proof; // typename FieldType::value_type challenge @@ -207,7 +206,9 @@ namespace nil { } template - Proof make_placeholder_proof( const placeholder_proof, Proof> &filled_proof) { + Proof make_placeholder_proof( + const placeholder_proof, Proof> &filled_proof) + { Proof proof; // evaluation_proof eval_proof @@ -215,12 +216,18 @@ namespace nil { make_placeholder_evaluation_proof(std::get<1>(filled_proof.value())); auto batch_info = proof.eval_proof.eval_proof.z.get_batch_info(); + auto const& commitments = std::get<0>(filled_proof.value()).value(); std::size_t cur = 0; - for( const auto &it:batch_info ){ - if( it.first == Proof::FIXED_VALUES_BATCH ) continue; + for (const auto &it: batch_info ) { + if (it.first == Proof::FIXED_VALUES_BATCH) continue; + + if (cur >= commitments.size()) { + throw std::invalid_argument("Not enough values for commitments"); + } + proof.commitments[it.first] = make_commitment( - std::get<0>(filled_proof.value()).value()[cur++] + commitments[cur++] ); } @@ -267,12 +274,13 @@ namespace nil { template AggregatedProof make_placeholder_aggregated_proof( - const placeholder_aggregated_proof_type, Proof> &filled_proof) { + const placeholder_aggregated_proof_type, Proof> &filled_proof) + { AggregatedProof proof; // std::vector> partial_proofs; auto filled_partial_proofs = std::get<0>(filled_proof.value()).value(); - for( const auto &it:filled_partial_proofs){ + for (const auto &it: filled_partial_proofs) { proof.partial_proofs.push_back( make_placeholder_partial_evaluation_proof(it) ); diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp index dbe9f483d0..02fde8f4a4 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp @@ -161,7 +161,8 @@ namespace nil { } template - std::vector> make_field_element_columns_vector( + std::vector> + make_field_element_columns_vector( const nil::marshalling::types::standard_array_list< nil::marshalling::field_type, field_element, FieldValueType>> @@ -169,9 +170,16 @@ namespace nil { const std::size_t columns_amount, const std::size_t rows_amount) { + if (field_elem_vector.value().size() != columns_amount * rows_amount) { + throw std::invalid_argument( + "Size of vector does not match the expected data size. Expected: " + + std::to_string(columns_amount * rows_amount) + " got " + + std::to_string(field_elem_vector.value().size())); + } + std::vector> result( columns_amount, std::vector(rows_amount)); - BOOST_ASSERT(field_elem_vector.value().size() == columns_amount * rows_amount); + std::size_t cur = 0; for (std::size_t i = 0; i < columns_amount; i++) { for (std::size_t j = 0; j < rows_amount; j++, cur++) { @@ -234,7 +242,7 @@ namespace nil { std::get<5>(filled_assignments.value()).value() ); - if ( desc.usable_rows_amount >= desc.rows_amount ) + if (desc.usable_rows_amount >= desc.rows_amount) throw std::invalid_argument( "Rows amount should be greater than usable rows amount. Rows amount = " + std::to_string(desc.rows_amount) + @@ -246,18 +254,21 @@ namespace nil { desc.witness_columns, desc.rows_amount ); + std::vector> public_inputs = make_field_element_columns_vector( std::get<7>(filled_assignments.value()), desc.public_input_columns, desc.rows_amount ); + std::vector> constants = make_field_element_columns_vector( std::get<8>(filled_assignments.value()), desc.constant_columns, desc.rows_amount ); + std::vector> selectors = make_field_element_columns_vector( std::get<9>(filled_assignments.value()), @@ -265,14 +276,15 @@ namespace nil { desc.rows_amount ); + using private_table = typename PlonkTable::private_table_type; using public_table = typename PlonkTable::public_table_type; return std::make_pair(desc, PlonkTable( std::make_shared(std::move(witnesses)), std::make_shared( - std::move(public_inputs), - std::move(constants), + std::move(public_inputs), + std::move(constants), std::move(selectors) ) )); diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint.hpp index 63ca335335..ee5a399288 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint.hpp @@ -55,8 +55,8 @@ namespace nil { template PlonkConstraint make_plonk_constraint( - const plonk_constraint, PlonkConstraint> &filled_constr - ) { + const plonk_constraint, PlonkConstraint> &filled_constr) + { return make_expression(filled_constr); } @@ -82,7 +82,8 @@ namespace nil { template std::vector - make_plonk_constraints(const plonk_constraints, Constraint> &filled_constraints){ + make_plonk_constraints(const plonk_constraints, Constraint> &filled_constraints) + { std::vector constraints; for (std::size_t i = 0; i < filled_constraints.value().size(); i++) { constraints.emplace_back(make_plonk_constraint(filled_constraints.value().at(i))); diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp index 35dc11e69e..adef7f5fd3 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp @@ -85,14 +85,14 @@ namespace nil { } template - PlonkConstraintSystem - make_plonk_constraint_system( - const plonk_constraint_system, PlonkConstraintSystem> &filled_system - ){ + PlonkConstraintSystem make_plonk_constraint_system( + const plonk_constraint_system, PlonkConstraintSystem> &filled_system) + { std::vector public_input_sizes; for(std::size_t i = 0; i < std::get<4>(filled_system.value()).value().size(); i++){ public_input_sizes.push_back(std::get<4>(filled_system.value()).value().at(i).value()); } + return PlonkConstraintSystem( make_plonk_gates(std::get<0>(filled_system.value())), make_plonk_copy_constraints(std::get<1>(filled_system.value())), diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/copy_constraint.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/copy_constraint.hpp index e3d618eefb..aded5cfe93 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/copy_constraint.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/copy_constraint.hpp @@ -42,8 +42,9 @@ namespace nil { template nil::crypto3::zk::snark::plonk_copy_constraint - make_plonk_copy_constraint(const plonk_copy_constraint, FieldType> &filled_copy_constraint - ){ + make_plonk_copy_constraint( + const plonk_copy_constraint, FieldType> &filled_copy_constraint) + { using VariableType = nil::crypto3::zk::snark::plonk_variable; return nil::crypto3::zk::snark::plonk_copy_constraint( make_variable(std::get<0>(filled_copy_constraint.value())), @@ -77,9 +78,11 @@ namespace nil { template std::vector > make_plonk_copy_constraints( - const plonk_copy_constraints, FieldType> &filled_constraints - ){ - std::vector< nil::crypto3::zk::snark::plonk_copy_constraint > constraints; + const plonk_copy_constraints, FieldType> &filled_constraints) + { + std::vector> constraints; + constraints.reserve(filled_constraints.value().size()); + for (std::size_t i = 0; i < filled_constraints.value().size(); i++) { constraints.emplace_back(make_plonk_copy_constraint(filled_constraints.value()[i])); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/gate.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/gate.hpp index 0687f28224..86499cb88a 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/gate.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/gate.hpp @@ -77,14 +77,18 @@ namespace nil { template PlonkGate make_plonk_gate( - const plonk_gate, PlonkGate> &filled_gate) { + const plonk_gate, PlonkGate> &filled_gate) + { std::size_t selector_index = std::get<0>(filled_gate.value()).value(); + auto const& filled_constraints = std::get<1>(filled_gate.value()).value(); std::vector constraints; - for (std::size_t i = 0; i < std::get<1>(filled_gate.value()).value().size(); i++) { + constraints.reserve(filled_constraints.size()); + + for (std::size_t i = 0; i < filled_constraints.size(); i++) { constraints.emplace_back(make_plonk_constraint( - std::get<1>(filled_gate.value()).value().at(i))); + filled_constraints.at(i))); } return {selector_index, constraints}; @@ -111,8 +115,10 @@ namespace nil { template std::vector make_plonk_gates( - const plonk_gates, PlonkGate> &filled_gates) { + const plonk_gates, PlonkGate> &filled_gates) + { std::vector gates; + gates.reserve(filled_gates.value().size()); for (std::size_t i = 0; i < filled_gates.value().size(); i++) { gates.emplace_back(make_plonk_gate(filled_gates.value().at(i))); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_constraint.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_constraint.hpp index 2233801c1d..7a8fd2dbeb 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_constraint.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_constraint.hpp @@ -64,7 +64,9 @@ namespace nil { } template - Constraint make_plonk_lookup_constraint(const plonk_lookup_constraint, Constraint> &filled_constraint){ + Constraint make_plonk_lookup_constraint( + const plonk_lookup_constraint, Constraint> &filled_constraint) + { return Constraint({ std::get<0>(filled_constraint.value()).value(), make_plonk_constraints(std::get<1>(filled_constraint.value())) @@ -92,9 +94,11 @@ namespace nil { } template - std::vector - make_plonk_lookup_constraints(const plonk_lookup_constraints, Constraint> &filled_constraints){ + std::vector make_plonk_lookup_constraints( + const plonk_lookup_constraints, Constraint> &filled_constraints) + { std::vector constraints; + constraints.reserve(filled_constraints.value().size()); for (std::size_t i = 0; i < filled_constraints.value().size(); i++) { constraints.emplace_back(make_plonk_lookup_constraint(filled_constraints.value().at(i))); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_gate.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_gate.hpp index e4d058f6c8..e2bddc63ae 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_gate.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_gate.hpp @@ -77,8 +77,8 @@ namespace nil { template PlonkGate make_plonk_lookup_gate( - const plonk_lookup_gate, PlonkGate> &filled_gate) { - + const plonk_lookup_gate, PlonkGate> &filled_gate) + { std::size_t selector_index = std::get<0>(filled_gate.value()).value(); std::vector constraints; @@ -113,8 +113,10 @@ namespace nil { template std::vector make_plonk_lookup_gates( - const plonk_lookup_gates, PlonkGate> &filled_gates) { + const plonk_lookup_gates, PlonkGate> &filled_gates) + { std::vector gates; + gates.reserve(filled_gates.value().size()); for (std::size_t i = 0; i < filled_gates.value().size(); i++) { gates.emplace_back(make_plonk_lookup_gate(filled_gates.value().at(i))); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_table.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_table.hpp index 602bf07c4c..d1379e672a 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_table.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/lookup_table.hpp @@ -66,8 +66,13 @@ namespace nil { > filled_options; for( std::size_t i = 0; i < table.lookup_options.size(); i++ ){ - BOOST_ASSERT(table.lookup_options[i].size() == table.columns_number); - for( std::size_t j = 0; j < table.lookup_options[i].size(); j++){ + if (table.lookup_options[i].size() != table.columns_number) { + throw std::invalid_argument( + std::string("Number of columns do not match. Expected: ") + + std::to_string(table.lookup_options[i].size()) + " got: " + + std::to_string(table.columns_number)); + } + for (std::size_t j = 0; j < table.lookup_options[i].size(); j++) { filled_options.value().push_back( fill_variable(table.lookup_options[i][j]) ); @@ -90,13 +95,19 @@ namespace nil { std::size_t columns_number = std::get<1>(filled_table.value()).value(); LookupTable result(columns_number, tag_index); + if (std::get<2>(filled_table.value()).value().size() % columns_number != 0) { + throw std::invalid_argument( + std::string("Number of elements in array should be multiple of columns number = ") + + std::to_string(columns_number) + " got: " + + std::to_string(std::get<2>(filled_table.value()).value().size())); + + } std::size_t op_n = std::get<2>(filled_table.value()).value().size() / columns_number; - BOOST_ASSERT(std::get<2>(filled_table.value()).value().size() % columns_number == 0); std::size_t cur = 0; for (std::size_t i = 0; i < op_n; i++) { std::vector row; - for( std::size_t j = 0; j < columns_number; j++, cur++ ){ + for (std::size_t j = 0; j < columns_number; j++, cur++ ) { row.emplace_back( make_variable( std::get<2>(filled_table.value()).value().at(cur) @@ -131,8 +142,10 @@ namespace nil { template std::vector make_plonk_lookup_tables( - const plonk_lookup_tables, PlonkTable> &filled_tables) { + const plonk_lookup_tables, PlonkTable> &filled_tables) + { std::vector tables; + tables.reserve(filled_tables.value().size()); for (std::size_t i = 0; i < filled_tables.value().size(); i++) { tables.emplace_back(make_plonk_lookup_table(filled_tables.value().at(i))); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/plonk_public_polynomial_dfs_table.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/plonk_public_polynomial_dfs_table.hpp index 30a2cb67d6..123f929d70 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/plonk_public_polynomial_dfs_table.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/plonk_public_polynomial_dfs_table.hpp @@ -72,10 +72,13 @@ namespace nil { template std::shared_ptr make_plonk_public_table( - const plonk_public_polynomial_table, PlonkPublicTable> &filled_public_table) { + const plonk_public_polynomial_table< + nil::marshalling::field_type, + PlonkPublicTable> &filled_public_table) + { using TTypeBase = nil::marshalling::field_type; using PolynomialType = typename PlonkPublicTable::column_type; - + return std::make_shared( make_polynomial_vector(std::get<0>(filled_public_table.value())), make_polynomial_vector(std::get<1>(filled_public_table.value())), diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/variable.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/variable.hpp index 58af1708e8..43d23305af 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/variable.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/plonk/variable.hpp @@ -81,8 +81,8 @@ namespace nil { template Variable make_variable( - const typename variable, Variable>::type &filled_var - ) { + const typename variable, Variable>::type &filled_var) + { return Variable(std::get<0>(filled_var.value()).value(), std::get<1>(filled_var.value()).value(), std::get<2>(filled_var.value()).value(), @@ -111,9 +111,11 @@ namespace nil { } template - std::vector - make_variables(const variables, typename Variable::assignment_type> &filled_vars){ + std::vector make_variables( + const variables, typename Variable::assignment_type> &filled_vars) + { std::vector vars; + vars.reserve(filled_vars.value().size()); for (std::size_t i = 0; i < filled_vars.value().size(); i++) { vars.emplace_back(make_variable(filled_vars.value().at(i))); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/sparse_vector.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/sparse_vector.hpp index a5c8192416..db5f6ce4bd 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/sparse_vector.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/sparse_vector.hpp @@ -130,8 +130,8 @@ namespace nil { template SparseVector make_sparse_vector( - const sparse_vector, SparseVector> &filled_sparse_vector) { - + const sparse_vector, SparseVector> &filled_sparse_vector) + { using TTypeBase = nil::marshalling::field_type; using integral_type = nil::marshalling::types::integral; diff --git a/crypto3/libs/marshalling/zk/test/CMakeLists.txt b/crypto3/libs/marshalling/zk/test/CMakeLists.txt index 459e8264d4..83095ea2f9 100644 --- a/crypto3/libs/marshalling/zk/test/CMakeLists.txt +++ b/crypto3/libs/marshalling/zk/test/CMakeLists.txt @@ -63,7 +63,6 @@ set(TESTS_NAMES "plonk_gates" "plonk_constraint_system" "plonk_assignment_table" -# "eddsa" ) foreach(TEST_NAME ${TESTS_NAMES}) diff --git a/crypto3/libs/marshalling/zk/test/eddsa.cpp b/crypto3/libs/marshalling/zk/test/eddsa.cpp deleted file mode 100644 index d48a232589..0000000000 --- a/crypto3/libs/marshalling/zk/test/eddsa.cpp +++ /dev/null @@ -1,129 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2022 Mikhail Komarov -// Copyright (c) 2022 Nikita Kaskov -// Copyright (c) 2022 Ilias Khairullin -// -// MIT License -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -//---------------------------------------------------------------------------// - -#define BOOST_TEST_MODULE crypto3_marshalling_eddsa_test - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include - -using namespace nil::crypto3; -using namespace nil::crypto3::algebra; -using namespace nil::marshalling; - -template -void print_byteblob(TIter iter_begin, TIter iter_end) { - for (TIter it = iter_begin; it != iter_end; it++) { - std::cout << std::hex << int(*it) << std::endl; - } -} - -template -void print_fp_curve_group_element(FpCurveGroupElement e) { - std::cout << e.X.data << " " << e.Y.data << " " << e.Z.data << std::endl; -} - -template -void print_fp2_curve_group_element(Fp2CurveGroupElement e) { - std::cout << "(" << e.X.data[0].data << " " << e.X.data[1].data << ") (" << e.Y.data[0].data << " " - << e.Y.data[1].data << ") (" << e.Z.data[0].data << " " << e.Z.data[1].data << ")" << std::endl; -} - - -BOOST_AUTO_TEST_SUITE(lpc_test_suite) - - BOOST_AUTO_TEST_CASE(lpc_bls12_381_be) { - using curve_type = algebra::curves::curve25519; - using group_type = typename curve_type::g1_type<>; - using group_affine_type = typename curve_type::g1_type; - using group_value_type = typename group_type::value_type; - using group_affine_value_type = typename group_affine_type::value_type; - - using params_type = void; - using scheme_type = pubkey::eddsa; - using private_key_type = pubkey::private_key; - using public_key_type = pubkey::public_key; - using _private_key_type = typename private_key_type::private_key_type; - using _public_key_type = typename public_key_type::public_key_type; - - // -----TEST 1 - _private_key_type privkey1 = {0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, - 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, - 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60}; - _public_key_type etalon_pubkey1 = {0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, - 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, - 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a}; - std::array msg1 = {}; - typename private_key_type::signature_type etalon_sig1 = { - 0xe5, 0x56, 0x43, 0x00, 0xc3, 0x60, 0xac, 0x72, 0x90, 0x86, 0xe2, 0xcc, 0x80, 0x6e, 0x82, 0x8a, - 0x84, 0x87, 0x7f, 0x1e, 0xb8, 0xe5, 0xd9, 0x74, 0xd8, 0x73, 0xe0, 0x65, 0x22, 0x49, 0x01, 0x55, - 0x5f, 0xb8, 0x82, 0x15, 0x90, 0xa3, 0x3b, 0xac, 0xc6, 0x1e, 0x39, 0x70, 0x1c, 0xf9, 0xb4, 0x6b, - 0xd2, 0x5b, 0xf5, 0xf0, 0x59, 0x5b, 0xbe, 0x24, 0x65, 0x51, 0x41, 0x43, 0x8e, 0x7a, 0x10, 0x0b}; - - using endianness = nil::marshalling::option::big_endian; - public_key_type pub_k(etalon_pubkey1); - private_key_type priv_k(privkey1); - - auto filled_key = nil::crypto3::marshalling::types::fill_eddsa_public_key< - public_key_type, endianness>(pub_k); - - auto made_key = nil::crypto3::marshalling::types::make_eddsa_public_key< - public_key_type, endianness>(filled_key); - - BOOST_CHECK(made_key.pubkey_point == pub_k.pubkey_point); - BOOST_CHECK(std::equal(made_key.pubkey.begin(), made_key.pubkey.end(), pub_k.pubkey.begin())); - - auto filled_priv_key = nil::crypto3::marshalling::types::fill_eddsa_private_key< - private_key_type, endianness>(priv_k); - - auto made_priv_key = nil::crypto3::marshalling::types::make_eddsa_private_key< - private_key_type, endianness>(filled_priv_key); - - BOOST_CHECK(made_priv_key.s_reduced == priv_k.s_reduced); - BOOST_CHECK(std::equal(made_priv_key.privkey.begin(), made_priv_key.privkey.end(), priv_k.privkey.begin())); - BOOST_CHECK( - std::equal(made_priv_key.h_privkey.begin(), made_priv_key.h_privkey.end(), priv_k.h_privkey.begin())); - } - -BOOST_AUTO_TEST_SUITE_END()