ut_bip39.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_bip39: BIP39 unit test for the MMGen suite
  4. """
  5. from mmgen.common import *
  6. from mmgen.exception import *
  7. class unit_test(object):
  8. vectors = (
  9. ( "00000000000000000000000000000000",
  10. "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
  11. ),
  12. ( "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f",
  13. "legal winner thank year wave sausage worth useful legal winner thank yellow"
  14. ),
  15. ( "80808080808080808080808080808080",
  16. "letter advice cage absurd amount doctor acoustic avoid letter advice cage above"
  17. ),
  18. ( "ffffffffffffffffffffffffffffffff",
  19. "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"
  20. ),
  21. ( "000000000000000000000000000000000000000000000000",
  22. "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon agent"
  23. ),
  24. ( "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f",
  25. "legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal will"
  26. ),
  27. ( "808080808080808080808080808080808080808080808080",
  28. "letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter always"
  29. ),
  30. ( "ffffffffffffffffffffffffffffffffffffffffffffffff",
  31. "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo when"
  32. ),
  33. ( "0000000000000000000000000000000000000000000000000000000000000000",
  34. "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art"
  35. ),
  36. ( "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f",
  37. "legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth title"
  38. ),
  39. ( "8080808080808080808080808080808080808080808080808080808080808080",
  40. "letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless"
  41. ),
  42. ( "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  43. "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo vote"
  44. ),
  45. ( "9e885d952ad362caeb4efe34a8e91bd2",
  46. "ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic"
  47. ),
  48. ( "6610b25967cdcca9d59875f5cb50b0ea75433311869e930b",
  49. "gravity machine north sort system female filter attitude volume fold club stay feature office ecology stable narrow fog"
  50. ),
  51. ( "68a79eaca2324873eacc50cb9c6eca8cc68ea5d936f98787c60c7ebc74e6ce7c",
  52. "hamster diagram private dutch cause delay private meat slide toddler razor book happy fancy gospel tennis maple dilemma loan word shrug inflict delay length"
  53. ),
  54. ( "c0ba5a8e914111210f2bd131f3d5e08d",
  55. "scheme spot photo card baby mountain device kick cradle pact join borrow"
  56. ),
  57. ( "6d9be1ee6ebd27a258115aad99b7317b9c8d28b6d76431c3",
  58. "horn tenant knee talent sponsor spell gate clip pulse soap slush warm silver nephew swap uncle crack brave"
  59. ),
  60. ( "9f6a2878b2520799a44ef18bc7df394e7061a224d2c33cd015b157d746869863",
  61. "panda eyebrow bullet gorilla call smoke muffin taste mesh discover soft ostrich alcohol speed nation flash devote level hobby quick inner drive ghost inside"
  62. ),
  63. ( "23db8160a31d3e0dca3688ed941adbf3",
  64. "cat swing flag economy stadium alone churn speed unique patch report train"
  65. ),
  66. ( "8197a4a47f0425faeaa69deebc05ca29c0a5b5cc76ceacc0",
  67. "light rule cinnamon wrap drastic word pride squirrel upgrade then income fatal apart sustain crack supply proud access"
  68. ),
  69. ( "066dca1a2bb7e8a1db2832148ce9933eea0f3ac9548d793112d9a95c9407efad",
  70. "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform"
  71. ),
  72. ( "f30f8c1da665478f49b001d94c5fc452",
  73. "vessel ladder alter error federal sibling chat ability sun glass valve picture"
  74. ),
  75. ( "c10ec20dc3cd9f652c7fac2f1230f7a3c828389a14392f05",
  76. "scissors invite lock maple supreme raw rapid void congress muscle digital elegant little brisk hair mango congress clump"
  77. ),
  78. ( "f585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f",
  79. "void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold"
  80. )
  81. )
  82. def run_test(self,name):
  83. msg_r('Testing BIP39 conversion routines...')
  84. qmsg('')
  85. from mmgen.bip39 import bip39
  86. bip39.check_wordlists()
  87. bip39.check_wordlist('bip39')
  88. vmsg('')
  89. qmsg('Checking seed to mnemonic conversion:')
  90. for v in self.vectors:
  91. chk = tuple(v[1].split())
  92. vmsg(' '+v[1])
  93. res = bip39.fromhex(v[0],'bip39')
  94. assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res,chk)
  95. vmsg('')
  96. qmsg('Checking mnemonic to seed conversion:')
  97. for v in self.vectors:
  98. chk = v[0]
  99. vmsg(' '+chk)
  100. res = bip39.tohex(v[1].split(),'bip39')
  101. assert res == chk, 'mismatch:\nres: {}\nchk: {}'.format(res,chk)
  102. vmsg('')
  103. qmsg('Checking error handling:')
  104. bad_data = (
  105. ('bad hex', 'AssertionError', 'not a hexadecimal'),
  106. ('bad id (tohex)', 'AssertionError', "must be 'bip39'"),
  107. ('bad seed len', 'AssertionError', 'invalid seed bit length'),
  108. ('bad mnemonic type', 'AssertionError', 'must be list'),
  109. ('bad id (fromhex)', 'AssertionError', "must be 'bip39'"),
  110. ('tostr = True', 'AssertionError', "'tostr' must be"),
  111. ('bad pad length (fromhex)', 'AssertionError', "invalid pad len"),
  112. ('bad pad length (tohex)', 'AssertionError', "invalid pad len"),
  113. ('bad word', 'MnemonicError', "not in the BIP39 word list"),
  114. ('bad checksum', 'MnemonicError', "checksum"),
  115. ('bad seed phrase length', 'MnemonicError', "phrase len"),
  116. )
  117. good_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong".split()
  118. bad_len_mn = "zoo zoo zoo".split()
  119. bad_chksum_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split()
  120. bad_word_mn = "admire zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo".split()
  121. bad_seed = 'deadbeef'
  122. good_seed = 'deadbeef' * 4
  123. def bad0(): bip39.fromhex('xx','bip39')
  124. def bad1(): bip39.fromhex(good_seed,'foo')
  125. def bad2(): bip39.fromhex(bad_seed,'bip39')
  126. def bad3(): bip39.tohex('string','bip39')
  127. def bad4(): bip39.tohex(good_mn,'foo')
  128. def bad5(): bip39.fromhex(good_seed,'bip39',tostr=True)
  129. def bad6(): bip39.fromhex(good_seed,'bip39',pad=23)
  130. def bad7(): bip39.tohex(good_mn,'bip39',pad=23)
  131. def bad8(): bip39.tohex(bad_word_mn,'bip39')
  132. def bad9(): bip39.tohex(bad_chksum_mn,'bip39')
  133. def bad10(): bip39.tohex(bad_len_mn,'bip39')
  134. for i in range(len(bad_data)):
  135. try:
  136. vmsg_r(' {:26}'.format(bad_data[i][0]+':'))
  137. locals()['bad'+str(i)]()
  138. except Exception as e:
  139. n = type(e).__name__
  140. vmsg(' {:15} [{}]'.format(n,e.args[0]))
  141. assert n == bad_data[i][1]
  142. assert bad_data[i][2] in e.args[0]
  143. else:
  144. rdie(3,"\nillegal action '{}' failed to raise exception".format(bad_data[n][0]))
  145. vmsg('')
  146. msg('OK')
  147. return True