forked from cryptocoin-builders/foundation-kawpow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·90 lines (62 loc) · 2.58 KB
/
test.js
File metadata and controls
executable file
·90 lines (62 loc) · 2.58 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
90
'use strict'
const kawpow = require('./index.js');
process.title = 'verify-test';
const headerHashBuf = Buffer.from('63543d3913fe56e6720c5e61e8d208d05582875822628f483279a3e8d9c9a8b3', 'hex');
const nonceBuf = hexToLE('88a23b0033eb959b');
const blockHeight = 262523;
const expectedMixHash = '89732e5ff8711c32558a308fc4b8ee77416038a70995670e3eb84cbdead2e337';
const expectedHash = '0000000718ba5143286c46f44eee668fdf59b8eba810df21e4e2f4ec9538fc20';
// Hash data
const hashResult = hash(headerHashBuf, nonceBuf, blockHeight);
// Verify mix hash
verify(hashResult.mixHashBuf, hashResult.hash, 1000);
console.log('Test completed successfully.');
function hash(headerHashBuf, nonceBuf, blockHeight) {
const mixOutBuf = Buffer.alloc(32, 0);
const hashOutBuf = Buffer.alloc(32, 0);
kawpow.hashOne(headerHashBuf, nonceBuf, blockHeight, mixOutBuf, hashOutBuf);
const mixHash = mixOutBuf.toString('hex');
const hash = hashOutBuf.toString('hex');
console.log(`Mix Hash: ${mixHash}`);
console.log(`Expected: ${expectedMixHash}\n`);
console.log(`Hash: ${hash}`);
console.log(`Expected: ${expectedHash}\n`);
if (mixHash !== expectedMixHash)
throw new Error(`Got invalid mix hash. Expected ${expectedMixHash}`);
if (hash !== expectedHash)
throw new Error(`Got invalid hash. Expected ${expectedHash}`);
return {
mixHashBuf: mixOutBuf,
hash: hash
};
}
function verify(mixHashBuf, expectedHash, iterations) {
console.log(`Verifying with ${iterations} iterations...`);
const verifyHashOutBuf = Buffer.alloc(32);
const startTimeMs = Date.now();
for (let i = 0; i < iterations; i++) {
const isValid = kawpow.verify(headerHashBuf, nonceBuf, blockHeight, mixHashBuf, verifyHashOutBuf);
if (!isValid)
throw new Error('Verification failed.');
}
const endTimeMs = Date.now();
const verifiedHash = verifyHashOutBuf.toString('hex');
console.log(`Verified Hash: ${verifiedHash}`)
if (verifiedHash !== expectedHash)
throw new Error(`Verified hash output does not match original hash.`);
const verifyPs = iterations / (endTimeMs - startTimeMs) * 1000;
console.log(`verify/sec = ${verifyPs}\n`);
}
function hexToLE(hex) {
return reverseBytes(Buffer.from(hex, 'hex'));
}
function reverseBytes(buffer, output) {
output = output || buffer;
const halfLen = buffer.length / 2;
for (let i = 0; i < halfLen; i++) {
const byte = buffer[i];
output[i] = buffer[buffer.length - i - 1];
output[buffer.length - i - 1] = byte;
}
return output;
}