Newdude
Newdude

Reputation: 25

understanding SHA 256

import hashlib
previous_proof = 0
new_proof = 0
check_proof = False

while check_proof is False:
  hash_operation = hashlib.sha256(str(new_proof**2 - previous_proof**2).encode()).hexdigest()
  if hash_operation[:4] == '0000':
      check_proof = True
  else:
    new_proof+=1
print(hash_operation)
print(   new_proof)

I made this script for finding a string in sha256 that starts with '0000'. Could anyone explain me why if i put '000000'instead of '0000' it takes more time to find the string? It is supposed to take less time because i have more zeros at the beginning,right?

Upvotes: 1

Views: 377

Answers (1)

user2864740
user2864740

Reputation: 61975

There are far less hashes that start with '000000' compared to '0000' as this is a restriction of allowed starts.

With/assuming uniform distribution, that is 256 (2^8) times less: so taking ~256 times as long seems reasonable.

Consider that for every '0000' 4-character start there are also '000000'-'0000FF' 6-character starts, where only 1 ('00') out of the range of 256 ('00'-'FF') possibilities of characters in positions 5 & 6 match.

              Matches 4?   Matches 6?
0000 00       Yes          Yes
0000 01       Yes          No
0000 10       Yes          No
0000 11       Yes          No
(many more)   Yes          No

Upvotes: 1

Related Questions