1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

Merge pull request #279 from Zokrates/stdlib-crate

Make a crate out of stdlib
This commit is contained in:
Stefan 2019-03-15 11:58:00 +01:00 committed by GitHub
commit 67ddb5be4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 241 additions and 6 deletions

View file

@ -20,7 +20,7 @@ jobs:
command: ./scripts/install_libsnark_prerequisites.sh
- run:
name: Build
command: ZOKRATES_HOME=$(pwd)/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" ./build.sh
command: ZOKRATES_HOME=$(pwd)/zokrates_stdlib/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" ./build.sh
- save_cache:
paths:
- /usr/local/cargo/registry
@ -50,10 +50,10 @@ jobs:
command: ./scripts/install_libsnark_prerequisites.sh
- run:
name: Build
command: ZOKRATES_HOME=$(pwd)/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" ./build.sh
command: ZOKRATES_HOME=$(pwd)/zokrates_stdlib/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" ./build.sh
- run:
name: Run tests
command: ZOKRATES_HOME=$(pwd)/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" cargo test --release -- --test-threads=1
command: ZOKRATES_HOME=$(pwd)/zokrates_stdlib/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" cargo test --release -- --test-threads=1
- run:
name: Generate code coverage report
command: ./scripts/cov.sh
@ -102,7 +102,7 @@ jobs:
command: ./scripts/install_solcjs_deb.sh
- run:
name: Run integration tests
command: ZOKRATES_HOME=$(pwd)/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" cargo test --release -- --ignored
command: ZOKRATES_HOME=$(pwd)/zokrates_stdlib/stdlib/ WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" cargo test --release -- --ignored
deploy:
docker:
- image: circleci/python

12
Cargo.lock generated
View file

@ -784,6 +784,18 @@ dependencies = [
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "stdlib"
version = "0.1.0"
dependencies = [
"serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)",
"zokrates_core 0.3.6",
"zokrates_field 0.3.2",
"zokrates_fs_resolver 0.4.0",
]
[[package]]
name = "strsim"
version = "0.7.0"

View file

@ -4,4 +4,5 @@ members = [
"zokrates_core",
"zokrates_cli",
"zokrates_fs_resolver",
"zokrates_stdlib",
]

View file

@ -29,6 +29,6 @@ RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain $RUST_TOOLCHAI
&& (cd src;./build_release.sh) \
&& mv ./src/target/release/zokrates . \
&& mv ./src/zokrates_cli/examples . \
&& mv ./src/stdlib/* $ZOKRATES_HOME \
&& mv ./src/zokrates_stdlib/stdlib/* $ZOKRATES_HOME \
&& rustup self uninstall -y \
&& rm -rf src

View file

@ -6,7 +6,7 @@ RUN useradd -u 1000 -m zokrates
ARG RUST_TOOLCHAIN=nightly-2019-01-01
ENV WITH_LIBSNARK=1
ENV ZOKRATES_HOME=/home/zokrates/ZoKrates/stdlib/
ENV ZOKRATES_HOME=/home/zokrates/ZoKrates/zokrates_stdlib/stdlib/
COPY ./scripts/install_libsnark_prerequisites.sh /tmp/
RUN /tmp/install_libsnark_prerequisites.sh

View file

@ -0,0 +1,20 @@
[package]
name = "zokrates_stdlib"
version = "0.1.0"
authors = ["schaeff <thibaut@schaeff.fr>"]
edition = "2018"
[features]
default = ["libsnark"]
libsnark = ["zokrates_core/libsnark"]
[dev-dependencies]
zokrates_field = { version = "0.3", path = "../zokrates_field" }
zokrates_core = { version = "0.3", path = "../zokrates_core" }
zokrates_fs_resolver = { version = "0.4", path = "../zokrates_fs_resolver" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
[build-dependencies]
glob = "0.3.0"

37
zokrates_stdlib/build.rs Normal file
View file

@ -0,0 +1,37 @@
use glob::glob;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let destination = Path::new(&out_dir).join("tests.rs");
let mut test_file = File::create(&destination).unwrap();
for directory in glob("./tests/bench/**/*.json").unwrap() {
write_test(&mut test_file, &directory.unwrap());
}
}
fn write_test(test_file: &mut File, test_path: &PathBuf) {
let test_name = format!(
"test_{}",
test_path
.strip_prefix("tests/bench")
.unwrap()
.display()
.to_string()
.replace("/", "_")
.replace(".json", "")
.replace(".", "")
);
write!(
test_file,
include_str!("./tests/test_template"),
test_name = test_name,
test_path = test_path.display()
)
.unwrap();
}

