Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(nargo)!: Make proving and verification keys optional #1880

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/nargo/src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ pub struct PreprocessedContractFunction {
)]
pub bytecode: Circuit,

pub proving_key: Vec<u8>,
pub verification_key: Vec<u8>,
pub proving_key: Option<Vec<u8>>,
pub verification_key: Option<Vec<u8>>,
}
4 changes: 2 additions & 2 deletions crates/nargo/src/artifacts/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ pub struct PreprocessedProgram {
)]
pub bytecode: Circuit,

pub proving_key: Vec<u8>,
pub verification_key: Vec<u8>,
pub proving_key: Option<Vec<u8>>,
pub verification_key: Option<Vec<u8>>,
}
21 changes: 17 additions & 4 deletions crates/nargo/src/ops/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";

pub fn preprocess_program<B: ProofSystemCompiler>(
backend: &B,
include_keys: bool,
common_reference_string: &[u8],
compiled_program: CompiledProgram,
) -> Result<PreprocessedProgram, B::Error> {
// TODO: currently `compiled_program`'s bytecode is already optimized for the backend.
// In future we'll need to apply those optimizations here.
let optimized_bytecode = compiled_program.circuit;
let (proving_key, verification_key) =
backend.preprocess(common_reference_string, &optimized_bytecode)?;

let (proving_key, verification_key) = if include_keys {
let (proving_key, verification_key) =
backend.preprocess(common_reference_string, &optimized_bytecode)?;
(Some(proving_key), Some(verification_key))
} else {
(None, None)
};

Ok(PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
Expand All @@ -28,14 +35,20 @@ pub fn preprocess_program<B: ProofSystemCompiler>(

pub fn preprocess_contract_function<B: ProofSystemCompiler>(
backend: &B,
include_keys: bool,
common_reference_string: &[u8],
func: ContractFunction,
) -> Result<PreprocessedContractFunction, B::Error> {
// TODO: currently `func`'s bytecode is already optimized for the backend.
// In future we'll need to apply those optimizations here.
let optimized_bytecode = func.bytecode;
let (proving_key, verification_key) =
backend.preprocess(common_reference_string, &optimized_bytecode)?;
let (proving_key, verification_key) = if include_keys {
let (proving_key, verification_key) =
backend.preprocess(common_reference_string, &optimized_bytecode)?;
(Some(proving_key), Some(verification_key))
} else {
(None, None)
};

Ok(PreprocessedContractFunction {
name: func.name,
Expand Down
7 changes: 5 additions & 2 deletions crates/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ pub(crate) fn run<B: Backend>(
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
let program = preprocess_program(backend, &common_reference_string, program)
let program = preprocess_program(backend, true, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
(common_reference_string, program)
}
};

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

write_cached_common_reference_string(&common_reference_string);
Expand Down
18 changes: 14 additions & 4 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub(crate) struct CompileCommand {
/// The name of the ACIR file
circuit_name: String,

/// Include Proving and Verification keys in the build artifacts.
#[arg(long)]
include_keys: bool,

/// Compile each contract function used within the program
#[arg(short, long)]
contracts: bool,
Expand Down Expand Up @@ -71,8 +75,13 @@ pub(crate) fn run<B: Backend>(
)
.map_err(CliError::CommonReferenceStringError)?;

preprocess_contract_function(backend, &common_reference_string, func)
.map_err(CliError::ProofSystemCompilerError)
preprocess_contract_function(
backend,
args.include_keys,
&common_reference_string,
func,
)
.map_err(CliError::ProofSystemCompilerError)
})?;

Ok(PreprocessedContract {
Expand All @@ -94,8 +103,9 @@ pub(crate) fn run<B: Backend>(
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;

let preprocessed_program = preprocess_program(backend, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
let preprocessed_program =
preprocess_program(backend, args.include_keys, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
save_program_to_file(&preprocessed_program, &args.circuit_name, circuit_dir);
}

Expand Down
7 changes: 6 additions & 1 deletion crates/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) fn prove_with_path<B: Backend, P: AsRef<Path>>(
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
let program = preprocess_program(backend, &common_reference_string, program)
let program = preprocess_program(backend, true, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
(common_reference_string, program)
}
Expand Down Expand Up @@ -137,12 +137,17 @@ pub(crate) fn prove_with_path<B: Backend, P: AsRef<Path>>(
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
4 changes: 3 additions & 1 deletion crates/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn verify_with_path<B: Backend, P: AsRef<Path>>(
let common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
let program = preprocess_program(backend, &common_reference_string, program)
let program = preprocess_program(backend, true, &common_reference_string, program)
.map_err(CliError::ProofSystemCompilerError)?;
(common_reference_string, program)
}
Expand All @@ -105,6 +105,8 @@ fn verify_with_path<B: Backend, P: AsRef<Path>>(
let public_inputs = public_abi.encode(&public_inputs_map, return_value)?;
let proof = load_hex_data(&proof_path)?;

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