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 free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) 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
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19const bigInt = require("./bigint.js");
20
21exports.mulScalar = (F, base, e) =>{
22 let res = F.zero;
23 let rem = bigInt(e);
24 let exp = base;
25
26 while (! rem.isZero()) {
27 if (rem.isOdd()) {
28 res = F.add(res, exp);
29 }
30 exp = F.double(exp);
31 rem = rem.shr(1);
32 }
33
34 return res;
35};
36
37
38exports.exp = (F, base, e) =>{
39 let res = F.one;
40 let rem = bigInt(e);
41 let exp = base;
42
43 while (! rem.isZero()) {
44 if (rem.isOdd()) {
45 res = F.mul(res, exp);
46 }
47 exp = F.square(exp);
48 rem = rem.shr(1);
49 }
50
51 return res;
52};