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): avm circuit actually uses public inputs columns passed from TS #6819

Merged
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
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 @@ -542,7 +542,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
37 changes: 26 additions & 11 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 @@ -47,18 +47,28 @@ std::vector<FF> Execution::getDefaultPublicInputs()
* @return The verifier key and zk proof of the execution.
*/
std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata)
std::vector<FF> const& calldata,
std::vector<FF> const& public_inputs_vec)
{
if (public_inputs_vec.size() != PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH) {
throw_or_abort("Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: if you have to modify this PR for some other reason, you could add the expected vs gotten sizes here.

}

auto instructions = Deserialization::parse(bytecode);
std::vector<FF> returndata{};
auto trace = gen_trace(instructions, returndata, calldata, getDefaultPublicInputs());
auto trace = gen_trace(instructions, returndata, calldata, public_inputs_vec);
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();
Comment on lines +68 to +69
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok with keeping the order, but I'd switch the types:

std::vector<ff> proof(public_inputs_vec);
HonkProof 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 @@ -108,8 +118,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 @@ -124,10 +134,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 @@ -29,8 +29,10 @@ class Execution {
static std::vector<Row> gen_trace(std::vector<Instruction> const& instructions,
std::vector<FF> const& calldata,
std::vector<FF> const& public_inputs);
static std::tuple<AvmFlavor::VerificationKey, bb::HonkProof> prove(std::vector<uint8_t> const& bytecode,
std::vector<FF> const& calldata = {});
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());
static bool verify(AvmFlavor::VerificationKey vk, HonkProof const& proof);
};

Expand Down
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ 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;

// END INDEXES in the PUBLIC_CIRCUIT_PUBLIC_INPUTS

// L2 and Da gas left are the 3rd last and 2nd last items in the context kernel inputs respectively
inline const std::size_t DA_GAS_LEFT_CONTEXT_INPUTS_OFFSET = KERNEL_INPUTS_LENGTH - 3;
inline const std::size_t L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET = KERNEL_INPUTS_LENGTH - 2;
inline const std::size_t L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET = KERNEL_INPUTS_LENGTH - 2;
Loading