Skip to content

Commit

Permalink
fix(nargo): correct inconsistent file extension for ACIR hashes (#994)
Browse files Browse the repository at this point in the history
* Made `save_and_preprocess_program` public for the crate

* Rename extension (`json.sha256`->`json.checksum`)

Idk which you prefer though

* Add integration test for the "cached use case flow"

By "cached use case flow" I mean the user proving and verifying over a circuit that was compiled using `nargo compile <circuit_name>`

* Add test helper function

* cargo fmt

* Both integration tests cannot run in parallel

* Add `ACIR_CHECKSUM` extension constant

* Test that fetching pk & vk works as expected

* Rollback integration test

* Rollback needless logic

* cargo fmt

* Extend test

Now it also checks that the keys it returns are the ones that we saved

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>

* Fix test & format

---------

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
ilitteri and TomAFrench authored Mar 16, 2023
1 parent 65b7108 commit 23c22d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
40 changes: 32 additions & 8 deletions crates/nargo/src/cli/fs/keys.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::path::{Path, PathBuf};

use acvm::{acir::circuit::Circuit, hash_constraint_system};

use super::{create_named_dir, load_hex_data, write_to_file};
use crate::{
constants::{PK_EXT, VK_EXT},
constants::{ACIR_CHECKSUM, PK_EXT, VK_EXT},
errors::CliError,
};

use super::{create_named_dir, load_hex_data, write_to_file};
use acvm::{acir::circuit::Circuit, hash_constraint_system};
use std::path::{Path, PathBuf};

pub(crate) fn save_key_to_dir<P: AsRef<Path>>(
key: Vec<u8>,
Expand All @@ -32,7 +29,7 @@ pub(crate) fn fetch_pk_and_vk<P: AsRef<Path>>(
check_proof: bool,
) -> Result<(Vec<u8>, Vec<u8>), CliError> {
let mut acir_hash_path = PathBuf::from(circuit_build_path.as_ref());
acir_hash_path.set_extension("json.checksum");
acir_hash_path.set_extension(ACIR_CHECKSUM);

let expected_acir_hash = load_hex_data(acir_hash_path.clone())?;

Expand Down Expand Up @@ -66,3 +63,30 @@ pub(crate) fn fetch_pk_and_vk<P: AsRef<Path>>(

Ok((proving_key, verification_key))
}

#[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 acvm::acir::circuit::Circuit;
use tempdir::TempDir;

#[test]
fn fetching_pk_and_vk_loads_expected_keys() {
let circuit = Circuit::default();
let circuit_name = "my_circuit";
let mut circuit_build_path = TempDir::new("temp_circuit_hash_dir").unwrap().into_path();

// These values are not meaningful, we just need distinct values.
let pk: Vec<u8> = vec![0];
let vk: Vec<u8> = vec![1, 2];
save_key_to_dir(pk.clone(), circuit_name, &circuit_build_path, true).unwrap();
save_key_to_dir(vk.clone(), circuit_name, &circuit_build_path, false).unwrap();

save_acir_hash_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();
assert_eq!(loaded_keys, (pk, vk));
}
}
4 changes: 2 additions & 2 deletions crates/nargo/src/cli/fs/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use acvm::{acir::circuit::Circuit, hash_constraint_system};
use noirc_driver::CompiledProgram;

use crate::errors::CliError;
use crate::{constants::ACIR_CHECKSUM, errors::CliError};

use super::{create_named_dir, write_to_file};

Expand All @@ -27,7 +27,7 @@ pub(crate) fn save_acir_hash_to_dir<P: AsRef<Path>>(
hash_dir: P,
) -> PathBuf {
let acir_hash = hash_constraint_system(circuit);
let hash_path = hash_dir.as_ref().join(hash_name).with_extension("json.sha256");
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);

hash_path
Expand Down
2 changes: 2 additions & 0 deletions crates/nargo/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ pub(crate) const WITNESS_EXT: &str = "tr";
pub(crate) const PK_EXT: &str = "pk";
/// The extension for verification keys.
pub(crate) const VK_EXT: &str = "vk";
/// The extension for ACIR hash files.
pub(crate) const ACIR_CHECKSUM: &str = "json.checksum";

0 comments on commit 23c22d7

Please sign in to comment.