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

Change how we handle transcript initialization, hash constraint syste… #269

Merged
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 @@ -69,7 +69,8 @@ namespace nil {
* <https://eprint.iacr.org/2019/1400.pdf>
*/
template<typename FieldType, typename MerkleTreeHashType, typename TranscriptHashType,
std::size_t Lambda, std::size_t M, bool UseGrinding = false, typename GrindingType = proof_of_work<TranscriptHashType>>
std::size_t Lambda, std::size_t M, bool UseGrinding = false,
typename GrindingType = nil::crypto3::zk::commitments::proof_of_work<TranscriptHashType>>
struct basic_batched_fri {
BOOST_STATIC_ASSERT_MSG(M == 2, "unsupported m value!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef PROOF_OF_WORK_HPP
#define PROOF_OF_WORK_HPP
#ifndef CRYPTO3_PROOF_OF_WORK_HPP
#define CRYPTO3_PROOF_OF_WORK_HPP

#include <boost/property_tree/ptree.hpp>

Expand Down Expand Up @@ -88,4 +88,4 @@ namespace nil {
}
}

#endif
#endif // CRYPTO3_PROOF_OF_WORK_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>

#include <nil/crypto3/zk/transcript/fiat_shamir.hpp>

#include <nil/crypto3/marshalling/zk/types/placeholder/transcript_initialization_context.hpp>
Expand Down Expand Up @@ -64,7 +66,6 @@ namespace nil {

// All fields below this line must be included in the transcript initilization, including
// static const fields.

constexpr static const std::size_t witness_columns = PlaceholderParamsType::witness_columns;
constexpr static const std::size_t public_input_columns = PlaceholderParamsType::public_input_columns;
constexpr static const std::size_t constant_columns = PlaceholderParamsType::constant_columns;
Expand All @@ -86,8 +87,9 @@ namespace nil {
};

template <typename PlaceholderParamsType, typename transcript_hash_type>
void init_transcript(
transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>& transcript,
typename transcript_hash_type::digest_type compute_constraint_system_with_params_hash(
const plonk_constraint_system<typename PlaceholderParamsType::field_type,
typename PlaceholderParamsType::arithmetization_params> &constraint_system,
std::size_t rows_amount,
std::size_t usable_rows_amount,
const typename PlaceholderParamsType::commitment_scheme_type::params_type& commitment_params,
Expand All @@ -104,15 +106,29 @@ namespace nil {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using value_marshalling_type = nil::crypto3::marshalling::types::transcript_initialization_context<
TTypeBase, nil::crypto3::zk::snark::detail::transcript_initialization_context<PlaceholderParamsType>>;
auto filled_val = nil::crypto3::marshalling::types::fill_transcript_initialization_context<
auto filled_context = nil::crypto3::marshalling::types::fill_transcript_initialization_context<
Endianness, nil::crypto3::zk::snark::detail::transcript_initialization_context<PlaceholderParamsType>>(context);

std::vector<std::uint8_t> cv(filled_val.length(), 0x00);
std::vector<std::uint8_t> cv(filled_context.length(), 0x00);
auto write_iter = cv.begin();
nil::marshalling::status_type status = filled_val.write(write_iter, cv.size());
nil::marshalling::status_type status = filled_context.write(write_iter, cv.size());
BOOST_CHECK(status == nil::marshalling::status_type::success);

// Append constraint_system to the buffer "cv".
using FieldType = typename PlaceholderParamsType::field_type;
using ConstraintSystem = plonk_constraint_system<FieldType, typename PlaceholderParamsType::arithmetization_params>;
using constraint_system_marshalling_type = nil::crypto3::marshalling::types::plonk_constraint_system<TTypeBase, ConstraintSystem>;

auto filled_constraint_system = nil::crypto3::marshalling::types::fill_plonk_constraint_system<Endianness, ConstraintSystem>(constraint_system);
cv.resize(filled_context.length() + filled_constraint_system.length(), 0x00);

// Function write wants an lvalue as 1st parameter.
write_iter = cv.begin() + filled_context.length();
status = filled_constraint_system.write(write_iter, filled_constraint_system.length());
BOOST_CHECK(status == nil::marshalling::status_type::success);

// TODO(martun): uncomment this after fix.
// transcript(cv);
// Return hash of "cv", which contains concatenated constraint system and other initialization parameters.
return hash<transcript_hash_type>(cv);
}
} // namespace detail
} // namespace snark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ namespace nil {
};

struct verification_key {
typename transcript_hash_type::digest_type constraint_system_hash;
typename transcript_hash_type::digest_type constraint_system_with_params_hash;
commitment_type fixed_values_commitment;

bool operator==(const verification_key &rhs) const {
return constraint_system_hash == rhs.constraint_system_hash &&
return constraint_system_with_params_hash == rhs.constraint_system_with_params_hash &&
fixed_values_commitment == rhs.fixed_values_commitment;
}

Expand All @@ -98,7 +98,7 @@ namespace nil {
std::string to_string() const{
std::stringstream ss;

ss << constraint_system_hash <<" " <<fixed_values_commitment;
ss << constraint_system_with_params_hash <<" " <<fixed_values_commitment;
return ss.str();
}
};
Expand Down Expand Up @@ -494,39 +494,26 @@ namespace nil {
std::array<std::set<int>, ParamsType::arithmetization_params::total_columns> c_rotations =
columns_rotations(constraint_system, table_description);

// Push fixed values and marshalled circuit to transcript.
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;
using ConstraintSystem = plonk_constraint_system<FieldType, typename ParamsType::arithmetization_params>;
using value_marshalling_type = nil::crypto3::marshalling::types::plonk_constraint_system<TTypeBase, ConstraintSystem>;
typename transcript_hash_type::digest_type constraint_system_with_params_hash =
nil::crypto3::zk::snark::detail::compute_constraint_system_with_params_hash<ParamsType, transcript_hash_type>(
constraint_system,
N_rows,
table_description.usable_rows_amount,
commitment_scheme.get_commitment_params(),
"Default application dependent transcript initialization string");

auto filled_val = nil::crypto3::marshalling::types::fill_plonk_constraint_system<Endianness, ConstraintSystem>(constraint_system);
std::vector<std::uint8_t> cv(filled_val.length(), 0x00);

// Function write wants an lvalue as 1st parameter.
auto write_iter = cv.begin();
nil::marshalling::status_type status = filled_val.write(write_iter, cv.size());
typename transcript_hash_type::digest_type circuit_hash = hash<transcript_hash_type>(cv);

typename preprocessed_data_type::verification_key vk = {circuit_hash, public_commitments.fixed_values};
typename preprocessed_data_type::verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values};
typename preprocessed_data_type::common_data_type common_data (
std::move(public_commitments), std::move(c_rotations),
N_rows, table_description.usable_rows_amount, max_gates_degree, vk
);

transcript_type transcript(std::vector<std::uint8_t>({}));
transcript(vk.constraint_system_hash);
transcript(vk.constraint_system_with_params_hash);
transcript(vk.fixed_values_commitment);

nil::crypto3::zk::snark::detail::init_transcript<ParamsType, transcript_hash_type>(
transcript,
common_data.rows_amount,
common_data.usable_rows_amount,
commitment_scheme.get_commitment_params(),
"Default application dependent transcript initialization string"
);

common_data.commitment_scheme_data = commitment_scheme.preprocess(transcript);

// Push circuit description to transcript
preprocessed_data_type preprocessed_data({
std::move(public_polynomial_table),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,10 @@ namespace nil {
, _is_lookup_enabled(constraint_system.lookup_gates().size() > 0)
, transcript(std::vector<std::uint8_t>({}))
{
// 1. Add circuit definition to transcript
// transcript(short_description);

// Initialize transcript.
transcript(preprocessed_public_data.common_data.vk.constraint_system_hash);
transcript(preprocessed_public_data.common_data.vk.constraint_system_with_params_hash);
transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment);

nil::crypto3::zk::snark::detail::init_transcript<ParamsType, transcript_hash_type>(
transcript,
preprocessed_public_data.common_data.rows_amount,
preprocessed_public_data.common_data.usable_rows_amount,
_commitment_scheme.get_commitment_params(),
"Default application dependent transcript initialization string"
);

// Setup commitment scheme. LPC adds an additional point here.
_commitment_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/gates_argument.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/detail/transcript_initialization_context.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -166,19 +169,10 @@ namespace nil {
const plonk_constraint_system<FieldType, typename ParamsType::arithmetization_params> &constraint_system,
commitment_scheme_type commitment_scheme
) {
// 1. Add circuit definition to transcript
// transcript(short_description);
transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(std::vector<std::uint8_t>({}));
transcript(preprocessed_public_data.common_data.vk.constraint_system_hash);
transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment);

nil::crypto3::zk::snark::detail::init_transcript<ParamsType, transcript_hash_type>(
transcript,
preprocessed_public_data.common_data.rows_amount,
preprocessed_public_data.common_data.usable_rows_amount,
commitment_scheme.get_commitment_params(),
"Default application dependent transcript initialization string"
);
transcript(preprocessed_public_data.common_data.vk.constraint_system_with_params_hash);
transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment);

// Setup commitment scheme. LPC adds an additional point here.
commitment_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data);
Expand Down
Loading