Skip to content

Commit

Permalink
chore: use faster hash function for checking keys
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Apr 4, 2023
1 parent 3891faf commit a007fe4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
13 changes: 6 additions & 7 deletions crates/nargo_cli/src/cli/fs/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
constants::{ACIR_CHECKSUM, PK_EXT, VK_EXT},
errors::CliError,
};
use acvm::{acir::circuit::Circuit, hash_constraint_system};
use acvm::{acir::circuit::Circuit, checksum_constraint_system};
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_constraint_system(circuit).to_be_bytes();

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 @@ -62,7 +61,7 @@ pub(crate) fn fetch_pk_and_vk<P: AsRef<Path>>(
#[cfg(test)]
mod tests {
use super::fetch_pk_and_vk;
use crate::cli::fs::{keys::save_key_to_dir, program::save_acir_hash_to_dir};
use crate::cli::fs::{keys::save_key_to_dir, program::save_acir_checksum_to_dir};
use acvm::acir::circuit::Circuit;
use tempdir::TempDir;

Expand All @@ -78,7 +77,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(&circuit, circuit_name, &circuit_build_path);
save_acir_checksum_to_dir(&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
8 changes: 4 additions & 4 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,14 +34,14 @@ fn save_build_artifact_to_file<P: AsRef<Path>, T: ?Sized + serde::Serialize>(
circuit_path
}

pub(crate) fn save_acir_hash_to_dir<P: AsRef<Path>>(
pub(crate) fn save_acir_checksum_to_dir<P: AsRef<Path>>(
circuit: &Circuit,
hash_name: &str,
hash_dir: P,
) -> PathBuf {
let acir_hash = hash_constraint_system(circuit);
let acir_checksum = checksum_constraint_system(circuit);
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.to_be_bytes()).as_bytes(), &hash_path);

hash_path
}
Expand Down
6 changes: 3 additions & 3 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::{read_program_from_file, save_acir_hash_to_dir},
program::{read_program_from_file, save_acir_checksum_to_dir},
};
use super::NargoConfig;

Expand Down Expand Up @@ -38,8 +38,8 @@ pub(crate) fn preprocess_with_path<P: AsRef<Path>>(
let (proving_key, verification_key) = backend.preprocess(circuit);

// 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(circuit, key_name, &preprocess_dir);
// If the checksums don't match then the circuit has been updated and keys are stale.
save_acir_checksum_to_dir(circuit, key_name, &preprocess_dir);

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

0 comments on commit a007fe4

Please sign in to comment.