@protontech/openpgp 4.10.6 → 5.4.0
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/README.md +311 -239
- package/dist/lightweight/bn.interface.min.mjs +3 -0
- package/dist/lightweight/bn.interface.min.mjs.map +1 -0
- package/dist/lightweight/bn.interface.mjs +340 -0
- package/dist/lightweight/bn.min.mjs +3 -0
- package/dist/lightweight/bn.min.mjs.map +1 -0
- package/dist/lightweight/bn.mjs +3434 -0
- package/dist/lightweight/elliptic.min.mjs +3 -0
- package/dist/lightweight/elliptic.min.mjs.map +1 -0
- package/dist/lightweight/elliptic.mjs +4313 -0
- package/dist/lightweight/openpgp.min.mjs +3 -0
- package/dist/lightweight/openpgp.min.mjs.map +1 -0
- package/dist/lightweight/openpgp.mjs +31379 -0
- package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
- package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
- package/dist/lightweight/ponyfill.es6.mjs +3831 -0
- package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
- package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
- package/dist/lightweight/web-streams-adapter.mjs +561 -0
- package/dist/node/openpgp.js +43947 -0
- package/dist/node/openpgp.min.js +17 -0
- package/dist/node/openpgp.min.js.map +1 -0
- package/dist/node/openpgp.min.mjs +17 -0
- package/dist/node/openpgp.min.mjs.map +1 -0
- package/dist/node/openpgp.mjs +43884 -0
- package/dist/openpgp.js +41082 -41563
- package/dist/openpgp.min.js +17 -2
- package/dist/openpgp.min.js.map +1 -0
- package/dist/openpgp.min.mjs +17 -0
- package/dist/openpgp.min.mjs.map +1 -0
- package/dist/openpgp.mjs +43872 -0
- package/lightweight/package.json +5 -0
- package/openpgp.d.ts +890 -0
- package/package.json +63 -57
- package/dist/compat/openpgp.js +0 -61067
- package/dist/compat/openpgp.min.js +0 -2
- package/dist/compat/openpgp.worker.js +0 -173
- package/dist/compat/openpgp.worker.min.js +0 -2
- package/dist/lightweight/elliptic.min.js +0 -5
- package/dist/lightweight/openpgp.js +0 -40024
- package/dist/lightweight/openpgp.min.js +0 -2
- package/dist/lightweight/openpgp.worker.js +0 -173
- package/dist/lightweight/openpgp.worker.min.js +0 -2
- package/dist/openpgp.worker.js +0 -173
- package/dist/openpgp.worker.min.js +0 -2
- package/src/cleartext.js +0 -220
- package/src/config/config.js +0 -224
- package/src/config/index.js +0 -7
- package/src/config/localStorage.js +0 -35
- package/src/crypto/aes_kw.js +0 -153
- package/src/crypto/cfb.js +0 -169
- package/src/crypto/cipher/aes.js +0 -27
- package/src/crypto/cipher/blowfish.js +0 -398
- package/src/crypto/cipher/cast5.js +0 -610
- package/src/crypto/cipher/des.js +0 -476
- package/src/crypto/cipher/index.js +0 -91
- package/src/crypto/cipher/twofish.js +0 -346
- package/src/crypto/cmac.js +0 -98
- package/src/crypto/crypto.js +0 -394
- package/src/crypto/eax.js +0 -172
- package/src/crypto/gcm.js +0 -141
- package/src/crypto/hash/index.js +0 -163
- package/src/crypto/hash/md5.js +0 -205
- package/src/crypto/index.js +0 -57
- package/src/crypto/ocb.js +0 -274
- package/src/crypto/pkcs1.js +0 -170
- package/src/crypto/pkcs5.js +0 -55
- package/src/crypto/public_key/dsa.js +0 -188
- package/src/crypto/public_key/elgamal.js +0 -137
- package/src/crypto/public_key/elliptic/curves.js +0 -385
- package/src/crypto/public_key/elliptic/ecdh.js +0 -414
- package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
- package/src/crypto/public_key/elliptic/eddsa.js +0 -119
- package/src/crypto/public_key/elliptic/index.js +0 -34
- package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
- package/src/crypto/public_key/index.js +0 -28
- package/src/crypto/public_key/prime.js +0 -275
- package/src/crypto/public_key/rsa.js +0 -597
- package/src/crypto/random.js +0 -145
- package/src/crypto/signature.js +0 -137
- package/src/encoding/armor.js +0 -433
- package/src/encoding/base64.js +0 -96
- package/src/enums.js +0 -493
- package/src/hkp.js +0 -89
- package/src/index.js +0 -161
- package/src/key/factory.js +0 -326
- package/src/key/helper.js +0 -363
- package/src/key/index.js +0 -32
- package/src/key/key.js +0 -890
- package/src/key/subkey.js +0 -187
- package/src/key/user.js +0 -230
- package/src/keyring/index.js +0 -12
- package/src/keyring/keyring.js +0 -229
- package/src/keyring/localstore.js +0 -119
- package/src/lightweight_helper.js +0 -26
- package/src/message.js +0 -825
- package/src/openpgp.js +0 -717
- package/src/packet/all_packets.js +0 -116
- package/src/packet/clone.js +0 -189
- package/src/packet/compressed.js +0 -194
- package/src/packet/index.js +0 -20
- package/src/packet/literal.js +0 -168
- package/src/packet/marker.js +0 -62
- package/src/packet/one_pass_signature.js +0 -156
- package/src/packet/packet.js +0 -300
- package/src/packet/packetlist.js +0 -232
- package/src/packet/public_key.js +0 -280
- package/src/packet/public_key_encrypted_session_key.js +0 -156
- package/src/packet/public_subkey.js +0 -44
- package/src/packet/secret_key.js +0 -448
- package/src/packet/secret_subkey.js +0 -41
- package/src/packet/signature.js +0 -782
- package/src/packet/sym_encrypted_aead_protected.js +0 -189
- package/src/packet/sym_encrypted_integrity_protected.js +0 -139
- package/src/packet/sym_encrypted_session_key.js +0 -204
- package/src/packet/symmetrically_encrypted.js +0 -118
- package/src/packet/trust.js +0 -35
- package/src/packet/user_attribute.js +0 -94
- package/src/packet/userid.js +0 -87
- package/src/polyfills.js +0 -64
- package/src/signature.js +0 -73
- package/src/type/ecdh_symkey.js +0 -69
- package/src/type/kdf_params.js +0 -114
- package/src/type/keyid.js +0 -110
- package/src/type/mpi.js +0 -138
- package/src/type/oid.js +0 -110
- package/src/type/s2k.js +0 -203
- package/src/util.js +0 -836
- package/src/wkd.js +0 -88
- package/src/worker/async_proxy.js +0 -190
- package/src/worker/worker.js +0 -167
- package/test/crypto/aes_kw.js +0 -57
- package/test/crypto/cipher/aes.js +0 -86
- package/test/crypto/cipher/blowfish.js +0 -58
- package/test/crypto/cipher/cast5.js +0 -25
- package/test/crypto/cipher/des.js +0 -143
- package/test/crypto/cipher/index.js +0 -7
- package/test/crypto/cipher/twofish.js +0 -71
- package/test/crypto/crypto.js +0 -383
- package/test/crypto/eax.js +0 -150
- package/test/crypto/ecdh.js +0 -359
- package/test/crypto/elliptic.js +0 -251
- package/test/crypto/elliptic_data.js +0 -102
- package/test/crypto/hash/index.js +0 -5
- package/test/crypto/hash/md5.js +0 -16
- package/test/crypto/hash/ripemd.js +0 -14
- package/test/crypto/hash/sha.js +0 -20
- package/test/crypto/index.js +0 -14
- package/test/crypto/ocb.js +0 -183
- package/test/crypto/pkcs5.js +0 -39
- package/test/crypto/random.js +0 -79
- package/test/crypto/rsa.js +0 -180
- package/test/crypto/validate.js +0 -387
- package/test/general/armor.js +0 -408
- package/test/general/brainpool.js +0 -360
- package/test/general/decompression.js +0 -60
- package/test/general/ecc_nist.js +0 -115
- package/test/general/ecc_secp256k1.js +0 -242
- package/test/general/forwarding.js +0 -43
- package/test/general/hkp.js +0 -165
- package/test/general/index.js +0 -20
- package/test/general/key.js +0 -3402
- package/test/general/keyring.js +0 -336
- package/test/general/oid.js +0 -39
- package/test/general/openpgp.js +0 -2542
- package/test/general/packet.js +0 -937
- package/test/general/signature.js +0 -1665
- package/test/general/streaming.js +0 -944
- package/test/general/testInputs.js +0 -18
- package/test/general/util.js +0 -183
- package/test/general/wkd.js +0 -48
- package/test/general/x25519.js +0 -556
- package/test/unittests.js +0 -64
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
// OpenPGP.js - An OpenPGP implementation in javascript
|
|
2
|
-
// Copyright (C) 2018 Proton Technologies AG
|
|
3
|
-
//
|
|
4
|
-
// This library is free software; you can redistribute it and/or
|
|
5
|
-
// modify it under the terms of the GNU Lesser General Public
|
|
6
|
-
// License as published by the Free Software Foundation; either
|
|
7
|
-
// version 3.0 of the License, or (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// This library is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
-
// Lesser General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU Lesser General Public
|
|
15
|
-
// License along with this library; if not, write to the Free Software
|
|
16
|
-
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @fileoverview Algorithms for probabilistic random prime generation
|
|
20
|
-
* @requires bn.js
|
|
21
|
-
* @requires crypto/random
|
|
22
|
-
* @module crypto/public_key/prime
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import BN from 'bn.js';
|
|
26
|
-
import random from '../random';
|
|
27
|
-
|
|
28
|
-
export default {
|
|
29
|
-
randomProbablePrime, isProbablePrime, fermat, millerRabin, divisionTest
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Probabilistic random number generator
|
|
34
|
-
* @param {Integer} bits Bit length of the prime
|
|
35
|
-
* @param {BN} e Optional RSA exponent to check against the prime
|
|
36
|
-
* @param {Integer} k Optional number of iterations of Miller-Rabin test
|
|
37
|
-
* @returns BN
|
|
38
|
-
* @async
|
|
39
|
-
*/
|
|
40
|
-
async function randomProbablePrime(bits, e, k) {
|
|
41
|
-
const min = new BN(1).shln(bits - 1);
|
|
42
|
-
const thirty = new BN(30);
|
|
43
|
-
/*
|
|
44
|
-
* We can avoid any multiples of 3 and 5 by looking at n mod 30
|
|
45
|
-
* n mod 30 = 0 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
|
|
46
|
-
* the next possible prime is mod 30:
|
|
47
|
-
* 1 7 7 7 7 7 7 11 11 11 11 13 13 17 17 17 17 19 19 23 23 23 23 29 29 29 29 29 29 1
|
|
48
|
-
*/
|
|
49
|
-
const adds = [1, 6, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 2];
|
|
50
|
-
|
|
51
|
-
let n = await random.getRandomBN(min, min.shln(1));
|
|
52
|
-
let i = n.mod(thirty).toNumber();
|
|
53
|
-
|
|
54
|
-
do {
|
|
55
|
-
n.iaddn(adds[i]);
|
|
56
|
-
i = (i + adds[i]) % adds.length;
|
|
57
|
-
// If reached the maximum, go back to the minimum.
|
|
58
|
-
if (n.bitLength() > bits) {
|
|
59
|
-
n = n.mod(min.shln(1)).iadd(min);
|
|
60
|
-
i = n.mod(thirty).toNumber();
|
|
61
|
-
}
|
|
62
|
-
} while (!await isProbablePrime(n, e, k));
|
|
63
|
-
return n;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Probabilistic primality testing
|
|
68
|
-
* @param {BN} n Number to test
|
|
69
|
-
* @param {BN} e Optional RSA exponent to check against the prime
|
|
70
|
-
* @param {Integer} k Optional number of iterations of Miller-Rabin test
|
|
71
|
-
* @returns {boolean}
|
|
72
|
-
* @async
|
|
73
|
-
*/
|
|
74
|
-
async function isProbablePrime(n, e, k) {
|
|
75
|
-
if (e && !n.subn(1).gcd(e).eqn(1)) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
if (!divisionTest(n)) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
if (!fermat(n)) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
if (!await millerRabin(n, k)) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
// TODO implement the Lucas test
|
|
88
|
-
// See Section C.3.3 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Tests whether n is probably prime or not using Fermat's test with b = 2.
|
|
94
|
-
* Fails if b^(n-1) mod n === 1.
|
|
95
|
-
* @param {BN} n Number to test
|
|
96
|
-
* @param {Integer} b Optional Fermat test base
|
|
97
|
-
* @returns {boolean}
|
|
98
|
-
*/
|
|
99
|
-
function fermat(n, b) {
|
|
100
|
-
b = b || new BN(2);
|
|
101
|
-
return b.toRed(BN.mont(n)).redPow(n.subn(1)).fromRed().cmpn(1) === 0;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function divisionTest(n) {
|
|
105
|
-
return small_primes.every(m => {
|
|
106
|
-
return n.modn(m) !== 0;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// https://github.com/gpg/libgcrypt/blob/master/cipher/primegen.c
|
|
111
|
-
const small_primes = [
|
|
112
|
-
7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
|
|
113
|
-
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
|
|
114
|
-
103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
|
|
115
|
-
157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
|
|
116
|
-
211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
|
|
117
|
-
269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
|
|
118
|
-
331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
|
|
119
|
-
389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
|
|
120
|
-
449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
|
|
121
|
-
509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
|
|
122
|
-
587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
|
|
123
|
-
643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
|
|
124
|
-
709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
|
|
125
|
-
773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
|
|
126
|
-
853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
|
|
127
|
-
919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
|
|
128
|
-
991, 997, 1009, 1013, 1019, 1021, 1031, 1033,
|
|
129
|
-
1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
|
|
130
|
-
1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
|
|
131
|
-
1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
|
|
132
|
-
1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277,
|
|
133
|
-
1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307,
|
|
134
|
-
1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
|
|
135
|
-
1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451,
|
|
136
|
-
1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
|
|
137
|
-
1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
|
|
138
|
-
1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609,
|
|
139
|
-
1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
|
|
140
|
-
1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733,
|
|
141
|
-
1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
|
|
142
|
-
1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
|
|
143
|
-
1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
|
|
144
|
-
1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
|
|
145
|
-
1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053,
|
|
146
|
-
2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111,
|
|
147
|
-
2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161,
|
|
148
|
-
2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
|
|
149
|
-
2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
|
|
150
|
-
2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
|
|
151
|
-
2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
|
|
152
|
-
2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473,
|
|
153
|
-
2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
|
|
154
|
-
2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633,
|
|
155
|
-
2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
|
|
156
|
-
2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729,
|
|
157
|
-
2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
|
|
158
|
-
2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
|
|
159
|
-
2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917,
|
|
160
|
-
2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999,
|
|
161
|
-
3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061,
|
|
162
|
-
3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
|
|
163
|
-
3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209,
|
|
164
|
-
3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
|
|
165
|
-
3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
|
|
166
|
-
3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391,
|
|
167
|
-
3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467,
|
|
168
|
-
3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533,
|
|
169
|
-
3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
|
|
170
|
-
3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643,
|
|
171
|
-
3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
|
|
172
|
-
3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
|
|
173
|
-
3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851,
|
|
174
|
-
3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917,
|
|
175
|
-
3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
|
|
176
|
-
4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
|
|
177
|
-
4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111,
|
|
178
|
-
4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
|
|
179
|
-
4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
|
|
180
|
-
4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297,
|
|
181
|
-
4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391,
|
|
182
|
-
4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457,
|
|
183
|
-
4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
|
|
184
|
-
4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597,
|
|
185
|
-
4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
|
|
186
|
-
4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
|
|
187
|
-
4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799,
|
|
188
|
-
4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889,
|
|
189
|
-
4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951,
|
|
190
|
-
4957, 4967, 4969, 4973, 4987, 4993, 4999
|
|
191
|
-
];
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// Miller-Rabin - Miller Rabin algorithm for primality test
|
|
195
|
-
// Copyright Fedor Indutny, 2014.
|
|
196
|
-
//
|
|
197
|
-
// This software is licensed under the MIT License.
|
|
198
|
-
//
|
|
199
|
-
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
200
|
-
// copy of this software and associated documentation files (the
|
|
201
|
-
// "Software"), to deal in the Software without restriction, including
|
|
202
|
-
// without limitation the rights to use, copy, modify, merge, publish,
|
|
203
|
-
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
204
|
-
// persons to whom the Software is furnished to do so, subject to the
|
|
205
|
-
// following conditions:
|
|
206
|
-
//
|
|
207
|
-
// The above copyright notice and this permission notice shall be included
|
|
208
|
-
// in all copies or substantial portions of the Software.
|
|
209
|
-
//
|
|
210
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
211
|
-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
212
|
-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
213
|
-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
214
|
-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
215
|
-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
216
|
-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
217
|
-
|
|
218
|
-
// Adapted on Jan 2018 from version 4.0.1 at https://github.com/indutny/miller-rabin
|
|
219
|
-
|
|
220
|
-
// Sample syntax for Fixed-Base Miller-Rabin:
|
|
221
|
-
// millerRabin(n, k, () => new BN(small_primes[Math.random() * small_primes.length | 0]))
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Tests whether n is probably prime or not using the Miller-Rabin test.
|
|
225
|
-
* See HAC Remark 4.28.
|
|
226
|
-
* @param {BN} n Number to test
|
|
227
|
-
* @param {Integer} k Optional number of iterations of Miller-Rabin test
|
|
228
|
-
* @param {Function} rand Optional function to generate potential witnesses
|
|
229
|
-
* @returns {boolean}
|
|
230
|
-
* @async
|
|
231
|
-
*/
|
|
232
|
-
async function millerRabin(n, k, rand) {
|
|
233
|
-
const len = n.bitLength();
|
|
234
|
-
const red = BN.mont(n);
|
|
235
|
-
const rone = new BN(1).toRed(red);
|
|
236
|
-
|
|
237
|
-
if (!k) {
|
|
238
|
-
k = Math.max(1, (len / 48) | 0);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const n1 = n.subn(1);
|
|
242
|
-
const rn1 = n1.toRed(red);
|
|
243
|
-
|
|
244
|
-
// Find d and s, (n - 1) = (2 ^ s) * d;
|
|
245
|
-
let s = 0;
|
|
246
|
-
while (!n1.testn(s)) { s++; }
|
|
247
|
-
const d = n.shrn(s);
|
|
248
|
-
|
|
249
|
-
for (; k > 0; k--) {
|
|
250
|
-
const a = rand ? rand() : await random.getRandomBN(new BN(2), n1);
|
|
251
|
-
|
|
252
|
-
let x = a.toRed(red).redPow(d);
|
|
253
|
-
if (x.eq(rone) || x.eq(rn1)) {
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
let i;
|
|
258
|
-
for (i = 1; i < s; i++) {
|
|
259
|
-
x = x.redSqr();
|
|
260
|
-
|
|
261
|
-
if (x.eq(rone)) {
|
|
262
|
-
return false;
|
|
263
|
-
}
|
|
264
|
-
if (x.eq(rn1)) {
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (i === s) {
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return true;
|
|
275
|
-
}
|