add zokrates_js crate
This commit is contained in:
parent
4b409af47c
commit
27d94bf59e
18 changed files with 6071 additions and 0 deletions
37
Cargo.lock
generated
37
Cargo.lock
generated
|
@ -103,6 +103,16 @@ dependencies = [
|
|||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-vec"
|
||||
version = "0.4.4"
|
||||
|
@ -239,6 +249,15 @@ dependencies = [
|
|||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console_error_panic_hook"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.4"
|
||||
|
@ -1381,6 +1400,8 @@ version = "0.2.55"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen-macro 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -1606,6 +1627,20 @@ dependencies = [
|
|||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_js"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"console_error_panic_hook 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"wasm-bindgen 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"zokrates_abi 0.1.0",
|
||||
"zokrates_core 0.4.0",
|
||||
"zokrates_field 0.3.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zokrates_parser"
|
||||
version = "0.1.3"
|
||||
|
@ -1662,6 +1697,7 @@ dependencies = [
|
|||
"checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491"
|
||||
"checksum bellman_ce 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "938ec0feff00f9dfda0e7cbfe8db8b717966a84f6a12e63ed0943c4a90d6a5de"
|
||||
"checksum bincode 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e103c8b299b28a9c6990458b7013dc4a8356a9b854c51b9883241f5866fac36e"
|
||||
"checksum bincode 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8ab639324e3ee8774d296864fbc0dbbb256cf1a41c490b94cba90c082915f92"
|
||||
"checksum bit-vec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "02b4ff8b16e6076c3e14220b39fbc1fabb6737522281a388998046859400895f"
|
||||
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
"checksum blake2-rfc_bellman_edition 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fdc60350286c7c3db13b98e91dbe5c8b6830a6821bc20af5b0c310ce94d74915"
|
||||
|
@ -1679,6 +1715,7 @@ dependencies = [
|
|||
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
|
||||
"checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62"
|
||||
"checksum colored 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "433e7ac7d511768127ed85b0c4947f47a254131e37864b2dc13f52aa32cd37e5"
|
||||
"checksum console_error_panic_hook 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211"
|
||||
"checksum constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "995a44c877f9212528ccc74b21a232f66ad69001e40ede5bcee2ac9ef2657120"
|
||||
"checksum crossbeam 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e"
|
||||
"checksum crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "acec9a3b0b3559f15aee4f90746c4e5e293b701c0f7d3925d24e01645267b68c"
|
||||
|
|
|
@ -7,6 +7,7 @@ members = [
|
|||
"zokrates_stdlib",
|
||||
"zokrates_embed",
|
||||
"zokrates_abi",
|
||||
"zokrates_js",
|
||||
"zokrates_test",
|
||||
"zokrates_core_test",
|
||||
]
|
|
@ -1,6 +1,7 @@
|
|||
mod bn128;
|
||||
|
||||
use zokrates_field::field::FieldPrime;
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
|
||||
pub use self::bn128::G16;
|
||||
#[cfg(feature = "libsnark")]
|
||||
|
@ -21,6 +22,18 @@ impl SetupKeypair {
|
|||
}
|
||||
}
|
||||
|
||||
impl Serialize for SetupKeypair {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut s = serializer.serialize_struct("SetupKeypair", 2)?;
|
||||
s.serialize_field("vk", &self.vk)?;
|
||||
s.serialize_field("pk", &self.pk)?;
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ProofSystem {
|
||||
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair;
|
||||
|
||||
|
|
32
zokrates_js/.circleci/config.yml
Normal file
32
zokrates_js/.circleci/config.yml
Normal file
|
@ -0,0 +1,32 @@
|
|||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: blockchainit/rust-wasm-env:latest
|
||||
working_directory: ~/zokrates-js
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
name: Generate cargo lockfile
|
||||
command: cargo generate-lockfile
|
||||
- restore_cache:
|
||||
keys:
|
||||
- v1-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}
|
||||
- run:
|
||||
name: Run build
|
||||
command: npm run build
|
||||
- save_cache:
|
||||
paths:
|
||||
- /usr/local/cargo/registry
|
||||
- target/debug/.fingerprint
|
||||
- target/debug/build
|
||||
- target/debug/deps
|
||||
key: v1-cargo-cache-{{ arch }}-{{ checksum "Cargo.lock" }}
|
||||
workflows:
|
||||
version: 2
|
||||
build:
|
||||
jobs:
|
||||
- build:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
7
zokrates_js/.gitignore
vendored
Normal file
7
zokrates_js/.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
node_modules
|
||||
/dist
|
||||
/target
|
||||
/pkg
|
||||
/wasm-pack.log
|
||||
/stdlib
|
||||
stdlib.json
|
1175
zokrates_js/Cargo.lock
generated
Normal file
1175
zokrates_js/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
20
zokrates_js/Cargo.toml
Normal file
20
zokrates_js/Cargo.toml
Normal file
|
@ -0,0 +1,20 @@
|
|||
[package]
|
||||
name = "zokrates_js"
|
||||
version = "1.0.0"
|
||||
authors = ["Darko Macesic"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
serde = "^1.0.59"
|
||||
serde_derive = "^1.0.59"
|
||||
bincode = "1.1.4"
|
||||
wasm-bindgen = { version = "0.2.46", features = ["serde-serialize"] }
|
||||
zokrates_core = { path = "../zokrates_core", features = ["wasm"] }
|
||||
zokrates_field = { path = "../zokrates_field" }
|
||||
zokrates_abi = { path = "../zokrates_abi" }
|
||||
|
||||
[target."cfg(debug_assertions)".dependencies]
|
||||
console_error_panic_hook = "0.1.5"
|
7
zokrates_js/Dockerfile
Normal file
7
zokrates_js/Dockerfile
Normal file
|
@ -0,0 +1,7 @@
|
|||
FROM blockchainit/rust-wasm-env:latest
|
||||
|
||||
ARG WORKSPACE_DIR=/build
|
||||
WORKDIR ${WORKSPACE_DIR}
|
||||
|
||||
COPY . .
|
||||
RUN npm run build
|
22
zokrates_js/Dockerfile.env
Normal file
22
zokrates_js/Dockerfile.env
Normal file
|
@ -0,0 +1,22 @@
|
|||
FROM node:10
|
||||
SHELL ["/bin/bash", "-c"]
|
||||
|
||||
ENV RUSTUP_HOME=/usr/local/rustup \
|
||||
CARGO_HOME=/usr/local/cargo \
|
||||
PATH=/usr/local/cargo/bin:$PATH
|
||||
|
||||
RUN set -eux; \
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends \
|
||||
ca-certificates \
|
||||
gcc \
|
||||
libc6-dev \
|
||||
; \
|
||||
\
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain nightly-2019-08-09; \
|
||||
chmod -R a+w $RUSTUP_HOME $CARGO_HOME; \
|
||||
rustup --version; \
|
||||
cargo --version; \
|
||||
rustc --version;
|
||||
|
||||
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -y
|
165
zokrates_js/LICENSE
Normal file
165
zokrates_js/LICENSE
Normal file
|
@ -0,0 +1,165 @@
|
|||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
58
zokrates_js/README.md
Normal file
58
zokrates_js/README.md
Normal file
|
@ -0,0 +1,58 @@
|
|||
# zokrates-js
|
||||
JavaScript bindings for [ZoKrates](https://github.com/Zokrates/ZoKrates) project. The goal of this project is to provide ZoKrates JavaScript API supporting both node.js and the web. ZoKrates is a toolbox for zkSNARKs on Ethereum that helps you use verifiable computation in your DApp, from the specification of your program in a high level language to generating proofs of computation and verifying those proofs in Solidity.
|
||||
|
||||
[](https://circleci.com/gh/Zokrates/zokrates-js/tree/master)
|
||||
|
||||
## Package
|
||||
Install zokrates-js with [npm](https://www.npmjs.com/package/zokrates-js):
|
||||
|
||||
```bash
|
||||
npm install zokrates-js
|
||||
```
|
||||
|
||||
## API
|
||||
| Function | Description |
|
||||
| ------ | ------ |
|
||||
| initialize | Loads binding wasm module and sets necessary callbacks |
|
||||
| compile | Compiles source code into ZoKrates internal representation of arithmetic circuits |
|
||||
| computeWitness | Computes a valid assignment of the variables, which include the results of the computation |
|
||||
| setup | Generates a trusted setup for the compiled program |
|
||||
| exportSolidityVerifier | Generates a Solidity contract which contains the generated verification key and a public function to verify a solution to the compiled program |
|
||||
| generateProof | Generates a proof for a computation of the compiled program |
|
||||
|
||||
### Usage
|
||||
```js
|
||||
import * as zokrates from 'zokrates-js'
|
||||
|
||||
function importResolver(location, path) {
|
||||
// implement your resolving logic here
|
||||
return {
|
||||
source: "def main() -> (): return",
|
||||
location: path
|
||||
};
|
||||
}
|
||||
|
||||
zokrates.initialize(importResolver).then(() => {
|
||||
// we have to initialize wasm module before calling api functions
|
||||
zokrates.compile("def main(private field a) -> (field): return a", "main")
|
||||
});
|
||||
```
|
||||
|
||||
## Installation
|
||||
Install rustup and wasm-pack:
|
||||
|
||||
```bash
|
||||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
|
||||
In order to compile this project you need the *nightly* version of Rust. You can install all of this by running:
|
||||
|
||||
```bash
|
||||
rustup install nightly
|
||||
rustup target add wasm32-unknown-unknown --toolchain nightly
|
||||
rustup default nightly
|
||||
```
|
||||
|
||||
## Development
|
||||
Anyone is welcome to help progress and improve this library. Tasks and issues can be found in the [issues tab](https://github.com/Zokrates/ZoKrates/issues). If your problem/task is not in the tasks, feel free to create a new issue explaining your problem/task.
|
24
zokrates_js/gulpfile.js
Normal file
24
zokrates_js/gulpfile.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
/** general */
|
||||
const gulp = require('gulp');
|
||||
const dree = require('dree');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
/** stdlib constants */
|
||||
const stdlibRoot = '../zokrates_stdlib/stdlib';
|
||||
const output = 'stdlib.json';
|
||||
|
||||
const options = {
|
||||
extensions: ['zok']
|
||||
};
|
||||
|
||||
gulp.task('stdlib', function (done) {
|
||||
var stdlib = {};
|
||||
dree.scan(stdlibRoot, options, function (file) {
|
||||
const content = fs.readFileSync(file.path).toString();
|
||||
stdlib[file.relativePath] = content;
|
||||
});
|
||||
|
||||
fs.writeFileSync(path.resolve(__dirname, output), JSON.stringify(stdlib));
|
||||
done();
|
||||
});
|
17
zokrates_js/js/index.d.ts
vendored
Normal file
17
zokrates_js/js/index.d.ts
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
export interface ResolverResult {
|
||||
source: string,
|
||||
location: string
|
||||
}
|
||||
|
||||
export interface SetupKeypair {
|
||||
vk: string,
|
||||
pk: Uint8Array,
|
||||
}
|
||||
|
||||
export function initialize(callback: (location: string, path: string) => ResolverResult): Promise<void>;
|
||||
export function compile(source: string, location: string): Uint8Array;
|
||||
export function getStdLib(): object;
|
||||
export function computeWitness(program: Uint8Array, args: string[]): string;
|
||||
export function setup(program: Uint8Array): SetupKeypair;
|
||||
export function exportSolidityVerifier(verifyingKey: string, isAbiv2: boolean): string;
|
||||
export function generateProof(program: Uint8Array, witness: string, provingKey: Uint8Array): string;
|
68
zokrates_js/js/index.js
Normal file
68
zokrates_js/js/index.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
import { getAbsolutePath, appendExtension } from './utils';
|
||||
import stdlib from '../stdlib.json';
|
||||
|
||||
const __extension = '.zok';
|
||||
const __reserved = ['ecc/', 'signature/', 'hashes/', 'utils/'];
|
||||
var __state = {};
|
||||
|
||||
export async function initialize(callback) {
|
||||
let zokrates = await import('../pkg/index.js');
|
||||
__state.zokrates = zokrates;
|
||||
|
||||
window.resolve = function (location, path) {
|
||||
return __resolve(location, path, callback);
|
||||
}
|
||||
}
|
||||
|
||||
function __isReservedPath(path) {
|
||||
return __reserved.some(function (p) { return path.startsWith(p) || path.startsWith(p) })
|
||||
}
|
||||
|
||||
function __resolve(location, path, callback) {
|
||||
let result;
|
||||
if(__isReservedPath(location)) {
|
||||
result = resolveFromStdlib(location, path);
|
||||
} else {
|
||||
result = callback(location, path);
|
||||
}
|
||||
if (!result) return null;
|
||||
return __state.zokrates.ResolverResult.new(result.source, result.location);
|
||||
}
|
||||
|
||||
export function resolveFromStdlib(location, path) {
|
||||
let key = appendExtension(getAbsolutePath(location, path), __extension);
|
||||
let source = stdlib[key];
|
||||
return source ? { source, location: key } : null;
|
||||
}
|
||||
|
||||
export function getStdLib() {
|
||||
return stdlib;
|
||||
}
|
||||
|
||||
export function compile(source, location) {
|
||||
if (typeof __state.zokrates === 'undefined') {
|
||||
throw new Error("You must call initialize() before calling this method")
|
||||
}
|
||||
return Uint8Array.from(__state.zokrates.compile(source, location));
|
||||
}
|
||||
|
||||
export function computeWitness(program, args) {
|
||||
const input = JSON.stringify(Array.from(args));
|
||||
return __state.zokrates.compute_witness(Array.from(program), input);
|
||||
}
|
||||
|
||||
export function setup(program) {
|
||||
let result = __state.zokrates.setup(Array.from(program));
|
||||
return {
|
||||
vk: result.vk,
|
||||
pk: Uint8Array.from(result.pk)
|
||||
};
|
||||
}
|
||||
|
||||
export function exportSolidityVerifier(verifyingKey, isAbiv2) {
|
||||
return __state.zokrates.export_solidity_verifier(verifyingKey, isAbiv2);
|
||||
}
|
||||
|
||||
export function generateProof(program, witness, provingKey) {
|
||||
return __state.zokrates.generate_proof(Array.from(program), witness, Array.from(provingKey));
|
||||
}
|
24
zokrates_js/js/utils.js
Normal file
24
zokrates_js/js/utils.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
export function getAbsolutePath(basePath, relativePath) {
|
||||
var stack = basePath.split('/');
|
||||
var chunks = relativePath.split('/');
|
||||
|
||||
stack.pop();
|
||||
|
||||
for(var i = 0; i < chunks.length; i++) {
|
||||
if (chunks[i] == ".") {
|
||||
continue;
|
||||
} else if (chunks[i] == "..") {
|
||||
stack.pop();
|
||||
} else {
|
||||
stack.push(chunks[i]);
|
||||
}
|
||||
}
|
||||
return stack.join('/');
|
||||
}
|
||||
|
||||
export function appendExtension(path, extension) {
|
||||
if (path.endsWith(extension)) {
|
||||
return path;
|
||||
}
|
||||
return path.concat(extension);
|
||||
}
|
4241
zokrates_js/package-lock.json
generated
Normal file
4241
zokrates_js/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
37
zokrates_js/package.json
Normal file
37
zokrates_js/package.json
Normal file
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"name": "zokrates-js",
|
||||
"main": "js/index.js",
|
||||
"author": "Darko Macesic <darem966@gmail.com>, Edi Sinovcic <edi@blockchain-it.hr>",
|
||||
"version": "1.0.17",
|
||||
"keywords": [
|
||||
"zokrates",
|
||||
"wasm-bindgen",
|
||||
"zksnarks"
|
||||
],
|
||||
"license": "GPLv3",
|
||||
"files": [
|
||||
"js",
|
||||
"pkg",
|
||||
"stdlib.json"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/blockchain-it-hr/zokrates-js"
|
||||
},
|
||||
"types": "js/index.d.ts",
|
||||
"scripts": {
|
||||
"cargo:build": "cargo build --target=wasm32-unknown-unknown",
|
||||
"wasm-pack:build": "wasm-pack build --out-name index --release --target bundler",
|
||||
"prebuild": "npm install && rimraf pkg && gulp stdlib",
|
||||
"build": "npm run wasm-pack:build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"dree": "^2.1.10",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-cli": "^2.2.0",
|
||||
"rimraf": "^3.0.0",
|
||||
"serve": "^11.2.0",
|
||||
"text-encoding": "^0.7.0"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
123
zokrates_js/src/lib.rs
Normal file
123
zokrates_js/src/lib.rs
Normal file
|
@ -0,0 +1,123 @@
|
|||
use wasm_bindgen::prelude::*;
|
||||
use zokrates_core::compile::{ compile as compile_core, CompileErrors };
|
||||
use zokrates_core::ir;
|
||||
use zokrates_field::field::{ FieldPrime };
|
||||
use zokrates_core::proof_system::{self, ProofSystem};
|
||||
use zokrates_abi::{ parse_strict, Inputs, Encode };
|
||||
|
||||
extern crate serde_derive;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct ResolverResult {
|
||||
source: String,
|
||||
location: String,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl ResolverResult {
|
||||
#[wasm_bindgen]
|
||||
pub fn new(source: String, location: String) -> Self {
|
||||
ResolverResult { source, location }
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
fn resolve(l: String, p: String) -> Option<ResolverResult>;
|
||||
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
}
|
||||
|
||||
fn deserialize_program(input: &JsValue) -> ir::Prog<FieldPrime> {
|
||||
let vec: Vec<u8> = input.into_serde().unwrap();
|
||||
bincode::deserialize(&vec).unwrap()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn compile(source: JsValue, location: JsValue) -> Result<JsValue, JsValue> {
|
||||
fn resolve_closure<'a>(
|
||||
l: String,
|
||||
p: String,
|
||||
) -> Result<(String, String), zokrates_core::imports::Error> {
|
||||
let result = resolve(l, p.clone());
|
||||
match result {
|
||||
Some(res) => Ok((res.source, res.location)),
|
||||
None => Err(zokrates_core::imports::Error::new(String::from(format!("Unable to resolve {}", p))))
|
||||
}
|
||||
};
|
||||
|
||||
let program_flattened: Result<ir::Prog<FieldPrime>, CompileErrors> = compile_core(
|
||||
source.as_string().unwrap(),
|
||||
location.as_string().unwrap(),
|
||||
Some(resolve_closure)
|
||||
);
|
||||
|
||||
match program_flattened {
|
||||
Ok(p) => {
|
||||
let data: Vec<u8> = bincode::serialize(&p).unwrap();
|
||||
Ok(JsValue::from_serde(&data).unwrap())
|
||||
}
|
||||
Err(ce) => Err(JsValue::from_str(&format!("{}", ce)))
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn compute_witness(program: JsValue, args: JsValue) -> Result<JsValue, JsValue> {
|
||||
let program_flattened = deserialize_program(&program);
|
||||
|
||||
let input: String = args.as_string().unwrap();
|
||||
let signature = program_flattened.signature.clone();
|
||||
|
||||
let arguments = parse_strict(&input, signature.inputs)
|
||||
.map(|parsed| Inputs::Abi(parsed))
|
||||
.map_err(|why| why.to_string());
|
||||
|
||||
match arguments {
|
||||
Ok(inputs) => {
|
||||
let witness = program_flattened.execute(&inputs.encode());
|
||||
match witness {
|
||||
Ok(witness) => Ok(JsValue::from_str(&format!("{}", witness))),
|
||||
Err(error) => Err(JsValue::from_str(&format!("Execution failed: {}", error)))
|
||||
}
|
||||
},
|
||||
Err(err) => Err(JsValue::from_str(&format!("{}", err)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn setup(program: JsValue) -> JsValue {
|
||||
let program_flattened = deserialize_program(&program);
|
||||
let proof = proof_system::G16{}.setup(program_flattened);
|
||||
JsValue::from_serde(&proof).unwrap()
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn export_solidity_verifier(vk: JsValue, is_abiv2: JsValue) -> JsValue {
|
||||
let verifier = proof_system::G16{}.export_solidity_verifier(
|
||||
vk.as_string().unwrap(),
|
||||
is_abiv2.as_bool().unwrap()
|
||||
);
|
||||
JsValue::from_str(verifier.as_str())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn generate_proof(program: JsValue, witness: JsValue, pk: JsValue) -> JsValue {
|
||||
let program_flattened = deserialize_program(&program);
|
||||
let str_witness: String = witness.as_string().unwrap();
|
||||
let witness_out: ir::Witness<FieldPrime> = ir::Witness::read(str_witness.as_bytes()).unwrap();
|
||||
let proving_key: Vec<u8> = pk.into_serde().unwrap();
|
||||
|
||||
let proof: String = proof_system::G16{}.generate_proof(program_flattened, witness_out, proving_key);
|
||||
JsValue::from_str(proof.as_str())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn main_js() -> Result<(), JsValue> {
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue