Skip to content

Commit

Permalink
chore: read proofs from backend on stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Sep 7, 2023
1 parent 3504759 commit aa16786
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
15 changes: 6 additions & 9 deletions crates/acvm_backend_barretenberg/src/cli/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>, BackendError> {
let mut command = std::process::Command::new(binary_path);

command
Expand All @@ -30,15 +29,15 @@ impl ProveCommand {
.arg("-w")
.arg(self.witness_path)
.arg("-o")
.arg(self.proof_path);
.arg("-");

if self.is_recursive {
command.arg("-r");
}

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()))
}
Expand All @@ -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);
}
6 changes: 4 additions & 2 deletions crates/acvm_backend_barretenberg/src/cli/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 };
Expand Down
22 changes: 2 additions & 20 deletions crates/acvm_backend_barretenberg/src/proof_system.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Vec<u8>> {
// 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<u8> {
// Barretenberg prepends the public inputs onto the proof so we need to remove
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Args;
use std::io::Write;
use std::path::PathBuf;

#[derive(Debug, Clone, Args)]
Expand All @@ -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();
}

0 comments on commit aa16786

Please sign in to comment.