-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaxentro-argon2id.js
65 lines (50 loc) · 1.48 KB
/
axentro-argon2id.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
const argon2 = require('argon2');
function bnToHex(bn) {
bn = BigInt(bn);
var pos = true;
if (bn < 0) {
pos = false;
bn = bitnot(bn);
}
var hex = bn.toString(16);
if (hex.length % 2) { hex = '0' + hex; }
// if (pos && (0x80 & parseInt(hex.slice(0, 2), 16))) {
// hex = '00' + hex;
// }
return hex;
}
function toBits(byte) {
var bits = [];
for(var x=7; x>=0; x--){
bits.push((byte&Math.pow(2,x))>>x);
}
return bits;
}
async function validateNonce(block_hash, block_nonce, difficulty) {
nonce = bnToHex(block_nonce);
const salt = Buffer.from(nonce);
const hash = await argon2.hash(block_hash, {
type: argon2.argon2id,
hashLength: 512,
timeCost: 1,
memoryCost: 65536,
parallelism: 1,
raw: true,
salt
});
const bits = Array.from(hash).map(toBits).flat();
const leading_bits = bits.slice(0, difficulty).join("");
return leading_bits.split("1")[0].length;
}
async function f1() {
const difficulty = 7;
const block_hash = 'f60db23095ad40a0f5486e5e91e09d3475544dc3d346f4ea1d350f7708b86b0f';
while(true){
let nonce = Math.floor(Math.random() * 1000000000000);
const res = await validateNonce(block_hash, nonce, difficulty);
if(res === difficulty){
console.log('matched difficulty: ' + res + ' expected difficulty: ' + difficulty + ' with nonce: ' + nonce);
}
}
}
f1();