arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/src/ratfield.js
src/ratfield.jsRaw
  1/*
  2    Copyright 2018 0kims association.
  3
  4    This file is part of snarkjs.
  5
  6    snarkjs is a free software: you can redistribute it and/or
  7    modify it under the terms of the GNU General Public License as published by the
  8    Free Software Foundation, either version 3 of the License, or (at your option)
  9    any later version.
 10
 11    snarkjs is distributed in the hope that it will be useful,
 12    but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 13    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 14    more details.
 15
 16    You should have received a copy of the GNU General Public License along with
 17    snarkjs. If not, see <https://www.gnu.org/licenses/>.
 18*/
 19
 20const fUtils = require("./futils.js");
 21
 22class RatField {
 23    constructor(F) {
 24        this.F = F;
 25        this.zero = [F.zero, F.one];
 26        this.one = [F.one, F.one];
 27        this.two = [F.two, F.one];
 28        this.twoinv = [F.one, F.two];
 29        this.q = F.q;
 30    }
 31
 32    add(a,b) {
 33        return [
 34            this.F.add(
 35                this.F.mul(a[0], b[1]),
 36                this.F.mul(a[1], b[0])),
 37            this.F.mul(a[1], b[1])];
 38    }
 39
 40    double(a) {
 41        return [this.F.add(a[0], a[0]), a[1]];
 42    }
 43
 44    sub(a,b) {
 45        return [
 46            this.F.sub(
 47                this.F.mul(a[0], b[1]),
 48                this.F.mul(a[1], b[0])),
 49            this.F.mul(a[1], b[1])];
 50    }
 51
 52    neg(a) {
 53        return [this.F.neg(a[0]), a[1]];
 54    }
 55
 56    mul(a,b) {
 57        return [
 58            this.F.mul(a[0], b[0]),
 59            this.F.mul(a[1], b[1]),
 60        ];
 61    }
 62
 63    copy(a) {
 64        return [a[0], a[1]];
 65    }
 66
 67    div(a, b) {
 68        return [
 69            this.F.mul(a[0], b[1]),
 70            this.F.mul(a[1], b[0]),
 71        ];
 72    }
 73
 74    inverse(a) {
 75        return [a[1], a[0]];
 76    }
 77
 78    square(a) {
 79        return [
 80            this.F.square(a[0]),
 81            this.F.square(a[1])
 82        ];
 83    }
 84
 85    mulScalar(base, e) {
 86        return [this.F.mulScalar(base[0], e) , base[1]];
 87    }
 88
 89    exp(base, e) {
 90        return fUtils.exp(this, base, e);
 91    }
 92
 93    equals(a, b) {
 94        return this.F.equals(
 95            this.F.mul(a[0], b[1]),
 96            this.F.mul(a[1], b[0])
 97        );
 98    }
 99
100    isZero(a) {
101        return this.F.isZero(a[0]);
102    }
103
104    affine(a) {
105        return [this.F.div(a[0], a[1]), this.F.one];
106    }
107
108    toString(a) {
109        const ca = this.affine(a);
110        return `"0x${ca[0].toString(16)}"`;
111    }
112
113    random() {
114        return [this.F.random(), this.F.one];
115    }
116
117    fromF(a) {
118        return [a, this.F.one];
119    }
120
121    toF(a) {
122        return this.affine(a)[0];
123    }
124}
125
126
127module.exports = RatField;