Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add full mocked backend implementation #2559

Merged
merged 9 commits into from
Sep 5, 2023
Merged
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/acvm_backend_barretenberg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ reqwest = { version = "0.11.16", default-features = false, features = [

[dev-dependencies]
serial_test = "2.0.0"
test-binary = "3.0.1"

[build-dependencies]
build-target = "0.4.0"
10 changes: 2 additions & 8 deletions crates/acvm_backend_barretenberg/src/cli/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::BackendError;
/// to verify a proof. See acvm_interop/contract.sol for the
/// remaining logic that is missing.
pub(crate) struct ContractCommand {
pub(crate) verbose: bool,
pub(crate) crs_path: PathBuf,
pub(crate) vk_path: PathBuf,
pub(crate) contract_path: PathBuf,
Expand All @@ -29,10 +28,6 @@ impl ContractCommand {
.arg("-o")
.arg(self.contract_path);

if self.verbose {
command.arg("-v");
}

let output = command.output().expect("Failed to execute command");
if output.status.success() {
Ok(())
Expand All @@ -47,7 +42,7 @@ impl ContractCommand {
fn contract_command() {
use tempfile::tempdir;

let backend = crate::get_bb();
let backend = crate::get_mock_backend();

let bytecode_path = PathBuf::from("./src/1_mul.bytecode");

Expand All @@ -59,7 +54,6 @@ fn contract_command() {
let crs_path = backend.backend_directory();

let write_vk_command = super::WriteVkCommand {
verbose: true,
bytecode_path,
vk_path_output: vk_path.clone(),
is_recursive: false,
Expand All @@ -68,7 +62,7 @@ fn contract_command() {

assert!(write_vk_command.run(&backend.binary_path()).is_ok());

let contract_command = ContractCommand { verbose: true, vk_path, crs_path, contract_path };
let contract_command = ContractCommand { vk_path, crs_path, contract_path };

assert!(contract_command.run(&backend.binary_path()).is_ok());
drop(temp_directory);
Expand Down
5 changes: 3 additions & 2 deletions crates/acvm_backend_barretenberg/src/cli/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ impl GatesCommand {
#[test]
#[serial_test::serial]
fn gate_command() {
let backend = crate::get_bb();
let backend = crate::get_mock_backend();
let bytecode_path = PathBuf::from("./src/1_mul.bytecode");

let crs_path = backend.backend_directory();

let gate_command = GatesCommand { crs_path, bytecode_path };

let output = gate_command.run(&backend.binary_path()).unwrap();
assert_eq!(output, 2788);
// Mock backend always returns zero gates.
assert_eq!(output, 0);
}
5 changes: 3 additions & 2 deletions crates/acvm_backend_barretenberg/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ pub(crate) use write_vk::WriteVkCommand;
fn no_command_provided_works() {
// This is a simple test to check that the binaries work

let backend = crate::get_bb();
let backend = crate::get_mock_backend();

let output = std::process::Command::new(backend.binary_path())
.output()
.expect("Failed to execute command");

let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(stderr, "No command provided.\n");
// Assert help message is printed due to no command being provided.
assert!(stderr.contains("Usage: mock_backend <COMMAND>"));
}
16 changes: 3 additions & 13 deletions crates/acvm_backend_barretenberg/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::BackendError;
///
/// The proof will be written to the specified output file.
pub(crate) struct ProveCommand {
pub(crate) verbose: bool,
pub(crate) crs_path: PathBuf,
pub(crate) is_recursive: bool,
pub(crate) bytecode_path: PathBuf,
Expand All @@ -33,9 +32,6 @@ impl ProveCommand {
.arg("-o")
.arg(self.proof_path);

if self.verbose {
command.arg("-v");
}
if self.is_recursive {
command.arg("-r");
}
Expand All @@ -54,7 +50,7 @@ impl ProveCommand {
fn prove_command() {
use tempfile::tempdir;

let backend = crate::get_bb();
let backend = crate::get_mock_backend();

let bytecode_path = PathBuf::from("./src/1_mul.bytecode");
let witness_path = PathBuf::from("./src/witness.tr");
Expand All @@ -63,14 +59,8 @@ fn prove_command() {
let proof_path = temp_directory.path().join("1_mul").with_extension("proof");

let crs_path = backend.backend_directory();
let prove_command = ProveCommand {
verbose: true,
crs_path,
bytecode_path,
witness_path,
is_recursive: false,
proof_path,
};
let prove_command =
ProveCommand { crs_path, bytecode_path, witness_path, is_recursive: false, proof_path };

let proof_created = prove_command.run(&backend.binary_path());
assert!(proof_created.is_ok());
Expand Down
17 changes: 3 additions & 14 deletions crates/acvm_backend_barretenberg/src/cli/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::path::{Path, PathBuf};
/// VerifyCommand will call the barretenberg binary
/// to verify a proof
pub(crate) struct VerifyCommand {
pub(crate) verbose: bool,
pub(crate) crs_path: PathBuf,
pub(crate) is_recursive: bool,
pub(crate) proof_path: PathBuf,
Expand All @@ -23,9 +22,6 @@ impl VerifyCommand {
.arg("-k")
.arg(self.vk_path);

if self.verbose {
command.arg("-v");
}
if self.is_recursive {
command.arg("-r");
}
Expand All @@ -44,7 +40,7 @@ fn verify_command() {

use super::{ProveCommand, WriteVkCommand};

let backend = crate::get_bb();
let backend = crate::get_mock_backend();

let bytecode_path = PathBuf::from("./src/1_mul.bytecode");
let witness_path = PathBuf::from("./src/witness.tr");
Expand All @@ -57,7 +53,6 @@ fn verify_command() {
let crs_path = backend.backend_directory();

let write_vk_command = WriteVkCommand {
verbose: true,
bytecode_path: bytecode_path.clone(),
crs_path: crs_path.clone(),
is_recursive: false,
Expand All @@ -68,7 +63,6 @@ fn verify_command() {
assert!(vk_written.is_ok());

let prove_command = ProveCommand {
verbose: true,
crs_path: crs_path.clone(),
is_recursive: false,
bytecode_path,
Expand All @@ -77,13 +71,8 @@ fn verify_command() {
};
prove_command.run(&backend.binary_path()).unwrap();

let verify_command = VerifyCommand {
verbose: true,
crs_path,
is_recursive: false,
proof_path,
vk_path: vk_path_output,
};
let verify_command =
VerifyCommand { crs_path, is_recursive: false, proof_path, vk_path: vk_path_output };

let verified = verify_command.run(&backend.binary_path());
assert!(verified);
Expand Down
15 changes: 3 additions & 12 deletions crates/acvm_backend_barretenberg/src/cli/write_vk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::BackendError;
/// WriteCommand will call the barretenberg binary
/// to write a verification key to a file
pub(crate) struct WriteVkCommand {
pub(crate) verbose: bool,
pub(crate) crs_path: PathBuf,
pub(crate) is_recursive: bool,
pub(crate) bytecode_path: PathBuf,
Expand All @@ -25,9 +24,6 @@ impl WriteVkCommand {
.arg("-o")
.arg(self.vk_path_output);

if self.verbose {
command.arg("-v");
}
if self.is_recursive {
command.arg("-r");
}
Expand All @@ -46,7 +42,7 @@ impl WriteVkCommand {
fn write_vk_command() {
use tempfile::tempdir;

let backend = crate::get_bb();
let backend = crate::get_mock_backend();

let bytecode_path = PathBuf::from("./src/1_mul.bytecode");

Expand All @@ -55,13 +51,8 @@ fn write_vk_command() {

let crs_path = backend.backend_directory();

let write_vk_command = WriteVkCommand {
verbose: true,
bytecode_path,
crs_path,
is_recursive: false,
vk_path_output,
};
let write_vk_command =
WriteVkCommand { bytecode_path, crs_path, is_recursive: false, vk_path_output };

let vk_written = write_vk_command.run(&backend.binary_path());
assert!(vk_written.is_ok());
Expand Down
15 changes: 11 additions & 4 deletions crates/acvm_backend_barretenberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ pub fn backends_directory() -> PathBuf {
}

#[cfg(test)]
fn get_bb() -> Backend {
let bb = Backend::new("acvm-backend-barretenberg".to_string());
crate::assert_binary_exists(&bb);
bb
test_binary::build_test_binary_once!(mock_backend, "test-binaries");

#[cfg(test)]
fn get_mock_backend() -> Backend {
std::env::set_var("NARGO_BACKEND_PATH", path_to_mock_backend());

let mock_backend = Backend::new("mock_backend".to_string());
if !mock_backend.binary_path().is_file() {
panic!("Mock backend binary does not exist at expected path");
}
mock_backend
}

fn assert_binary_exists(backend: &Backend) -> PathBuf {
Expand Down
13 changes: 3 additions & 10 deletions crates/acvm_backend_barretenberg/src/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl Backend {
let binary_path = assert_binary_exists(self);
// Create proof and store it in the specified path
ProveCommand {
verbose: true,
crs_path: self.crs_directory(),
is_recursive,
bytecode_path,
Expand Down Expand Up @@ -140,7 +139,6 @@ impl Backend {

let binary_path = assert_binary_exists(self);
WriteVkCommand {
verbose: false,
crs_path: self.crs_directory(),
is_recursive,
bytecode_path,
Expand All @@ -149,14 +147,9 @@ impl Backend {
.run(&binary_path)?;

// Verify the proof
let valid_proof = VerifyCommand {
verbose: false,
crs_path: self.crs_directory(),
is_recursive,
proof_path,
vk_path,
}
.run(&binary_path);
let valid_proof =
VerifyCommand { crs_path: self.crs_directory(), is_recursive, proof_path, vk_path }
.run(&binary_path);

Ok(valid_proof)
}
Expand Down
6 changes: 2 additions & 4 deletions crates/acvm_backend_barretenberg/src/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl Backend {

let binary_path = assert_binary_exists(self);
WriteVkCommand {
verbose: false,
crs_path: self.crs_directory(),
is_recursive: false,
bytecode_path,
Expand All @@ -36,7 +35,6 @@ impl Backend {

let contract_path = temp_directory_path.join("contract");
ContractCommand {
verbose: false,
crs_path: self.crs_directory(),
vk_path,
contract_path: contract_path.clone(),
Expand All @@ -60,7 +58,7 @@ mod tests {
native_types::{Expression, Witness},
};

use crate::get_bb;
use crate::get_mock_backend;

#[test]
#[serial_test::serial]
Expand All @@ -77,7 +75,7 @@ mod tests {
assert_messages: Default::default(),
};

let contract = get_bb().eth_contract(&circuit).unwrap();
let contract = get_mock_backend().eth_contract(&circuit).unwrap();

assert!(contract.contains("contract BaseUltraVerifier"));
assert!(contract.contains("contract UltraVerifier"));
Expand Down
Loading
Loading