forked from LiskArchive/js-nacl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.js
133 lines (111 loc) · 3.8 KB
/
benchmark.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function main () {
if (typeof window !== 'undefined') {
var browser =
navigator.userAgent.match(/Firefox\/[^ ]+/) ||
navigator.userAgent.match(/Chrome\/[^ ]+/) ||
navigator.userAgent.match(/Safari\/[^ ]+/) ||
["unknown/0.0"]; // perhaps it's IE. meh
output(browser[0]);
} else {
output("node/" + process.version);
}
output("Test,Iterations per second,Seconds per iteration");
try {
nacl_factory.instantiate(do_tests, {
memoryInitializerPrefixURL: 'lib/'
});
} catch (e) {
console.error(e);
output('EXCEPTION: ' + e.message);
}
}
var TIMELIMIT = 1000;
function measure(desc, f) {
try {
var iterations;
var iter_per_sec;
var best_iter_per_sec = 0;
var startTime;
var stopTime;
var delta;
var i;
var result;
for (iterations = 0; iterations < 3; iterations++) {
startTime = new Date().getTime();
i = 0;
while (1) {
result = f();
i++;
stopTime = new Date().getTime();
delta = stopTime - startTime;
if (delta > TIMELIMIT) break;
}
iter_per_sec = i / (delta / 1000);
if (iter_per_sec > best_iter_per_sec) best_iter_per_sec = iter_per_sec;
}
var sec_per_iter = 1.0 / best_iter_per_sec;
output([JSON.stringify(desc), best_iter_per_sec, sec_per_iter].join(','));
} catch (e) {
result = null;
output([JSON.stringify(desc), "FAILED", "FAILED", JSON.stringify(e)].join(','));
}
return result;
}
function do_tests(nacl) {
var hello = nacl.encode_utf8("hello");
var kp = nacl.crypto_box_keypair_from_seed(hello);
var skp = nacl.crypto_sign_keypair_from_seed(nacl.crypto_hash_string("This is my passphrase").subarray(0, 32));
var selfShared = nacl.crypto_box_precompute(kp.boxPk, kp.boxSk);
var n = nacl.crypto_box_random_nonce();
var c = nacl.crypto_box_precomputed(hello, n, selfShared);
var m = nacl.crypto_box_open_precomputed(c, n, selfShared);
var c2 = nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk);
var m2 = nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk);
var signed = nacl.crypto_sign(m, skp.signSk);
var tests = [
['nacl.crypto_hash_string("hello")',
function () { return nacl.crypto_hash_string("hello") }],
['nacl.crypto_hash(hello)',
function () { return nacl.crypto_hash(hello) }],
['nacl.crypto_box_keypair_from_seed(hello)',
function () { return nacl.crypto_box_keypair_from_seed(hello) }],
['nacl.crypto_box_precompute(kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box_precompute(kp.boxPk, kp.boxSk) }],
['nacl.crypto_box_random_nonce()',
function () { return nacl.crypto_box_random_nonce() }],
['nacl.crypto_box_precomputed(hello, n, selfShared)',
function () { return nacl.crypto_box_precomputed(hello, n, selfShared) }],
['nacl.crypto_box_open_precomputed(c, n, selfShared)',
function () { return nacl.crypto_box_open_precomputed(c, n, selfShared) }],
['nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box(hello, n, kp.boxPk, kp.boxSk) }],
['nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk)',
function () { return nacl.crypto_box_open(c2, n, kp.boxPk, kp.boxSk) }],
['nacl.crypto_sign(m, skp.signSk)',
function () { return nacl.crypto_sign(m, skp.signSk) }],
['nacl.crypto_sign_open(signed, skp.signPk)',
function () { return nacl.crypto_sign_open(signed, skp.signPk) }]
];
var testNum = 0;
function runNextTest() {
if (testNum < tests.length) {
measure(tests[testNum][0], tests[testNum][1]);
testNum++;
setTimeout(runNextTest, 0);
}
}
runNextTest();
}
var output;
if (typeof window !== 'undefined') {
output = function (x) {
document.getElementById("output").innerHTML += x + "\n";
};
window.onload = main;
} else {
nacl_factory = require("./lib/nacl_factory.js");
output = function (x) {
console.log(x);
};
main();
}