-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nargo)!: save program ABI alongside ACIR (#922)
* feat!(nargo): save program ABI alongside ACIR * chore: clippy fix * fix: update extension for reading program hash * chore: add some error handling to `read_program_from_file` * chore: replace `.sha256` extension with `.checksum`
- Loading branch information
1 parent
c730329
commit ddaf305
Showing
12 changed files
with
110 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::path::{Path, PathBuf}; | ||
|
||
use acvm::hash_constraint_system; | ||
use noirc_driver::CompiledProgram; | ||
|
||
use crate::errors::CliError; | ||
|
||
use super::{create_named_dir, write_to_file}; | ||
|
||
pub(crate) fn save_program_to_file<P: AsRef<Path>>( | ||
compiled_program: &CompiledProgram, | ||
circuit_name: &str, | ||
circuit_dir: P, | ||
) -> PathBuf { | ||
let mut circuit_path = create_named_dir(circuit_dir.as_ref(), "target"); | ||
circuit_path.push(circuit_name); | ||
circuit_path.set_extension("json"); | ||
|
||
write_to_file(&serde_json::to_vec(compiled_program).unwrap(), &circuit_path); | ||
|
||
// Save a checksum of the circuit to compare against during proving and verification | ||
let acir_hash = hash_constraint_system(&compiled_program.circuit); | ||
circuit_path.set_extension("json.checksum"); | ||
write_to_file(hex::encode(acir_hash).as_bytes(), &circuit_path); | ||
|
||
circuit_path | ||
} | ||
|
||
pub(crate) fn read_program_from_file<P: AsRef<Path>>( | ||
circuit_path: P, | ||
) -> Result<CompiledProgram, CliError> { | ||
let file_path = circuit_path.as_ref().with_extension("json"); | ||
|
||
let input_string = std::fs::read(&file_path).map_err(|_| CliError::PathNotValid(file_path))?; | ||
|
||
let program = serde_json::from_slice(&input_string).expect("could not deserialize program"); | ||
|
||
Ok(program) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use acvm::acir::circuit::Circuit; | ||
|
||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
pub struct CompiledProgram { | ||
#[serde(serialize_with = "serialize_circuit", deserialize_with = "deserialize_circuit")] | ||
pub circuit: Circuit, | ||
pub abi: noirc_abi::Abi, | ||
} | ||
|
||
fn serialize_circuit<S>(circuit: &Circuit, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut circuit_bytes: Vec<u8> = Vec::new(); | ||
circuit.write(&mut circuit_bytes).unwrap(); | ||
|
||
circuit_bytes.serialize(s) | ||
} | ||
|
||
fn deserialize_circuit<'de, D>(deserializer: D) -> Result<Circuit, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let circuit_bytes = Vec::<u8>::deserialize(deserializer)?; | ||
let circuit = Circuit::read(&*circuit_bytes).unwrap(); | ||
Ok(circuit) | ||
} |