mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
All Bit and logical operators working
This commit is contained in:
@@ -1,16 +1,32 @@
|
||||
const bigInt = require("big-integer");
|
||||
const assert = require("assert");
|
||||
|
||||
module.exports = class ZqField {
|
||||
constructor(p) {
|
||||
this.p = p;
|
||||
this.bitLength = p.bitLength();
|
||||
this.mask = bigInt.one.shiftLeft(this.bitLength - 1).minus(bigInt.one);
|
||||
}
|
||||
|
||||
add(a, b) {
|
||||
return a.add(b).mod(this.p);
|
||||
let res = a.add(b);
|
||||
if (res.geq(this.p)) {
|
||||
res = res.minsu(this.p);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
sub(a, b) {
|
||||
return a.minus(b).mod(this.p);
|
||||
if (a.geq(b)) {
|
||||
return a.minus(b);
|
||||
} else {
|
||||
return this.p.minus(b.minus(a));
|
||||
}
|
||||
}
|
||||
|
||||
neg(a) {
|
||||
if (a.isZero()) return a;
|
||||
return this.p.minus(a);
|
||||
}
|
||||
|
||||
mul(a, b) {
|
||||
@@ -46,6 +62,7 @@ module.exports = class ZqField {
|
||||
}
|
||||
|
||||
idiv(a, b) {
|
||||
assert(!b.isZero(), "Division by zero");
|
||||
return a.divide(b);
|
||||
}
|
||||
|
||||
@@ -53,5 +70,46 @@ module.exports = class ZqField {
|
||||
return a.mod(b);
|
||||
}
|
||||
|
||||
pow(a, b) {
|
||||
return a.modPow(b, this.p);
|
||||
}
|
||||
|
||||
band(a, b) {
|
||||
return a.and(b).and(this.mask);
|
||||
}
|
||||
|
||||
bor(a, b) {
|
||||
return a.or(b).and(this.mask);
|
||||
}
|
||||
|
||||
bxor(a, b) {
|
||||
return a.xor(b).and(this.mask);
|
||||
}
|
||||
|
||||
bnot(a) {
|
||||
return a.xor(this.mask).and(this.mask);
|
||||
}
|
||||
|
||||
shl(a, b) {
|
||||
if (b.geq(this.bitLength)) return bigInt.zero;
|
||||
return a.shiftLeft(b).and(this.mask);
|
||||
}
|
||||
|
||||
shr(a, b) {
|
||||
if (b.geq(this.bitLength)) return bigInt.zero;
|
||||
return a.shiftRight(b).and(this.mask);
|
||||
}
|
||||
|
||||
land(a, b) {
|
||||
return (a.isZero() || b.isZero) ? bigInt.zero : bigInt.one;
|
||||
}
|
||||
|
||||
lor(a, b) {
|
||||
return (a.isZero() && b.isZero) ? bigInt.zero : bigInt.one;
|
||||
}
|
||||
|
||||
lnot(a) {
|
||||
return a.isZero() ? bigInt.one : bigInt.zero;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user