ut_bip39.py 7.3 KB

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