add log and env_logger, refactor compile for clarity
This commit is contained in:
parent
3643817da1
commit
fe21b7049e
25 changed files with 139 additions and 14 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,6 +1,6 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
target
|
||||
|
||||
# ZoKrates default files
|
||||
out
|
||||
|
|
76
Cargo.lock
generated
76
Cargo.lock
generated
|
@ -24,6 +24,15 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.11.0"
|
||||
|
@ -634,6 +643,19 @@ version = "1.6.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex 1.4.2",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "environment"
|
||||
version = "0.1.1"
|
||||
|
@ -904,6 +926,12 @@ version = "0.4.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.2.0"
|
||||
|
@ -1451,13 +1479,25 @@ version = "0.2.11"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"aho-corasick 0.6.10",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"thread_local",
|
||||
"regex-syntax 0.5.6",
|
||||
"thread_local 0.3.6",
|
||||
"utf8-ranges",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
|
||||
dependencies = [
|
||||
"aho-corasick 0.7.15",
|
||||
"memchr",
|
||||
"regex-syntax 0.6.21",
|
||||
"thread_local 1.0.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.1.9"
|
||||
|
@ -1476,6 +1516,12 @@ dependencies = [
|
|||
"ucd-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
|
||||
|
||||
[[package]]
|
||||
name = "remove_dir_all"
|
||||
version = "0.5.3"
|
||||
|
@ -1725,6 +1771,15 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
|
@ -1743,6 +1798,15 @@ dependencies = [
|
|||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.1.0"
|
||||
|
@ -2044,9 +2108,10 @@ dependencies = [
|
|||
"bincode",
|
||||
"clap",
|
||||
"dirs",
|
||||
"env_logger",
|
||||
"fs_extra",
|
||||
"glob 0.2.11",
|
||||
"regex",
|
||||
"regex 0.2.11",
|
||||
"serde_json",
|
||||
"tempdir",
|
||||
"zokrates_abi",
|
||||
|
@ -2081,6 +2146,7 @@ dependencies = [
|
|||
"git2",
|
||||
"hex",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"num",
|
||||
"num-bigint",
|
||||
"pairing_ce",
|
||||
|
@ -2088,7 +2154,7 @@ dependencies = [
|
|||
"rand 0.4.6",
|
||||
"rand 0.7.3",
|
||||
"reduce",
|
||||
"regex",
|
||||
"regex 0.2.11",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
|
|
|
@ -19,6 +19,7 @@ zokrates_core = { version = "0.5", path = "../zokrates_core", features = ["multi
|
|||
zokrates_fs_resolver = { version = "0.5", path = "../zokrates_fs_resolver"}
|
||||
serde_json = "1.0"
|
||||
dirs = "3.0.1"
|
||||
env_logger = "0.8.2"
|
||||
|
||||
[dev-dependencies]
|
||||
glob = "0.2.11"
|
||||
|
|
|
@ -11,6 +11,7 @@ use constants::*;
|
|||
use helpers::*;
|
||||
|
||||
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||
use env_logger;
|
||||
use serde_json::{from_reader, to_writer_pretty, Value};
|
||||
use std::convert::TryFrom;
|
||||
use std::env;
|
||||
|
@ -35,6 +36,10 @@ use {
|
|||
};
|
||||
|
||||
fn main() {
|
||||
let mut log_builder = env_logger::Builder::from_default_env();
|
||||
|
||||
log_builder.format_module_path(false).init();
|
||||
|
||||
cli().unwrap_or_else(|e| {
|
||||
println!("{}", e);
|
||||
std::process::exit(1);
|
||||
|
|
|
@ -37,6 +37,9 @@ rand_0_7 = { version = "0.7", package = "rand" }
|
|||
csv = "1"
|
||||
bellman_ce = { version = "^0.3", default-features = false }
|
||||
|
||||
# logging
|
||||
log = "0.4"
|
||||
|
||||
ark-ff = { git = "https://github.com/arkworks-rs/algebra", default-features = false, optional = true }
|
||||
ark-ec = { git = "https://github.com/arkworks-rs/algebra", default-features = false, optional = true }
|
||||
|
||||
|
@ -44,11 +47,9 @@ ark-bn254 = { git = "https://github.com/arkworks-rs/curves", features = ["curve"
|
|||
ark-bls12-377 = { git = "https://github.com/arkworks-rs/curves", features = ["curve"], default-features = false, optional = true }
|
||||
ark-bw6-761 = { git = "https://github.com/arkworks-rs/curves", default-features = false, optional = true }
|
||||
|
||||
|
||||
ark-gm17 = { git = "https://github.com/arkworks-rs/gm17", default-features = false, optional = true }
|
||||
ark-serialize = { git = "https://github.com/arkworks-rs/algebra", default-features = false, optional = true }
|
||||
ark-relations = { git = "https://github.com/arkworks-rs/snark", default-features = false, optional = true }
|
||||
#algebra = { git = "https://github.com/scipr-lab/ark.git", features = ["bn254", "bls12_377", "bw6_761"], default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "^0.3.0"
|
||||
|
|
|
@ -7,6 +7,7 @@ use absy::{Module, ModuleId, Program};
|
|||
use flatten::Flattener;
|
||||
use imports::{self, Importer};
|
||||
use ir;
|
||||
use log::info;
|
||||
use macros;
|
||||
use macros::process_macros;
|
||||
use semantics::{self, Checker};
|
||||
|
@ -147,6 +148,8 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
|
|||
location: FilePath,
|
||||
resolver: Option<&dyn Resolver<E>>,
|
||||
) -> Result<CompilationArtifacts<T>, CompileErrors> {
|
||||
info!("Compile \"{}\"", location.display());
|
||||
|
||||
let arena = Arena::new();
|
||||
|
||||
let (typed_ast, abi) = check_with_arena(source, location, resolver, &arena)?;
|
||||
|
@ -189,7 +192,7 @@ fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>(
|
|||
arena: &'ast Arena<String>,
|
||||
) -> Result<(ZirProgram<'ast, T>, Abi), CompileErrors> {
|
||||
let source = arena.alloc(source);
|
||||
let compiled = compile_program::<T, E>(source, location.clone(), resolver, &arena)?;
|
||||
let compiled = parse_program::<T, E>(source, location.clone(), resolver, &arena)?;
|
||||
|
||||
// check semantics
|
||||
let typed_ast = Checker::check(compiled).map_err(|errors| {
|
||||
|
@ -204,7 +207,7 @@ fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>(
|
|||
Ok((typed_ast, abi))
|
||||
}
|
||||
|
||||
pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
|
||||
pub fn parse_program<'ast, T: Field, E: Into<imports::Error>>(
|
||||
source: &'ast str,
|
||||
location: FilePath,
|
||||
resolver: Option<&dyn Resolver<E>>,
|
||||
|
@ -212,7 +215,7 @@ pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
|
|||
) -> Result<Program<'ast>, CompileErrors> {
|
||||
let mut modules = HashMap::new();
|
||||
|
||||
let main = compile_module::<T, E>(&source, location.clone(), resolver, &mut modules, &arena)?;
|
||||
let main = parse_module::<T, E>(&source, location.clone(), resolver, &mut modules, &arena)?;
|
||||
|
||||
modules.insert(location.clone(), main);
|
||||
|
||||
|
@ -222,13 +225,15 @@ pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
|
|||
})
|
||||
}
|
||||
|
||||
pub fn compile_module<'ast, T: Field, E: Into<imports::Error>>(
|
||||
pub fn parse_module<'ast, T: Field, E: Into<imports::Error>>(
|
||||
source: &'ast str,
|
||||
location: FilePath,
|
||||
resolver: Option<&dyn Resolver<E>>,
|
||||
modules: &mut HashMap<ModuleId, Module<'ast>>,
|
||||
arena: &'ast Arena<String>,
|
||||
) -> Result<Module<'ast>, CompileErrors> {
|
||||
info!("Parse module \"{}\"", location.display());
|
||||
|
||||
let ast = pest::generate_ast(&source)
|
||||
.map_err(|e| CompileErrors::from(CompileErrorInner::from(e).in_file(&location)))?;
|
||||
|
||||
|
|
|
@ -149,6 +149,8 @@ impl<T: Field> FlatUExpression<T> {
|
|||
|
||||
impl<'ast, T: Field> Flattener<'ast, T> {
|
||||
pub fn flatten(p: ZirProgram<'ast, T>) -> FlatProg<T> {
|
||||
log::info!("Flatten ({} statements)", p.main().statements.len());
|
||||
|
||||
Flattener::new().flatten_program(p)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
//! @date 2018
|
||||
|
||||
use crate::absy::*;
|
||||
use crate::compile::compile_module;
|
||||
use crate::compile::parse_module;
|
||||
use crate::compile::{CompileErrorInner, CompileErrors};
|
||||
use crate::embed::FlatEmbed;
|
||||
use crate::parser::Position;
|
||||
|
@ -259,7 +259,7 @@ impl Importer {
|
|||
None => {
|
||||
let source = arena.alloc(source);
|
||||
|
||||
let compiled = compile_module::<T, E>(
|
||||
let compiled = parse_module::<T, E>(
|
||||
source,
|
||||
new_location.clone(),
|
||||
resolver,
|
||||
|
|
|
@ -19,6 +19,8 @@ extern crate zokrates_common;
|
|||
extern crate zokrates_field;
|
||||
extern crate zokrates_pest_ast;
|
||||
|
||||
extern crate log;
|
||||
|
||||
extern crate bellman_ce as bellman;
|
||||
extern crate ff_ce as ff;
|
||||
extern crate pairing_ce as pairing;
|
||||
|
|
|
@ -32,6 +32,8 @@ impl<T: Field> DirectiveOptimizer<T> {
|
|||
}
|
||||
|
||||
pub fn optimize(p: Prog<T>) -> Prog<T> {
|
||||
log::info!("Optimize directives");
|
||||
|
||||
DirectiveOptimizer::new().fold_module(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@ impl DuplicateOptimizer {
|
|||
}
|
||||
|
||||
pub fn optimize<T: Field>(p: Prog<T>) -> Prog<T> {
|
||||
log::info!("Optimize duplicate constraints");
|
||||
|
||||
Self::new().fold_module(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,6 +60,8 @@ impl<T: Field> RedefinitionOptimizer<T> {
|
|||
}
|
||||
|
||||
pub fn optimize(p: Prog<T>) -> Prog<T> {
|
||||
log::info!("Optimize redefinitions");
|
||||
|
||||
RedefinitionOptimizer::new().fold_module(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ impl TautologyOptimizer {
|
|||
}
|
||||
|
||||
pub fn optimize<T: Field>(p: Prog<T>) -> Prog<T> {
|
||||
log::info!("Optimize tautologies");
|
||||
|
||||
TautologyOptimizer::new().fold_module(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -102,6 +102,8 @@ impl<T: Field> Propagate<T> for FlatFunction<T> {
|
|||
|
||||
impl<T: Field> FlatProg<T> {
|
||||
pub fn propagate(self) -> FlatProg<T> {
|
||||
log::info!("Propagate (flat)");
|
||||
|
||||
let main = self.main.propagate();
|
||||
|
||||
FlatProg { main }
|
||||
|
|
|
@ -47,6 +47,8 @@ fn flatten_identifier_rec<'a>(
|
|||
|
||||
impl<'ast, T: Field> Flattener<T> {
|
||||
pub fn flatten(p: typed_absy::TypedProgram<T>) -> zir::ZirProgram<T> {
|
||||
log::info!("Flatten complex types");
|
||||
|
||||
let mut f = Flattener {
|
||||
phantom: PhantomData,
|
||||
};
|
||||
|
|
|
@ -78,6 +78,8 @@ impl<'ast, T: Field> Inliner<'ast, T> {
|
|||
}
|
||||
|
||||
pub fn inline(p: TypedProgram<T>) -> TypedProgram<T> {
|
||||
log::info!("Inline");
|
||||
|
||||
let main_module_id = p.main;
|
||||
|
||||
// get the main module
|
||||
|
|
|
@ -30,6 +30,8 @@ impl PropagatedUnroller {
|
|||
pub fn unroll<'ast, T: Field>(
|
||||
p: TypedProgram<'ast, T>,
|
||||
) -> Result<TypedProgram<'ast, T>, &'static str> {
|
||||
log::info!("Unroll (iterative)");
|
||||
|
||||
let mut blocked_at = None;
|
||||
|
||||
// unroll a first time, retrieving whether the unroll is complete
|
||||
|
|
|
@ -42,10 +42,12 @@ impl<'ast, T: Field> Propagator<'ast, T> {
|
|||
}
|
||||
|
||||
pub fn propagate(p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> {
|
||||
log::info!("Propagate");
|
||||
Propagator::new().fold_program(p)
|
||||
}
|
||||
|
||||
pub fn propagate_verbose(p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> {
|
||||
log::info!("Propagate (verbose)");
|
||||
Propagator::verbose().fold_program(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ impl<'ast> RedefinitionOptimizer<'ast> {
|
|||
}
|
||||
|
||||
pub fn optimize<T: Field>(p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> {
|
||||
log::info!("Optimize redefinitions");
|
||||
|
||||
RedefinitionOptimizer::new().fold_program(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ pub struct ReturnBinder;
|
|||
|
||||
impl ReturnBinder {
|
||||
pub fn bind<'ast, T: Field>(p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> {
|
||||
log::info!("Bind outputs");
|
||||
|
||||
ReturnBinder {}.fold_program(p)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ impl<'ast, T: Field> UintOptimizer<'ast, T> {
|
|||
}
|
||||
|
||||
pub fn optimize(p: ZirProgram<'ast, T>) -> ZirProgram<'ast, T> {
|
||||
log::info!("Optimize uint");
|
||||
|
||||
UintOptimizer::new().fold_program(p)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,8 @@ impl UnconstrainedVariableDetector {
|
|||
}
|
||||
}
|
||||
pub fn detect<T: Field>(p: Prog<T>) -> Prog<T> {
|
||||
log::info!("Detect unconstrained variables");
|
||||
|
||||
let mut instance = Self::new(&p);
|
||||
let p = instance.fold_module(p);
|
||||
|
||||
|
|
|
@ -55,6 +55,8 @@ impl<'ast> Unroller<'ast> {
|
|||
}
|
||||
|
||||
pub fn unroll<T: Field>(p: TypedProgram<T>) -> Output<T> {
|
||||
log::info!("Unroll");
|
||||
|
||||
let mut unroller = Unroller::new();
|
||||
let p = unroller.fold_program(p);
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@ impl<'ast, T: Field> VariableAccessRemover<'ast, T> {
|
|||
}
|
||||
|
||||
pub fn apply(p: TypedProgram<'ast, T>) -> TypedProgram<'ast, T> {
|
||||
log::info!("Remove variable access");
|
||||
|
||||
Self::new().fold_program(p)
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,19 @@ pub struct ZirProgram<'ast, T> {
|
|||
pub main: ZirModuleId,
|
||||
}
|
||||
|
||||
impl<'ast, T> ZirProgram<'ast, T> {
|
||||
pub fn main(&self) -> &ZirFunction<'ast, T> {
|
||||
self.modules
|
||||
.get(&self.main)
|
||||
.map(|m| m.functions.iter().find(|(k, _)| k.id == "main").unwrap().1)
|
||||
.map(|f| match f {
|
||||
ZirFunctionSymbol::Here(f) => f,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T: fmt::Display> fmt::Display for ZirProgram<'ast, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for (module_id, module) in &self.modules {
|
||||
|
|
Loading…
Reference in a new issue