Skip to content

Commit

Permalink
chore!: Remove keys from preprocessed artifacts (#2283)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Aug 30, 2023
1 parent 91efe44 commit 4554287
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 170 deletions.
2 changes: 1 addition & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ acvm.workspace = true
fm.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
noirc_errors.workspace = true
noirc_frontend.workspace = true
noirc_printable_type.workspace = true
iter-extended.workspace = true
serde.workspace = true
thiserror.workspace = true
noirc_errors.workspace = true
base64.workspace = true
3 changes: 0 additions & 3 deletions crates/nargo/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,4 @@ pub struct PreprocessedContractFunction {
deserialize_with = "super::deserialize_circuit"
)]
pub bytecode: Circuit,

pub proving_key: Option<Vec<u8>>,
pub verification_key: Option<Vec<u8>>,
}
2 changes: 1 addition & 1 deletion crates/nargo/src/artifacts/debug.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use noirc_errors::debug_info::DebugInfo;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
path::PathBuf,
};

use fm::FileId;
use noirc_errors::debug_info::DebugInfo;
use noirc_frontend::hir::Context;

/// For a given file, we store the source code and the path to the file
Expand Down
3 changes: 0 additions & 3 deletions crates/nargo/src/artifacts/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ pub struct PreprocessedProgram {
deserialize_with = "super::deserialize_circuit"
)]
pub bytecode: Circuit,

pub proving_key: Option<Vec<u8>>,
pub verification_key: Option<Vec<u8>>,
}
2 changes: 0 additions & 2 deletions crates/nargo/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
pub use self::codegen_verifier::codegen_verifier;
pub use self::execute::execute_circuit;
pub use self::preprocess::{preprocess_contract_function, preprocess_program};
pub use self::prove::prove_execution;
pub use self::test::{run_test, TestStatus};
pub use self::verify::verify_proof;

mod codegen_verifier;
mod execute;
mod foreign_calls;
mod preprocess;
mod prove;
mod test;
mod verify;
70 changes: 0 additions & 70 deletions crates/nargo/src/ops/preprocess.rs

This file was deleted.

45 changes: 25 additions & 20 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ use super::{
use crate::errors::CliError;
use acvm::Backend;
use clap::Args;
use nargo::{
ops::{codegen_verifier, preprocess_program},
package::Package,
};
use nargo::artifacts::program::PreprocessedProgram;
use nargo::{ops::codegen_verifier, package::Package};
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection};
use noirc_driver::CompileOptions;
use noirc_frontend::graph::CrateName;

// TODO(#1388): pull this from backend.
const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";

/// Generates a Solidity verifier smart contract for the program
#[derive(Debug, Clone, Args)]
pub(crate) struct CodegenVerifierCommand {
Expand Down Expand Up @@ -77,26 +78,30 @@ fn smart_contract_for_package<B: Backend>(
circuit_build_path: PathBuf,
compile_options: &CompileOptions,
) -> Result<String, CliError<B>> {
let common_reference_string = read_cached_common_reference_string();
let (common_reference_string, preprocessed_program) = if circuit_build_path.exists() {
let program = read_program_from_file(circuit_build_path)?;
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.bytecode)
.map_err(CliError::CommonReferenceStringError)?;
(common_reference_string, program)
let preprocessed_program = if circuit_build_path.exists() {
read_program_from_file(circuit_build_path)?
} else {
let (_, program) = compile_package(backend, package, compile_options)?;
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
let (program, _) = preprocess_program(backend, true, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
(common_reference_string, program)

PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
bytecode: program.circuit,
}
};

let verification_key = preprocessed_program
.verification_key
.expect("Verification key should exist as `true` is passed to `preprocess_program`");
let common_reference_string = read_cached_common_reference_string();
let common_reference_string = update_common_reference_string(
backend,
&common_reference_string,
&preprocessed_program.bytecode,
)
.map_err(CliError::CommonReferenceStringError)?;

let (_, verification_key) = backend
.preprocess(&common_reference_string, &preprocessed_program.bytecode)
.map_err(CliError::ProofSystemCompilerError)?;

let smart_contract_string = codegen_verifier(
backend,
&common_reference_string,
Expand Down
33 changes: 20 additions & 13 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use acvm::{acir::circuit::Circuit, compiler::AcirTransformationMap, Backend};
use iter_extended::try_vecmap;
use nargo::artifacts::contract::PreprocessedContractFunction;
use nargo::artifacts::debug::DebugArtifact;
use nargo::artifacts::program::PreprocessedProgram;
use nargo::package::Package;
use nargo::prepare_package;
use nargo::{artifacts::contract::PreprocessedContract, NargoError};
Expand All @@ -15,8 +17,6 @@ use noirc_frontend::hir::Context;

use clap::Args;

use nargo::ops::{preprocess_contract_function, preprocess_program};

use crate::errors::{CliError, CompileError};

use super::fs::program::save_debug_artifact_to_file;
Expand Down Expand Up @@ -94,13 +94,17 @@ pub(crate) fn run<B: Backend>(
)
.map_err(CliError::CommonReferenceStringError)?;

preprocess_contract_function(
backend,
args.include_keys,
&common_reference_string,
func,
)
.map_err(CliError::ProofSystemCompilerError)
Ok::<_, CliError<B>>((
PreprocessedContractFunction {
name: func.name,
function_type: func.function_type,
is_internal: func.is_internal,
abi: func.abi,

bytecode: func.bytecode,
},
func.debug,
))
})?;

let (preprocessed_contract_functions, debug_infos): (Vec<_>, Vec<_>) =
Expand Down Expand Up @@ -138,13 +142,16 @@ pub(crate) fn run<B: Backend>(
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;

let (preprocessed_program, debug_info) =
preprocess_program(backend, args.include_keys, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
let preprocessed_program = PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
bytecode: program.circuit,
};

save_program_to_file(&preprocessed_program, &package.name, &circuit_dir);

if args.output_debug {
let debug_artifact = DebugArtifact::new(vec![debug_info], &context);
let debug_artifact = DebugArtifact::new(vec![program.debug], &context);
let circuit_name: String = (&package.name).into();
save_debug_artifact_to_file(&debug_artifact, &circuit_name, &circuit_dir);
}
Expand Down
54 changes: 27 additions & 27 deletions crates/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ use acvm::Backend;
use clap::Args;
use nargo::artifacts::program::PreprocessedProgram;
use nargo::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};
use nargo::ops::{preprocess_program, prove_execution, verify_proof};
use nargo::ops::{prove_execution, verify_proof};
use nargo::package::Package;
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection};
use noirc_abi::input_parser::Format;
use noirc_driver::CompileOptions;
use noirc_frontend::graph::CrateName;

