Skip to content

Commit

Permalink
Added is_kzg and is_lpc type_traits #309
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Mar 10, 2024
1 parent 497f415 commit 12edb2b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
8 changes: 4 additions & 4 deletions include/nil/crypto3/zk/commitments/polynomial/kzg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,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 @@ -671,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 @@ -849,7 +849,7 @@ namespace nil {
typename KZGScheme::commitment_type,
typename KZGScheme::poly_type> {
public:
constexpr static bool is_kzg_commitment_scheme_v2 = true;
static constexpr bool is_kzg(){ return true; }

using curve_type = typename KZGScheme::curve_type;
using field_type = typename KZGScheme::field_type;
Expand Down Expand Up @@ -912,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
4 changes: 2 additions & 2 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 All @@ -67,8 +69,6 @@ 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
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

0 comments on commit 12edb2b

Please sign in to comment.