Bitcoin Forum
June 24, 2024, 03:48:44 PM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Nonce too short - Please help  (Read 905 times)
Rambo123 (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
September 27, 2015, 02:51:57 PM
Last edit: September 27, 2015, 03:05:48 PM by Rambo123
 #1

Hello,

I have the following problem:

Sometimes, the nonce of a block is shorter than 8 Bytes and I don't know how to handel that case.
For example please take a look at this block:

https://blockexplorer.com/block/000000000000000007a9aef4df0b3782c31b713ebc573685cdce376d7308e5b0

The Nonce of the block is: 70755497
Converted to hex it is: 4a7a43 ---> 6 Bytes instead of 8

Other example (previous block of the block above):

https://blockexplorer.com/block/0000000000000000051073be739add17c258ad9f60b6a2cd4bd9616d13163be6

Nonce: 2735789883
Hex: 3bd710a3 --> !8! Bytes



If I now try to double SHA this kind of block with only 6 Bytes for the nonce I don't get a correct result for the block hash.
If've tried to put "00" bevor and behind the digits, but that doesn't solve the problem.

Does anyone know how to fix this?
Thank you a lot Smiley

Sincerely,
Rambo123

DannyHamilton
Legendary
*
Offline Offline

Activity: 3430
Merit: 4680



View Profile
September 27, 2015, 07:32:55 PM
Last edit: September 27, 2015, 07:43:05 PM by DannyHamilton
 #2

The Nonce of the block is: 70755497
Converted to hex it is: 4a7a43 ---> 6 Bytes instead of 8

One of us seems to have made an error in our math.

When I convert the decimal number 70755497 to hex in big-endian byte order, I get 0x0437A4A9

If I convert that to little-endian byte order, I get: A9A43704

Did I make the mistake, or did you?

Lets see...

0 * 167 = 0
4 * 166 = 67108864
3 * 165 = 3145728
7 * 164 = 458752
10 * 163 = 40960
4 * 162 = 1024
10 * 161 = 160
9 * 160 = 9

67108864 + 3145728 + 458752 + 40960 + 1024 + 160 + 9 = 70755497

By the way, the nonce is only 4 bytes, not 8.  Each byte is represented with two characters when encoded in hexadecimal.

Example of 1 byte in multiple representations:

Ascii representation (1 character):
z

Hexadecimal representation (2 characters):
7A

Decimal representation (3 characters):
122

Octal representation (3 characters):
172

Binary representation (8 characters):
1111010

teukon
Legendary
*
Offline Offline

Activity: 1246
Merit: 1004



View Profile
September 27, 2015, 09:59:04 PM
Last edit: September 28, 2015, 05:13:44 AM by teukon
 #3

The Nonce of the block is: 70755497
Converted to hex it is: 4a7a43 ---> 6 Bytes instead of 8

Something's gone wobbly here.

The nonce of 70755497 is stored in the block header as "a9a43704" (4 bytes).  This is precisely the 4-byte big-endian little-endian representation of the nonce.

My quick Python 3 check:
Code:
>>> (70755497).to_bytes(4, byteorder='little')
b'\xa9\xa47\x04'
>>> ''.join(format(byte, '02x') for byte in _)
'a9a43704'

If I now try to double SHA this kind of block with only 6 Bytes for the nonce I don't get a correct result for the block hash.
If've tried to put "00" bevor and behind the digits, but that doesn't solve the problem.

In this big-endian little-endian form you'll get the correct block hash.
Code:
import hashlib

def sha256(message):
    return hashlib.sha256(message).digest()

version = '03000000'
previous_hash = 'e63b16136d61d94bcda2b6609fad58c217dd9a73be7310050000000000000000'
merkle_root = '213f7741f14eb9b972b44402647b20f1e2705c73416dfb35d5f0b6fca86eb8e9'
time = '098d0656'
bits = 'ba871218'
nonce = 'a9a43704'
header = ''.join([version, previous_hash, merkle_root, time, bits, nonce])
hash_bytes = sha256(sha256(bytes.fromhex(header)))
print(''.join(format(byte, '02x') for byte in reversed(hash_bytes)))
returns 000000000000000007a9aef4df0b3782c31b713ebc573685cdce376d7308e5b0.

Edit: Corrected big-endian to little-endian throughout.  Cheers Danny.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!