I'm writing functions for the proof of work algorithm in blockchain. As per this algorithm, I have to take two pieces of data (actual data - a dictionary and a nonce value - an integer) and hash them, and then verify whether the same information hashed again gives the same hash value. So basically I'm just verifying the 'determinism' of the hashlib. The same information should give the same hash.
FUNCTION 1: Here's the function that creates the nonce:
def findnonce(data):
start = time.perf_counter()
TARGETVALUE_FOR_POW = 2**260
nonce = 0
sha = hasher.sha256()
sha.update((str(data)+str(nonce)).encode('utf-8'))
actualValue = int(sha.hexdigest(), 16)
while actualValue > TARGETVALUE_FOR_POW:
nonce += 1
sha.update((str(data)+str(nonce)).encode('utf-8'))
actualValue = int(sha.hexdigest(), 16)
nonce +=45 #just adding 45 since the 'solving' will always get finished at 0
#(imp point about this below!)
print(f"Internal hash value: {sha.hexdigest()} | vb type: {type(sha.hexdigest())} \n")
print(f"internal nonce 1: {nonce} | vb type: {type(nonce)} \n")
printbinary(str(data))
printbinary(str(nonce))
return(sha.hexdigest(), nonce, round(time.perf_counter()-start,4))
FUNCTION 2: Here's the function that verfies by just doing the calculation again.
def verifynonce(nonce,data):
sha = hasher.sha256()
sha.update((str(data)+str(nonce)).encode('utf-8'))
print(f"Internal nonce 2: {nonce} | vb type: {type(nonce)} \n")
print(f"Calculated hash value: {sha.hexdigest()} | vb type: {type(sha.hexdigest())} \n")
printbinary(str(data))
printbinary(str(nonce))
return True
FUNCTION 3: A print binary function to print the string in binary to compare in terminal.
def printbinary(value):
print(f"this value in binary is:")
arr = bytes(value,'utf-8')
for byte in arr:
print(byte,end=" ")
print()
Finally calling the functions as...
import time
import hashlib as hasher
data = {1:"hello", 2:"world"}
hashvalue, noncevalue, performance = findnonce(data)
verifynonce(noncevalue,data)
PROBLEM: When the the hashing happens in the first function with data and nonce, the final hash value is different from the final hash value in the verify nonce function. I need the two hash values to be the same, since the point of the algorithm is to verfiy is a node reporting the 'correct' nonce. So I am I am banking on the 'determinism' of the sha256 module. That is should give the same output for the same input. Which isn't happening here.
OUTPUT VALUES(TERMINAL-MAC OSX):
Internal hash value: e1de3abe7fb33a8719ef5018499c8db86c28bd72b6669d9bf71ea2b912dac776 | vb type: <class 'str'>
internal nonce 1: 45 | vb type: <class 'int'>
this value in binary
123 49 58 32 39 104 101 108 108 111 39 44 32 50 58 32 39 119 111 114 108 100 39 125
this value in binary
52 53
Internal nonce 2: 45 | vb type: <class 'int'>
Calculated hash value: b23be7014dbff4a62bf14eb558151aa70b73f41e85623e6847fe00aa10e16dff | vb type: <class 'str'>
this value in binary
123 49 58 32 39 104 101 108 108 111 39 44 32 50 58 32 39 119 111 114 108 100 39 125
this value in binary
52 53
The binary byte version is the same for data and nonce. And it should be. All I'm doing is creating two variables data and nonce, passing them same variables to two different functions, and asking them to calculate the hash value. Yet the final hash value calculated in both functions is different.
IMP POINT!: This problem doesn't happen if the nonce+=45 line is commented out. Then the nonce is 0 (integer 0) and for a value of 0 for the nonce, the hashes generated are the same! So just for nonce = 0, it gives the same hash value.
I have a strong feeling that I'm making a very stupid mistake somewhere. Maybe somewhere the nonce or data is changing and I'm not able to catch that point. Any help is appreciated. Thanks.