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)!: use faster hash function for checking preprocessed keys #1094

Merged
merged 3 commits into from
Apr 5, 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
15 changes: 7 additions & 8 deletions crates/nargo_cli/src/cli/fs/keys.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{create_named_dir, load_hex_data, write_to_file};
use super::{create_named_dir, load_hex_data, program::checksum_acir, write_to_file};
use crate::{
constants::{ACIR_CHECKSUM, PK_EXT, VK_EXT},
errors::CliError,
};
use acvm::{acir::circuit::Circuit, hash_constraint_system};
use acvm::acir::circuit::Circuit;
use std::path::{Path, PathBuf};

pub(crate) fn save_key_to_dir<P: AsRef<Path>>(
Expand All @@ -30,11 +30,10 @@ pub(crate) fn fetch_pk_and_vk<P: AsRef<Path>>(
) -> Result<(Vec<u8>, Vec<u8>), CliError> {
let acir_hash_path = circuit_build_path.as_ref().with_extension(ACIR_CHECKSUM);

let expected_acir_hash = load_hex_data(acir_hash_path.clone())?;
let expected_acir_checksum = load_hex_data(acir_hash_path.clone())?;
let new_acir_checksum = checksum_acir(circuit);

let new_acir_hash = hash_constraint_system(circuit);

if new_acir_hash[..] != expected_acir_hash {
if new_acir_checksum[..] != expected_acir_checksum {
return Err(CliError::MismatchedAcir(acir_hash_path));
}

Expand Down Expand Up @@ -64,7 +63,7 @@ mod tests {
use super::fetch_pk_and_vk;
use crate::cli::fs::{
keys::save_key_to_dir,
program::{hash_acir, save_acir_hash_to_dir},
program::{checksum_acir, save_acir_checksum_to_dir},
};
use acvm::acir::circuit::Circuit;
use tempdir::TempDir;
Expand All @@ -81,7 +80,7 @@ mod tests {
save_key_to_dir(&pk, circuit_name, &circuit_build_path, true).unwrap();
save_key_to_dir(&vk, circuit_name, &circuit_build_path, false).unwrap();

save_acir_hash_to_dir(hash_acir(&circuit), circuit_name, &circuit_build_path);
save_acir_checksum_to_dir(checksum_acir(&circuit), circuit_name, &circuit_build_path);
circuit_build_path.push(circuit_name);

let loaded_keys = fetch_pk_and_vk(&circuit, circuit_build_path, true, true).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions crates/nargo_cli/src/cli/fs/program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};

use acvm::{acir::circuit::Circuit, hash_constraint_system};
use acvm::{acir::circuit::Circuit, checksum_constraint_system};
use noirc_driver::{CompiledContract, CompiledProgram};

use crate::{constants::ACIR_CHECKSUM, errors::CliError};
Expand Down Expand Up @@ -34,16 +34,16 @@ fn save_build_artifact_to_file<P: AsRef<Path>, T: ?Sized + serde::Serialize>(
circuit_path
}

pub(crate) fn hash_acir(circuit: &Circuit) -> [u8; 32] {
hash_constraint_system(circuit)
pub(crate) fn checksum_acir(circuit: &Circuit) -> [u8; 4] {
checksum_constraint_system(circuit).to_be_bytes()
}
pub(crate) fn save_acir_hash_to_dir<P: AsRef<Path>>(
acir_hash: [u8; 32],
pub(crate) fn save_acir_checksum_to_dir<P: AsRef<Path>>(
acir_checksum: [u8; 4],
hash_name: &str,
hash_dir: P,
) -> PathBuf {
let hash_path = hash_dir.as_ref().join(hash_name).with_extension(ACIR_CHECKSUM);
write_to_file(hex::encode(acir_hash).as_bytes(), &hash_path);
write_to_file(hex::encode(acir_checksum).as_bytes(), &hash_path);

hash_path
}
Expand Down
10 changes: 5 additions & 5 deletions crates/nargo_cli/src/cli/preprocess_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{constants::TARGET_DIR, errors::CliError};

use super::fs::{
keys::save_key_to_dir,
program::{hash_acir, read_program_from_file, save_acir_hash_to_dir},
program::{checksum_acir, read_program_from_file, save_acir_checksum_to_dir},
};
use super::NargoConfig;

Expand Down Expand Up @@ -38,16 +38,16 @@ pub(crate) fn run(args: PreprocessCommand, config: NargoConfig) -> Result<(), Cl
pub(crate) struct PreprocessedData {
pub(crate) proving_key: Vec<u8>,
pub(crate) verification_key: Vec<u8>,
pub(crate) program_hash: [u8; 32],
pub(crate) program_checksum: [u8; 4],
}

impl From<&Circuit> for PreprocessedData {
fn from(circuit: &Circuit) -> Self {
let backend = crate::backends::ConcreteBackend;
let (proving_key, verification_key) = backend.preprocess(circuit);
let program_hash = hash_acir(circuit);
let program_checksum = checksum_acir(circuit);

PreprocessedData { proving_key, verification_key, program_hash }
PreprocessedData { proving_key, verification_key, program_checksum }
}
}

Expand All @@ -58,7 +58,7 @@ pub(crate) fn save_preprocess_data<P: AsRef<Path>>(
) -> Result<(PathBuf, PathBuf), CliError> {
// Save a checksum of the circuit to compare against during proving and verification.
// If hash doesn't match then the circuit has been updated and keys are stale.
save_acir_hash_to_dir(data.program_hash, key_name, &preprocess_dir);
save_acir_checksum_to_dir(data.program_checksum, key_name, &preprocess_dir);

let pk_path = save_key_to_dir(&data.proving_key, key_name, &preprocess_dir, true)?;
let vk_path = save_key_to_dir(&data.verification_key, key_name, preprocess_dir, false)?;
Expand Down