Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implemented KZG placeholder proof and common_data marshalling #300 #308

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 45 additions & 13 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 @@ -319,7 +325,7 @@ namespace nil {
static void update_transcript(const typename KZG::public_key_type &public_key,
typename KZG::transcript_type &transcript) {

/* The procedure of updating the transcript is subject to review and change
/* The procedure of updating the transcript is subject to review and change
* #295 */

nil::marshalling::status_type status;
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 @@ -661,7 +671,7 @@ namespace nil {
}

void update_transcript(std::size_t batch_ind, typename KZGScheme::transcript_type &transcript) {
/* The procedure of updating the transcript is subject to review and change
/* The procedure of updating the transcript is subject to review and change
* #295 */

// Push commitments to transcript
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:
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;
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 @@ -886,7 +912,7 @@ namespace nil {
}

void update_transcript(std::size_t batch_ind, typename KZGScheme::transcript_type &transcript) {
/* The procedure of updating the transcript is subject to review and change
/* The procedure of updating the transcript is subject to review and change
* #295 */

// Push commitments to transcript
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 @@ -52,6 +52,8 @@ namespace nil {
typename LPCScheme::commitment_type, PolynomialType>{

public:
static constexpr bool is_lpc(){return true;}

using field_type = typename LPCScheme::field_type;
using value_type = typename field_type::value_type;
using params_type = typename LPCScheme::params_type;
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
70 changes: 69 additions & 1 deletion include/nil/crypto3/zk/commitments/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,45 @@ namespace nil {
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
class has_available_static_member_function_is_kzg {
struct no { };

protected:
template<typename C>
static void test(std::nullptr_t) {
struct t {
using C::is_kzg;
};
}

template<typename>
static no test(...);

public:
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
class has_available_static_member_function_is_lpc {
struct no { };

protected:
template<typename C>

static void test(std::nullptr_t) {
struct t {
using C::is_lpc;
};
}

template<typename>
static no test(...);

public:
constexpr static const bool value = !std::is_same<no, decltype(test<T>(nullptr))>::value;
};

template<typename T>
struct is_commitment {
using commitment_type = typename member_type_commitment_type<T>::type;
Expand All @@ -109,12 +148,41 @@ namespace nil {
typedef T type;
};

// An idea was copied from this example:
// https://stackoverflow.com/questions/54920801/check-if-static-function-is-available-in-class-at-compile-time

template<typename T, typename Enable = void>
struct is_kzg_struct: std::false_type{
static const bool value = false;
};

template<class T>
struct is_kzg_struct<T, std::enable_if_t<std::is_invocable_r<bool, decltype(T::is_kzg)>::value>>
: std::integral_constant<bool, T::is_kzg()>
{};

template<class T>
constexpr bool is_kzg = is_kzg_struct<T>::value;


template<typename T, typename Enable = void>
struct is_lpc_struct: std::false_type{
static const bool value = false;
};

template<class T>
struct is_lpc_struct<T, std::enable_if_t<std::is_invocable_r<bool, decltype(T::is_lpc)>::value>>
: std::integral_constant<bool, T::is_lpc()>
{};

template<class T>
constexpr bool is_lpc = is_lpc_struct<T>::value;

template<bool Condition, typename Type, std::size_t Size>
struct select_container {
using type = typename std::
conditional<Condition, std::array<Type, Size>, std::vector<Type>>::type;
};

} // namespace zk
} // namespace crypto3
} // namespace nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define CRYPTO3_ZK_PLONK_PLACEHOLDER_TABLE_DESCRIPTION_HPP

#include <nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp>
#include <limits>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -74,6 +75,8 @@ namespace nil {
case plonk_variable<typename FieldType::value_type>::column_type::selector:
return witness_columns + public_input_columns + constant_columns + a.index;
}
/* unreachable*/
return std::numeric_limits<size_t>::max();
}

std::size_t table_width() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ namespace nil {
return !(rhs == *this);
}


std::string to_string() const{
std::stringstream ss;

ss << constraint_system_with_params_hash << " " << fixed_values_commitment;
// TODO: KZG fixed_values_commitments are vector<uint8_t>
// need operator<<(ostream,vector<uint8_t>)?
ss << constraint_system_with_params_hash /*<< " " << fixed_values_commitment*/;
return ss.str();
}
};
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
2 changes: 1 addition & 1 deletion test/commitment/kzg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ struct placeholder_class_test_initializer {
using kzg_scheme_type = typename zk::commitments::kzg_commitment_scheme_v2<kzg_type>;

scalar_value_type alpha = 7;
auto params = typename kzg_type::params_type(8, 8, alpha);
auto params = kzg_scheme_type::create_params(8, alpha);
kzg_scheme_type kzg(params);

typename kzg_type::batch_of_polynomials_type polys(4);
Expand Down
Loading