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

Merge pull request #1083 from Zokrates/optimize-js-bindings

Optimize bindings in zokrates-js causing encoding and OOM issues
This commit is contained in:
Thibaut Schaeffer 2022-01-12 16:21:58 +01:00 committed by GitHub
commit 6526acfa5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 183 additions and 6575 deletions

View file

@ -110,7 +110,7 @@ jobs:
command: WITH_LIBSNARK=1 RUSTFLAGS="-D warnings" ./integration_test.sh
deploy:
docker:
- image: circleci/python:latest-node
- image: cimg/python:3.8-node
steps:
- checkout
- setup_remote_docker:

View file

@ -32,13 +32,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain none -y \
&& rustup toolchain install $RUST_VERSION --allow-downgrade --profile minimal --component rustfmt clippy \
&& cargo install --git https://github.com/rustwasm/wasm-pack \
&& rm -rf /usr/local/cargo/registry \
&& curl -sL https://deb.nodesource.com/setup_lts.x | bash - && apt-get install -y nodejs && npm i -g solc \
&& curl -sL https://raw.githubusercontent.com/Sarcasm/run-clang-format/master/run-clang-format.py > /opt/run-clang-format.py \
&& chmod +x /opt/run-clang-format.py \
&& ln -s /opt/run-clang-format.py /usr/bin \
&& rustup --version; cargo --version; rustc --version; wasm-pack --version; echo nodejs $(node -v);
&& rustup --version; cargo --version; rustc --version; echo nodejs $(node -v);
RUN cd /opt && curl -LO https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz \
&& tar -xzf geckodriver-v0.28.0-linux64.tar.gz geckodriver \

View file

@ -0,0 +1 @@
Fix out of memory issues in `zokrates-js`

View file

