sha256test.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. import sys,os,hashlib
  19. from binascii import hexlify
  20. from mmgen.sha256 import Sha256
  21. random_rounds = int(sys.argv[1]) if len(sys.argv) == 2 else 500
  22. def msg(s): sys.stderr.write(s)
  23. def green(s): return '\033[32;1m' + s + '\033[0m'
  24. def compare_hashes(dlen,data):
  25. sha2 = hashlib.sha256(data).hexdigest().encode()
  26. # msg('Dlen {:<5} {}\r'.format(dlen,sha2))
  27. my_sha2 = Sha256(data).hexdigest()
  28. assert my_sha2 == sha2,'Hashes do not match!'
  29. def test_K():
  30. msg('Testing generated constants: ')
  31. K_ref = [1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998]
  32. def toSigned32(n): return ((n & 0xffffffff) ^ 0x80000000) - 0x80000000
  33. K_sig = [toSigned32(n) for n in Sha256.K]
  34. assert K_sig == K_ref,'Generated constants in K[] differ from reference value'
  35. msg('OK\n')
  36. def test_ref():
  37. inputs = (
  38. '','x','xa','the','the quick','the quick brown fox',
  39. '\x00','\x00\x00','\x00'*256,'\x00'*512,'\x00'*511,'\x00'*513,
  40. '\x0f','\x0f\x0f','\x0f'*256,'\x0f'*512,'\x0f'*511,'\x0f'*513,
  41. '\x0f\x0d','\x0e\x0e'*256,'\x00\x0f'*512,'\x0e\x0f'*511,'\x0a\x0d'*513
  42. )
  43. for i,data in enumerate(inputs):
  44. msg('\rTesting reference input data: {:4}/{} '.format(i+1,len(inputs)))
  45. compare_hashes(len(data),data.encode())
  46. msg('OK\n')
  47. def test_random(rounds):
  48. for i in range(rounds):
  49. if i+1 in (1,rounds) or not (i+1) % 10:
  50. msg('\rTesting random input data: {:4}/{} '.format(i+1,rounds))
  51. dlen = int(hexlify(os.urandom(4)),16) >> 18
  52. compare_hashes(dlen,os.urandom(dlen))
  53. msg('OK\n')
  54. msg(green('Testing MMGen implementation of Sha256()\n'))
  55. test_K()
  56. test_ref()
  57. test_random(random_rounds)