ut_subseed.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_subseed: subseed unit test for the MMGen suite
  4. """
  5. from mmgen.util import msg,msg_r
  6. from ..include.common import cfg,vmsg_r
  7. class unit_test:
  8. def run_test(self,name,ut):
  9. from mmgen.seed import Seed
  10. from mmgen.subseed import SubSeedList,SubSeedIdxRange
  11. nSubseeds = SubSeedList.dfl_len
  12. def basic_ops():
  13. msg_r('Testing basic ops...')
  14. for a,b,c,d,e,f,h in (
  15. (8,'4710FBF0','0C1B0615','803B165C','2669AC64',256,'10L'),
  16. (6,'9D07ABBD','EBA9C33F','20787E6A','192E2AA2',192,'10L'),
  17. (4,'43670520','04A4CCB3','B5F21D7B','C1934CFF',128,'10L'),
  18. ):
  19. seed_bin = bytes.fromhex('deadbeef' * a)
  20. seed = Seed( cfg, seed_bin )
  21. assert seed.sid == b, seed.sid
  22. subseed = seed.subseed('2s')
  23. assert subseed.sid == c, subseed.sid
  24. subseed = seed.subseed('3')
  25. assert subseed.sid == d, subseed.sid
  26. subseed = seed.subseed_by_seed_id(e)
  27. assert subseed.bitlen == f, subseed.bitlen
  28. assert subseed.sid == e, subseed.sid
  29. assert subseed.idx == 10, subseed.idx
  30. assert subseed.ss_idx == h, subseed.ss_idx
  31. seed2 = Seed( cfg, seed_bin )
  32. ss2_list = seed2.subseeds
  33. seed2.subseeds._generate(1)
  34. assert len(ss2_list) == 1, len(ss2_list)
  35. seed2.subseeds._generate(1) # do nothing
  36. seed2.subseeds._generate(2) # append one item
  37. seed2.subseeds._generate(5)
  38. assert len(ss2_list) == 5, len(ss2_list)
  39. seed2.subseeds._generate(3) # do nothing
  40. assert len(ss2_list) == 5, len(ss2_list)
  41. seed2.subseeds._generate(10)
  42. assert len(ss2_list) == 10, len(ss2_list)
  43. assert seed.pfmt() == seed2.pfmt()
  44. assert seed.subseeds.pfmt() == seed2.subseeds.pfmt()
  45. s = seed.subseeds.format(1,nSubseeds)
  46. s_lines = s.strip().split('\n')
  47. assert len(s_lines) == nSubseeds + 4, s
  48. a = seed.subseed('2L').sid
  49. b = [e for e in s_lines if ' 2L:' in e][0].strip().split()[1]
  50. assert a == b, b
  51. c = seed.subseed('2').sid
  52. assert c == a, c
  53. a = seed.subseed('5S').sid
  54. b = [e for e in s_lines if ' 5S:' in e][0].strip().split()[3]
  55. assert a == b, b
  56. s = seed.subseeds.format(nSubseeds+1,nSubseeds+2)
  57. s_lines = s.strip().split('\n')
  58. assert len(s_lines) == 6, s
  59. ss_idx = str(nSubseeds+2) + 'S'
  60. a = seed.subseed(ss_idx).sid
  61. b = [e for e in s_lines if f' {ss_idx}:' in e][0].strip().split()[3]
  62. assert a == b, b
  63. s = seed.subseeds.format(1,10)
  64. s_lines = s.strip().split('\n')
  65. assert len(s_lines) == 14, s
  66. vmsg_r(f'\n{s}')
  67. msg('OK')
  68. def defaults_and_limits():
  69. msg_r('Testing defaults and limits...')
  70. seed_bin = bytes.fromhex('deadbeef' * 8)
  71. seed = Seed( cfg, seed_bin, nSubseeds=11 )
  72. seed.subseeds._generate()
  73. ss = seed.subseeds
  74. assert len(ss.data['long']) == len(ss.data['short']), len(ss.data['short'])
  75. assert len(ss) == 11, len(ss)
  76. seed = Seed( cfg, seed_bin )
  77. seed.subseeds._generate()
  78. ss = seed.subseeds
  79. assert len(ss.data['long']) == len(ss.data['short']), len(ss.data['short'])
  80. assert len(ss) == nSubseeds, len(ss)
  81. seed = Seed( cfg, seed_bin )
  82. seed.subseed_by_seed_id('EEEEEEEE')
  83. ss = seed.subseeds
  84. assert len(ss.data['long']) == len(ss.data['short']), len(ss.data['short'])
  85. assert len(ss) == nSubseeds, len(ss)
  86. seed = Seed( cfg, seed_bin )
  87. subseed = seed.subseed_by_seed_id('803B165C')
  88. assert len(ss.data['long']) == len(ss.data['short']), len(ss.data['short'])
  89. assert subseed.sid == '803B165C', subseed.sid
  90. assert subseed.idx == 3, subseed.idx
  91. seed = Seed( cfg, seed_bin )
  92. subseed = seed.subseed_by_seed_id('803B165C',last_idx=1)
  93. assert len(ss.data['long']) == len(ss.data['short']), len(ss.data['short'])
  94. assert subseed is None, subseed
  95. r = SubSeedIdxRange('1-5')
  96. r2 = SubSeedIdxRange(1,5)
  97. assert r2 == r, r2
  98. assert r == (r.first,r.last), r
  99. assert r.first == 1, r.first
  100. assert r.last == 5, r.last
  101. assert r.items == [1,2,3,4,5], r.items
  102. assert list(r.iterate()) == r.items, list(r.iterate())
  103. r = SubSeedIdxRange('22')
  104. r2 = SubSeedIdxRange(22,22)
  105. assert r2 == r, r2
  106. assert r == (r.first,r.last), r
  107. assert r.first == 22, r.first
  108. assert r.last == 22, r.last
  109. assert r.items == [22], r
  110. assert list(r.iterate()) == r.items, list(r.iterate())
  111. r = SubSeedIdxRange('3-3')
  112. assert r.items == [3], r.items
  113. r = SubSeedIdxRange(f'{nSubseeds-1}-{nSubseeds}')
  114. assert r.items == [nSubseeds-1,nSubseeds], r.items
  115. for n,e in enumerate(SubSeedIdxRange('1-5').iterate(),1):
  116. assert n == e, e
  117. assert n == 5, n
  118. msg('OK')
  119. def collisions():
  120. ss_count,ltr,last_sid,collisions_chk = (
  121. (SubSeedIdxRange.max_idx,'S','2788F26B',470),
  122. (49509,'L','8D1FE500',2)
  123. )[bool(cfg.fast)]
  124. last_idx = str(ss_count) + ltr
  125. msg_r(f'Testing Seed ID collisions ({ss_count} subseed pairs)...')
  126. seed_bin = bytes.fromhex('12abcdef' * 8) # 95B3D78D
  127. seed = Seed( cfg, seed_bin )
  128. seed.subseeds._generate(ss_count)
  129. ss = seed.subseeds
  130. assert seed.subseed(last_idx).sid == last_sid, seed.subseed(last_idx).sid
  131. for sid in ss.data['long']:
  132. # msg(sid)
  133. assert sid not in ss.data['short']
  134. collisions = 0
  135. for k in ('short','long'):
  136. for sid in ss.data[k]:
  137. collisions += ss.data[k][sid][1]
  138. assert collisions == collisions_chk, collisions
  139. vmsg_r(f'\n{collisions} collisions, last_sid {last_sid}')
  140. msg('OK')
  141. basic_ops()
  142. defaults_and_limits()
  143. collisions()
  144. return True