Skip to content

Commit

Permalink
feat(avm): avm circuit actually uses public inputs columns passed fro…
Browse files Browse the repository at this point in the history
…m TS (#6819)
  • Loading branch information
dbanks12 committed Jun 4, 2024
1 parent ffeb0e1 commit 1139a8d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ void avm_prove(const std::filesystem::path& bytecode_path,
init_bn254_crs(1 << 17);

// Prove execution and return vk
auto const [verification_key, proof] = avm_trace::Execution::prove(bytecode, calldata);
auto const [verification_key, proof] = avm_trace::Execution::prove(bytecode, calldata, public_inputs_vec);
// TODO(ilyas): <#4887>: Currently we only need these two parts of the vk, look into pcs_verification key reqs
std::vector<uint64_t> vk_vector = { verification_key.circuit_size, verification_key.num_public_inputs };
std::vector<fr> vk_as_fields = { verification_key.circuit_size, verification_key.num_public_inputs };
Expand Down
35 changes: 23 additions & 12 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace bb::avm_trace {
std::vector<FF> Execution::getDefaultPublicInputs()
{
std::vector<FF> public_inputs_vec(PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH);
public_inputs_vec.at(DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET) = 1000000000;
public_inputs_vec.at(L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET) = 1000000000;
public_inputs_vec.at(DA_START_GAS_LEFT_PCPI_OFFSET) = 1000000000;
public_inputs_vec.at(L2_START_GAS_LEFT_PCPI_OFFSET) = 1000000000;
return public_inputs_vec;
}

Expand All @@ -51,19 +51,25 @@ std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<u
std::vector<FF> const& public_inputs_vec,
ExecutionHints const& execution_hints)
{
// TODO: temp
info("logging to silence warning for now: ", public_inputs_vec.size());
if (public_inputs_vec.size() != PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH) {
throw_or_abort("Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
}

auto instructions = Deserialization::parse(bytecode);
std::vector<FF> returndata{};
auto trace = gen_trace(instructions, returndata, calldata, getDefaultPublicInputs(), execution_hints);
auto trace = gen_trace(instructions, returndata, calldata, public_inputs_vec, execution_hints);
auto circuit_builder = bb::AvmCircuitBuilder();
circuit_builder.set_trace(std::move(trace));

auto composer = AvmComposer();
auto prover = composer.create_prover(circuit_builder);
auto verifier = composer.create_verifier(circuit_builder);
auto proof = prover.construct_proof();

// The proof starts with the serialized public inputs
HonkProof proof(public_inputs_vec);
auto raw_proof = prover.construct_proof();
// append the raw proof after the public inputs
proof.insert(proof.end(), raw_proof.begin(), raw_proof.end());
// TODO(#4887): Might need to return PCS vk when full verify is supported
return std::make_tuple(*verifier.key, proof);
}
Expand Down Expand Up @@ -130,8 +136,8 @@ VmPublicInputs Execution::convert_public_inputs(std::vector<FF> const& public_in
// Transaction fee
kernel_inputs[TRANSACTION_FEE_SELECTOR] = public_inputs_vec[TRANSACTION_FEE_OFFSET];

kernel_inputs[DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET];
kernel_inputs[L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET];
kernel_inputs[DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[DA_START_GAS_LEFT_PCPI_OFFSET];
kernel_inputs[L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET] = public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET];

return public_inputs;
}
Expand All @@ -146,10 +152,15 @@ bool Execution::verify(AvmFlavor::VerificationKey vk, HonkProof const& proof)
// crs_factory_);
// output_state.pcs_verification_key = std::move(pcs_verification_key);

// TODO: We hardcode public inputs for now
VmPublicInputs public_inputs = convert_public_inputs(getDefaultPublicInputs());
std::vector<std::vector<FF>> public_inputs_vec = copy_public_inputs_columns(public_inputs);
return verifier.verify_proof(proof, public_inputs_vec);
std::vector<FF> public_inputs_vec;
std::vector<FF> raw_proof;
std::copy(
proof.begin(), proof.begin() + PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH, std::back_inserter(public_inputs_vec));
std::copy(proof.begin() + PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH, proof.end(), std::back_inserter(raw_proof));

VmPublicInputs public_inputs = convert_public_inputs(public_inputs_vec);
std::vector<std::vector<FF>> public_inputs_columns = copy_public_inputs_columns(public_inputs);
return verifier.verify_proof(raw_proof, public_inputs_columns);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class Execution {
std::vector<FF> const& public_inputs,
ExecutionHints const& execution_hints);

static std::tuple<AvmFlavor::VerificationKey, bb::HonkProof> prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata = {},
std::vector<FF> const& public_inputs_vec = {},
ExecutionHints const& execution_hints = {});
static std::tuple<AvmFlavor::VerificationKey, bb::HonkProof> prove(
std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata = {},
std::vector<FF> const& public_inputs_vec = getDefaultPublicInputs(),
ExecutionHints const& execution_hints = {});
static bool verify(AvmFlavor::VerificationKey vk, HonkProof const& proof);
};

Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ inline const uint32_t FEE_PER_DA_GAS_OFFSET = PCPI_GLOBALS_START + 6;
inline const uint32_t FEE_PER_L2_GAS_OFFSET = PCPI_GLOBALS_START + 7;

inline const uint32_t TRANSACTION_FEE_OFFSET = PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH - 1;
inline const uint32_t DA_GAS_LEFT_PCPI_OFFSET = PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH - 3 - GAS_LENGTH;
inline const uint32_t L2_GAS_LEFT_PCPI_OFFSET = PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH - 2 - GAS_LENGTH;
inline const uint32_t DA_START_GAS_LEFT_PCPI_OFFSET = PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH - 3 - GAS_LENGTH;
inline const uint32_t L2_START_GAS_LEFT_PCPI_OFFSET = PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH - 2 - GAS_LENGTH;

// Kernel output pil offset (Where update objects are inlined)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class AvmExecutionTests : public ::testing::Test {
void SetUp() override
{
srs::init_crs_factory("../srs_db/ignition");
public_inputs_vec.at(DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET) = DEFAULT_INITIAL_DA_GAS;
public_inputs_vec.at(L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET) = DEFAULT_INITIAL_L2_GAS;
public_inputs_vec.at(DA_START_GAS_LEFT_PCPI_OFFSET) = DEFAULT_INITIAL_DA_GAS;
public_inputs_vec.at(L2_START_GAS_LEFT_PCPI_OFFSET) = DEFAULT_INITIAL_L2_GAS;
};

/**
Expand Down

0 comments on commit 1139a8d

Please sign in to comment.