Skip to content

Commit

Permalink
Fixed marshalling for LPC commitment state.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed Aug 30, 2024
1 parent 45c206e commit 4cb22cc
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace nil {
integral_type,
// constexpr static std::size_t m;
integral_type,
// constexpr static std::uint32_t grinding_type::mask; If use_grinding==false, this will be 0.
// constexpr static std::uint32_t grinding_parameters; If use_grinding==false, this will be 0.
integral_type,
// const std::size_t max_degree;
integral_type,
Expand Down Expand Up @@ -188,21 +188,25 @@ namespace nil {
make_commitment_params(const typename commitment_params<nil::marshalling::field_type<Endianness>, CommitmentSchemeType, std::enable_if_t<nil::crypto3::zk::is_lpc<CommitmentSchemeType>>>::type &filled_params) {
using CommitmentParamsType = typename CommitmentSchemeType::params_type;

auto step_list = make_integer_vector<Endianness, std::size_t>(std::get<5>(filled_params.value()));
std::size_t lambda = std::get<0>(filled_params.value()).value();
std::size_t r = std::accumulate(step_list.begin(), step_list.end(), 0);
// We skip value #1 which is 'm'. It's a static value, cannot be set from a marshalling.
// We still need to include it when converting to a marshalling structure, to include it
// in the transcript value intialization.
std::size_t grinding_parameter = std::get<2>(filled_params.value()).value();
std::size_t max_degree = std::get<3>(filled_params.value()).value();
std::size_t degree_log = std::ceil(std::log2(max_degree));

// We skip value #4, which is unity roots. They will be generated again.

auto step_list = make_integer_vector<Endianness, std::size_t>(std::get<5>(filled_params.value()));
std::size_t expand_factor = std::get<6>(filled_params.value()).value();
std::size_t grinding_parameter = std::get<2>(filled_params.value()).value();
auto D = math::calculate_domain_set<typename CommitmentParamsType::field_type>(r + expand_factor + 1, r);
// TODO: check generators correctness
std::size_t r = std::accumulate(step_list.begin(), step_list.end(), 0);

return CommitmentParamsType(
max_degree,
D,
step_list,
expand_factor,
degree_log,
lambda,
expand_factor,
(grinding_parameter != 0),
grinding_parameter
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ namespace nil {
namespace marshalling {
namespace types {

// Default commitment marshalling typetype.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
// Default commitment marshalling type.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void>
struct commitment;

// Default commitment marshalling typetype.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
// Default commitment marshalling type.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void>
struct commitment_preprocessed_data;

// Default commitment scheme proof marshalling type in fact it'll be one of tuple's elements for LPC and KZG
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void>
struct eval_proof;

template < typename TTypeBase, typename EvalStorage >
template<typename TTypeBase, typename EvalStorage>
using eval_storage = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/commitment_params.hpp>
#include <nil/crypto3/marshalling/containers/types/merkle_proof.hpp>
#include <nil/crypto3/marshalling/containers/types/merkle_tree.hpp>

#include <nil/crypto3/zk/commitments/type_traits.hpp>

Expand Down Expand Up @@ -219,6 +220,9 @@ namespace nil {
return proof;
}

template <typename TTypeBase, typename LPCScheme, typename enable = void>
struct precommitment_type;

// Will be used to store precommitment type of a commitment scheme. It's useful only for LPC for now,
// and in practive precommitment contains a merkle tree. The following check checks that statement,
// that the precommitment is a merkle tree.
Expand All @@ -235,6 +239,9 @@ namespace nil {
using type = merkle_tree<TTypeBase, typename LPCScheme::precommitment_type>;
};

template <typename TTypeBase, typename CommitmentScheme, typename enable = void>
struct commitment_scheme_state;

// We need the ability to save the whole state of a commitment scheme, every sinlge field,
// so we can resume our program's execution from where it was stopped.
// This will allow us to separate the preprocessor from prover, because LPC has a preprocess step, which
Expand All @@ -247,18 +254,18 @@ namespace nil {
// std::map<std::size_t, precommitment_type> _trees;
nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, std::int64_t>,
nil::marshalling::types::integral<TTypeBase, std::size_t>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,
nil::marshalling::types::array_list<
TTypeBase,
precommitment_type<TTypeBase, typename LPCScheme::precommitment_type>,
typename precommitment_type<TTypeBase, LPCScheme>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,
// typename fri_type::params_type _fri_params;
commitment_params<TTypeBase, LPCScheme>,
typename commitment_params<TTypeBase, LPCScheme>::type,

// value_type _etha;
field_element<TTypeBase, typename LPCScheme::value_type>,
Expand All @@ -270,16 +277,17 @@ namespace nil {
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,
// TODO(martun): next value was supposed to be a vector of bool, but our marshalling core
// does not allow us to create an array_list of bools.
nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, bool>,
nil::marshalling::types::integral<TTypeBase, std::size_t>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// preprocessed_data_type _fixed_polys_values;
typename commitment_preprocessed_data<
nil::marshalling::field_type<Endianness>, LPCScheme,
TTypeBase, LPCScheme,
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>
>::type
>
Expand All @@ -291,7 +299,7 @@ namespace nil {
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>>::type
fill_commitment_scheme(const LPCScheme &scheme) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = commitment_scheme_state<nil::marshalling::field_type<Endianness>, LPCScheme>;
using result_type = typename commitment_scheme_state<nil::marshalling::field_type<Endianness>, LPCScheme>::type;

// std::map<std::size_t, precommitment_type> _trees;
nil::marshalling::types::array_list<
Expand All @@ -302,13 +310,16 @@ namespace nil {
> filled_trees_keys;
nil::marshalling::types::array_list<
TTypeBase,
precommitment_type<TTypeBase, typename LPCScheme::precommitment_type>,
typename precommitment_type<TTypeBase, LPCScheme>::type,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_trees_values;
for (const auto&[key, value]: scheme.get_trees()) {
filled_trees_keys.value().push_back(nil::marshalling::types::integral<TTypeBase, std::size_t>(key));
filled_trees_values.value().push_back(fill_precommitment<Endianness, LPCScheme>(value));
// Precommitment for LPC is a merkle tree. We may want to abstract away this part into a separate
// fill_precommitment function.
filled_trees_values.value().push_back(
fill_merkle_tree<typename LPCScheme::precommitment_type, Endianness>(value));
}

//std::map<std::size_t, bool> _batch_fixed;
Expand All @@ -320,61 +331,69 @@ namespace nil {
> filled_batch_fixed_keys;
nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, bool>,
nil::marshalling::types::integral<TTypeBase, std::size_t>,
nil::marshalling::option::sequence_size_field_prefix<
nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_batch_fixed_values;
for (const auto&[key, value]: scheme.get_batch_fixed()) {
filled_batch_fixed_keys.value().push_back(
nil::marshalling::types::integral<TTypeBase, std::size_t>(key));
filled_batch_fixed_values.value(),push_back(
nil::marshalling::types::integral<TTypeBase, bool>(value));
// Here we convert the value, that is a 'bool' into size_t, which is not good.
filled_batch_fixed_values.value().push_back(
nil::marshalling::types::integral<TTypeBase, std::size_t>(value));
}

return result_type(std::make_tuple(
filled_trees_keys,
filled_trees_values,
fill_commitment_params<TTypeBase, LPCScheme>(scheme.get_fri_params()),
fill_commitment_params<Endianness, LPCScheme>(scheme.get_fri_params()),
field_element<TTypeBase, typename LPCScheme::value_type>(scheme.get_etha()),
filled_batch_fixed_keys,
filled_batch_fixed_values,
fill_commitment_preprocessed_data<Endianness, LPCScheme>(scheme.get_fixed_polys_values())
);
));
}

template<typename Endianness, typename LPCScheme>
LPCScheme make_commitment_scheme(
typename commitment_scheme_state<
nil::marshalling::field_type<Endianness>, LPCScheme,
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>>::type& fill_commitment_scheme
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>>::type& filled_commitment_scheme
) {
std::map<std::size_t, precommitment_type> trees;
using TTypeBase = typename nil::marshalling::field_type<Endianness>;

std::map<std::size_t, typename LPCScheme::precommitment_type> trees;
// TODO(martun): this check must be made in release mode as well, maybe we need to start returning statuses
// from make_ functions.
const auto& filled_tree_keys = std::get<0>(fill_commitment_scheme.value()).value();
const auto& filled_tree_values = std::get<1>(fill_commitment_scheme.value()).value();
BOOST_ASSERT(filled_tree_keys.size(), filled_tree_values.size());
const auto& filled_tree_keys = std::get<0>(filled_commitment_scheme.value()).value();
const auto& filled_tree_values = std::get<1>(filled_commitment_scheme.value()).value();
BOOST_ASSERT(filled_tree_keys.size() == filled_tree_values.size());

for (std::size_t i = 0; i < filled_tree_keys.size(); i++) {
trees[filled_tree_keys[i]] = filled_tree_values[i];
trees[std::size_t(filled_tree_keys[i].value())] =
make_merkle_tree<typename LPCScheme::precommitment_type, Endianness>(
filled_tree_values[i]);
}

typename fri_type::params_type fri_params = make_commitment_params<TTypeBase, LPCScheme>(
std::get<2>(fill_commitment_scheme.value()).value());
value_type etha = std::get<3>(fill_commitment_scheme.value()).value();
typename LPCScheme::fri_type::params_type fri_params = make_commitment_params<Endianness, LPCScheme>(
std::get<2>(filled_commitment_scheme.value()));
typename LPCScheme::value_type etha = std::get<3>(filled_commitment_scheme.value()).value();

std::map<std::size_t, bool> batch_fixed;
const auto& batch_fixed_keys = std::get<4>(fill_commitment_scheme.value()).value();
const auto& batch_fixed_values = std::get<5>(fill_commitment_scheme.value()).value();
BOOST_ASSERT(batch_fixed_keys.size(), batch_fixed_values.size());
const auto& batch_fixed_keys = std::get<4>(filled_commitment_scheme.value()).value();
const auto& batch_fixed_values = std::get<5>(filled_commitment_scheme.value()).value();
BOOST_ASSERT(batch_fixed_keys.size() == batch_fixed_values.size());

for (std::size_t i = 0; i < batch_fixed_keys.size(); i++) {
batch_fixed[batch_fixed_keys[i]] = batch_fixed_values[i];
// Here we convert the value from type size_t back into a 'bool', which is not good.
batch_fixed[std::size_t(batch_fixed_keys[i].value())] = bool(batch_fixed_values[i].value());
}

preprocessed_data_type fixed_polys_values = make_commitment_preprocessed_data<Endianness, LPCScheme>(
std::get<6>(fill_commitment_scheme.value()).value());
typename LPCScheme::preprocessed_data_type fixed_polys_values =
make_commitment_preprocessed_data<Endianness, LPCScheme>(
std::get<6>(filled_commitment_scheme.value()));

return LPCScheme::proof_type(trees, fri_params, etha, batch_fixed, fixed_polys_values);
return LPCScheme(trees, fri_params, etha, batch_fixed, fixed_polys_values);
}
} // namespace types
} // namespace marshalling
Expand Down
16 changes: 6 additions & 10 deletions libs/marshalling/zk/test/lpc_commitment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void test_lpc_state_recovery(const LPC& lpc_commitment_scheme) {
auto read_iter = cv.begin();
test_val_read.read(read_iter, cv.size());
BOOST_CHECK(status == nil::marshalling::status_type::success);
typename LPC constructed_val_read =
LPC constructed_val_read =
nil::crypto3::marshalling::types::make_commitment_scheme<Endianness, LPC>(test_val_read);
BOOST_CHECK(lpc_commitment_scheme == constructed_val_read);
}
Expand Down Expand Up @@ -460,18 +460,14 @@ BOOST_FIXTURE_TEST_CASE(batches_num_3_test, zk::test_tools::random_test_initiali
static_assert(!zk::is_commitment<merkle_tree_type>::value);
static_assert(!zk::is_commitment<std::size_t>::value);

constexpr static const std::size_t d_extended = d;
std::size_t extended_log = boost::static_log2<d_extended>::value;
std::vector<std::shared_ptr<math::evaluation_domain<field_type>>> D =
math::calculate_domain_set<field_type>(extended_log, r);
std::size_t degree_log = boost::static_log2<d>::value;

// Setup params
typename fri_type::params_type fri_params(
d - 1, // max_degree
D,
generate_random_step_list(r, 1, generic_random_engine),
2, //expand_factor
lambda
1, /*max_step*/
degree_log,
lambda,
2 /*expand_factor*/
);

using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme<lpc_type, math::polynomial<typename field_type::value_type>>;
Expand Down
Loading

0 comments on commit 4cb22cc

Please sign in to comment.