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(avm): new public inputs witgen #10179

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 22 additions & 3 deletions barretenberg/cpp/pil/avm/main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include "binary.pil";
include "constants_gen.pil";
include "constants_misc.pil";
include "gas.pil";
include "kernel.pil";
//include "kernel.pil";
include "bytecode.pil";
include "fixed/powers.pil";
include "gadgets/conversion.pil";
Expand All @@ -16,6 +16,17 @@ include "gadgets/mem_slice.pil";
include "gadgets/merkle_tree.pil";

namespace main(256);
//===== PUBLIC INPUT POLYNOMIALS ======================================
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are moved over to just keep the existing public inputs structure and is not representative of the final version

Copy link
Collaborator

Choose a reason for hiding this comment

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

Were these copied from another PIL file? Should they be removed from their original file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah - the old pil file (kernel.pil) is commented out - it will eventually be deleted entirely, but ive kept it because we will need to reference / copy stuff from it when we constrain the new inputs

pol public kernel_inputs;
pol public kernel_value_out;
pol public kernel_side_effect_out;
pol public kernel_metadata_out;

pol constant sel_l2_start_gas_kernel_input;
pol constant sel_da_start_gas_kernel_input;
pol constant sel_l2_end_gas_kernel_input;
pol constant sel_da_end_gas_kernel_input;

//===== CONSTANT POLYNOMIALS ==================================================
pol constant clk(i) { i };
pol constant sel_first = [1] + [0]*; // Used mostly to toggle off the first row consisting
Expand Down Expand Up @@ -433,11 +444,19 @@ namespace main(256);
pol SEL_ALL_GADGET = sel_op_radix_be + sel_op_sha256 + sel_op_poseidon2 + sel_op_keccak
+ sel_op_ecadd + sel_op_msm;
pol SEL_ALL_MEMORY = sel_op_mov + sel_op_set;
pol KERNEL_INPUT_SELECTORS = sel_op_address + sel_op_sender
+ sel_op_function_selector + sel_op_transaction_fee + sel_op_chain_id
+ sel_op_version + sel_op_block_number + sel_op_timestamp
+ sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas + sel_op_is_static_call;
pol KERNEL_OUTPUT_SELECTORS = sel_op_note_hash_exists + sel_op_emit_note_hash + sel_op_nullifier_exists
+ sel_op_emit_nullifier + sel_op_l1_to_l2_msg_exists + sel_op_emit_unencrypted_log
+ sel_op_emit_l2_to_l1_msg + sel_op_sload + sel_op_sstore;
// Ensure that only one kernel lookup is active when the kernel_in_offset is active
pol OPCODE_SELECTORS = sel_op_fdiv + sel_op_calldata_copy + sel_op_get_contract_instance
+ sel_op_returndata_size + sel_op_returndata_copy + sel_op_debug_log
+ SEL_ALL_ALU + SEL_ALL_BINARY + SEL_ALL_MEMORY + SEL_ALL_GADGET
+ KERNEL_INPUT_SELECTORS + KERNEL_OUTPUT_SELECTORS + SEL_ALL_LEFTGAS
+ SEL_ALL_CTRL_FLOW;
+ SEL_ALL_LEFTGAS + SEL_ALL_CTRL_FLOW
+ KERNEL_INPUT_SELECTORS + KERNEL_OUTPUT_SELECTORS;

pol CUR_AND_NEXT_ARE_MAIN = sel_execution_row * sel_execution_row';

Expand Down
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "barretenberg/stdlib/client_ivc_verifier/client_ivc_recursive_verifier.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_keccak_flavor.hpp"
#include "barretenberg/vm/avm/trace/public_inputs.hpp"

