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

Update common data marshalling functions for poseidon compatibility #90 #91

Open
wants to merge 1 commit 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 @@ -117,10 +117,21 @@ namespace nil {
nil::marshalling::types::integral<TTypeBase, octet_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_constraint_system_with_params_hash;
for( std::size_t i = 0; i < common_data.vk.constraint_system_with_params_hash.size(); i++){
filled_constraint_system_with_params_hash.value().push_back(
nil::marshalling::types::integral<TTypeBase, octet_type>(common_data.vk.constraint_system_with_params_hash[i])
);
if constexpr(nil::crypto3::hashes::is_poseidon<typename CommonDataType::transcript_hash_type>::value){
auto integral = typename CommonDataType::field_type::integral_type(common_data.vk.constraint_system_with_params_hash.data);
std::vector<unsigned char> blob;
export_bits(integral, std::back_inserter(blob), 8);
for( std::size_t i = blob.size(); i > 0; i--){
filled_constraint_system_with_params_hash.value().push_back(
nil::marshalling::types::integral<TTypeBase, octet_type>(blob[i-1])
);
}
} else {
for( std::size_t i = 0; i < common_data.vk.constraint_system_with_params_hash.size(); i++){
filled_constraint_system_with_params_hash.value().push_back(
nil::marshalling::types::integral<TTypeBase, octet_type>(common_data.vk.constraint_system_with_params_hash[i])
);
}
}

return result_type(std::make_tuple(
Expand Down Expand Up @@ -159,10 +170,21 @@ namespace nil {

typename CommonDataType::verification_key_type vk;
vk.fixed_values_commitment = fixed_values;
for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){
vk.constraint_system_with_params_hash[i] = (std::get<5>(filled_common_data.value()).value()[i].value());
if constexpr(nil::crypto3::hashes::is_poseidon<typename CommonDataType::transcript_hash_type>::value){
std::vector<std::uint8_t> blob;
for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){
blob.push_back(std::uint8_t(std::get<5>(filled_common_data.value()).value()[i].value()));
}
typename CommonDataType::field_type::integral_type newval;
import_bits(newval, blob.begin(), blob.end(), 8, false);
vk.constraint_system_with_params_hash = typename CommonDataType::field_type::value_type(newval);
} else {
for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){
vk.constraint_system_with_params_hash[i] = (std::get<5>(filled_common_data.value()).value()[i].value());
}
}


return CommonDataType(commitments, columns_rotations, rows_amount, usable_rows_amount, max_gates_degree, vk);
}
} // namespace types
Expand Down
77 changes: 77 additions & 0 deletions test/placeholder_common_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <nil/crypto3/hash/type_traits.hpp>
#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/keccak.hpp>
#include <nil/crypto3/hash/poseidon.hpp>

#include <nil/crypto3/random/algebraic_random_device.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>
Expand Down Expand Up @@ -195,6 +196,82 @@ struct test_initializer {
}
};

BOOST_AUTO_TEST_SUITE(placeholder_circuit1_poseidon)
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;

using curve_type = algebra::curves::pallas;
using field_type = typename curve_type::base_field_type;
using poseidon_type = hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy<field_type>>;
using merkle_hash_type = poseidon_type;
using transcript_hash_type = poseidon_type;
constexpr static const std::size_t table_rows_log = 4;

struct placeholder_test_params {
constexpr static const std::size_t table_rows = 1 << table_rows_log;
constexpr static const std::size_t permutation_size = 4;
constexpr static const std::size_t usable_rows = (1 << table_rows_log) - 3;


constexpr static const std::size_t witness_columns = witness_columns_1;
constexpr static const std::size_t public_input_columns = public_columns_1;
constexpr static const std::size_t constant_columns = constant_columns_1;
constexpr static const std::size_t selector_columns = selector_columns_1;

constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t m = 2;
};
typedef placeholder_circuit_params<field_type> circuit_params;
using transcript_type = typename transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;

using lpc_params_type = commitments::list_polynomial_commitment_params<
merkle_hash_type,
transcript_hash_type,
placeholder_test_params::lambda,
placeholder_test_params::m,
true
>;

using lpc_type = commitments::list_polynomial_commitment<field_type, lpc_params_type>;
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
using policy_type = zk::snark::detail::placeholder_policy<field_type, lpc_placeholder_params_type>;

BOOST_AUTO_TEST_CASE(prover_test) {
auto circuit = circuit_test_1<field_type>();

plonk_table_description<field_type> desc(
placeholder_test_params::witness_columns,
placeholder_test_params::public_input_columns,
placeholder_test_params::constant_columns,
placeholder_test_params::selector_columns
);

desc.rows_amount = placeholder_test_params::table_rows;
desc.usable_rows_amount = placeholder_test_params::usable_rows;

typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates);
typename policy_type::variable_assignment_type assignments = circuit.table;

std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};


typename lpc_type::fri_type::params_type fri_params = create_fri_params<typename lpc_type::fri_type, field_type>(table_rows_log);
lpc_scheme_type lpc_scheme(fri_params);

typename placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type
lpc_preprocessed_public_data = placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::process(
constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size()
);

