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
#6168 to migrate
`nargo info` over.
  • Loading branch information
TomAFrench committed May 10, 2024
1 parent c3c602f commit 78adcc0
Show file tree
Hide file tree
Showing 33 changed files with 129 additions and 248 deletions.
4 changes: 2 additions & 2 deletions barretenberg/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 barretenberg/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 barretenberg/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
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
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
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 barretenberg/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
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
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 barretenberg/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 barretenberg/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 barretenberg/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 barretenberg/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 barretenberg/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 barretenberg/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 barretenberg/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 barretenberg/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
4 changes: 0 additions & 4 deletions noir/noir-repo/compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ pub struct CompileOptions {
#[arg(long, conflicts_with = "deny_warnings")]
pub silence_warnings: bool,

/// Output ACIR gzipped bytecode instead of the JSON artefact
#[arg(long, hide = true)]
pub only_acir: bool,

/// Disables the builtin Aztec macros being used in the compiler
#[arg(long, hide = true)]
pub disable_macros: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "short"
name = "unit_value"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
[dependencies]
5 changes: 3 additions & 2 deletions noir/noir-repo/test_programs/rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ process_dir() {
if [ -d ./target/ ]; then
rm -r ./target/
fi
nargo compile --only-acir && nargo execute witness
nargo execute witness

if [ -d "$current_dir/acir_artifacts/$dir_name/target" ]; then
rm -r "$current_dir/acir_artifacts/$dir_name/target"
fi
mkdir $current_dir/acir_artifacts/$dir_name/target

mv ./target/$dir_name.json $current_dir/acir_artifacts/$dir_name/target/program.json
mv ./target/*.gz $current_dir/acir_artifacts/$dir_name/target/

cd $current_dir
Expand Down Expand Up @@ -70,4 +71,4 @@ if [ ! -z "$exit_status" ]; then
echo "Rebuild failed!"
exit $exit_status
fi
echo "Rebuild Succeeded!"
echo "Rebuild Succeeded!"
6 changes: 3 additions & 3 deletions noir/noir-repo/tooling/backend_interface/src/cli/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ fn contract_command() -> Result<(), BackendError> {

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path();
let bytecode_path = temp_directory_path.join("acir.gz");
let artifact_path = temp_directory_path.join("program.json");
let vk_path = temp_directory_path.join("vk");

let crs_path = backend.backend_directory();

std::fs::File::create(&bytecode_path).expect("file should be created");
std::fs::File::create(&artifact_path).expect("file should be created");

let write_vk_command = super::WriteVkCommand {
bytecode_path,
artifact_path,
vk_path_output: vk_path.clone(),
crs_path: crs_path.clone(),
};
Expand Down
10 changes: 5 additions & 5 deletions noir/noir-repo/tooling/backend_interface/src/cli/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::string_from_stderr;
/// for the given bytecode.
pub(crate) struct GatesCommand {
pub(crate) crs_path: PathBuf,
pub(crate) bytecode_path: PathBuf,
pub(crate) artifact_path: PathBuf,
}

#[derive(Deserialize)]
Expand All @@ -31,7 +31,7 @@ impl GatesCommand {
.arg("-c")
.arg(self.crs_path)
.arg("-b")
.arg(self.bytecode_path)
.arg(self.artifact_path)
.output()?;

if !output.status.success() {
Expand All @@ -53,12 +53,12 @@ fn gate_command() -> Result<(), BackendError> {

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path();
let bytecode_path = temp_directory_path.join("acir.gz");
let artifact_path = temp_directory_path.join("program.json");
let crs_path = backend.backend_directory();

std::fs::File::create(&bytecode_path).expect("file should be created");
std::fs::File::create(&artifact_path).expect("file should be created");

let gate_command = GatesCommand { crs_path, bytecode_path };
let gate_command = GatesCommand { crs_path, artifact_path };

let output = gate_command.run(backend.binary_path())?;
// Mock backend always returns zero gates.
Expand Down
10 changes: 5 additions & 5 deletions noir/noir-repo/tooling/backend_interface/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::string_from_stderr;
/// The proof will be written to the specified output file.
pub(crate) struct ProveCommand {
pub(crate) crs_path: PathBuf,
pub(crate) bytecode_path: PathBuf,
pub(crate) artifact_path: PathBuf,
pub(crate) witness_path: PathBuf,
}

Expand All @@ -26,7 +26,7 @@ impl ProveCommand {
.arg("-c")
.arg(self.crs_path)
.arg("-b")
.arg(self.bytecode_path)
.arg(self.artifact_path)
.arg("-w")
.arg(self.witness_path)
.arg("-o")
Expand All @@ -49,14 +49,14 @@ fn prove_command() -> Result<(), BackendError> {

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path();
let bytecode_path = temp_directory_path.join("acir.gz");
let artifact_path = temp_directory_path.join("acir.gz");
let witness_path = temp_directory_path.join("witness.tr");

std::fs::File::create(&bytecode_path).expect("file should be created");
std::fs::File::create(&artifact_path).expect("file should be created");
std::fs::File::create(&witness_path).expect("file should be created");

let crs_path = backend.backend_directory();
let prove_command = ProveCommand { crs_path, bytecode_path, witness_path };
let prove_command = ProveCommand { crs_path, artifact_path, witness_path };

let proof = prove_command.run(backend.binary_path())?;
assert_eq!(proof, "proof".as_bytes());
Expand Down
8 changes: 4 additions & 4 deletions noir/noir-repo/tooling/backend_interface/src/cli/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ fn verify_command() -> Result<(), BackendError> {

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path();
let bytecode_path = temp_directory_path.join("acir.gz");
let artifact_path = temp_directory_path.join("acir.json");
let witness_path = temp_directory_path.join("witness.tr");
let proof_path = temp_directory_path.join("1_mul.proof");
let vk_path_output = temp_directory_path.join("vk");

let crs_path = backend.backend_directory();

std::fs::File::create(&bytecode_path).expect("file should be created");
std::fs::File::create(&artifact_path).expect("file should be created");
std::fs::File::create(&witness_path).expect("file should be created");

let write_vk_command = WriteVkCommand {
bytecode_path: bytecode_path.clone(),
artifact_path: artifact_path.clone(),
crs_path: crs_path.clone(),
vk_path_output: vk_path_output.clone(),
};

write_vk_command.run(backend.binary_path())?;

let prove_command = ProveCommand { crs_path: crs_path.clone(), bytecode_path, witness_path };
let prove_command = ProveCommand { crs_path: crs_path.clone(), artifact_path, witness_path };
let proof = prove_command.run(backend.binary_path())?;

write_to_file(&proof, &proof_path);
Expand Down
10 changes: 5 additions & 5 deletions noir/noir-repo/tooling/backend_interface/src/cli/write_vk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::BackendError;
/// to write a verification key to a file
pub(crate) struct WriteVkCommand {
pub(crate) crs_path: PathBuf,
pub(crate) bytecode_path: PathBuf,
pub(crate) artifact_path: PathBuf,
pub(crate) vk_path_output: PathBuf,
}

Expand All @@ -21,7 +21,7 @@ impl WriteVkCommand {
.arg("-c")
.arg(self.crs_path)
.arg("-b")
.arg(self.bytecode_path)
.arg(self.artifact_path)
.arg("-o")
.arg(self.vk_path_output);

Expand All @@ -42,14 +42,14 @@ fn write_vk_command() -> Result<(), BackendError> {

let temp_directory = tempdir().expect("could not create a temporary directory");
let temp_directory_path = temp_directory.path();
let bytecode_path = temp_directory_path.join("acir.gz");
let artifact_path = temp_directory_path.join("program.json");
let vk_path_output = temp_directory.path().join("vk");

let crs_path = backend.backend_directory();

std::fs::File::create(&bytecode_path).expect("file should be created");
std::fs::File::create(&artifact_path).expect("file should be created");

let write_vk_command = WriteVkCommand { bytecode_path, crs_path, vk_path_output };
let write_vk_command = WriteVkCommand { artifact_path, crs_path, vk_path_output };

write_vk_command.run(backend.binary_path())?;
drop(temp_directory);
Expand Down
Loading

0 comments on commit 78adcc0

Please sign in to comment.