seed splitting: 'split' -> 'share' for all identifiers, where applicable
This commit is contained in:
parent
2388662e72
commit
b0105598da
4 changed files with 56 additions and 56 deletions
10
mmgen/obj.py
10
mmgen/obj.py
|
|
@ -339,9 +339,9 @@ class MMGenIdx(int,InitErrors):
|
|||
except Exception as e:
|
||||
return cls.init_fail(e,num)
|
||||
|
||||
class SeedSplitIdx(MMGenIdx): max_val = 1024
|
||||
class SeedSplitCount(SeedSplitIdx): min_val = 2
|
||||
class MasterSplitIdx(MMGenIdx): max_val = 1024
|
||||
class SeedShareIdx(MMGenIdx): max_val = 1024
|
||||
class SeedShareCount(SeedShareIdx): min_val = 2
|
||||
class MasterShareIdx(MMGenIdx): max_val = 1024
|
||||
class AddrIdx(MMGenIdx): max_digits = 7
|
||||
|
||||
class AddrIdxList(list,InitErrors,MMGenObject):
|
||||
|
|
@ -872,8 +872,8 @@ class MMGenPWIDString(MMGenLabel):
|
|||
desc = 'password ID string'
|
||||
forbidden = list(' :/\\')
|
||||
|
||||
class SeedSplitIDString(MMGenPWIDString):
|
||||
desc = 'seed split ID string'
|
||||
class SeedShareIDString(MMGenPWIDString):
|
||||
desc = 'seed share ID string'
|
||||
|
||||
class MMGenAddrType(str,Hilite,InitErrors,MMGenObject):
|
||||
width = 1
|
||||
|
|
|
|||
|
|
@ -206,11 +206,11 @@ class Seed(SeedBase):
|
|||
def subseed_by_seed_id(self,sid,last_idx=None,print_msg=False):
|
||||
return self.subseeds.get_subseed_by_seed_id(sid,last_idx=last_idx,print_msg=print_msg)
|
||||
|
||||
def splitlist(self,count,id_str=None):
|
||||
return SeedSplitList(self,count,id_str)
|
||||
def split(self,count,id_str=None):
|
||||
return SeedShareList(self,count,id_str)
|
||||
|
||||
@staticmethod
|
||||
def join_splits(seed_list):
|
||||
def join_shares(seed_list):
|
||||
if not hasattr(seed_list,'__next__'): # seed_list can be iterator or iterable
|
||||
seed_list = iter(seed_list)
|
||||
|
||||
|
|
@ -219,7 +219,7 @@ class Seed(SeedBase):
|
|||
ret = 0
|
||||
count = 0
|
||||
|
||||
def add_split(ss):
|
||||
def add_share(ss):
|
||||
if d.slen:
|
||||
assert ss.length == d.slen,'Seed length mismatch! {} != {}'.format(ss.length,d.slen)
|
||||
else:
|
||||
|
|
@ -228,9 +228,9 @@ class Seed(SeedBase):
|
|||
d.count += 1
|
||||
|
||||
for ss in seed_list:
|
||||
add_split(ss)
|
||||
add_share(ss)
|
||||
|
||||
SeedSplitCount(d.count)
|
||||
SeedShareCount(d.count)
|
||||
return Seed(seed_bin=d.ret.to_bytes(d.slen // 8,'big'))
|
||||
|
||||
class SubSeed(SeedBase):
|
||||
|
|
@ -257,15 +257,15 @@ class SubSeed(SeedBase):
|
|||
byte_len = 16 if short else seed.length // 8
|
||||
return scramble_seed(seed.data,scramble_key)[:byte_len]
|
||||
|
||||
class SeedSplitList(SubSeedList):
|
||||
class SeedShareList(SubSeedList):
|
||||
have_short = False
|
||||
split_type = 'N-of-N'
|
||||
|
||||
count = MMGenImmutableAttr('count',SeedSplitCount)
|
||||
id_str = MMGenImmutableAttr('id_str',SeedSplitIDString)
|
||||
count = MMGenImmutableAttr('count',SeedShareCount)
|
||||
id_str = MMGenImmutableAttr('id_str',SeedShareIDString)
|
||||
|
||||
def __init__(self,parent_seed,count,id_str=None):
|
||||
self.member_type = SeedSplit
|
||||
self.member_type = SeedShare
|
||||
self.parent_seed = parent_seed
|
||||
self.id_str = id_str or 'default'
|
||||
self.count = count
|
||||
|
|
@ -273,8 +273,8 @@ class SeedSplitList(SubSeedList):
|
|||
while True:
|
||||
self.data = { 'long': IndexedDict(), 'short': IndexedDict() }
|
||||
self._generate(count-1)
|
||||
self.last_split = SeedSplitLast(self)
|
||||
sid = self.last_split.sid
|
||||
self.last_share = SeedShareLast(self)
|
||||
sid = self.last_share.sid
|
||||
if sid in self.data['long'] or sid == parent_seed.sid:
|
||||
# collision: throw out entire split list and redo with new start nonce
|
||||
if g.debug_subseed:
|
||||
|
|
@ -289,21 +289,21 @@ class SeedSplitList(SubSeedList):
|
|||
B = self.join().data
|
||||
assert A == B,'Data mismatch!\noriginal seed: {!r}\nrejoined seed: {!r}'.format(A,B)
|
||||
|
||||
def get_split_by_idx(self,idx):
|
||||
def get_share_by_idx(self,idx):
|
||||
if idx == self.count:
|
||||
return self.last_split
|
||||
return self.last_share
|
||||
else:
|
||||
ss_idx = SubSeedIdx(str(idx) + 'L')
|
||||
return self.get_subseed_by_ss_idx(ss_idx)
|
||||
|
||||
def get_split_by_seed_id(self,sid,last_idx=None):
|
||||
def get_share_by_seed_id(self,sid,last_idx=None):
|
||||
if sid == self.data['long'].key(self.count-1):
|
||||
return self.last_split
|
||||
return self.last_share
|
||||
else:
|
||||
return self.get_subseed_by_seed_id(sid,last_idx=last_idx)
|
||||
|
||||
def join(self):
|
||||
return Seed.join_splits(self.get_split_by_idx(i+1) for i in range(len(self)))
|
||||
return Seed.join_shares(self.get_share_by_idx(i+1) for i in range(len(self)))
|
||||
|
||||
def format(self):
|
||||
assert self.split_type == 'N-of-N'
|
||||
|
|
@ -313,7 +313,7 @@ class SeedSplitList(SubSeedList):
|
|||
hdr = ' {} {} ({} bits)\n'.format('Seed:',self.parent_seed.sid.hl(),self.parent_seed.length)
|
||||
hdr += ' {} {c}-of-{c} (XOR)\n'.format('Split Type:',c=self.count)
|
||||
hdr += ' {} {}\n\n'.format('ID String:',self.id_str.hl())
|
||||
hdr += fs1.format('Splits')
|
||||
hdr += fs1.format('Shares')
|
||||
hdr += fs1.format('------')
|
||||
|
||||
sl = self.data['long'].keys
|
||||
|
|
@ -321,7 +321,7 @@ class SeedSplitList(SubSeedList):
|
|||
|
||||
return hdr + ''.join(body)
|
||||
|
||||
class SeedSplit(SubSeed):
|
||||
class SeedShare(SubSeed):
|
||||
|
||||
@staticmethod
|
||||
def make_subseed_bin(parent_list,idx:int,nonce:int,length:str):
|
||||
|
|
@ -336,9 +336,9 @@ class SeedSplit(SubSeed):
|
|||
byte_len = seed.length // 8
|
||||
return scramble_seed(seed.data,scramble_key)[:byte_len]
|
||||
|
||||
class SeedSplitLast(SubSeed):
|
||||
class SeedShareLast(SubSeed):
|
||||
|
||||
idx = MMGenImmutableAttr('idx',SeedSplitIdx)
|
||||
idx = MMGenImmutableAttr('idx',SeedShareIdx)
|
||||
nonce = 0
|
||||
|
||||
def __init__(self,parent_list):
|
||||
|
|
@ -347,7 +347,7 @@ class SeedSplitLast(SubSeed):
|
|||
|
||||
@staticmethod
|
||||
def make_subseed_bin(parent_list):
|
||||
seed_list = (parent_list.get_split_by_idx(i+1) for i in range(len(parent_list)))
|
||||
seed_list = (parent_list.get_share_by_idx(i+1) for i in range(len(parent_list)))
|
||||
seed = parent_list.parent_seed
|
||||
|
||||
ret = int(seed.data.hex(),16)
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ tests = OrderedDict([
|
|||
'bad': ('s',1.1,10000000,-1,0),
|
||||
'good': (('7',7),(1,1),(9999999,9999999))
|
||||
}),
|
||||
('SeedSplitIdx', {
|
||||
('SeedShareIdx', {
|
||||
'bad': ('s',1.1,1025,-1,0),
|
||||
'good': (('7',7),(1,1),(1024,1024))
|
||||
}),
|
||||
('SeedSplitCount', {
|
||||
('SeedShareCount', {
|
||||
'bad': ('s',2.1,1025,-1,0,1),
|
||||
'good': (('7',7),(2,2),(1024,1024))
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ class unit_test(object):
|
|||
|
||||
def run_test(self,name):
|
||||
from mmgen.seed import Seed
|
||||
from mmgen.obj import SeedSplitIdx
|
||||
from mmgen.obj import SeedShareIdx
|
||||
|
||||
def basic_ops():
|
||||
test_data = {
|
||||
|
|
@ -34,29 +34,29 @@ class unit_test(object):
|
|||
seed = Seed(seed_bin)
|
||||
assert seed.sid == b, seed.sid
|
||||
|
||||
for split_count,j,k,l in ((2,c,c,d),(5,e,f,h)):
|
||||
for share_count,j,k,l in ((2,c,c,d),(5,e,f,h)):
|
||||
|
||||
splitlist = seed.splitlist(split_count,id_str)
|
||||
A = len(splitlist)
|
||||
assert A == split_count, A
|
||||
shares = seed.split(share_count,id_str)
|
||||
A = len(shares)
|
||||
assert A == share_count, A
|
||||
|
||||
s = splitlist.format()
|
||||
s = shares.format()
|
||||
vmsg_r('\n{}'.format(s))
|
||||
assert len(s.strip().split('\n')) == split_count+6, s
|
||||
assert len(s.strip().split('\n')) == share_count+6, s
|
||||
|
||||
A = splitlist.get_split_by_idx(1).sid
|
||||
B = splitlist.get_split_by_seed_id(j).sid
|
||||
A = shares.get_share_by_idx(1).sid
|
||||
B = shares.get_share_by_seed_id(j).sid
|
||||
assert A == B == j, A
|
||||
|
||||
A = splitlist.get_split_by_idx(split_count-1).sid
|
||||
B = splitlist.get_split_by_seed_id(k).sid
|
||||
A = shares.get_share_by_idx(share_count-1).sid
|
||||
B = shares.get_share_by_seed_id(k).sid
|
||||
assert A == B == k, A
|
||||
|
||||
A = splitlist.get_split_by_idx(split_count).sid
|
||||
B = splitlist.get_split_by_seed_id(l).sid
|
||||
A = shares.get_share_by_idx(share_count).sid
|
||||
B = shares.get_share_by_seed_id(l).sid
|
||||
assert A == B == l, A
|
||||
|
||||
A = splitlist.join().sid
|
||||
A = shares.join().sid
|
||||
assert A == b, A
|
||||
|
||||
msg('OK')
|
||||
|
|
@ -67,17 +67,17 @@ class unit_test(object):
|
|||
seed_bin = bytes.fromhex('deadbeef' * 8)
|
||||
seed = Seed(seed_bin)
|
||||
|
||||
splitlist = seed.splitlist(SeedSplitIdx.max_val)
|
||||
s = splitlist.format()
|
||||
shares = seed.split(SeedShareIdx.max_val)
|
||||
s = shares.format()
|
||||
# vmsg_r('\n{}'.format(s))
|
||||
assert len(s.strip().split('\n')) == 1030, s
|
||||
|
||||
A = splitlist.get_split_by_idx(1024).sid
|
||||
B = splitlist.get_split_by_seed_id('4BA23728').sid
|
||||
A = shares.get_share_by_idx(1024).sid
|
||||
B = shares.get_share_by_seed_id('4BA23728').sid
|
||||
assert A == '4BA23728', A
|
||||
assert B == '4BA23728', B
|
||||
|
||||
A = splitlist.join().sid
|
||||
A = shares.join().sid
|
||||
B = seed.sid
|
||||
assert A == B, A
|
||||
|
||||
|
|
@ -86,24 +86,24 @@ class unit_test(object):
|
|||
def collisions():
|
||||
ss_count,last_sid,collisions_chk = (65535,'B5CBCE0A',3)
|
||||
|
||||
msg_r('Testing Seed ID collisions ({} seed splits)...'.format(ss_count))
|
||||
msg_r('Testing Seed ID collisions ({} seed shares)...'.format(ss_count))
|
||||
vmsg('')
|
||||
|
||||
seed_bin = bytes.fromhex('1dabcdef' * 4)
|
||||
seed = Seed(seed_bin)
|
||||
|
||||
SeedSplitIdx.max_val = ss_count
|
||||
splitlist = seed.splitlist(ss_count)
|
||||
A = splitlist.get_split_by_idx(ss_count).sid
|
||||
B = splitlist.get_split_by_seed_id(last_sid).sid
|
||||
SeedShareIdx.max_val = ss_count
|
||||
shares = seed.split(ss_count)
|
||||
A = shares.get_share_by_idx(ss_count).sid
|
||||
B = shares.get_share_by_seed_id(last_sid).sid
|
||||
assert A == last_sid, A
|
||||
assert B == last_sid, B
|
||||
|
||||
assert splitlist.nonce_start == 0, splitlist.nonce_start
|
||||
assert shares.nonce_start == 0, shares.nonce_start
|
||||
|
||||
collisions = 0
|
||||
for sid in splitlist.data['long']:
|
||||
collisions += splitlist.data['long'][sid][1]
|
||||
for sid in shares.data['long']:
|
||||
collisions += shares.data['long'][sid][1]
|
||||
|
||||
assert collisions == collisions_chk, collisions
|
||||
vmsg_r('\n{} collisions, last_sid {}'.format(collisions,last_sid))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue