type
This commit is contained in:
1 parent
305c6fea94
commit
31cff943be
2 files changed
+53
-52
No files matched your search
@@ -1,8 +1,8 @@
|
|||||||
from collections.abc import Sequence, Iterable, Iterator
|
from collections.abc import Iterable, Iterator, Sequence
|
||||||
from typing import Literal
|
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
from operator import xor
|
from operator import xor
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
|
||||||
def maj(a, b, c):
|
def maj(a, b, c):
|
||||||
@@ -10,15 +10,15 @@ def maj(a, b, c):
|
|||||||
return 1 * (a + b + c >= 2)
|
return 1 * (a + b + c >= 2)
|
||||||
|
|
||||||
|
|
||||||
class Bih(Sequence):
|
class LSFR(Sequence[int]):
|
||||||
def __init__(
|
def __init__(
|
||||||
self, length: Literal[19, 22, 23], initializer: Iterable | None = None
|
self, length: Literal[19, 22, 23], initializer: Iterable[int] | None = None
|
||||||
):
|
):
|
||||||
match length:
|
match length:
|
||||||
case 19:
|
case 19:
|
||||||
self.m = 8
|
self.m = 8 # bit to decide major
|
||||||
self.n = (13, 16, 17, 18)
|
self.n = (13, 16, 17, 18) # bits to xor to 0 when self.maj == major
|
||||||
self.g = 18
|
self.g = 18 # bit to xor to stream
|
||||||
case 22:
|
case 22:
|
||||||
self.m = 10
|
self.m = 10
|
||||||
self.n = (20, 21)
|
self.n = (20, 21)
|
||||||
@@ -33,38 +33,58 @@ class Bih(Sequence):
|
|||||||
|
|
||||||
if initializer is None:
|
if initializer is None:
|
||||||
initializer = [0] * length
|
initializer = [0] * length
|
||||||
self.q = list(initializer)
|
self.q: list[int] = list(initializer)
|
||||||
if len(self.q) != length:
|
if len(self.q) != length:
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.q)
|
return len(self.q)
|
||||||
|
|
||||||
|
def __getitem__(self, bro):
|
||||||
|
return self.q[bro]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.q)
|
return str(self.q)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def maj(self):
|
def maj(self) -> int:
|
||||||
return self[self.m]
|
return self[self.m]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def st(self):
|
def st(self) -> int:
|
||||||
return self[self.g]
|
return self[self.g]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def element_new(self):
|
def elements_new(self) -> tuple[int, ...]:
|
||||||
return tuple(self[n] for n in self.n)
|
return tuple(self[n] for n in self.n)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def n_new(self):
|
def n_new(self) -> int:
|
||||||
return reduce(xor, self.element_new)
|
return reduce(xor, self.elements_new)
|
||||||
|
|
||||||
def roll(self):
|
def roll(self):
|
||||||
assert len(self) == self.len
|
assert len(self) == self.len
|
||||||
self.q = [self.n_new, *self[:-1]]
|
self.q = [self.n_new, *self[:-1]]
|
||||||
|
|
||||||
def __getitem__(self, bro):
|
|
||||||
return self.q[bro]
|
def majroll(a: LSFR, b: LSFR, c: LSFR):
|
||||||
|
"roll when maj"
|
||||||
|
r = maj(a.maj, b.maj, c.maj)
|
||||||
|
for i in (a, b, c):
|
||||||
|
if i.maj == r:
|
||||||
|
i.roll()
|
||||||
|
|
||||||
|
|
||||||
|
def s(a: LSFR, b: LSFR, c: LSFR):
|
||||||
|
"collect for stream"
|
||||||
|
return reduce(xor, (i.st for i in (a, b, c)))
|
||||||
|
|
||||||
|
|
||||||
|
def op(a: LSFR, b: LSFR, c: LSFR, n: int) -> Iterator[int]:
|
||||||
|
"this the one"
|
||||||
|
for _ in range(n):
|
||||||
|
majroll(a, b, c)
|
||||||
|
yield s(a, b, c)
|
||||||
|
|
||||||
|
|
||||||
def dihcheese(n: int, length: int):
|
def dihcheese(n: int, length: int):
|
||||||
@@ -72,29 +92,12 @@ def dihcheese(n: int, length: int):
|
|||||||
|
|
||||||
|
|
||||||
def dih(n: int, length: int):
|
def dih(n: int, length: int):
|
||||||
return Bih(length, dihcheese(n, length))
|
return LSFR(length, dihcheese(n, length)) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def majroll(a: Bih, b: Bih, c: Bih):
|
|
||||||
r = maj(a.maj, b.maj, c.maj)
|
|
||||||
for i in (a, b, c):
|
|
||||||
if i.maj == r:
|
|
||||||
i.roll()
|
|
||||||
|
|
||||||
|
|
||||||
def s(a: Bih, b: Bih, c: Bih):
|
|
||||||
return reduce(xor, (i.st for i in (a, b, c)))
|
|
||||||
|
|
||||||
|
|
||||||
def cih(n: int):
|
def cih(n: int):
|
||||||
n = [int(i) for i in str(n)]
|
ln = [int(i) for i in str(n)]
|
||||||
return Bih(len(n), n)
|
return LSFR(len(ln), ln) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def op(a: Bih, b: Bih, c: Bih, n: int):
|
|
||||||
for i in range(n):
|
|
||||||
majroll(a, b, c)
|
|
||||||
yield s(a, b, c)
|
|
||||||
|
|
||||||
|
|
||||||
def opdih(a, b, c, n):
|
def opdih(a, b, c, n):
|
||||||
@@ -121,12 +124,12 @@ def main1(fahh=True):
|
|||||||
return list(d)
|
return list(d)
|
||||||
|
|
||||||
|
|
||||||
def render(dih: Iterator[int]):
|
def render(dih: Iterable[int]):
|
||||||
bih = " ".join(str(i) for i in reversed(list(dih)))
|
bih = " ".join(str(i) for i in reversed(list(dih)))
|
||||||
return f"-> {bih}"
|
return f"-> {bih}"
|
||||||
|
|
||||||
|
|
||||||
def dihrender(dih: Iterator[int]):
|
def dihrender(dih: Iterable[int]):
|
||||||
ar = "->"
|
ar = "->"
|
||||||
cih = []
|
cih = []
|
||||||
for i in dih:
|
for i in dih:
|
||||||
@@ -151,4 +154,3 @@ if __name__ == "__main__":
|
|||||||
print(main2())
|
print(main2())
|
||||||
print(*dihrender(main1(False)), sep="\n")
|
print(*dihrender(main1(False)), sep="\n")
|
||||||
print(render(main1()))
|
print(render(main1()))
|
||||||
|
|
||||||
+13
-14
@@ -3,15 +3,15 @@ from functools import cached_property
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
def inv(a, n):
|
def inv(a: int, n: int):
|
||||||
return pow(a, -1, n)
|
return pow(a, -1, n)
|
||||||
|
|
||||||
|
|
||||||
class LdaRsahh:
|
class LdaRsahh:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
p,
|
p: int,
|
||||||
q,
|
q: int,
|
||||||
d: int | None = None,
|
d: int | None = None,
|
||||||
e: int | None = None,
|
e: int | None = None,
|
||||||
ln_ops: Callable[[int, int], int] | None = None,
|
ln_ops: Callable[[int, int], int] | None = None,
|
||||||
@@ -23,9 +23,9 @@ class LdaRsahh:
|
|||||||
raise ValueError("at least d or e needs to be fed in")
|
raise ValueError("at least d or e needs to be fed in")
|
||||||
ln = self.ln
|
ln = self.ln
|
||||||
if e is None:
|
if e is None:
|
||||||
e = inv(d, ln)
|
e = inv(d, ln) # type: ignore
|
||||||
elif not 1 < e < ln:
|
elif not 1 < e < ln:
|
||||||
e %= ln
|
e %= ln # bad
|
||||||
if not 1 < e < ln:
|
if not 1 < e < ln:
|
||||||
raise ValueError(f"{e = }, {ln = }, 1 < e < ln not satisfied")
|
raise ValueError(f"{e = }, {ln = }, 1 < e < ln not satisfied")
|
||||||
if math.gcd(e, ln) != 1:
|
if math.gcd(e, ln) != 1:
|
||||||
@@ -38,19 +38,19 @@ class LdaRsahh:
|
|||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def n(self):
|
def n(self):
|
||||||
return self.p * self.q
|
return self._p * self._q
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def ln(self):
|
def ln(self):
|
||||||
return self._lnops((self.p - 1), (self.q - 1))
|
return self._lnops((self._p - 1), (self._q - 1))
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def p(self):
|
# def p(self):
|
||||||
return self._p
|
# return self._p
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def q(self):
|
# def q(self):
|
||||||
return self._q
|
# return self._q
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def d(self):
|
def d(self):
|
||||||
@@ -65,4 +65,3 @@ class LdaRsahh:
|
|||||||
|
|
||||||
def dec(self, c: int):
|
def dec(self, c: int):
|
||||||
return pow(c, self.d, self.n)
|
return pow(c, self.d, self.n)
|
||||||
|
|
||||||
Reference in new issue
Block a user