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

fix circom signal ordering, add test

This commit is contained in:
schaeff 2022-08-29 10:52:08 +02:00
parent 307bbac0e2
commit 1d62d7e6ab
3 changed files with 27 additions and 8 deletions

View file

@ -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

View file

@ -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"
}
}

View file

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