Skip to content

Commit

Permalink
Work in progress on KZG marshalling #300
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Mar 6, 2024
1 parent 8b5632c commit af3caeb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1014,4 +1016,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP
#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP
52 changes: 42 additions & 10 deletions include/nil/crypto3/zk/commitments/polynomial/kzg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) 2020-2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2020-2021 Ilias Khairullin <[email protected]>
// Copyright (c) 2022 Ekaterina Chukavina <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
Expand Down Expand Up @@ -217,6 +218,9 @@ namespace nil {
typename PolynomialType = math::polynomial_dfs<typename CurveType::scalar_field_type::value_type>
>
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;
Expand All @@ -235,8 +239,10 @@ namespace nil {

using commitment_type = std::vector<std::uint8_t>; // Used in placeholder because it's easy to push it into transcript

using eval_storage_type = eval_storage<field_type>;

struct proof_type {
eval_storage<field_type> z;
eval_storage_type z;
single_commitment_type kzg_proof;
};

Expand Down Expand Up @@ -605,8 +611,12 @@ namespace nil {

namespace commitments{
// Placeholder-friendly class
template<typename KZGScheme, typename PolynomialType = typename math::polynomial_dfs<typename KZGScheme::field_type::value_type>>
class kzg_commitment_scheme : public polys_evaluator<typename KZGScheme::params_type, typename KZGScheme::commitment_type, PolynomialType>{
template<typename KZGScheme>
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;
Expand All @@ -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:
Expand Down Expand Up @@ -832,9 +842,15 @@ namespace nil {
* Dan Boneh, Justin Drake, Ben Fisch,
* <https://eprint.iacr.org/2020/081.pdf>
*/
template<typename KZGScheme, typename PolynomialType = typename math::polynomial_dfs<typename KZGScheme::field_type::value_type>>
class kzg_commitment_scheme_v2 : public polys_evaluator<typename KZGScheme::params_type, typename KZGScheme::commitment_type, PolynomialType>{
template<typename KZGScheme>
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;
Expand All @@ -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<field_type>;
using single_commitment_type = typename KZGScheme::single_commitment_type;

struct proof_type {
eval_storage<field_type> 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:
Expand Down Expand Up @@ -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){
Expand Down
3 changes: 3 additions & 0 deletions include/nil/crypto3/zk/commitments/polynomial/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace nil {
using eval_storage_type = typename LPCScheme::eval_storage_type;
using preprocessed_data_type = std::map<std::size_t, std::vector<value_type>>;

constexpr static bool is_lpc = true;

private:
std::map<std::size_t, precommitment_type> _trees;
typename fri_type::params_type _fri_params;
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#ifndef CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP
#define CRYPTO3_ZK_PLONK_PLACEHOLDER_PROOF_HPP

#include <cstddef>
#include <map>

namespace nil {
namespace crypto3 {
namespace zk {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit af3caeb

Please sign in to comment.