1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

Merge branch 'develop' into update-docs

This commit is contained in:
dark64 2020-05-25 16:08:56 +02:00
commit ce6915bc60
10 changed files with 196 additions and 30 deletions

View file

@ -43,8 +43,8 @@ jobs:
keys:
- v4-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}
- run:
name: Check format
command: rustup component add rustfmt; cargo fmt --all -- --check
name: Check format if rustfmt is available
command: "! rustup component add rustfmt || cargo fmt --all -- --check"
- run:
name: Install libsnark prerequisites
command: ./scripts/install_libsnark_prerequisites.sh

34
.githooks/pre-commit Executable file
View file

@ -0,0 +1,34 @@
#!/bin/bash
log () {
printf "[\033[1;37mpre-commit\033[0m] $1\n"
}
rustfmt +nightly --version &>/dev/null
if [ $? != 0 ]; then
log "\033[0;33mWARN\033[0m: rustfmt +nightly not found, skipping pre-commit hook"
exit 0
fi
files=()
for file in $(git diff --name-only --cached); do
if [ ${file: -3} == ".rs" ]; then
rustfmt +nightly --check $file &>/dev/null
if [ $? != 0 ]; then
files+=($file)
fi
fi
done
if [ -n "$files" ]; then
log "the following files have improper formatting:\n"
for file in "${files[@]}"; do
printf "\033[0;33m\t$file\n"
done
printf "\n\033[0m"
log "aborting commit"
exit 1
fi
exit 0

46
Cargo.lock generated
View file

