diff --git a/barretenberg/cpp/src/barretenberg/bb/main.cpp b/barretenberg/cpp/src/barretenberg/bb/main.cpp index 588200222d6..12cfa7f26e7 100644 --- a/barretenberg/cpp/src/barretenberg/bb/main.cpp +++ b/barretenberg/cpp/src/barretenberg/bb/main.cpp @@ -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 vk_vector = { verification_key.circuit_size, verification_key.num_public_inputs }; std::vector vk_as_fields = { verification_key.circuit_size, verification_key.num_public_inputs }; diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp index 733f118beb2..83afebb9fc1 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp @@ -32,8 +32,8 @@ namespace bb::avm_trace { std::vector Execution::getDefaultPublicInputs() { std::vector 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; } @@ -47,18 +47,28 @@ std::vector Execution::getDefaultPublicInputs() * @return The verifier key and zk proof of the execution. */ std::tuple Execution::prove(std::vector const& bytecode, - std::vector const& calldata) + std::vector const& calldata, + std::vector 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"); + } + auto instructions = Deserialization::parse(bytecode); std::vector 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(); + // 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); } @@ -108,8 +118,8 @@ VmPublicInputs Execution::convert_public_inputs(std::vector 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; } @@ -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> public_inputs_vec = copy_public_inputs_columns(public_inputs); - return verifier.verify_proof(proof, public_inputs_vec); + std::vector public_inputs_vec; + std::vector 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> public_inputs_columns = copy_public_inputs_columns(public_inputs); + return verifier.verify_proof(raw_proof, public_inputs_columns); } /** diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.hpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.hpp index af4919a9b18..cc7bef51b61 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.hpp @@ -29,8 +29,10 @@ class Execution { static std::vector gen_trace(std::vector const& instructions, std::vector const& calldata, std::vector const& public_inputs); - static std::tuple prove(std::vector const& bytecode, - std::vector const& calldata = {}); + static std::tuple prove( + std::vector const& bytecode, + std::vector const& calldata = {}, + std::vector const& public_inputs_vec = getDefaultPublicInputs()); static bool verify(AvmFlavor::VerificationKey vk, HonkProof const& proof); }; diff --git a/barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp b/barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp index d3ca2ab0a92..dd331e283b5 100644 --- a/barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp +++ b/barretenberg/cpp/src/barretenberg/vm/avm_trace/constants.hpp @@ -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; \ No newline at end of file +inline const std::size_t L2_GAS_LEFT_CONTEXT_INPUTS_OFFSET = KERNEL_INPUTS_LENGTH - 2;