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

feat: Integrate verify_proof calls in mock protocol circuits #9253

Merged
merged 34 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
97206cd
feat: Integrate verify_proof calls in mock protocol circuits
sirasistant Oct 16, 2024
be16bfa
type
sirasistant Oct 16, 2024
ccf7f6b
feat: add skip auto verify
sirasistant Oct 16, 2024
ffd40f7
add a new ivc flow that properly handles kernels
ledwards2225 Oct 16, 2024
a7757e3
let client ivc use internal vkeys
ledwards2225 Oct 16, 2024
2ee9971
handle ivc rec constraints in acir to buf
ledwards2225 Oct 16, 2024
193bd17
working without using the vks from the kernels
ledwards2225 Oct 18, 2024
9851a1a
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
sirasistant Oct 21, 2024
3ad39c1
remove outdated TODO
ledwards2225 Oct 21, 2024
d6be6f6
come basic write vk infra in place but untested
ledwards2225 Oct 21, 2024
7c36935
move mocking method WiP to new file
ledwards2225 Oct 22, 2024
ba7774b
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
ledwards2225 Oct 23, 2024
5b4db5d
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
ledwards2225 Nov 11, 2024
7ec851b
get things building again
ledwards2225 Nov 11, 2024
98ec48f
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
ledwards2225 Dec 3, 2024
4c8cb69
fix build
ledwards2225 Dec 3, 2024
859d722
bit of cleanup
ledwards2225 Dec 3, 2024
1301bd5
update index ts with vk inputs
ledwards2225 Dec 3, 2024
58d5f0d
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
ledwards2225 Dec 3, 2024
ba13fa3
update write vk for ivc
ledwards2225 Dec 3, 2024
357f4a7
WiP skip auto verify path in ivc api
ledwards2225 Dec 3, 2024
9c61152
delete old code
ledwards2225 Dec 4, 2024
1b70bac
WiP; test passes with old settings
ledwards2225 Dec 4, 2024
3b47361
test passes with no auto verify and internal VKs
ledwards2225 Dec 4, 2024
7121402
clear out old method
ledwards2225 Dec 4, 2024
d789d32
generate vks for ivc integration seems to work
ledwards2225 Dec 4, 2024
c8acf05
tests pass with VKs generated via new flow
ledwards2225 Dec 4, 2024
49a39c4
cleaaaan
ledwards2225 Dec 4, 2024
4e17f70
copy new ivc integration circuits in earthfile
ledwards2225 Dec 4, 2024
3d7bc15
format noir
ledwards2225 Dec 4, 2024
8d7cda2
init srs within accum methods
ledwards2225 Dec 4, 2024
2f30133
Merge branch 'master' into arv/integrate_verify_proof_in_mock_circuits
ledwards2225 Dec 4, 2024
d480f34
remove opt from no auto verify
ledwards2225 Dec 5, 2024
cee7472
comment
ledwards2225 Dec 5, 2024
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
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/bb/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class API {
struct Flags {
std::optional<std::string> output_type; // bytes, fields, bytes_and_fields, fields_msgpack
std::optional<std::string> input_type; // compiletime_stack, runtime_stack
bool no_auto_verify; // TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): remove
};

virtual void prove(const Flags& flags,
Expand Down
58 changes: 55 additions & 3 deletions barretenberg/cpp/src/barretenberg/bb/api_client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ class ClientIVCAPI : public API {
ClientIVC ivc{ { E2E_FULL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };

// Accumulate the entire program stack into the IVC
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1116): remove manual setting of is_kernel once
// databus has been integrated into noir kernel programs
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1116): remove manual setting of is_kernel
bool is_kernel = false;
for (Program& program : folding_stack) {
// Construct a bberg circuit from the acir representation then accumulate it into the IVC
Expand All @@ -163,6 +162,47 @@ class ClientIVCAPI : public API {
return ivc;
};

static ClientIVC _accumulate_without_auto_verify(std::vector<acir_format::AcirProgram>& folding_stack)
{
using Builder = MegaCircuitBuilder;
using Program = acir_format::AcirProgram;

using namespace acir_format;

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1163) set these dynamically
init_bn254_crs(1 << 20);
init_grumpkin_crs(1 << 15);

// TODO(#7371) dedupe this with the rest of the similar code
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): remove use of auto_verify_mode
ClientIVC ivc{ { E2E_FULL_TEST_STRUCTURE }, /*auto_verify_mode=*/false };

// Accumulate the entire program stack into the IVC
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1116): remove manual setting of is_kernel once
// databus has been integrated into noir kernel programs
bool is_kernel = false;
for (Program& program : folding_stack) {

Builder circuit;

is_kernel = !program.constraints.ivc_recursion_constraints.empty();
if (is_kernel) {
vinfo("Accumulating KERNEL.");
circuit = create_kernel_circuit(program.constraints, ivc, program.witness);
} else {
vinfo("Accumulating APP.");
circuit = create_circuit<Builder>(
program.constraints, /*recursive=*/false, 0, program.witness, false, ivc.goblin.op_queue);
}

// Do one step of ivc accumulator or, if there is only one circuit in the stack, prove that circuit. In this
// case, no work is added to the Goblin opqueue, but VM proofs for trivial inputs are produced.
ivc.accumulate(circuit, /*one_circuit=*/folding_stack.size() == 1);
}

return ivc;
};

