Skip to content

Commit

Permalink
7211: review comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Jun 28, 2024
1 parent aebab70 commit a35d7e5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
11 changes: 3 additions & 8 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,22 +723,18 @@ void avm_prove(const std::filesystem::path& bytecode_path,
*
* @param proof_path Path to the file containing the serialized proof
* @param vk_path Path to the file containing the serialized verification key
* @param calldata_path Path to the file containing the serialised calldata (could be empty)
* @return true If the proof is valid
* @return false If the proof is invalid
*/
bool avm_verify(const std::filesystem::path& proof_path,
const std::filesystem::path& vk_path,
const std::filesystem::path& calldata_path)
bool avm_verify(const std::filesystem::path& proof_path, const std::filesystem::path& vk_path)
{
std::vector<fr> const proof = many_from_buffer<fr>(read_file(proof_path));
std::vector<fr> const calldata = many_from_buffer<fr>(read_file(calldata_path));
std::vector<uint8_t> vk_bytes = read_file(vk_path);
auto circuit_size = from_buffer<size_t>(vk_bytes, 0);
auto num_public_inputs = from_buffer<size_t>(vk_bytes, sizeof(size_t));
auto vk = AvmFlavor::VerificationKey(circuit_size, num_public_inputs);

const bool verified = avm_trace::Execution::verify(vk, proof, calldata);
const bool verified = avm_trace::Execution::verify(vk, proof);
vinfo("verified: ", verified);
return verified;
}
Expand Down Expand Up @@ -1148,8 +1144,7 @@ int main(int argc, char* argv[])
avm_dump_trace_path = get_option(args, "--avm-dump-trace", "");
avm_prove(avm_bytecode_path, avm_calldata_path, avm_public_inputs_path, avm_hints_path, output_path);
} else if (command == "avm_verify") {
std::filesystem::path avm_calldata_path = get_option(args, "--avm-calldata", "./target/avm_calldata.bin");
return avm_verify(proof_path, vk_path, avm_calldata_path) ? 0 : 1;
return avm_verify(proof_path, vk_path) ? 0 : 1;
#endif
} else if (command == "prove_ultra_honk") {
std::string output_path = get_option(args, "-o", "./proofs/proof");
Expand Down
23 changes: 17 additions & 6 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "barretenberg/vm/avm_trace/avm_execution.hpp"
#include "barretenberg/bb/log.hpp"
#include "barretenberg/common/serialize.hpp"
#include "barretenberg/numeric/uint256/uint256.hpp"
#include "barretenberg/vm/avm_trace/avm_common.hpp"
#include "barretenberg/vm/avm_trace/avm_deserialization.hpp"
#include "barretenberg/vm/avm_trace/avm_helper.hpp"
Expand Down Expand Up @@ -78,10 +79,11 @@ std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<u
auto prover = composer.create_prover(circuit_builder);
auto verifier = composer.create_verifier(circuit_builder);

// The proof starts with the serialized public inputs
// Proof structure: public_inputs | calldata_size | calldata | raw proof
HonkProof proof(public_inputs_vec);
proof.emplace_back(calldata.size());
proof.insert(proof.end(), calldata.begin(), calldata.end());
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 @@ -251,7 +253,7 @@ VmPublicInputs Execution::convert_public_inputs(std::vector<FF> const& public_in
return public_inputs;
}

bool Execution::verify(AvmFlavor::VerificationKey vk, HonkProof const& proof, std::vector<FF> const& calldata)
bool Execution::verify(AvmFlavor::VerificationKey vk, HonkProof const& proof)
{
auto verification_key = std::make_shared<AvmFlavor::VerificationKey>(vk);
AvmVerifier verifier(verification_key);
Expand All @@ -261,11 +263,20 @@ bool Execution::verify(AvmFlavor::VerificationKey vk, HonkProof const& proof, st
// crs_factory_);
// output_state.pcs_verification_key = std::move(pcs_verification_key);

// Proof structure: public_inputs | calldata_size | calldata | raw proof
std::vector<FF> public_inputs_vec;
std::vector<FF> calldata;
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));

// This can be made nicer using BB's serialize::read, probably.
const auto public_inputs_offset = proof.begin();
const auto calldata_size_offset = public_inputs_offset + PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH;
const auto calldata_offset = calldata_size_offset + 1;
const auto raw_proof_offset = calldata_offset + static_cast<int64_t>(uint64_t(*calldata_size_offset));

std::copy(public_inputs_offset, calldata_size_offset, std::back_inserter(public_inputs_vec));
std::copy(calldata_offset, raw_proof_offset, std::back_inserter(calldata));
std::copy(raw_proof_offset, 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, calldata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Execution {
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, std::vector<FF> const& calldata);
static bool verify(AvmFlavor::VerificationKey vk, HonkProof const& proof);
};

} // namespace bb::avm_trace
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AvmCastTests : public ::testing::Test {

VmPublicInputs public_inputs;
AvmTraceBuilder trace_builder;
std::vector<FF> calldata{};
std::vector<FF> calldata;

std::vector<Row> trace;
size_t main_addr;
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ const proveAndVerifyAvmTestContract = async (

// Then we verify.
const rawVkPath = path.join(succeededRes.vkPath!, 'vk');
const calldataPath = path.join(bbWorkingDirectory, 'avm_calldata.bin');
const verificationRes = await verifyAvmProof(bbPath, succeededRes.proofPath!, rawVkPath, logger, calldataPath);
const verificationRes = await verifyAvmProof(bbPath, succeededRes.proofPath!, rawVkPath, logger);
expect(verificationRes.status).toBe(BB_RESULT.SUCCESS);
};

Expand Down
22 changes: 9 additions & 13 deletions yarn-project/bb-prover/src/bb/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const VK_FILENAME = 'vk';
export const VK_FIELDS_FILENAME = 'vk_fields.json';
export const PROOF_FILENAME = 'proof';
export const PROOF_FIELDS_FILENAME = 'proof_fields.json';
export const CALLDATA_FILENAME = 'avm_calldata.bin';
export const AVM_BYTECODE_FILENAME = 'avm_bytecode.bin';
export const AVM_CALLDATA_FILENAME = 'avm_calldata.bin';
export const AVM_PUBLIC_INPUTS_FILENAME = 'avm_public_inputs.bin';
export const AVM_HINTS_FILENAME = 'avm_hints.bin';

export enum BB_RESULT {
SUCCESS,
Expand Down Expand Up @@ -277,10 +280,10 @@ export async function generateAvmProof(
}

// Paths for the inputs
const bytecodePath = join(workingDirectory, 'avm_bytecode.bin');
const calldataPath = join(workingDirectory, CALLDATA_FILENAME);
const publicInputsPath = join(workingDirectory, 'avm_public_inputs.bin');
const avmHintsPath = join(workingDirectory, 'avm_hints.bin');
const bytecodePath = join(workingDirectory, AVM_BYTECODE_FILENAME);
const calldataPath = join(workingDirectory, AVM_CALLDATA_FILENAME);
const publicInputsPath = join(workingDirectory, AVM_PUBLIC_INPUTS_FILENAME);
const avmHintsPath = join(workingDirectory, AVM_HINTS_FILENAME);

// The proof is written to e.g. /workingDirectory/proof
const outputPath = workingDirectory;
Expand Down Expand Up @@ -386,7 +389,6 @@ export async function verifyProof(
* @param pathToBB - The full path to the bb binary
* @param proofFullPath - The full path to the proof to be verified
* @param verificationKeyPath - The full path to the circuit verification key
* @param calldataPath - The full path to calldata
* @param log - A logging function
* @returns An object containing a result indication and duration taken
*/
Expand All @@ -395,9 +397,8 @@ export async function verifyAvmProof(
proofFullPath: string,
verificationKeyPath: string,
log: LogFn,
calldataPath?: string,
): Promise<BBFailure | BBSuccess> {
return await verifyProofInternal(pathToBB, proofFullPath, verificationKeyPath, 'avm_verify', log, calldataPath);
return await verifyProofInternal(pathToBB, proofFullPath, verificationKeyPath, 'avm_verify', log);
}

/**
Expand All @@ -407,7 +408,6 @@ export async function verifyAvmProof(
* @param verificationKeyPath - The full path to the circuit verification key
* @param command - The BB command to execute (verify/avm_verify)
* @param log - A logging function
* @param calldataPath - The full path to calldata (only relevant for avm_verify)
* @returns An object containing a result indication and duration taken
*/
async function verifyProofInternal(
Expand All @@ -416,7 +416,6 @@ async function verifyProofInternal(
verificationKeyPath: string,
command: 'verify' | 'avm_verify',
log: LogFn,
calldataPath?: string,
): Promise<BBFailure | BBSuccess> {
const binaryPresent = await fs
.access(pathToBB, fs.constants.R_OK)
Expand All @@ -428,9 +427,6 @@ async function verifyProofInternal(

try {
const args = ['-p', proofFullPath, '-k', verificationKeyPath];
if (calldataPath !== undefined) {
args.push('--avm-calldata', calldataPath);
}
const timer = new Timer();
const result = await executeBB(pathToBB, command, args, log);
const duration = timer.ms();
Expand Down
2 changes: 0 additions & 2 deletions yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import * as path from 'path';
import {
type BBSuccess,
BB_RESULT,
CALLDATA_FILENAME,
PROOF_FIELDS_FILENAME,
PROOF_FILENAME,
VK_FILENAME,
Expand Down Expand Up @@ -641,7 +640,6 @@ export class BBNativeRollupProver implements ServerCircuitProver {
proofFileName,
verificationKeyPath!,
logFunction,
verificationFunction instanceof verifyAvmProof ? path.join(bbWorkingDirectory, CALLDATA_FILENAME) : undefined,
);

if (result.status === BB_RESULT.FAILURE) {
Expand Down

0 comments on commit a35d7e5

Please sign in to comment.