@mtcute/core 0.1.0 → 0.1.1
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/cjs/base-client.js +4 -1
- package/cjs/base-client.js.map +1 -1
- package/cjs/network/auth-key.d.ts +3 -3
- package/cjs/network/auth-key.js +12 -12
- package/cjs/network/auth-key.js.map +1 -1
- package/cjs/network/authorization.js +62 -57
- package/cjs/network/authorization.js.map +1 -1
- package/cjs/network/mtproto-session.d.ts +2 -2
- package/cjs/network/mtproto-session.js +2 -2
- package/cjs/network/mtproto-session.js.map +1 -1
- package/cjs/network/multi-session-connection.d.ts +1 -1
- package/cjs/network/multi-session-connection.js +2 -2
- package/cjs/network/multi-session-connection.js.map +1 -1
- package/cjs/network/network-manager.d.ts +1 -0
- package/cjs/network/network-manager.js +21 -27
- package/cjs/network/network-manager.js.map +1 -1
- package/cjs/network/session-connection.d.ts +2 -1
- package/cjs/network/session-connection.js +48 -35
- package/cjs/network/session-connection.js.map +1 -1
- package/cjs/network/transports/intermediate.d.ts +3 -0
- package/cjs/network/transports/intermediate.js +5 -3
- package/cjs/network/transports/intermediate.js.map +1 -1
- package/cjs/network/transports/obfuscated.js +14 -18
- package/cjs/network/transports/obfuscated.js.map +1 -1
- package/cjs/storage/abstract.d.ts +23 -2
- package/cjs/storage/abstract.js.map +1 -1
- package/cjs/storage/json.js +8 -9
- package/cjs/storage/json.js.map +1 -1
- package/cjs/storage/localstorage.js +5 -2
- package/cjs/storage/localstorage.js.map +1 -1
- package/cjs/storage/memory.d.ts +7 -8
- package/cjs/storage/memory.js +7 -7
- package/cjs/storage/memory.js.map +1 -1
- package/cjs/utils/bigint-utils.d.ts +18 -11
- package/cjs/utils/bigint-utils.js +148 -47
- package/cjs/utils/bigint-utils.js.map +1 -1
- package/cjs/utils/buffer-utils.d.ts +7 -1
- package/cjs/utils/buffer-utils.js +16 -3
- package/cjs/utils/buffer-utils.js.map +1 -1
- package/cjs/utils/crypto/abstract.d.ts +16 -9
- package/cjs/utils/crypto/abstract.js +6 -9
- package/cjs/utils/crypto/abstract.js.map +1 -1
- package/cjs/utils/crypto/factorization.d.ts +2 -1
- package/cjs/utils/crypto/factorization.js +28 -36
- package/cjs/utils/crypto/factorization.js.map +1 -1
- package/cjs/utils/crypto/index.d.ts +4 -0
- package/cjs/utils/crypto/index.js +4 -0
- package/cjs/utils/crypto/index.js.map +1 -1
- package/cjs/utils/crypto/keys.d.ts +2 -2
- package/cjs/utils/crypto/keys.js +4 -4
- package/cjs/utils/crypto/keys.js.map +1 -1
- package/cjs/utils/crypto/miller-rabin.d.ts +2 -2
- package/cjs/utils/crypto/miller-rabin.js +20 -22
- package/cjs/utils/crypto/miller-rabin.js.map +1 -1
- package/cjs/utils/crypto/mtproto.d.ts +3 -3
- package/cjs/utils/crypto/mtproto.js +12 -12
- package/cjs/utils/crypto/mtproto.js.map +1 -1
- package/cjs/utils/crypto/node.d.ts +21 -0
- package/cjs/utils/crypto/node.js +74 -0
- package/cjs/utils/crypto/node.js.map +1 -0
- package/cjs/utils/crypto/password.js +22 -33
- package/cjs/utils/crypto/password.js.map +1 -1
- package/cjs/utils/crypto/wasm.d.ts +22 -0
- package/cjs/utils/crypto/wasm.js +41 -0
- package/cjs/utils/crypto/wasm.js.map +1 -0
- package/cjs/utils/crypto/web.d.ts +11 -0
- package/cjs/utils/crypto/web.js +40 -0
- package/cjs/utils/crypto/web.js.map +1 -0
- package/cjs/utils/deque.d.ts +1 -1
- package/cjs/utils/deque.js +9 -3
- package/cjs/utils/deque.js.map +1 -1
- package/cjs/utils/function-utils.d.ts +4 -1
- package/cjs/utils/function-utils.js +8 -1
- package/cjs/utils/function-utils.js.map +1 -1
- package/cjs/utils/index.d.ts +0 -1
- package/cjs/utils/index.js +0 -1
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/logger.d.ts +0 -1
- package/cjs/utils/logger.js +3 -4
- package/cjs/utils/logger.js.map +1 -1
- package/cjs/utils/lru-map.d.ts +1 -0
- package/cjs/utils/lru-map.js +6 -0
- package/cjs/utils/lru-map.js.map +1 -1
- package/cjs/utils/peer-utils.js +5 -1
- package/cjs/utils/peer-utils.js.map +1 -1
- package/cjs/utils/platform/crypto.js +2 -2
- package/cjs/utils/platform/crypto.js.map +1 -1
- package/cjs/utils/platform/crypto.web.js +2 -2
- package/cjs/utils/platform/crypto.web.js.map +1 -1
- package/cjs/utils/string-session.js +4 -2
- package/cjs/utils/string-session.js.map +1 -1
- package/esm/base-client.js +4 -1
- package/esm/base-client.js.map +1 -1
- package/esm/network/auth-key.d.ts +3 -3
- package/esm/network/auth-key.js +13 -13
- package/esm/network/auth-key.js.map +1 -1
- package/esm/network/authorization.js +64 -59
- package/esm/network/authorization.js.map +1 -1
- package/esm/network/mtproto-session.d.ts +2 -2
- package/esm/network/mtproto-session.js +2 -2
- package/esm/network/mtproto-session.js.map +1 -1
- package/esm/network/multi-session-connection.d.ts +1 -1
- package/esm/network/multi-session-connection.js +2 -2
- package/esm/network/multi-session-connection.js.map +1 -1
- package/esm/network/network-manager.d.ts +1 -0
- package/esm/network/network-manager.js +21 -27
- package/esm/network/network-manager.js.map +1 -1
- package/esm/network/session-connection.d.ts +2 -1
- package/esm/network/session-connection.js +50 -37
- package/esm/network/session-connection.js.map +1 -1
- package/esm/network/transports/intermediate.d.ts +3 -0
- package/esm/network/transports/intermediate.js +6 -4
- package/esm/network/transports/intermediate.js.map +1 -1
- package/esm/network/transports/obfuscated.js +15 -19
- package/esm/network/transports/obfuscated.js.map +1 -1
- package/esm/storage/abstract.d.ts +23 -2
- package/esm/storage/abstract.js.map +1 -1
- package/esm/storage/json.js +8 -9
- package/esm/storage/json.js.map +1 -1
- package/esm/storage/localstorage.js +5 -2
- package/esm/storage/localstorage.js.map +1 -1
- package/esm/storage/memory.d.ts +7 -8
- package/esm/storage/memory.js +7 -7
- package/esm/storage/memory.js.map +1 -1
- package/esm/utils/bigint-utils.d.ts +18 -11
- package/esm/utils/bigint-utils.js +142 -44
- package/esm/utils/bigint-utils.js.map +1 -1
- package/esm/utils/buffer-utils.d.ts +7 -1
- package/esm/utils/buffer-utils.js +14 -1
- package/esm/utils/buffer-utils.js.map +1 -1
- package/esm/utils/crypto/abstract.d.ts +16 -9
- package/esm/utils/crypto/abstract.js +6 -9
- package/esm/utils/crypto/abstract.js.map +1 -1
- package/esm/utils/crypto/factorization.d.ts +2 -1
- package/esm/utils/crypto/factorization.js +29 -34
- package/esm/utils/crypto/factorization.js.map +1 -1
- package/esm/utils/crypto/index.d.ts +4 -0
- package/esm/utils/crypto/index.js +4 -0
- package/esm/utils/crypto/index.js.map +1 -1
- package/esm/utils/crypto/keys.d.ts +2 -2
- package/esm/utils/crypto/keys.js +4 -4
- package/esm/utils/crypto/keys.js.map +1 -1
- package/esm/utils/crypto/miller-rabin.d.ts +2 -2
- package/esm/utils/crypto/miller-rabin.js +21 -20
- package/esm/utils/crypto/miller-rabin.js.map +1 -1
- package/esm/utils/crypto/mtproto.d.ts +3 -3
- package/esm/utils/crypto/mtproto.js +12 -12
- package/esm/utils/crypto/mtproto.js.map +1 -1
- package/esm/utils/crypto/node.d.ts +21 -0
- package/esm/utils/crypto/node.js +69 -0
- package/esm/utils/crypto/node.js.map +1 -0
- package/esm/utils/crypto/password.js +24 -32
- package/esm/utils/crypto/password.js.map +1 -1
- package/esm/utils/crypto/wasm.d.ts +22 -0
- package/esm/utils/crypto/wasm.js +37 -0
- package/esm/utils/crypto/wasm.js.map +1 -0
- package/esm/utils/crypto/web.d.ts +11 -0
- package/esm/utils/crypto/web.js +36 -0
- package/esm/utils/crypto/web.js.map +1 -0
- package/esm/utils/deque.d.ts +1 -1
- package/esm/utils/deque.js +9 -3
- package/esm/utils/deque.js.map +1 -1
- package/esm/utils/function-utils.d.ts +4 -1
- package/esm/utils/function-utils.js +8 -1
- package/esm/utils/function-utils.js.map +1 -1
- package/esm/utils/index.d.ts +0 -1
- package/esm/utils/index.js +0 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/logger.d.ts +0 -1
- package/esm/utils/logger.js +3 -4
- package/esm/utils/logger.js.map +1 -1
- package/esm/utils/lru-map.d.ts +1 -0
- package/esm/utils/lru-map.js +6 -0
- package/esm/utils/lru-map.js.map +1 -1
- package/esm/utils/peer-utils.js +5 -1
- package/esm/utils/peer-utils.js.map +1 -1
- package/esm/utils/platform/crypto.js +1 -1
- package/esm/utils/platform/crypto.js.map +1 -1
- package/esm/utils/platform/crypto.web.js +2 -2
- package/esm/utils/platform/crypto.web.js.map +1 -1
- package/esm/utils/string-session.js +4 -2
- package/esm/utils/string-session.js.map +1 -1
- package/package.json +4 -5
- package/cjs/utils/crypto/common.d.ts +0 -12
- package/cjs/utils/crypto/common.js +0 -54
- package/cjs/utils/crypto/common.js.map +0 -1
- package/cjs/utils/crypto/node-crypto.d.ts +0 -10
- package/cjs/utils/crypto/node-crypto.js +0 -46
- package/cjs/utils/crypto/node-crypto.js.map +0 -1
- package/cjs/utils/crypto/subtle.d.ts +0 -12
- package/cjs/utils/crypto/subtle.js +0 -86
- package/cjs/utils/crypto/subtle.js.map +0 -1
- package/cjs/utils/flood-control.d.ts +0 -14
- package/cjs/utils/flood-control.js +0 -65
- package/cjs/utils/flood-control.js.map +0 -1
- package/cjs/utils/platform/random.d.ts +0 -1
- package/cjs/utils/platform/random.js +0 -7
- package/cjs/utils/platform/random.js.map +0 -1
- package/cjs/utils/platform/random.web.d.ts +0 -1
- package/cjs/utils/platform/random.web.js +0 -10
- package/cjs/utils/platform/random.web.js.map +0 -1
- package/esm/utils/crypto/common.d.ts +0 -12
- package/esm/utils/crypto/common.js +0 -50
- package/esm/utils/crypto/common.js.map +0 -1
- package/esm/utils/crypto/node-crypto.d.ts +0 -10
- package/esm/utils/crypto/node-crypto.js +0 -42
- package/esm/utils/crypto/node-crypto.js.map +0 -1
- package/esm/utils/crypto/subtle.d.ts +0 -12
- package/esm/utils/crypto/subtle.js +0 -59
- package/esm/utils/crypto/subtle.js.map +0 -1
- package/esm/utils/flood-control.d.ts +0 -14
- package/esm/utils/flood-control.js +0 -61
- package/esm/utils/flood-control.js.map +0 -1
- package/esm/utils/platform/random.d.ts +0 -1
- package/esm/utils/platform/random.js +0 -4
- package/esm/utils/platform/random.js.map +0 -1
- package/esm/utils/platform/random.web.d.ts +0 -1
- package/esm/utils/platform/random.web.js +0 -6
- package/esm/utils/platform/random.web.js.map +0 -1
|
@@ -1,68 +1,94 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.twoMultiplicity = exports.randomBigIntInRange = exports.randomBigIntBits = exports.randomBigInt = exports.bufferToBigInt = exports.bigIntToBuffer = void 0;
|
|
7
|
-
const big_integer_1 = __importDefault(require("big-integer"));
|
|
3
|
+
exports.bigIntModInv = exports.bigIntModPow = exports.bigIntGcd = exports.bigIntAbs = exports.bigIntMin = exports.twoMultiplicity = exports.randomBigIntInRange = exports.randomBigIntBits = exports.randomBigInt = exports.bufferToBigInt = exports.bigIntToBuffer = exports.bigIntBitLength = void 0;
|
|
8
4
|
const buffer_utils_js_1 = require("./buffer-utils.js");
|
|
5
|
+
/**
|
|
6
|
+
* Get the minimum number of bits required to represent a number
|
|
7
|
+
*/
|
|
8
|
+
function bigIntBitLength(n) {
|
|
9
|
+
// not the fastest way, but at least not .toString(2) and not too complex
|
|
10
|
+
// taken from: https://stackoverflow.com/a/76616288/22656950
|
|
11
|
+
const i = (n.toString(16).length - 1) * 4;
|
|
12
|
+
return i + 32 - Math.clz32(Number(n >> BigInt(i)));
|
|
13
|
+
}
|
|
14
|
+
exports.bigIntBitLength = bigIntBitLength;
|
|
9
15
|
/**
|
|
10
16
|
* Convert a big integer to a buffer
|
|
11
17
|
*
|
|
12
18
|
* @param value Value to convert
|
|
13
|
-
* @param length Length of the resulting buffer (by default it's
|
|
19
|
+
* @param length Length of the resulting buffer (by default it's the minimum required)
|
|
14
20
|
* @param le Whether to use little-endian encoding
|
|
15
21
|
*/
|
|
16
22
|
function bigIntToBuffer(value, length = 0, le = false) {
|
|
17
|
-
const
|
|
18
|
-
|
|
23
|
+
const bits = bigIntBitLength(value);
|
|
24
|
+
const bytes = Math.ceil(bits / 8);
|
|
25
|
+
if (length !== 0 && bytes > length) {
|
|
19
26
|
throw new Error('Value out of bounds');
|
|
20
27
|
}
|
|
21
|
-
if (length
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
if (length === 0)
|
|
29
|
+
length = bytes;
|
|
30
|
+
const buf = new ArrayBuffer(length);
|
|
31
|
+
const u8 = new Uint8Array(buf);
|
|
32
|
+
const unaligned = length % 8;
|
|
33
|
+
const dv = new DataView(buf, 0, length - unaligned);
|
|
34
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
35
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
36
|
+
dv.setBigUint64(i, value & 0xffffffffffffffffn, true);
|
|
37
|
+
value >>= 64n;
|
|
25
38
|
}
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
if (unaligned > 0) {
|
|
40
|
+
for (let i = length - unaligned; i < length; i++) {
|
|
41
|
+
u8[i] = Number(value & 0xffn);
|
|
42
|
+
value >>= 8n;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!le)
|
|
46
|
+
u8.reverse();
|
|
47
|
+
return u8;
|
|
31
48
|
}
|
|
32
49
|
exports.bigIntToBuffer = bigIntToBuffer;
|
|
33
50
|
/**
|
|
34
51
|
* Convert a buffer to a big integer
|
|
35
52
|
*
|
|
36
53
|
* @param buffer Buffer to convert
|
|
37
|
-
* @param offset Offset to start reading from
|
|
38
|
-
* @param length Length to read
|
|
39
54
|
* @param le Whether to use little-endian encoding
|
|
40
55
|
*/
|
|
41
|
-
function bufferToBigInt(buffer,
|
|
42
|
-
const arr = [...buffer.subarray(offset, offset + length)];
|
|
56
|
+
function bufferToBigInt(buffer, le = false) {
|
|
43
57
|
if (le)
|
|
44
|
-
|
|
45
|
-
|
|
58
|
+
buffer = (0, buffer_utils_js_1.bufferToReversed)(buffer);
|
|
59
|
+
const unaligned = buffer.length % 8;
|
|
60
|
+
const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned);
|
|
61
|
+
let res = 0n;
|
|
62
|
+
// it is faster to work with 64-bit words than with bytes directly
|
|
63
|
+
for (let i = 0; i < dv.byteLength; i += 8) {
|
|
64
|
+
res = (res << 64n) | BigInt(dv.getBigUint64(i, false));
|
|
65
|
+
}
|
|
66
|
+
if (unaligned > 0) {
|
|
67
|
+
for (let i = buffer.length - unaligned; i < buffer.length; i++) {
|
|
68
|
+
res = (res << 8n) | BigInt(buffer[i]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return res;
|
|
46
72
|
}
|
|
47
73
|
exports.bufferToBigInt = bufferToBigInt;
|
|
48
74
|
/**
|
|
49
|
-
* Generate a random big integer of the given size (in bytes)
|
|
75
|
+
* Generate a cryptographically safe random big integer of the given size (in bytes)
|
|
50
76
|
* @param size Size in bytes
|
|
51
77
|
*/
|
|
52
|
-
function randomBigInt(size) {
|
|
53
|
-
return bufferToBigInt(
|
|
78
|
+
function randomBigInt(crypto, size) {
|
|
79
|
+
return bufferToBigInt(crypto.randomBytes(size));
|
|
54
80
|
}
|
|
55
81
|
exports.randomBigInt = randomBigInt;
|
|
56
82
|
/**
|
|
57
83
|
* Generate a random big integer of the given size (in bits)
|
|
58
84
|
* @param bits
|
|
59
85
|
*/
|
|
60
|
-
function randomBigIntBits(bits) {
|
|
61
|
-
let num = randomBigInt(Math.ceil(bits / 8));
|
|
62
|
-
const bitLength = num
|
|
63
|
-
if (bitLength
|
|
64
|
-
const toTrim =
|
|
65
|
-
num
|
|
86
|
+
function randomBigIntBits(crypto, bits) {
|
|
87
|
+
let num = randomBigInt(crypto, Math.ceil(bits / 8));
|
|
88
|
+
const bitLength = bigIntBitLength(num);
|
|
89
|
+
if (bitLength > bits) {
|
|
90
|
+
const toTrim = bitLength - bits;
|
|
91
|
+
num >>= BigInt(toTrim);
|
|
66
92
|
}
|
|
67
93
|
return num;
|
|
68
94
|
}
|
|
@@ -73,15 +99,15 @@ exports.randomBigIntBits = randomBigIntBits;
|
|
|
73
99
|
* @param max Maximum value (exclusive)
|
|
74
100
|
* @param min Minimum value (inclusive)
|
|
75
101
|
*/
|
|
76
|
-
function randomBigIntInRange(max, min =
|
|
77
|
-
const interval = max
|
|
78
|
-
if (interval
|
|
102
|
+
function randomBigIntInRange(crypto, max, min = 1n) {
|
|
103
|
+
const interval = max - min;
|
|
104
|
+
if (interval < 0n)
|
|
79
105
|
throw new Error('expected min < max');
|
|
80
|
-
const byteSize = interval
|
|
81
|
-
let result = randomBigInt(byteSize);
|
|
82
|
-
while (result
|
|
83
|
-
result
|
|
84
|
-
return min
|
|
106
|
+
const byteSize = bigIntBitLength(interval) / 8;
|
|
107
|
+
let result = randomBigInt(crypto, byteSize);
|
|
108
|
+
while (result > interval)
|
|
109
|
+
result -= interval;
|
|
110
|
+
return min + result;
|
|
85
111
|
}
|
|
86
112
|
exports.randomBigIntInRange = randomBigIntInRange;
|
|
87
113
|
/**
|
|
@@ -89,16 +115,91 @@ exports.randomBigIntInRange = randomBigIntInRange;
|
|
|
89
115
|
* @param n
|
|
90
116
|
*/
|
|
91
117
|
function twoMultiplicity(n) {
|
|
92
|
-
if (n ===
|
|
93
|
-
return
|
|
94
|
-
let m =
|
|
95
|
-
let pow =
|
|
118
|
+
if (n === 0n)
|
|
119
|
+
return 0n;
|
|
120
|
+
let m = 0n;
|
|
121
|
+
let pow = 1n;
|
|
96
122
|
while (true) {
|
|
97
|
-
if (
|
|
123
|
+
if ((n & pow) !== 0n)
|
|
98
124
|
return m;
|
|
99
|
-
m
|
|
100
|
-
pow
|
|
125
|
+
m += 1n;
|
|
126
|
+
pow <<= 1n;
|
|
101
127
|
}
|
|
102
128
|
}
|
|
103
129
|
exports.twoMultiplicity = twoMultiplicity;
|
|
130
|
+
function bigIntMin(a, b) {
|
|
131
|
+
return a < b ? a : b;
|
|
132
|
+
}
|
|
133
|
+
exports.bigIntMin = bigIntMin;
|
|
134
|
+
function bigIntAbs(a) {
|
|
135
|
+
return a < 0n ? -a : a;
|
|
136
|
+
}
|
|
137
|
+
exports.bigIntAbs = bigIntAbs;
|
|
138
|
+
function bigIntGcd(a, b) {
|
|
139
|
+
// using euclidean algorithm is fast enough on smaller numbers
|
|
140
|
+
// https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
|
|
141
|
+
while (b !== 0n) {
|
|
142
|
+
const t = b;
|
|
143
|
+
b = a % b;
|
|
144
|
+
a = t;
|
|
145
|
+
}
|
|
146
|
+
return a;
|
|
147
|
+
}
|
|
148
|
+
exports.bigIntGcd = bigIntGcd;
|
|
149
|
+
function bigIntModPow(base, exp, mod) {
|
|
150
|
+
// using the binary method is good enough for our use case
|
|
151
|
+
// https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
|
|
152
|
+
base %= mod;
|
|
153
|
+
let result = 1n;
|
|
154
|
+
while (exp > 0n) {
|
|
155
|
+
if (exp % 2n === 1n) {
|
|
156
|
+
result = (result * base) % mod;
|
|
157
|
+
}
|
|
158
|
+
exp >>= 1n;
|
|
159
|
+
base = base ** 2n % mod;
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
exports.bigIntModPow = bigIntModPow;
|
|
164
|
+
// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license
|
|
165
|
+
function eGcd(a, b) {
|
|
166
|
+
let x = 0n;
|
|
167
|
+
let y = 1n;
|
|
168
|
+
let u = 1n;
|
|
169
|
+
let v = 0n;
|
|
170
|
+
while (a !== 0n) {
|
|
171
|
+
const q = b / a;
|
|
172
|
+
const r = b % a;
|
|
173
|
+
const m = x - u * q;
|
|
174
|
+
const n = y - v * q;
|
|
175
|
+
b = a;
|
|
176
|
+
a = r;
|
|
177
|
+
x = u;
|
|
178
|
+
y = v;
|
|
179
|
+
u = m;
|
|
180
|
+
v = n;
|
|
181
|
+
}
|
|
182
|
+
return [b, x, y];
|
|
183
|
+
}
|
|
184
|
+
function toZn(a, n) {
|
|
185
|
+
if (typeof a === 'number')
|
|
186
|
+
a = BigInt(a);
|
|
187
|
+
if (typeof n === 'number')
|
|
188
|
+
n = BigInt(n);
|
|
189
|
+
if (n <= 0n) {
|
|
190
|
+
throw new RangeError('n must be > 0');
|
|
191
|
+
}
|
|
192
|
+
const aZn = a % n;
|
|
193
|
+
return aZn < 0n ? aZn + n : aZn;
|
|
194
|
+
}
|
|
195
|
+
function bigIntModInv(a, n) {
|
|
196
|
+
const [g, x] = eGcd(toZn(a, n), n);
|
|
197
|
+
if (g !== 1n) {
|
|
198
|
+
throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`); // modular inverse does not exist
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
return toZn(x, n);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.bigIntModInv = bigIntModInv;
|
|
104
205
|
//# sourceMappingURL=bigint-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAgD;AAEhD,uDAA+C;AAE/C;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,KAAiB,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK;IACpE,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAA;IAEtC,IAAI,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,MAAM,EAAE;QACvC,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CA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C,CAAC,CAAC,CAAA;KACzB;AACL,CAAC;AAXD,0CAWC","sourcesContent":["import bigInt, { BigInteger } from 'big-integer'\n\nimport { randomBytes } from './buffer-utils.js'\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's computed automatically)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: BigInteger, length = 0, le = false): Uint8Array {\n const array = value.toArray(256).value\n\n if (length !== 0 && array.length > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length !== 0) {\n // padding\n while (array.length !== length) array.unshift(0)\n }\n\n if (le) array.reverse()\n\n const buffer = new Uint8Array(length || array.length)\n buffer.set(array, 0)\n\n return buffer\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param offset Offset to start reading from\n * @param length Length to read\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, offset = 0, length = buffer.length, le = false): BigInteger {\n const arr = [...buffer.subarray(offset, offset + length)]\n\n if (le) arr.reverse()\n\n return bigInt.fromArray(arr as unknown as number[], 256)\n}\n\n/**\n * Generate a random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(size: number): BigInteger {\n return bufferToBigInt(randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(bits: number): BigInteger {\n let num = randomBigInt(Math.ceil(bits / 8))\n\n const bitLength = num.bitLength()\n\n if (bitLength.gt(bits)) {\n const toTrim = bigInt.randBetween(bitLength.minus(bits), 8)\n num = num.shiftRight(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(max: BigInteger, min = bigInt.one): BigInteger {\n const interval = max.minus(min)\n if (interval.isNegative()) throw new Error('expected min < max')\n\n const byteSize = interval.bitLength().divide(8).toJSNumber()\n\n let result = randomBigInt(byteSize)\n while (result.gt(interval)) result = result.minus(interval)\n\n return min.plus(result)\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: BigInteger): BigInteger {\n if (n === bigInt.zero) return bigInt.zero\n\n let m = bigInt.zero\n let pow = bigInt.one\n\n while (true) {\n if (!n.and(pow).isZero()) return m\n m = m.plus(bigInt.one)\n pow = pow.shiftLeft(1)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bigint-utils.js","sourceRoot":"","sources":["../../../src/utils/bigint-utils.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AAGpD;;GAEG;AACH,SAAgB,eAAe,CAAC,CAAS;IACrC,yEAAyE;IACzE,4DAA4D;IAE5D,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;IAEzC,OAAO,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACtD,CAAC;AAPD,0CAOC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,GAAG,KAAK;IAChE,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAA;IAEjC,IAAI,MAAM,KAAK,CAAC,IAAI,KAAK,GAAG,MAAM,EAAE;QAChC,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;KACzC;IAED,IAAI,MAAM,KAAK,CAAC;QAAE,MAAM,GAAG,KAAK,CAAA;IAEhC,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,MAAM,CAAC,CAAA;IACnC,MAAM,EAAE,GAAG,IAAI,UA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AAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IACxC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IAExC,IAAI,CAAC,IAAI,EAAE,EAAE;QACT,MAAM,IAAI,UAAU,CAAC,eAAe,CAAC,CAAA;KACxC;IAED,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAA;IAEjB,OAAO,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAA;AACnC,CAAC;AAED,SAAgB,YAAY,CAAC,CAAS,EAAE,CAAS;IAC7C,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAElC,IAAI,CAAC,KAAK,EAAE,EAAE;QACV,MAAM,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,iCAAiC,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA,CAAC,iCAAiC;KACzH;SAAM;QACH,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KACpB;AACL,CAAC;AARD,oCAQC","sourcesContent":["import { bufferToReversed } from './buffer-utils.js'\nimport { ICryptoProvider } from './crypto/abstract.js'\n\n/**\n * Get the minimum number of bits required to represent a number\n */\nexport function bigIntBitLength(n: bigint) {\n // not the fastest way, but at least not .toString(2) and not too complex\n // taken from: https://stackoverflow.com/a/76616288/22656950\n\n const i = (n.toString(16).length - 1) * 4\n\n return i + 32 - Math.clz32(Number(n >> BigInt(i)))\n}\n\n/**\n * Convert a big integer to a buffer\n *\n * @param value Value to convert\n * @param length Length of the resulting buffer (by default it's the minimum required)\n * @param le Whether to use little-endian encoding\n */\nexport function bigIntToBuffer(value: bigint, length = 0, le = false): Uint8Array {\n const bits = bigIntBitLength(value)\n const bytes = Math.ceil(bits / 8)\n\n if (length !== 0 && bytes > length) {\n throw new Error('Value out of bounds')\n }\n\n if (length === 0) length = bytes\n\n const buf = new ArrayBuffer(length)\n const u8 = new Uint8Array(buf)\n\n const unaligned = length % 8\n const dv = new DataView(buf, 0, length - unaligned)\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n dv.setBigUint64(i, value & 0xffffffffffffffffn, true)\n value >>= 64n\n }\n\n if (unaligned > 0) {\n for (let i = length - unaligned; i < length; i++) {\n u8[i] = Number(value & 0xffn)\n value >>= 8n\n }\n }\n\n if (!le) u8.reverse()\n\n return u8\n}\n\n/**\n * Convert a buffer to a big integer\n *\n * @param buffer Buffer to convert\n * @param le Whether to use little-endian encoding\n */\nexport function bufferToBigInt(buffer: Uint8Array, le = false): bigint {\n if (le) buffer = bufferToReversed(buffer)\n\n const unaligned = buffer.length % 8\n const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength - unaligned)\n\n let res = 0n\n\n // it is faster to work with 64-bit words than with bytes directly\n for (let i = 0; i < dv.byteLength; i += 8) {\n res = (res << 64n) | BigInt(dv.getBigUint64(i, false))\n }\n\n if (unaligned > 0) {\n for (let i = buffer.length - unaligned; i < buffer.length; i++) {\n res = (res << 8n) | BigInt(buffer[i])\n }\n }\n\n return res\n}\n\n/**\n * Generate a cryptographically safe random big integer of the given size (in bytes)\n * @param size Size in bytes\n */\nexport function randomBigInt(crypto: ICryptoProvider, size: number): bigint {\n return bufferToBigInt(crypto.randomBytes(size))\n}\n\n/**\n * Generate a random big integer of the given size (in bits)\n * @param bits\n */\nexport function randomBigIntBits(crypto: ICryptoProvider, bits: number): bigint {\n let num = randomBigInt(crypto, Math.ceil(bits / 8))\n\n const bitLength = bigIntBitLength(num)\n\n if (bitLength > bits) {\n const toTrim = bitLength - bits\n num >>= BigInt(toTrim)\n }\n\n return num\n}\n\n/**\n * Generate a random big integer in the range [min, max)\n *\n * @param max Maximum value (exclusive)\n * @param min Minimum value (inclusive)\n */\nexport function randomBigIntInRange(crypto: ICryptoProvider, max: bigint, min = 1n): bigint {\n const interval = max - min\n if (interval < 0n) throw new Error('expected min < max')\n\n const byteSize = bigIntBitLength(interval) / 8\n\n let result = randomBigInt(crypto, byteSize)\n while (result > interval) result -= interval\n\n return min + result\n}\n\n/**\n * Compute the multiplicity of 2 in the prime factorization of n\n * @param n\n */\nexport function twoMultiplicity(n: bigint): bigint {\n if (n === 0n) return 0n\n\n let m = 0n\n let pow = 1n\n\n while (true) {\n if ((n & pow) !== 0n) return m\n m += 1n\n pow <<= 1n\n }\n}\n\nexport function bigIntMin(a: bigint, b: bigint): bigint {\n return a < b ? a : b\n}\n\nexport function bigIntAbs(a: bigint): bigint {\n return a < 0n ? -a : a\n}\n\nexport function bigIntGcd(a: bigint, b: bigint): bigint {\n // using euclidean algorithm is fast enough on smaller numbers\n // https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations\n\n while (b !== 0n) {\n const t = b\n b = a % b\n a = t\n }\n\n return a\n}\n\nexport function bigIntModPow(base: bigint, exp: bigint, mod: bigint): bigint {\n // using the binary method is good enough for our use case\n // https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method\n\n base %= mod\n\n let result = 1n\n\n while (exp > 0n) {\n if (exp % 2n === 1n) {\n result = (result * base) % mod\n }\n\n exp >>= 1n\n base = base ** 2n % mod\n }\n\n return result\n}\n\n// below code is based on https://github.com/juanelas/bigint-mod-arith, MIT license\n\nfunction eGcd(a: bigint, b: bigint): [bigint, bigint, bigint] {\n let x = 0n\n let y = 1n\n let u = 1n\n let v = 0n\n\n while (a !== 0n) {\n const q = b / a\n const r: bigint = b % a\n const m = x - u * q\n const n = y - v * q\n b = a\n a = r\n x = u\n y = v\n u = m\n v = n\n }\n\n return [b, x, y]\n}\n\nfunction toZn(a: number | bigint, n: number | bigint): bigint {\n if (typeof a === 'number') a = BigInt(a)\n if (typeof n === 'number') n = BigInt(n)\n\n if (n <= 0n) {\n throw new RangeError('n must be > 0')\n }\n\n const aZn = a % n\n\n return aZn < 0n ? aZn + n : aZn\n}\n\nexport function bigIntModInv(a: bigint, n: bigint): bigint {\n const [g, x] = eGcd(toZn(a, n), n)\n\n if (g !== 1n) {\n throw new RangeError(`${a.toString()} does not have inverse modulo ${n.toString()}`) // modular inverse does not exist\n } else {\n return toZn(x, n)\n }\n}\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { _randomBytes as randomBytes } from './platform/random.js';
|
|
2
1
|
/**
|
|
3
2
|
* Check if two buffers are equal
|
|
4
3
|
*
|
|
@@ -18,4 +17,11 @@ export declare function cloneBuffer(buf: Uint8Array, start?: number, end?: numbe
|
|
|
18
17
|
* Concatenate multiple buffers into one
|
|
19
18
|
*/
|
|
20
19
|
export declare function concatBuffers(buffers: Uint8Array[]): Uint8Array;
|
|
20
|
+
/**
|
|
21
|
+
* Shortcut for creating a DataView from a Uint8Array
|
|
22
|
+
*/
|
|
21
23
|
export declare function dataViewFromBuffer(buf: Uint8Array): DataView;
|
|
24
|
+
/**
|
|
25
|
+
* Reverse a buffer (or a part of it) into a new buffer
|
|
26
|
+
*/
|
|
27
|
+
export declare function bufferToReversed(buf: Uint8Array, start?: number, end?: number): Uint8Array;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var random_js_1 = require("./platform/random.js");
|
|
5
|
-
Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function () { return random_js_1._randomBytes; } });
|
|
3
|
+
exports.bufferToReversed = exports.dataViewFromBuffer = exports.concatBuffers = exports.cloneBuffer = exports.buffersEqual = void 0;
|
|
6
4
|
/**
|
|
7
5
|
* Check if two buffers are equal
|
|
8
6
|
*
|
|
@@ -56,8 +54,23 @@ function concatBuffers(buffers) {
|
|
|
56
54
|
return ret;
|
|
57
55
|
}
|
|
58
56
|
exports.concatBuffers = concatBuffers;
|
|
57
|
+
/**
|
|
58
|
+
* Shortcut for creating a DataView from a Uint8Array
|
|
59
|
+
*/
|
|
59
60
|
function dataViewFromBuffer(buf) {
|
|
60
61
|
return new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
61
62
|
}
|
|
62
63
|
exports.dataViewFromBuffer = dataViewFromBuffer;
|
|
64
|
+
/**
|
|
65
|
+
* Reverse a buffer (or a part of it) into a new buffer
|
|
66
|
+
*/
|
|
67
|
+
function bufferToReversed(buf, start = 0, end = buf.length) {
|
|
68
|
+
const len = end - start;
|
|
69
|
+
const ret = new Uint8Array(len);
|
|
70
|
+
for (let i = 0; i < len; i++) {
|
|
71
|
+
ret[i] = buf[end - i - 1];
|
|
72
|
+
}
|
|
73
|
+
return ret;
|
|
74
|
+
}
|
|
75
|
+
exports.bufferToReversed = bufferToReversed;
|
|
63
76
|
//# sourceMappingURL=buffer-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-utils.js","sourceRoot":"","sources":["../../../src/utils/buffer-utils.ts"],"names":[],"mappings":";;;AAAA
|
|
1
|
+
{"version":3,"file":"buffer-utils.js","sourceRoot":"","sources":["../../../src/utils/buffer-utils.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,CAAa,EAAE,CAAa;IACrD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAEvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC/B,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;KAClC;IAED,OAAO,IAAI,CAAA;AACf,CAAC;AARD,oCAQC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,GAAe,EAAE,KAAK,GAAG,CAAC,EAAE,GA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,GAAG,GAAG,KAAK,CAAA;IACvB,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;IAE/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC1B,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;KAC5B;IAED,OAAO,GAAG,CAAA;AACd,CAAC;AATD,4CASC","sourcesContent":["/**\n * Check if two buffers are equal\n *\n * @param a First buffer\n * @param b Second buffer\n */\nexport function buffersEqual(a: Uint8Array, b: Uint8Array): boolean {\n if (a.length !== b.length) return false\n\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false\n }\n\n return true\n}\n\n/**\n * Copy a buffer\n *\n * @param buf Buffer to copy\n * @param start Start offset\n * @param end End offset\n */\nexport function cloneBuffer(buf: Uint8Array, start = 0, end = buf.length): Uint8Array {\n const ret = new Uint8Array(end - start)\n ret.set(buf.subarray(start, end))\n\n return ret\n}\n\n/**\n * Concatenate multiple buffers into one\n */\nexport function concatBuffers(buffers: Uint8Array[]): Uint8Array {\n if (buffers.length === 1) return buffers[0]\n\n /* eslint-disable no-restricted-globals */\n if (typeof Buffer !== 'undefined') {\n return Buffer.concat(buffers)\n }\n /* eslint-enable no-restricted-globals */\n\n let length = 0\n\n for (const buf of buffers) {\n length += buf.length\n }\n\n const ret = new Uint8Array(length)\n let offset = 0\n\n for (const buf of buffers) {\n ret.set(buf, offset)\n offset += buf.length\n }\n\n return ret\n}\n\n/**\n * Shortcut for creating a DataView from a Uint8Array\n */\nexport function dataViewFromBuffer(buf: Uint8Array): DataView {\n return new DataView(buf.buffer, buf.byteOffset, buf.byteLength)\n}\n\n/**\n * Reverse a buffer (or a part of it) into a new buffer\n */\nexport function bufferToReversed(buf: Uint8Array, start = 0, end = buf.length): Uint8Array {\n const len = end - start\n const ret = new Uint8Array(len)\n\n for (let i = 0; i < len; i++) {\n ret[i] = buf[end - i - 1]\n }\n\n return ret\n}\n"]}
|
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { MaybeAsync } from '../../types/index.js';
|
|
2
2
|
export interface IEncryptionScheme {
|
|
3
|
-
encrypt(data: Uint8Array):
|
|
4
|
-
decrypt(data: Uint8Array):
|
|
3
|
+
encrypt(data: Uint8Array): Uint8Array;
|
|
4
|
+
decrypt(data: Uint8Array): Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
export interface IAesCtr {
|
|
7
|
+
process(data: Uint8Array): Uint8Array;
|
|
8
|
+
close?(): void;
|
|
5
9
|
}
|
|
6
10
|
export interface ICryptoProvider {
|
|
7
11
|
initialize?(): MaybeAsync<void>;
|
|
8
|
-
sha1(data: Uint8Array):
|
|
9
|
-
sha256(data: Uint8Array):
|
|
12
|
+
sha1(data: Uint8Array): Uint8Array;
|
|
13
|
+
sha256(data: Uint8Array): Uint8Array;
|
|
10
14
|
pbkdf2(password: Uint8Array, salt: Uint8Array, iterations: number, keylen?: number, // = 64
|
|
11
15
|
algo?: string): MaybeAsync<Uint8Array>;
|
|
12
16
|
hmacSha256(data: Uint8Array, key: Uint8Array): MaybeAsync<Uint8Array>;
|
|
13
|
-
createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean):
|
|
14
|
-
createAesIge(key: Uint8Array, iv: Uint8Array):
|
|
15
|
-
createAesEcb(key: Uint8Array): MaybeAsync<IEncryptionScheme>;
|
|
17
|
+
createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean): IAesCtr;
|
|
18
|
+
createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme;
|
|
16
19
|
factorizePQ(pq: Uint8Array): MaybeAsync<[Uint8Array, Uint8Array]>;
|
|
20
|
+
gzip(data: Uint8Array, maxSize: number): Uint8Array | null;
|
|
21
|
+
gunzip(data: Uint8Array): Uint8Array;
|
|
22
|
+
randomFill(buf: Uint8Array): void;
|
|
23
|
+
randomBytes(size: number): Uint8Array;
|
|
17
24
|
}
|
|
18
25
|
export declare abstract class BaseCryptoProvider {
|
|
19
|
-
|
|
26
|
+
abstract randomFill(buf: Uint8Array): void;
|
|
20
27
|
factorizePQ(pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
21
|
-
|
|
28
|
+
randomBytes(size: number): Uint8Array;
|
|
22
29
|
}
|
|
23
30
|
export type CryptoProviderFactory = () => ICryptoProvider;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseCryptoProvider = void 0;
|
|
4
|
-
const common_js_1 = require("./common.js");
|
|
5
4
|
const factorization_js_1 = require("./factorization.js");
|
|
6
5
|
class BaseCryptoProvider {
|
|
7
|
-
createAesIge(key, iv) {
|
|
8
|
-
const ecb = this.createAesEcb(key);
|
|
9
|
-
if ('then' in ecb) {
|
|
10
|
-
return ecb.then((ecb) => new common_js_1.AesModeOfOperationIge(key, iv, ecb));
|
|
11
|
-
}
|
|
12
|
-
return new common_js_1.AesModeOfOperationIge(key, iv, ecb);
|
|
13
|
-
}
|
|
14
6
|
factorizePQ(pq) {
|
|
15
|
-
return (0, factorization_js_1.factorizePQSync)(pq);
|
|
7
|
+
return (0, factorization_js_1.factorizePQSync)(this, pq);
|
|
8
|
+
}
|
|
9
|
+
randomBytes(size) {
|
|
10
|
+
const buf = new Uint8Array(size);
|
|
11
|
+
this.randomFill(buf);
|
|
12
|
+
return buf;
|
|
16
13
|
}
|
|
17
14
|
}
|
|
18
15
|
exports.BaseCryptoProvider = BaseCryptoProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/utils/crypto/abstract.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/utils/crypto/abstract.ts"],"names":[],"mappings":";;;AACA,yDAAoD;AA0CpD,MAAsB,kBAAkB;IAGpC,WAAW,CAAC,EAAc;QACtB,OAAO,IAAA,kCAAe,EAAC,IAAkC,EAAE,EAAE,CAAC,CAAA;IAClE,CAAC;IAED,WAAW,CAAC,IAAY;QACpB,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QAEpB,OAAO,GAAG,CAAA;IACd,CAAC;CACJ;AAbD,gDAaC","sourcesContent":["import { MaybeAsync } from '../../types/index.js'\nimport { factorizePQSync } from './factorization.js'\n\nexport interface IEncryptionScheme {\n encrypt(data: Uint8Array): Uint8Array\n decrypt(data: Uint8Array): Uint8Array\n}\n\nexport interface IAesCtr {\n process(data: Uint8Array): Uint8Array\n close?(): void\n}\n\nexport interface ICryptoProvider {\n initialize?(): MaybeAsync<void>\n\n sha1(data: Uint8Array): Uint8Array\n\n sha256(data: Uint8Array): Uint8Array\n\n pbkdf2(\n password: Uint8Array,\n salt: Uint8Array,\n iterations: number,\n keylen?: number, // = 64\n algo?: string, // sha1 or sha512 (default sha512)\n ): MaybeAsync<Uint8Array>\n\n hmacSha256(data: Uint8Array, key: Uint8Array): MaybeAsync<Uint8Array>\n\n createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean): IAesCtr\n\n createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme\n\n factorizePQ(pq: Uint8Array): MaybeAsync<[Uint8Array, Uint8Array]>\n\n gzip(data: Uint8Array, maxSize: number): Uint8Array | null\n gunzip(data: Uint8Array): Uint8Array\n\n randomFill(buf: Uint8Array): void\n randomBytes(size: number): Uint8Array\n}\n\nexport abstract class BaseCryptoProvider {\n abstract randomFill(buf: Uint8Array): void\n\n factorizePQ(pq: Uint8Array) {\n return factorizePQSync(this as unknown as ICryptoProvider, pq)\n }\n\n randomBytes(size: number) {\n const buf = new Uint8Array(size)\n this.randomFill(buf)\n\n return buf\n }\n}\n\nexport type CryptoProviderFactory = () => ICryptoProvider\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { ICryptoProvider } from './abstract.js';
|
|
1
2
|
/**
|
|
2
3
|
* Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm
|
|
3
4
|
* @param pq
|
|
4
5
|
*/
|
|
5
|
-
export declare function factorizePQSync(pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
6
|
+
export declare function factorizePQSync(crypto: ICryptoProvider, pq: Uint8Array): [Uint8Array, Uint8Array];
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.factorizePQSync = void 0;
|
|
7
|
-
const big_integer_1 = __importDefault(require("big-integer"));
|
|
8
4
|
const bigint_utils_js_1 = require("../bigint-utils.js");
|
|
9
5
|
/**
|
|
10
6
|
* Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm
|
|
11
7
|
* @param pq
|
|
12
8
|
*/
|
|
13
|
-
function factorizePQSync(pq) {
|
|
9
|
+
function factorizePQSync(crypto, pq) {
|
|
14
10
|
const pq_ = (0, bigint_utils_js_1.bufferToBigInt)(pq);
|
|
15
|
-
const n = PollardRhoBrent(pq_);
|
|
16
|
-
const m = pq_
|
|
11
|
+
const n = PollardRhoBrent(crypto, pq_);
|
|
12
|
+
const m = pq_ / n;
|
|
17
13
|
let p;
|
|
18
14
|
let q;
|
|
19
|
-
if (n
|
|
15
|
+
if (n < m) {
|
|
20
16
|
p = n;
|
|
21
17
|
q = m;
|
|
22
18
|
}
|
|
@@ -27,42 +23,38 @@ function factorizePQSync(pq) {
|
|
|
27
23
|
return [(0, bigint_utils_js_1.bigIntToBuffer)(p), (0, bigint_utils_js_1.bigIntToBuffer)(q)];
|
|
28
24
|
}
|
|
29
25
|
exports.factorizePQSync = factorizePQSync;
|
|
30
|
-
function PollardRhoBrent(n) {
|
|
31
|
-
if (n
|
|
32
|
-
return
|
|
33
|
-
let y = (0, bigint_utils_js_1.randomBigIntInRange)(n
|
|
34
|
-
const c = (0, bigint_utils_js_1.randomBigIntInRange)(n
|
|
35
|
-
const m = (0, bigint_utils_js_1.randomBigIntInRange)(n
|
|
36
|
-
let g =
|
|
37
|
-
let r =
|
|
38
|
-
let q =
|
|
26
|
+
function PollardRhoBrent(crypto, n) {
|
|
27
|
+
if (n % 2n === 0n)
|
|
28
|
+
return 2n;
|
|
29
|
+
let y = (0, bigint_utils_js_1.randomBigIntInRange)(crypto, n - 1n);
|
|
30
|
+
const c = (0, bigint_utils_js_1.randomBigIntInRange)(crypto, n - 1n);
|
|
31
|
+
const m = (0, bigint_utils_js_1.randomBigIntInRange)(crypto, n - 1n);
|
|
32
|
+
let g = 1n;
|
|
33
|
+
let r = 1n;
|
|
34
|
+
let q = 1n;
|
|
39
35
|
let ys;
|
|
40
36
|
let x;
|
|
41
|
-
while (g
|
|
37
|
+
while (g === 1n) {
|
|
42
38
|
x = y;
|
|
43
|
-
for (let i = 0; r
|
|
44
|
-
y =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
while (k.lt(r) && g.eq(1)) {
|
|
39
|
+
for (let i = 0; r >= i; i++)
|
|
40
|
+
y = (((y * y) % n) + c) % n;
|
|
41
|
+
let k = 0n;
|
|
42
|
+
while (k < r && g === 1n) {
|
|
48
43
|
ys = y;
|
|
49
|
-
for (let i =
|
|
50
|
-
y =
|
|
51
|
-
q = q.
|
|
52
|
-
// y = (y * y % n + c) % n
|
|
53
|
-
// q = q * abs(x - y) % n
|
|
44
|
+
for (let i = 0n; i < (0, bigint_utils_js_1.bigIntMin)(m, r - k); i++) {
|
|
45
|
+
y = (((y * y) % n) + c) % n;
|
|
46
|
+
q = (q * (0, bigint_utils_js_1.bigIntAbs)(x - y)) % n;
|
|
54
47
|
}
|
|
55
|
-
g =
|
|
56
|
-
k = k
|
|
48
|
+
g = (0, bigint_utils_js_1.bigIntGcd)(q, n);
|
|
49
|
+
k = k + m;
|
|
57
50
|
}
|
|
58
|
-
r
|
|
51
|
+
r <<= 1n;
|
|
59
52
|
}
|
|
60
|
-
if (g
|
|
53
|
+
if (g === n) {
|
|
61
54
|
do {
|
|
62
|
-
ys =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} while (g.leq(big_integer_1.default.one));
|
|
55
|
+
ys = (((ys * ys) % n) + c) % n;
|
|
56
|
+
g = (0, bigint_utils_js_1.bigIntGcd)(x - ys, n);
|
|
57
|
+
} while (g <= 1n);
|
|
66
58
|
}
|
|
67
59
|
return g;
|
|
68
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factorization.js","sourceRoot":"","sources":["../../../../src/utils/crypto/factorization.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"factorization.js","sourceRoot":"","sources":["../../../../src/utils/crypto/factorization.ts"],"names":[],"mappings":";;;AAAA,wDAO2B;AAG3B;;;GAGG;AACH,SAAgB,eAAe,CAAC,MAAuB,EAAE,EAAc;IACnE,MAAM,GAAG,GAAG,IAAA,gCAAc,EAAC,EAAE,CAAC,CAAA;IAE9B,MAAM,CAAC,GAAG,eAAe,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACtC,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC,CAAA;IAEjB,IAAI,CAAC,CAAA;IACL,IAAI,CAAC,CAAA;IAEL,IAAI,CAAC,GAAG,CAAC,EAAE;QACP,CAAC,GAAG,CAAC,CAAA;QACL,CAAC,GAAG,CAAC,CAAA;KACR;SAAM;QACH,CAAC,GAAG,CAAC,CAAA;QACL,CAAC,GAAG,CAAC,CAAA;KACR;IAED,OAAO,CAAC,IAAA,gCAAc,EAAC,CAAC,CAAC,EAAE,IAAA,gCAAc,EAAC,CAAC,CAAC,CAAC,CAAA;AACjD,CAAC;AAlBD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GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;aACjC;YAED,CAAC,GAAG,IAAA,2BAAS,EAAC,CAAC,EAAE,CAAC,CAAC,CAAA;YACnB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;SACZ;QAED,CAAC,KAAK,EAAE,CAAA;KACX;IAED,IAAI,CAAC,KAAK,CAAC,EAAE;QACT,GAAG;YACC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAG,GAAG,EAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;YAEhC,CAAC,GAAG,IAAA,2BAAS,EAAC,CAAE,GAAG,EAAG,EAAE,CAAC,CAAC,CAAA;SAC7B,QAAQ,CAAC,IAAI,EAAE,EAAC;KACpB;IAED,OAAO,CAAC,CAAA;AACZ,CAAC","sourcesContent":["import {\n bigIntAbs,\n bigIntGcd,\n bigIntMin,\n bigIntToBuffer,\n bufferToBigInt,\n randomBigIntInRange,\n} from '../bigint-utils.js'\nimport { ICryptoProvider } from './abstract.js'\n\n/**\n * Factorize `p*q` to `p` and `q` synchronously using Brent-Pollard rho algorithm\n * @param pq\n */\nexport function factorizePQSync(crypto: ICryptoProvider, pq: Uint8Array): [Uint8Array, Uint8Array] {\n const pq_ = bufferToBigInt(pq)\n\n const n = PollardRhoBrent(crypto, pq_)\n const m = pq_ / n\n\n let p\n let q\n\n if (n < m) {\n p = n\n q = m\n } else {\n p = m\n q = n\n }\n\n return [bigIntToBuffer(p), bigIntToBuffer(q)]\n}\n\nfunction PollardRhoBrent(crypto: ICryptoProvider, n: bigint): bigint {\n if (n % 2n === 0n) return 2n\n\n let y = randomBigIntInRange(crypto, n - 1n)\n const c = randomBigIntInRange(crypto, n - 1n)\n const m = randomBigIntInRange(crypto, n - 1n)\n let g = 1n\n let r = 1n\n let q = 1n\n\n let ys: bigint\n let x: bigint\n\n while (g === 1n) {\n x = y\n for (let i = 0; r >= i; i++) y = (((y * y) % n) + c) % n\n\n let k = 0n\n\n while (k < r && g === 1n) {\n ys = y\n\n for (let i = 0n; i < bigIntMin(m, r - k); i++) {\n y = (((y * y) % n) + c) % n\n q = (q * bigIntAbs(x - y)) % n\n }\n\n g = bigIntGcd(q, n)\n k = k + m\n }\n\n r <<= 1n\n }\n\n if (g === n) {\n do {\n ys = (((ys! * ys!) % n) + c) % n\n\n g = bigIntGcd(x! - ys!, n)\n } while (g <= 1n)\n }\n\n return g\n}\n"]}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export * from './abstract.js';
|
|
2
|
+
export * from './factorization.js';
|
|
2
3
|
export * from './keys.js';
|
|
4
|
+
export * from './miller-rabin.js';
|
|
5
|
+
export * from './mtproto.js';
|
|
3
6
|
export * from './password.js';
|
|
7
|
+
export * from './utils.js';
|
|
4
8
|
import { CryptoProviderFactory } from './abstract.js';
|
|
5
9
|
export declare const defaultCryptoProviderFactory: CryptoProviderFactory;
|
|
@@ -16,8 +16,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.defaultCryptoProviderFactory = void 0;
|
|
18
18
|
__exportStar(require("./abstract.js"), exports);
|
|
19
|
+
__exportStar(require("./factorization.js"), exports);
|
|
19
20
|
__exportStar(require("./keys.js"), exports);
|
|
21
|
+
__exportStar(require("./miller-rabin.js"), exports);
|
|
22
|
+
__exportStar(require("./mtproto.js"), exports);
|
|
20
23
|
__exportStar(require("./password.js"), exports);
|
|
24
|
+
__exportStar(require("./utils.js"), exports);
|
|
21
25
|
const crypto_js_1 = require("../platform/crypto.js");
|
|
22
26
|
exports.defaultCryptoProviderFactory = crypto_js_1._defaultCryptoProviderFactory;
|
|
23
27
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/crypto/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gDAA6B;AAC7B,4CAAyB;AACzB,gDAA6B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/crypto/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gDAA6B;AAC7B,qDAAkC;AAClC,4CAAyB;AACzB,oDAAiC;AACjC,+CAA4B;AAC5B,gDAA6B;AAC7B,6CAA0B;AAE1B,qDAAqE;AAGxD,QAAA,4BAA4B,GAA0B,yCAA6B,CAAA","sourcesContent":["export * from './abstract.js'\nexport * from './factorization.js'\nexport * from './keys.js'\nexport * from './miller-rabin.js'\nexport * from './mtproto.js'\nexport * from './password.js'\nexport * from './utils.js'\n\nimport { _defaultCryptoProviderFactory } from '../platform/crypto.js'\nimport { CryptoProviderFactory } from './abstract.js'\n\nexport const defaultCryptoProviderFactory: CryptoProviderFactory = _defaultCryptoProviderFactory\n"]}
|
|
@@ -9,7 +9,7 @@ import { ICryptoProvider } from './abstract.js';
|
|
|
9
9
|
* @param key PEM-encoded RSA public key
|
|
10
10
|
* @param old Whether this is an "old" key
|
|
11
11
|
*/
|
|
12
|
-
export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old?: boolean):
|
|
12
|
+
export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old?: boolean): TlPublicKey;
|
|
13
13
|
/**
|
|
14
14
|
* Add public key to the global index.
|
|
15
15
|
*
|
|
@@ -17,7 +17,7 @@ export declare function parsePublicKey(crypto: ICryptoProvider, key: string, old
|
|
|
17
17
|
* @param key PEM-encoded RSA public key
|
|
18
18
|
* @param old Whether this is an "old" key
|
|
19
19
|
*/
|
|
20
|
-
export declare function addPublicKey(crypto: ICryptoProvider, key: string, old?: boolean):
|
|
20
|
+
export declare function addPublicKey(crypto: ICryptoProvider, key: string, old?: boolean): void;
|
|
21
21
|
/**
|
|
22
22
|
* Get public key by its fingerprint.
|
|
23
23
|
*
|