arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/src/zqfield.js
src/zqfield.jsRaw
 1const crypto = require("crypto");
 2
 3const bigInt = require("./bigint");
 4const fUtils = require("./futils.js");
 5
 6class ZqField {
 7    constructor(q) {
 8        this.q = q;
 9        this.zero = bigInt.zero;
10        this.one = bigInt.one;
11        this.add = bigInt.genAdd();
12        this.double = bigInt.genDouble();
13        this.sub = bigInt.genSub();
14        this.neg = bigInt.genNeg();
15        this.mul = bigInt.genMul(q);
16        this.inverse = bigInt.genInverse(q);
17        this.square = bigInt.genSquare(q);
18        this.equals = bigInt.genEquals(q);
19        this.affine = bigInt.genAffine(q);
20        this.isZero = bigInt.genIsZero(q);
21        this.two = this.add(this.one, this.one);
22        this.twoinv = this.inverse(this.two);
23    }
24
25    copy(a) {
26        return bigInt(a);
27    }
28
29    div(a, b) {
30        return this.mul(a, this.inverse(b));
31    }
32
33    mulScalar(base, e) {
34        return this.mul(base, bigInt(e));
35    }
36
37    exp(base, e) {
38        return fUtils.exp(this, base, e);
39    }
40
41    toString(a) {
42        const ca = this.affine(a);
43        return `"0x${ca.toString(16)}"`;
44    }
45
46    random() {
47        let res = bigInt(0);
48        let n = bigInt(this.q);
49        while (!n.isZero()) {
50            res = res.shl(8).add(bigInt(crypto.randomBytes(1)[0]));
51            n = n.shr(8);
52        }
53        return res;
54    }
55}
56
57
58module.exports = ZqField;