-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
266 lines (250 loc) · 7.18 KB
/
function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* A helper function for BigInt mod computation.
@param {BigInt} this - the BigInt base (eg. 20n).
@param {BigInt} _m - the BigInt modular (eg. BigInt(2)).
@return this mod _m (eg. 20n.mod(BigInt(2))=BigInt(0)).
*/
// eslint-disable-next-line no-extend-native, func-names
BigInt.prototype.mod = function (_m) {
return ((this % _m) + _m) % _m;
};
/**
@param {object} _bigints - the BigInt array object
(eg. [ BigInt(0), BigInt(0), BigInt(0), BigInt(1), BigInt(1), 3n ] or [BigInt(1)]).
@return BigInt sum of each element in _bigint
(eg. [ 5n ] or [BigInt(1)]).
*/
function addBigInt(_bigintA, _bigintB) {
return _bigintA + _bigintB;
}
/**
@param {BigInt} _bigintA - the BigInt _bigintA
(eg. [BigInt(1)]).
@param {BigInt} _bigintB - the BigInt _bigintB
(eg. [BigInt(2)]).
@return BigInt _bigintA - _bigintB
(eg. [ -BigInt(1) ]).
*/
function subBigInt(_bigintA, _bigintB) {
return _bigintA - _bigintB;
}
/**
@param {BigInt} _bigintA - the BigInt _bigintA
(eg. [BigInt(1)]).
@param {BigInt} _bigintB - the BigInt _bigintB
(eg. [BigInt(2)]).
@return BigInt _bigintA * _bigintB
(eg. [ BigInt(1) ]).
*/
function mulBigInt(_bigintA, _bigintB) {
return _bigintA * _bigintB;
}
/**
@param {object} _bigints - the BigInt array object
(eg. [ BigInt(0), BigInt(0), BigInt(0), BigInt(1), BigInt(1), 3n ] or [BigInt(1)]).
@param {BigInt} _m - the BigInt modular (eg. BigInt(2)).
@return BigInt sum of each element mod _m in _bigint
(eg. [ BigInt(1) ]).
*/
// function addMod(_bigints, _m) {
// return _bigints.reduce((e, acc) => (e + acc).mod(_m), BigInt(0));
// }
function addModBigInt(_bigintA, _bigintB, _m) {
return (_bigintA + _bigintB).mod(_m);
}
/**
@param {BigInt} _bigintA - the BigInt _bigintA
(eg. [BigInt(1)]).
@param {BigInt} _bigintB - the BigInt _bigintB
(eg. [BigInt(2)]).
@param {BigInt} _m - the BigInt modular (eg. BigInt(2)).
@return BigInt (_bigintA - _bigintB) mod _m
(eg. [ BigInt(1) ]).
*/
function subModBigInt(_bigintA, _bigintB, _m) {
return (_bigintA - _bigintB).mod(_m);
}
/**
@param {BigInt} _bigintA - the BigInt _bigintA
(eg. [BigInt(1)]).
@param {BigInt} _bigintB - the BigInt _bigintB
(eg. [BigInt(2)]).
@param {BigInt} _m - the BigInt modular (eg. BigInt(2)).
@return BigInt (_bigintA * _bigintB) mod _m
(eg. [ BigInt(1) ]).
*/
function mulModBigInt(_bigintA, _bigintB, _m) {
return (_bigintA * _bigintB).mod(_m);
}
/**
@param {BigInt} _bigint - the BigInt _bigint (eg.-11n).
@return abs of _bigint (eg.11n).
*/
function absBigInt(_bigint) {
return _bigint >= BigInt(0) ? _bigint : -_bigint;
}
/**
* extended Euclidean algorithm
* given two numbers a, b
* compute (g,x,y) such that ax+by=gcd(a,b)=g.
@param {BigInt} _bigintA - the BigInt _bigintA
(eg.23894798501898n).
@param {BigInt} _bigintB - the BigInt _bigintB
(eg.23948178468116n).
@return a BigInt triple { g: BigInt(2), x: 2437250447493n, y: -2431817869532n },
such that _bigintA * x + _bigintB * y = g = gcd(_bigintA, _bigintB).
*/
function eGcdBigInt(_bigintA, _bigintB) {
let _bigintAAbs = absBigInt(_bigintA);
let _bigintBAbs = absBigInt(_bigintB);
let x = BigInt(0);
let y = BigInt(1);
let u = BigInt(1);
let v = BigInt(0);
while (_bigintAAbs !== BigInt(0)) {
const q = _bigintBAbs / _bigintAAbs;
const r = BigInt(_bigintBAbs % _bigintAAbs);
const m = x - u * q;
const n = y - v * q;
_bigintBAbs = _bigintAAbs;
_bigintAAbs = r;
x = u;
y = v;
u = m;
v = n;
}
return {
g: _bigintBAbs,
x,
y,
};
}
/**
@param {BigInt} _bigint - the BigInt _bigint (eg. 3n).
@param {BigInt} _m - the BigInt modular (eg. 25n).
@return {BigInt}- BigInt Modular inverse
*/
function modInv(a, n) {
const egcd = eGcdBigInt(a.mod(n), n);
if (egcd.g !== BigInt(1)) {
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
} else {
return egcd.x.mod(n);
}
}
/**
@param {BigInt} _base - the BigInt _base (eg. 3n).
@param {BigInt} _exponent - the BigInt modular (eg. 3n).
@param {BigInt} _m - the BigInt modular (eg. 25n).
@return {BigInt}- BigInt _powMod = _base ** _exponent mod _m (eg. BigInt(2)).
*/
function powModBigInt(_base, _exponent, _m) {
if (_m <= BigInt(0)) {
throw new Error('Error: Modular of powMod must be positive');
} else if (_m === BigInt(1)) {
return BigInt(0);
}
let _powMod = BigInt(1);
let b = _base.mod(_m);
if (_exponent < 0) {
return modInv(powModBigInt(_base, absBigInt(_exponent), _m), _m);
}
while (_exponent > 0) {
if (_exponent.mod(BigInt(2)) === BigInt(1)) {
_powMod = (_powMod * b).mod(_m);
}
// eslint-disable-next-line no-param-reassign
_exponent /= BigInt(2);
b = (b ** BigInt(2)).mod(_m);
}
return _powMod;
}
/**
* Euclidean algorithm
Greatest common divisor of two integers
based on the iterative binary algorithm.
@param {BigInt} _bigintA - the BigInt _bigintA
(eg.23894798501898n).
@param {BigInt} _bigintB - the BigInt _bigintB
@return a BigInt Greatest common divisor of _bigintA and _bigintB.
(eg.BigInt(2)).
*/
function gcdBigInt(_bigintA, _bigintB) {
let aAbs = absBigInt(_bigintA);
let bAbs = absBigInt(_bigintB);
if (aAbs === BigInt(0)) {
return bAbs;
}
if (bAbs === BigInt(0)) {
return aAbs;
}
let shift = BigInt(0);
// eslint-disable-next-line no-bitwise
while (((aAbs | bAbs) & BigInt(1)) === BigInt(0)) {
// eslint-disable-next-line no-bitwise
aAbs >>= BigInt(1);
// eslint-disable-next-line no-bitwise
bAbs >>= BigInt(1);
shift++;
}
// eslint-disable-next-line no-bitwise
while ((aAbs & BigInt(1)) === BigInt(0)) aAbs >>= BigInt(1);
do {
// eslint-disable-next-line no-bitwise
while ((bAbs & BigInt(1)) === BigInt(0)) bAbs >>= BigInt(1);
if (aAbs > bAbs) {
const x = aAbs;
aAbs = bAbs;
bAbs = x;
}
bAbs -= aAbs;
} while (bAbs !== BigInt(0));
// eslint-disable-next-line no-bitwise
return aAbs << shift;
}
/**
* least common multiple of two Bigints
@param {BigInt} _bigintA - the BigInt _bigintA
(eg.492997048111900109466724n).
@param {BigInt} _bigintB - the BigInt _bigintB
@return abs(_bigintA*_bigintB)/gcd(_bigintA,_bigintB)
*/
function lcmBigInt(_bigintA, _bigintB) {
if (_bigintA === BigInt(0) && _bigintB === BigInt(0)) {
return BigInt(0);
}
return BigInt(absBigInt((_bigintA / gcdBigInt(_bigintA, _bigintB)) * _bigintB));
}
/**
@param {BigInt} _bigintA - the positive BigInt _bigintA
(eg.492997048111900109466724n).
@param {BigInt} _bigintB - the positive BigInt _bigintB
@return the max value of {_bigintA, _bigintB}
*/
function maxBigInt(_bigintA, _bigintB) {
return _bigintA >= _bigintB ? _bigintA : _bigintB;
}
/**
@param {BigInt} _bigintA - the positive BigInt _bigintA
(eg.492997048111900109466724n).
@param {BigInt} _bigintB - the positive BigInt _bigintB
@return the min value of {_bigintA, _bigintB}
*/
function minBigInt(_bigintA, _bigintB) {
return _bigintA >= _bigintB ? _bigintB : _bigintA;
}
module.exports = {
addBigInt,
subBigInt,
mulBigInt,
addModBigInt,
subModBigInt,
mulModBigInt,
absBigInt,
eGcdBigInt,
gcdBigInt,
lcmBigInt,
maxBigInt,
minBigInt,
powModBigInt,
};