From fb9ee4f66a562c829e2a4c18951b3d77e6651e32 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:21:14 +0100 Subject: [PATCH] chore: encapsulate abstraction leaks from `bb` into new crate (#2747) --- Cargo.lock | 49 ++++++++++++------- Cargo.toml | 4 +- tooling/acvm_backend_barretenberg/.gitignore | 1 - .../CHANGELOG.md | 0 .../Cargo.toml | 5 +- .../src/cli/contract.rs | 0 .../src/cli/gates.rs | 0 .../src/cli/info.rs | 0 .../src/cli/mod.rs | 0 .../src/cli/prove.rs | 0 .../src/cli/verify.rs | 0 .../src/cli/write_vk.rs | 0 .../src/download.rs | 0 .../src/lib.rs | 8 ++- .../src/proof_system.rs | 43 +++------------- .../src/smart_contract.rs | 13 +---- .../test-binaries/mock_backend/Cargo.lock | 0 .../test-binaries/mock_backend/Cargo.toml | 0 .../mock_backend/src/contract_cmd.rs | 6 +-- .../mock_backend/src/gates_cmd.rs | 0 .../mock_backend/src/info_cmd.rs | 0 .../test-binaries/mock_backend/src/main.rs | 0 .../mock_backend/src/prove_cmd.rs | 0 .../mock_backend/src/verify_cmd.rs | 0 .../mock_backend/src/write_vk_cmd.rs | 0 tooling/bb_abstraction_leaks/Cargo.toml | 17 +++++++ .../build.rs | 0 .../src/contract.sol | 0 tooling/bb_abstraction_leaks/src/lib.rs | 34 +++++++++++++ tooling/nargo_cli/Cargo.toml | 3 +- tooling/nargo_cli/src/backends.rs | 4 +- .../src/cli/backend_cmd/install_cmd.rs | 2 +- .../nargo_cli/src/cli/backend_cmd/ls_cmd.rs | 2 +- .../src/cli/backend_cmd/uninstall_cmd.rs | 2 +- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 8 ++- tooling/nargo_cli/src/cli/compile_cmd.rs | 2 +- tooling/nargo_cli/src/cli/info_cmd.rs | 2 +- tooling/nargo_cli/src/errors.rs | 2 +- tooling/nargo_cli/tests/execute.rs | 5 +- 39 files changed, 122 insertions(+), 90 deletions(-) delete mode 100644 tooling/acvm_backend_barretenberg/.gitignore rename tooling/{acvm_backend_barretenberg => backend_interface}/CHANGELOG.md (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/Cargo.toml (78%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/contract.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/gates.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/info.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/mod.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/prove.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/verify.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/cli/write_vk.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/download.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/lib.rs (94%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/proof_system.rs (74%) rename tooling/{acvm_backend_barretenberg => backend_interface}/src/smart_contract.rs (79%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/Cargo.lock (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/Cargo.toml (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/contract_cmd.rs (72%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/gates_cmd.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/info_cmd.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/main.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/prove_cmd.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/verify_cmd.rs (100%) rename tooling/{acvm_backend_barretenberg => backend_interface}/test-binaries/mock_backend/src/write_vk_cmd.rs (100%) create mode 100644 tooling/bb_abstraction_leaks/Cargo.toml rename tooling/{acvm_backend_barretenberg => bb_abstraction_leaks}/build.rs (100%) rename tooling/{acvm_backend_barretenberg => bb_abstraction_leaks}/src/contract.sol (100%) create mode 100644 tooling/bb_abstraction_leaks/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8cccc14d393..646d422f686 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,24 +46,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "acvm-backend-barretenberg" -version = "0.11.0" -dependencies = [ - "acvm", - "build-target", - "const_format", - "dirs", - "flate2", - "reqwest", - "serde", - "serde_json", - "tar", - "tempfile", - "test-binary", - "thiserror", -] - [[package]] name = "acvm_blackbox_solver" version = "0.26.1" @@ -402,6 +384,25 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backend-interface" +version = "0.11.0" +dependencies = [ + "acvm", + "bb_abstraction_leaks", + "build-target", + "const_format", + "dirs", + "flate2", + "reqwest", + "serde", + "serde_json", + "tar", + "tempfile", + "test-binary", + "thiserror", +] + [[package]] name = "backtrace" version = "0.3.68" @@ -435,6 +436,15 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "bb_abstraction_leaks" +version = "0.11.0" +dependencies = [ + "acvm", + "build-target", + "const_format", +] + [[package]] name = "bincode" version = "1.3.3" @@ -2186,10 +2196,11 @@ name = "nargo_cli" version = "0.12.0" dependencies = [ "acvm", - "acvm-backend-barretenberg", "assert_cmd", "assert_fs", "async-lsp", + "backend-interface", + "bb_abstraction_leaks", "build-data", "clap", "color-eyre", diff --git a/Cargo.toml b/Cargo.toml index dac1c15e0a5..2879823163e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,8 @@ members = [ "compiler/utils/arena", "compiler/utils/iter-extended", # Crates related to tooling built ontop of the Noir compiler - "tooling/acvm_backend_barretenberg", + "tooling/backend_interface", + "tooling/bb_abstraction_leaks", "tooling/nargo", "tooling/nargo_cli", "tooling/nargo_toml", @@ -41,6 +42,7 @@ nargo_cli = { path = "tooling/nargo_cli" } nargo_toml = { path = "tooling/nargo_toml" } noir_lsp = { path = "tooling/lsp" } noirc_abi = { path = "tooling/noirc_abi" } +bb_abstraction_leaks = { path = "tooling/bb_abstraction_leaks" } noirc_driver = { path = "compiler/noirc_driver" } noirc_errors = { path = "compiler/noirc_errors" } noirc_evaluator = { path = "compiler/noirc_evaluator" } diff --git a/tooling/acvm_backend_barretenberg/.gitignore b/tooling/acvm_backend_barretenberg/.gitignore deleted file mode 100644 index 106a4f552a0..00000000000 --- a/tooling/acvm_backend_barretenberg/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!src/witness.tr diff --git a/tooling/acvm_backend_barretenberg/CHANGELOG.md b/tooling/backend_interface/CHANGELOG.md similarity index 100% rename from tooling/acvm_backend_barretenberg/CHANGELOG.md rename to tooling/backend_interface/CHANGELOG.md diff --git a/tooling/acvm_backend_barretenberg/Cargo.toml b/tooling/backend_interface/Cargo.toml similarity index 78% rename from tooling/acvm_backend_barretenberg/Cargo.toml rename to tooling/backend_interface/Cargo.toml index a86fa90b0be..127770c5722 100644 --- a/tooling/acvm_backend_barretenberg/Cargo.toml +++ b/tooling/backend_interface/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "acvm-backend-barretenberg" -description = "An ACVM backend which allows proving/verifying ACIR circuits against Aztec Lab's Barretenberg library." +name = "backend-interface" +description = "The definition of the backend CLI interface which Nargo uses for proving/verifying ACIR circuits." version = "0.11.0" authors.workspace = true edition.workspace = true @@ -15,6 +15,7 @@ dirs.workspace = true thiserror.workspace = true serde.workspace = true serde_json.workspace = true +bb_abstraction_leaks.workspace = true tempfile = "3.6.0" diff --git a/tooling/acvm_backend_barretenberg/src/cli/contract.rs b/tooling/backend_interface/src/cli/contract.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/contract.rs rename to tooling/backend_interface/src/cli/contract.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/gates.rs b/tooling/backend_interface/src/cli/gates.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/gates.rs rename to tooling/backend_interface/src/cli/gates.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/info.rs b/tooling/backend_interface/src/cli/info.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/info.rs rename to tooling/backend_interface/src/cli/info.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/mod.rs b/tooling/backend_interface/src/cli/mod.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/mod.rs rename to tooling/backend_interface/src/cli/mod.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/prove.rs b/tooling/backend_interface/src/cli/prove.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/prove.rs rename to tooling/backend_interface/src/cli/prove.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/verify.rs b/tooling/backend_interface/src/cli/verify.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/verify.rs rename to tooling/backend_interface/src/cli/verify.rs diff --git a/tooling/acvm_backend_barretenberg/src/cli/write_vk.rs b/tooling/backend_interface/src/cli/write_vk.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/cli/write_vk.rs rename to tooling/backend_interface/src/cli/write_vk.rs diff --git a/tooling/acvm_backend_barretenberg/src/download.rs b/tooling/backend_interface/src/download.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/src/download.rs rename to tooling/backend_interface/src/download.rs diff --git a/tooling/acvm_backend_barretenberg/src/lib.rs b/tooling/backend_interface/src/lib.rs similarity index 94% rename from tooling/acvm_backend_barretenberg/src/lib.rs rename to tooling/backend_interface/src/lib.rs index b79ca514043..6d18b6b6ead 100644 --- a/tooling/acvm_backend_barretenberg/src/lib.rs +++ b/tooling/backend_interface/src/lib.rs @@ -9,10 +9,10 @@ mod proof_system; mod smart_contract; use acvm::acir::circuit::Opcode; +use bb_abstraction_leaks::ACVM_BACKEND_BARRETENBERG; pub use download::download_backend; const BACKENDS_DIR: &str = ".nargo/backends"; -pub const ACVM_BACKEND_BARRETENBERG: &str = "acvm-backend-barretenberg"; pub fn backends_directory() -> PathBuf { let home_directory = dirs::home_dir().unwrap(); @@ -65,6 +65,10 @@ impl Backend { Backend { name, binary_path } } + pub fn name(&self) -> &str { + &self.name + } + fn binary_path(&self) -> &PathBuf { &self.binary_path } @@ -77,7 +81,7 @@ impl Backend { if self.name == ACVM_BACKEND_BARRETENBERG { // If we're trying to use barretenberg, automatically go and install it. let bb_url = std::env::var("BB_BINARY_URL") - .unwrap_or_else(|_| env!("BB_BINARY_URL").to_string()); + .unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned()); download_backend(&bb_url, binary_path)?; return Ok(binary_path); } diff --git a/tooling/acvm_backend_barretenberg/src/proof_system.rs b/tooling/backend_interface/src/proof_system.rs similarity index 74% rename from tooling/acvm_backend_barretenberg/src/proof_system.rs rename to tooling/backend_interface/src/proof_system.rs index b681883fdad..ffdb7531ed1 100644 --- a/tooling/acvm_backend_barretenberg/src/proof_system.rs +++ b/tooling/backend_interface/src/proof_system.rs @@ -63,15 +63,10 @@ impl Backend { } .run(binary_path)?; - // Barretenberg return the proof prepended with the public inputs. - // - // This is not how the API expects the proof to be formatted, - // so we remove the public inputs from the proof. - // - // TODO: As noted in the verification procedure, this is an abstraction leak - // TODO: and will need modifications to barretenberg - let proof = - remove_public_inputs(circuit.public_inputs().0.len(), &proof_with_public_inputs); + let proof = bb_abstraction_leaks::remove_public_inputs( + circuit.public_inputs().0.len(), + &proof_with_public_inputs, + ); Ok(proof) } @@ -92,12 +87,10 @@ impl Backend { let flattened_public_inputs: Vec = public_inputs.into_iter().map(|(_, el)| el).collect(); - // Barretenberg expects the proof to be prepended with the public inputs. - // - // TODO: This is an abstraction leak and barretenberg's API should accept the public inputs - // TODO: separately and then prepend them internally - let proof_with_public_inputs = - prepend_public_inputs(proof.to_vec(), flattened_public_inputs.to_vec()); + let proof_with_public_inputs = bb_abstraction_leaks::prepend_public_inputs( + proof.to_vec(), + flattened_public_inputs.to_vec(), + ); // Create a temporary file for the proof let proof_path = temp_directory.join("proof").with_extension("proof"); @@ -139,26 +132,6 @@ pub(super) fn write_to_file(bytes: &[u8], path: &Path) -> String { } } -/// Removes the public inputs which are prepended to a proof by Barretenberg. -fn remove_public_inputs(num_pub_inputs: usize, proof: &[u8]) -> Vec { - // Barretenberg prepends the public inputs onto the proof so we need to remove - // the first `num_pub_inputs` field elements. - let num_bytes_to_remove = num_pub_inputs * (FieldElement::max_num_bytes() as usize); - proof[num_bytes_to_remove..].to_vec() -} - -/// Prepends a set of public inputs to a proof. -fn prepend_public_inputs(proof: Vec, public_inputs: Vec) -> Vec { - if public_inputs.is_empty() { - return proof; - } - - let public_inputs_bytes = - public_inputs.into_iter().flat_map(|assignment| assignment.to_be_bytes()); - - public_inputs_bytes.chain(proof).collect() -} - // TODO: See nargo/src/artifacts/mod.rs // TODO: This method should live in ACVM and be the default method for serializing/deserializing circuits pub(super) fn serialize_circuit(circuit: &Circuit) -> Vec { diff --git a/tooling/acvm_backend_barretenberg/src/smart_contract.rs b/tooling/backend_interface/src/smart_contract.rs similarity index 79% rename from tooling/acvm_backend_barretenberg/src/smart_contract.rs rename to tooling/backend_interface/src/smart_contract.rs index e5018c69bd9..5f56557cad4 100644 --- a/tooling/acvm_backend_barretenberg/src/smart_contract.rs +++ b/tooling/backend_interface/src/smart_contract.rs @@ -6,9 +6,6 @@ use crate::{ use acvm::acir::circuit::Circuit; use tempfile::tempdir; -/// Embed the Solidity verifier file -const ULTRA_VERIFIER_CONTRACT: &str = include_str!("contract.sol"); - impl Backend { pub fn eth_contract(&self, circuit: &Circuit) -> Result { let binary_path = self.assert_binary_exists()?; @@ -32,11 +29,7 @@ impl Backend { } .run(binary_path)?; - let verification_key_library = - ContractCommand { crs_path: self.crs_directory(), vk_path }.run(binary_path)?; - - drop(temp_directory); - Ok(format!("{verification_key_library}{ULTRA_VERIFIER_CONTRACT}")) + ContractCommand { crs_path: self.crs_directory(), vk_path }.run(binary_path) } } @@ -67,9 +60,7 @@ mod tests { let contract = get_mock_backend()?.eth_contract(&circuit)?; - assert!(contract.contains("contract BaseUltraVerifier")); - assert!(contract.contains("contract UltraVerifier")); - assert!(contract.contains("library UltraVerificationKey")); + assert!(contract.contains("contract VerifierContract")); Ok(()) } diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/Cargo.lock b/tooling/backend_interface/test-binaries/mock_backend/Cargo.lock similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/Cargo.lock rename to tooling/backend_interface/test-binaries/mock_backend/Cargo.lock diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/Cargo.toml b/tooling/backend_interface/test-binaries/mock_backend/Cargo.toml similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/Cargo.toml rename to tooling/backend_interface/test-binaries/mock_backend/Cargo.toml diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/contract_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/contract_cmd.rs similarity index 72% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/contract_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/contract_cmd.rs index fb8daf784f1..7ee41121d61 100644 --- a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/contract_cmd.rs +++ b/tooling/backend_interface/test-binaries/mock_backend/src/contract_cmd.rs @@ -17,9 +17,5 @@ pub(crate) struct ContractCommand { pub(crate) fn run(args: ContractCommand) { assert!(args.vk_path.is_file(), "Could not find vk file at provided path"); - std::io::stdout() - .write_all( - b"contract BaseUltraVerifier contract UltraVerifier library UltraVerificationKey", - ) - .unwrap(); + std::io::stdout().write_all(b"contract VerifierContract {}").unwrap(); } diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/gates_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/gates_cmd.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/gates_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/gates_cmd.rs diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/info_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/info_cmd.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/info_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/info_cmd.rs diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/main.rs b/tooling/backend_interface/test-binaries/mock_backend/src/main.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/main.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/main.rs diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/prove_cmd.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/prove_cmd.rs diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/verify_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/verify_cmd.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/verify_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/verify_cmd.rs diff --git a/tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/write_vk_cmd.rs b/tooling/backend_interface/test-binaries/mock_backend/src/write_vk_cmd.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/test-binaries/mock_backend/src/write_vk_cmd.rs rename to tooling/backend_interface/test-binaries/mock_backend/src/write_vk_cmd.rs diff --git a/tooling/bb_abstraction_leaks/Cargo.toml b/tooling/bb_abstraction_leaks/Cargo.toml new file mode 100644 index 00000000000..86164ba11c3 --- /dev/null +++ b/tooling/bb_abstraction_leaks/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "bb_abstraction_leaks" +description = "A crate which encapsulates knowledge about Barretenberg which is currently leaking into Nargo" +version = "0.11.0" +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +acvm.workspace = true + +[build-dependencies] +build-target = "0.4.0" +const_format = "0.2.30" diff --git a/tooling/acvm_backend_barretenberg/build.rs b/tooling/bb_abstraction_leaks/build.rs similarity index 100% rename from tooling/acvm_backend_barretenberg/build.rs rename to tooling/bb_abstraction_leaks/build.rs diff --git a/tooling/acvm_backend_barretenberg/src/contract.sol b/tooling/bb_abstraction_leaks/src/contract.sol similarity index 100% rename from tooling/acvm_backend_barretenberg/src/contract.sol rename to tooling/bb_abstraction_leaks/src/contract.sol diff --git a/tooling/bb_abstraction_leaks/src/lib.rs b/tooling/bb_abstraction_leaks/src/lib.rs new file mode 100644 index 00000000000..799e14c36f0 --- /dev/null +++ b/tooling/bb_abstraction_leaks/src/lib.rs @@ -0,0 +1,34 @@ +#![warn(unused_crate_dependencies, unused_extern_crates)] +#![warn(unreachable_pub)] + +use acvm::FieldElement; + +pub const ACVM_BACKEND_BARRETENBERG: &str = "acvm-backend-barretenberg"; +pub const BB_DOWNLOAD_URL: &str = env!("BB_BINARY_URL"); + +/// Embed the Solidity verifier file +const ULTRA_VERIFIER_CONTRACT: &str = include_str!("contract.sol"); + +pub fn complete_barretenberg_verifier_contract(contract: String) -> String { + format!("{contract}{ULTRA_VERIFIER_CONTRACT}") +} + +/// Removes the public inputs which are prepended to a proof by Barretenberg. +pub fn remove_public_inputs(num_pub_inputs: usize, proof: &[u8]) -> Vec { + // Barretenberg prepends the public inputs onto the proof so we need to remove + // the first `num_pub_inputs` field elements. + let num_bytes_to_remove = num_pub_inputs * (FieldElement::max_num_bytes() as usize); + proof[num_bytes_to_remove..].to_vec() +} + +/// Prepends a set of public inputs to a proof. +pub fn prepend_public_inputs(proof: Vec, public_inputs: Vec) -> Vec { + if public_inputs.is_empty() { + return proof; + } + + let public_inputs_bytes = + public_inputs.into_iter().flat_map(|assignment| assignment.to_be_bytes()); + + public_inputs_bytes.chain(proof).collect() +} diff --git a/tooling/nargo_cli/Cargo.toml b/tooling/nargo_cli/Cargo.toml index 0b85c66b6fa..e14d43b9fc3 100644 --- a/tooling/nargo_cli/Cargo.toml +++ b/tooling/nargo_cli/Cargo.toml @@ -50,7 +50,8 @@ color-eyre = "0.6.2" tokio = { version = "1.0", features = ["io-std"] } # Backends -acvm-backend-barretenberg = { path = "../acvm_backend_barretenberg" } +backend-interface = { path = "../backend_interface" } +bb_abstraction_leaks.workspace = true [target.'cfg(not(unix))'.dependencies] tokio-util = { version = "0.7.8", features = ["compat"] } diff --git a/tooling/nargo_cli/src/backends.rs b/tooling/nargo_cli/src/backends.rs index 8ca8305e180..8b1da2cd118 100644 --- a/tooling/nargo_cli/src/backends.rs +++ b/tooling/nargo_cli/src/backends.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; -use acvm_backend_barretenberg::backends_directory; -pub(crate) use acvm_backend_barretenberg::Backend; +use backend_interface::backends_directory; +pub(crate) use backend_interface::Backend; fn active_backend_file_path() -> PathBuf { backends_directory().join(".selected_backend") diff --git a/tooling/nargo_cli/src/cli/backend_cmd/install_cmd.rs b/tooling/nargo_cli/src/cli/backend_cmd/install_cmd.rs index 99bca29d3b7..974db9ff7f5 100644 --- a/tooling/nargo_cli/src/cli/backend_cmd/install_cmd.rs +++ b/tooling/nargo_cli/src/cli/backend_cmd/install_cmd.rs @@ -1,6 +1,6 @@ use clap::Args; -use acvm_backend_barretenberg::{backends_directory, download_backend}; +use backend_interface::{backends_directory, download_backend}; use crate::errors::{BackendError, CliError}; diff --git a/tooling/nargo_cli/src/cli/backend_cmd/ls_cmd.rs b/tooling/nargo_cli/src/cli/backend_cmd/ls_cmd.rs index 38ff6d3b744..da37b104d65 100644 --- a/tooling/nargo_cli/src/cli/backend_cmd/ls_cmd.rs +++ b/tooling/nargo_cli/src/cli/backend_cmd/ls_cmd.rs @@ -1,4 +1,4 @@ -use acvm_backend_barretenberg::backends_directory; +use backend_interface::backends_directory; use clap::Args; use crate::errors::CliError; diff --git a/tooling/nargo_cli/src/cli/backend_cmd/uninstall_cmd.rs b/tooling/nargo_cli/src/cli/backend_cmd/uninstall_cmd.rs index 469f099de28..7497f1bc2f6 100644 --- a/tooling/nargo_cli/src/cli/backend_cmd/uninstall_cmd.rs +++ b/tooling/nargo_cli/src/cli/backend_cmd/uninstall_cmd.rs @@ -1,6 +1,6 @@ use clap::Args; -use acvm_backend_barretenberg::backends_directory; +use backend_interface::backends_directory; use crate::{ backends::{ diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index 5ad451f6a6a..30b865063d3 100644 --- a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -10,6 +10,7 @@ use crate::errors::CliError; use acvm::acir::circuit::Opcode; use acvm::Language; +use bb_abstraction_leaks::ACVM_BACKEND_BARRETENBERG; use clap::Args; use nargo::artifacts::program::PreprocessedProgram; use nargo::package::Package; @@ -91,7 +92,12 @@ fn smart_contract_for_package( } }; - let smart_contract_string = backend.eth_contract(&preprocessed_program.bytecode)?; + let mut smart_contract_string = backend.eth_contract(&preprocessed_program.bytecode)?; + + if backend.name() == ACVM_BACKEND_BARRETENBERG { + smart_contract_string = + bb_abstraction_leaks::complete_barretenberg_verifier_contract(smart_contract_string); + } Ok(smart_contract_string) } diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index fb8a91c8811..3ccd99558a7 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -2,7 +2,7 @@ use std::path::Path; use acvm::acir::circuit::Opcode; use acvm::Language; -use acvm_backend_barretenberg::BackendOpcodeSupport; +use backend_interface::BackendOpcodeSupport; use fm::FileManager; use iter_extended::vecmap; use nargo::artifacts::contract::PreprocessedContract; diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index 39708a26bd8..8018308ee54 100644 --- a/tooling/nargo_cli/src/cli/info_cmd.rs +++ b/tooling/nargo_cli/src/cli/info_cmd.rs @@ -1,5 +1,5 @@ use acvm::Language; -use acvm_backend_barretenberg::BackendError; +use backend_interface::BackendError; use clap::Args; use iter_extended::vecmap; use nargo::package::Package; diff --git a/tooling/nargo_cli/src/errors.rs b/tooling/nargo_cli/src/errors.rs index 205f68f624e..49fa02d281d 100644 --- a/tooling/nargo_cli/src/errors.rs +++ b/tooling/nargo_cli/src/errors.rs @@ -70,7 +70,7 @@ pub(crate) enum CliError { /// Error related to communication with backend. #[error(transparent)] - BackendCommunicationError(#[from] acvm_backend_barretenberg::BackendError), + BackendCommunicationError(#[from] backend_interface::BackendError), } #[derive(Debug, thiserror::Error)] diff --git a/tooling/nargo_cli/tests/execute.rs b/tooling/nargo_cli/tests/execute.rs index 9e02951573d..e2bef43b571 100644 --- a/tooling/nargo_cli/tests/execute.rs +++ b/tooling/nargo_cli/tests/execute.rs @@ -12,10 +12,7 @@ mod tests { use super::*; - test_binary::build_test_binary_once!( - mock_backend, - "../acvm_backend_barretenberg/test-binaries" - ); + test_binary::build_test_binary_once!(mock_backend, "../backend_interface/test-binaries"); // include tests generated by `build.rs` include!(concat!(env!("OUT_DIR"), "/execute.rs"));