View file

@ -0,0 +1 @@
fn main() {}

View file

@ -0,0 +1,15 @@
{
"entry_point": "./stdlib/utils/binary/and.code",
"tests": [
{
"input": {
"values": ["0", "0"]
},
"output": {
"Ok": {
"values": ["0"]
}
}
}
]
}

View file

@ -0,0 +1,15 @@
{
"entry_point": "./stdlib/hashes/sha256/512bitPacked.code",
"tests": [
{
"input": {
"values": ["0", "0", "0", "5"]
},
"output": {
"Ok": {
"values": ["263561599766550617289250058199814760685", "65303172752238645975888084098459749904"]
}
}
}
]
}

View file

@ -0,0 +1,15 @@
{
"entry_point": "./stdlib/utils/binary/or.code",
"tests": [
{
"input": {
"values": ["0", "0"]
},
"output": {
"Ok": {
"values": ["0"]
}
}
}
]
}

View file

@ -0,0 +1,7 @@
#[macro_use]
extern crate serde_derive;
#[macro_use]
mod utils;
include!(concat!(env!("OUT_DIR"), "/tests.rs"));

View file

@ -0,0 +1,44 @@
#[test]
#[ignore]
#[allow(non_snake_case)]
fn {test_name}() {{
use zokrates_field::field::{{Field, FieldPrime}};
use std::path::PathBuf;
use zokrates_fs_resolver::resolve;
use zokrates_core::compile::compile;
use std::fs::File;
use std::io::{{BufReader, Read}};
let t: utils::Tests = serde_json::from_reader(
BufReader::new(
File::open(
&PathBuf::from("{test_path}")
).unwrap()
)
).unwrap();
let mut code_reader = BufReader::new(File::open(&t.entry_point).unwrap());
let bin = compile(
&mut code_reader,
Some(t.entry_point.parent().unwrap().to_str().unwrap().to_string()),
Some(resolve)
).unwrap();
for test in t.tests.into_iter() {{
let input = &test.input.values;
let output = bin.execute(&input.iter().map(|v| FieldPrime::from_dec_string(v.clone())).collect());
match utils::compare(output, test.output) {{
Err(e) => {{
let mut code = File::open(&t.entry_point).unwrap();
let mut s = String::new();
code.read_to_string(&mut s).unwrap();
let context = format!("\n{{}}\nCalled with input ({{}})\n", s, input.iter().map(|i| format!("{{}}", i)).collect::<Vec<_>>().join(", "));
panic!("{{}}{{}}", context, e)
}},
Ok(..) => {{}}
}};
}}
}}

View file

@ -0,0 +1,68 @@
use std::path::PathBuf;
use zokrates_core::ir;
use zokrates_field::field::{Field, FieldPrime};
#[derive(Serialize, Deserialize)]
pub struct Tests {
pub entry_point: PathBuf,
pub tests: Vec<Test>,
}
#[derive(Serialize, Deserialize)]
pub struct Input {
pub values: Vec<Val>,
}
#[derive(Serialize, Deserialize)]
pub struct Test {
pub input: Input,
pub output: TestResult,
}
pub type TestResult = Result<Output, ir::Error>;
#[derive(PartialEq, Debug)]
pub struct ComparableResult(Result<Vec<FieldPrime>, ir::Error>);
#[derive(Serialize, Deserialize)]
pub struct Output {
values: Vec<Val>,
}
type Val = String;
impl From<ir::ExecutionResult<FieldPrime>> for ComparableResult {
fn from(r: ir::ExecutionResult<FieldPrime>) -> ComparableResult {
ComparableResult(r.map(|v| v.return_values().iter().map(|&x| x.clone()).collect()))
}
}
impl From<TestResult> for ComparableResult {
fn from(r: TestResult) -> ComparableResult {
ComparableResult(r.map(|v| {
v.values
.into_iter()
.map(|v| FieldPrime::from_dec_string(v))
.collect()
}))
}
}
pub fn compare(
result: ir::ExecutionResult<FieldPrime>,
expected: TestResult,
) -> Result<(), String> {
// extract outputs from result
let result = ComparableResult::from(result);
// deserialize expected result
let expected = ComparableResult::from(expected);
if result != expected {
return Err(format!(
"Expected {:?} but found {:?}",
expected.0, result.0
));
}
Ok(())
}