-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeJSAlgamaWithModPow.js
More file actions
89 lines (75 loc) · 3.21 KB
/
nodeJSAlgamaWithModPow.js
File metadata and controls
89 lines (75 loc) · 3.21 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const args = require('minimist')(process.argv.slice(2))
function getBoundInteger(value, lowBound, upBound, defaultValue)
{
switch (typeof value)
{
case 'string' : value = parseInt(value);
case "number" : return (value < lowBound ? lowBound : (value > upBound ? upBound : value));
case 'undefined':
default: return defaultValue;
}
}
var loopCount = getBoundInteger(args['loopCount'], 10, 100000, 100);
var baseNumberString = getBoundInteger(args['baseNum'], 10, 99999999999999999999, 12345).toString();
var exponentNumberString = getBoundInteger(args['expNum'], 10, 999, 12).toString();
var moduloNumberString = getBoundInteger(args['modNum'], 10, 999999999, 123).toString();
console.log('loopCount', loopCount);
console.log('baseNumberString', baseNumberString);
console.log('exponentNumberString', exponentNumberString);
console.log('moduloNumberString', moduloNumberString);
///////////////////////////////////////////////////////////
//import java.math.BigInteger;
//import java.security.SecureRandom;
var moduleStr = 'bigint-crypto-utils'
const bigintCryptoUtils = require(moduleStr);
var prime;
const primePromise = bigintCryptoUtils.prime(2048);
const bigInt1KBits = BigInt(2n ** 256n);
const bigInt256Bits = BigInt(2n ** 32n);
const bigIn32Bits = BigInt(2n ** 4n);
var errHandler = function(err) {
console.log(err);
}
async function f() {
let promise = primePromise.then(function(result) {
prime = result;
}, errHandler);
await promise.catch((err) => { console.log(err); }); // wait till the promise resolves (*)
//val p = BigInteger.probablePrime(1000, new SecureRandom());
var p = prime;
//val g = new BigInteger(1000, new SecureRandom());
var g = BigInt(bigintCryptoUtils.randBetween(bigInt1KBits));
//val privateKey = new BigInteger(1000, new SecureRandom());
var privateKey = BigInt(bigintCryptoUtils.randBetween(bigInt1KBits));
//val publicKey = g.modPow(privateKey, p)
var publicKey = bigintCryptoUtils.modPow(g, privateKey, p)
//var publicKey = (g ** privateKey) % p
//println(s"private key ${privateKey}")
console.log("private key ", privateKey)
//println(s"public key ${publicKey}")
console.log("public key ", publicKey)
//val start = System.currentTimeMillis();
nameTag = "elgamal pairs"
var m,r,x,y;
console.time(nameTag);
for (var i = 1; i < loopCount; i++) {
//var m = new BigInteger(32, new SecureRandom());
m = bigintCryptoUtils.randBetween(bigIn32Bits);
//var r = new BigInteger(1000, new SecureRandom());
r = bigintCryptoUtils.randBetween(bigInt1KBits,m);
//var x = g.modPow(r, p);
x = bigintCryptoUtils.modPow(g, r, p)
// x = (g ** r) % p;
//var y = publicKey.modPow(r, p).multiply(m).mod(p);
y = (bigintCryptoUtils.modPow(publicKey, r, p) * m)%p;
// y = (((publicKey ** r) % p) * m) % p;
//println(s"x = ${x}");
console.log("x = ", x.toString())
//println(s"y = ${y}");
console.log("y = ", x.toString())
}
//val end = System.currentTimeMillis();
console.timeEnd(nameTag);
}
f();
return