use super::compile_cmd::compile_package;
use super::fs::common_reference_string::update_common_reference_string;
use super::fs::{
common_reference_string::{
read_cached_common_reference_string, update_common_reference_string,
write_cached_common_reference_string,
},
common_reference_string::read_cached_common_reference_string,
inputs::{read_inputs_from_file, write_inputs_to_file},
program::read_program_from_file,
proof::save_proof_to_dir,
};
use super::NargoConfig;
use crate::{cli::execute_cmd::execute_program, errors::CliError};

// TODO(#1388): pull this from backend.
const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";

/// Create proof for this program. The proof is returned as a hex encoded string.
#[derive(Debug, Clone, Args)]
pub(crate) struct ProveCommand {
Expand Down Expand Up @@ -92,29 +93,33 @@ pub(crate) fn prove_package<B: Backend>(
check_proof: bool,
compile_options: &CompileOptions,
) -> Result<(), CliError<B>> {
let common_reference_string = read_cached_common_reference_string();

let (common_reference_string, preprocessed_program, debug_data) = if circuit_build_path.exists()
{
let (preprocessed_program, debug_data) = if circuit_build_path.exists() {
let program = read_program_from_file(circuit_build_path)?;
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.bytecode)
.map_err(CliError::CommonReferenceStringError)?;
(common_reference_string, program, None)

(program, None)
} else {
let (context, program) = compile_package(backend, package, compile_options)?;
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
let (program, debug) = preprocess_program(backend, true, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
(common_reference_string, program, Some((debug, context)))
let preprocessed_program = PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
bytecode: program.circuit,
};
(preprocessed_program, Some((program.debug, context)))
};

write_cached_common_reference_string(&common_reference_string);
let common_reference_string = read_cached_common_reference_string();
let common_reference_string = update_common_reference_string(
backend,
&common_reference_string,
&preprocessed_program.bytecode,
)
.map_err(CliError::CommonReferenceStringError)?;

let (proving_key, verification_key) = backend
.preprocess(&common_reference_string, &preprocessed_program.bytecode)
.map_err(CliError::ProofSystemCompilerError)?;

let PreprocessedProgram { abi, bytecode, proving_key, verification_key, .. } =
preprocessed_program;
let PreprocessedProgram { abi, bytecode, .. } = preprocessed_program;

// Parse the initial witness values from Prover.toml
let (inputs_map, _) =
Expand All @@ -135,17 +140,12 @@ pub(crate) fn prove_package<B: Backend>(
Format::Toml,
)?;

let proving_key =
proving_key.expect("Proving key should exist as `true` is passed to `preprocess_program`");

let proof =
prove_execution(backend, &common_reference_string, &bytecode, solved_witness, &proving_key)
.map_err(CliError::ProofSystemCompilerError)?;

if check_proof {
let public_inputs = public_abi.encode(&public_inputs, return_value)?;
let verification_key = verification_key
.expect("Verification key should exist as `true` is passed to `preprocess_program`");
let valid_proof = verify_proof(
backend,
&common_reference_string,
Expand Down
Loading

0 comments on commit 4554287

Please sign in to comment.