ut_seedsplit.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_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','3E87A907','7D1FEA56','BFEBFFFF','629A9808'),
  29. (4,'43670520','6739535C','ABF4DD38','6739535C','778E9C60','89CBCFD2','689FABF5','70BED76B'),
  30. ),
  31. 'φυβαρ': (
  32. (8,'4710FBF0','6AE6177F','AC5FA32E','6AE6177F','9777A750','C7CF2AFC','035AAACB','C777FBE4'),
  33. (4,'43670520','6739535C','37EBA2F5','6739535C','927549D2','29BADEE7','9CA73A03','313F5528'))
  34. },
  35. '5': {
  36. 'default': (
  37. (8,'4710FBF0','5EFAC3D6','B489167D','5EFAC3D6','BB004DC5','1A0381C0','4EA182E3','547FB2DC'),
  38. (4,'43670520','EE93DB0E','44962A7D','EE93DB0E','07339882','376A05B1','CE51D022','00149CA3'),
  39. ),
  40. 'φυβαρ': (
  41. (8,'4710FBF0','5EFAC3D6','A6E27EE3','5EFAC3D6','32C24668','B4C54297','1EC9B71B','8C5C6B1C'),
  42. (4,'43670520','EE93DB0E','B584E963','EE93DB0E','4BEA2AB2','4BEA65C7','140FC43F','BBD19461'))
  43. }
  44. }
  45. if master_idx:
  46. test_data = test_data_master[str(master_idx)]
  47. for id_str in (None,'default','φυβαρ'):
  48. msg_r(f'Testing basic ops (id_str={id_str!r}, master_idx={master_idx})...')
  49. vmsg('')
  50. for a,b,c,d,e,f,h,i,p in test_data[id_str if id_str is not None else 'default']:
  51. seed_bin = bytes.fromhex('deadbeef' * a)
  52. seed = Seed( cfg, seed_bin )
  53. assert seed.sid == b, seed.sid
  54. for share_count,j,k,l,m in ((2,c,c,d,i),(5,e,f,h,p)):
  55. shares = seed.split(share_count,id_str,master_idx)
  56. A = len(shares)
  57. assert A == share_count, A
  58. s = shares.format()
  59. vmsg_r(f'\n{s}')
  60. assert len(s.strip().split('\n')) == share_count+6, s
  61. if master_idx:
  62. A = shares.get_share_by_idx(1,base_seed=False).sid
  63. B = shares.get_share_by_seed_id(j,base_seed=False).sid
  64. assert A == B == m, A
  65. A = shares.get_share_by_idx(1,base_seed=True).sid
  66. B = shares.get_share_by_seed_id(j,base_seed=True).sid
  67. assert A == B == j, A
  68. A = shares.get_share_by_idx(share_count-1,base_seed=True).sid
  69. B = shares.get_share_by_seed_id(k,base_seed=True).sid
  70. assert A == B == k, A
  71. A = shares.get_share_by_idx(share_count).sid
  72. B = shares.get_share_by_seed_id(l).sid
  73. assert A == B == l, A
  74. A = shares.join().sid
  75. assert A == b, A
  76. if master_idx:
  77. slist = [shares.get_share_by_idx(i+1,base_seed=True) for i in range(len(shares))]
  78. A = Seed.join_shares( cfg, slist, master_idx, id_str ).sid
  79. assert A == b, A
  80. msg('OK')
  81. def defaults_and_limits():
  82. msg_r('Testing defaults and limits...')
  83. seed_bin = bytes.fromhex('deadbeef' * 8)
  84. seed = Seed( cfg, seed_bin )
  85. shares = seed.split(SeedShareIdx.max_val)
  86. s = shares.format()
  87. # vmsg_r(f'\n{s}')
  88. assert len(s.strip().split('\n')) == 1030, s
  89. A = shares.get_share_by_idx(1024).sid
  90. B = shares.get_share_by_seed_id('4BA23728').sid
  91. assert A == '4BA23728', A
  92. assert B == '4BA23728', B
  93. A = shares.join().sid
  94. B = seed.sid
  95. assert A == B, A
  96. msg('OK')
  97. def collisions(seed_hex,ss_count,last_sid,collisions_chk,master_idx):
  98. msg_r(f'Testing Seed ID collisions ({ss_count} seed shares, master_idx={master_idx})...')
  99. vmsg('')
  100. seed_bin = bytes.fromhex(seed_hex)
  101. seed = Seed( cfg, seed_bin )
  102. SeedShareIdx.max_val = ss_count
  103. shares = seed.split(ss_count,master_idx=master_idx)
  104. A = shares.get_share_by_idx(ss_count).sid
  105. B = shares.get_share_by_seed_id(last_sid).sid
  106. assert A == last_sid, A
  107. assert B == last_sid, B
  108. assert shares.nonce_start == 0, shares.nonce_start
  109. collisions = 0
  110. for sid in shares.data['long']:
  111. collisions += shares.data['long'][sid][1]
  112. assert collisions == collisions_chk, collisions
  113. vmsg_r(f'{collisions} collisions, last_sid {last_sid}')
  114. msg('OK')
  115. def last_share_collisions():
  116. msg_r('Testing last share collisions with shortened Seed IDs')
  117. vmsg('')
  118. seed_bin = bytes.fromhex('2eadbeef'*8)
  119. seed = Seed( cfg, seed_bin )
  120. ssm_save = SeedShareIdx.max_val
  121. ssm = SeedShareIdx.max_val = 2048
  122. shares = SeedShareList(seed,count=ssm,id_str='foo',master_idx=1,debug_last_share=True)
  123. lsid = shares.last_share.sid
  124. collisions = shares.data['long'][lsid][1]
  125. assert collisions == 2, collisions
  126. assert lsid == 'B5B8AD09', lsid
  127. SeedShareIdx.max_val = ssm_save
  128. vmsg_r(f'{collisions} collisions, last_share sid {lsid}')
  129. msg('..OK')
  130. basic_ops(master_idx=None)
  131. basic_ops(master_idx=1)
  132. basic_ops(master_idx=5)
  133. defaults_and_limits()
  134. last_share_collisions()
  135. collisions('1dabcdef'*4,65535,'B5CBCE0A',3,master_idx=None)
  136. collisions('18abcdef'*4,65535,'FF03CE82',3,master_idx=1)
  137. return True