From 497f415dc6dab3d31358ce475c1408f6024e5c3f Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Wed, 6 Mar 2024 21:56:50 +0300 Subject: [PATCH] Work in progress on KZG marshalling #300 --- .../detail/polynomial/basic_fri.hpp | 4 +- .../crypto3/zk/commitments/polynomial/kzg.hpp | 52 +++++++++++++++---- .../crypto3/zk/commitments/polynomial/lpc.hpp | 3 ++ .../snark/systems/plonk/placeholder/proof.hpp | 5 +- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp b/include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp index 1d65d0613..c48bdb2f8 100644 --- a/include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp +++ b/include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp @@ -78,6 +78,8 @@ namespace nil { struct basic_batched_fri { BOOST_STATIC_ASSERT_MSG(M == 2, "unsupported m value!"); + constexpr static const bool is_fri = true; + constexpr static const std::size_t m = M; constexpr static const std::size_t lambda = Lambda; @@ -1014,4 +1016,4 @@ namespace nil { } // namespace crypto3 } // namespace nil -#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP \ No newline at end of file +#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP diff --git a/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp b/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp index c2781f968..9f31137bf 100644 --- a/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp +++ b/include/nil/crypto3/zk/commitments/polynomial/kzg.hpp @@ -3,6 +3,7 @@ // Copyright (c) 2020-2021 Nikita Kaskov // Copyright (c) 2020-2021 Ilias Khairullin // Copyright (c) 2022 Ekaterina Chukavina +// Copyright (c) 2024 Vasiliy Olekhov // // MIT License // @@ -217,6 +218,9 @@ namespace nil { typename PolynomialType = math::polynomial_dfs > struct batched_kzg { + + constexpr static bool is_kzg = true; + typedef CurveType curve_type; typedef TranscriptHashType transcript_hash_type; typedef typename curve_type::gt_type::value_type gt_value_type; @@ -235,8 +239,10 @@ namespace nil { using commitment_type = std::vector; // Used in placeholder because it's easy to push it into transcript + using eval_storage_type = eval_storage; + struct proof_type { - eval_storage z; + eval_storage_type z; single_commitment_type kzg_proof; }; @@ -605,8 +611,12 @@ namespace nil { namespace commitments{ // Placeholder-friendly class - template> - class kzg_commitment_scheme : public polys_evaluator{ + template + class kzg_commitment_scheme : + public polys_evaluator< + typename KZGScheme::params_type, + typename KZGScheme::commitment_type, + typename KZGScheme::poly_type> { public: using curve_type = typename KZGScheme::curve_type; using field_type = typename KZGScheme::field_type; @@ -616,7 +626,7 @@ namespace nil { 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 = PolynomialType; + using poly_type = typename KZGScheme::poly_type; using proof_type = typename KZGScheme::proof_type; using endianness = nil::marshalling::option::big_endian; private: @@ -832,9 +842,15 @@ namespace nil { * Dan Boneh, Justin Drake, Ben Fisch, * */ - template> - class kzg_commitment_scheme_v2 : public polys_evaluator{ + template + class kzg_commitment_scheme_v2 : + public polys_evaluator< + typename KZGScheme::params_type, + typename KZGScheme::commitment_type, + typename KZGScheme::poly_type> { public: + constexpr static bool is_kzg_commitment_scheme_v2 = true; + using curve_type = typename KZGScheme::curve_type; using field_type = typename KZGScheme::field_type; using params_type = typename KZGScheme::params_type; @@ -844,10 +860,20 @@ namespace nil { 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 = PolynomialType; + using poly_type = typename KZGScheme::poly_type; + + using eval_storage_type = eval_storage; + using single_commitment_type = typename KZGScheme::single_commitment_type; + struct proof_type { - eval_storage z; - typename KZGScheme::single_commitment_type pi_1, pi_2; + eval_storage_type z; + single_commitment_type pi_1, pi_2; + 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 { + return !(*this == other); + } }; using endianness = nil::marshalling::option::big_endian; private: @@ -920,7 +946,13 @@ namespace nil { void mark_batch_as_fixed(std::size_t index) { } - kzg_commitment_scheme_v2(params_type kzg_params) : _params(kzg_params) {} + static params_type create_params(std::size_t d, typename KZGScheme::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); + } // Differs from static, because we pack the result into byte blob. commitment_type commit(std::size_t index){ diff --git a/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp b/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp index 0441681f5..d98541396 100644 --- a/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp +++ b/include/nil/crypto3/zk/commitments/polynomial/lpc.hpp @@ -67,6 +67,8 @@ namespace nil { using eval_storage_type = typename LPCScheme::eval_storage_type; using preprocessed_data_type = std::map>; + constexpr static bool is_lpc = true; + private: std::map _trees; typename fri_type::params_type _fri_params; @@ -331,6 +333,7 @@ namespace nil { constexpr static const std::size_t lambda = LPCParams::lambda; constexpr static const std::size_t m = LPCParams::m; constexpr static const bool is_const_size = LPCParams::is_const_size; + constexpr static const bool is_batched_list_polynomial_commitment = true; typedef LPCParams lpc_params; diff --git a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp index b9ff71281..c1ef0982c 100644 --- a/include/nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp +++ b/include/nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp @@ -28,6 +28,9 @@ #ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP #define CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP +#include +#include + namespace nil { namespace crypto3 { namespace zk { @@ -67,7 +70,7 @@ namespace nil { typename commitment_scheme_type::proof_type eval_proof; bool operator==(const evaluation_proof &rhs) const { - return challenge == rhs.challenge && eval_proof == rhs.eval_proof; + return challenge == rhs.challenge && eval_proof == rhs.eval_proof; } bool operator!=(const evaluation_proof &rhs) const { return !(rhs == *this);