Skip to content

Commit

Permalink
Squash WIP onto trace str work
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Nov 18, 2024
1 parent 603b9c2 commit 4803ce7
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 23 deletions.
14 changes: 12 additions & 2 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,29 @@ void ClientIVC::accumulate(ClientCircuit& circuit, const std::shared_ptr<Verific
// Construct the proving key for circuit
std::shared_ptr<DeciderProvingKey> proving_key;
if (!initialized) {
info("CIVC is NOT initialized");
proving_key = std::make_shared<DeciderProvingKey>(circuit, trace_settings);
trace_usage_tracker = ExecutionTraceUsageTracker(trace_settings);
} else {
proving_key = std::make_shared<DeciderProvingKey>(
circuit, trace_settings, fold_output.accumulator->proving_key.commitment_key);
info("CIVC is YES IT IS initialized");
proving_key = std::make_shared<DeciderProvingKey>(circuit, trace_settings);
}

if (!bn254_commitment_key) {
info("commitment key being initialized");
bn254_commitment_key = std::make_shared<typename MegaFlavor::CommitmentKey>(proving_key->dyadic_circuit_size);
} else {
info("commitment key already initialized");
}
proving_key->proving_key.commitment_key = bn254_commitment_key;

vinfo("getting honk vk... precomputed?: ", precomputed_vk);
// Update the accumulator trace usage based on the present circuit
trace_usage_tracker.update(circuit);

// Set the verification key from precomputed if available, else compute it
honk_vk = precomputed_vk ? precomputed_vk : std::make_shared<VerificationKey>(proving_key->proving_key);
vinfo("set honk vk");
if (mock_vk) {
honk_vk->set_metadata(proving_key->proving_key);
}
Expand Down
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class ClientIVC {
// Management of linking databus commitments between circuits in the IVC
DataBusDepot bus_depot;

std::shared_ptr<typename MegaFlavor::CommitmentKey> bn254_commitment_key;

// Settings related to the use of fixed block sizes for each gate in the execution trace
TraceSettings trace_settings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ template <class Curve> class CommitmentKey {
: pippenger_runtime_state(get_num_needed_srs_points(num_points))
, crs_factory(srs::get_crs_factory<Curve>())
, srs(crs_factory->get_prover_crs(get_num_needed_srs_points(num_points)))
{}
{
info("CommitmentKey constructor from num_points being called!");
}

// Note: This constructor is to be used only by Plonk; For Honk the srs lives in the CommitmentKey
CommitmentKey(const size_t num_points, std::shared_ptr<srs::factories::ProverCrs<Curve>> prover_crs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ WASM_EXPORT void acir_prove_and_verify_aztec_client(uint8_t const* acir_stack,
}
// TODO(#7371) dedupe this with the rest of the similar code
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1101): remove use of auto_verify_mode
ClientIVC ivc{ { E2E_FULL_TEST_STRUCTURE }, /*auto_verify_mode=*/true };
ClientIVC ivc{ { CLIENT_IVC_BENCH_STRUCTURE }, /*auto_verify_mode=*/true };

// Accumulate the entire program stack into the IVC
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1116): remove manual setting of is_kernel once databus
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ template <typename FF, typename CommitmentKey_> class ProvingKey_ {
const size_t num_public_inputs,
std::shared_ptr<CommitmentKey_> commitment_key = nullptr)
{
info("In base PK constructor; commitment_key is nullptr?: ", commitment_key == nullptr);
this->commitment_key = commitment_key;
this->evaluation_domain = bb::EvaluationDomain<FF>(dyadic_circuit_size, dyadic_circuit_size);
this->circuit_size = dyadic_circuit_size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,9 @@ class MegaFlavor {
VerificationKey(ProvingKey& proving_key)
{
set_metadata(proving_key);
vinfo("set metadata in vk constructor");
if (proving_key.commitment_key == nullptr) {
vinfo("commitment key in proving key is null; setting commitment key");
proving_key.commitment_key = std::make_shared<CommitmentKey>(proving_key.circuit_size);
}
for (auto [polynomial, commitment] : zip_view(proving_key.polynomials.get_precomputed(), this->get_all())) {
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/sumcheck/sumcheck_output.hpp"
#include "barretenberg/transcript/transcript.hpp"
#include "barretenberg/ultra_honk/decider_proving_key.hpp"
#include "barretenberg/ultra_honk/decider_proving_key.hpp" // WORKTODO: not needed?
#include "sumcheck_round.hpp"

namespace bb {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ template <IsUltraFlavor Flavor> void DeciderProver_<Flavor>::execute_pcs_rounds(
if (proving_key->proving_key.commitment_key == nullptr) {
proving_key->proving_key.commitment_key =
std::make_shared<CommitmentKey>(proving_key->proving_key.circuit_size);
vinfo("made commitment key");
}
vinfo("made commitment key");
using OpeningClaim = ProverOpeningClaim<Curve>;

OpeningClaim prover_opening_claim;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ template <IsHonkFlavor Flavor> class DeciderProvingKey_ {
std::vector<FF> gate_challenges;
// The target sum, which is typically nonzero for a ProtogalaxyProver's accmumulator
FF target_sum;

size_t final_active_wire_idx{ 0 }; // idx of last non-trivial wire value in the trace
size_t dyadic_circuit_size{ 0 }; // final power-of-2 circuit size

DeciderProvingKey_(Circuit& circuit,
TraceSettings trace_settings = {},
std::shared_ptr<typename Flavor::CommitmentKey> commitment_key = nullptr)
: is_structured(trace_settings.structure.has_value())
DeciderProvingKey_(Circuit& circuit, TraceSettings trace_settings = {})
: is_structured(trace_settings.structure != TraceStructure::NONE)
{
PROFILE_THIS_NAME("DeciderProvingKey(Circuit&)");
vinfo("Constructing DeciderProvingKey");
Expand Down Expand Up @@ -79,6 +77,7 @@ template <IsHonkFlavor Flavor> class DeciderProvingKey_ {

// Complete the public inputs execution trace block from circuit.public_inputs
Trace::populate_public_inputs_block(circuit);
vinfo("populated public inputs block");
circuit.blocks.compute_offsets(is_structured);

// Find index of last non-trivial wire value in the trace
Expand All @@ -98,12 +97,14 @@ template <IsHonkFlavor Flavor> class DeciderProvingKey_ {

PROFILE_THIS_NAME("constructing proving key");

proving_key = ProvingKey(dyadic_circuit_size, circuit.public_inputs.size(), commitment_key);
proving_key = ProvingKey(dyadic_circuit_size, circuit.public_inputs.size());
// If not using structured trace OR if using structured trace but overflow has occurred (overflow block in
// use), allocate full size polys
vinfo("allocated polynomials object in proving key");
if ((IsMegaFlavor<Flavor> && !is_structured) || (is_structured && circuit.blocks.has_overflow)) {
// Allocate full size polynomials
proving_key.polynomials = typename Flavor::ProverPolynomials(dyadic_circuit_size);
vinfo("allocated polynomials object in proving key");
} else { // Allocate only a correct amount of memory for each polynomial
// Allocate the wires and selectors polynomials
{
Expand Down Expand Up @@ -328,7 +329,6 @@ template <IsHonkFlavor Flavor> class DeciderProvingKey_ {
private:
static constexpr size_t num_zero_rows = Flavor::has_zero_row ? 1 : 0;
static constexpr size_t NUM_WIRES = Circuit::NUM_WIRES;
size_t dyadic_circuit_size = 0; // final power-of-2 circuit size

size_t compute_dyadic_size(Circuit&);

Expand Down
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/ultra_honk/oink_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ template <IsUltraFlavor Flavor> void OinkProver<Flavor>::prove()
// Generate relation separators alphas for sumcheck/combiner computation
proving_key->alphas = generate_alphas_round();

#ifndef __wasm__
// Free the commitment key
proving_key->proving_key.commitment_key = nullptr;
#endif
}

/**
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/scripts/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ -z "$SKIP_CPP_BUILD" ]; then
# Build the wasms and strip debug symbols.
cd ../cpp
cmake --preset wasm-threads -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm-threads
cmake --preset wasm -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm
# cmake --preset wasm -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm
./scripts/strip-wasm.sh
cd ../ts
fi
Expand Down
4 changes: 2 additions & 2 deletions barretenberg/ts/src/barretenberg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export class Barretenberg extends BarretenbergApi {

async initSRSClientIVC(): Promise<void> {
// crsPath can be undefined
const crs = await Crs.new(2 ** 21 + 1, this.options.crsPath);
const grumpkinCrs = await GrumpkinCrs.new(2 ** 16 + 1, this.options.crsPath);
const crs = await Crs.new(2 ** 20 + 1, this.options.crsPath);
const grumpkinCrs = await GrumpkinCrs.new(2 ** 14 + 1, this.options.crsPath);

// Load CRS into wasm global CRS state.
// TODO: Make RawBuffer be default behavior, and have a specific Vector type for when wanting length prefixed.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ivc-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"./package.local.json"
],
"scripts": {
"build": "yarn clean && yarn generate && rm -rf dest && webpack && tsc -b",
"build": "yarn clean && yarn generate && tsc -b && rm -rf dest && webpack",
"clean": "rm -rf ./dest .tsbuildinfo src/types artifacts",
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/ivc-integration/package.local.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"build": "yarn clean && yarn generate && rm -rf dest && webpack && tsc -b",
"build": "yarn clean && yarn generate && tsc -b && rm -rf dest && webpack",
"clean": "rm -rf ./dest .tsbuildinfo src/types artifacts",
"test:non-browser": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --testPathIgnorePatterns=browser --passWithNoTests ",
"test:browser": "./run_browser_tests.sh",
Expand Down
63 changes: 59 additions & 4 deletions yarn-project/ivc-integration/src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,73 @@ import { generate3FunctionTestingIVCStack, proveAndVerifyBrowser } from './index
createDebug.enable('*');
const logger = createDebug('aztec:ivc-test');

// Function to set up the output element and redirect all console output
function setupConsoleOutput() {
const container = document.createElement('div');
container.style.marginBottom = '10px';
document.body.appendChild(container);

const copyButton = document.createElement('button');
copyButton.innerText = 'Copy Logs to Clipboard';
copyButton.style.marginBottom = '10px';
copyButton.addEventListener('click', () => {
const logContent = logContainer.textContent || ''; // Get text content of log container
navigator.clipboard
.writeText(logContent)
.then(() => {
alert('Logs copied to clipboard!');
})
.catch(err => {
console.error('Failed to copy logs:', err);
});
});
container.appendChild(copyButton);

const logContainer = document.createElement('pre');
logContainer.id = 'logOutput';
logContainer.style.border = '1px solid #ccc';
logContainer.style.padding = '10px';
logContainer.style.maxHeight = '400px';
logContainer.style.overflowY = 'auto';
container.appendChild(logContainer);

// Helper to append messages to logContainer
function addLogMessage(message: string) {
logContainer.textContent += message + '\n';
logContainer.scrollTop = logContainer.scrollHeight; // Auto-scroll to the bottom
}

// Override console methods to output to the page as well
const originalLog = console.log;
const originalDebug = console.debug;

console.log = (...args: any[]) => {
const message = args.join(' ');
originalLog.apply(console, args); // Keep original behavior
addLogMessage(message);
};

console.debug = (...args: any[]) => {
const message = args.join(' ');
originalDebug.apply(console, args); // Keep original behavior
addLogMessage(message);
};
}

(window as any).proveAndVerifyBrowser = proveAndVerifyBrowser;

document.addEventListener('DOMContentLoaded', function () {
setupConsoleOutput(); // Initialize console output capture

const button = document.createElement('button');
button.innerText = 'Run Test';
button.addEventListener('click', async () => {
logger(`generating circuit and witness...`);
[];
logger(`Generating circuit and witness...`);
const [bytecodes, witnessStack] = await generate3FunctionTestingIVCStack();
logger(`done. proving and verifying...`);
logger(`Done. Proving and verifying...`);
const verified = await proveAndVerifyBrowser(bytecodes, witnessStack);
logger(`verified? ${verified}`);
logger(`Verified? ${verified}`);
});

document.body.appendChild(button);
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Client IVC Integration', () => {
// 1. Run a mock app that creates two commitments
// 2. Run the init kernel to process the app run
// 3. Run the tail kernel to finish the client IVC chain.
it('Should generate a verifiable client IVC proof from a simple mock tx via bb.js', async () => {
it.only('Should generate a verifiable client IVC proof from a simple mock tx via bb.js', async () => {
const tx = {
number_of_calls: '0x1',
};
Expand Down

0 comments on commit 4803ce7

Please sign in to comment.