Skip to content

Commit

Permalink
Signed => unsigned #82
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Oct 18, 2024
1 parent a07d5a3 commit c62c7f1
Show file tree
Hide file tree
Showing 31 changed files with 866 additions and 321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ namespace nil {
using ranges = std::vector<std::pair<std::size_t, std::size_t>>;

public:
static constexpr const std::size_t private_storage_index = std::numeric_limits<std::size_t>::max();
static constexpr const std::size_t batch_private_storage_index = std::numeric_limits<std::size_t>::max() - 1;
static constexpr const std::size_t batch_constant_storage_index = std::numeric_limits<std::size_t>::max() - 2;
static constexpr const std::size_t private_storage_index = std::numeric_limits<std::size_t>::max()-2;
static constexpr const std::size_t batch_private_storage_index = std::numeric_limits<std::size_t>::max() - 3;
static constexpr const std::size_t batch_constant_storage_index = std::numeric_limits<std::size_t>::max() - 4;

assignment(std::size_t witness_amount, std::size_t public_input_amount,
std::size_t constant_amount, std::size_t selector_amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ void test_multiple_arithmetizations(std::string folder_name){
BOOST_AUTO_TEST_SUITE(blueprint_pallas_test_suite)

BOOST_AUTO_TEST_CASE(basic_test) {
// test_multiple_arithmetizations<default_zkllvm_params>("../libs/blueprint/test/verifiers/placeholder/data/merkle_tree_poseidon");
test_multiple_arithmetizations<default_zkllvm_params>("../test/verifiers/placeholder/data/merkle_tree_poseidon");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace nil {
case status_type::error_status_amount:
return "unreachable";
}
return "unreachable";
}
};
} // namespace marshalling
Expand Down
320 changes: 216 additions & 104 deletions crypto3/libs/marshalling/zk/test/detail/circuits.hpp

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions crypto3/libs/marshalling/zk/test/placeholder_common_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,7 @@ struct placeholder_kzg_test_fixture_v2 : public test_tools::random_test_initiali

using policy_type = zk::snark::detail::placeholder_policy<field_type, kzg_placeholder_params_type>;

using circuit_type =
circuit_description<field_type,
placeholder_circuit_params<field_type>,
UsableRowsAmount>;
using circuit_type = circuit_description<field_type, placeholder_circuit_params<field_type>>;

placeholder_kzg_test_fixture_v2()
: desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns)
Expand Down
3 changes: 1 addition & 2 deletions crypto3/libs/marshalling/zk/test/placeholder_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,7 @@ struct placeholder_kzg_test_fixture_v2 : public test_tools::random_test_initiali

using circuit_type =
circuit_description<field_type,
placeholder_circuit_params<field_type>,
usable_rows_amount>;
placeholder_circuit_params<field_type>>;

placeholder_kzg_test_fixture_v2()
: desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns)
Expand Down
66 changes: 66 additions & 0 deletions crypto3/libs/marshalling/zk/test/plonk_constraint_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,69 @@ BOOST_FIXTURE_TEST_CASE(constraint_system_marshalling_test, test_tools::random_t
test_constraint_system<Endianness, typename policy_type::constraint_system_type>(constraint_system);
}
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(placeholder_circuit8)
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;

constexpr static const std::size_t table_rows_log = 4;
constexpr static const std::size_t table_rows = 1 << table_rows_log;
constexpr static const std::size_t usable_rows = 14;

struct placeholder_test_params {
using merkle_hash_type = hashes::keccak_1600<512>;
using transcript_hash_type = hashes::keccak_1600<512>;

constexpr static const std::size_t witness_columns = witness_columns_8;
constexpr static const std::size_t public_input_columns = public_columns_8;
constexpr static const std::size_t constant_columns = constant_columns_8;
constexpr static const std::size_t selector_columns = selector_columns_8;

constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t m = 2;
};

using circuit_params = placeholder_circuit_params<field_type>;
using transcript_type = typename transcript::fiat_shamir_heuristic_sequential<typename placeholder_test_params::transcript_hash_type>;
using lpc_params_type = commitments::list_polynomial_commitment_params<
typename placeholder_test_params::merkle_hash_type,
typename placeholder_test_params::transcript_hash_type,
placeholder_test_params::m
>;

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, circuit_params>;

