fix circom signal ordering, add test
This commit is contained in:
parent
307bbac0e2
commit
1d62d7e6ab
3 changed files with 27 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
|||
use byteorder::{LittleEndian, WriteBytesExt};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::io::Result;
|
||||
use std::{io::Write, ops::Add};
|
||||
use zokrates_ast::flat::Variable;
|
||||
|
@ -65,6 +65,9 @@ pub fn r1cs_program<T: Field>(prog: Prog<T>) -> (Vec<Variable>, usize, Vec<Const
|
|||
// position where private part of witness starts
|
||||
let private_inputs_offset = variables.len();
|
||||
|
||||
// build a set of all variables
|
||||
let mut ordered_variables_set = BTreeSet::default();
|
||||
|
||||
// first pass through statements to populate `variables`
|
||||
for (quad, lin) in prog.statements.iter().filter_map(|s| match s {
|
||||
Statement::Constraint(quad, lin, _) => Some((quad, lin)),
|
||||
|
@ -72,16 +75,21 @@ pub fn r1cs_program<T: Field>(prog: Prog<T>) -> (Vec<Variable>, usize, Vec<Const
|
|||
Statement::Log(..) => None,
|
||||
}) {
|
||||
for (k, _) in &quad.left.0 {
|
||||
provide_variable_idx(&mut variables, k);
|
||||
ordered_variables_set.insert(k);
|
||||
}
|
||||
for (k, _) in &quad.right.0 {
|
||||
provide_variable_idx(&mut variables, k);
|
||||
ordered_variables_set.insert(k);
|
||||
}
|
||||
for (k, _) in &lin.0 {
|
||||
provide_variable_idx(&mut variables, k);
|
||||
ordered_variables_set.insert(k);
|
||||
}
|
||||
}
|
||||
|
||||
// create indices for the variables *in increasing order*
|
||||
for variable in ordered_variables_set {
|
||||
provide_variable_idx(&mut variables, variable);
|
||||
}
|
||||
|
||||
let mut constraints = vec![];
|
||||
|
||||
// second pass to convert program to raw sparse vectors
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
"dree": "^2.6.1",
|
||||
"mocha": "^9.2.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"snarkjs": "^0.4.19",
|
||||
"snarkjs": "^0.4.24",
|
||||
"wasm-pack": "^0.10.2"
|
||||
}
|
||||
}
|
|
@ -18,12 +18,15 @@ describe("tests", () => {
|
|||
.mkdtemp(path.join(os.tmpdir(), path.sep))
|
||||
.then((folder) => {
|
||||
tmpFolder = folder;
|
||||
return;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (globalThis.curve_bn128) globalThis.curve_bn128.terminate();
|
||||
if (globalThis.curve_bn128) {
|
||||
return globalThis.curve_bn128.terminate()
|
||||
};
|
||||
});
|
||||
|
||||
describe("metadata", () => {
|
||||
|
@ -165,7 +168,11 @@ describe("tests", () => {
|
|||
it("compile", () => {
|
||||
assert.doesNotThrow(() => {
|
||||
const code =
|
||||
"def main(private field a, field b) -> bool { return a * a == b; }";
|
||||
`def main(private field a, field b) {
|
||||
bool check = if (a == 0){ true} else {a * a == b};
|
||||
assert(check);
|
||||
return true;
|
||||
}`;
|
||||
artifacts = provider.compile(code, { snarkjs: true });
|
||||
});
|
||||
});
|
||||
|
@ -199,7 +206,6 @@ describe("tests", () => {
|
|||
.then(() => {
|
||||
return snarkjs.zKey
|
||||
.newZKey(r1csPath, "./tests/powersOfTau5_0000.ptau", zkeyPath)
|
||||
.then(() => {});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -234,6 +240,11 @@ describe("tests", () => {
|
|||
.writeFile(witnessPath, computationResult.snarkjs.witness)
|
||||
.then(() => {
|
||||
return snarkjs.groth16.prove(zkeyPath, witnessPath);
|
||||
}).then(r => {
|
||||
return snarkjs.zKey.exportVerificationKey(zkeyPath).then((vk) => {
|
||||
assert(snarkjs.groth16.verify(vk, r.publicSignals, r.proof) === true);
|
||||
return
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue