UNPKG

@h0llyw00dzz/crypto-rand

Version:

Cryptographically secure random utilities for Node.js and browsers

137 lines 4.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isProbablePrime = isProbablePrime; exports.modPow = modPow; exports.modInverse = modInverse; /** * Internal math utilities for cryptographic operations. * These functions are intended for internal use only within the crypto-rand package. */ const crypto = __importStar(require("crypto")); /** * [Miller-Rabin primality test](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test) * * @param n - The number to test for primality * @param k - The number of iterations for the test * @param getRandomBytes - Function to generate random bytes (defaults to crypto.randomBytes) * @returns A boolean indicating whether the number is probably prime */ function isProbablePrime(n, k, getRandomBytes = crypto.randomBytes) { // Handle small numbers if (n <= 1n) return false; if (n <= 3n) return true; if (n % 2n === 0n) return false; // Write n-1 as 2ʳ × d where d is odd let r = 0; let d = n - 1n; while (d % 2n === 0n) { d /= 2n; r++; } // Witness loop for (let i = 0; i < k; i++) { // Generate a random integer a in the range [2, n-2] const randomBytes = getRandomBytes(64); // 64 bytes should be enough for most primes let a = BigInt('0x' + randomBytes.toString('hex')) % (n - 4n) + 2n; // Compute aᵈ mod n let x = modPow(a, d, n); if (x === 1n || x === n - 1n) continue; let continueWitness = false; for (let j = 0; j < r - 1; j++) { x = modPow(x, 2n, n); if (x === n - 1n) { continueWitness = true; break; } } if (continueWitness) continue; return false; } return true; } /** * [Modular exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation): baseᵉˣᵖᵒⁿᵉⁿᵗ mod modulus * * @param base - The base value * @param exponent - The exponent value * @param modulus - The modulus value * @returns The result of the modular exponentiation */ function modPow(base, exponent, modulus) { if (modulus === 1n) return 0n; let result = 1n; base = base % modulus; while (exponent > 0n) { if (exponent % 2n === 1n) { result = (result * base) % modulus; } exponent = exponent >> 1n; base = (base * base) % modulus; } return result; } /** * Calculate the [modular multiplicative inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) using the [Extended Euclidean Algorithm](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) * * @param a - The number to find the inverse for * @param m - The modulus * @returns The [modular multiplicative inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) inverse of a modulo m */ function modInverse(a, m) { // Extended Euclidean Algorithm to find modular multiplicative inverse let [old_r, r] = [a, m]; let [old_s, s] = [1n, 0n]; let [old_t, t] = [0n, 1n]; while (r !== 0n) { const quotient = old_r / r; [old_r, r] = [r, old_r - quotient * r]; [old_s, s] = [s, old_s - quotient * s]; [old_t, t] = [t, old_t - quotient * t]; } // If old_r != 1, then a and m are not coprime and inverse doesn't exist if (old_r !== 1n) { throw new Error('Modular inverse does not exist'); } // Make sure the result is positive return (old_s % m + m) % m; } //# sourceMappingURL=math_helper.js.map