-
Notifications
You must be signed in to change notification settings - Fork 2
/
random.js
54 lines (46 loc) · 1.54 KB
/
random.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
/*
* Random Number generation, now uses the glue to Java
*/
Random = {};
Random.GENERATOR = null;
Random.setupGenerator = function() {
if (Random.GENERATOR == null && !USE_SJCL) {
if (BigInt.use_applet) {
var foo = BigInt.APPLET.newSecureRandom();
Random.GENERATOR = BigInt.APPLET.newSecureRandom();
} else {
// we do it twice because of some weird bug;
var foo = new java.security.SecureRandom();
Random.GENERATOR = new java.security.SecureRandom();
}
}
};
Random.getRandomInteger = function(max) {
var bit_length = max.bitLength();
Random.setupGenerator();
var random;
if (USE_SJCL) {
random = sjcl.random.randomWords(bit_length / 32, 0);
// we get a bit array instead of a BigInteger in this case
var rand_bi = new BigInt(sjcl.codec.hex.fromBits(random), 16);
return rand_bi.mod(max);
} else if (BigInt.use_applet) {
random = BigInt.APPLET.randomBigInteger(bit_length, Random.GENERATOR);
} else {
random = new java.math.BigInteger(bit_length, Random.GENERATOR);
}
return BigInt._from_java_object(random).mod(max);
};
Random.getRandomPrime = function(n_bits) {
Random.setupGenerator();
var certainty = 80;
var prime;
if (USE_SJCL) {
throw "getRandomPrime not implemented for SJCL";
} else if (BigInt.use_applet) {
prime = BigInt.APPLET.randomPrimeBigInteger(n_bits, certainty, Random.GENERATOR);
} else {
prime = new java.math.BigInteger(n_bits, certainty, Random.GENERATOR);
}
return BigInt._from_java_object(prime);
};