Skip to content

Commit

Permalink
feat(proof producer): add exp circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Jan 22, 2025
1 parent 9384dab commit b1f0ccf
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <nil/proof-generator/assigner/rw.hpp>
#include <nil/proof-generator/assigner/copy.hpp>
#include <nil/proof-generator/assigner/zkevm.hpp>
#include <nil/proof-generator/assigner/exp.hpp>
#include <nil/proof-generator/assigner/trace_parser.hpp>


Expand All @@ -26,7 +27,8 @@ namespace nil {
{circuits::BYTECODE, fill_bytecode_assignment_table<BlueprintFieldType>},
{circuits::RW, fill_rw_assignment_table<BlueprintFieldType>},
{circuits::ZKEVM, fill_zkevm_assignment_table<BlueprintFieldType>},
{circuits::COPY, fill_copy_events_assignment_table<BlueprintFieldType>}
{circuits::COPY, fill_copy_events_assignment_table<BlueprintFieldType>},
{circuits::EXP, fill_exp_assignment_table<BlueprintFieldType>},
};

template<typename BlueprintFieldType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef PROOF_GENERATOR_LIBS_ASSIGNER_EXP_HPP_
#define PROOF_GENERATOR_LIBS_ASSIGNER_EXP_HPP_

#include <optional>
#include <boost/log/trivial.hpp>
#include <boost/filesystem.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp>
#include <nil/blueprint/zkevm_bbf/exp.hpp>
#include <nil/proof-generator/assigner/options.hpp>
#include <nil/proof-generator/assigner/trace_parser.hpp>
#include <nil/proof-generator/preset/limits.hpp>


namespace nil {
namespace proof_generator {

/// @brief Fill assignment table
template<typename BlueprintFieldType>
std::optional<std::string> fill_exp_assignment_table(nil::crypto3::zk::snark::plonk_assignment_table<BlueprintFieldType>& assignment_table,
const boost::filesystem::path& trace_base_path,
const AssignerOptions& options) {
BOOST_LOG_TRIVIAL(debug) << "fill exp table from " << trace_base_path << "\n";

using ComponentType = nil::blueprint::bbf::exponentiation<BlueprintFieldType, nil::blueprint::bbf::GenerationStage::ASSIGNMENT>;

typename nil::blueprint::bbf::context<BlueprintFieldType, nil::blueprint::bbf::GenerationStage::ASSIGNMENT> context_object(assignment_table, limits::max_rows);

typename ComponentType::input_type input;


const auto exp_trace_path = get_exp_trace_path(trace_base_path);
const auto exp_operations = deserialize_exp_traces_from_file(exp_trace_path, options);
if (!exp_operations) {
return "can't read exp operations from file: " + exp_trace_path.string();
}
input = std::move(exp_operations->value);

ComponentType instance(
context_object,
input,
limits::max_rows,
limits::max_exp_rows
);

return {};
}
} // proof_generator
} // nil

#endif // PROOF_GENERATOR_LIBS_ASSIGNER_EXP_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef PROOF_GENERATOR_LIBS_PRESET_EXP_HPP_
#define PROOF_GENERATOR_LIBS_PRESET_EXP_HPP_

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <nil/blueprint/bbf/enums.hpp>
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp>
#include <nil/proof-generator/preset/limits.hpp>
#include <nil/blueprint/zkevm_bbf/exp.hpp>
#include <nil/blueprint/bbf/l1_wrapper.hpp>
#include <optional>
#include <string>


namespace nil {
namespace proof_generator {
template<typename BlueprintFieldType>
std::optional<std::string> initialize_exp_circuit(
std::optional<blueprint::circuit<nil::crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>>>& exp_circuit,
std::optional<nil::crypto3::zk::snark::plonk_assignment_table<BlueprintFieldType>>& exp_table) {

using ComponentType = nil::blueprint::bbf::exponentiation<BlueprintFieldType, nil::blueprint::bbf::GenerationStage::CONSTRAINTS>;

// initialize assignment table
const auto desc = ComponentType::get_table_description(limits::max_rows, limits::max_exp_rows);
exp_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns);
BOOST_LOG_TRIVIAL(debug) << "exp table:\n"
<< "witnesses = " << exp_table->witnesses_amount()
<< " public inputs = " << exp_table->public_inputs_amount()
<< " constants = " << exp_table->constants_amount()
<< " selectors = " << exp_table->selectors_amount() << "\n";

std::size_t start_row = 0;
std::vector<std::size_t> witnesses(desc.witness_columns);
std::iota(witnesses.begin(), witnesses.end(), 0); // fill 0, 1, ...
std::vector<std::size_t> public_inputs(desc.public_input_columns);
std::iota(public_inputs.begin(), public_inputs.end(), 0); // fill 0, 1, ...
std::vector<std::size_t> constants(desc.constant_columns);
std::iota(constants.begin(), constants.end(), 0); // fill 0, 1, ...

using L1WrapperType = nil::blueprint::components::plonk_l1_wrapper<BlueprintFieldType,
nil::blueprint::bbf::exponentiation, std::size_t, std::size_t>;
L1WrapperType wrapper(witnesses, public_inputs, constants);

typename ComponentType::input_type input;

nil::blueprint::circuit<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType>> circuit;

nil::blueprint::components::generate_circuit<BlueprintFieldType, nil::blueprint::bbf::exponentiation, std::size_t, std::size_t>(
wrapper, circuit, *exp_table, input, start_row,
limits::max_rows, limits::max_exp_rows);

zk::snark::pack_lookup_tables_horizontal(
circuit.get_reserved_indices(),
circuit.get_reserved_tables(),
circuit.get_reserved_dynamic_tables(),
circuit, *exp_table,
exp_table->rows_amount(),
100000
);

exp_circuit.emplace(circuit);

return {};
}
} // proof_generator
} // nil
#endif // PROOF_GENERATOR_LIBS_PRESET_EXP_HPP_
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once
#pragma once

