-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
279 lines (219 loc) · 8.47 KB
/
test.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
// get the curve and crypto functions
var bootstrap = require('./util/nodejs.bootstrap.js');
var crypto = bootstrap.crypto;
var curve25519 = bootstrap.curve25519;
var converters = bootstrap.converters;
var output = require('./util/output');
function extractPhraseFromLine(line) {
return line.split(' ')[1];
}
function extractBytesFromLine(line) {
var bytes = [];
var parts = line.split(' ');
for (var i = 1; i < parts.length - 1; ++i) {
var byte = parts[i] & 0xFF;
if (0 != (byte & 0x80))
byte |= 0xFFFFFF00;
bytes.push(parseInt(parts[i], 16) & 0xFF);
}
return bytes;
}
function extractHexStringFromLine(line) {
return line.split(' ').slice(1).join('').toLowerCase();
}
function areEqual(bytes1, bytes2) {
if (bytes1.length !== bytes2.length)
return false;
for (var i = 0; i < bytes1.length; ++i) {
if (bytes1[i] !== bytes2[i])
return false;
}
return true;
}
function measureActionTime(action, denom) {
var start = process.hrtime();
action();
var diff = process.hrtime(start);
var elapsed = diff[0] * 1e3 + diff[1] / 1e6;
console.log('TOT: ' + elapsed.toFixed(3) + 'ms ');
console.log('AVG: ' + (elapsed / denom).toFixed(3) + 'ms ');
console.log('');
}
function loadTestCases(inputFile, testCaseParameters, runTestCase) {
var fs = require('fs');
fs.readFile(inputFile, 'utf8', function (err, data) {
if (err)
return console.log(err);
console.log('Running test cases from ' + inputFile);
var linesPerTest = testCaseParameters.length;
var lines = data.split('\n');
var numTestCases = (lines.length / linesPerTest) | 0;
console.log('test cases: ' + numTestCases + ' (lines: ' + lines.length + ')');
var testCases = [];
for (var i = 0; i < numTestCases; ++i) {
var startLine = i * linesPerTest;
var testCase = {};
for (var j = 0; j < linesPerTest; ++j) {
var parameter = testCaseParameters[j];
var converter = extractBytesFromLine;
if (parameter.type === 'string')
converter = extractPhraseFromLine;
if (parameter.type == 'hexstring')
converter = extractHexStringFromLine;
testCase[parameter.name || parameter] = converter(lines[startLine + j]);
}
testCases.push(testCase);
}
measureActionTime(function () {
var numFailed = 0;
var numPassed = 0;
for (var i = 0; i < testCases.length; ++i) {
var testCase = testCases[i];
if (runTestCase(testCase)) {
++numPassed;
} else {
++numFailed;
console.log('^' + i + ': failed :(');
}
}
console.log(numFailed + ' failures / ' + numPassed + ' passed');
}, testCases.length);
});
}
function runSignTest() {
loadTestCases('./data/signtest.dat', ['v', 'h', 'x', 's'], function (testCase) {
var v = curve25519.sign(testCase.h, testCase.x, testCase.s);
if (!areEqual(v, testCase.v)) {
output.logBytes('E', testCase.v);
output.logBytes('A', v);
return false;
}
return true;
});
}
function runKeygenTest() {
loadTestCases('./data/keygentest.dat', ['k1', 'p', 's', 'k2'], function (testCase) {
var result = curve25519.keygen(testCase.k1);
var pairs = [
{ a: result.p, e: testCase.p, name: 'p' },
{ a: result.s, e: testCase.s, name: 's' },
{ a: result.k, e: testCase.k2, name: 'k' }
];
var hasFailure = false;
for (var j = 0; j < pairs.length; ++j) {
var pair = pairs[j];
if (areEqual(pair.e, pair.a))
continue;
hasFailure = true;
output.logBytes('E(' + pair.name + ')', pair.e);
output.logBytes('A(' + pair.name + ')', pair.a);
}
return !hasFailure;
});
}
function runVerifyTest () {
loadTestCases('./data/verifytest.dat', ['y', 'v', 'h', 'p'], function (testCase) {
var y = curve25519.verify(testCase.v, testCase.h, testCase.p);
if (!areEqual(y, testCase.y)) {
output.logBytes('E', testCase.y);
output.logBytes('A', y);
return false;
}
return true;
});
}
function runCryptoPublicKeyTest () {
loadTestCases('./data/cryptopublickeytest.dat', [{ name: 'p', type: 'string' }, { name: 'k', type: 'hexstring' }], function (testCase) {
var phraseAsHexString = converters.stringToHexString(testCase.p);
var k = crypto.getPublicKey(phraseAsHexString);
if (!areEqual(k, testCase.k)) {
console.log('E: ' + testCase.k);
console.log('A: ' + k);
return false;
}
return true;
});
}
function runCryptoSignTest () {
loadTestCases('./data/cryptosigntest.dat', [{ name: 'p', type: 'string' }, 'm', { name: 's', type: 'hexstring' }], function (testCase) {
var messageAsHexString = converters.byteArrayToHexString(testCase.m);
var phraseAsHexString = converters.stringToHexString(testCase.p);
var s = crypto.sign(messageAsHexString, phraseAsHexString);
if (!areEqual(s, testCase.s)) {
console.log('E: ' + testCase.s);
console.log('A: ' + s);
return false;
}
return true;
});
}
function runCryptoVerifyTest () {
console.log('Running verification test cases ...');
var secrets = ['alpha', 'gamma', 'zeta'];
var messages = ['seahawks', 'saints', 'patriots', 'colts'];
console.log('generating public keys (' + secrets.length + ') ...');
var keys = [];
for (var i = 0; i < secrets.length; ++i)
keys.push(crypto.getPublicKey(converters.stringToHexString(secrets[i])));
function foreachSecretAndMessage (callback) {
for (var i = 0; i < secrets.length; ++i) {
for (var j = 0; j < messages.length; ++j)
callback(secrets[i], keys[i], messages[j]);
}
}
console.log('generating signatures (' + messages.length + ') ...');
var sigs = {};
foreachSecretAndMessage(function (secret, key, message) {
var sig = crypto.sign(
converters.stringToHexString(message),
converters.stringToHexString(secret));
sigs[secret + '.' + message] = sig;
});
console.log('verifying ...');
measureActionTime(function () {
var numPassed = 0;
var numFailed = 0;
foreachSecretAndMessage(function (secret, key, message) {
var id = secret + '.' + message;
for (var i in sigs) {
var result = crypto.verify(
sigs[i],
converters.stringToHexString(message),
key);
if (result == (id == i)) {
++numPassed;
continue;
}
++numFailed;
console.log('E: ' + (id == i));
console.log('A: ' + result);
console.log('^[' + secret + ', ' + message + ', ' + id + '] :(');
}
});
console.log(numFailed + ' failures / ' + numPassed + ' passed');
}, secrets.length * messages.length * secrets.length * messages.length);
}
function runCryptoApiUsageTest () {
console.log('*** Crypto API Usage Example ***');
var nxtCrypto = crypto;
var message = '54686973206973206120736563726574206d6573736167652074686174206e6565647320746f206265207369676e6564';
var secretPhrase = '54686973206973206d7920766572792073656372657420706872617365';
var publicKey = nxtCrypto.getPublicKey(secretPhrase);
console.log(publicKey); // 698168d8669c9310d68101dfcc974ed4
// ef454692da6f028f68114db5fdcc4f6a
var signature = nxtCrypto.sign(message, secretPhrase);
console.log(signature); // 94956bf3de7cfdedb2562a0eff698fed
// 7f3e54bbf4476fbb23a192ddea04040f
// 68efa5d03c3f9ebec4109401b50433f1
// df267299d8b1ad2c485046c45e6b38da
var isVerified = nxtCrypto.verify(signature, message, publicKey)
console.log(isVerified); // true
console.log();
}
runCryptoApiUsageTest();
runSignTest();
runKeygenTest();
runVerifyTest();
runCryptoPublicKeyTest();
runCryptoSignTest()
runCryptoVerifyTest();