-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
196 lines (156 loc) · 6.54 KB
/
index.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
const mtp = require('bindings')('hashermtp.node');
const MTP_L = 64;
const ZCOIN_INPUT_SIZE = 80;
const NONCE_SIZE = 4;
const TARGET_SIZE = 32;
const MTP_HASH_ROOT_SIZE = 16;
const MTP_BLOCK_SIZE = 8 * MTP_L * 2 * 128;
const MAX_PROOF_SIZE = MTP_L * 3 * 353;
const MTP_HASH_VALUE_SIZE = 32;
const HASH_OUTPUT_BUFFER = Buffer.alloc(4 + NONCE_SIZE + MTP_HASH_VALUE_SIZE + MTP_HASH_ROOT_SIZE + MTP_BLOCK_SIZE + MAX_PROOF_SIZE);
/**
* Solve the hash problem. This function will try different nonce until it finds one such that the
* computed hash is less than the `target` difficulty.
*
* @param mtpInput {Buffer} 80-byte header data to hash
* @param target {Buffer} 32-byte target
* @param nonceStart {Buffer} 4-byte nonce to start with.
* @param nonceEnd {Buffer} 4-byte nonce to stop at.
*
* @returns {boolean|object} False if a nonce was not found, otherwise an object containing nonce and MTP proofs
* in Buffers.
* @returns {{
* nonce: {Buffer},
* hashValue: {Buffer},
* hashRoot: {Buffer},
* block: {Buffer},
* proof: {Buffer}
* }}
*/
module.exports.hash = hash;
/**
* This function will hash the MTP input using the supplied nonce value.
*
* @param mtpInput {Buffer} 80-byte header data to hash
* @param nonce {Buffer} 4-byte nonce.
*
* @returns {boolean|object} False if hashing failed, otherwise an object containing nonce and MTP proofs
* in Buffers.
* @returns {{
* nonce: {Buffer},
* hashValue: {Buffer},
* hashRoot: {Buffer},
* block: {Buffer},
* proof: {Buffer}
* }}
*/
module.exports.hashOne = hashOne;
/**
* Verify the given MTP proofs are valid and get hash result.
*
* @param mtpHeader {Buffer} 80-byte header that was hashed.
* @param nonce {Buffer} 4-byte nonce to check.
* @param hashRoot {Buffer} 16-byte MTP hash root used for verification.
* @param block {Buffer} MTP block data used for verification.
* @param proof {Buffer} NTP proof data used for verification.
* @param hashValueOut {Buffer} A 32-byte buffer to put the hash result into.
*
* @returns {boolean} True if verification is successful, otherwise false.
*/
module.exports.verify = verify;
/**
* Verify the given MTP proofs are valid and get hash result. Use to quickly find invalid proofs. In some cases may
* pass an invalid proof.
*
* "verifyFast" is already used by "verify" before doing a full verify of the proofs.
*
* @param mtpHeader {Buffer} 80-byte header that was hashed.
* @param nonce {Buffer} 4-byte nonce to check.
* @param hashRoot {Buffer} 16-byte MTP hash root used for verification.
* @param block {Buffer} MTP block data used for verification.
* @param proof {Buffer} NTP proof data used for verification.
* @param hashValueOut {Buffer} A 32-byte buffer to put the hash result into.
*
* @returns {boolean} True if verification is successful, otherwise false.
*/
module.exports.verifyFast = verifyFast;
function hash(mtpInput, target, nonceStart, nonceEnd) {
_expectBuffer(mtpInput, 'mtpInput', ZCOIN_INPUT_SIZE);
_expectBuffer(target, 'target', TARGET_SIZE);
_expectBuffer(nonceStart, 'nonceStart', NONCE_SIZE);
_expectBuffer(nonceEnd, 'nonceEnd', NONCE_SIZE);
const isSuccess = mtp.hash(mtpInput, target, nonceStart, nonceEnd, HASH_OUTPUT_BUFFER);
if (!isSuccess)
return false;
const size = HASH_OUTPUT_BUFFER.readUInt32LE(0);
const proofSize = size - NONCE_SIZE - MTP_HASH_VALUE_SIZE - MTP_HASH_ROOT_SIZE - MTP_BLOCK_SIZE;
const buffers = {
nonce: Buffer.alloc(NONCE_SIZE),
hashValue: Buffer.alloc(MTP_HASH_VALUE_SIZE),
hashRoot: Buffer.alloc(MTP_HASH_ROOT_SIZE),
block: Buffer.alloc(MTP_BLOCK_SIZE),
proof: Buffer.alloc(proofSize)
};
let pos = 4/*output size*/;
HASH_OUTPUT_BUFFER.copy(buffers.nonce, 0, pos);
pos += NONCE_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.hashValue, 0, pos);
pos += MTP_HASH_VALUE_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.hashRoot, 0, pos);
pos += MTP_HASH_ROOT_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.block, 0, pos);
pos += MTP_BLOCK_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.proof, 0, pos);
return buffers;
}
function hashOne(mtpInput, nonce) {
_expectBuffer(mtpInput, 'mtpInput', ZCOIN_INPUT_SIZE);
_expectBuffer(nonce, 'nonce', NONCE_SIZE);
const isSuccess = mtp.hash_one(mtpInput, nonce, HASH_OUTPUT_BUFFER);
if (!isSuccess)
return false;
const size = HASH_OUTPUT_BUFFER.readUInt32LE(0);
const proofSize = size - NONCE_SIZE - MTP_HASH_VALUE_SIZE - MTP_HASH_ROOT_SIZE - MTP_BLOCK_SIZE;
const buffers = {
nonce: Buffer.alloc(NONCE_SIZE),
hashValue: Buffer.alloc(MTP_HASH_VALUE_SIZE),
hashRoot: Buffer.alloc(MTP_HASH_ROOT_SIZE),
block: Buffer.alloc(MTP_BLOCK_SIZE),
proof: Buffer.alloc(proofSize)
};
let pos = 4/*output size*/;
HASH_OUTPUT_BUFFER.copy(buffers.nonce, 0, pos);
pos += NONCE_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.hashValue, 0, pos);
pos += MTP_HASH_VALUE_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.hashRoot, 0, pos);
pos += MTP_HASH_ROOT_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.block, 0, pos);
pos += MTP_BLOCK_SIZE;
HASH_OUTPUT_BUFFER.copy(buffers.proof, 0, pos);
return buffers;
}
function verify(mtpHeader, nonce, hashRoot, block, proof, hashValueOut) {
_expectBuffer(mtpHeader, 'mtpHeader', ZCOIN_INPUT_SIZE);
_expectBuffer(nonce, 'nonce', NONCE_SIZE);
_expectBuffer(hashRoot, 'hashRoot', MTP_HASH_ROOT_SIZE);
_expectBuffer(block, 'block', MTP_BLOCK_SIZE);
_expectBuffer(proof, 'proof');
_expectBuffer(hashValueOut, 'hashValueOut', MTP_HASH_VALUE_SIZE);
return mtp.verify(mtpHeader, nonce, hashRoot, block, proof, hashValueOut);
}
function verifyFast(mtpHeader, nonce, hashRoot, block, proof, hashValueOut) {
_expectBuffer(mtpHeader, 'mtpHeader', ZCOIN_INPUT_SIZE);
_expectBuffer(nonce, 'nonce', NONCE_SIZE);
_expectBuffer(hashRoot, 'hashRoot', MTP_HASH_ROOT_SIZE);
_expectBuffer(block, 'block', MTP_BLOCK_SIZE);
_expectBuffer(proof, 'proof');
_expectBuffer(hashValueOut, 'hashValueOut', MTP_HASH_VALUE_SIZE);
return mtp.verify_fast(mtpHeader, nonce, hashRoot, block, proof, hashValueOut);
}
function _expectBuffer(buffer, name, size) {
if (!Buffer.isBuffer(buffer))
throw new Error(`"${name}" is expected to be a Buffer. Got ${(typeof buffer)} instead.`);
if (size && buffer.length !== size)
throw new Error(`"${name}" is expected to be exactly ${size} bytes. Got ${buffer.length} instead.`);
}