BOOST_FIXTURE_TEST_CASE(constraint_system_marshalling_test, test_tools::random_test_initializer<field_type>) {
auto circuit = circuit_test_8<field_type>(
alg_random_engines.template get_alg_engine<field_type>(),
generic_random_engine
);
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 = circuit.table_rows;
desc.usable_rows_amount = circuit.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;

if(has_argv("--print"))
test_constraint_system<Endianness, typename policy_type::constraint_system_type>(constraint_system, "circuit7");
else
test_constraint_system<Endianness, typename policy_type::constraint_system_type>(constraint_system);
}
BOOST_AUTO_TEST_SUITE_END()
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,6 @@ namespace nil {
"\t\t\tstate.l_shifted = addmod( state.l_shifted, mulmod(state.shifted_selector_value, mulmod( state.theta_acc, basic_marshalling.get_uint256_be(blob, " << _var_indices.at(shifted_var) * 0x20 << "), modulus), modulus), modulus);" << std::endl;
lookup_str << "\t\t\tstate.theta_acc = mulmod(state.theta_acc, state.theta, modulus);" << std::endl;
}
lookup_str <<
"\t\t\tl = mulmod( l, state.mask, modulus);" << std::endl;
lookup_str <<
"\t\t\tstate.l_shifted = mulmod( state.l_shifted, state.shifted_mask, modulus);" << std::endl;
lookup_str << "\t\t\tstate.g = mulmod(state.g, addmod( state.factor, addmod(l, mulmod(state.beta, state.l_shifted, modulus), modulus), modulus), modulus);" << std::endl;
j++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,6 @@ contract modular_verifier_$TEST_NAME$ is IModularVerifier{
uint256 theta = transcript.get_field_challenge(tr_state, modulus);
state.F[7] = modular_gate_argument.verify(blob[table_offset:table_end_offset], theta);
state.F[7] = mulmod(
state.F[7],
addmod(
1,
modulus - addmod(
basic_marshalling.get_uint256_be(blob, special_selectors_offset),
basic_marshalling.get_uint256_be(blob, special_selectors_offset + 0x40),
modulus
),
modulus
),
modulus
);
// console.log("F[0] = ", state.F[0]);
// console.log("F[1] = ", state.F[1]);
// console.log("F[2] = ", state.F[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ namespace nil {
namespace crypto3 {
namespace zk {
namespace snark {

/************************* PLONK constraint system ****************************/
constexpr static std::size_t const PLONK_SPECIAL_SELECTOR_ALL_USABLE_ROWS_SELECTED = std::numeric_limits<std::size_t>::max();
constexpr static std::size_t const PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED = std::numeric_limits<std::size_t>::max() - 1; // Useful for lookup tables

template<typename FieldType>
struct plonk_constraint_system {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ namespace nil {
}

for (std::uint32_t pi_index = 0; pi_index < table._public_table.public_inputs_amount(); pi_index++) {
table._public_table._public_inputs[pi_index].resize(usable_rows_amount, FieldType::value_type::zero());
table._public_table._public_inputs[pi_index].resize(padded_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t c_index = 0; c_index < table._public_table.constants_amount(); c_index++) {
table._public_table._constants[c_index].resize(usable_rows_amount, FieldType::value_type::zero());
table._public_table._constants[c_index].resize(padded_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t s_index = 0; s_index < table._public_table.selectors_amount(); s_index++) {
table._public_table._selectors[s_index].resize(usable_rows_amount, FieldType::value_type::zero());
table._public_table._selectors[s_index].resize(padded_rows_amount, FieldType::value_type::zero());
}


Expand All @@ -120,25 +120,6 @@ namespace nil {
table._private_table._witnesses[w_index][i] = alg_rnd();
}
}

for (std::uint32_t pi_index = 0; pi_index < table._public_table.public_inputs_amount(); pi_index++) {
table._public_table._public_inputs[pi_index].resize(padded_rows_amount, FieldType::value_type::zero());
}

for (std::uint32_t c_index = 0; c_index < table._public_table.constants_amount(); c_index++) {
table._public_table._constants[c_index].resize(padded_rows_amount);
for(std::size_t i = usable_rows_amount; i < padded_rows_amount; i++) {
table._public_table._constants[c_index][i] = alg_rnd();
}
}

for (std::uint32_t s_index = 0; s_index < table._public_table.selectors_amount(); s_index++) {
table._public_table._selectors[s_index].resize(padded_rows_amount);
for(std::size_t i = usable_rows_amount; i < padded_rows_amount; i++) {
table._public_table._selectors[s_index][i] = alg_rnd();
}
}

return padded_rows_amount;
}
} // namespace snark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ namespace nil {
const plonk_polynomial_dfs_table<FieldType> &assignments,
std::shared_ptr<math::evaluation_domain<FieldType>> domain,
std::size_t extended_domain_size,
std::unordered_map<polynomial_dfs_variable_type, polynomial_dfs_type>& variable_values_out) {
std::unordered_map<polynomial_dfs_variable_type, polynomial_dfs_type>& variable_values_out,
const polynomial_dfs_type &mask_polynomial,
const polynomial_dfs_type &lagrange_0
) {

std::unordered_map<polynomial_dfs_variable_type, size_t> variable_counts;

Expand All @@ -94,26 +97,33 @@ namespace nil {
visitor.visit(expr);

for (const auto& [var, count]: variable_counts) {
// We may have variable values in required sizes in some cases.
if (variable_values_out.find(var) != variable_values_out.end())
continue;
polynomial_dfs_type assignment = assignments.get_variable_value(var, domain);
if (count > 1) {
assignment.resize(extended_domain_size, domain, extended_domain);
// We may have variable values in required sizes in some cases.
if( var.index == PLONK_SPECIAL_SELECTOR_ALL_USABLE_ROWS_SELECTED && var.type == polynomial_dfs_variable_type::column_type::selector ) {
variable_values_out[var] = mask_polynomial;
} else if (var.index == PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED && var.type == polynomial_dfs_variable_type::column_type::selector){
variable_values_out[var] = mask_polynomial - lagrange_0;
} else {
polynomial_dfs_type assignment = assignments.get_variable_value(var, domain);
if (count > 1) {
assignment.resize(extended_domain_size, domain, extended_domain);
}
variable_values_out[var] = assignment;
}
variable_values_out[var] = assignment;
}
}

static inline std::array<polynomial_dfs_type, argument_size>
prove_eval(
const typename policy_type::constraint_system_type &constraint_system,
const plonk_polynomial_dfs_table<FieldType>
&column_polynomials,
std::shared_ptr<math::evaluation_domain<FieldType>> original_domain,
std::uint32_t max_gates_degree,
const polynomial_dfs_type &mask_polynomial,
transcript_type& transcript) {
static inline std::array<polynomial_dfs_type, argument_size> prove_eval(
const typename policy_type::constraint_system_type &constraint_system,
const plonk_polynomial_dfs_table<FieldType>
&column_polynomials,
std::shared_ptr<math::evaluation_domain<FieldType>> original_domain,
std::uint32_t max_gates_degree,
const polynomial_dfs_type &mask_polynomial,
const polynomial_dfs_type &lagrange_0,
transcript_type& transcript
) {
PROFILE_SCOPE("gate_argument_time");

// max_gates_degree that comes from the outside does not take into account multiplication
Expand Down Expand Up @@ -168,8 +178,8 @@ namespace nil {
}
}

auto selector = polynomial_dfs_variable_type(
gate.selector_index, 0, false, polynomial_dfs_variable_type::column_type::selector);
polynomial_dfs_variable_type selector = polynomial_dfs_variable_type(
gate.selector_index, 0, false, polynomial_dfs_variable_type::column_type::selector);

for (size_t i = 0; i < extended_domain_sizes.size(); ++i) {
gate_results[i] *= selector;
Expand All @@ -184,8 +194,11 @@ namespace nil {
if (i != 0 && extended_domain_sizes[i] != extended_domain_sizes[i-1]) {
variable_values.clear();
}
build_variable_value_map(expressions[i], column_polynomials, original_domain,
extended_domain_sizes[i], variable_values);
build_variable_value_map(
expressions[i], column_polynomials, original_domain,
extended_domain_sizes[i], variable_values,
mask_polynomial, lagrange_0
);

math::cached_expression_evaluator<polynomial_dfs_variable_type> evaluator(
expressions[i], [&assignments=variable_values, domain_size=extended_domain_sizes[i]]
Expand All @@ -197,7 +210,6 @@ namespace nil {
F[0] += evaluator.evaluate();
}

F[0] *= mask_polynomial;
return F;
}

Expand Down Expand Up @@ -230,7 +242,6 @@ namespace nil {
F[0] += gate_result;
}

F[0] *= mask_value;
return F;
}
};
Expand Down
Loading

0 comments on commit c62c7f1

Please sign in to comment.