-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.html
358 lines (313 loc) · 12.8 KB
/
benchmark.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content=
"text/html;charset=utf8" />
<title>
Benchmark
</title>
<script type="text/javascript" src="lib/jquery.min.js">
</script>
<script type="text/javascript" src=
"3rdparty/jquery.dataTables.min.js">
</script>
<script type="text/javascript" src="lib/util.js">
</script>
<script type="text/javascript" src="3rdparty/sjcl.js">
</script>
<script type="text/javascript" src="lib/glengine.js">
</script>
<script type="text/javascript" src="lib/aes.js">
</script>
<script type="text/javascript">
//<![CDATA[
// the count of the fixed point
var c_fp = 2;
// the size of the first block
var sz_blk0 = 16;
// the stride of multiples increase
var stride = 4;
// at what size will the benchmark stop
var sz_blk_max = 1048576;
// the number of times each group of tests runs
var n = 3;
var iv = 0x12345678;
var gctr;
var glaes = null;
var sjclaes = null;
function clear(){
document.getElementById("info").innerHTML = "";
document.getElementById("err").innerHTML = "";
}
function showMsg(msg){
document.getElementById("info").innerHTML += msg + "<br/>";
}
function showErr(msg){
document.getElementById("err").innerHTML += msg + "<br/>";
}
function init(){
try{
var ts = new Date();
glaes = new GLAES();
var te = new Date();
var config = "GLAES initialization: " + (te - ts) + "ms<br/>";
config += window.navigator.userAgent;
$("#configuration").html(config);
} catch(e){
showErr(e);
$("#btn_glaes").prop("disabled", true);
}
}
function addResult(node){
var nRslt = document.getElementById("result");
if(!nRslt.hasChildNodes())
nRslt.appendChild(node);
else
nRslt.insertBefore(node, nRslt.firstChild);
}
function initTable(title){
var table = document.createElement("table");
var p = document.createElement("p");
var h = document.createElement("h3");
h.innerHTML = title;
p.appendChild(h);
p.appendChild(table);
p.appendChild(document.createElement("hr"));
// insert the table as the first child of the node "#result"
addResult(p);
var html = "<thead>" +
"<th>Block Size(B)<\/th>" +
"<th>Time(ms)<\/th>" +
"<th>Throughput(KB/s)<\/th>" +
"<\/thead>";
table = $(table);
table.html(html);
return table.dataTable({
"bPaginate" : false,
"bSearch" : false,
"bFilter" : false,
"bInfo" : false
});
}
function initVars(){
var sz = $("#t_sz_blk").val();
var cnt = $("#t_times").val();
try{
sz = parseInt(sz);
sz_blk_max = sz;
gctr = generateCTRSeq(iv, sz_blk_max);
cnt = parseInt(cnt);
n = cnt;
return true;
} catch(e){
showErr(e);
return false;
}
}
function runGLAES(btn){
$("button").prop("disabled", true);
showMsg("Benchmark started!");
benchmark("GLAES",
function(key){
glaes.setKey(key);
},
function(pt, ct, ctr){
var rslt = glaes.encrypt_ecb(pt, ct);
// var rslt = glaes.encrypt_ctr(pt, ct, 0, ctr);
return rslt;
}
);
$("button").prop("disabled", false);
showMsg("Benchmark completed!");
}
function runSJCL(btn){
$("button").prop("disabled", true);
showMsg("Benchmark started!");
benchmark("SJCL-AES",
function(key){
sjclaes = new sjcl.cipher.aes(bytesToWords(key));
},
function(pt, ct, ctr){
var sjclpt = bytesToWords(pt);
if(!ct)
ct = new Uint8Array(pt.length);
// sjclEncryptCTR(sjclaes, pt, ctr, ct);
for(var i = 0; i < sjclpt.length; i += 4){
ct.set(wordsToBytes(sjclaes.encrypt(sjclpt.slice(i, i + 4))), i * 4);
}
}
);
$("button").prop("disabled", false);
showMsg("Benchmark completed!");
}
function benchmark(title, setkey, encrypt){
// initialize variables
clear();
if(!initVars())
return;
// initialize the datatable
var table = initTable(title);
var randomtext = new Uint8Array(sz_blk_max);
var ciphertext = new Uint8Array(sz_blk_max);
var key = new Array(16);
// generate random values for the plaintext
for(var i = 0; i < randomtext.length; i++)
randomtext[i] = parseInt(Math.floor(Math.random() * 256));
// generate values for the key
// the size of the key is 16 bytes
for(var i = 0; i < key.length; i++)
key[i] = i * 0x11;
// glaes.setKey(key);
setkey(key);
// for comparison
var s_aes = new sjcl.cipher.aes(bytesToWords(key));
var sjclct = new Uint8Array(randomtext.length);
// sjclEncryptCTR(s_aes, randomtext, gctr, sjclct);
var sjclpt = bytesToWords(randomtext);
for(var i = 0; i < sjclpt.length; i += 4){
sjclct.set(wordsToBytes(s_aes.encrypt(sjclpt.slice(i, i + 4))), i * 4);
}
// cold boot the engine
// glaes.encrypt(randomtext.subarray(0, 16));
for(var sz = sz_blk0; sz <= sz_blk_max; sz = sz * stride){
showMsg("Testing for block size " + sz);
var pt = randomtext.subarray(0, sz);
var ct = ciphertext.subarray(0, sz);
var ts = new Date();
for(var i = 0; i < n; i++){
encrypt(pt, ct, gctr.subarray(0, pt.length));
}
var te = new Date();
var time = te - ts;
// check if the encryption result is correct
var isCorrect = true;
for(var j = 0; j < sz; j++){
if(ct[j] != sjclct[j]){
isCorrect = false;
break;
}
}
if(!isCorrect){
showErr("The result is not correct for block size " + sz + " !");
// return;
}
// output the result
var throughput = (sz * n / time).toFixed(c_fp);
var t = new Array(sz, time, throughput);
table.fnAddData(t);
}
}
// for benchmark 2
var sz_pt;
var sz_framebuffer;
function runGLAES2(btn){
$("button").prop("disabled", true);
showMsg("Benchmark started!");
benchmark2("GLAES",
function(key){
glaes.setKey(key);
},
function(pt, ct, ctr){
glaes.encrypt_ecb(pt, ct);//, 0, ctr);
}
);
$("button").prop("disabled", false);
showMsg("Benchmark completed!");
}
function initVars2(){
try{
sz_pt = parseInt($("#t_sz_pt").val());
sz_framebuffer = parseInt($("#t_sz_framebuffer").val());
// gctr = generateCTRSeq(iv, sz_pt);
return true;
} catch(e) {
return false;
}
}
function benchmark2(title, setkey, encrypt){
if(!initVars2())
return;
var randomtext = new Uint8Array(sz_pt);
var ciphertext = new Uint8Array(sz_pt);
var sjclct = new Uint8Array(sz_pt);
var key = new Array(16);
// generate random values for the plaintext
for(var i = 0; i < randomtext.length; i++)
randomtext[i] = parseInt(Math.floor(Math.random() * 256));
// generate values for the key
// the size of the key is 16 bytes
for(var i = 0; i < key.length; i++)
key[i] = i * 0x11;
// glaes.setKey(key);
setkey(key);
sjclaes = new sjcl.cipher.aes(bytesToWords(key));
var ts = new Date();
for(var i = 0; i < randomtext.length; i += sz_framebuffer){
encrypt(randomtext.subarray(i, i + sz_framebuffer), ciphertext.subarray(i, i + sz_framebuffer));
}
var te = new Date();
var sjclpt = bytesToWords(randomtext);
for(var i = 0; i < sjclpt.length; i += 4){
sjclct.set(wordsToBytes(sjclaes.encrypt(sjclpt.slice(i, i + 4))), i * 4);
}
// compare the result
for(var i = 0; i < randomtext.length; i++){
if(sjclct[i] != ciphertext[i]){
showErr("The result is incorrect!");
break;
}
}
// output the result
var p = document.createElement("p");
var h3 = document.createElement("h3");
h3.innerHTML = title;
p.appendChild(h3);
addResult(p);
var p1 = document.createElement("p");
p.appendChild(p1);
p1.innerHTML = "Encrypted " + sz_pt + " bytes with framebuffer size " + sz_framebuffer + " in " + (te - ts) + "ms, throughput is " + (sz_pt / (te - ts)).toFixed(c_fp) + "KB/s";
p.appendChild(document.createElement("hr"));
}
//]]>
</script>
</head>
<body onload="init()">
<div>
<div id="configuration" style="font-weight:bold; color:green;"></div>
<div id="err" style='font-weight: bold;color: red;'></div>
<div id="info" style='font-weight: bold;color: blue;'></div>
<div>
<h3>
Timing the encryption of given size of plaintext for
given times
</h3>
<p>
Plantext Size: <input id="t_sz_blk" type="text" value=
"1048576" /> Count: <input id="t_times" type="text"
value="3" />
</p>
<p>
<button onclick='runSJCL(this)'>Run SJCL-AES</button>
<button id='btn_glaes' onclick='runGLAES(this)'>Run
GLAES</button>
</p>
<hr />
</div>
<div>
<h3>
Timing the encryption of given size of plaintext for
different framebuffer size
</h3>
<p>
Plantext Size: <input id="t_sz_pt" type="text" value=
"1048576" /> Framebuffer Size: <input id=
"t_sz_framebuffer" type="text" value="65536" />
</p>
<p>
<button onclick="runGLAES2(this)">Run</button>
</p>
<hr />
</div>
<div id="result"></div>
</div>
</body>
</html>