diff --git a/crates/acvm_backend_barretenberg/src/bb/contract.rs b/crates/acvm_backend_barretenberg/src/bb/contract.rs index d84a8daa44a..49edefafb1b 100644 --- a/crates/acvm_backend_barretenberg/src/bb/contract.rs +++ b/crates/acvm_backend_barretenberg/src/bb/contract.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use super::{assert_binary_exists, get_binary_path, CliShimError}; /// VerifyCommand will call the barretenberg binary @@ -9,9 +11,9 @@ use super::{assert_binary_exists, get_binary_path, CliShimError}; /// remaining logic that is missing. pub(crate) struct ContractCommand { pub(crate) verbose: bool, - pub(crate) path_to_crs: String, - pub(crate) path_to_vk: String, - pub(crate) path_to_contract: String, + pub(crate) crs_path: PathBuf, + pub(crate) vk_path: PathBuf, + pub(crate) contract_path: PathBuf, } impl ContractCommand { @@ -22,11 +24,11 @@ impl ContractCommand { command .arg("contract") .arg("-c") - .arg(self.path_to_crs) + .arg(self.crs_path) .arg("-k") - .arg(self.path_to_vk) + .arg(self.vk_path) .arg("-o") - .arg(self.path_to_contract); + .arg(self.contract_path); if self.verbose { command.arg("-v"); @@ -46,30 +48,25 @@ impl ContractCommand { fn contract_command() { use tempfile::tempdir; - let path_to_1_mul = "./src/1_mul.bytecode"; + let bytecode_path = PathBuf::from("./src/1_mul.bytecode"); let temp_directory = tempdir().expect("could not create a temporary directory"); let temp_directory_path = temp_directory.path(); - let path_to_crs = temp_directory_path.join("crs"); - let path_to_vk = temp_directory_path.join("vk"); - let path_to_contract = temp_directory_path.join("contract"); + let crs_path = temp_directory_path.join("crs"); + let vk_path = temp_directory_path.join("vk"); + let contract_path = temp_directory_path.join("contract"); let write_vk_command = super::WriteVkCommand { verbose: true, - path_to_bytecode: path_to_1_mul.to_string(), - path_to_vk_output: path_to_vk.to_str().unwrap().to_string(), + bytecode_path, + vk_path_output: vk_path.clone(), is_recursive: false, - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + crs_path: crs_path.clone(), }; assert!(write_vk_command.run().is_ok()); - let contract_command = ContractCommand { - verbose: true, - path_to_vk: path_to_vk.to_str().unwrap().to_string(), - path_to_crs: path_to_crs.to_str().unwrap().to_string(), - path_to_contract: path_to_contract.to_str().unwrap().to_string(), - }; + let contract_command = ContractCommand { verbose: true, vk_path, crs_path, contract_path }; assert!(contract_command.run().is_ok()); drop(temp_directory); diff --git a/crates/acvm_backend_barretenberg/src/bb/gates.rs b/crates/acvm_backend_barretenberg/src/bb/gates.rs index 38cfb9b135d..eaa631bd076 100644 --- a/crates/acvm_backend_barretenberg/src/bb/gates.rs +++ b/crates/acvm_backend_barretenberg/src/bb/gates.rs @@ -1,11 +1,13 @@ +use std::path::PathBuf; + use super::{assert_binary_exists, get_binary_path}; /// GatesCommand will call the barretenberg binary /// to return the number of gates needed to create a proof /// for the given bytecode. pub(crate) struct GatesCommand { - pub(crate) path_to_crs: String, - pub(crate) path_to_bytecode: String, + pub(crate) crs_path: PathBuf, + pub(crate) bytecode_path: PathBuf, } impl GatesCommand { @@ -14,9 +16,9 @@ impl GatesCommand { let output = std::process::Command::new(get_binary_path()) .arg("gates") .arg("-c") - .arg(self.path_to_crs) + .arg(self.crs_path) .arg("-b") - .arg(self.path_to_bytecode) + .arg(self.bytecode_path) .output() .expect("Failed to execute command"); @@ -55,16 +57,13 @@ impl GatesCommand { fn gate_command() { use tempfile::tempdir; - let path_to_1_mul = "./src/1_mul.bytecode"; + let bytecode_path = PathBuf::from("./src/1_mul.bytecode"); let temp_directory = tempdir().expect("could not create a temporary directory"); let temp_directory_path = temp_directory.path(); - let path_to_crs = temp_directory_path.join("crs"); + let crs_path = temp_directory_path.join("crs"); - let gate_command = GatesCommand { - path_to_crs: path_to_crs.to_str().unwrap().to_string(), - path_to_bytecode: path_to_1_mul.to_string(), - }; + let gate_command = GatesCommand { crs_path, bytecode_path }; let output = gate_command.run(); assert_eq!(output, 2775); diff --git a/crates/acvm_backend_barretenberg/src/bb/prove.rs b/crates/acvm_backend_barretenberg/src/bb/prove.rs index 481748e291a..a2cad1c9811 100644 --- a/crates/acvm_backend_barretenberg/src/bb/prove.rs +++ b/crates/acvm_backend_barretenberg/src/bb/prove.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use super::{assert_binary_exists, get_binary_path, CliShimError}; /// ProveCommand will call the barretenberg binary @@ -9,11 +11,11 @@ use super::{assert_binary_exists, get_binary_path, CliShimError}; /// The proof will be written to the specified output file. pub(crate) struct ProveCommand { pub(crate) verbose: bool, - pub(crate) path_to_crs: String, + pub(crate) crs_path: PathBuf, pub(crate) is_recursive: bool, - pub(crate) path_to_bytecode: String, - pub(crate) path_to_witness: String, - pub(crate) path_to_proof: String, + pub(crate) bytecode_path: PathBuf, + pub(crate) witness_path: PathBuf, + pub(crate) proof_path: PathBuf, } impl ProveCommand { @@ -24,13 +26,13 @@ impl ProveCommand { command .arg("prove") .arg("-c") - .arg(self.path_to_crs) + .arg(self.crs_path) .arg("-b") - .arg(self.path_to_bytecode) + .arg(self.bytecode_path) .arg("-w") - .arg(self.path_to_witness) + .arg(self.witness_path) .arg("-o") - .arg(self.path_to_proof); + .arg(self.proof_path); if self.verbose { command.arg("-v"); @@ -54,22 +56,22 @@ impl ProveCommand { fn prove_command() { use tempfile::tempdir; - let path_to_1_mul = "./src/1_mul.bytecode"; - let path_to_1_mul_witness = "./src/witness.tr"; + let bytecode_path = PathBuf::from("./src/1_mul.bytecode"); + let witness_path = PathBuf::from("./src/witness.tr"); let temp_directory = tempdir().expect("could not create a temporary directory"); let temp_directory_path = temp_directory.path(); - let path_to_crs = temp_directory_path.join("crs"); - let path_to_proof = temp_directory_path.join("1_mul").with_extension("proof"); + let crs_path = temp_directory_path.join("crs"); + let proof_path = temp_directory_path.join("1_mul").with_extension("proof"); let prove_command = ProveCommand { verbose: true, - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + crs_path, + bytecode_path, + witness_path, is_recursive: false, - path_to_bytecode: path_to_1_mul.to_string(), - path_to_witness: path_to_1_mul_witness.to_string(), - path_to_proof: path_to_proof.to_str().unwrap().to_string(), + proof_path, }; let proof_created = prove_command.run(); diff --git a/crates/acvm_backend_barretenberg/src/bb/verify.rs b/crates/acvm_backend_barretenberg/src/bb/verify.rs index f7352fa232f..77e356a98c5 100644 --- a/crates/acvm_backend_barretenberg/src/bb/verify.rs +++ b/crates/acvm_backend_barretenberg/src/bb/verify.rs @@ -1,13 +1,15 @@ +use std::path::PathBuf; + use super::{assert_binary_exists, get_binary_path}; /// VerifyCommand will call the barretenberg binary /// to verify a proof pub(crate) struct VerifyCommand { pub(crate) verbose: bool, - pub(crate) path_to_crs: String, + pub(crate) crs_path: PathBuf, pub(crate) is_recursive: bool, - pub(crate) path_to_proof: String, - pub(crate) path_to_vk: String, + pub(crate) proof_path: PathBuf, + pub(crate) vk_path: PathBuf, } impl VerifyCommand { @@ -18,11 +20,11 @@ impl VerifyCommand { command .arg("verify") .arg("-c") - .arg(self.path_to_crs) + .arg(self.crs_path) .arg("-p") - .arg(self.path_to_proof) + .arg(self.proof_path) .arg("-k") - .arg(self.path_to_vk); + .arg(self.vk_path); if self.verbose { command.arg("-v"); @@ -43,22 +45,22 @@ fn verify_command() { use crate::bb::{ProveCommand, WriteVkCommand}; - let path_to_1_mul = "./src/1_mul.bytecode"; - let path_to_1_mul_witness = "./src/witness.tr"; + let bytecode_path = PathBuf::from("./src/1_mul.bytecode"); + let witness_path = PathBuf::from("./src/witness.tr"); let temp_directory = tempdir().expect("could not create a temporary directory"); let temp_directory_path = temp_directory.path(); - let path_to_crs = temp_directory_path.join("crs"); - let path_to_proof = temp_directory_path.join("1_mul").with_extension("proof"); - let path_to_vk = temp_directory_path.join("vk"); + let crs_path = temp_directory_path.join("crs"); + let proof_path = temp_directory_path.join("1_mul").with_extension("proof"); + let vk_path_output = temp_directory_path.join("vk"); let write_vk_command = WriteVkCommand { verbose: true, - path_to_bytecode: path_to_1_mul.to_string(), - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + bytecode_path: bytecode_path.clone(), + crs_path: crs_path.clone(), is_recursive: false, - path_to_vk_output: path_to_vk.to_str().unwrap().to_string(), + vk_path_output: vk_path_output.clone(), }; let vk_written = write_vk_command.run(); @@ -66,20 +68,20 @@ fn verify_command() { let prove_command = ProveCommand { verbose: true, - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + crs_path: crs_path.clone(), is_recursive: false, - path_to_bytecode: path_to_1_mul.to_string(), - path_to_witness: path_to_1_mul_witness.to_string(), - path_to_proof: path_to_proof.to_str().unwrap().to_string(), + bytecode_path, + witness_path, + proof_path: proof_path.clone(), }; prove_command.run().unwrap(); let verify_command = VerifyCommand { verbose: true, - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + crs_path, is_recursive: false, - path_to_proof: path_to_proof.to_str().unwrap().to_string(), - path_to_vk: path_to_vk.to_str().unwrap().to_string(), + proof_path, + vk_path: vk_path_output, }; let verified = verify_command.run(); diff --git a/crates/acvm_backend_barretenberg/src/bb/write_vk.rs b/crates/acvm_backend_barretenberg/src/bb/write_vk.rs index 67706632185..01473ea4ef3 100644 --- a/crates/acvm_backend_barretenberg/src/bb/write_vk.rs +++ b/crates/acvm_backend_barretenberg/src/bb/write_vk.rs @@ -1,13 +1,15 @@ +use std::path::PathBuf; + use super::{assert_binary_exists, get_binary_path, CliShimError}; /// WriteCommand will call the barretenberg binary /// to write a verification key to a file pub(crate) struct WriteVkCommand { pub(crate) verbose: bool, - pub(crate) path_to_crs: String, + pub(crate) crs_path: PathBuf, pub(crate) is_recursive: bool, - pub(crate) path_to_bytecode: String, - pub(crate) path_to_vk_output: String, + pub(crate) bytecode_path: PathBuf, + pub(crate) vk_path_output: PathBuf, } impl WriteVkCommand { @@ -18,11 +20,11 @@ impl WriteVkCommand { command .arg("write_vk") .arg("-c") - .arg(self.path_to_crs) + .arg(self.crs_path) .arg("-b") - .arg(self.path_to_bytecode) + .arg(self.bytecode_path) .arg("-o") - .arg(self.path_to_vk_output); + .arg(self.vk_path_output); if self.verbose { command.arg("-v"); @@ -46,19 +48,19 @@ impl WriteVkCommand { fn write_vk_command() { use tempfile::tempdir; - let path_to_1_mul = "./src/1_mul.bytecode"; + let bytecode_path = PathBuf::from("./src/1_mul.bytecode"); let temp_directory = tempdir().expect("could not create a temporary directory"); let temp_directory_path = temp_directory.path(); - let path_to_crs = temp_directory_path.join("crs"); - let path_to_vk = temp_directory_path.join("vk"); + let crs_path = temp_directory_path.join("crs"); + let vk_path_output = temp_directory_path.join("vk"); let write_vk_command = WriteVkCommand { verbose: true, - path_to_bytecode: path_to_1_mul.to_string(), - path_to_crs: path_to_crs.to_str().unwrap().to_string(), + bytecode_path, + crs_path, is_recursive: false, - path_to_vk_output: path_to_vk.to_str().unwrap().to_string(), + vk_path_output, }; let vk_written = write_vk_command.run(); diff --git a/crates/acvm_backend_barretenberg/src/proof_system.rs b/crates/acvm_backend_barretenberg/src/proof_system.rs index 93f40666f6c..fa6952b947f 100644 --- a/crates/acvm_backend_barretenberg/src/proof_system.rs +++ b/crates/acvm_backend_barretenberg/src/proof_system.rs @@ -18,20 +18,15 @@ impl Backend { pub fn get_exact_circuit_size(&self, circuit: &Circuit) -> Result { let temp_directory = tempdir().expect("could not create a temporary directory"); - let temp_directory = temp_directory.path(); - let temp_dir_path_str = temp_directory.to_str().unwrap(); + let temp_directory = temp_directory.path().to_path_buf(); // Create a temporary file for the circuit - // let circuit_path = temp_directory.join("circuit").with_extension("bytecode"); let serialized_circuit = serialize_circuit(circuit); write_to_file(serialized_circuit.as_bytes(), &circuit_path); - let number_of_gates_needed = GatesCommand { - path_to_crs: temp_dir_path_str.to_string(), - path_to_bytecode: circuit_path.as_os_str().to_str().unwrap().to_string(), - } - .run(); + let number_of_gates_needed = + GatesCommand { crs_path: temp_directory, bytecode_path: circuit_path }.run(); Ok(number_of_gates_needed) } @@ -68,8 +63,7 @@ impl Backend { is_recursive: bool, ) -> Result, BackendError> { let temp_directory = tempdir().expect("could not create a temporary directory"); - let temp_directory = temp_directory.path(); - let temp_dir_path_str = temp_directory.to_str().unwrap(); + let temp_directory = temp_directory.path().to_path_buf(); // Create a temporary file for the witness let serialized_witnesses: Vec = @@ -79,26 +73,25 @@ impl Backend { // Create a temporary file for the circuit // - let circuit_path = temp_directory.join("circuit").with_extension("bytecode"); + let bytecode_path = temp_directory.join("circuit").with_extension("bytecode"); let serialized_circuit = serialize_circuit(circuit); - write_to_file(serialized_circuit.as_bytes(), &circuit_path); + write_to_file(serialized_circuit.as_bytes(), &bytecode_path); let proof_path = temp_directory.join("proof").with_extension("proof"); // Create proof and store it in the specified path ProveCommand { verbose: true, - path_to_crs: temp_dir_path_str.to_string(), + crs_path: temp_directory, is_recursive, - path_to_bytecode: circuit_path.as_os_str().to_str().unwrap().to_string(), - path_to_witness: witness_path.as_os_str().to_str().unwrap().to_string(), - path_to_proof: proof_path.as_os_str().to_str().unwrap().to_string(), + bytecode_path, + witness_path, + proof_path: proof_path.clone(), } .run() .expect("prove command failed"); - let proof_with_public_inputs = - read_bytes_from_file(proof_path.as_os_str().to_str().unwrap()).unwrap(); + let proof_with_public_inputs = read_bytes_from_file(&proof_path).unwrap(); // Barretenberg return the proof prepended with the public inputs. // @@ -120,8 +113,7 @@ impl Backend { is_recursive: bool, ) -> Result { let temp_directory = tempdir().expect("could not create a temporary directory"); - let temp_directory = temp_directory.path(); - let temp_dir_path = temp_directory.to_str().unwrap(); + let temp_directory = temp_directory.path().to_path_buf(); // Unlike when proving, we omit any unassigned witnesses. // Witness values should be ordered by their index but we skip over any indices without an assignment. @@ -140,18 +132,18 @@ impl Backend { write_to_file(&proof_with_public_inputs, &proof_path); // Create a temporary file for the circuit - let circuit_path = temp_directory.join("circuit").with_extension("bytecode"); + let bytecode_path = temp_directory.join("circuit").with_extension("bytecode"); let serialized_circuit = serialize_circuit(circuit); - write_to_file(serialized_circuit.as_bytes(), &circuit_path); + write_to_file(serialized_circuit.as_bytes(), &bytecode_path); // Create the verification key and write it to the specified path let vk_path = temp_directory.join("vk"); WriteVkCommand { verbose: false, - path_to_crs: temp_dir_path.to_string(), + crs_path: temp_directory.clone(), is_recursive, - path_to_bytecode: circuit_path.as_os_str().to_str().unwrap().to_string(), - path_to_vk_output: vk_path.as_os_str().to_str().unwrap().to_string(), + bytecode_path, + vk_path_output: vk_path.clone(), } .run() .expect("write vk command failed"); @@ -159,10 +151,10 @@ impl Backend { // Verify the proof Ok(VerifyCommand { verbose: false, - path_to_crs: temp_dir_path.to_string(), + crs_path: temp_directory, is_recursive, - path_to_proof: proof_path.as_os_str().to_str().unwrap().to_string(), - path_to_vk: vk_path.as_os_str().to_str().unwrap().to_string(), + proof_path, + vk_path, } .run()) } @@ -182,7 +174,7 @@ pub(super) fn write_to_file(bytes: &[u8], path: &Path) -> String { } } -pub(super) fn read_bytes_from_file(path: &str) -> std::io::Result> { +pub(super) fn read_bytes_from_file(path: &Path) -> std::io::Result> { // Open the file for reading. let mut file = File::open(path)?; diff --git a/crates/acvm_backend_barretenberg/src/smart_contract.rs b/crates/acvm_backend_barretenberg/src/smart_contract.rs index 29633ef6bfc..cde89647cdd 100644 --- a/crates/acvm_backend_barretenberg/src/smart_contract.rs +++ b/crates/acvm_backend_barretenberg/src/smart_contract.rs @@ -13,37 +13,36 @@ const ULTRA_VERIFIER_CONTRACT: &str = include_str!("contract.sol"); impl Backend { pub fn eth_contract(&self, circuit: &Circuit) -> Result { let temp_directory = tempdir().expect("could not create a temporary directory"); - let temp_directory_path = temp_directory.path(); - let temp_dir_path = temp_directory_path.to_str().unwrap(); + let temp_directory_path = temp_directory.path().to_path_buf(); // Create a temporary file for the circuit - let circuit_path = temp_directory_path.join("circuit").with_extension("bytecode"); + let bytecode_path = temp_directory_path.join("circuit").with_extension("bytecode"); let serialized_circuit = serialize_circuit(circuit); - write_to_file(serialized_circuit.as_bytes(), &circuit_path); + write_to_file(serialized_circuit.as_bytes(), &bytecode_path); // Create the verification key and write it to the specified path - let vk_path = temp_directory_path.join("vk").to_str().unwrap().to_string(); + let vk_path = temp_directory_path.join("vk"); WriteVkCommand { verbose: false, - path_to_crs: temp_dir_path.to_string(), + crs_path: temp_directory_path.clone(), is_recursive: false, - path_to_bytecode: circuit_path.as_os_str().to_str().unwrap().to_string(), - path_to_vk_output: vk_path.clone(), + bytecode_path, + vk_path_output: vk_path.clone(), } .run() .expect("write vk command failed"); - let path_to_contract = temp_directory_path.join("contract").to_str().unwrap().to_string(); + let contract_path = temp_directory_path.join("contract"); ContractCommand { verbose: false, - path_to_crs: temp_dir_path.to_string(), - path_to_vk: vk_path, - path_to_contract: path_to_contract.clone(), + crs_path: temp_directory_path, + vk_path, + contract_path: contract_path.clone(), } .run() .expect("contract command failed"); - let verification_key_library_bytes = read_bytes_from_file(&path_to_contract).unwrap(); + let verification_key_library_bytes = read_bytes_from_file(&contract_path).unwrap(); let verification_key_library = String::from_utf8(verification_key_library_bytes).unwrap(); drop(temp_directory);