arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/src/futils.js
src/futils.jsRaw
 1/*
 2    Copyright 2018 0kims association.
 3
 4    This file is part of zksnark JavaScript library.
 5
 6    zksnark JavaScript library 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    zksnark JavaScript library 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    zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20const bigInt = require("./bigint.js");
21
22exports.mulScalar = (F, base, e) =>{
23    let res = F.zero;
24    let rem = bigInt(e);
25    let exp = base;
26
27    while (! rem.isZero()) {
28        if (rem.isOdd()) {
29            res = F.add(res, exp);
30        }
31        exp = F.double(exp);
32        rem = rem.shr(1);
33    }
34
35    return res;
36};
37
38
39exports.exp = (F, base, e) =>{
40    let res = F.one;
41    let rem = bigInt(e);
42    let exp = base;
43
44    while (! rem.isZero()) {
45        if (rem.isOdd()) {
46            res = F.mul(res, exp);
47        }
48        exp = F.square(exp);
49        rem = rem.shr(1);
50    }
51
52    return res;
53};