mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-22 20:48:17 +03:00
add serialized circuit file
This commit is contained in:
parent
3c6d7e2d90
commit
22fcd9e007
@ -12,6 +12,8 @@ use snarkos_curves::{bls12_377::Bls12_377, edwards_bls12::Fq};
|
||||
use snarkos_models::gadgets::r1cs::ConstraintSystem;
|
||||
|
||||
use clap::ArgMatches;
|
||||
use serde::Serialize;
|
||||
use snarkos_models::curves::{PairingEngine, PrimeField};
|
||||
use std::{convert::TryFrom, env::current_dir};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
25
package/src/errors/outputs/circuit.rs
Normal file
25
package/src/errors/outputs/circuit.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use std::{io, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CircuitFileError {
|
||||
#[error("{}: {}", _0, _1)]
|
||||
Crate(&'static str, String),
|
||||
|
||||
#[error("creating: {}", _0)]
|
||||
Creating(io::Error),
|
||||
|
||||
#[error("Cannot read from the provided file path - {:?}", _0)]
|
||||
FileReadError(PathBuf),
|
||||
|
||||
#[error("Cannot remove the provided file - {:?}", _0)]
|
||||
FileRemovalError(PathBuf),
|
||||
|
||||
#[error("writing: {}", _0)]
|
||||
Writing(io::Error),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for CircuitFileError {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
CircuitFileError::Crate("std::io", format!("{}", error))
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
pub mod circuit;
|
||||
pub use circuit::*;
|
||||
|
||||
pub mod checksum;
|
||||
pub use checksum::*;
|
||||
|
||||
|
73
package/src/outputs/circuit.rs
Normal file
73
package/src/outputs/circuit.rs
Normal file
@ -0,0 +1,73 @@
|
||||
//! The serialized circuit output file.
|
||||
|
||||
use crate::{errors::CircuitFileError, outputs::OUTPUTS_DIRECTORY_NAME};
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::Write,
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
pub static CIRCUIT_FILE_EXTENSION: &str = ".bytes";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CircuitFile {
|
||||
pub package_name: String,
|
||||
}
|
||||
|
||||
impl CircuitFile {
|
||||
pub fn new(package_name: &str) -> Self {
|
||||
Self {
|
||||
package_name: package_name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exists_at(&self, path: &PathBuf) -> bool {
|
||||
let path = self.setup_file_path(path);
|
||||
path.exists()
|
||||
}
|
||||
|
||||
/// Reads the serialized circuit from the given file path if it exists.
|
||||
pub fn read_from(&self, path: &PathBuf) -> Result<Vec<u8>, CircuitFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
Ok(fs::read(&path).map_err(|_| CircuitFileError::FileReadError(path.clone()))?)
|
||||
}
|
||||
|
||||
/// Writes the given serialized circuit to a file.
|
||||
pub fn write_to(&self, path: &PathBuf, serialized_circuit: &[u8]) -> Result<(), CircuitFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
|
||||
let mut file = File::create(&path)?;
|
||||
file.write_all(serialized_circuit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes the serialized circuit at the given path if it exists. Returns `true` on success,
|
||||
/// `false` if the file doesn't exist, and `Error` if the file system fails during operation.
|
||||
pub fn remove(&self, path: &PathBuf) -> Result<bool, CircuitFileError> {
|
||||
let path = self.setup_file_path(path);
|
||||
if !path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.clone()))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
|
||||
let mut path = path.to_owned();
|
||||
if path.is_dir() {
|
||||
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
|
||||
path.push(PathBuf::from(OUTPUTS_DIRECTORY_NAME));
|
||||
}
|
||||
path.push(PathBuf::from(format!(
|
||||
"{}{}",
|
||||
self.package_name, CIRCUIT_FILE_EXTENSION
|
||||
)));
|
||||
}
|
||||
path
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
pub mod circuit;
|
||||
pub use self::circuit::*;
|
||||
|
||||
pub mod checksum;
|
||||
pub use self::checksum::*;
|
||||
|
||||
|
@ -6,6 +6,7 @@ use crate::{
|
||||
inputs::{INPUTS_DIRECTORY_NAME, INPUT_FILE_EXTENSION},
|
||||
outputs::{
|
||||
CHECKSUM_FILE_EXTENSION,
|
||||
CIRCUIT_FILE_EXTENSION,
|
||||
OUTPUTS_DIRECTORY_NAME,
|
||||
PROOF_FILE_EXTENSION,
|
||||
PROVING_KEY_FILE_EXTENSION,
|
||||
@ -118,7 +119,7 @@ fn is_excluded(path: &Path) -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// excluded extensions: `.in`, `.bytes`, `lpk`, `lvk`, `.proof`, `.sum`
|
||||
// excluded extensions: `.in`, `.bytes`, `lpk`, `lvk`, `.proof`, `.sum`, `.zip`, `.bytes`
|
||||
path.extension()
|
||||
.map(|ext| {
|
||||
ext.eq(INPUT_FILE_EXTENSION.trim_start_matches("."))
|
||||
@ -128,6 +129,7 @@ fn is_excluded(path: &Path) -> bool {
|
||||
| ext.eq(PROOF_FILE_EXTENSION.trim_start_matches("."))
|
||||
| ext.eq(CHECKSUM_FILE_EXTENSION.trim_start_matches("."))
|
||||
| ext.eq(ZIP_FILE_EXTENSION.trim_start_matches("."))
|
||||
| ext.eq(CIRCUIT_FILE_EXTENSION.trim_start_matches("."))
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user