-
Notifications
You must be signed in to change notification settings - Fork 10
/
ristretto255.benchmarks.js
281 lines (267 loc) · 7.93 KB
/
ristretto255.benchmarks.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* global ristretto255 */
if (!Uint8Array.prototype.fill) {
// eslint-disable-next-line
Uint8Array.prototype.fill = Array.prototype.fill;
}
/* Set-up */
const t00 = performance.now();
const NUM_OF_REPS = 100;
const ristrettoECPoints = [];
const ristrettoSerializedPoints = [];
const hashes = [];
const scalars = [];
const h = ristretto255.unsafe.point.alloc();
/* Generate random ristretto points */
for (let i = 0; i < NUM_OF_REPS; i++) {
const ristrettoECPoint = ristretto255.unsafe.point.getRandom();
ristrettoECPoints.push(ristrettoECPoint);
ristrettoSerializedPoints.push(
ristretto255.unsafe.point.toBytes(ristrettoECPoint)
);
const hTemp = new Uint8Array(32);
for (let j = 0; j < 32; j++) {
hTemp[j] = i;
}
hashes.push(hTemp);
scalars.push(ristretto255.scalar.getRandom());
}
const functions = [
{
// ristretto points are represented as Uint8Array(32)
name: 'High-level ristretto255 group operations',
functions: [
{
name: 'getRandom',
description: 'Generate a random group element',
execute: () => ristretto255.getRandom()
},
{
name: 'isValid',
description: 'Check if a value represents a valid element',
execute: i => ristretto255.isValid(ristrettoSerializedPoints[i])
},
{
name: 'fromHash',
description:
'Hash to group: generate a group element from 64-element byte array, e.g. an output of SHA-512',
execute: i => ristretto255.fromHash(hashes[i])
},
{
name: 'add',
description: 'Add two group elements',
execute: i =>
ristretto255.add(
ristrettoSerializedPoints[i],
ristrettoSerializedPoints[(i + 1) % NUM_OF_REPS]
)
},
{
name: 'sub',
description: 'Subtract two group elements',
execute: i =>
ristretto255.sub(
ristrettoSerializedPoints[i],
ristrettoSerializedPoints[(i + 1) % NUM_OF_REPS]
)
},
{
name: 'scalarMultBase',
description: 'Multiply a generator of the group by a scalar',
execute: i => ristretto255.scalarMultBase(scalars[i])
},
{
name: 'scalarMult',
description: 'Multiply a group element by a scalar',
execute: i =>
ristretto255.scalarMult(scalars[i], ristrettoSerializedPoints[i])
}
]
},
{
name: 'Scalar operations',
functions: [
{
name: 'scalar.getRandom',
description: 'Generate a random scalar',
execute: () => ristretto255.scalar.getRandom()
},
{
name: 'scalar.invert',
description: 'Invert a scalar',
execute: i => ristretto255.scalar.invert(scalars[i])
},
{
name: 'scalar.negate',
description: 'Negate a scalar',
execute: i =>
ristretto255.scalar.negate(scalars[i], scalars[i % NUM_OF_REPS])
},
{
name: 'scalar.add',
description: 'Add two scalars',
execute: i =>
ristretto255.scalar.add(scalars[i], scalars[i % NUM_OF_REPS])
},
{
name: 'scalar.sub',
description: 'Subtract two scalars',
execute: i =>
ristretto255.scalar.sub(scalars[i], scalars[i % NUM_OF_REPS])
},
{
name: 'scalar.mul',
description: 'Multiply two scalars',
execute: i =>
ristretto255.scalar.mul(scalars[i], scalars[i % NUM_OF_REPS])
}
]
},
{
name: 'Low-level unsafe functions (unless if used by a cryptographer)',
functions: [
{
name: 'unsafe.point.toBytes',
description:
'Serialize a curve25519 point to ristretto255 group element',
execute: i => ristretto255.unsafe.point.toBytes(ristrettoECPoints[i])
},
{
name: 'unsafe.point.fromBytes',
description:
'Deserialize a curve25519 point from ristretto255 group element',
execute: i =>
ristretto255.unsafe.point.fromBytes(h, ristrettoSerializedPoints[i])
},
{
name: 'unsafe.point.getRandom',
description:
'Generate a random ristretto255 group element represented as curve25519 point',
execute: () => ristretto255.unsafe.point.getRandom()
},
{
name: 'unsafe.point.fromHash',
description:
'Generate a ristretto255 group element represented as curve25519 point from a 64 elements byte array such as an output of SHA512',
execute: i => ristretto255.unsafe.point.fromHash(hashes[i])
},
{
name: 'unsafe.point.add',
description: 'Add two curve25519 points',
execute: i =>
ristretto255.unsafe.point.add(
ristrettoECPoints[i],
ristrettoECPoints[i % NUM_OF_REPS]
)
},
{
name: 'unsafe.point.sub',
description: 'Subtract two curve25519 points',
execute: i =>
ristretto255.unsafe.point.sub(
ristrettoECPoints[i],
ristrettoECPoints[i % NUM_OF_REPS]
)
},
{
name: 'unsafe.point.scalarMultBase',
description: "Multiply a curve25519's base point by a scalar",
execute: i =>
ristretto255.unsafe.point.scalarMultBase(
ristrettoECPoints[i],
scalars[i]
)
},
{
name: 'unsafe.point.scalarMult',
description: "Multiply a curve25519's point by a scalar",
execute: i =>
ristretto255.unsafe.point.scalarMult(
ristrettoECPoints[i],
ristrettoECPoints[i % NUM_OF_REPS],
scalars[i]
)
}
]
}
];
const template = (groupName, results) => `
<h2>${groupName}</h2>
<div class="benchmarks">
<table class="benchmarks">
<tr>
<th>Function name</th>
<th>Time in ms</th>
<th>Comments</th>
</tr>
${results
.map(result => {
return `
<tr>
<td>${result.functionName}</td>
<td>${result.timing}</td>
<td>${result.description}</td>
</tr>
`;
})
.join('')}
</table>
</div>`;
function average(data) {
const sum = data.reduce(function add(acc, value) {
return acc + value;
}, 0);
const avg = sum / data.length;
return avg;
}
// The credit for computing std goes to
// https://derickbailey.com/2014/09/21/calculating-standard-deviation-with-array-map-and-array-reduce-in-javascript/
function standardDeviation(values) {
const avg = average(values);
const squareDiffs = values.map(function f(value) {
const diff = value - avg;
const sqrDiff = diff * diff;
return sqrDiff;
});
const stdDev = Math.sqrt(average(squareDiffs));
return stdDev;
}
const generateBenchmarks = () => {
functions.forEach(group => {
const results = group.functions.map(func => {
// const t0 = performance.now();
const timing = [];
for (let i = 0; i < NUM_OF_REPS; i++) {
const t0 = performance.now();
func.execute(i);
const t1 = performance.now();
timing.push(t1 - t0);
}
// compute the average
const avg = average(timing);
const std = standardDeviation(timing);
return {
functionName: func.name,
description: func.description,
timing: `${avg.toFixed(
2
)}<small font-size="smaller"> ± ${std.toFixed(2)}</small>`
};
});
document.getElementById('container').innerHTML += template(
group.name,
results
);
});
};
generateBenchmarks();
const t01 = performance.now();
document.getElementById('total_time').innerHTML = `Benchmark runtime: ${(
(t01 - t00) /
1000
).toFixed(2)} sec with ${NUM_OF_REPS} reps on each operation.`;