-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigIntManager.js
58 lines (45 loc) · 1.62 KB
/
bigIntManager.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const bigInteger = require('big-integer');
const crypto = require('crypto');
const jsbn = require('jsbn').BigInteger;
const thread = require('threads');
const debug = require('debug');
const log = debug('app::bigIntegerManager');
function trim(number, numberOfLength){
}
async function asyncCryptoRandomGenerator(length){
return new Promise((resolve, reject)=>{
crypto.randomBytes(length ,(err, buff)=>{
if(err)
reject(err);
resolve(buff);
})
})
}
/**
* This function doesn't produce the prime numbers exactly with same length of bits,
* so it's better to pass it one less than your target bits and check it yourself to be prime.
* @param {integer} bitLength indicate the length of digits in binary representation.
*/
async function getPrime(bitLength){
const primeProducer = await thread.spawn(new thread.Worker('./primeProducerThread'));
const prime = await primeProducer.producePrime(bitLength);
await thread.Thread.terminate(primeProducer);
return bigInteger(prime.value);
}
/**
*
* @param {bigInteger.BigInteger} max excluded
* @param {number|bigInteger.BigInteger} min included
* @returns {Promise<bigInteger.BigInteger>}
*/
async function randomGeneratorInRange(max, min){
const range = max.subtract(min).subtract(1);
let bi = undefined;
do {
const buffer = await asyncCryptoRandomGenerator(Math.ceil(range.bitLength()/8));
bi = bigInteger(buffer.toString('hex'), 16).add(min);
} while (bi.compare(max) >= 0);
return bi;
}
module.exports.getInRange = randomGeneratorInRange;
module.exports.getPrime = getPrime;