#include <cstddef>
#ifndef DISABLE_AZTEC_VM
Expand Down Expand Up @@ -954,13 +955,12 @@ void avm_prove(const std::filesystem::path& calldata_path,
const std::filesystem::path& output_path)
{
std::vector<fr> const calldata = many_from_buffer<fr>(read_file(calldata_path));
std::vector<fr> const public_inputs_vec = many_from_buffer<fr>(read_file(public_inputs_path));
auto const avm_new_public_inputs = AvmPublicInputs::from(read_file(public_inputs_path));
auto const avm_hints = bb::avm_trace::ExecutionHints::from(read_file(hints_path));

// Using [0] is fine now for the top-level call, but we might need to index by address in future
vinfo("bytecode size: ", avm_hints.all_contract_bytecode[0].bytecode.size());
vinfo("calldata size: ", calldata.size());
vinfo("public_inputs size: ", public_inputs_vec.size());
vinfo("hints.storage_value_hints size: ", avm_hints.storage_value_hints.size());
vinfo("hints.note_hash_exists_hints size: ", avm_hints.note_hash_exists_hints.size());
vinfo("hints.nullifier_exists_hints size: ", avm_hints.nullifier_exists_hints.size());
Expand All @@ -974,7 +974,7 @@ void avm_prove(const std::filesystem::path& calldata_path,

// Prove execution and return vk
auto const [verification_key, proof] =
AVM_TRACK_TIME_V("prove/all", avm_trace::Execution::prove(calldata, public_inputs_vec, avm_hints));
AVM_TRACK_TIME_V("prove/all", avm_trace::Execution::prove(calldata, avm_new_public_inputs, avm_hints));

std::vector<fr> vk_as_fields = verification_key.to_field_elements();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "barretenberg/vm/avm/generated/verifier.hpp"
#include "barretenberg/vm/avm/tests/helpers.test.hpp"
#include "barretenberg/vm/avm/trace/helper.hpp"
#include "barretenberg/vm/avm/trace/public_inputs.hpp"
#include "barretenberg/vm/avm/trace/trace.hpp"
#include "barretenberg/vm/aztec_constants.hpp"
#include "barretenberg/vm/constants.hpp"
Expand Down Expand Up @@ -43,9 +44,11 @@ class AcirAvmRecursionConstraint : public ::testing::Test {
static void SetUpTestSuite() { bb::srs::init_crs_factory("../srs_db/ignition"); }

// mutate the input kernel_public_inputs_vec to add end gas values
static InnerBuilder create_inner_circuit(std::vector<FF>& kernel_public_inputs_vec)
static InnerBuilder create_inner_circuit([[maybe_unused]] std::vector<FF>& kernel_public_inputs_vec)
{
auto public_inputs = convert_public_inputs(kernel_public_inputs_vec);
AvmPublicInputs public_inputs;
public_inputs.gas_settings.gas_limits.l2_gas = 1000000;
public_inputs.gas_settings.gas_limits.da_gas = 1000000;
AvmTraceBuilder trace_builder(public_inputs);
InnerBuilder builder;

Expand All @@ -58,12 +61,6 @@ class AcirAvmRecursionConstraint : public ::testing::Test {
trace_builder.op_return(0, 0, 100);
auto trace = trace_builder.finalize(); // Passing true enables a longer trace with lookups

avm_trace::inject_end_gas_values(public_inputs, trace);
kernel_public_inputs_vec.at(DA_END_GAS_LEFT_PCPI_OFFSET) =
std::get<KERNEL_INPUTS>(public_inputs).at(DA_END_GAS_KERNEL_INPUTS_COL_OFFSET);
kernel_public_inputs_vec.at(L2_END_GAS_LEFT_PCPI_OFFSET) =
std::get<KERNEL_INPUTS>(public_inputs).at(L2_END_GAS_KERNEL_INPUTS_COL_OFFSET);

builder.set_trace(std::move(trace));
builder.check_circuit();
return builder;
Expand Down Expand Up @@ -132,9 +129,9 @@ class AcirAvmRecursionConstraint : public ::testing::Test {
TEST_F(AcirAvmRecursionConstraint, TestBasicSingleAvmRecursionConstraint)
{
std::vector<FF> public_inputs_vec;
public_inputs_vec.resize(PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH);
public_inputs_vec.at(L2_START_GAS_LEFT_PCPI_OFFSET) = FF(1000000);
public_inputs_vec.at(DA_START_GAS_LEFT_PCPI_OFFSET) = FF(1000000);
// public_inputs_vec.resize(PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH);
// public_inputs_vec.at(L2_START_GAS_LEFT_PCPI_OFFSET) = FF(1000000);
// public_inputs_vec.at(DA_START_GAS_LEFT_PCPI_OFFSET) = FF(1000000);

std::vector<InnerBuilder> layer_1_circuits;
layer_1_circuits.push_back(create_inner_circuit(public_inputs_vec));
Expand All @@ -151,4 +148,4 @@ TEST_F(AcirAvmRecursionConstraint, TestBasicSingleAvmRecursionConstraint)
EXPECT_EQ(verifier.verify_proof(proof), true);
}

#endif // DISABLE_AZTEC_VM
#endif // DISABLE_AZTEC_VM
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,6 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_dyn_da_gas_op_cost.set_if_valid_index(i, rows[i].main_dyn_da_gas_op_cost);
polys.main_dyn_gas_multiplier.set_if_valid_index(i, rows[i].main_dyn_gas_multiplier);
polys.main_dyn_l2_gas_op_cost.set_if_valid_index(i, rows[i].main_dyn_l2_gas_op_cost);
polys.main_emit_l2_to_l1_msg_write_offset.set_if_valid_index(
i, rows[i].main_emit_l2_to_l1_msg_write_offset);
polys.main_emit_note_hash_write_offset.set_if_valid_index(i, rows[i].main_emit_note_hash_write_offset);
polys.main_emit_nullifier_write_offset.set_if_valid_index(i, rows[i].main_emit_nullifier_write_offset);
polys.main_emit_unencrypted_log_write_offset.set_if_valid_index(
i, rows[i].main_emit_unencrypted_log_write_offset);
polys.main_ia.set_if_valid_index(i, rows[i].main_ia);
polys.main_ib.set_if_valid_index(i, rows[i].main_ib);
polys.main_ic.set_if_valid_index(i, rows[i].main_ic);
Expand All @@ -280,10 +274,6 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_inv.set_if_valid_index(i, rows[i].main_inv);
polys.main_is_fake_row.set_if_valid_index(i, rows[i].main_is_fake_row);
polys.main_is_gas_accounted.set_if_valid_index(i, rows[i].main_is_gas_accounted);
polys.main_kernel_in_offset.set_if_valid_index(i, rows[i].main_kernel_in_offset);
polys.main_kernel_out_offset.set_if_valid_index(i, rows[i].main_kernel_out_offset);
polys.main_l1_to_l2_msg_exists_write_offset.set_if_valid_index(
i, rows[i].main_l1_to_l2_msg_exists_write_offset);
polys.main_l2_gas_remaining.set_if_valid_index(i, rows[i].main_l2_gas_remaining);
polys.main_l2_gas_u16_r0.set_if_valid_index(i, rows[i].main_l2_gas_u16_r0);
polys.main_l2_gas_u16_r1.set_if_valid_index(i, rows[i].main_l2_gas_u16_r1);
Expand All @@ -292,12 +282,6 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_mem_addr_b.set_if_valid_index(i, rows[i].main_mem_addr_b);
polys.main_mem_addr_c.set_if_valid_index(i, rows[i].main_mem_addr_c);
polys.main_mem_addr_d.set_if_valid_index(i, rows[i].main_mem_addr_d);
polys.main_note_hash_exist_write_offset.set_if_valid_index(i,
rows[i].main_note_hash_exist_write_offset);
polys.main_nullifier_exists_write_offset.set_if_valid_index(i,
rows[i].main_nullifier_exists_write_offset);
polys.main_nullifier_non_exists_write_offset.set_if_valid_index(
i, rows[i].main_nullifier_non_exists_write_offset);
polys.main_op_err.set_if_valid_index(i, rows[i].main_op_err);
polys.main_opcode_val.set_if_valid_index(i, rows[i].main_opcode_val);
polys.main_pc.set_if_valid_index(i, rows[i].main_pc);
Expand All @@ -311,8 +295,6 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_sel_calldata.set_if_valid_index(i, rows[i].main_sel_calldata);
polys.main_sel_execution_end.set_if_valid_index(i, rows[i].main_sel_execution_end);
polys.main_sel_execution_row.set_if_valid_index(i, rows[i].main_sel_execution_row);
polys.main_sel_kernel_inputs.set_if_valid_index(i, rows[i].main_sel_kernel_inputs);
polys.main_sel_kernel_out.set_if_valid_index(i, rows[i].main_sel_kernel_out);
polys.main_sel_mem_op_a.set_if_valid_index(i, rows[i].main_sel_mem_op_a);
polys.main_sel_mem_op_b.set_if_valid_index(i, rows[i].main_sel_mem_op_b);
polys.main_sel_mem_op_c.set_if_valid_index(i, rows[i].main_sel_mem_op_c);
Expand Down Expand Up @@ -388,10 +370,7 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.main_sel_rng_16.set_if_valid_index(i, rows[i].main_sel_rng_16);
polys.main_sel_rng_8.set_if_valid_index(i, rows[i].main_sel_rng_8);
polys.main_sel_slice_gadget.set_if_valid_index(i, rows[i].main_sel_slice_gadget);
polys.main_side_effect_counter.set_if_valid_index(i, rows[i].main_side_effect_counter);
polys.main_sload_write_offset.set_if_valid_index(i, rows[i].main_sload_write_offset);
polys.main_space_id.set_if_valid_index(i, rows[i].main_space_id);
polys.main_sstore_write_offset.set_if_valid_index(i, rows[i].main_sstore_write_offset);
polys.main_tag_err.set_if_valid_index(i, rows[i].main_tag_err);
polys.main_w_in_tag.set_if_valid_index(i, rows[i].main_w_in_tag);
polys.mem_addr.set_if_valid_index(i, rows[i].mem_addr);
Expand Down Expand Up @@ -831,8 +810,6 @@ AvmCircuitBuilder::ProverPolynomials AvmCircuitBuilder::compute_polynomials() co
polys.lookup_l2_gas_rng_chk_1_counts.set_if_valid_index(i, rows[i].lookup_l2_gas_rng_chk_1_counts);
polys.lookup_da_gas_rng_chk_0_counts.set_if_valid_index(i, rows[i].lookup_da_gas_rng_chk_0_counts);
polys.lookup_da_gas_rng_chk_1_counts.set_if_valid_index(i, rows[i].lookup_da_gas_rng_chk_1_counts);
polys.kernel_output_lookup_counts.set_if_valid_index(i, rows[i].kernel_output_lookup_counts);
polys.lookup_into_kernel_counts.set_if_valid_index(i, rows[i].lookup_into_kernel_counts);
polys.lookup_cd_value_counts.set_if_valid_index(i, rows[i].lookup_cd_value_counts);
polys.lookup_ret_value_counts.set_if_valid_index(i, rows[i].lookup_ret_value_counts);
polys.incl_main_tag_err_counts.set_if_valid_index(i, rows[i].incl_main_tag_err_counts);
Expand Down
Loading
Loading