Skip to content

Commit 2dc5653

Browse files
authored
feat: merge-train/avm (#19422)
BEGIN_COMMIT_OVERRIDE feat(avm)!: dont expose/require VK (#19414) chore(avm): params should be references chore: defensive asserts for map/vector retrievals and optional values in cpp (#19439) END_COMMIT_OVERRIDE
2 parents b1be2ea + f21fd9c commit 2dc5653

File tree

36 files changed

+189
-315
lines changed

36 files changed

+189
-315
lines changed

barretenberg/cpp/src/barretenberg/api/api_avm.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace bb {
77
extern const bool avm_enabled;
88

99
/**
10-
* @brief Writes an avm proof and corresponding (incomplete) verification key to files.
10+
* @brief Writes an avm proof to a file.
1111
*
1212
* Communication:
13-
* - Filesystem: The proof and vk are written to the paths output_path/proof and output_path/vk
13+
* - Filesystem: The proof is written to the path output_path/proof
1414
*
1515
* @param inputs_path Path to the file containing the serialised avm public inputs and hints
16-
* @param output_path Path (directory) to write the output proof and verification keys
16+
* @param output_path Path (directory) to write the output proof
1717
*/
1818
void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::path& output_path);
1919

@@ -27,14 +27,12 @@ void avm_check_circuit(const std::filesystem::path& inputs_path);
2727
* an exit code of 0 will be returned for success and 1 for failure.
2828
*
2929
* @param proof_path Path to the file containing the serialized proof
30-
* @param vk_path Path to the file containing the serialized verification key
30+
* @param public_inputs_path Path to the file containing the serialized public inputs
3131
* @return true If the proof is valid
3232
* @return false If the proof is invalid
3333
*/
3434
// NOTE: The proof should NOT include the public inputs.
35-
bool avm_verify(const std::filesystem::path& proof_path,
36-
const std::filesystem::path& public_inputs_path,
37-
const std::filesystem::path& vk_path);
35+
bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::path& public_inputs_path);
3836

3937
/**
4038
* @brief Simulates an public transaction

barretenberg/cpp/src/barretenberg/bb/cli.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ int parse_and_run_cli_command(int argc, char* argv[])
585585
add_crs_path_option(avm_verify_command);
586586
add_avm_public_inputs_option(avm_verify_command);
587587
add_proof_path_option(avm_verify_command);
588-
add_vk_path_option(avm_verify_command);
589588

590589
/***************************************************************************************************************
591590
* Subcommand: aztec_process_artifact
@@ -894,7 +893,7 @@ int parse_and_run_cli_command(int argc, char* argv[])
894893
} else if (avm_check_circuit_command->parsed()) {
895894
avm_check_circuit(avm_inputs_path);
896895
} else if (avm_verify_command->parsed()) {
897-
return avm_verify(proof_path, avm_public_inputs_path, vk_path) ? 0 : 1;
896+
return avm_verify(proof_path, avm_public_inputs_path) ? 0 : 1;
898897
} else if (avm_simulate_command->parsed()) {
899898
avm_simulate(avm_inputs_path);
900899
} else if (avm_write_vk_command->parsed()) {

barretenberg/cpp/src/barretenberg/vm2/api_avm.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::
3131
{
3232
avm2::AvmAPI avm;
3333
auto inputs = avm2::AvmAPI::ProvingInputs::from(read_file(inputs_path));
34-
auto [proof, vk] = avm.prove(inputs);
34+
auto proof = avm.prove(inputs);
3535

36-
// NOTE: As opposed to Avm1 and other proof systems, the public inputs are NOT part of the proof.
36+
// NOTE: As opposed to other proof systems, the public inputs are NOT part of the proof.
3737
write_file(output_path / "proof", to_buffer(proof));
38-
write_file(output_path / "vk", vk);
3938

4039
print_avm_stats();
4140

4241
// NOTE: Temporarily we also verify after proving.
4342
// The reasoning is that proving will always pass unless it crashes.
4443
// We want to return an exit code != 0 if the proof is invalid so that the prover client saves the inputs.
4544
info("verifying...");
46-
bool res = avm.verify(proof, inputs.public_inputs, vk);
45+
bool res = avm.verify(proof, inputs.public_inputs);
4746
info("verification: ", res ? "success" : "failure");
4847
if (!res) {
4948
throw std::runtime_error("Generated proof is invalid!!!!!");
@@ -62,16 +61,13 @@ void avm_check_circuit(const std::filesystem::path& inputs_path)
6261
}
6362

6463
// NOTE: The proof should NOT include the public inputs.
65-
bool avm_verify(const std::filesystem::path& proof_path,
66-
const std::filesystem::path& public_inputs_path,
67-
const std::filesystem::path& vk_path)
64+
bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::path& public_inputs_path)
6865
{
6966
const auto proof = many_from_buffer<fr>(read_file(proof_path));
70-
std::vector<uint8_t> vk_bytes = read_file(vk_path);
7167
auto public_inputs = avm2::PublicInputs::from(read_file(public_inputs_path));
7268

7369
avm2::AvmAPI avm;
74-
bool res = avm.verify(proof, public_inputs, vk_bytes);
70+
bool res = avm.verify(proof, public_inputs);
7571
info("verification: ", res ? "success" : "failure");
7672

7773
print_avm_stats();

barretenberg/cpp/src/barretenberg/vm2/api_avm.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace bb {
77
extern const bool avm_enabled;
88

99
/**
10-
* @brief Writes an avm proof and corresponding (incomplete) verification key to files.
10+
* @brief Writes an avm proof to a file.
1111
*
1212
* Communication:
13-
* - Filesystem: The proof and vk are written to the paths output_path/proof and output_path/vk
13+
* - Filesystem: The proof is written to the path output_path/proof
1414
*
1515
* @param inputs_path Path to the file containing the serialised avm public inputs and hints
16-
* @param output_path Path (directory) to write the output proof and verification keys
16+
* @param output_path Path (directory) to write the output proof
1717
*/
1818
void avm_prove(const std::filesystem::path& inputs_path, const std::filesystem::path& output_path);
1919

@@ -27,14 +27,12 @@ void avm_check_circuit(const std::filesystem::path& inputs_path);
2727
* an exit code of 0 will be returned for success and 1 for failure.
2828
*
2929
* @param proof_path Path to the file containing the serialized proof
30-
* @param vk_path Path to the file containing the serialized verification key
30+
* @param public_inputs_path Path to the file containing the serialized public inputs
3131
* @return true If the proof is valid
3232
* @return false If the proof is invalid
3333
*/
3434
// NOTE: The proof should NOT include the public inputs.
35-
bool avm_verify(const std::filesystem::path& proof_path,
36-
const std::filesystem::path& public_inputs_path,
37-
const std::filesystem::path& vk_path);
35+
bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::path& public_inputs_path);
3836

