-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e5ebc5
commit 5a23fa2
Showing
6 changed files
with
99 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import hashlib | ||
import os | ||
|
||
|
||
def leading_zeros(num): | ||
if num == 0: | ||
return 8 | ||
|
||
leading_zeros = 0 | ||
while num & 0b10000000 == 0: | ||
leading_zeros += 1 | ||
num = num << 1 | ||
num = num & 0b11111111 | ||
return leading_zeros | ||
|
||
|
||
def total_leading_zeros(hash): | ||
to_return = 0 | ||
for byte in hash: | ||
l_zeros = leading_zeros(byte) | ||
to_return += l_zeros | ||
if l_zeros < 8: | ||
break | ||
|
||
return to_return | ||
|
||
|
||
def gen(hash, difficulty): | ||
difficulty = total_leading_zeros(difficulty) | ||
for i in range(1000): | ||
pow = b'' + os.urandom(10) | ||
hasher = hashlib.sha256() | ||
hasher.update(hash) | ||
hasher.update(pow) | ||
|
||
generated_hash = hasher.digest() | ||
ghash_leadin_zeros = total_leading_zeros(generated_hash) | ||
|
||
if ghash_leadin_zeros >= difficulty: | ||
print(pow, True) | ||
else: | ||
print(pow, False) | ||
|
||
|
||
gen(hashlib.sha256(b'text').digest(), | ||
b'\x0F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
use blockchaintree::tools; | ||
|
||
#[test] | ||
fn pow_test() {} |