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

Merge pull request #752 from dark64/zokrates-js-hotfix

Fix serialization issue in zokrates.js
This commit is contained in:
Thibaut Schaeffer 2021-03-04 18:35:43 +01:00 committed by GitHub
commit 2019803d20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 29 deletions

View file

@ -1,6 +1,6 @@
{
"name": "zokrates-js",
"version": "1.0.27",
"version": "1.0.28",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View file

@ -2,7 +2,7 @@
"name": "zokrates-js",
"main": "index.js",
"author": "Darko Macesic <darem966@gmail.com>",
"version": "1.0.27",
"version": "1.0.28",
"keywords": [
"zokrates",
"wasm-bindgen",

View file

@ -36,8 +36,8 @@ pub struct ComputationResult {
}
#[inline]
fn deserialize_program(value: &Vec<u8>) -> Result<ir::Prog<Bn128Field>, JsValue> {
deserialize(&value)
fn deserialize_program(value: &[u8]) -> Result<ir::Prog<Bn128Field>, JsValue> {
deserialize(value)
.map_err(|err| JsValue::from_str(&format!("Could not deserialize program: {}", err)))
}
@ -125,11 +125,9 @@ pub fn compile(
}
#[wasm_bindgen]
pub fn compute_witness(artifacts: JsValue, args: JsValue) -> Result<JsValue, JsValue> {
let result: CompilationResult = artifacts.into_serde().unwrap();
let program_flattened = deserialize_program(&result.program)?;
let abi: Abi = serde_json::from_str(result.abi.as_str())
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())
.map_err(|err| JsValue::from_str(&format!("Could not deserialize abi: {}", err)))?;
let signature: Signature = abi.signature();
@ -157,9 +155,8 @@ pub fn compute_witness(artifacts: JsValue, args: JsValue) -> Result<JsValue, JsV
}
#[wasm_bindgen]
pub fn setup(program: JsValue) -> Result<JsValue, JsValue> {
let input: Vec<u8> = program.into_serde().unwrap();
let program_flattened = deserialize_program(&input)?;
pub fn setup(program: &[u8]) -> Result<JsValue, JsValue> {
let program_flattened = deserialize_program(program)?;
let keypair = <Bellman as Backend<Bn128Field, G16>>::setup(program_flattened);
Ok(JsValue::from_serde(&keypair).unwrap())
}
@ -178,19 +175,17 @@ pub fn export_solidity_verifier(vk: JsValue, abi_version: JsValue) -> Result<JsV
}
#[wasm_bindgen]
pub fn generate_proof(program: JsValue, witness: JsValue, pk: JsValue) -> Result<JsValue, JsValue> {
let input: Vec<u8> = program.into_serde().unwrap();
let program_flattened = deserialize_program(&input)?;
pub fn generate_proof(program: &[u8], witness: JsValue, pk: &[u8]) -> Result<JsValue, JsValue> {
let program_flattened = deserialize_program(program)?;
let str_witness = witness.as_string().unwrap();
let ir_witness: ir::Witness<Bn128Field> = ir::Witness::read(str_witness.as_bytes())
.map_err(|err| JsValue::from_str(&format!("Could not read witness: {}", err)))?;
let proving_key: Vec<u8> = pk.into_serde().unwrap();
let proof = <Bellman as Backend<Bn128Field, G16>>::generate_proof(
program_flattened,
ir_witness,
proving_key,
pk.to_vec(),
);
Ok(JsValue::from_serde(&proof).unwrap())

View file

@ -18,11 +18,11 @@ describe('tests', function() {
assert.ok(artifacts !== undefined);
})
});
it('should throw on invalid code', function() {
assert.throws(() => this.zokrates.compile(":-)"));
});
it('should resolve stdlib module', function() {
const stdlib = require('../stdlib.json');
assert.doesNotThrow(() => {
@ -30,7 +30,7 @@ describe('tests', function() {
this.zokrates.compile(code);
});
});
it('should resolve user module', function() {
assert.doesNotThrow(() => {
const code = 'import "test" as test\ndef main() -> field: return test()';
@ -59,10 +59,10 @@ describe('tests', function() {
assert.doesNotThrow(() => {
const code = 'def main(private field a) -> field: return a * a';
const artifacts = this.zokrates.compile(code);
const result = this.zokrates.computeWitness(artifacts, ["2"]);
const output = JSON.parse(result.output);
assert.deepEqual(output, ["4"]);
});
});
@ -71,7 +71,7 @@ describe('tests', function() {
assert.throws(() => {
const code = 'def main(private field a) -> field: return a * a';
const artifacts = this.zokrates.compile(code);
this.zokrates.computeWitness(artifacts, ["1", "2"]);
});
});
@ -80,7 +80,7 @@ describe('tests', function() {
assert.throws(() => {
const code = 'def main(private field a) -> field: return a * a';
const artifacts = this.zokrates.compile(code);
this.zokrates.computeWitness(artifacts, [true]);
});
});
@ -91,7 +91,7 @@ describe('tests', function() {
assert.doesNotThrow(() => {
const code = 'def main(private field a) -> field: return a * a';
const artifacts = this.zokrates.compile(code);
this.zokrates.setup(artifacts.program);
});
});
@ -137,6 +137,7 @@ describe('tests', function() {
assert(this.zokrates.verify(keypair.vk, proof) == true);
})
});
it('should fail', function() {
assert.doesNotThrow(() => {
const code = 'def main(private field a) -> field: return a * a';

View file

@ -46,7 +46,7 @@ module.exports = (dep) => {
};
const { program, abi } = zokrates.compile(source, location, callback, createConfig(config));
return {
program: Array.from(program),
program: new Uint8Array(program),
abi
}
},
@ -54,11 +54,12 @@ module.exports = (dep) => {
const { vk, pk } = zokrates.setup(program);
return {
vk,
pk: Array.from(pk)
pk: new Uint8Array(pk)
};
},
computeWitness: (artifacts, args) => {
return zokrates.compute_witness(artifacts, JSON.stringify(Array.from(args)));
const { program, abi } = artifacts;
return zokrates.compute_witness(program, abi, JSON.stringify(Array.from(args)));
},
exportSolidityVerifier: (verificationKey, abiVersion) => {
return zokrates.export_solidity_verifier(verificationKey, abiVersion);