snarkjs/src/zqfield.js

76 lines
1.2 KiB
JavaScript
Raw Normal View History

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.nq = bigInt.zero.minus(q);
this.zero = bigInt.zero;
this.one = bigInt.one;
}
copy(a) {
return bigInt(a);
}
add(a, b) {
return a.add(b);
}
2018-08-14 09:06:00 +02:00
double(a) {
return this.add(a,a);
}
2018-08-12 20:37:43 +02:00
sub(a, b) {
return a.minus(b);
}
neg(a) {
return bigInt.zero.minus(a);
}
mul(a, b) {
2018-08-15 11:05:17 +02:00
return a.mulMod(this.q, b);
2018-08-12 20:37:43 +02:00
}
inverse(a) {
2018-08-15 11:05:17 +02:00
return a.modInv(this.q);
2018-08-12 20:37:43 +02:00
}
div(a, b) {
return this.mul(a, this.inverse(b));
}
square(a) {
return a.square().mod(this.q);
}
isZero(a) {
return a.isZero();
}
equals(a, b) {
return this.affine(a).equals(this.affine(b));
}
affine(a) {
2018-08-15 11:05:17 +02:00
return a.affine(this.q);
2018-08-12 20:37:43 +02:00
}
2018-08-14 09:06:00 +02:00
mulEscalar(base, e) {
return fUtils.mulEscalar(this, base, e);
}
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-15 11:05:17 +02:00
module.exports = ZqField;