Skip to content

Commit

Permalink
Fixed constants amount for sha256_process.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iluvmagick committed Feb 21, 2024
1 parent 3b6ebc1 commit bcee85e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ namespace nil {
integral_type(var_value(assignment, instance_input.data[1]).data)};
std::array<std::array<std::array<integral_type, 3>, 4>, 2> range_chunks;
std::array<std::array<integral_type, 4>, 2> output_chunks;
std::size_t shift = 0;

for (std::size_t data_idx = 0; data_idx < 2; data_idx++) {
for (std::size_t chunk_idx = 0; chunk_idx < 4; chunk_idx++) {
Expand Down Expand Up @@ -275,15 +274,18 @@ namespace nil {
}

template<typename BlueprintFieldType>
std::size_t generate_gates(
std::array<std::size_t, 2> generate_gates(
const plonk_native_decomposition<BlueprintFieldType> &component,
circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> &bp,
assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>
&assignment,
const typename plonk_native_decomposition<BlueprintFieldType>::input_type
&instance_input) {
&instance_input,
const typename lookup_library<BlueprintFieldType>::left_reserved_type &lookup_tables_indices) {

using var = typename plonk_native_decomposition<BlueprintFieldType>::var;
using constraint = crypto3::zk::snark::plonk_constraint<BlueprintFieldType>;
using lookup_constraint = crypto3::zk::snark::plonk_lookup_constraint<BlueprintFieldType>;

const typename BlueprintFieldType::integral_type one = 1;
std::array<std::size_t, 2> selectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,13 @@ namespace nil {
&instance_input,
const std::size_t start_row_index) {

std::size_t row = start_row_index + 2;
using var = typename plonk_sha256_process<BlueprintFieldType>::var;
std::size_t row = start_row_index;
for (std::size_t i = 0; i < 8; ++i) {
bp.add_copy_constraint({var(component.W(i), row, false), instance_input.input_state[i]});
}

row = start_row_index + 2;
for (std::size_t i = 1; i <= 15; ++i) {
bp.add_copy_constraint(
{var(component.W(0), row + (i - 1) * 5 + 0, false), instance_input.input_words[i]});
Expand Down Expand Up @@ -1389,7 +1393,7 @@ namespace nil {
typename BlueprintFieldType::value_type h = input_state[7];

std::array<typename BlueprintFieldType::integral_type, 8> sparse_values {};
for (std::size_t i = 0; i < 4; i++) {
for (std::size_t i = 0; i < 8; i++) {
assignment.witness(component.W(i), row) = input_state[i];
typename BlueprintFieldType::integral_type integral_input_state_sparse =
typename BlueprintFieldType::integral_type(input_state[i].data);
Expand All @@ -1404,32 +1408,12 @@ namespace nil {
}

std::vector<std::size_t> input_state_sparse_sizes = {32};
const auto base = i < 4 ? plonk_sha256_process<BlueprintFieldType>::base4
: plonk_sha256_process<BlueprintFieldType>::base7;
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> input_state_sparse_chunks =
detail::split_and_sparse<BlueprintFieldType>(
input_state_sparse, input_state_sparse_sizes,
plonk_sha256_process<BlueprintFieldType>::base4);
assignment.witness(component.W(i), row + 1) = input_state_sparse_chunks[1][0];
sparse_values[i] = input_state_sparse_chunks[1][0];
}
for (std::size_t i = 4; i < 8; i++) {
assignment.witness(component.W(i), row) = input_state[i];
typename BlueprintFieldType::integral_type integral_input_state_sparse =
typename BlueprintFieldType::integral_type(input_state[i].data);
std::vector<bool> input_state_sparse(32);
{
nil::marshalling::status_type status;
std::vector<bool> input_state_sparse_all =
nil::marshalling::pack<nil::marshalling::option::big_endian>(integral_input_state_sparse,
status);
std::copy(input_state_sparse_all.end() - 32, input_state_sparse_all.end(),
input_state_sparse.begin());
}

std::vector<std::size_t> input_state_sparse_sizes = {32};
std::array<std::vector<typename BlueprintFieldType::integral_type>, 2> input_state_sparse_chunks =
detail::split_and_sparse<BlueprintFieldType>(
input_state_sparse, input_state_sparse_sizes,
plonk_sha256_process<BlueprintFieldType>::base7);
base);
assignment.witness(component.W(i), row + 1) = input_state_sparse_chunks[1][0];
sparse_values[i] = input_state_sparse_chunks[1][0];
}
Expand Down
15 changes: 8 additions & 7 deletions include/nil/blueprint/utils/connectedness_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ namespace nil {
const std::size_t end_row = start_row_index + rows_amount;

nil::blueprint::assignment<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> output_assignment(
assignment.witnesses_amount(), assignment.constants_amount(),
assignment.public_inputs_amount(), assignment.selectors_amount()
assignment.witnesses_amount(), assignment.public_inputs_amount(),
assignment.constants_amount(), assignment.selectors_amount()
);

// We do '+1' in all the assignments to separate the unassigned cells (0 by default)
// from the ones which actually got checked.
for (std::size_t witness_column = 0; witness_column < row_size; witness_column++) {
for (std::size_t witness_column = 0; witness_column < assignment.witnesses_amount(); witness_column++) {
std::size_t last_row =
std::min<std::size_t>(end_row, assignment.witness_column_size(witness_column));
for (std::size_t row = start_row_index; row < last_row; row++) {
Expand All @@ -114,15 +114,16 @@ namespace nil {
const auto output_value =
zones.find_set(copy_var_address<BlueprintFieldType>(
row_size, start_row_index, rows_amount, variable)) + 1;
switch (variable.type) {
const auto &variable_ref = variable.get();
switch (variable_ref.type) {
case var::column_type::constant:
output_assignment.constant(variable.index, variable.rotation) = output_value;
output_assignment.constant(variable_ref.index, variable_ref.rotation) = output_value;
break;
case var::column_type::public_input:
output_assignment.public_input(variable.index, variable.rotation) = output_value;
output_assignment.public_input(variable_ref.index, variable_ref.rotation) = output_value;
break;
case var::column_type::witness:
output_assignment.witness(variable.index, variable.rotation) = output_value;
output_assignment.witness(variable_ref.index, variable_ref.rotation) = output_value;
break;
case var::column_type::selector:
BOOST_ASSERT_MSG(false, "Selector variables should not be input variables.");
Expand Down
24 changes: 12 additions & 12 deletions test/hashes/plonk/decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,29 @@ void test_decomposition(std::vector<typename BlueprintFieldType::value_type> pub

auto result_check = [&expected_res](AssignmentType &assignment,
typename component_type::result_type &real_res) {
for (std::size_t i = 0; i < real_res.output.size(); i++){
std::cout << var_value(assignment, real_res.output[i]).data << std::endl;
}
for (std::size_t i = 0; i < expected_res.size(); i++){
std::cout << expected_res[i].data << std::endl;
}
// for (std::size_t i = 0; i < real_res.output.size(); i++){
// std::cout << var_value(assignment, real_res.output[i]).data << std::endl;
// }
// for (std::size_t i = 0; i < expected_res.size(); i++){
// std::cout << expected_res[i].data << std::endl;
// }
for (std::size_t i = 0; i < real_res.output.size(); i++){
assert(expected_res[i] == var_value(assignment, real_res.output[i]));
}
};
auto result_check_to_fail = [&expected_res](AssignmentType &assignment,
typename component_type::result_type &real_res) { };
auto result_check_to_fail = [](AssignmentType &assignment,
typename component_type::result_type &real_res) {};

component_type component_instance({0, 1, 2, 3, 4, 5, 6, 7, 8},{},{});

if (expected_to_pass) {
crypto3::test_component<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
crypto3::test_component<component_type, BlueprintFieldType, hash_type, Lambda>(
component_instance, desc, public_input, result_check, instance_input,
nil::crypto3::detail::connectedness_check_type::WEAK);
nil::blueprint::connectedness_check_type::type::WEAK);
} else {
crypto3::test_component_to_fail<component_type, BlueprintFieldType, ArithmetizationParams, hash_type, Lambda>(
crypto3::test_component_to_fail<component_type, BlueprintFieldType, hash_type, Lambda>(
component_instance, desc, public_input, result_check_to_fail, instance_input,
nil::crypto3::detail::connectedness_check_type::WEAK);
nil::blueprint::connectedness_check_type::type::WEAK);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/hashes/plonk/sha256_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(blueprint_plonk_sha256_process) {
using BlueprintFieldType = typename curve_type::base_field_type;
constexpr std::size_t WitnessColumns = 9;
constexpr std::size_t PublicInputColumns = 1;
constexpr std::size_t ConstantColumns = 20;
constexpr std::size_t ConstantColumns = 33;
constexpr std::size_t SelectorColumns = 30;
using hash_type = nil::crypto3::hashes::keccak_1600<256>;
constexpr std::size_t Lambda = 1;
Expand Down

0 comments on commit bcee85e

Please sign in to comment.