@ -1,7 +1,7 @@
use crate::typed_absy::types::{ConcreteSignature, ConcreteType};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct AbiInput {
pub name: String,
pub public: bool,
@ -11,7 +11,7 @@ pub struct AbiInput {
pub type AbiOutput = ConcreteType;
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Abi {
pub inputs: Vec<AbiInput>,
pub outputs: Vec<AbiOutput>,

10
zokrates_js/Cargo.lock generated
View file

@ -1712,7 +1712,7 @@ version = "0.1.0"
[[package]]
name = "zokrates_core"
version = "0.6.8"
version = "0.6.9"
dependencies = [
"bellman_ce",
"cfg-if 0.1.10",
@ -1742,7 +1742,7 @@ dependencies = [
[[package]]
name = "zokrates_embed"
version = "0.1.5"
version = "0.1.6"
dependencies = [
"ark-bls12-377",
"ark-bw6-761",
@ -1760,7 +1760,7 @@ dependencies = [
[[package]]
name = "zokrates_field"
version = "0.4.0"
version = "0.5.0"
dependencies = [
"ark-bls12-377",
"ark-bls12-381",
@ -1783,7 +1783,7 @@ dependencies = [
[[package]]
name = "zokrates_js"
version = "1.0.37"
version = "1.0.38"
dependencies = [
"console_error_panic_hook",
"js-sys",
@ -1807,7 +1807,7 @@ dependencies = [
[[package]]
name = "zokrates_pest_ast"
version = "0.2.4"
version = "0.2.5"
dependencies = [
"from-pest",
"lazy_static",

View file

@ -4,5 +4,5 @@ COPY . src
RUN cd src/zokrates_js && npm run build && rm -rf target/
RUN cd / && mkdir build \
&& mv src/zokrates_js/* /build \
&& cp -rf src/zokrates_js/* /build/ \
&& rm -rf src

View file

@ -48,9 +48,14 @@ declare module 'zokrates-js' {
output: string
}
export interface Abi {
inputs: Array<any>,
outputs: Array<any>
}
export interface CompilationArtifacts {
program: Uint8Array,
abi: string,
abi: Abi,
}
export interface SetupKeypair {

File diff suppressed because it is too large Load diff

View file

@ -31,10 +31,11 @@
"build:bundler:dev": "rimraf pkg && npm run wasm-pack -- --target bundler --dev && npm run clean-pkg",
"build:node": "rimraf node/pkg && npm run wasm-pack -- --target nodejs -d node/pkg --release && npm run clean-node-pkg",
"build:node:dev": "rimraf node/pkg && npm run wasm-pack -- --target nodejs -d node/pkg --dev && npm run clean-node-pkg",
"clean-pkg": "find pkg/* | grep -P \"^.*\\.(md|d\\.ts)$\" | xargs rm",
"clean-node-pkg": "find node/pkg/* | grep -P \"^.*\\.(md|d\\.ts)$\" | xargs rm",
"clean-pkg": "rimraf pkg/README.md",
"clean-node-pkg": "rimraf node/pkg/README.md",
"pretest": "npm run setup && npm run build:node",
"test": "mocha --require esm --recursive tests"
"test": "npm run run-tests",
"run-tests": "mocha --require esm --recursive tests"
},
"devDependencies": {
"dree": "^2.6.1",
@ -45,7 +46,8 @@
"rimraf": "^3.0.2",
"serve": "^11.3.2",
"text-encoding": "^0.7.0",
"toml": "^3.0.0"
"toml": "^3.0.0",
"wasm-pack": "^0.10.2"
},
"dependencies": {}
}

View file

@ -20,18 +20,30 @@ use zokrates_core::typed_absy::abi::Abi;
use zokrates_core::typed_absy::types::ConcreteSignature as Signature;
use zokrates_field::Bn128Field;
#[wasm_bindgen]
pub struct CompilationResult {
program: Vec<u8>,
abi: Abi,
}
#[wasm_bindgen]
impl CompilationResult {
pub fn program(&self) -> js_sys::Uint8Array {
let arr = js_sys::Uint8Array::new_with_length(self.program.len() as u32);
arr.copy_from(&self.program);
arr
}
pub fn abi(&self) -> JsValue {
JsValue::from_serde(&self.abi).unwrap()
}
}
#[derive(Serialize, Deserialize)]
pub struct ResolverResult {
source: String,
location: String,
}
#[derive(Serialize, Deserialize)]
pub struct CompilationResult {
program: Vec<u8>,
abi: String,
}
#[derive(Serialize, Deserialize)]
pub struct ComputationResult {
witness: String,
@ -104,7 +116,7 @@ pub fn compile(
location: JsValue,
resolve_callback: &js_sys::Function,
config: JsValue,
) -> Result<JsValue, JsValue> {
) -> Result<CompilationResult, JsValue> {
let resolver = JsResolver::new(resolve_callback);
let config: CompileConfig = config.into_serde().unwrap_or_default();
@ -129,18 +141,17 @@ pub fn compile(
)
})?;
let result = CompilationResult {
abi: to_string_pretty(artifacts.abi()).unwrap(),
Ok(CompilationResult {
abi: artifacts.abi().clone(),
program: serialize_program(artifacts.prog()),
};
Ok(JsValue::from_serde(&result).unwrap())
})
}
#[wasm_bindgen]
pub fn compute_witness(program: &[u8], abi: JsValue, args: JsValue) -> Result<JsValue, JsValue> {
let program_flattened = deserialize_program(program)?;
let abi: Abi = serde_json::from_str(abi.as_string().unwrap().as_str())
let abi: Abi = abi
.into_serde()
.map_err(|err| JsValue::from_str(&format!("Could not deserialize abi: {}", err)))?;
let signature: Signature = abi.signature();

View file

@ -40,10 +40,10 @@ module.exports = (dep) => {
const callback = (currentLocation, importLocation) => {
return resolveFromStdlib(currentLocation, importLocation) || resolveCallback(currentLocation, importLocation);
};
const { program, abi } = zokrates.compile(source, location, callback, config);
const ptr = zokrates.compile(source, location, callback, config);
return {
program: new Uint8Array(program),
abi
program: ptr.program(),
abi: ptr.abi()
}
},
setup: (program) => {