3937
/**
4038
* @brief Simulates an public transaction

barretenberg/cpp/src/barretenberg/vm2/avm_api.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace bb::avm2 {
1010

1111
using namespace bb::avm2::simulation;
1212

13-
std::pair<AvmAPI::AvmProof, AvmAPI::AvmVerificationKey> AvmAPI::prove(const AvmAPI::ProvingInputs& inputs)
13+
AvmAPI::AvmProof AvmAPI::prove(const AvmAPI::ProvingInputs& inputs)
1414
{
1515
// Simulate.
1616
vinfo("Simulating...");
@@ -27,10 +27,10 @@ std::pair<AvmAPI::AvmProof, AvmAPI::AvmVerificationKey> AvmAPI::prove(const AvmA
2727
// Prove.
2828
vinfo("Proving...");
2929
AvmProvingHelper proving_helper;
30-
auto [proof, vk] = AVM_TRACK_TIME_V("proving/all", proving_helper.prove(std::move(trace)));
30+
auto proof = AVM_TRACK_TIME_V("proving/all", proving_helper.prove(std::move(trace)));
3131

3232
vinfo("Done!");
33-
return { std::move(proof), std::move(vk) };
33+
return proof;
3434
}
3535

3636
bool AvmAPI::check_circuit(const AvmAPI::ProvingInputs& inputs)
@@ -63,11 +63,11 @@ bool AvmAPI::check_circuit(const AvmAPI::ProvingInputs& inputs)
6363
return proving_helper.check_circuit(std::move(trace));
6464
}
6565

66-
bool AvmAPI::verify(const AvmProof& proof, const PublicInputs& pi, const AvmVerificationKey& vk_data)
66+
bool AvmAPI::verify(const AvmProof& proof, const PublicInputs& pi)
6767
{
6868
vinfo("Verifying...");
6969
AvmProvingHelper proving_helper;
70-
return AVM_TRACK_TIME_V("verifing/all", proving_helper.verify(proof, pi, vk_data));
70+
return AVM_TRACK_TIME_V("verifing/all", proving_helper.verify(proof, pi));
7171
}
7272

7373
AvmAPI::AvmVerificationKey AvmAPI::get_verification_key()

barretenberg/cpp/src/barretenberg/vm2/avm_api.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class AvmAPI : public AvmSimAPI {
1515
AvmAPI() = default;
1616

1717
// NOTE: The public inputs are NOT part of the proof.
18-
std::pair<AvmProof, AvmVerificationKey> prove(const ProvingInputs& inputs);
18+
AvmProof prove(const ProvingInputs& inputs);
1919
bool check_circuit(const ProvingInputs& inputs);
20-
bool verify(const AvmProof& proof, const PublicInputs& pi, const AvmVerificationKey& vk_data);
20+
bool verify(const AvmProof& proof, const PublicInputs& pi);
2121
AvmVerificationKey get_verification_key();
2222
};
2323

barretenberg/cpp/src/barretenberg/vm2/constraining/recursion/recursive_verifier.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class AvmRecursiveTests : public ::testing::Test {
4444
const auto public_inputs_cols = public_inputs.to_columns();
4545

4646
InnerProver prover;
47-
const auto [proof, vk_data] = prover.prove(std::move(trace));
48-
const auto verification_key = InnerProver::create_verification_key(vk_data);
47+
const auto proof = prover.prove(std::move(trace));
48+
const auto verification_key = InnerProver::create_verification_key(InnerProver().get_verification_key());
4949
InnerVerifier verifier(verification_key);
5050

5151
const bool verified = verifier.verify_proof(proof, public_inputs_cols);

barretenberg/cpp/src/barretenberg/vm2/constraining/verifier.test.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ class AvmVerifierTests : public ::testing::Test {
2424
};
2525

2626
// Helper function to create proof.
27-
static NativeProofResult create_proof_and_vk()
27+
static NativeProofResult create_proof()
2828
{
2929
auto [trace, public_inputs] = testing::get_minimal_trace_with_pi();
3030

3131
Prover prover;
3232
auto public_inputs_cols = public_inputs.to_columns();
33-
const auto [proof, vk_data] = prover.prove(std::move(trace));
34-
const auto verification_key = prover.create_verification_key(vk_data);
33+
const auto proof = prover.prove(std::move(trace));
34+
auto vk = AvmProvingHelper::create_verification_key(AvmProvingHelper().get_verification_key());
3535

36-
return { proof, verification_key, public_inputs_cols };
36+
return { proof, vk, public_inputs_cols };
3737
}
3838
};
3939

@@ -43,11 +43,9 @@ TEST_F(AvmVerifierTests, GoodPublicInputs)
4343
GTEST_SKIP() << "Skipping slow test";
4444
}
4545

46-
NativeProofResult proof_result = create_proof_and_vk();
47-
auto [proof, verification_key, public_inputs_cols] = proof_result;
48-
49-
Verifier verifier(verification_key);
46+
auto [proof, vk, public_inputs_cols] = create_proof();
5047

48+
Verifier verifier(vk);
5149
const bool verified = verifier.verify_proof(proof, public_inputs_cols);
5250

5351
ASSERT_TRUE(verified) << "native proof verification failed";
@@ -59,11 +57,10 @@ TEST_F(AvmVerifierTests, NegativeBadPublicInputs)
5957
GTEST_SKIP() << "Skipping slow test";
6058
}
6159

62-
NativeProofResult proof_result = create_proof_and_vk();
63-
auto [proof, verification_key, public_inputs_cols] = proof_result;
60+
auto [proof, vk, public_inputs_cols] = create_proof();
6461
auto verify_with_corrupt_pi_col = [&](size_t col_idx) {
6562
public_inputs_cols[col_idx][5] += FF::one();
66-
Verifier verifier(verification_key);
63+
Verifier verifier(vk);
6764
const bool verified = verifier.verify_proof(proof, public_inputs_cols);
6865
ASSERT_FALSE(verified)
6966
<< "native proof verification succeeded, but should have failed due to corruption of public inputs col "
@@ -73,17 +70,17 @@ TEST_F(AvmVerifierTests, NegativeBadPublicInputs)
7370
for (size_t col_idx = 0; col_idx < 4; col_idx++) {
7471
verify_with_corrupt_pi_col(col_idx);
7572
}
76-
Verifier verifier(verification_key);
73+
Verifier verifier(vk);
7774
const bool verified = verifier.verify_proof(proof, public_inputs_cols);
7875
ASSERT_TRUE(verified) << "native proof verification failed, but should have succeeded";
7976
}
8077

8178
// Verify that the actual proof size matches COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS
8279
TEST_F(AvmVerifierTests, ProofSizeMatchesComputedConstant)
8380
{
84-
NativeProofResult proof_result = create_proof_and_vk();
81+
auto [proof, vk, public_inputs_cols] = create_proof();
8582

86-
const size_t actual_proof_size = proof_result.proof.size();
83+
const size_t actual_proof_size = proof.size();
8784
const size_t computed_proof_size = AvmFlavor::COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS;
8885

8986
EXPECT_EQ(actual_proof_size, computed_proof_size)

barretenberg/cpp/src/barretenberg/vm2/dsl/avm2_recursion_constraint.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ class AvmRecursionConstraintTestingFunctions {
5252
auto [trace, public_inputs] = avm2::testing::get_minimal_trace_with_pi();
5353

5454
AvmProver prover;
55-
auto [proof, vk_data] = prover.prove(std::move(trace));
55+
auto proof = prover.prove(std::move(trace));
5656
proof.resize(AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED, FF::zero()); // Pad proof
5757

58-
const bool verified = prover.verify(proof, public_inputs, vk_data);
58+
const bool verified = prover.verify(proof, public_inputs);
5959
EXPECT_TRUE(verified) << "native proof verification failed";
6060

6161
auto public_inputs_flat = PublicInputs::columns_to_flat(public_inputs.to_columns());

barretenberg/cpp/src/barretenberg/vm2/proving_helper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ AvmProvingHelper::VkData AvmProvingHelper::get_verification_key()
5151
return serialized_vk;
5252
}
5353

54-
std::pair<AvmProvingHelper::Proof, AvmProvingHelper::VkData> AvmProvingHelper::prove(tracegen::TraceContainer&& trace)
54+
AvmProvingHelper::Proof AvmProvingHelper::prove(tracegen::TraceContainer&& trace)
5555
{
5656
auto polynomials = AVM_TRACK_TIME_V("proving/prove:compute_polynomials", constraining::compute_polynomials(trace));
5757
auto proving_key =
@@ -66,7 +66,7 @@ std::pair<AvmProvingHelper::Proof, AvmProvingHelper::VkData> AvmProvingHelper::p
6666
auto proof = AVM_TRACK_TIME_V("proving/construct_proof", prover.construct_proof());
6767
auto serialized_vk = to_buffer(verification_key->to_field_elements());
6868

69-
return { std::move(proof), std::move(serialized_vk) };
69+
return proof;
7070
}
7171

7272
bool AvmProvingHelper::check_circuit(tracegen::TraceContainer&& trace)
@@ -97,8 +97,9 @@ bool AvmProvingHelper::check_circuit(tracegen::TraceContainer&& trace)
9797
return true;
9898
}
9999

100-
bool AvmProvingHelper::verify(const AvmProvingHelper::Proof& proof, const PublicInputs& pi, const VkData& vk_data)
100+
bool AvmProvingHelper::verify(const AvmProvingHelper::Proof& proof, const PublicInputs& pi)
101101
{
102+
auto vk_data = AVM_TRACK_TIME_V("proving/verify:get_verification_key", get_verification_key());
102103
auto vk = AVM_TRACK_TIME_V("proving/verify:create_verification_key", create_verification_key(vk_data));
103104
auto verifier = AVM_TRACK_TIME_V("proving/verify:construct_verifier", AvmVerifier(std::move(vk)));
104105
return AVM_TRACK_TIME_V("proving/verify_proof", verifier.verify_proof(proof, pi.to_columns()));

0 commit comments

Comments
 (0)