using common_data_type = placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type::common_data_type;
if(has_argv("--print"))
test_placeholder_common_data<common_data_type>(lpc_preprocessed_public_data.common_data, "circuit1");
else
test_placeholder_common_data<common_data_type>(lpc_preprocessed_public_data.common_data);
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(placeholder_circuit1)
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;
Expand Down
101 changes: 101 additions & 0 deletions test/placeholder_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <nil/crypto3/hash/type_traits.hpp>
#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/keccak.hpp>
#include <nil/crypto3/hash/poseidon.hpp>

#include <nil/crypto3/random/algebraic_random_device.hpp>
#include <nil/crypto3/random/algebraic_engine.hpp>
Expand Down Expand Up @@ -291,12 +292,112 @@ struct test_initializer {
}
};

BOOST_AUTO_TEST_SUITE(placeholder_circuit1_poseidon)
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;

using curve_type = algebra::curves::pallas;
using field_type = typename curve_type::base_field_type;
using poseidon_type = hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy<field_type>>;

using merkle_hash_type = poseidon_type;
using transcript_hash_type = poseidon_type;
constexpr static const std::size_t table_rows_log = 4;

struct placeholder_test_params {
constexpr static const std::size_t table_rows = 1 << table_rows_log;
constexpr static const std::size_t permutation_size = 4;
constexpr static const std::size_t usable_rows = (1 << table_rows_log) - 3;


constexpr static const std::size_t witness_columns = witness_columns_1;
constexpr static const std::size_t public_input_columns = public_columns_1;
constexpr static const std::size_t constant_columns = constant_columns_1;
constexpr static const std::size_t selector_columns = selector_columns_1;

constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t m = 2;
};
typedef placeholder_circuit_params<field_type> circuit_params;
using transcript_type = typename transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;

using lpc_params_type = commitments::list_polynomial_commitment_params<
merkle_hash_type,
transcript_hash_type,
placeholder_test_params::lambda,
placeholder_test_params::m,
true,
crypto3::zk::commitments::proof_of_work<transcript_hash_type, std::uint32_t, 0xFFFF8000 >
>;

using lpc_type = commitments::list_polynomial_commitment<field_type, lpc_params_type>;
using lpc_scheme_type = typename commitments::lpc_commitment_scheme<lpc_type>;
using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params<circuit_params, lpc_scheme_type>;
using policy_type = zk::snark::detail::placeholder_policy<field_type, lpc_placeholder_params_type>;

BOOST_FIXTURE_TEST_CASE(proof_marshalling_test, test_initializer) {
auto circuit = circuit_test_1<field_type>(test_global_alg_rnd_engine<field_type>);

plonk_table_description<field_type> desc(
placeholder_test_params::witness_columns,
placeholder_test_params::public_input_columns,
placeholder_test_params::constant_columns,
placeholder_test_params::selector_columns
);

desc.rows_amount = placeholder_test_params::table_rows;
desc.usable_rows_amount = placeholder_test_params::usable_rows;

typename policy_type::constraint_system_type constraint_system(
circuit.gates,
circuit.copy_constraints,
circuit.lookup_gates,
circuit.lookup_tables
);
typename policy_type::variable_assignment_type assignments = circuit.table;

std::vector<std::size_t> columns_with_copy_constraints = {0, 1, 2, 3};


typename lpc_type::fri_type::params_type fri_params = create_fri_params<typename lpc_type::fri_type, field_type>(table_rows_log);
lpc_scheme_type lpc_scheme(fri_params);

typename placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type
lpc_preprocessed_public_data = placeholder_public_preprocessor<field_type, lpc_placeholder_params_type>::process(
constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size()
);

typename placeholder_private_preprocessor<field_type, lpc_placeholder_params_type>::preprocessed_data_type
lpc_preprocessed_private_data = placeholder_private_preprocessor<field_type, lpc_placeholder_params_type>::process(
constraint_system, assignments.private_table(), desc
);

auto lpc_proof = placeholder_prover<field_type, lpc_placeholder_params_type>::process(
lpc_preprocessed_public_data, lpc_preprocessed_private_data, desc, constraint_system, lpc_scheme
);

if (has_argv("--print")) {
print_placeholder_proof_with_params<Endianness, lpc_placeholder_params_type>(
lpc_preprocessed_public_data,
lpc_proof, lpc_scheme, desc, "circuit1"
);
} else {
test_placeholder_proof<Endianness, placeholder_proof<field_type, lpc_placeholder_params_type>>(lpc_proof, fri_params);
}
auto verifier_res = placeholder_verifier<field_type, lpc_placeholder_params_type>::process(
lpc_preprocessed_public_data, lpc_proof, desc, constraint_system, lpc_scheme
);
BOOST_CHECK(verifier_res);
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(placeholder_circuit1)
using Endianness = nil::marshalling::option::big_endian;
using TTypeBase = nil::marshalling::field_type<Endianness>;

using curve_type = algebra::curves::pallas;
using field_type = typename curve_type::base_field_type;

using merkle_hash_type = hashes::keccak_1600<256>;
using transcript_hash_type = hashes::keccak_1600<256>;
constexpr static const std::size_t table_rows_log = 4;
Expand Down
Loading