import { __commonJS } from "../../../_virtual/rolldown_runtime.mjs";
import { require_forge } from "./forge.mjs";
import { require_util } from "./util.mjs";
import { require_random } from "./random.mjs";
import { require_jsbn } from "./jsbn.mjs";

//#region ../../node_modules/node-forge/lib/prime.js
var require_prime = /* @__PURE__ */ __commonJS({ "../../node_modules/node-forge/lib/prime.js": ((exports, module) => {
	/**
	* Prime number generation API.
	*
	* @author Dave Longley
	*
	* Copyright (c) 2014 Digital Bazaar, Inc.
	*/
	var forge = require_forge();
	require_util();
	require_jsbn();
	require_random();
	(function() {
		if (forge.prime) {
			module.exports = forge.prime;
			return;
		}
		var prime = module.exports = forge.prime = forge.prime || {};
		var BigInteger = forge.jsbn.BigInteger;
		var GCD_30_DELTA = [
			6,
			4,
			2,
			4,
			2,
			4,
			6,
			2
		];
		var THIRTY = new BigInteger(null);
		THIRTY.fromInt(30);
		var op_or = function(x, y) {
			return x | y;
		};
		/**
		* Generates a random probable prime with the given number of bits.
		*
		* Alternative algorithms can be specified by name as a string or as an
		* object with custom options like so:
		*
		* {
		*   name: 'PRIMEINC',
		*   options: {
		*     maxBlockTime: <the maximum amount of time to block the main
		*       thread before allowing I/O other JS to run>,
		*     millerRabinTests: <the number of miller-rabin tests to run>,
		*     workerScript: <the worker script URL>,
		*     workers: <the number of web workers (if supported) to use,
		*       -1 to use estimated cores minus one>.
		*     workLoad: the size of the work load, ie: number of possible prime
		*       numbers for each web worker to check per work assignment,
		*       (default: 100).
		*   }
		* }
		*
		* @param bits the number of bits for the prime number.
		* @param options the options to use.
		*          [algorithm] the algorithm to use (default: 'PRIMEINC').
		*          [prng] a custom crypto-secure pseudo-random number generator to use,
		*            that must define "getBytesSync".
		*
		* @return callback(err, num) called once the operation completes.
		*/
		prime.generateProbablePrime = function(bits, options, callback) {
			if (typeof options === "function") {
				callback = options;
				options = {};
			}
			options = options || {};
			var algorithm = options.algorithm || "PRIMEINC";
			if (typeof algorithm === "string") algorithm = { name: algorithm };
			algorithm.options = algorithm.options || {};
			var prng = options.prng || forge.random;
			var rng = { nextBytes: function(x) {
				var b = prng.getBytesSync(x.length);
				for (var i = 0; i < x.length; ++i) x[i] = b.charCodeAt(i);
			} };
			if (algorithm.name === "PRIMEINC") return primeincFindPrime(bits, rng, algorithm.options, callback);
			throw new Error("Invalid prime generation algorithm: " + algorithm.name);
		};
		function primeincFindPrime(bits, rng, options, callback) {
			if ("workers" in options) return primeincFindPrimeWithWorkers(bits, rng, options, callback);
			return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
		}
		function primeincFindPrimeWithoutWorkers(bits, rng, options, callback) {
			var num = generateRandom(bits, rng);
			var deltaIdx = 0;
			var mrTests = getMillerRabinTests(num.bitLength());
			if ("millerRabinTests" in options) mrTests = options.millerRabinTests;
			var maxBlockTime = 10;
			if ("maxBlockTime" in options) maxBlockTime = options.maxBlockTime;
			_primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
		}
		function _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback) {
			var start = +/* @__PURE__ */ new Date();
			do {
				if (num.bitLength() > bits) num = generateRandom(bits, rng);
				if (num.isProbablePrime(mrTests)) return callback(null, num);
				num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
			} while (maxBlockTime < 0 || +/* @__PURE__ */ new Date() - start < maxBlockTime);
			forge.util.setImmediate(function() {
				_primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
			});
		}
		function primeincFindPrimeWithWorkers(bits, rng, options, callback) {
			if (typeof Worker === "undefined") return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
			var num = generateRandom(bits, rng);
			var numWorkers = options.workers;
			var workLoad = options.workLoad || 100;
			var range = workLoad * 30 / 8;
			var workerScript = options.workerScript || "forge/prime.worker.js";
			if (numWorkers === -1) return forge.util.estimateCores(function(err, cores) {
				if (err) cores = 2;
				numWorkers = cores - 1;
				generate();
			});
			generate();
			function generate() {
				numWorkers = Math.max(1, numWorkers);
				var workers = [];
				for (var i = 0; i < numWorkers; ++i) workers[i] = new Worker(workerScript);
				var running = numWorkers;
				for (var i = 0; i < numWorkers; ++i) workers[i].addEventListener("message", workerMessage);
				var found = false;
				function workerMessage(e) {
					if (found) return;
					--running;
					var data = e.data;
					if (data.found) {
						for (var i$1 = 0; i$1 < workers.length; ++i$1) workers[i$1].terminate();
						found = true;
						return callback(null, new BigInteger(data.prime, 16));
					}
					if (num.bitLength() > bits) num = generateRandom(bits, rng);
					var hex = num.toString(16);
					e.target.postMessage({
						hex,
						workLoad
					});
					num.dAddOffset(range, 0);
				}
			}
		}
		/**
		* Generates a random number using the given number of bits and RNG.
		*
		* @param bits the number of bits for the number.
		* @param rng the random number generator to use.
		*
		* @return the random number.
		*/
		function generateRandom(bits, rng) {
			var num = new BigInteger(bits, rng);
			var bits1 = bits - 1;
			if (!num.testBit(bits1)) num.bitwiseTo(BigInteger.ONE.shiftLeft(bits1), op_or, num);
			num.dAddOffset(31 - num.mod(THIRTY).byteValue(), 0);
			return num;
		}
		/**
		* Returns the required number of Miller-Rabin tests to generate a
		* prime with an error probability of (1/2)^80.
		*
		* See Handbook of Applied Cryptography Chapter 4, Table 4.4.
		*
		* @param bits the bit size.
		*
		* @return the required number of iterations.
		*/
		function getMillerRabinTests(bits) {
			if (bits <= 100) return 27;
			if (bits <= 150) return 18;
			if (bits <= 200) return 15;
			if (bits <= 250) return 12;
			if (bits <= 300) return 9;
			if (bits <= 350) return 8;
			if (bits <= 400) return 7;
			if (bits <= 500) return 6;
			if (bits <= 600) return 5;
			if (bits <= 800) return 4;
			if (bits <= 1250) return 3;
			return 2;
		}
	})();
}) });

//#endregion
export default require_prime();

export { require_prime };
//# sourceMappingURL=prime.mjs.map