public:
void prove(const API::Flags& flags,
const std::filesystem::path& bytecode_path,
Expand All @@ -179,7 +219,19 @@ class ClientIVCAPI : public API {

std::vector<acir_format::AcirProgram> folding_stack =
_build_folding_stack(*flags.input_type, bytecode_path, witness_path);
ClientIVC ivc = _accumulate(folding_stack);

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1163) set these dynamically
init_bn254_crs(1 << 20);
init_grumpkin_crs(1 << 15);

ClientIVC ivc;
if (flags.no_auto_verify) {
vinfo("performing accumulation WITHOUT auto-verify");
ivc = _accumulate_without_auto_verify(folding_stack);
} else {
vinfo("performing accumulation with auto-verify");
ivc = _accumulate(folding_stack);
}
ClientIVC::Proof proof = ivc.prove();

// Write the proof and verification keys into the working directory in 'binary' format (in practice it seems
Expand Down
63 changes: 62 additions & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "barretenberg/constants.hpp"
#include "barretenberg/dsl/acir_format/acir_format.hpp"
#include "barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp"
#include "barretenberg/dsl/acir_format/ivc_recursion_constraint.hpp"
#include "barretenberg/dsl/acir_format/proof_surgeon.hpp"
#include "barretenberg/dsl/acir_proofs/acir_composer.hpp"
#include "barretenberg/dsl/acir_proofs/honk_contract.hpp"
Expand Down Expand Up @@ -833,6 +834,62 @@ void write_vk_honk(const std::string& bytecodePath, const std::string& outputPat
}
}

/**
* @brief Compute and write to file a MegaHonk VK for a circuit to be accumulated in the IVC
* @note This method differes from write_vk_honk<MegaFlavor> in that it handles kernel circuits which require special
* treatment (i.e. construction of mock IVC state to correctly complete the kernel logic).
*
* @param bytecodePath
* @param witnessPath
*/
void write_vk_for_ivc(const std::string& bytecodePath, const std::string& outputPath)
{
using Builder = ClientIVC::ClientCircuit;
using Prover = ClientIVC::MegaProver;
using DeciderProvingKey = ClientIVC::DeciderProvingKey;
using VerificationKey = ClientIVC::MegaVerificationKey;

// TODO(https://github.com/AztecProtocol/barretenberg/issues/1163) set these dynamically
init_bn254_crs(1 << 20);
init_grumpkin_crs(1 << 15);

auto constraints = get_constraint_system(bytecodePath, /*honk_recursion=*/false);
acir_format::WitnessVector witness = {};

TraceSettings trace_settings{ E2E_FULL_TEST_STRUCTURE };

// The presence of ivc recursion constraints determines whether or not the program is a kernel
bool is_kernel = !constraints.ivc_recursion_constraints.empty();

Builder builder;
if (is_kernel) {
// Create a mock IVC instance based on the IVC recursion constraints in the kernel program
ClientIVC mock_ivc = create_mock_ivc_from_constraints(constraints.ivc_recursion_constraints, trace_settings);
builder = acir_format::create_kernel_circuit(constraints, mock_ivc, witness);
} else {
builder = acir_format::create_circuit<Builder>(
constraints, /*recursive=*/false, 0, witness, /*honk_recursion=*/false);
}
// Add public inputs corresponding to pairing point accumulator
builder.add_pairing_point_accumulator(stdlib::recursion::init_default_agg_obj_indices<Builder>(builder));

// Construct the verification key via the prover-constructed proving key with the proper trace settings
auto proving_key = std::make_shared<DeciderProvingKey>(builder, trace_settings);
Prover prover{ proving_key };
init_bn254_crs(prover.proving_key->proving_key.circuit_size);
VerificationKey vk(prover.proving_key->proving_key);

// Write the VK to file as a buffer
auto serialized_vk = to_buffer(vk);
if (outputPath == "-") {
writeRawBytesToStdout(serialized_vk);
vinfo("vk written to stdout");
} else {
write_file(outputPath, serialized_vk);
vinfo("vk written to: ", outputPath);
}
}

