replace program input type from JsValue to u8 slice ref
This commit is contained in:
parent
09357e1e6b
commit
f30bd5319f
3 changed files with 24 additions and 27 deletions
|
@ -36,8 +36,8 @@ pub struct ComputationResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize_program(value: &Vec<u8>) -> Result<ir::Prog<Bn128Field>, JsValue> {
|
fn deserialize_program(value: &[u8]) -> Result<ir::Prog<Bn128Field>, JsValue> {
|
||||||
deserialize(&value)
|
deserialize(value)
|
||||||
.map_err(|err| JsValue::from_str(&format!("Could not deserialize program: {}", err)))
|
.map_err(|err| JsValue::from_str(&format!("Could not deserialize program: {}", err)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,11 +125,9 @@ pub fn compile(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn compute_witness(artifacts: JsValue, args: JsValue) -> Result<JsValue, JsValue> {
|
pub fn compute_witness(program: &[u8], abi: JsValue, args: JsValue) -> Result<JsValue, JsValue> {
|
||||||
let result: CompilationResult = artifacts.into_serde().unwrap();
|
let program_flattened = deserialize_program(program)?;
|
||||||
let program_flattened = deserialize_program(&result.program)?;
|
let abi: Abi = serde_json::from_str(abi.as_string().unwrap().as_str())
|
||||||
|
|
||||||
let abi: Abi = serde_json::from_str(result.abi.as_str())
|
|
||||||
.map_err(|err| JsValue::from_str(&format!("Could not deserialize abi: {}", err)))?;
|
.map_err(|err| JsValue::from_str(&format!("Could not deserialize abi: {}", err)))?;
|
||||||
|
|
||||||
let signature: Signature = abi.signature();
|
let signature: Signature = abi.signature();
|
||||||
|
@ -157,9 +155,8 @@ pub fn compute_witness(artifacts: JsValue, args: JsValue) -> Result<JsValue, JsV
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn setup(program: JsValue) -> Result<JsValue, JsValue> {
|
pub fn setup(program: &[u8]) -> Result<JsValue, JsValue> {
|
||||||
let input: Vec<u8> = program.into_serde().unwrap();
|
let program_flattened = deserialize_program(program)?;
|
||||||
let program_flattened = deserialize_program(&input)?;
|
|
||||||
let keypair = <Bellman as Backend<Bn128Field, G16>>::setup(program_flattened);
|
let keypair = <Bellman as Backend<Bn128Field, G16>>::setup(program_flattened);
|
||||||
Ok(JsValue::from_serde(&keypair).unwrap())
|
Ok(JsValue::from_serde(&keypair).unwrap())
|
||||||
}
|
}
|
||||||
|
@ -178,19 +175,17 @@ pub fn export_solidity_verifier(vk: JsValue, abi_version: JsValue) -> Result<JsV
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn generate_proof(program: JsValue, witness: JsValue, pk: JsValue) -> Result<JsValue, JsValue> {
|
pub fn generate_proof(program: &[u8], witness: JsValue, pk: &[u8]) -> Result<JsValue, JsValue> {
|
||||||
let input: Vec<u8> = program.into_serde().unwrap();
|
let program_flattened = deserialize_program(program)?;
|
||||||
let program_flattened = deserialize_program(&input)?;
|
|
||||||
|
|
||||||
let str_witness = witness.as_string().unwrap();
|
let str_witness = witness.as_string().unwrap();
|
||||||
|
|
||||||
let ir_witness: ir::Witness<Bn128Field> = ir::Witness::read(str_witness.as_bytes())
|
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)))?;
|
.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(
|
let proof = <Bellman as Backend<Bn128Field, G16>>::generate_proof(
|
||||||
program_flattened,
|
program_flattened,
|
||||||
ir_witness,
|
ir_witness,
|
||||||
proving_key,
|
pk.to_vec(),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(JsValue::from_serde(&proof).unwrap())
|
Ok(JsValue::from_serde(&proof).unwrap())
|
||||||
|
|
|
@ -18,11 +18,11 @@ describe('tests', function() {
|
||||||
assert.ok(artifacts !== undefined);
|
assert.ok(artifacts !== undefined);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw on invalid code', function() {
|
it('should throw on invalid code', function() {
|
||||||
assert.throws(() => this.zokrates.compile(":-)"));
|
assert.throws(() => this.zokrates.compile(":-)"));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve stdlib module', function() {
|
it('should resolve stdlib module', function() {
|
||||||
const stdlib = require('../stdlib.json');
|
const stdlib = require('../stdlib.json');
|
||||||
assert.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
|
@ -30,7 +30,7 @@ describe('tests', function() {
|
||||||
this.zokrates.compile(code);
|
this.zokrates.compile(code);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve user module', function() {
|
it('should resolve user module', function() {
|
||||||
assert.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
const code = 'import "test" as test\ndef main() -> field: return test()';
|
const code = 'import "test" as test\ndef main() -> field: return test()';
|
||||||
|
@ -59,10 +59,10 @@ describe('tests', function() {
|
||||||
assert.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
const code = 'def main(private field a) -> field: return a * a';
|
const code = 'def main(private field a) -> field: return a * a';
|
||||||
const artifacts = this.zokrates.compile(code);
|
const artifacts = this.zokrates.compile(code);
|
||||||
|
|
||||||
const result = this.zokrates.computeWitness(artifacts, ["2"]);
|
const result = this.zokrates.computeWitness(artifacts, ["2"]);
|
||||||
const output = JSON.parse(result.output);
|
const output = JSON.parse(result.output);
|
||||||
|
|
||||||
assert.deepEqual(output, ["4"]);
|
assert.deepEqual(output, ["4"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -71,7 +71,7 @@ describe('tests', function() {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
const code = 'def main(private field a) -> field: return a * a';
|
const code = 'def main(private field a) -> field: return a * a';
|
||||||
const artifacts = this.zokrates.compile(code);
|
const artifacts = this.zokrates.compile(code);
|
||||||
|
|
||||||
this.zokrates.computeWitness(artifacts, ["1", "2"]);
|
this.zokrates.computeWitness(artifacts, ["1", "2"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -80,7 +80,7 @@ describe('tests', function() {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
const code = 'def main(private field a) -> field: return a * a';
|
const code = 'def main(private field a) -> field: return a * a';
|
||||||
const artifacts = this.zokrates.compile(code);
|
const artifacts = this.zokrates.compile(code);
|
||||||
|
|
||||||
this.zokrates.computeWitness(artifacts, [true]);
|
this.zokrates.computeWitness(artifacts, [true]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -91,7 +91,7 @@ describe('tests', function() {
|
||||||
assert.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
const code = 'def main(private field a) -> field: return a * a';
|
const code = 'def main(private field a) -> field: return a * a';
|
||||||
const artifacts = this.zokrates.compile(code);
|
const artifacts = this.zokrates.compile(code);
|
||||||
|
|
||||||
this.zokrates.setup(artifacts.program);
|
this.zokrates.setup(artifacts.program);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -137,6 +137,7 @@ describe('tests', function() {
|
||||||
assert(this.zokrates.verify(keypair.vk, proof) == true);
|
assert(this.zokrates.verify(keypair.vk, proof) == true);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail', function() {
|
it('should fail', function() {
|
||||||
assert.doesNotThrow(() => {
|
assert.doesNotThrow(() => {
|
||||||
const code = 'def main(private field a) -> field: return a * a';
|
const code = 'def main(private field a) -> field: return a * a';
|
||||||
|
|
|
@ -46,7 +46,7 @@ module.exports = (dep) => {
|
||||||
};
|
};
|
||||||
const { program, abi } = zokrates.compile(source, location, callback, createConfig(config));
|
const { program, abi } = zokrates.compile(source, location, callback, createConfig(config));
|
||||||
return {
|
return {
|
||||||
program: Array.from(program),
|
program: new Uint8Array(program),
|
||||||
abi
|
abi
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -54,11 +54,12 @@ module.exports = (dep) => {
|
||||||
const { vk, pk } = zokrates.setup(program);
|
const { vk, pk } = zokrates.setup(program);
|
||||||
return {
|
return {
|
||||||
vk,
|
vk,
|
||||||
pk: Array.from(pk)
|
pk: new Uint8Array(pk)
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computeWitness: (artifacts, args) => {
|
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) => {
|
exportSolidityVerifier: (verificationKey, abiVersion) => {
|
||||||
return zokrates.export_solidity_verifier(verificationKey, abiVersion);
|
return zokrates.export_solidity_verifier(verificationKey, abiVersion);
|
||||||
|
|
Loading…
Reference in a new issue