mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
div operators
This commit is contained in:
@@ -6,12 +6,37 @@ const c_tester = require("../index.js").c_tester;
|
||||
const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
|
||||
|
||||
function normalize(o) {
|
||||
if ((typeof(o) == "bigint") || o.isZero !== undefined) {
|
||||
const res = bigInt(o);
|
||||
return norm(res);
|
||||
} else if (Array.isArray(o)) {
|
||||
return o.map(normalize);
|
||||
} else if (typeof o == "object") {
|
||||
const res = {};
|
||||
for (let k in o) {
|
||||
res[k] = normalize(o[k]);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
const res = bigInt(o);
|
||||
return norm(res);
|
||||
}
|
||||
|
||||
function norm(n) {
|
||||
let res = n.mod(__P__);
|
||||
if (res.isNegative()) res = __P__.add(res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function doTest(circuit, testVectors) {
|
||||
const cir = await c_tester(path.join(__dirname, "circuits", circuit));
|
||||
|
||||
for (let i=0; i<testVectors.length; i++) {
|
||||
const w = await cir.calculateWitness(testVectors[i][0]);
|
||||
await cir.assertOut(w, testVectors[i][1] );
|
||||
const w = await cir.calculateWitness(normalize(testVectors[i][0]));
|
||||
await cir.assertOut(w, normalize(testVectors[i][1]) );
|
||||
}
|
||||
|
||||
await cir.release();
|
||||
@@ -129,4 +154,52 @@ describe("basic cases", function () {
|
||||
]
|
||||
);
|
||||
});
|
||||
it("inc", async () => {
|
||||
await doTest(
|
||||
"inc.circom",
|
||||
[
|
||||
[{in: 0}, {out: [5, 2]}],
|
||||
[{in: 1}, {out: [6, 4]}],
|
||||
[{in: 2}, {out: [7, 6]}],
|
||||
[{in: 3}, {out: [8, 8]}],
|
||||
[{in: __P__.minus(2)}, {out: [3,__P__.minus(2)]}],
|
||||
]
|
||||
);
|
||||
});
|
||||
it("dec", async () => {
|
||||
await doTest(
|
||||
"dec.circom",
|
||||
[
|
||||
[{in: 0}, {out: [1, __P__.minus(2)]}],
|
||||
[{in: 1}, {out: [2, 0]}],
|
||||
[{in: 2}, {out: [3, 2]}],
|
||||
[{in: 3}, {out: [4, 4]}],
|
||||
[{in: __P__.minus(2)}, {out: [__P__.minus(1),__P__.minus(6)]}],
|
||||
]
|
||||
);
|
||||
});
|
||||
it("ops", async () => {
|
||||
await doTest(
|
||||
"ops.circom",
|
||||
[
|
||||
[{in: [-2, 2]}, {add: 0, sub: -4, mul: -4}],
|
||||
[{in: [-1, 1]}, {add: 0, sub: -2, mul: -1}],
|
||||
[{in: [ 0, 0]}, {add: 0, sub: 0, mul: 0}],
|
||||
[{in: [ 1,-1]}, {add: 0, sub: 2, mul: -1}],
|
||||
[{in: [ 2,-2]}, {add: 0, sub: 4, mul: -4}],
|
||||
[{in: [-2,-3]}, {add: -5, sub: 1, mul: 6}],
|
||||
[{in: [ 2, 3]}, {add: 5, sub: -1, mul: 6}],
|
||||
]
|
||||
);
|
||||
});
|
||||
it("ops2", async () => {
|
||||
await doTest(
|
||||
"ops2.circom",
|
||||
[
|
||||
[{in: [-2, 2]}, {div: -1, idiv: bigInt("10944121435919637611123202872628637544274182200208017171849102093287904247807"), mod: 1}],
|
||||
[{in: [-1, 1]}, {div: -1, idiv: -1, mod: 0}],
|
||||
[{in: [ 1,-1]}, {div: -1, idiv: 0, mod: 1}],
|
||||
]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
23
test/circuits/dec.circom
Normal file
23
test/circuits/dec.circom
Normal file
@@ -0,0 +1,23 @@
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
// First play with variables;
|
||||
|
||||
var c = 3;
|
||||
var d = c--; // d --> 3
|
||||
var e = --c; // e --> 1
|
||||
|
||||
out[0] <== in + e; // in + 1
|
||||
|
||||
// Then play with signals
|
||||
|
||||
c = in;
|
||||
d = c--; //d <-- in;
|
||||
e = --c; // d <-- in-2
|
||||
|
||||
out[1] <== in + e; // 2*in -2
|
||||
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
24
test/circuits/inc.circom
Normal file
24
test/circuits/inc.circom
Normal file
@@ -0,0 +1,24 @@
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
// First play with variables;
|
||||
|
||||
var c = 3;
|
||||
var d = c++; // d --> 3
|
||||
var e = ++c; // e --> 5
|
||||
|
||||
out[0] <== in + e; // in + 5
|
||||
|
||||
// Then play with signals
|
||||
|
||||
c = in;
|
||||
d = c++; //d <-- in;
|
||||
e = ++c; // d <-- in+2
|
||||
|
||||
out[1] <== in + e; // 2*in +2
|
||||
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
|
||||
12
test/circuits/ops.circom
Normal file
12
test/circuits/ops.circom
Normal file
@@ -0,0 +1,12 @@
|
||||
template Ops() {
|
||||
signal input in[2];
|
||||
signal output add;
|
||||
signal output sub;
|
||||
signal output mul;
|
||||
|
||||
add <-- in[0] + in[1];
|
||||
sub <-- in[0] - in[1];
|
||||
mul <-- in[0] * in[1];
|
||||
}
|
||||
|
||||
component main = Ops();
|
||||
12
test/circuits/ops2.circom
Normal file
12
test/circuits/ops2.circom
Normal file
@@ -0,0 +1,12 @@
|
||||
template Ops2() {
|
||||
signal input in[2];
|
||||
signal output div;
|
||||
signal output idiv;
|
||||
signal output mod;
|
||||
|
||||
div <-- in[0] / in[1];
|
||||
idiv <-- in[0] \ in[1];
|
||||
mod <-- in[0] % in[1];
|
||||
}
|
||||
|
||||
component main = Ops2();
|
||||
Reference in New Issue
Block a user