/**
* @brief Write a toml file containing recursive verifier inputs for a given program + witness
*
Expand Down Expand Up @@ -1073,7 +1130,8 @@ int main(int argc, char* argv[])

const API::Flags flags = [&args]() {
return API::Flags{ .output_type = get_option(args, "--output_type", "fields_msgpack"),
.input_type = get_option(args, "--input_type", "compiletime_stack") };
.input_type = get_option(args, "--input_type", "compiletime_stack"),
.no_auto_verify = flag_present(args, "--no_auto_verify") };
}();

const std::string command = args[0];
Expand Down Expand Up @@ -1227,6 +1285,9 @@ int main(int argc, char* argv[])
} else if (command == "write_vk_mega_honk") {
std::string output_path = get_option(args, "-o", "./target/vk");
write_vk_honk<MegaFlavor>(bytecode_path, output_path, recursive);
} else if (command == "write_vk_for_ivc") {
std::string output_path = get_option(args, "-o", "./target/vk");
write_vk_for_ivc(bytecode_path, output_path);
} else if (command == "proof_as_fields_honk") {
std::string output_path = get_option(args, "-o", proof_path + "_fields.json");
proof_as_fields_honk(proof_path, output_path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ void handle_blackbox_func_call(Program::Opcode::BlackBoxFuncCall const& arg,
af.honk_recursion_constraints.push_back(c);
af.original_opcode_indices.honk_recursion_constraints.push_back(opcode_index);
break;
case OINK:
case PG:
af.ivc_recursion_constraints.push_back(c);
af.original_opcode_indices.ivc_recursion_constraints.push_back(opcode_index);
break;
case AVM:
af.avm_recursion_constraints.push_back(c);
af.original_opcode_indices.avm_recursion_constraints.push_back(opcode_index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void create_block_constraints(MegaCircuitBuilder& builder,
process_call_data_operations(builder, constraint, has_valid_witness_assignments, init);
// The presence of calldata is used to indicate that the present circuit is a kernel. This is needed in the
// databus consistency checks to indicate that the corresponding return data belongs to a kernel (else an app).
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1165): is_kernel must be known prior to this stage
// since we must determine whether to use create_circuit or create_kernel_circuit. Resolve.
builder.databus_propagation_data.is_kernel = true;
} break;
case BlockType::ReturnData: {
Expand Down
1 change: 1 addition & 0 deletions noir-projects/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source:
RUN yarn

COPY mega_honk_circuits.json .
COPY ivc_integration_circuits.json .
COPY --dir aztec-nr noir-contracts noir-protocol-circuits mock-protocol-circuits scripts .

build-contracts:
Expand Down
8 changes: 8 additions & 0 deletions noir-projects/ivc_integration_circuits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"mock_private_kernel_init",
"mock_private_kernel_inner",
"mock_private_kernel_reset.*",
"mock_private_kernel_tail.*",
"app_creator",
"app_reader"
]
8 changes: 4 additions & 4 deletions noir-projects/mega_honk_circuits.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
"private_kernel_init",
"private_kernel_inner",
"private_kernel_reset.*",
"private_kernel_tail.*"
"^private_kernel_init",
"^private_kernel_inner",
"^private_kernel_reset.*",
"^private_kernel_tail.*"
]
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use dep::mock_types::{
AppPublicInputs, PrivateKernelPublicInputs, PrivateKernelPublicInputsBuilder, TxRequest,
AppPublicInputs, CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS, PrivateKernelPublicInputs,
PrivateKernelPublicInputsBuilder, PROOF_TYPE_OINK, TxRequest,
};

fn main(
tx: TxRequest,
app_inputs: call_data(1) AppPublicInputs,
app_vk: [Field; CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS],
) -> return_data PrivateKernelPublicInputs {
std::verify_proof_with_type(app_vk, [], [], 0, PROOF_TYPE_OINK);

let mut private_kernel_inputs = PrivateKernelPublicInputsBuilder::from_tx(tx);
private_kernel_inputs.ingest_app_inputs(app_inputs);
private_kernel_inputs.finish()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use dep::mock_types::{AppPublicInputs, PrivateKernelPublicInputs, PrivateKernelPublicInputsBuilder};
use dep::mock_types::{
AppPublicInputs, CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS, PrivateKernelPublicInputs,
PrivateKernelPublicInputsBuilder, PROOF_TYPE_PG,
};

fn main(
prev_kernel_public_inputs: call_data(0) PrivateKernelPublicInputs,
kernel_vk: [Field; CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS],
app_inputs: call_data(1) AppPublicInputs,
app_vk: [Field; CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS],
) -> return_data PrivateKernelPublicInputs {
std::verify_proof_with_type(kernel_vk, [], [], 0, PROOF_TYPE_PG);
std::verify_proof_with_type(app_vk, [], [], 0, PROOF_TYPE_PG);

let mut private_kernel_inputs =
PrivateKernelPublicInputsBuilder::from_previous_kernel(prev_kernel_public_inputs);
private_kernel_inputs.ingest_app_inputs(app_inputs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use dep::mock_types::{
MAX_COMMITMENT_READ_REQUESTS_PER_TX, MAX_COMMITMENTS_PER_TX, PrivateKernelPublicInputs,
CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS, MAX_COMMITMENT_READ_REQUESTS_PER_TX,
MAX_COMMITMENTS_PER_TX, PrivateKernelPublicInputs, PROOF_TYPE_PG,
};

// Mock reset kernel that reset read requests.
// It needs hints to locate the commitment that matches the read requests.
fn main(
mut prev_kernel_public_inputs: call_data(0) PrivateKernelPublicInputs,
kernel_vk: [Field; CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS],
commitment_read_hints: [u32; MAX_COMMITMENT_READ_REQUESTS_PER_TX],
) -> return_data PrivateKernelPublicInputs {
std::verify_proof_with_type(kernel_vk, [], [], 0, PROOF_TYPE_PG);

for i in 0..MAX_COMMITMENT_READ_REQUESTS_PER_TX {
if commitment_read_hints[i] != MAX_COMMITMENTS_PER_TX {
assert_eq(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use dep::mock_types::{
KernelPublicInputs, MAX_COMMITMENT_READ_REQUESTS_PER_TX, PrivateKernelPublicInputs,
CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS, KernelPublicInputs,
MAX_COMMITMENT_READ_REQUESTS_PER_TX, PrivateKernelPublicInputs, PROOF_TYPE_PG,
};

// The tail kernel finishes the client IVC chain exposing the final public inputs with no remaining calls or unfulfilled read requests.
fn main(
prev_kernel_public_inputs: call_data(0) PrivateKernelPublicInputs,
kernel_vk: [Field; CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS],
) -> pub KernelPublicInputs {
std::verify_proof_with_type(kernel_vk, [], [], 0, PROOF_TYPE_PG);

assert_eq(prev_kernel_public_inputs.remaining_calls, 0);
for i in 0..MAX_COMMITMENT_READ_REQUESTS_PER_TX {
assert_eq(prev_kernel_public_inputs.read_requests[i], 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type = "lib"
authors = [""]
compiler_version = ">=0.32.0"

[dependencies]
[dependencies]
protocol_types = { path = "../../../noir-protocol-circuits/crates/types" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ global MAX_COMMITMENTS_PER_TX: u32 = 4;
global MAX_COMMITMENT_READ_REQUESTS_PER_CALL: u32 = 2;
global MAX_COMMITMENT_READ_REQUESTS_PER_TX: u32 = 4;

pub use protocol_types::constants::{
CLIENT_IVC_VERIFICATION_KEY_LENGTH_IN_FIELDS, PROOF_TYPE_OINK, PROOF_TYPE_PG,
};

struct TxRequest {
number_of_calls: u32,
}
Expand Down
Loading
Loading