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