1

I'm trying to calculate the hash result for the "light" implementation of the Etash algorithm from ethereum/ethash/js but I can't seem to get the correct result.

The expected result for comparison was calculated with both ethereumj and ethashjs.

My code for the Javascript implementation:

"use strict";

var ethash = require('./ethash');
var util = require('./util');

// init params
var ethashParams = ethash.defaultParams();
ethashParams.cacheSize = 16776896 //cachesize for epoc 0
ethashParams.dagSize = 1073739904 //fullsize for epoc 0

// create hasher
var seed = Buffer.alloc(32).fill(0); //epoc 0
var hasher = new ethash.Ethash(ethashParams, seed);

console.log('Ethash cache hash: ' + util.bytesToHexString(hasher.cacheDigest()));
// expected: 35ded12eecf2ce2e8da2e15c06d463aae9b84cb2530a00b932e4bbc484cde353 --> OK

        
var header = util.hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
var nonce = util.hexStringToBytes("0000000000000000");

var hash = hasher.hash(header, nonce);
console.log("Ethash hash result: " + util.bytesToHexString(hash));
// expected: 89eaac0ac621d2bb2d1a62455119cb0d0a0883d554e748bab23c2066da080916 --> OK


header = util.hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
nonce = util.hexStringToBytes("6c18bd55f3d77ab5");

hash = hasher.hash(header, nonce);
console.log("Ethash hash result: " + util.bytesToHexString(hash));
// expected: 0a9f8a9d1954c796b8bbafe0d443f1586c17ffe995070ea5a40436946c1bf2d7 --> NOT OK

Output:

$ node ./post.js 
Ethash cache hash: 35ded12eecf2ce2e8da2e15c06d463aae9b84cb2530a00b932e4bbc484cde353
Ethash hash result: 89eaac0ac621d2bb2d1a62455119cb0d0a0883d554e748bab23c2066da080916
Ethash hash result: 72d628f1bb9e7229f43c210824975d179f6b55da47ebe4655b4a55d677697391

So it seems that the cache is calculated correctly (the hash matches that of the other implementations). The hash result for a nonce = '0x0000000000000000' also matches, but a random other nonce calculates a result that does not match. I assume this has something to do with the byte order of the nonce... any ideas how the nonce should be specified for the Javascript implementation to get a correct hash result?

1

Well, the solution is simple. I was correct assuming it has something to do with the byte order. Reversing the nonce before passing it to the hash-function did the trick:

header = util.hexStringToBytes("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
nonce = util.hexStringToBytes("6c18bd55f3d77ab5").reverse();

hash = hasher.hash(header, nonce);
console.log("Ethash hash result: " + util.bytesToHexString(hash));
// expected: 0a9f8a9d1954c796b8bbafe0d443f1586c17ffe995070ea5a40436946c1bf2d7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.