@vardario/cognito-client 4.0.4 → 4.0.5
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/LICENSE +1 -1
- package/lib/bigint-math.d.ts +10 -0
- package/lib/bigint-math.js +67 -0
- package/lib/browser.js +1208 -0
- package/lib/cognito-client.js +18 -20
- package/lib/utils.d.ts +19 -14
- package/lib/utils.js +83 -59
- package/package.json +7 -11
package/LICENSE
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const abs: (n: bigint) => bigint;
|
|
2
|
+
export interface Egcd {
|
|
3
|
+
g: bigint;
|
|
4
|
+
x: bigint;
|
|
5
|
+
y: bigint;
|
|
6
|
+
}
|
|
7
|
+
export declare function eGcd(a: number | bigint, b: number | bigint): Egcd;
|
|
8
|
+
export declare function modInv(a: bigint, n: bigint): bigint;
|
|
9
|
+
export declare function toZn(a: bigint, n: bigint): bigint;
|
|
10
|
+
export declare function modPow(b: bigint, e: bigint, n: bigint): bigint;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const abs = (n) => (n < 0n ? -n : n);
|
|
2
|
+
export function eGcd(a, b) {
|
|
3
|
+
if (typeof a === 'number')
|
|
4
|
+
a = BigInt(a);
|
|
5
|
+
if (typeof b === 'number')
|
|
6
|
+
b = BigInt(b);
|
|
7
|
+
if (a <= 0n || b <= 0n)
|
|
8
|
+
throw new RangeError('a and b MUST be > 0'); // a and b MUST be positive
|
|
9
|
+
let x = 0n;
|
|
10
|
+
let y = 1n;
|
|
11
|
+
let u = 1n;
|
|
12
|
+
let v = 0n;
|
|
13
|
+
while (a !== 0n) {
|
|
14
|
+
const q = b / a;
|
|
15
|
+
const r = b % a;
|
|
16
|
+
const m = x - u * q;
|
|
17
|
+
const n = y - v * q;
|
|
18
|
+
b = a;
|
|
19
|
+
a = r;
|
|
20
|
+
x = u;
|
|
21
|
+
y = v;
|
|
22
|
+
u = m;
|
|
23
|
+
v = n;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
g: b,
|
|
27
|
+
x,
|
|
28
|
+
y
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function modInv(a, n) {
|
|
32
|
+
const egcd = eGcd(toZn(a, n), n);
|
|
33
|
+
if (egcd.g !== 1n) {
|
|
34
|
+
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return toZn(egcd.x, n);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function toZn(a, n) {
|
|
41
|
+
if (n <= 0n) {
|
|
42
|
+
throw new RangeError('n must be > 0');
|
|
43
|
+
}
|
|
44
|
+
const aZn = a % n;
|
|
45
|
+
return aZn < 0n ? aZn + n : aZn;
|
|
46
|
+
}
|
|
47
|
+
export function modPow(b, e, n) {
|
|
48
|
+
if (n <= 0n) {
|
|
49
|
+
throw new RangeError('n must be > 0');
|
|
50
|
+
}
|
|
51
|
+
else if (n === 1n) {
|
|
52
|
+
return 0n;
|
|
53
|
+
}
|
|
54
|
+
b = toZn(b, n);
|
|
55
|
+
if (e < 0n) {
|
|
56
|
+
return modInv(modPow(b, abs(e), n), n);
|
|
57
|
+
}
|
|
58
|
+
let r = 1n;
|
|
59
|
+
while (e > 0) {
|
|
60
|
+
if (e % 2n === 1n) {
|
|
61
|
+
r = (r * b) % n;
|
|
62
|
+
}
|
|
63
|
+
e = e / 2n;
|
|
64
|
+
b = b ** 2n % n;
|
|
65
|
+
}
|
|
66
|
+
return r;
|
|
67
|
+
}
|