Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Common data, public input sizes #88
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Mar 6, 2024
1 parent 9fac26d commit 90e8e1b
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ namespace nil {
nil::marshalling::types::array_list <TTypeBase,
nil::marshalling::types::integral<TTypeBase, octet_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>
>,
// std::size_t witness_columns;
nil::marshalling::types::integral<TTypeBase, std::size_t>,
// std::size_t public_input_columns;
nil::marshalling::types::integral<TTypeBase, std::size_t>,
// std::size_t constant_columns;
nil::marshalling::types::integral<TTypeBase, std::size_t>,
// std::size_t selector_columns;
nil::marshalling::types::integral<TTypeBase, std::size_t>
>
>;

Expand Down Expand Up @@ -117,10 +125,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 All @@ -129,12 +148,16 @@ namespace nil {
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.rows_amount),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.usable_rows_amount),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.max_gates_degree),
filled_constraint_system_with_params_hash
filled_constraint_system_with_params_hash,
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.witness_columns),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.public_input_columns),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.constant_columns),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.selector_columns)
));
}

template<typename Endianness, typename CommonDataType>
CommonDataType
std::tuple <CommonDataType, typename CommonDataType::table_description_type>
make_placeholder_common_data(const
placeholder_common_data<nil::marshalling::field_type<Endianness>, CommonDataType> &filled_common_data
){
Expand All @@ -153,17 +176,42 @@ namespace nil {
auto rows_amount = std::get<2>(filled_common_data.value()).value();
auto usable_rows_amount = std::get<3>(filled_common_data.value()).value();
auto max_gates_degree = std::get<4>(filled_common_data.value()).value();
auto witness_columns = std::get<6>(filled_common_data.value()).value();
auto public_input_columns = std::get<7>(filled_common_data.value()).value();
auto constant_columns = std::get<8>(filled_common_data.value()).value();
auto selector_columns = std::get<9>(filled_common_data.value()).value();

typename CommonDataType::commitments_type commitments;
commitments.fixed_values = fixed_values;

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);
typename CommonDataType::table_description_type table_description(
witness_columns, public_input_columns, constant_columns, selector_columns,
usable_rows_amount, rows_amount
);

return std::make_tuple(CommonDataType(
commitments, columns_rotations,
rows_amount, usable_rows_amount,
witness_columns, public_input_columns,
constant_columns, selector_columns,
max_gates_degree, vk
), table_description);
}
} // namespace types
} // namespace marshalling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,43 @@ namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
template<typename TTypeBase>
using public_input_sizes_type =
nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, std::size_t>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>;

template<typename TTypeBase, typename PlonkConstraintSystem>
using plonk_constraint_system = nil::marshalling::types::bundle<
TTypeBase, std::tuple<
plonk_gates< TTypeBase, typename PlonkConstraintSystem::gates_container_type::value_type >, // gates
plonk_copy_constraints< TTypeBase, typename PlonkConstraintSystem::variable_type >, // copy constraints
plonk_lookup_gates< TTypeBase, typename PlonkConstraintSystem::lookup_gates_container_type::value_type >, // lookup constraints
// If we don't have lookup gates, we don't need lookup tables
plonk_lookup_tables< TTypeBase, typename PlonkConstraintSystem::lookup_tables_type::value_type > // lookup tables
plonk_lookup_tables< TTypeBase, typename PlonkConstraintSystem::lookup_tables_type::value_type >, // lookup tables
// public input sizes
public_input_sizes_type<TTypeBase>
>
>;

template<typename Endianness, typename PlonkConstraintSystem>
plonk_constraint_system<nil::marshalling::field_type<Endianness>, PlonkConstraintSystem>
fill_plonk_constraint_system(const PlonkConstraintSystem &system) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = plonk_constraint_system<nil::marshalling::field_type<Endianness>, PlonkConstraintSystem>;
public_input_sizes_type<TTypeBase> public_input_sizes;
for(std::size_t i = 0; i < system.public_input_sizes_num(); i++){
public_input_sizes.value().push_back(nil::marshalling::types::integral<nil::marshalling::field_type<Endianness>, std::size_t>(system.public_input_size(i)));
}

return result_type(std::make_tuple(
fill_plonk_gates<Endianness, typename PlonkConstraintSystem::gates_container_type::value_type>(system.gates()),
fill_plonk_copy_constraints<Endianness, typename PlonkConstraintSystem::variable_type>(system.copy_constraints()),
fill_plonk_lookup_gates<Endianness, typename PlonkConstraintSystem::lookup_gates_container_type::value_type>(system.lookup_gates()),
fill_plonk_lookup_tables<Endianness, typename PlonkConstraintSystem::lookup_tables_type::value_type>(system.lookup_tables())
fill_plonk_lookup_tables<Endianness, typename PlonkConstraintSystem::lookup_tables_type::value_type>(system.lookup_tables()),
public_input_sizes
));
}

Expand All @@ -77,11 +93,16 @@ namespace nil {
make_plonk_constraint_system(
const plonk_constraint_system<nil::marshalling::field_type<Endianness>, PlonkConstraintSystem> &filled_system
){
std::vector<std::size_t> public_input_sizes;
for(std::size_t i = 0; i < std::get<4>(filled_system.value()).value().size(); i++){
public_input_sizes.push_back(std::get<4>(filled_system.value()).value().at(i).value());
}
return PlonkConstraintSystem(
make_plonk_gates<Endianness, typename PlonkConstraintSystem::gates_container_type::value_type>(std::get<0>(filled_system.value())),
make_plonk_copy_constraints<Endianness, typename PlonkConstraintSystem::variable_type>(std::get<1>(filled_system.value())),
make_plonk_lookup_gates<Endianness, typename PlonkConstraintSystem::lookup_gates_container_type::value_type>(std::get<2>(filled_system.value())),
make_plonk_lookup_tables<Endianness, typename PlonkConstraintSystem::lookup_tables_type::value_type>(std::get<3>(filled_system.value()))
make_plonk_lookup_tables<Endianness, typename PlonkConstraintSystem::lookup_tables_type::value_type>(std::get<3>(filled_system.value())),
public_input_sizes
);
}
} //namespace types
Expand Down
3 changes: 2 additions & 1 deletion test/detail/circuits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ namespace nil {
std::vector<plonk_gate<FieldType, plonk_constraint<FieldType>>> gates;
std::vector<plonk_copy_constraint<FieldType>> copy_constraints;
std::vector<plonk_lookup_gate<FieldType, plonk_lookup_constraint<FieldType>>> lookup_gates;

std::vector<plonk_lookup_table<FieldType>> lookup_tables;
std::vector<std::size_t> public_input_sizes;

circuit_description() : table_rows(0){
}
Expand Down Expand Up @@ -249,6 +249,7 @@ namespace nil {
typedef placeholder_circuit_params<FieldType> circuit_params;

circuit_description<FieldType, circuit_params, 5, permutation> test_circuit;
test_circuit.public_input_sizes = {3};

std::array<std::vector<typename FieldType::value_type>, table_columns> table;

Expand Down
Loading

0 comments on commit 90e8e1b

Please sign in to comment.