Skip to content

Commit

Permalink
feat!: switch bb over to read ACIR from nargo artifacts (#6283)
Browse files Browse the repository at this point in the history
This PR further harmonises the `nargo`/`bb` interface by switching `bb`
over to reading the ACIR bytecode directly out of a `nargo` JSON build
artifact.

I've dropped support for contracts in `nargo info` as we're dropping
this feature anyway and this would require us to write a whole bunch of
extra files which we don't need.

Draft as it builds on #6279 plus requires
AztecProtocol/aztec-packages#6168 to migrate
`nargo info` over.
  • Loading branch information
TomAFrench authored and AztecBot committed May 11, 2024
1 parent db38194 commit 7d076dd
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions acir_tests/flows/all_cmds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

VFLAG=${VERBOSE:+-v}
BFLAG="-b ./target/acir.gz"
BFLAG="-b ./target/program.json"
FLAGS="-c $CRS_PATH $VFLAG"

# Test we can perform the proof/verify flow.
Expand All @@ -19,4 +19,4 @@ $BIN contract -k vk $BFLAG -o - | grep "Verification Key Hash" > /dev/null
OUTPUT=$($BIN proof_as_fields -k vk -p proof -o - | jq .)
[ -n "$OUTPUT" ] || exit 1
OUTPUT=$($BIN vk_as_fields -k vk -o - | jq .)
[ -n "$OUTPUT" ] || exit 1
[ -n "$OUTPUT" ] || exit 1
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_and_verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ VFLAG=${VERBOSE:+-v}

# This is the fastest flow, because it only generates pk/vk once, gate count once, etc.
# It may not catch all class of bugs.
$BIN prove_and_verify $VFLAG -c $CRS_PATH -b ./target/acir.gz
$BIN prove_and_verify $VFLAG -c $CRS_PATH -b ./target/program.json
4 changes: 2 additions & 2 deletions acir_tests/flows/prove_and_verify_goblin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -eu

VFLAG=${VERBOSE:+-v}

$BIN prove_and_verify_goblin $VFLAG -c $CRS_PATH -b ./target/acir.gz
$BIN prove_and_verify_goblin $VFLAG -c $CRS_PATH -b ./target/program.json

# This command can be used to run all of the tests in sequence with the debugger
# lldb-16 -o run -b -- $BIN prove_and_verify_goblin $VFLAG -c $CRS_PATH -b ./target/acir.gz
# lldb-16 -o run -b -- $BIN prove_and_verify_goblin $VFLAG -c $CRS_PATH -b ./target/program.json
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_and_verify_goblin_ultra_honk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eu

VFLAG=${VERBOSE:+-v}

$BIN prove_and_verify_goblin_ultra_honk $VFLAG -c $CRS_PATH -b ./target/acir.gz
$BIN prove_and_verify_goblin_ultra_honk $VFLAG -c $CRS_PATH -b ./target/program.json
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_and_verify_ultra_honk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eu

VFLAG=${VERBOSE:+-v}

$BIN prove_and_verify_ultra_honk $VFLAG -c $CRS_PATH -b ./target/acir.gz
$BIN prove_and_verify_ultra_honk $VFLAG -c $CRS_PATH -b ./target/program.json
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_and_verify_ultra_honk_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eu

VFLAG=${VERBOSE:+-v}

$BIN prove_and_verify_ultra_honk_program $VFLAG -c $CRS_PATH -b ./target/acir.gz
$BIN prove_and_verify_ultra_honk_program $VFLAG -c $CRS_PATH -b ./target/program.json
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_then_verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

VFLAG=${VERBOSE:+-v}
BFLAG="-b ./target/acir.gz"
BFLAG="-b ./target/program.json"
FLAGS="-c $CRS_PATH $VFLAG"

# Test we can perform the proof/verify flow.
Expand Down
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_then_verify_goblin_ultra_honk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

VFLAG=${VERBOSE:+-v}
BFLAG="-b ./target/acir.gz"
BFLAG="-b ./target/program.json"
FLAGS="-c $CRS_PATH $VFLAG"

# Test we can perform the proof/verify flow.
Expand Down
2 changes: 1 addition & 1 deletion acir_tests/flows/prove_then_verify_ultra_honk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -eu

VFLAG=${VERBOSE:+-v}
BFLAG="-b ./target/acir.gz"
BFLAG="-b ./target/program.json"
FLAGS="-c $CRS_PATH $VFLAG"

# Test we can perform the proof/verify flow.
Expand Down
4 changes: 2 additions & 2 deletions acir_tests/flows/sol.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export PROOF_AS_FIELDS="$(pwd)/proof_fields.json"
$BIN prove -o proof
$BIN write_vk -o vk
$BIN proof_as_fields -k vk -c $CRS_PATH -p $PROOF
$BIN contract -k vk -c $CRS_PATH -b ./target/acir.gz -o Key.sol
$BIN contract -k vk -c $CRS_PATH -b ./target/program.json -o Key.sol

# Export the paths to the environment variables for the js test runner
export KEY_PATH="$(pwd)/Key.sol"
Expand All @@ -20,4 +20,4 @@ export BASE_PATH=$(realpath "../../../sol/src/ultra/BaseUltraVerifier.sol")
# index.js will start an anvil, on a random port
# Deploy the verifier then send a test transaction
export TEST_NAME=$(basename $(pwd))
node ../../sol-test/src/index.js
node ../../sol-test/src/index.js
2 changes: 1 addition & 1 deletion acir_tests/flows/write_contract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -eu
export TEST_NAME=$(basename $(pwd))

$BIN write_vk -o vk
$BIN contract -k vk -c $CRS_PATH -b ./target/acir.gz -o $TEST_NAME.sol
$BIN contract -k vk -c $CRS_PATH -b ./target/program.json -o $TEST_NAME.sol
2 changes: 1 addition & 1 deletion acir_tests/gen_inner_proof_inputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $BIN vk_as_fields $VFLAG -c $CRS_PATH
echo "Generate proof to file..."
[ -d "$PROOF_DIR" ] || mkdir $PWD/proofs
[ -e "$PROOF_PATH" ] || touch $PROOF_PATH
$BIN prove $VFLAG -c $CRS_PATH -b ./target/acir.gz -o "./proofs/$PROOF_NAME" $RFLAG
$BIN prove $VFLAG -c $CRS_PATH -b ./target/program.json -o "./proofs/$PROOF_NAME" $RFLAG

echo "Write proof as fields for recursion..."
$BIN proof_as_fields $VFLAG -c $CRS_PATH -p "./proofs/$PROOF_NAME"
Expand Down
14 changes: 11 additions & 3 deletions acir_tests/headless-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ function formatAndPrintLog(message: string): void {
}

const readBytecodeFile = (path: string): Uint8Array => {
const data = fs.readFileSync(path);
const buffer = gunzipSync(data);
return buffer;
const extension = path.substring(path.lastIndexOf('.') + 1);

if (extension == 'json') {
const encodedCircuit = JSON.parse(fs.readFileSync(path, 'utf8'));
const decompressed = gunzipSync(Uint8Array.from(atob(encodedCircuit.bytecode), c => c.charCodeAt(0)));
return decompressed;
}

const encodedCircuit = fs.readFileSync(path);
const decompressed = gunzipSync(encodedCircuit);
return decompressed;
};

const readWitnessFile = (path: string): Uint8Array => {
Expand Down
2 changes: 1 addition & 1 deletion acir_tests/run_acir_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ else
continue
fi

if [[ ! -f ./$TEST_NAME/target/acir.gz || ! -f ./$TEST_NAME/target/witness.gz ]]; then
if [[ ! -f ./$TEST_NAME/target/program.json || ! -f ./$TEST_NAME/target/witness.gz ]]; then
echo -e "\033[33mSKIPPED\033[0m (uncompiled)"
continue
fi
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/barretenberg/bb/get_bytecode.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#pragma once
#include "exec_pipe.hpp"
#include <filesystem>

/**
* We can assume for now we're running on a unix like system and use the following to extract the bytecode.
*/
inline std::vector<uint8_t> get_bytecode(const std::string& bytecodePath)
{
std::filesystem::path filePath = bytecodePath;
if (filePath.extension() == ".json") {
// Try reading json files as if they are a Nargo build artifact
std::string command = "jq -r '.bytecode' \"" + bytecodePath + "\" | base64 -d | gunzip -c";
return exec_pipe(command);
}

// For other extensions, assume file is a raw ACIR program
std::string command = "gunzip -c \"" + bytecodePath + "\"";
return exec_pipe(command);
}
}
2 changes: 1 addition & 1 deletion cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ int main(int argc, char* argv[])

std::string command = args[0];

std::string bytecode_path = get_option(args, "-b", "./target/acir.gz");
std::string bytecode_path = get_option(args, "-b", "./target/program.json");
std::string witness_path = get_option(args, "-w", "./target/witness.gz");
std::string proof_path = get_option(args, "-p", "./proofs/proof");
std::string vk_path = get_option(args, "-k", "./target/vk");
Expand Down
8 changes: 8 additions & 0 deletions ts/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const MAX_CIRCUIT_SIZE = 2 ** 19;
const threads = +process.env.HARDWARE_CONCURRENCY! || undefined;

function getBytecode(bytecodePath: string) {
const extension = bytecodePath.substring(bytecodePath.lastIndexOf('.') + 1);

if (extension == 'json') {
const encodedCircuit = JSON.parse(readFileSync(bytecodePath, 'utf8'));
const decompressed = gunzipSync(Buffer.from(encodedCircuit.bytecode, 'base64'));
return decompressed;
}

const encodedCircuit = readFileSync(bytecodePath);
const decompressed = gunzipSync(encodedCircuit);
return decompressed;
Expand Down

0 comments on commit 7d076dd

Please sign in to comment.