rainbowadn/rainbowadn/wrisbt/wrisbtindex.py
2022-05-19 11:03:16 +03:00

64 lines
2.3 KiB
Python

from typing import Iterable
from rainbowadn.hashing.hash_point_format import hash_point_format, tabulate
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.rainbow_factory import RainbowFactory
from rainbowadn.hashing.recursivementionable import RecursiveMentionable
from rainbowadn.hashing.resolverorigin import ResolverOrigin
from rainbowadn.wrisbt.wrisbtparametres import WrisbtParametres
from rainbowadn.wrisbt.wrisbtroot import WrisbtRoot, WrisbtRootFactory
__all__ = ('WrisbtIndex', 'WrisbtIndexFactory',)
class WrisbtIndex(RecursiveMentionable):
def __init__(
self,
total: HashPoint[WrisbtRoot],
delta: HashPoint[WrisbtRoot],
keymin: int
):
assert isinstance(total, HashPoint)
assert isinstance(delta, HashPoint)
assert isinstance(keymin, int)
self.total = total
self.delta = delta
assert keymin >= 2
self.keymin = keymin
def points(self) -> Iterable[HashPoint]:
return [self.total, self.delta]
def __bytes__(self):
return bytes(self.total) + bytes(self.delta)
def __factory__(self) -> RainbowFactory['WrisbtIndex']:
return WrisbtIndexFactory(self.keymin)
def str(self, tab: int) -> str:
assert isinstance(tab, int)
return f'(index)' \
f'{tabulate(tab)}{hash_point_format(self.total, tab)}' \
f'{tabulate(tab)}{hash_point_format(self.delta, tab)}'
class WrisbtIndexFactory(RainbowFactory[WrisbtIndex]):
def __init__(self, keymin: int):
assert isinstance(keymin, int)
assert keymin >= 2
self.keymin = keymin
self.root_factory: RainbowFactory[WrisbtRoot] = WrisbtRootFactory(
WrisbtParametres(keymin, HashPoint.HASH_LENGTH)
)
assert isinstance(self.root_factory, RainbowFactory)
def from_bytes(self, source: bytes, resolver: HashResolver) -> WrisbtIndex:
assert isinstance(source, bytes)
assert isinstance(resolver, HashResolver)
return WrisbtIndex(
ResolverOrigin(self.root_factory, source[:HashPoint.HASH_LENGTH], resolver).hash_point(),
ResolverOrigin(self.root_factory, source[HashPoint.HASH_LENGTH:], resolver).hash_point(),
self.keymin
)