@novnc/novnc 1.3.0-g42ec5f3 → 1.3.0-g658e415
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/rfb.js +13 -2
- package/lib/rfb.js +16 -3
- package/package.json +1 -1
- package/core/util/bigint-mod-arith.js +0 -283
- package/lib/util/bigint-mod-arith.js +0 -339
package/core/rfb.js
CHANGED
|
@@ -27,7 +27,6 @@ import XtScancode from "./input/xtscancodes.js";
|
|
|
27
27
|
import { encodings } from "./encodings.js";
|
|
28
28
|
import RSAAESAuthenticationState from "./ra2.js";
|
|
29
29
|
import { MD5 } from "./util/md5.js";
|
|
30
|
-
import { modPow } from "./util/bigint-mod-arith.js";
|
|
31
30
|
|
|
32
31
|
import RawDecoder from "./decoders/raw.js";
|
|
33
32
|
import CopyRectDecoder from "./decoders/copyrect.js";
|
|
@@ -1595,7 +1594,19 @@ export default class RFB extends EventTargetMixin {
|
|
|
1595
1594
|
let exponentHex = "0x"+Array.from(exponent, byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('');
|
|
1596
1595
|
let modulusHex = "0x"+Array.from(modulus, byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('');
|
|
1597
1596
|
|
|
1598
|
-
let
|
|
1597
|
+
let b = BigInt(baseHex);
|
|
1598
|
+
let e = BigInt(exponentHex);
|
|
1599
|
+
let m = BigInt(modulusHex);
|
|
1600
|
+
let r = 1n;
|
|
1601
|
+
b = b % m;
|
|
1602
|
+
while (e > 0) {
|
|
1603
|
+
if (e % 2n === 1n) {
|
|
1604
|
+
r = (r * b) % m;
|
|
1605
|
+
}
|
|
1606
|
+
e = e / 2n;
|
|
1607
|
+
b = (b * b) % m;
|
|
1608
|
+
}
|
|
1609
|
+
let hexResult = r.toString(16);
|
|
1599
1610
|
|
|
1600
1611
|
while (hexResult.length/2<exponent.length || (hexResult.length%2 != 0)) {
|
|
1601
1612
|
hexResult = "0"+hexResult;
|
package/lib/rfb.js
CHANGED
|
@@ -47,8 +47,6 @@ var _ra = _interopRequireDefault(require("./ra2.js"));
|
|
|
47
47
|
|
|
48
48
|
var _md = require("./util/md5.js");
|
|
49
49
|
|
|
50
|
-
var _bigintModArith = require("./util/bigint-mod-arith.js");
|
|
51
|
-
|
|
52
50
|
var _raw = _interopRequireDefault(require("./decoders/raw.js"));
|
|
53
51
|
|
|
54
52
|
var _copyrect = _interopRequireDefault(require("./decoders/copyrect.js"));
|
|
@@ -1942,7 +1940,22 @@ var RFB = /*#__PURE__*/function (_EventTargetMixin) {
|
|
|
1942
1940
|
var modulusHex = "0x" + Array.from(modulus, function (_byte4) {
|
|
1943
1941
|
return ('0' + (_byte4 & 0xFF).toString(16)).slice(-2);
|
|
1944
1942
|
}).join('');
|
|
1945
|
-
var
|
|
1943
|
+
var b = BigInt(baseHex);
|
|
1944
|
+
var e = BigInt(exponentHex);
|
|
1945
|
+
var m = BigInt(modulusHex);
|
|
1946
|
+
var r = 1n;
|
|
1947
|
+
b = b % m;
|
|
1948
|
+
|
|
1949
|
+
while (e > 0) {
|
|
1950
|
+
if (e % 2n === 1n) {
|
|
1951
|
+
r = r * b % m;
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
e = e / 2n;
|
|
1955
|
+
b = b * b % m;
|
|
1956
|
+
}
|
|
1957
|
+
|
|
1958
|
+
var hexResult = r.toString(16);
|
|
1946
1959
|
|
|
1947
1960
|
while (hexResult.length / 2 < exponent.length || hexResult.length % 2 != 0) {
|
|
1948
1961
|
hexResult = "0" + hexResult;
|
package/package.json
CHANGED
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* bigint-mod-arith implementation:
|
|
3
|
-
* https://github.com/juanelas/bigint-mod-arith
|
|
4
|
-
*
|
|
5
|
-
* Full attribution follows:
|
|
6
|
-
*
|
|
7
|
-
* -------------------------------------------------------------------------
|
|
8
|
-
*
|
|
9
|
-
* MIT License
|
|
10
|
-
*
|
|
11
|
-
* Copyright (c) 2018 Juan Hernández Serrano
|
|
12
|
-
*
|
|
13
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
15
|
-
* in the Software without restriction, including without limitation the rights
|
|
16
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
18
|
-
* furnished to do so, subject to the following conditions:
|
|
19
|
-
*
|
|
20
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
21
|
-
* copies or substantial portions of the Software.
|
|
22
|
-
*
|
|
23
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
-
* SOFTWARE.
|
|
30
|
-
*
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
|
35
|
-
*
|
|
36
|
-
* @param a
|
|
37
|
-
*
|
|
38
|
-
* @returns The absolute value of a
|
|
39
|
-
*/
|
|
40
|
-
function abs(a) {
|
|
41
|
-
return (a >= 0) ? a : -a;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Returns the bitlength of a number
|
|
46
|
-
*
|
|
47
|
-
* @param a
|
|
48
|
-
* @returns The bit length
|
|
49
|
-
*/
|
|
50
|
-
function bitLength(a) {
|
|
51
|
-
if (typeof a === 'number') {
|
|
52
|
-
a = BigInt(a);
|
|
53
|
-
}
|
|
54
|
-
if (a === 1n) {
|
|
55
|
-
return 1;
|
|
56
|
-
}
|
|
57
|
-
let bits = 1;
|
|
58
|
-
do {
|
|
59
|
-
bits++;
|
|
60
|
-
} while ((a >>= 1n) > 1n);
|
|
61
|
-
return bits;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm.
|
|
66
|
-
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
67
|
-
*
|
|
68
|
-
* @param a
|
|
69
|
-
* @param b
|
|
70
|
-
*
|
|
71
|
-
* @throws {RangeError}
|
|
72
|
-
* This excepction is thrown if a or b are less than 0
|
|
73
|
-
*
|
|
74
|
-
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
75
|
-
*/
|
|
76
|
-
function eGcd(a, b) {
|
|
77
|
-
if (typeof a === 'number') {
|
|
78
|
-
a = BigInt(a);
|
|
79
|
-
}
|
|
80
|
-
if (typeof b === 'number') {
|
|
81
|
-
b = BigInt(b);
|
|
82
|
-
}
|
|
83
|
-
if (a <= 0n || b <= 0n) {
|
|
84
|
-
throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive
|
|
85
|
-
}
|
|
86
|
-
let x = 0n;
|
|
87
|
-
let y = 1n;
|
|
88
|
-
let u = 1n;
|
|
89
|
-
let v = 0n;
|
|
90
|
-
while (a !== 0n) {
|
|
91
|
-
const q = b / a;
|
|
92
|
-
const r = b % a;
|
|
93
|
-
const m = x - (u * q);
|
|
94
|
-
const n = y - (v * q);
|
|
95
|
-
b = a;
|
|
96
|
-
a = r;
|
|
97
|
-
x = u;
|
|
98
|
-
y = v;
|
|
99
|
-
u = m;
|
|
100
|
-
v = n;
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
g: b,
|
|
104
|
-
x: x,
|
|
105
|
-
y: y
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Greatest-common divisor of two integers based on the iterative binary algorithm.
|
|
111
|
-
*
|
|
112
|
-
* @param a
|
|
113
|
-
* @param b
|
|
114
|
-
*
|
|
115
|
-
* @returns The greatest common divisor of a and b
|
|
116
|
-
*/
|
|
117
|
-
function gcd(a, b) {
|
|
118
|
-
let aAbs = (typeof a === 'number') ? BigInt(abs(a)) : abs(a);
|
|
119
|
-
let bAbs = (typeof b === 'number') ? BigInt(abs(b)) : abs(b);
|
|
120
|
-
if (aAbs === 0n) {
|
|
121
|
-
return bAbs;
|
|
122
|
-
} else if (bAbs === 0n) {
|
|
123
|
-
return aAbs;
|
|
124
|
-
}
|
|
125
|
-
let shift = 0n;
|
|
126
|
-
while (((aAbs | bAbs) & 1n) === 0n) {
|
|
127
|
-
aAbs >>= 1n;
|
|
128
|
-
bAbs >>= 1n;
|
|
129
|
-
shift++;
|
|
130
|
-
}
|
|
131
|
-
while ((aAbs & 1n) === 0n) {
|
|
132
|
-
aAbs >>= 1n;
|
|
133
|
-
}
|
|
134
|
-
do {
|
|
135
|
-
while ((bAbs & 1n) === 0n) {
|
|
136
|
-
bAbs >>= 1n;
|
|
137
|
-
}
|
|
138
|
-
if (aAbs > bAbs) {
|
|
139
|
-
const x = aAbs;
|
|
140
|
-
aAbs = bAbs;
|
|
141
|
-
bAbs = x;
|
|
142
|
-
}
|
|
143
|
-
bAbs -= aAbs;
|
|
144
|
-
} while (bAbs !== 0n);
|
|
145
|
-
// rescale
|
|
146
|
-
return aAbs << shift;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The least common multiple computed as abs(a*b)/gcd(a,b)
|
|
151
|
-
* @param a
|
|
152
|
-
* @param b
|
|
153
|
-
*
|
|
154
|
-
* @returns The least common multiple of a and b
|
|
155
|
-
*/
|
|
156
|
-
function lcm(a, b) {
|
|
157
|
-
if (typeof a === 'number') {
|
|
158
|
-
a = BigInt(a);
|
|
159
|
-
}
|
|
160
|
-
if (typeof b === 'number') {
|
|
161
|
-
b = BigInt(b);
|
|
162
|
-
}
|
|
163
|
-
if (a === 0n && b === 0n) {
|
|
164
|
-
return BigInt(0);
|
|
165
|
-
}
|
|
166
|
-
return abs(a * b) / gcd(a, b);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
|
171
|
-
*
|
|
172
|
-
* @param a
|
|
173
|
-
* @param b
|
|
174
|
-
*
|
|
175
|
-
* @returns Maximum of numbers a and b
|
|
176
|
-
*/
|
|
177
|
-
function max(a, b) {
|
|
178
|
-
return (a >= b) ? a : b;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
|
183
|
-
*
|
|
184
|
-
* @param a
|
|
185
|
-
* @param b
|
|
186
|
-
*
|
|
187
|
-
* @returns Minimum of numbers a and b
|
|
188
|
-
*/
|
|
189
|
-
function min(a, b) {
|
|
190
|
-
return (a >= b) ? b : a;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Finds the smallest positive element that is congruent to a in modulo n
|
|
195
|
-
*
|
|
196
|
-
* @remarks
|
|
197
|
-
* a and b must be the same type, either number or bigint
|
|
198
|
-
*
|
|
199
|
-
* @param a - An integer
|
|
200
|
-
* @param n - The modulo
|
|
201
|
-
*
|
|
202
|
-
* @throws {RangeError}
|
|
203
|
-
* Excpeption thrown when n is not > 0
|
|
204
|
-
*
|
|
205
|
-
* @returns A bigint with the smallest positive representation of a modulo n
|
|
206
|
-
*/
|
|
207
|
-
function toZn(a, n) {
|
|
208
|
-
if (typeof a === 'number') {
|
|
209
|
-
a = BigInt(a);
|
|
210
|
-
}
|
|
211
|
-
if (typeof n === 'number') {
|
|
212
|
-
n = BigInt(n);
|
|
213
|
-
}
|
|
214
|
-
if (n <= 0n) {
|
|
215
|
-
throw new RangeError('n must be > 0');
|
|
216
|
-
}
|
|
217
|
-
const aZn = a % n;
|
|
218
|
-
return (aZn < 0n) ? aZn + n : aZn;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Modular inverse.
|
|
223
|
-
*
|
|
224
|
-
* @param a The number to find an inverse for
|
|
225
|
-
* @param n The modulo
|
|
226
|
-
*
|
|
227
|
-
* @throws {RangeError}
|
|
228
|
-
* Excpeption thorwn when a does not have inverse modulo n
|
|
229
|
-
*
|
|
230
|
-
* @returns The inverse modulo n
|
|
231
|
-
*/
|
|
232
|
-
function modInv(a, n) {
|
|
233
|
-
const egcd = eGcd(toZn(a, n), n);
|
|
234
|
-
if (egcd.g !== 1n) {
|
|
235
|
-
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
236
|
-
} else {
|
|
237
|
-
return toZn(egcd.x, n);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
|
243
|
-
*
|
|
244
|
-
* @param b base
|
|
245
|
-
* @param e exponent
|
|
246
|
-
* @param n modulo
|
|
247
|
-
*
|
|
248
|
-
* @throws {RangeError}
|
|
249
|
-
* Excpeption thrown when n is not > 0
|
|
250
|
-
*
|
|
251
|
-
* @returns b**e mod n
|
|
252
|
-
*/
|
|
253
|
-
function modPow(b, e, n) {
|
|
254
|
-
if (typeof b === 'number') {
|
|
255
|
-
b = BigInt(b);
|
|
256
|
-
}
|
|
257
|
-
if (typeof e === 'number') {
|
|
258
|
-
e = BigInt(e);
|
|
259
|
-
}
|
|
260
|
-
if (typeof n === 'number') {
|
|
261
|
-
n = BigInt(n);
|
|
262
|
-
}
|
|
263
|
-
if (n <= 0n) {
|
|
264
|
-
throw new RangeError('n must be > 0');
|
|
265
|
-
} else if (n === 1n) {
|
|
266
|
-
return 0n;
|
|
267
|
-
}
|
|
268
|
-
b = toZn(b, n);
|
|
269
|
-
if (e < 0n) {
|
|
270
|
-
return modInv(modPow(b, abs(e), n), n);
|
|
271
|
-
}
|
|
272
|
-
let r = 1n;
|
|
273
|
-
while (e > 0) {
|
|
274
|
-
if ((e % 2n) === 1n) {
|
|
275
|
-
r = r * b % n;
|
|
276
|
-
}
|
|
277
|
-
e = e / 2n;
|
|
278
|
-
b = b ** 2n % n;
|
|
279
|
-
}
|
|
280
|
-
return r;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export { abs, bitLength, eGcd, gcd, lcm, max, min, modInv, modPow, toZn };
|
|
@@ -1,339 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.abs = abs;
|
|
7
|
-
exports.bitLength = bitLength;
|
|
8
|
-
exports.eGcd = eGcd;
|
|
9
|
-
exports.gcd = gcd;
|
|
10
|
-
exports.lcm = lcm;
|
|
11
|
-
exports.max = max;
|
|
12
|
-
exports.min = min;
|
|
13
|
-
exports.modInv = modInv;
|
|
14
|
-
exports.modPow = modPow;
|
|
15
|
-
exports.toZn = toZn;
|
|
16
|
-
|
|
17
|
-
/*
|
|
18
|
-
* bigint-mod-arith implementation:
|
|
19
|
-
* https://github.com/juanelas/bigint-mod-arith
|
|
20
|
-
*
|
|
21
|
-
* Full attribution follows:
|
|
22
|
-
*
|
|
23
|
-
* -------------------------------------------------------------------------
|
|
24
|
-
*
|
|
25
|
-
* MIT License
|
|
26
|
-
*
|
|
27
|
-
* Copyright (c) 2018 Juan Hernández Serrano
|
|
28
|
-
*
|
|
29
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
30
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
31
|
-
* in the Software without restriction, including without limitation the rights
|
|
32
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
33
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
34
|
-
* furnished to do so, subject to the following conditions:
|
|
35
|
-
*
|
|
36
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
37
|
-
* copies or substantial portions of the Software.
|
|
38
|
-
*
|
|
39
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
40
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
41
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
42
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
43
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
44
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
45
|
-
* SOFTWARE.
|
|
46
|
-
*
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0
|
|
51
|
-
*
|
|
52
|
-
* @param a
|
|
53
|
-
*
|
|
54
|
-
* @returns The absolute value of a
|
|
55
|
-
*/
|
|
56
|
-
function abs(a) {
|
|
57
|
-
return a >= 0 ? a : -a;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Returns the bitlength of a number
|
|
61
|
-
*
|
|
62
|
-
* @param a
|
|
63
|
-
* @returns The bit length
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
function bitLength(a) {
|
|
68
|
-
if (typeof a === 'number') {
|
|
69
|
-
a = BigInt(a);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (a === 1n) {
|
|
73
|
-
return 1;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
var bits = 1;
|
|
77
|
-
|
|
78
|
-
do {
|
|
79
|
-
bits++;
|
|
80
|
-
} while ((a >>= 1n) > 1n);
|
|
81
|
-
|
|
82
|
-
return bits;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm.
|
|
86
|
-
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
87
|
-
*
|
|
88
|
-
* @param a
|
|
89
|
-
* @param b
|
|
90
|
-
*
|
|
91
|
-
* @throws {RangeError}
|
|
92
|
-
* This excepction is thrown if a or b are less than 0
|
|
93
|
-
*
|
|
94
|
-
* @returns A triple (g, x, y), such that ax + by = g = gcd(a, b).
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
function eGcd(a, b) {
|
|
99
|
-
if (typeof a === 'number') {
|
|
100
|
-
a = BigInt(a);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (typeof b === 'number') {
|
|
104
|
-
b = BigInt(b);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (a <= 0n || b <= 0n) {
|
|
108
|
-
throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
var x = 0n;
|
|
112
|
-
var y = 1n;
|
|
113
|
-
var u = 1n;
|
|
114
|
-
var v = 0n;
|
|
115
|
-
|
|
116
|
-
while (a !== 0n) {
|
|
117
|
-
var q = b / a;
|
|
118
|
-
var r = b % a;
|
|
119
|
-
var m = x - u * q;
|
|
120
|
-
var n = y - v * q;
|
|
121
|
-
b = a;
|
|
122
|
-
a = r;
|
|
123
|
-
x = u;
|
|
124
|
-
y = v;
|
|
125
|
-
u = m;
|
|
126
|
-
v = n;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
g: b,
|
|
131
|
-
x: x,
|
|
132
|
-
y: y
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Greatest-common divisor of two integers based on the iterative binary algorithm.
|
|
137
|
-
*
|
|
138
|
-
* @param a
|
|
139
|
-
* @param b
|
|
140
|
-
*
|
|
141
|
-
* @returns The greatest common divisor of a and b
|
|
142
|
-
*/
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
function gcd(a, b) {
|
|
146
|
-
var aAbs = typeof a === 'number' ? BigInt(abs(a)) : abs(a);
|
|
147
|
-
var bAbs = typeof b === 'number' ? BigInt(abs(b)) : abs(b);
|
|
148
|
-
|
|
149
|
-
if (aAbs === 0n) {
|
|
150
|
-
return bAbs;
|
|
151
|
-
} else if (bAbs === 0n) {
|
|
152
|
-
return aAbs;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
var shift = 0n;
|
|
156
|
-
|
|
157
|
-
while (((aAbs | bAbs) & 1n) === 0n) {
|
|
158
|
-
aAbs >>= 1n;
|
|
159
|
-
bAbs >>= 1n;
|
|
160
|
-
shift++;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
while ((aAbs & 1n) === 0n) {
|
|
164
|
-
aAbs >>= 1n;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
do {
|
|
168
|
-
while ((bAbs & 1n) === 0n) {
|
|
169
|
-
bAbs >>= 1n;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (aAbs > bAbs) {
|
|
173
|
-
var x = aAbs;
|
|
174
|
-
aAbs = bAbs;
|
|
175
|
-
bAbs = x;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
bAbs -= aAbs;
|
|
179
|
-
} while (bAbs !== 0n); // rescale
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return aAbs << shift;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* The least common multiple computed as abs(a*b)/gcd(a,b)
|
|
186
|
-
* @param a
|
|
187
|
-
* @param b
|
|
188
|
-
*
|
|
189
|
-
* @returns The least common multiple of a and b
|
|
190
|
-
*/
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
function lcm(a, b) {
|
|
194
|
-
if (typeof a === 'number') {
|
|
195
|
-
a = BigInt(a);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (typeof b === 'number') {
|
|
199
|
-
b = BigInt(b);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (a === 0n && b === 0n) {
|
|
203
|
-
return BigInt(0);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return abs(a * b) / gcd(a, b);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
|
|
210
|
-
*
|
|
211
|
-
* @param a
|
|
212
|
-
* @param b
|
|
213
|
-
*
|
|
214
|
-
* @returns Maximum of numbers a and b
|
|
215
|
-
*/
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
function max(a, b) {
|
|
219
|
-
return a >= b ? a : b;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
|
|
223
|
-
*
|
|
224
|
-
* @param a
|
|
225
|
-
* @param b
|
|
226
|
-
*
|
|
227
|
-
* @returns Minimum of numbers a and b
|
|
228
|
-
*/
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
function min(a, b) {
|
|
232
|
-
return a >= b ? b : a;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Finds the smallest positive element that is congruent to a in modulo n
|
|
236
|
-
*
|
|
237
|
-
* @remarks
|
|
238
|
-
* a and b must be the same type, either number or bigint
|
|
239
|
-
*
|
|
240
|
-
* @param a - An integer
|
|
241
|
-
* @param n - The modulo
|
|
242
|
-
*
|
|
243
|
-
* @throws {RangeError}
|
|
244
|
-
* Excpeption thrown when n is not > 0
|
|
245
|
-
*
|
|
246
|
-
* @returns A bigint with the smallest positive representation of a modulo n
|
|
247
|
-
*/
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
function toZn(a, n) {
|
|
251
|
-
if (typeof a === 'number') {
|
|
252
|
-
a = BigInt(a);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (typeof n === 'number') {
|
|
256
|
-
n = BigInt(n);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (n <= 0n) {
|
|
260
|
-
throw new RangeError('n must be > 0');
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
var aZn = a % n;
|
|
264
|
-
return aZn < 0n ? aZn + n : aZn;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Modular inverse.
|
|
268
|
-
*
|
|
269
|
-
* @param a The number to find an inverse for
|
|
270
|
-
* @param n The modulo
|
|
271
|
-
*
|
|
272
|
-
* @throws {RangeError}
|
|
273
|
-
* Excpeption thorwn when a does not have inverse modulo n
|
|
274
|
-
*
|
|
275
|
-
* @returns The inverse modulo n
|
|
276
|
-
*/
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
function modInv(a, n) {
|
|
280
|
-
var egcd = eGcd(toZn(a, n), n);
|
|
281
|
-
|
|
282
|
-
if (egcd.g !== 1n) {
|
|
283
|
-
throw new RangeError("".concat(a.toString(), " does not have inverse modulo ").concat(n.toString())); // modular inverse does not exist
|
|
284
|
-
} else {
|
|
285
|
-
return toZn(egcd.x, n);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
|
|
290
|
-
*
|
|
291
|
-
* @param b base
|
|
292
|
-
* @param e exponent
|
|
293
|
-
* @param n modulo
|
|
294
|
-
*
|
|
295
|
-
* @throws {RangeError}
|
|
296
|
-
* Excpeption thrown when n is not > 0
|
|
297
|
-
*
|
|
298
|
-
* @returns b**e mod n
|
|
299
|
-
*/
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
function modPow(b, e, n) {
|
|
303
|
-
if (typeof b === 'number') {
|
|
304
|
-
b = BigInt(b);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
if (typeof e === 'number') {
|
|
308
|
-
e = BigInt(e);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (typeof n === 'number') {
|
|
312
|
-
n = BigInt(n);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
if (n <= 0n) {
|
|
316
|
-
throw new RangeError('n must be > 0');
|
|
317
|
-
} else if (n === 1n) {
|
|
318
|
-
return 0n;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
b = toZn(b, n);
|
|
322
|
-
|
|
323
|
-
if (e < 0n) {
|
|
324
|
-
return modInv(modPow(b, abs(e), n), n);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
var r = 1n;
|
|
328
|
-
|
|
329
|
-
while (e > 0) {
|
|
330
|
-
if (e % 2n === 1n) {
|
|
331
|
-
r = r * b % n;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
e = e / 2n;
|
|
335
|
-
b = Math.pow(b, 2n) % n;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return r;
|
|
339
|
-
}
|