Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NerdSHA256Plus blockheader format #513

Open
invpe opened this issue Oct 25, 2024 · 0 comments
Open

NerdSHA256Plus blockheader format #513

invpe opened this issue Oct 25, 2024 · 0 comments

Comments

@invpe
Copy link

invpe commented Oct 25, 2024

What's the required blockheader format for NerdSHA256Plus ?

When given the block header in LE format (as it comes from the pool), with reversed merkle (post double sha256) to keep it LE

  std::string blockheader = "20000000dec6d0b18efc502ceb1c4df64793bbbcb291a6450002ac290000000000000000fbc46586cc5fc60ed3a55b2572abb4c4096f79b76878cc7c59f6d5ebb0d4e228671aa7731702f12800000000";
  to_byte_array(blockheader.c_str(), 80 * 2, u8BlockHeaderBytes);

BLOCKHEADER: 20000000dec6d0b18efc502ceb1c4df64793bbbcb291a6450002ac290000000000000000fbc46586cc5fc60ed3a55b2572abb4c4096f79b76878cc7c59f6d5ebb0d4e228671aa7731702f12800000000

Then passed through the hashing

nerdSHA256_context midstate0;   
  nerd_mids(&midstate0, u8BlockHeaderBytes);
  unsigned char *pHeader64 = u8BlockHeaderBytes + 64;
  nerd_sha256d(&midstate0, pHeader64, block_hash0);
  Serial.printf("NERDPLUS: ");
  for (size_t x = 0; x < sizeof(block_hash0); x++) {
    Serial.printf("%02x", block_hash0[x]);
  }
  Serial.println("");

The output returned by NerdSHA256Plus is

NERDPLUS: a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5|d34efd79

Where the NerdSHA256 (no plus) implementation returns correct results:

NERD: f44e3665ba9047ecf8259e0c598821f60df77aa1a5b680145f810644d34efd79
SHA256D: f44e3665ba9047ecf8259e0c598821f60df77aa1a5b680145f810644d34efd79
SHA256MID: f44e3665ba9047ecf8259e0c598821f60df77aa1a5b680145f810644d34efd79

Is there any expectation on the endianness for NerdSHA256Plus?
I noticed the NerdMiner reversing all blockheader fields coming from the pool but not merkle (post hashing its BE)

// reverse version
    uint8_t buff;
    size_t bword, bsize, boffset;
    boffset = 0;
    bsize = 4;
    for (size_t j = boffset; j < boffset + (bsize/2); j++) {
        buff = mMiner.bytearray_blockheader[j];
        mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j];
        mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
    }

    // reverse prev hash (4-byte word swap)
    boffset = 4;
    bword = 4;
    bsize = 32;
    for (size_t i = 1; i <= bsize / bword; i++) {
        for (size_t j = boffset; j < boffset + bword / 2; j++) {
            buff = mMiner.bytearray_blockheader[j];
            mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j];
            mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j] = buff;
        }
        boffset += bword;
    }

/*
    // reverse merkle (4-byte word swap)
    boffset = 36;
    bword = 4;
    bsize = 32;
    for (size_t i = 1; i <= bsize / bword; i++) {
        for (size_t j = boffset; j < boffset + bword / 2; j++) {
            buff = mMiner.bytearray_blockheader[j];
            mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j];
            mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j] = buff;
        }
        boffset += bword;
    }
*/
    // reverse ntime
    boffset = 68;
    bsize = 4;
    for (size_t j = boffset; j < boffset + (bsize/2); j++) {
        buff = mMiner.bytearray_blockheader[j];
        mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j];
        mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
    }

    // reverse difficulty
    boffset = 72;
    bsize = 4;
    for (size_t j = boffset; j < boffset + (bsize/2); j++) {
        buff = mMiner.bytearray_blockheader[j];
        mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j];
        mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant