Riskless/static/js/modules/crypto/random_primes.js

1345 lines
15 KiB
JavaScript
Raw Normal View History

import { mod_exp } from "./math.js";
2023-04-30 17:42:52 +00:00
export const KEY_SIZE = 512;
2023-04-13 12:32:29 +00:00
export function cryptoRandom(bits) {
if (bits === undefined) {
2023-04-13 12:32:29 +00:00
bits = KEY_SIZE;
}
let length = bits / 64;
const byteArray = new BigUint64Array(length);
2023-02-08 15:52:02 +00:00
window.crypto.getRandomValues(byteArray);
let intRepr = 0n;
for (let int of byteArray) {
intRepr <<= 64n;
intRepr += int;
}
return intRepr;
}
2023-04-17 15:32:35 +00:00
window.cryptoRandom = cryptoRandom;
2023-02-08 15:52:02 +00:00
/**
2023-04-14 15:04:24 +00:00
* Generate random integer of length N bits.
2023-02-08 15:52:02 +00:00
*
2023-04-14 15:04:24 +00:00
* We generate between 2^(N - 1) and 2^N - 1 by adding differences.
2023-02-08 15:52:02 +00:00
*/
function generate_bigint() {
let intRepr = cryptoRandom();
2023-02-08 15:52:02 +00:00
// Drop the MSB to force into range from above
intRepr >>= 1n;
// Add 2^2047 to force into range from below
2023-04-13 12:32:29 +00:00
intRepr += 2n ** BigInt(KEY_SIZE - 1);
2023-02-08 15:52:02 +00:00
return intRepr;
}
function small_prime_test(n) {
// Assumes that n > 10000
for (let p of SMALL_PRIMES) {
if (n % p === 0n) {
return false;
}
}
return true;
}
function miller_rabin(n, k) {
// Eradicate the trivial case
if (n % 2n === 0n) {
return false;
}
// Extract factors of 2
let s = 0n;
let d = n - 1n;
while (d % 2n === 0n) {
d >>= 1n;
s += 1n;
}
for (; k > 0; k--) {
let a = cryptoRandom();
let x = mod_exp(a, d, n);
2023-02-08 15:52:02 +00:00
if (x === 1n || x === n - 1n) {
continue;
}
let composite = true;
for (let s_ = 0; s_ < s; s_++) {
x = x ** 2n % n;
if (x === n - 1n) {
composite = false;
break;
}
}
if (composite) {
return false;
}
}
return true;
}
export function generate_prime() {
2023-02-08 15:52:02 +00:00
while (true) {
2023-04-30 17:42:52 +00:00
let n = generate_bigint() | 0b11n;
2023-02-08 15:52:02 +00:00
if (small_prime_test(n) && miller_rabin(n, 40)) {
return n;
}
}
}
2023-04-20 09:13:03 +00:00
export function generate_safe_prime() {
while (true) {
let n = generate_prime();
2023-04-24 13:20:44 +00:00
if (small_prime_test((n - 1n) / 2n)) {
// Remove this conditional if you want it to run fast! It (probably) won't break it
// if (miller_rabin((n - 1n) / 2n, 40)) {
2023-04-20 09:13:03 +00:00
return n;
2023-04-24 13:20:44 +00:00
// }
2023-04-20 09:13:03 +00:00
}
}
}
2023-04-30 17:42:52 +00:00
window.generate_safe_prime = generate_safe_prime;
2023-02-08 15:52:02 +00:00
const SMALL_PRIMES = [
2n,
3n,
5n,
7n,
11n,
13n,
17n,
19n,
23n,
29n,
31n,
37n,
41n,
43n,
47n,
53n,
59n,
61n,
67n,
71n,
73n,
79n,
83n,
89n,
97n,
101n,
103n,
107n,
109n,
113n,
127n,
131n,
137n,
139n,
149n,
151n,
157n,
163n,
167n,
173n,
179n,
181n,
191n,
193n,
197n,
199n,
211n,
223n,
227n,
229n,
233n,
239n,
241n,
251n,
257n,
263n,
269n,
271n,
277n,
281n,
283n,
293n,
307n,
311n,
313n,
317n,
331n,
337n,
347n,
349n,
353n,
359n,
367n,
373n,
379n,
383n,
389n,
397n,
401n,
409n,
419n,
421n,
431n,
433n,
439n,
443n,
449n,
457n,
461n,
463n,
467n,
479n,
487n,
491n,
499n,
503n,
509n,
521n,
523n,
541n,
547n,
557n,
563n,
569n,
571n,
577n,
587n,
593n,
599n,
601n,
607n,
613n,
617n,
619n,
631n,
641n,
643n,
647n,
653n,
659n,
661n,
673n,
677n,
683n,
691n,
701n,
709n,
719n,
727n,
733n,
739n,
743n,
751n,
757n,
761n,
769n,
773n,
787n,
797n,
809n,
811n,
821n,
823n,
827n,
829n,
839n,
853n,
857n,
859n,
863n,
877n,
881n,
883n,
887n,
907n,
911n,
919n,
929n,
937n,
941n,
947n,
953n,
967n,
971n,
977n,
983n,
991n,
997n,
1009n,
1013n,
1019n,
1021n,
1031n,
1033n,
1039n,
1049n,
1051n,
1061n,
1063n,
1069n,
1087n,
1091n,
1093n,
1097n,
1103n,
1109n,
1117n,
1123n,
1129n,
1151n,
1153n,
1163n,
1171n,
1181n,
1187n,
1193n,
1201n,
1213n,
1217n,
1223n,
1229n,
1231n,
1237n,
1249n,
1259n,
1277n,
1279n,
1283n,
1289n,
1291n,
1297n,
1301n,
1303n,
1307n,
1319n,
1321n,
1327n,
1361n,
1367n,
1373n,
1381n,
1399n,
1409n,
1423n,
1427n,
1429n,
1433n,
1439n,
1447n,
1451n,
1453n,
1459n,
1471n,
1481n,
1483n,
1487n,
1489n,
1493n,
1499n,
1511n,
1523n,
1531n,
1543n,
1549n,
1553n,
1559n,
1567n,
1571n,
1579n,
1583n,
1597n,
1601n,
1607n,
1609n,
1613n,
1619n,
1621n,
1627n,
1637n,
1657n,
1663n,
1667n,
1669n,
1693n,
1697n,
1699n,
1709n,
1721n,
1723n,
1733n,
1741n,
1747n,
1753n,
1759n,
1777n,
1783n,
1787n,
1789n,
1801n,
1811n,
1823n,
1831n,
1847n,
1861n,
1867n,
1871n,
1873n,
1877n,
1879n,
1889n,
1901n,
1907n,
1913n,
1931n,
1933n,
1949n,
1951n,
1973n,
1979n,
1987n,
1993n,
1997n,
1999n,
2003n,
2011n,
2017n,
2027n,
2029n,
2039n,
2053n,
2063n,
2069n,
2081n,
2083n,
2087n,
2089n,
2099n,
2111n,
2113n,
2129n,
2131n,
2137n,
2141n,
2143n,
2153n,
2161n,
2179n,
2203n,
2207n,
2213n,
2221n,
2237n,
2239n,
2243n,
2251n,
2267n,
2269n,
2273n,
2281n,
2287n,
2293n,
2297n,
2309n,
2311n,
2333n,
2339n,
2341n,
2347n,
2351n,
2357n,
2371n,
2377n,
2381n,
2383n,
2389n,
2393n,
2399n,
2411n,
2417n,
2423n,
2437n,
2441n,
2447n,
2459n,
2467n,
2473n,
2477n,
2503n,
2521n,
2531n,
2539n,
2543n,
2549n,
2551n,
2557n,
2579n,
2591n,
2593n,
2609n,
2617n,
2621n,
2633n,
2647n,
2657n,
2659n,
2663n,
2671n,
2677n,
2683n,
2687n,
2689n,
2693n,
2699n,
2707n,
2711n,
2713n,
2719n,
2729n,
2731n,
2741n,
2749n,
2753n,
2767n,
2777n,
2789n,
2791n,
2797n,
2801n,
2803n,
2819n,
2833n,
2837n,
2843n,
2851n,
2857n,
2861n,
2879n,
2887n,
2897n,
2903n,
2909n,
2917n,
2927n,
2939n,
2953n,
2957n,
2963n,
2969n,
2971n,
2999n,
3001n,
3011n,
3019n,
3023n,
3037n,
3041n,
3049n,
3061n,
3067n,
3079n,
3083n,
3089n,
3109n,
3119n,
3121n,
3137n,
3163n,
3167n,
3169n,
3181n,
3187n,
3191n,
3203n,
3209n,
3217n,
3221n,
3229n,
3251n,
3253n,
3257n,
3259n,
3271n,
3299n,
3301n,
3307n,
3313n,
3319n,
3323n,
3329n,
3331n,
3343n,
3347n,
3359n,
3361n,
3371n,
3373n,
3389n,
3391n,
3407n,
3413n,
3433n,
3449n,
3457n,
3461n,
3463n,
3467n,
3469n,
3491n,
3499n,
3511n,
3517n,
3527n,
3529n,
3533n,
3539n,
3541n,
3547n,
3557n,
3559n,
3571n,
3581n,
3583n,
3593n,
3607n,
3613n,
3617n,
3623n,
3631n,
3637n,
3643n,
3659n,
3671n,
3673n,
3677n,
3691n,
3697n,
3701n,
3709n,
3719n,
3727n,
3733n,
3739n,
3761n,
3767n,
3769n,
3779n,
3793n,
3797n,
3803n,
3821n,
3823n,
3833n,
3847n,
3851n,
3853n,
3863n,
3877n,
3881n,
3889n,
3907n,
3911n,
3917n,
3919n,
3923n,
3929n,
3931n,
3943n,
3947n,
3967n,
3989n,
4001n,
4003n,
4007n,
4013n,
4019n,
4021n,
4027n,
4049n,
4051n,
4057n,
4073n,
4079n,
4091n,
4093n,
4099n,
4111n,
4127n,
4129n,
4133n,
4139n,
4153n,
4157n,
4159n,
4177n,
4201n,
4211n,
4217n,
4219n,
4229n,
4231n,
4241n,
4243n,
4253n,
4259n,
4261n,
4271n,
4273n,
4283n,
4289n,
4297n,
4327n,
4337n,
4339n,
4349n,
4357n,
4363n,
4373n,
4391n,
4397n,
4409n,
4421n,
4423n,
4441n,
4447n,
4451n,
4457n,
4463n,
4481n,
4483n,
4493n,
4507n,
4513n,
4517n,
4519n,
4523n,
4547n,
4549n,
4561n,
4567n,
4583n,
4591n,
4597n,
4603n,
4621n,
4637n,
4639n,
4643n,
4649n,
4651n,
4657n,
4663n,
4673n,
4679n,
4691n,
4703n,
4721n,
4723n,
4729n,
4733n,
4751n,
4759n,
4783n,
4787n,
4789n,
4793n,
4799n,
4801n,
4813n,
4817n,
4831n,
4861n,
4871n,
4877n,
4889n,
4903n,
4909n,
4919n,
4931n,
4933n,
4937n,
4943n,
4951n,
4957n,
4967n,
4969n,
4973n,
4987n,
4993n,
4999n,
5003n,
5009n,
5011n,
5021n,
5023n,
5039n,
5051n,
5059n,
5077n,
5081n,
5087n,
5099n,
5101n,
5107n,
5113n,
5119n,
5147n,
5153n,
5167n,
5171n,
5179n,
5189n,
5197n,
5209n,
5227n,
5231n,
5233n,
5237n,
5261n,
5273n,
5279n,
5281n,
5297n,
5303n,
5309n,
5323n,
5333n,
5347n,
5351n,
5381n,
5387n,
5393n,
5399n,
5407n,
5413n,
5417n,
5419n,
5431n,
5437n,
5441n,
5443n,
5449n,
5471n,
5477n,
5479n,
5483n,
5501n,
5503n,
5507n,
5519n,
5521n,
5527n,
5531n,
5557n,
5563n,
5569n,
5573n,
5581n,
5591n,
5623n,
5639n,
5641n,
5647n,
5651n,
5653n,
5657n,
5659n,
5669n,
5683n,
5689n,
5693n,
5701n,
5711n,
5717n,
5737n,
5741n,
5743n,
5749n,
5779n,
5783n,
5791n,
5801n,
5807n,
5813n,
5821n,
5827n,
5839n,
5843n,
5849n,
5851n,
5857n,
5861n,
5867n,
5869n,
5879n,
5881n,
5897n,
5903n,
5923n,
5927n,
5939n,
5953n,
5981n,
5987n,
6007n,
6011n,
6029n,
6037n,
6043n,
6047n,
6053n,
6067n,
6073n,
6079n,
6089n,
6091n,
6101n,
6113n,
6121n,
6131n,
6133n,
6143n,
6151n,
6163n,
6173n,
6197n,
6199n,
6203n,
6211n,
6217n,
6221n,
6229n,
6247n,
6257n,
6263n,
6269n,
6271n,
6277n,
6287n,
6299n,
6301n,
6311n,
6317n,
6323n,
6329n,
6337n,
6343n,
6353n,
6359n,
6361n,
6367n,
6373n,
6379n,
6389n,
6397n,
6421n,
6427n,
6449n,
6451n,
6469n,
6473n,
6481n,
6491n,
6521n,
6529n,
6547n,
6551n,
6553n,
6563n,
6569n,
6571n,
6577n,
6581n,
6599n,
6607n,
6619n,
6637n,
6653n,
6659n,
6661n,
6673n,
6679n,
6689n,
6691n,
6701n,
6703n,
6709n,
6719n,
6733n,
6737n,
6761n,
6763n,
6779n,
6781n,
6791n,
6793n,
6803n,
6823n,
6827n,
6829n,
6833n,
6841n,
6857n,
6863n,
6869n,
6871n,
6883n,
6899n,
6907n,
6911n,
6917n,
6947n,
6949n,
6959n,
6961n,
6967n,
6971n,
6977n,
6983n,
6991n,
6997n,
7001n,
7013n,
7019n,
7027n,
7039n,
7043n,
7057n,
7069n,
7079n,
7103n,
7109n,
7121n,
7127n,
7129n,
7151n,
7159n,
7177n,
7187n,
7193n,
7207n,
7211n,
7213n,
7219n,
7229n,
7237n,
7243n,
7247n,
7253n,
7283n,
7297n,
7307n,
7309n,
7321n,
7331n,
7333n,
7349n,
7351n,
7369n,
7393n,
7411n,
7417n,
7433n,
7451n,
7457n,
7459n,
7477n,
7481n,
7487n,
7489n,
7499n,
7507n,
7517n,
7523n,
7529n,
7537n,
7541n,
7547n,
7549n,
7559n,
7561n,
7573n,
7577n,
7583n,
7589n,
7591n,
7603n,
7607n,
7621n,
7639n,
7643n,
7649n,
7669n,
7673n,
7681n,
7687n,
7691n,
7699n,
7703n,
7717n,
7723n,
7727n,
7741n,
7753n,
7757n,
7759n,
7789n,
7793n,
7817n,
7823n,
7829n,
7841n,
7853n,
7867n,
7873n,
7877n,
7879n,
7883n,
7901n,
7907n,
7919n,
7927n,
7933n,
7937n,
7949n,
7951n,
7963n,
7993n,
8009n,
8011n,
8017n,
8039n,
8053n,
8059n,
8069n,
8081n,
8087n,
8089n,
8093n,
8101n,
8111n,
8117n,
8123n,
8147n,
8161n,
8167n,
8171n,
8179n,
8191n,
8209n,
8219n,
8221n,
8231n,
8233n,
8237n,
8243n,
8263n,
8269n,
8273n,
8287n,
8291n,
8293n,
8297n,
8311n,
8317n,
8329n,
8353n,
8363n,
8369n,
8377n,
8387n,
8389n,
8419n,
8423n,
8429n,
8431n,
8443n,
8447n,
8461n,
8467n,
8501n,
8513n,
8521n,
8527n,
8537n,
8539n,
8543n,
8563n,
8573n,
8581n,
8597n,
8599n,
8609n,
8623n,
8627n,
8629n,
8641n,
8647n,
8663n,
8669n,
8677n,
8681n,
8689n,
8693n,
8699n,
8707n,
8713n,
8719n,
8731n,
8737n,
8741n,
8747n,
8753n,
8761n,
8779n,
8783n,
8803n,
8807n,
8819n,
8821n,
8831n,
8837n,
8839n,
8849n,
8861n,
8863n,
8867n,
8887n,
8893n,
8923n,
8929n,
8933n,
8941n,
8951n,
8963n,
8969n,
8971n,
8999n,
9001n,
9007n,
9011n,
9013n,
9029n,
9041n,
9043n,
9049n,
9059n,
9067n,
9091n,
9103n,
9109n,
9127n,
9133n,
9137n,
9151n,
9157n,
9161n,
9173n,
9181n,
9187n,
9199n,
9203n,
9209n,
9221n,
9227n,
9239n,
9241n,
9257n,
9277n,
9281n,
9283n,
9293n,
9311n,
9319n,
9323n,
9337n,
9341n,
9343n,
9349n,
9371n,
9377n,
9391n,
9397n,
9403n,
9413n,
9419n,
9421n,
9431n,
9433n,
9437n,
9439n,
9461n,
9463n,
9467n,
9473n,
9479n,
9491n,
9497n,
9511n,
9521n,
9533n,
9539n,
9547n,
9551n,
9587n,
9601n,
9613n,
9619n,
9623n,
9629n,
9631n,
9643n,
9649n,
9661n,
9677n,
9679n,
9689n,
9697n,
9719n,
9721n,
9733n,
9739n,
9743n,
9749n,
9767n,
9769n,
9781n,
9787n,
9791n,
9803n,
9811n,
9817n,
9829n,
9833n,
9839n,
9851n,
9857n,
9859n,
9871n,
9883n,
9887n,
9901n,
9907n,
9923n,
9929n,
9931n,
9941n,
9949n,
9967n,
9973n,
];