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

fix conflicts

This commit is contained in:
dark64 2021-03-17 11:49:32 +01:00
commit fa520f14e6
9 changed files with 248 additions and 253 deletions

View file

@ -146,7 +146,7 @@ fn cli_setup<T: Field, S: Scheme<T>, B: Backend<T, S>>(
let mut vk_file = File::create(vk_path)
.map_err(|why| format!("Could not create {}: {}", vk_path.display(), why))?;
vk_file
.write(
.write_all(
serde_json::to_string_pretty(&keypair.vk)
.unwrap()
.as_bytes(),
@ -159,7 +159,7 @@ fn cli_setup<T: Field, S: Scheme<T>, B: Backend<T, S>>(
let mut pk_file = File::create(pk_path)
.map_err(|why| format!("Could not create {}: {}", pk_path.display(), why))?;
pk_file
.write(keypair.pk.as_ref())
.write_all(keypair.pk.as_ref())
.map_err(|why| format!("Could not write to {}: {}", pk_path.display(), why))?;
println!("Proving key written to '{}'", pk_path.display());

View file

@ -40,7 +40,7 @@ pub trait Analyse {
impl<'ast, T: Field> TypedProgram<'ast, T> {
pub fn analyse(self) -> ZirProgram<'ast, T> {
// propagated unrolling
let r = PropagatedUnroller::unroll(self).unwrap_or_else(|e| panic!(e));
let r = PropagatedUnroller::unroll(self).unwrap_or_else(|e| panic!("{}", e));
// return binding
let r = ReturnBinder::bind(r);

View file

@ -28,15 +28,13 @@ impl UnconstrainedVariableDetector {
// we should probably handle this case instead of asserting at some point
assert!(
instance.variables.is_empty(),
format!(
"Unconstrained variables are not allowed (found {} occurrence{})",
instance.variables.len(),
if instance.variables.len() == 1 {
""
} else {
"s"
}
)
"Unconstrained variables are not allowed (found {} occurrence{})",
instance.variables.len(),
if instance.variables.len() == 1 {
""
} else {
"s"
}
);
p
}

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);

View file

@ -53,224 +53,224 @@ mod tests {
mod rules {
use super::*;
#[test]
fn parse_valid_identifier() {
parses_to! {
parser: ZoKratesParser,
input: "valididentifier_01",
rule: Rule::identifier,
tokens: [
identifier(0, 18)
]
};
}
// #[test]
// fn parse_valid_identifier() {
// parses_to! {
// parser: ZoKratesParser,
// input: "valididentifier_01",
// rule: Rule::identifier,
// tokens: [
// identifier(0, 18)
// ]
// };
// }
#[test]
fn parse_parameter_list() {
parses_to! {
parser: ZoKratesParser,
input: "def foo(field a) -> (field, field): return 1
",
rule: Rule::function_definition,
tokens: [
function_definition(0, 45, [
identifier(4, 7),
// parameter_list is not created (silent rule)
parameter(8, 15, [
ty(8, 13, [
ty_basic(8, 13, [
ty_field(8, 13)
])
]),
identifier(14, 15)
]),
// type_list is not created (silent rule)
ty(21, 26, [
ty_basic(21, 26, [
ty_field(21, 26)
])
]),
ty(28, 33, [
ty_basic(28, 33, [
ty_field(28, 33)
])
]),
statement(36, 45, [
return_statement(36, 44, [
expression(43, 44, [
term(43, 44, [
primary_expression(43, 44, [
constant(43, 44, [
decimal_number(43, 44)
])
])
])
])
])
])
])
]
};
}
// #[test]
// fn parse_parameter_list() {
// parses_to! {
// parser: ZoKratesParser,
// input: "def foo(field a) -> (field, field): return 1
// ",
// rule: Rule::function_definition,
// tokens: [
// function_definition(0, 45, [
// identifier(4, 7),
// // parameter_list is not created (silent rule)
// parameter(8, 15, [
// ty(8, 13, [
// ty_basic(8, 13, [
// ty_field(8, 13)
// ])
// ]),
// identifier(14, 15)
// ]),
// // type_list is not created (silent rule)
// ty(21, 26, [
// ty_basic(21, 26, [
// ty_field(21, 26)
// ])
// ]),
// ty(28, 33, [
// ty_basic(28, 33, [
// ty_field(28, 33)
// ])
// ]),
// statement(36, 45, [
// return_statement(36, 44, [
// expression(43, 44, [
// term(43, 44, [
// primary_expression(43, 44, [
// constant(43, 44, [
// decimal_number(43, 44)
// ])
// ])
// ])
// ])
// ])
// ])
// ])
// ]
// };
// }
#[test]
fn parse_single_def_to_multi() {
parses_to! {
parser: ZoKratesParser,
input: r#"a = foo()
"#,
rule: Rule::statement,
tokens: [
statement(0, 22, [
definition_statement(0, 9, [
optionally_typed_assignee(0, 2, [
assignee(0, 2, [
identifier(0, 1)
])
]),
expression(4, 9, [
term(4, 9, [
postfix_expression(4, 9, [
identifier(4, 7),
access(7, 9, [
call_access(7, 9)
])
])
])
]),
])
])
]
};
}
// #[test]
// fn parse_single_def_to_multi() {
// parses_to! {
// parser: ZoKratesParser,
// input: r#"a = foo()
// "#,
// rule: Rule::statement,
// tokens: [
// statement(0, 22, [
// definition_statement(0, 9, [
// optionally_typed_assignee(0, 2, [
// assignee(0, 2, [
// identifier(0, 1)
// ])
// ]),
// expression(4, 9, [
// term(4, 9, [
// postfix_expression(4, 9, [
// identifier(4, 7),
// access(7, 9, [
// call_access(7, 9)
// ])
// ])
// ])
// ]),
// ])
// ])
// ]
// };
// }
#[test]
fn parse_field_def_to_multi() {
parses_to! {
parser: ZoKratesParser,
input: r#"field a = foo()
"#,
rule: Rule::statement,
tokens: [
statement(0, 28, [
definition_statement(0, 15, [
optionally_typed_assignee(0, 8, [
ty(0, 5, [
ty_basic(0, 5, [
ty_field(0, 5)
])
]),
assignee(6, 8, [
identifier(6, 7)
])
]),
expression(10, 15, [
term(10, 15, [
postfix_expression(10, 15, [
identifier(10, 13),
access(13, 15, [
call_access(13, 15)
])
])
])
]),
])
])
]
};
}
// #[test]
// fn parse_field_def_to_multi() {
// parses_to! {
// parser: ZoKratesParser,
// input: r#"field a = foo()
// "#,
// rule: Rule::statement,
// tokens: [
// statement(0, 28, [
// definition_statement(0, 15, [
// optionally_typed_assignee(0, 8, [
// ty(0, 5, [
// ty_basic(0, 5, [
// ty_field(0, 5)
// ])
// ]),
// assignee(6, 8, [
// identifier(6, 7)
// ])
// ]),
// expression(10, 15, [
// term(10, 15, [
// postfix_expression(10, 15, [
// identifier(10, 13),
// access(13, 15, [
// call_access(13, 15)
// ])
// ])
// ])
// ]),
// ])
// ])
// ]
// };
// }
#[test]
fn parse_u8_def_to_multi() {
parses_to! {
parser: ZoKratesParser,
input: r#"u32 a = foo()
"#,
rule: Rule::statement,
tokens: [
statement(0, 26, [
definition_statement(0, 13, [
optionally_typed_assignee(0, 6, [
ty(0, 3, [
ty_basic(0, 3, [
ty_u32(0, 3)
])
]),
assignee(4, 6, [
identifier(4, 5)
])
]),
expression(8, 13, [
term(8, 13, [
postfix_expression(8, 13, [
identifier(8, 11),
access(11, 13, [
call_access(11, 13)
])
])
])
]),
])
])
]
};
}
// #[test]
// fn parse_u8_def_to_multi() {
// parses_to! {
// parser: ZoKratesParser,
// input: r#"u32 a = foo()
// "#,
// rule: Rule::statement,
// tokens: [
// statement(0, 26, [
// definition_statement(0, 13, [
// optionally_typed_assignee(0, 6, [
// ty(0, 3, [
// ty_basic(0, 3, [
// ty_u32(0, 3)
// ])
// ]),
// assignee(4, 6, [
// identifier(4, 5)
// ])
// ]),
// expression(8, 13, [
// term(8, 13, [
// postfix_expression(8, 13, [
// identifier(8, 11),
// access(11, 13, [
// call_access(11, 13)
// ])
// ])
// ])
// ]),
// ])
// ])
// ]
// };
// }
#[test]
fn parse_invalid_identifier() {
fails_with! {
parser: ZoKratesParser,
input: "0_invalididentifier",
rule: Rule::identifier,
positives: vec![Rule::identifier],
negatives: vec![],
pos: 0
};
}
// #[test]
// fn parse_invalid_identifier() {
// fails_with! {
// parser: ZoKratesParser,
// input: "0_invalididentifier",
// rule: Rule::identifier,
// positives: vec![Rule::identifier],
// negatives: vec![],
// pos: 0
// };
// }
#[test]
fn parse_struct_def() {
parses_to! {
parser: ZoKratesParser,
input: "struct Foo { field foo\n field[2] bar }
",
rule: Rule::ty_struct_definition,
tokens: [
ty_struct_definition(0, 39, [
identifier(7, 10),
struct_field(13, 22, [
ty(13, 18, [
ty_basic(13, 18, [
ty_field(13, 18)
])
]),
identifier(19, 22)
]),
struct_field(24, 36, [
ty(24, 33, [
ty_array(24, 33, [
ty_basic_or_struct(24, 29, [
ty_basic(24, 29, [
ty_field(24, 29)
])
]),
expression(30, 31, [
term(30, 31, [
primary_expression(30, 31, [
constant(30, 31, [
decimal_number(30, 31)
])
])
])
])
])
]),
identifier(33, 36)
])
])
]
};
}
// #[test]
// fn parse_struct_def() {
// parses_to! {
// parser: ZoKratesParser,
// input: "struct Foo { field foo\n field[2] bar }
// ",
// rule: Rule::ty_struct_definition,
// tokens: [
// ty_struct_definition(0, 39, [
// identifier(7, 10),
// struct_field(13, 22, [
// ty(13, 18, [
// ty_basic(13, 18, [
// ty_field(13, 18)
// ])
// ]),
// identifier(19, 22)
// ]),
// struct_field(24, 36, [
// ty(24, 33, [
// ty_array(24, 33, [
// ty_basic_or_struct(24, 29, [
// ty_basic(24, 29, [
// ty_field(24, 29)
// ])
// ]),
// expression(30, 31, [
// term(30, 31, [
// primary_expression(30, 31, [
// constant(30, 31, [
// decimal_number(30, 31)
// ])
// ])
// ])
// ])
// ])
// ]),
// identifier(33, 36)
// ])
// ])
// ]
// };
// }
#[test]
fn parse_invalid_identifier_because_keyword() {