From aa16786dc7f463eb90fefcc098908b124dc3f15b Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 7 Sep 2023 18:33:48 +0100 Subject: [PATCH] chore: read proofs from backend on stdout --- .../src/cli/prove.rs | 15 +++++-------- .../src/cli/verify.rs | 6 +++-- .../src/proof_system.rs | 22 ++----------------- .../mock_backend/src/prove_cmd.rs | 3 ++- 4 files changed, 14 insertions(+), 32 deletions(-) diff --git a/crates/acvm_backend_barretenberg/src/cli/prove.rs b/crates/acvm_backend_barretenberg/src/cli/prove.rs index 602cb0f88a8..82078a38282 100644 --- a/crates/acvm_backend_barretenberg/src/cli/prove.rs +++ b/crates/acvm_backend_barretenberg/src/cli/prove.rs @@ -14,11 +14,10 @@ pub(crate) struct ProveCommand { pub(crate) is_recursive: bool, pub(crate) bytecode_path: PathBuf, pub(crate) witness_path: PathBuf, - pub(crate) proof_path: PathBuf, } impl ProveCommand { - pub(crate) fn run(self, binary_path: &Path) -> Result<(), BackendError> { + pub(crate) fn run(self, binary_path: &Path) -> Result, BackendError> { let mut command = std::process::Command::new(binary_path); command @@ -30,7 +29,7 @@ impl ProveCommand { .arg("-w") .arg(self.witness_path) .arg("-o") - .arg(self.proof_path); + .arg("-"); if self.is_recursive { command.arg("-r"); @@ -38,7 +37,7 @@ impl ProveCommand { let output = command.output().expect("Failed to execute command"); if output.status.success() { - Ok(()) + Ok(output.stdout) } else { Err(BackendError(String::from_utf8(output.stderr).unwrap())) } @@ -56,16 +55,14 @@ fn prove_command() { let temp_directory_path = temp_directory.path(); let bytecode_path = temp_directory_path.join("acir.gz"); let witness_path = temp_directory_path.join("witness.tr"); - let proof_path = temp_directory_path.join("1_mul.proof"); std::fs::File::create(&bytecode_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, is_recursive: false, proof_path }; + let prove_command = ProveCommand { crs_path, bytecode_path, witness_path, is_recursive: false }; - let proof_created = prove_command.run(&backend.binary_path()); - assert!(proof_created.is_ok()); + let proof = prove_command.run(&backend.binary_path()).unwrap(); + assert_eq!(proof, "proof".as_bytes()); drop(temp_directory); } diff --git a/crates/acvm_backend_barretenberg/src/cli/verify.rs b/crates/acvm_backend_barretenberg/src/cli/verify.rs index 9b497bf7f8b..2b04e5048bb 100644 --- a/crates/acvm_backend_barretenberg/src/cli/verify.rs +++ b/crates/acvm_backend_barretenberg/src/cli/verify.rs @@ -39,6 +39,7 @@ fn verify_command() { use tempfile::tempdir; use super::{ProveCommand, WriteVkCommand}; + use crate::proof_system::write_to_file; let backend = crate::get_mock_backend(); @@ -69,9 +70,10 @@ fn verify_command() { is_recursive: false, bytecode_path, witness_path, - proof_path: proof_path.clone(), }; - prove_command.run(&backend.binary_path()).unwrap(); + let proof = prove_command.run(&backend.binary_path()).unwrap(); + + write_to_file(&proof, &proof_path); let verify_command = VerifyCommand { crs_path, is_recursive: false, proof_path, vk_path: vk_path_output }; diff --git a/crates/acvm_backend_barretenberg/src/proof_system.rs b/crates/acvm_backend_barretenberg/src/proof_system.rs index a53513fa49e..ca5a6818343 100644 --- a/crates/acvm_backend_barretenberg/src/proof_system.rs +++ b/crates/acvm_backend_barretenberg/src/proof_system.rs @@ -1,5 +1,5 @@ use std::fs::File; -use std::io::{Read, Write}; +use std::io::Write; use std::path::Path; use acvm::acir::circuit::Opcode; @@ -54,21 +54,16 @@ impl Backend { let serialized_circuit = serialize_circuit(circuit); write_to_file(&serialized_circuit, &bytecode_path); - let proof_path = temp_directory.join("proof").with_extension("proof"); - let binary_path = assert_binary_exists(self); // Create proof and store it in the specified path - ProveCommand { + let proof_with_public_inputs = ProveCommand { crs_path: self.crs_directory(), is_recursive, bytecode_path, witness_path, - proof_path: proof_path.clone(), } .run(&binary_path)?; - let proof_with_public_inputs = read_bytes_from_file(&proof_path).unwrap(); - // Barretenberg return the proof prepended with the public inputs. // // This is not how the API expects the proof to be formatted, @@ -147,19 +142,6 @@ pub(super) fn write_to_file(bytes: &[u8], path: &Path) -> String { } } -pub(super) fn read_bytes_from_file(path: &Path) -> std::io::Result> { - // Open the file for reading. - let mut file = File::open(path)?; - - // Create a buffer to store the bytes. - let mut buffer = Vec::new(); - - // Read bytes from the file. - file.read_to_end(&mut buffer)?; - - Ok(buffer) -} - /// 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 diff --git a/crates/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs b/crates/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs index bf53b9e694c..3967778d4e8 100644 --- a/crates/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs +++ b/crates/acvm_backend_barretenberg/test-binaries/mock_backend/src/prove_cmd.rs @@ -1,4 +1,5 @@ use clap::Args; +use std::io::Write; use std::path::PathBuf; #[derive(Debug, Clone, Args)] @@ -20,5 +21,5 @@ pub(crate) fn run(args: ProveCommand) { assert!(args.bytecode_path.is_file(), "Could not find bytecode file at provided path"); assert!(args.witness_path.is_file(), "Could not find witness file at provided path"); - std::fs::write(args.proof_path, "proof").unwrap(); + std::io::stdout().write_all(b"proof").unwrap(); }