mnemonic.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 by philemon <mmgen-py@yandex.com>
  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. """
  19. mnemonic.py: Test suite for mmgen.mnemonic module
  20. """
  21. from mmgen.mnemonic import *
  22. from test import *
  23. import sys
  24. from binascii import hexlify
  25. from mmgen.mn_electrum import electrum_words as el
  26. from mmgen.mn_tirosh import tirosh_words as tl
  27. def do_random_tests(n):
  28. r = get_random(n)
  29. em = get_mnemonic_from_seed(r,el.strip().split("\n"),
  30. "electrum",print_info=False)
  31. tm = get_mnemonic_from_seed(r,tl.strip().split("\n"),
  32. "tirosh",print_info=False)
  33. print "Seed: %s (%s bits)" % (hexlify(r),len(r)*8)
  34. print "Electrum: %s" % " ".join(em)
  35. print "Tirosh: %s" % " ".join(tm)
  36. def hextobaseN_test(num_in,base,wl,quiet=False):
  37. do_msg = nomsg if quiet else msg
  38. do_msg("Input: %s" % num_in)
  39. num_enc = "".join(hextobaseN(base,num_in,wl,len(num_in)*2))
  40. do_msg("Encoded value: %s" % num_enc)
  41. num_dec = baseNtohex(base,num_enc,wl)
  42. do_msg("Decoded value: %s" % num_dec)
  43. test_equality(num_in,num_dec,wl,quiet)
  44. return num_enc,num_dec
  45. def baseNtohex_test(num_in,base,wl,quiet=False):
  46. do_msg = nomsg if quiet else msg
  47. do_msg("Input: %s" % num_in)
  48. num_enc = baseNtohex(base,list(num_in),wl)
  49. do_msg("Encoded value: %s" % num_enc)
  50. num_dec = "".join(hextobaseN(base,num_enc,wl,len(num_enc)*2))
  51. do_msg("Decoded value: %s" % num_dec)
  52. test_equality(num_in,num_dec,wl,quiet)
  53. return num_enc,num_dec
  54. def random128(): do_random_tests(16)
  55. def random192(): do_random_tests(24)
  56. def random256(): do_random_tests(32)
  57. def random512(): do_random_tests(64)
  58. def electrum(): check_wordlist(el,"electrum")
  59. def tirosh(): check_wordlist(tl,"tirosh")
  60. def base10tohex(num,quiet=False):
  61. enc,dec = baseNtohex_test(num,10,"0123456789",quiet)
  62. print "Decimal: %s" % num
  63. print "Hex (encoded): %s" % enc
  64. print "Decimal (recoded): %s" % dec.lstrip("0")
  65. def hextobase10(num,quiet=False):
  66. enc,dec= hextobaseN_test(num,10,"0123456789",quiet)
  67. print "Hex: %s" % num
  68. print "Decimal (encoded): %s" % enc.lstrip("0")
  69. print "Hex (recoded): %s" % dec
  70. def base8tohex(num,quiet=False):
  71. enc,dec = baseNtohex_test(num,8,"01234567",quiet)
  72. print "Octal: %s" % num
  73. print "Hex (encoded): %s" % enc
  74. print "Octal (recoded): %s" % "0" + dec.lstrip("0")
  75. def hextobase8(num,quiet=False):
  76. enc,dec = hextobaseN_test(num,8,"01234567",quiet)
  77. print "Hex: %s" % num
  78. print "Octal: %s" % "0" + enc.lstrip("0")
  79. print "Hex (recoded): %s" % dec
  80. tests = {
  81. "random128": [],
  82. "random192": [],
  83. "random256": [],
  84. "random512": [],
  85. "electrum": [],
  86. "tirosh": [],
  87. "base10tohex": ['base10num [int]','quiet [bool=False]'],
  88. "hextobase10": ['hexnum [str]', 'quiet [bool=False]'],
  89. "base8tohex": ['base8num [int]', 'quiet [bool=False]'],
  90. "hextobase8": ['hexnum [str]', 'quiet [bool=False]'],
  91. }
  92. args = process_test_args(sys.argv, tests)
  93. eval(sys.argv[1])(*args)