#include <cstdint>
#include "nil/proof-generator/preset/preset.hpp"

namespace nil {
namespace proof_generator {
namespace limits {

const std::size_t max_copy = 30000;
const std::size_t max_rw_size = 60000;
const std::size_t max_keccak_blocks = 500;
const std::size_t max_bytecode_size = 20000;
const std::size_t max_rows = 500000;
const std::size_t max_rows = 500000;
const std::size_t max_mpt_size = 30;
const std::size_t max_zkevm_rows = 25000;
const std::size_t max_exp_rows = 25000;

const std::size_t RLC_CHALLENGE = 7; // should be the same between all components

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nil/proof-generator/preset/rw.hpp"
#include "nil/proof-generator/preset/zkevm.hpp"
#include "nil/proof-generator/preset/copy.hpp"
#include "nil/proof-generator/preset/exp.hpp"

#include <optional>
#include <string>
Expand All @@ -24,6 +25,7 @@ namespace nil {
const Name RW = "rw";
const Name ZKEVM = "zkevm";
const Name COPY = "copy";
const Name EXP = "exp";

} // namespace circuits

Expand Down Expand Up @@ -60,7 +62,8 @@ namespace nil {
{circuits::BYTECODE, initialize_bytecode_circuit<BlueprintFieldType>},
{circuits::RW, initialize_rw_circuit<BlueprintFieldType>},
{circuits::ZKEVM, initialize_zkevm_circuit<BlueprintFieldType>},
{circuits::COPY, initialize_copy_circuit<BlueprintFieldType>}
{circuits::COPY, initialize_copy_circuit<BlueprintFieldType>},
{circuits::EXP, initialize_exp_circuit<BlueprintFieldType>},
};
} // proof_generator
} // nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ INSTANTIATE_TEST_SUITE_P(SimpleRw, ProverTests, ::testing::Values(Input{SimpleIn
INSTANTIATE_TEST_SUITE_P(SimpleBytecode, ProverTests, ::testing::Values(Input{SimpleIncrement, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(SimpleCopy, ProverTests, ::testing::Values(Input{SimpleIncrement, COPY}));
INSTANTIATE_TEST_SUITE_P(SimpleZkevm, ProverTests, ::testing::Values(Input{SimpleIncrement, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(SimpleExp, ProverTests, ::testing::Values(Input{SimpleIncrement, EXP}));

// Multiple calls of Counter contract increment function (several transactions)
// // Multiple calls of Counter contract increment function (several transactions)
const std::string MultiTxIncrement = "increment_multi_tx";
INSTANTIATE_TEST_SUITE_P(MultiTxRw, ProverTests, ::testing::Values(Input{MultiTxIncrement, RW}));
INSTANTIATE_TEST_SUITE_P(MultiTxBytecode, ProverTests, :: testing::Values(Input{MultiTxIncrement, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(MultiTxCopy, ProverTests, ::testing::Values(Input{MultiTxIncrement, COPY}));
INSTANTIATE_TEST_SUITE_P(MultiTxZkevm, ProverTests, ::testing::Values(Input{MultiTxIncrement, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(MultiTxExp, ProverTests, ::testing::Values(Input{MultiTxIncrement, EXP}));

// Single call of exp operation
// // Single call of exp operation
const std::string SimpleExp = "exp/exp";
INSTANTIATE_TEST_SUITE_P(SimpleExpRw, ProverTests, ::testing::Values(Input{SimpleExp, RW}));
INSTANTIATE_TEST_SUITE_P(SimpleExpBytecode, ProverTests, :: testing::Values(Input{SimpleExp, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(SimpleExpCopy, ProverTests, ::testing::Values(Input{SimpleExp, COPY}));
INSTANTIATE_TEST_SUITE_P(SimpleExpZkevm, ProverTests, ::testing::Values(Input{SimpleExp, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(SimpleExpExp, ProverTests, ::testing::Values(Input{SimpleExp, EXP}));

// RW trace is picked from another trace set and has different trace_idx
TEST(ProverTest, TraceIndexMismatch) {
Expand Down

0 comments on commit b1f0ccf

Please sign in to comment.