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

test(proof producer): add proof generation/verifaction for circuits #302

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
105 changes: 77 additions & 28 deletions proof-producer/tests/bin/proof-producer/test_zkevm_bbf_circuits.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>

#include <filesystem>
#include <string>

#include <nil/blueprint/utils/satisfiability_check.hpp>
Expand All @@ -18,8 +19,7 @@ namespace {
} // namespace


class ProverTests: public ::testing::TestWithParam<Input> {

class ProverTestBase: public ::testing::TestWithParam<Input> {
public:
using CurveType = nil::crypto3::algebra::curves::pallas;
using HashType = nil::crypto3::hashes::keccak_1600<256>;
Expand All @@ -32,8 +32,10 @@ class ProverTests: public ::testing::TestWithParam<Input> {
static constexpr std::size_t max_quotient_chunks = 0;
};

class ProverTestsAssignment : public ProverTestBase {
};

TEST_P(ProverTests, FillAssignmentAndCheck) {
TEST_P(ProverTestsAssignment, FillAssignmentAndCheck) {
const auto input = GetParam();
const std::string trace_base_path = std::string(TEST_DATA_DIR) + input.trace_base_name;
nil::proof_generator::Prover<CurveType, HashType> prover(
Expand Down Expand Up @@ -66,36 +68,36 @@ using namespace nil::proof_generator::circuits;

// Single call of Counter contract increment function
const std::string SimpleIncrement = "increment_simple";
INSTANTIATE_TEST_SUITE_P(SimpleRw, ProverTests, ::testing::Values(Input{SimpleIncrement, RW}));
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}));
INSTANTIATE_TEST_SUITE_P(SimpleRw, ProverTestsAssignment, ::testing::Values(Input{SimpleIncrement, RW}));
INSTANTIATE_TEST_SUITE_P(SimpleBytecode, ProverTestsAssignment, ::testing::Values(Input{SimpleIncrement, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(SimpleCopy, ProverTestsAssignment, ::testing::Values(Input{SimpleIncrement, COPY}));
INSTANTIATE_TEST_SUITE_P(SimpleZkevm, ProverTestsAssignment, ::testing::Values(Input{SimpleIncrement, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(SimpleExp, ProverTestsAssignment, ::testing::Values(Input{SimpleIncrement, EXP}));

// // 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}));
INSTANTIATE_TEST_SUITE_P(MultiTxRw, ProverTestsAssignment, ::testing::Values(Input{MultiTxIncrement, RW}));
INSTANTIATE_TEST_SUITE_P(MultiTxBytecode, ProverTestsAssignment, :: testing::Values(Input{MultiTxIncrement, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(MultiTxCopy, ProverTestsAssignment, ::testing::Values(Input{MultiTxIncrement, COPY}));
INSTANTIATE_TEST_SUITE_P(MultiTxZkevm, ProverTestsAssignment, ::testing::Values(Input{MultiTxIncrement, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(MultiTxExp, ProverTestsAssignment, ::testing::Values(Input{MultiTxIncrement, EXP}));

// // 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}));
INSTANTIATE_TEST_SUITE_P(SimpleExpRw, ProverTestsAssignment, ::testing::Values(Input{SimpleExp, RW}));
INSTANTIATE_TEST_SUITE_P(SimpleExpBytecode, ProverTestsAssignment, :: testing::Values(Input{SimpleExp, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(SimpleExpCopy, ProverTestsAssignment, ::testing::Values(Input{SimpleExp, COPY}));
INSTANTIATE_TEST_SUITE_P(SimpleExpZkevm, ProverTestsAssignment, ::testing::Values(Input{SimpleExp, ZKEVM}));
INSTANTIATE_TEST_SUITE_P(SimpleExpExp, ProverTestsAssignment, ::testing::Values(Input{SimpleExp, EXP}));

// RW trace is picked from another trace set and has different trace_idx
TEST(ProverTest, TraceIndexMismatch) {
const std::string trace_base_path = std::string(TEST_DATA_DIR) + "/broken_index/increment_simple.pb";
nil::proof_generator::Prover<ProverTests::CurveType, ProverTests::HashType> prover(
ProverTests::lambda,
ProverTests::expand_factor,
ProverTests::max_quotient_chunks,
ProverTests::grind,
nil::proof_generator::Prover<ProverTestBase::CurveType, ProverTestBase::HashType> prover(
ProverTestBase::lambda,
ProverTestBase::expand_factor,
ProverTestBase::max_quotient_chunks,
ProverTestBase::grind,
ZKEVM
);

Expand All @@ -106,14 +108,61 @@ TEST(ProverTest, TraceIndexMismatch) {
// Trace files contain different proto hash
TEST(ProverTest, DifferentProtoHash) {
const std::string trace_base_path = std::string(TEST_DATA_DIR) + "/different_proto/increment_simple.pb";
nil::proof_generator::Prover<ProverTests::CurveType, ProverTests::HashType> prover(
ProverTests::lambda,
ProverTests::expand_factor,
ProverTests::max_quotient_chunks,
ProverTests::grind,
nil::proof_generator::Prover<ProverTestBase::CurveType, ProverTestBase::HashType> prover(
ProverTestBase::lambda,
ProverTestBase::expand_factor,
ProverTestBase::max_quotient_chunks,
ProverTestBase::grind,
ZKEVM
);

ASSERT_TRUE(prover.setup_prover());
ASSERT_FALSE(prover.fill_assignment_table(trace_base_path));
}

namespace fs = std::filesystem;

class ProverTestsVerification : public ProverTestBase {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suspect these tests takes much time on CI (on local dev server zkevm verification with minimal size takes ~1h).
Check will be duplicated in regular benchmarks, so most probably no need add it to CI

protected:
fs::path proof_file_path;
fs::path json_file_path;

void SetUp() override {
proof_file_path = fs::temp_directory_path() / "proof.dat";
json_file_path = fs::temp_directory_path() / "proof.json";
}

void TearDown() override {
if (fs::exists(proof_file_path))
fs::remove(proof_file_path);
if (fs::exists(json_file_path))
fs::remove(json_file_path);
}
};

TEST_P(ProverTestsVerification, GenerateProof) {
const auto input = GetParam();
const std::string trace_base_path = std::string(TEST_DATA_DIR) + input.trace_base_name;
nil::proof_generator::Prover<CurveType, HashType> prover(
lambda,
expand_factor,
max_quotient_chunks,
grind,
input.circuit_name
);

ASSERT_TRUE(prover.setup_prover());
ASSERT_TRUE(prover.fill_assignment_table(trace_base_path));
ASSERT_TRUE(prover.preprocess_public_data());
ASSERT_TRUE(prover.preprocess_private_data());
ASSERT_TRUE(prover.generate_to_file(
proof_file_path,
json_file_path,
false /*don't skip verification*/
));
}

INSTANTIATE_TEST_SUITE_P(SimpleRw, ProverTestsVerification, ::testing::Values(Input{SimpleIncrement, RW}));
INSTANTIATE_TEST_SUITE_P(SimpleBytecode, ProverTestsVerification, ::testing::Values(Input{SimpleIncrement, BYTECODE}));
INSTANTIATE_TEST_SUITE_P(SimpleCopy, ProverTestsVerification, ::testing::Values(Input{SimpleIncrement, COPY}));
INSTANTIATE_TEST_SUITE_P(SimpleExp, ProverTestsVerification, ::testing::Values(Input{SimpleIncrement, EXP}));
Loading