@ -82,9 +82,9 @@ dependencies = [
[[package]]
name = "bellman_ce"
version = "0.3.3"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0300152b8d0c0220c70a39d6da08adff84352f5dd28d91944bc60e9dc8cbebb9"
checksum = "7e1a2edf80a8ed042463f8888946f70fcd901f1615711bb253b7dc32b9c9fe73"
dependencies = [
"bit-vec",
"byteorder",
@ -447,7 +447,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83c0a6a2c53b0bd6d8cae976816dc8a171e0554c03f304e3dac110473e0219fd"
dependencies = [
"byteorder",
"ff_derive_ce",
"ff_derive_ce 0.7.0",
"hex",
"rand 0.4.6",
]
[[package]]
name = "ff_ce"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ced6646e4e98a53da162e38ffe9c865edbd7a2f9ff197067b0a8bf1114bf8a"
dependencies = [
"byteorder",
"ff_derive_ce 0.8.0",
"hex",
"rand 0.4.6",
]
@ -466,6 +478,20 @@ dependencies = [
"syn 1.0.17",
]
[[package]]
name = "ff_derive_ce"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50c052fa6d4c2f12305ec364bfb8ef884836f3f61ea015b202372ff996d1ac4b"
dependencies = [
"num-bigint 0.2.6",
"num-integer",
"num-traits 0.2.11",
"proc-macro2 1.0.10",
"quote 1.0.3",
"syn 1.0.17",
]
[[package]]
name = "from-pest"
version = "0.3.1"
@ -880,12 +906,12 @@ dependencies = [
[[package]]
name = "pairing_ce"
version = "0.20.0"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c7b23aa7f0489d9a5e8e15cc5b7cb4f3e9b9d64812b8875fb91d25b640b0939"
checksum = "f55ca3bd80245b5d43dd4467bc9ab5daf869bd76c6cd3ca54c4499b41923657d"
dependencies = [
"byteorder",
"ff_ce",
"ff_ce 0.10.2",
"rand 0.4.6",
]
@ -1167,9 +1193,9 @@ dependencies = [
[[package]]
name = "sapling-crypto_ce"
version = "0.1.2"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec318b68765114e578fe42446b6861565b302bc93089aa01669d19417a6de993"
checksum = "1c4ff5309ec3e4bd800ad4ab3f71e9b76e9ea81c9f0eda6efa16008afbe440b3"
dependencies = [
"bellman_ce",
"blake2-rfc_bellman_edition",
@ -1691,7 +1717,7 @@ dependencies = [
"cc",
"cmake",
"csv",
"ff_ce",
"ff_ce 0.9.0",
"git2",
"glob 0.2.11",
"hex",
@ -1736,7 +1762,7 @@ version = "0.3.5"
dependencies = [
"bellman_ce",
"bincode",
"ff_ce",
"ff_ce 0.9.0",
"lazy_static",
"num-bigint 0.2.6",
"num-integer",

View file

@ -37,3 +37,11 @@ ZoKrates is released under the GNU Lesser General Public License v3.
We happily welcome contributions. You can either pick an existing issue or reach out on [Gitter](https://gitter.im/ZoKrates/Lobby).
Unless you explicitly state otherwise, any contribution you intentionally submit for inclusion in the work shall be licensed as above, without any additional terms or conditions.
### Git Hooks
You can enable zokrates git hooks locally by running:
```sh
git config core.hooksPath .githooks
```

View file

@ -20,11 +20,19 @@ For each command, you can get the list of expected arguments using `--help`.
zokrates compile -i /path/to/add.zok
```
Compiles a `.zok` source code file into ZoKrates internal representation of arithmetic circuits.
Compiles a `.zok` source code file and its dependencies into ZoKrates internal representation of arithmetic circuits.
Creates a compiled binary file at `./out`.
Unless the `--light` flag is set, a human-readable `.ztf` file is generated, which displays the compilation output in ZoKrates Text Format.
## `check`
```sh
zokrates check -i /path/to/add.zok
```
Checks a `.zok` source code file and its dependencies for errors.
## `compute-witness`
```sh

View file

@ -16,7 +16,7 @@ use std::io::{stdin, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::string::String;
use zokrates_abi::Encode;
use zokrates_core::compile::{compile, CompilationArtifacts, CompileError};
use zokrates_core::compile::{check, compile, CompilationArtifacts, CompileError};
use zokrates_core::ir::{self, ProgEnum};
use zokrates_core::proof_system::*;
use zokrates_core::typed_absy::abi::Abi;
@ -342,6 +342,46 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
Ok(())
}
fn cli_check<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
println!("Checking {}\n", sub_matches.value_of("input").unwrap());
let path = PathBuf::from(sub_matches.value_of("input").unwrap());
let file = File::open(path.clone())
.map_err(|why| format!("Couldn't open input file {}: {}", path.display(), why))?;
let mut reader = BufReader::new(file);
let mut source = String::new();
reader.read_to_string(&mut source).unwrap();
let fmt_error = |e: &CompileError| {
format!(
"{}:{}",
e.file()
.canonicalize()
.unwrap()
.strip_prefix(std::env::current_dir().unwrap())
.unwrap()
.display(),
e.value()
)
};
let resolver = FileSystemResolver::new();
let _ = check::<T, _>(source, path, Some(&resolver)).map_err(|e| {
format!(
"Check failed:\n\n{}",
e.0.iter()
.map(|e| fmt_error(e))
.collect::<Vec<_>>()
.join("\n\n")
)
})?;
println!("Program checked, no errors found.");
Ok(())
}
fn cli_verify<T: Field, P: ProofSystem<T>>(sub_matches: &ArgMatches) -> Result<(), String> {
let vk_path = Path::new(sub_matches.value_of("verification-key-path").unwrap());
let vk = std::fs::read_to_string(vk_path)
@ -420,6 +460,25 @@ fn cli() -> Result<(), String> {
.required(false)
)
)
.subcommand(SubCommand::with_name("check")
.about("Checks a program for errors")
.arg(Arg::with_name("input")
.short("i")
.long("input")
.help("Path of the source code")
.value_name("FILE")
.takes_value(true)
.required(true)
).arg(Arg::with_name("curve")
.short("c")
.long("curve")
.help("Curve to be used in the compilation")
.takes_value(true)
.required(false)
.possible_values(CURVES)
.default_value(&default_curve)
)
)
.subcommand(SubCommand::with_name("setup")
.about("Performs a trusted setup for a given constraint system")
.arg(Arg::with_name("input")
@ -667,6 +726,15 @@ fn cli() -> Result<(), String> {
_ => unreachable!(),
}
}
("check", Some(sub_matches)) => {
let curve = sub_matches.value_of("curve").unwrap();
match curve {
constants::BN128 => cli_check::<Bn128Field>(sub_matches)?,
constants::BLS12_381 => cli_check::<Bls12Field>(sub_matches)?,
_ => unreachable!(),
}
}
("compute-witness", Some(sub_matches)) => {
println!("Computing witness...");

View file

@ -26,7 +26,7 @@ serde_bytes = "0.10"
bincode = "0.8.0"
hex = "0.4.2"
regex = "0.2"
pairing_ce = "0.20"
pairing_ce = "^0.21"
ff_ce = "0.9"
zokrates_field = { version = "0.3.0", path = "../zokrates_field" }
zokrates_pest_ast = { version = "0.1.0", path = "../zokrates_pest_ast" }
@ -34,7 +34,7 @@ zokrates_embed = { path = "../zokrates_embed" }
zokrates_common = { path = "../zokrates_common" }
rand = "0.4"
csv = "1"
bellman_ce = { version = "=0.3.3", default-features = false}
bellman_ce = { version = "^0.3", default-features = false}
[dev-dependencies]
glob = "0.2.11"

View file

@ -17,6 +17,7 @@ use std::fmt;
use std::io;
use std::path::PathBuf;
use typed_absy::abi::Abi;
use typed_absy::TypedProgram;
use typed_arena::Arena;
use zokrates_common::Resolver;
use zokrates_field::Field;
@ -149,19 +150,10 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
) -> Result<CompilationArtifacts<T>, CompileErrors> {
let arena = Arena::new();
let source = arena.alloc(source);
let compiled = compile_program(source, location.clone(), resolver, &arena)?;
// check semantics
let typed_ast = Checker::check(compiled).map_err(|errors| {
CompileErrors(errors.into_iter().map(|e| CompileError::from(e)).collect())
})?;
let typed_ast = check_with_arena(source, location, resolver, &arena)?;
let abi = typed_ast.abi();
// analyse (unroll and constant propagation)
let typed_ast = typed_ast.analyse();
// flatten input program
let program_flattened = Flattener::flatten(typed_ast);
@ -183,6 +175,36 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
})
}
pub fn check<'ast, T: Field, E: Into<imports::Error>>(
source: String,
location: FilePath,
resolver: Option<&dyn Resolver<E>>,
) -> Result<(), CompileErrors> {
let arena = Arena::new();
check_with_arena::<T, _>(source, location, resolver, &arena).map(|_| ())
}
fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>(
source: String,
location: FilePath,
resolver: Option<&dyn Resolver<E>>,
arena: &'ast Arena<String>,
) -> Result<TypedProgram<'ast, T>, CompileErrors> {
let source = arena.alloc(source);
let compiled = compile_program(source, location.clone(), resolver, &arena)?;
// check semantics
let typed_ast = Checker::check(compiled).map_err(|errors| {
CompileErrors(errors.into_iter().map(|e| CompileError::from(e)).collect())
})?;
// analyse (unroll and constant propagation)
let typed_ast = typed_ast.analyse();
Ok(typed_ast)
}
pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
source: &'ast str,
location: FilePath,

View file

@ -10,5 +10,5 @@ wasm = ["bellman_ce/wasm", "sapling-crypto_ce/wasm"]
multicore = ["bellman_ce/multicore", "sapling-crypto_ce/multicore"]
[dependencies]
bellman_ce = { version = "=0.3.3", default-features = false}
sapling-crypto_ce = { version = "0.1.1", default-features = false }
bellman_ce = { version = "^0.3", default-features = false}
sapling-crypto_ce = { version = "0.1.3", default-features = false }

View file

@ -10,11 +10,11 @@ serde_derive = "1.0"
lazy_static = "1.4"
bincode = "0.8.0"
serde_json = "1.0"
bellman_ce = { version = "=0.3.3", default-features = false}
bellman_ce = { version = "^0.3", default-features = false}
sha2 = "0.8.0"
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1", default-features = false }
pairing_ce = "0.20"
pairing_ce = "^0.21"
ff_ce = { features = ["derive"], version = "0.9" }
[dev-dependencies]