circomlib/test/sign.js
test/sign.jsRaw
1const path = require("path");
2const Fr = require("ffjavascript").bn128.Fr;
3const Scalar = require("ffjavascript").Scalar;
4const tester = require("circom").tester;
5
6function print(circuit, w, s) {
7 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
8}
9
10function getBits(v, n) {
11 const res = [];
12 for (let i=0; i<n; i++) {
13 if (Scalar.isOdd(Scalar.shr(v, i))) {
14 res.push(Fr.one);
15 } else {
16 res.push(Fr.zero);
17 }
18 }
19 return res;
20}
21
22const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
23
24describe("Sign test", function() {
25 let circuit;
26 this.timeout(100000);
27
28 before( async() => {
29 circuit = await tester(path.join(__dirname, "circuits", "sign_test.circom"));
30 });
31
32 it("Sign of 0", async () => {
33 const inp = getBits(Scalar.e(0), 254);
34 const w = await circuit.calculateWitness({in: inp}, true);
35
36 await circuit.assertOut(w, {sign: 0});
37 });
38
39 it("Sign of 3", async () => {
40 const inp = getBits(Scalar.e(3), 254);
41 const w = await circuit.calculateWitness({in: inp}, true);
42
43 await circuit.assertOut(w, {sign: 0});
44 });
45
46 it("Sign of q/2", async () => {
47 const inp = getBits(Scalar.shr(q, 1), 254);
48 const w = await circuit.calculateWitness({in: inp}, true);
49
50 await circuit.assertOut(w, {sign: 0});
51 });
52
53 it("Sign of q/2+1", async () => {
54 const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254);
55 const w = await circuit.calculateWitness({in: inp}, true);
56
57 await circuit.assertOut(w, {sign: 1});
58 });
59
60 it("Sign of q-1", async () => {
61 const inp = getBits(Scalar.sub(q, 1), 254);
62 const w = await circuit.calculateWitness({in: inp}, true);
63
64 await circuit.assertOut(w, {sign: 1});
65 });
66
67 it("Sign of q", async () => {
68 const inp = getBits(q, 254);
69 const w = await circuit.calculateWitness({in: inp}, true);
70
71 await circuit.assertOut(w, {sign: 1});
72 });
73
74 it("Sign of all ones", async () => {
75 const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254);
76 const w = await circuit.calculateWitness({in: inp}, true);
77
78 await circuit.assertOut(w, {sign: 1});
79 });
80});