ut_seedsplit.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.ut_seedsplit: seed splitting unit test for the MMGen suite
  4. """
  5. from mmgen.util import msg, msg_r
  6. from ..include.common import cfg, vmsg, vmsg_r
  7. class unit_test:
  8. def run_test(self, name, ut):
  9. from mmgen.seed import Seed
  10. from mmgen.seedsplit import SeedShareList, SeedShareIdx
  11. cfg.debug_subseed = bool(cfg.verbose)
  12. def basic_ops(master_idx):
  13. test_data = {
  14. 'default': (
  15. (8, '4710FBF0', 'B3D9411B', '2670E83D', 'D1FC57ED', 'AE49CABE', '63FFBA62', 0, 0),
  16. (6, '9D07ABBD', 'AF5DC2F6', '1A3BBDAC', '2548AEE9', 'B94F7450', '1F4E5A12', 0, 0),
  17. (4, '43670520', '1F72C066', 'E5AA8DA1', 'A33966A0', 'D2BCE0A5', 'A568C315', 0, 0),
  18. ),
  19. 'φυβαρ': (
  20. (8, '4710FBF0', '269D658C', '9D25889E', '6D730ECB', 'C61A963F', '9FE99C05', 0, 0),
  21. (6, '9D07ABBD', '4998B33E', 'F00CE041', 'C612BEE5', '35CD3675', '41B3BE61', 0, 0),
  22. (4, '43670520', '77140076', 'EA82CB30', '80F7AEDE', 'D168D768', '77BE57AA', 0, 0),
  23. )
  24. }
  25. test_data_master = {
  26. '1': {
  27. 'default': (
  28. (8, '4710FBF0', '6AE6177F', 'AC12090C', '6AE6177F',
  29. '3E87A907', '7D1FEA56', 'BFEBFFFF', '629A9808'),
  30. (4, '43670520', '6739535C', 'ABF4DD38', '6739535C',
  31. '778E9C60', '89CBCFD2', '689FABF5', '70BED76B'),
  32. ),
  33. 'φυβαρ': (
  34. (8, '4710FBF0', '6AE6177F', 'AC5FA32E', '6AE6177F',
  35. '9777A750', 'C7CF2AFC', '035AAACB', 'C777FBE4'),
  36. (4, '43670520', '6739535C', '37EBA2F5', '6739535C',
  37. '927549D2', '29BADEE7', '9CA73A03', '313F5528'))
  38. },
  39. '5': {
  40. 'default': (
  41. (8, '4710FBF0', '5EFAC3D6', 'B489167D', '5EFAC3D6',
  42. 'BB004DC5', '1A0381C0', '4EA182E3', '547FB2DC'),
  43. (4, '43670520', 'EE93DB0E', '44962A7D', 'EE93DB0E',
  44. '07339882', '376A05B1', 'CE51D022', '00149CA3'),
  45. ),
  46. 'φυβαρ': (
  47. (8, '4710FBF0', '5EFAC3D6', 'A6E27EE3', '5EFAC3D6',
  48. '32C24668', 'B4C54297', '1EC9B71B', '8C5C6B1C'),
  49. (4, '43670520', 'EE93DB0E', 'B584E963', 'EE93DB0E',
  50. '4BEA2AB2', '4BEA65C7', '140FC43F', 'BBD19461'))
  51. }
  52. }
  53. if master_idx:
  54. test_data = test_data_master[str(master_idx)]
  55. for id_str in (None, 'default', 'φυβαρ'):
  56. msg_r(f'Testing basic ops (id_str={id_str!r}, master_idx={master_idx})...')
  57. vmsg('')
  58. for a, b, c, d, e, f, h, i, p in test_data[id_str if id_str is not None else 'default']:
  59. seed_bin = bytes.fromhex('deadbeef' * a)
  60. seed = Seed(cfg, seed_bin)
  61. assert seed.sid == b, seed.sid
  62. for share_count, j, k, l, m in (
  63. (2, c, c, d, i),
  64. (5, e, f, h, p)):
  65. shares = seed.split(share_count, id_str, master_idx)
  66. A = len(shares)
  67. assert A == share_count, A
  68. s = shares.format()
  69. vmsg_r(f'\n{s}')
  70. assert len(s.strip().split('\n')) == share_count+6, s
  71. if master_idx:
  72. A = shares.get_share_by_idx(1, base_seed=False).sid
  73. B = shares.get_share_by_seed_id(j, base_seed=False).sid
  74. assert A == B == m, A
  75. A = shares.get_share_by_idx(1, base_seed=True).sid
  76. B = shares.get_share_by_seed_id(j, base_seed=True).sid
  77. assert A == B == j, A
  78. A = shares.get_share_by_idx(share_count-1, base_seed=True).sid
  79. B = shares.get_share_by_seed_id(k, base_seed=True).sid
  80. assert A == B == k, A
  81. A = shares.get_share_by_idx(share_count).sid
  82. B = shares.get_share_by_seed_id(l).sid
  83. assert A == B == l, A
  84. A = shares.join().sid
  85. assert A == b, A
  86. if master_idx:
  87. slist = [shares.get_share_by_idx(i+1, base_seed=True) for i in range(len(shares))]
  88. A = Seed.join_shares(cfg, slist, master_idx, id_str).sid
  89. assert A == b, A
  90. msg('OK')
  91. def defaults_and_limits():
  92. msg_r('Testing defaults and limits...')
  93. seed_bin = bytes.fromhex('deadbeef' * 8)
  94. seed = Seed(cfg, seed_bin)
  95. shares = seed.split(SeedShareIdx.max_val)
  96. s = shares.format()
  97. # vmsg_r(f'\n{s}')
  98. assert len(s.strip().split('\n')) == 1030, s
  99. A = shares.get_share_by_idx(1024).sid
  100. B = shares.get_share_by_seed_id('4BA23728').sid
  101. assert A == '4BA23728', A
  102. assert B == '4BA23728', B
  103. A = shares.join().sid
  104. B = seed.sid
  105. assert A == B, A
  106. msg('OK')
  107. def collisions(seed_hex, ss_count, last_sid, collisions_chk, master_idx):
  108. msg_r(f'Testing Seed ID collisions ({ss_count} seed shares, master_idx={master_idx})...')
  109. vmsg('')
  110. seed_bin = bytes.fromhex(seed_hex)
  111. seed = Seed(cfg, seed_bin)
  112. SeedShareIdx.max_val = ss_count
  113. shares = seed.split(ss_count, master_idx=master_idx)
  114. A = shares.get_share_by_idx(ss_count).sid
  115. B = shares.get_share_by_seed_id(last_sid).sid
  116. assert A == last_sid, A
  117. assert B == last_sid, B
  118. assert shares.nonce_start == 0, shares.nonce_start
  119. collisions = 0
  120. for sid in shares.data['long']:
  121. collisions += shares.data['long'][sid][1]
  122. assert collisions == collisions_chk, collisions
  123. vmsg_r(f'{collisions} collisions, last_sid {last_sid}')
  124. msg('OK')
  125. def last_share_collisions():
  126. msg_r('Testing last share collisions with shortened Seed IDs')
  127. vmsg('')
  128. seed_bin = bytes.fromhex('2eadbeef'*8)
  129. seed = Seed(cfg, seed_bin)
  130. ssm_save = SeedShareIdx.max_val
  131. ssm = SeedShareIdx.max_val = 2048
  132. shares = SeedShareList(seed, count=ssm, id_str='foo', master_idx=1, debug_last_share=True)
  133. lsid = shares.last_share.sid
  134. collisions = shares.data['long'][lsid][1]
  135. assert collisions == 2, collisions
  136. assert lsid == 'B5B8AD09', lsid
  137. SeedShareIdx.max_val = ssm_save
  138. vmsg_r(f'{collisions} collisions, last_share sid {lsid}')
  139. msg('..OK')
  140. basic_ops(master_idx=None)
  141. basic_ops(master_idx=1)
  142. basic_ops(master_idx=5)
  143. defaults_and_limits()
  144. last_share_collisions()
  145. collisions('1dabcdef'*4, 65535, 'B5CBCE0A', 3, master_idx=None)
  146. collisions('18abcdef'*4, 65535, 'FF03CE82', 3, master_idx=1)
  147. return True