snarkjs/src/zqfield.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-09-05 04:56:49 +02:00
/*
Copyright 2018 0kims association
This file is part of zksnark javascript library.
zksnark javascript library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
zksnark javascript library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-25 00:16:12 +02:00
const crypto = require("crypto");
2018-08-15 09:59:18 +02:00
const bigInt = require("./bigint");
2018-08-14 09:06:00 +02:00
const fUtils = require("./futils.js");
2018-08-12 20:37:43 +02:00
2018-08-15 11:05:17 +02:00
class ZqField {
2018-08-12 20:37:43 +02:00
constructor(q) {
this.q = q;
this.zero = bigInt.zero;
this.one = bigInt.one;
2018-08-15 14:49:08 +02:00
this.add = bigInt.genAdd();
this.double = bigInt.genDouble();
this.sub = bigInt.genSub();
this.neg = bigInt.genNeg();
this.mul = bigInt.genMul(q);
this.inverse = bigInt.genInverse(q);
this.square = bigInt.genSquare(q);
this.equals = bigInt.genEquals(q);
this.affine = bigInt.genAffine(q);
this.isZero = bigInt.genIsZero(q);
2018-08-18 14:11:51 +02:00
this.two = this.add(this.one, this.one);
this.twoinv = this.inverse(this.two);
2018-08-12 20:37:43 +02:00
}
copy(a) {
return bigInt(a);
}
div(a, b) {
return this.mul(a, this.inverse(b));
}
2018-08-25 00:16:12 +02:00
mulScalar(base, e) {
return this.mul(base, bigInt(e));
2018-08-14 09:06:00 +02:00
}
exp(base, e) {
return fUtils.exp(this, base, e);
}
2018-08-12 20:37:43 +02:00
toString(a) {
const ca = this.affine(a);
return `"0x${ca.toString(16)}"`;
}
2018-08-25 00:16:12 +02:00
random() {
let res = bigInt(0);
let n = bigInt(this.q);
while (!n.isZero()) {
res = res.shl(8).add(bigInt(crypto.randomBytes(1)[0]));
n = n.shr(8);
}
return res;
}
2018-08-12 20:37:43 +02:00
}
2018-08-15 11:05:17 +02:00
module.exports = ZqField;