delete that
its in usth/crypto now
This commit is contained in:
1 parent
a455587ad0
commit
dae367bab1
5 files changed
-560
No files matched your search
-154
@@ -1,154 +0,0 @@
|
|||||||
from collections.abc import Sequence, Iterable, Iterator
|
|
||||||
from typing import Literal
|
|
||||||
from functools import reduce
|
|
||||||
from itertools import chain
|
|
||||||
from operator import xor
|
|
||||||
|
|
||||||
|
|
||||||
def maj(a, b, c):
|
|
||||||
a, b, c = bool(a), bool(b), bool(c)
|
|
||||||
return 1 * (a + b + c >= 2)
|
|
||||||
|
|
||||||
|
|
||||||
class Bih(Sequence):
|
|
||||||
def __init__(
|
|
||||||
self, length: Literal[19, 22, 23], initializer: Iterable | None = None
|
|
||||||
):
|
|
||||||
match length:
|
|
||||||
case 19:
|
|
||||||
self.m = 8
|
|
||||||
self.n = (13, 16, 17, 18)
|
|
||||||
self.g = 18
|
|
||||||
case 22:
|
|
||||||
self.m = 10
|
|
||||||
self.n = (20, 21)
|
|
||||||
self.g = 21
|
|
||||||
case 23:
|
|
||||||
self.m = 10
|
|
||||||
self.n = (7, 20, 21, 22)
|
|
||||||
self.g = 22
|
|
||||||
case _:
|
|
||||||
raise ValueError()
|
|
||||||
self.len = length
|
|
||||||
|
|
||||||
if initializer is None:
|
|
||||||
initializer = [0] * length
|
|
||||||
self.q = list(initializer)
|
|
||||||
if len(self.q) != length:
|
|
||||||
raise ValueError()
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.q)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return str(self.q)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def maj(self):
|
|
||||||
return self[self.m]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def st(self):
|
|
||||||
return self[self.g]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def element_new(self):
|
|
||||||
return tuple(self[n] for n in self.n)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def n_new(self):
|
|
||||||
return reduce(xor, self.element_new)
|
|
||||||
|
|
||||||
def roll(self):
|
|
||||||
assert len(self) == self.len
|
|
||||||
self.q = [self.n_new, *self[:-1]]
|
|
||||||
|
|
||||||
def __getitem__(self, bro):
|
|
||||||
return self.q[bro]
|
|
||||||
|
|
||||||
|
|
||||||
def dihcheese(n: int, length: int):
|
|
||||||
return map(int, f"{n:b}".zfill(length))
|
|
||||||
|
|
||||||
|
|
||||||
def dih(n: int, length: int):
|
|
||||||
return Bih(length, dihcheese(n, length))
|
|
||||||
|
|
||||||
|
|
||||||
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):
|
|
||||||
n = [int(i) for i in str(n)]
|
|
||||||
return Bih(len(n), n)
|
|
||||||
|
|
||||||
|
|
||||||
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):
|
|
||||||
a = dih(a, 19)
|
|
||||||
b = dih(b, 22)
|
|
||||||
c = dih(c, 23)
|
|
||||||
return op(a, b, c, n)
|
|
||||||
|
|
||||||
|
|
||||||
def opcih(a, b, c, n):
|
|
||||||
a = cih(a)
|
|
||||||
b = cih(b)
|
|
||||||
c = cih(c)
|
|
||||||
return op(a, b, c, n)
|
|
||||||
|
|
||||||
|
|
||||||
def main1(fahh=True):
|
|
||||||
a = dih(513365, 19)
|
|
||||||
b = dih(3355443, 22)
|
|
||||||
c = dih(7401712, 23)
|
|
||||||
d = op(a, b, c, 10)
|
|
||||||
if not fahh:
|
|
||||||
return d
|
|
||||||
return list(d)
|
|
||||||
|
|
||||||
|
|
||||||
def render(dih: Iterator[int]):
|
|
||||||
bih = " ".join(str(i) for i in reversed(list(dih)))
|
|
||||||
return f"-> {bih}"
|
|
||||||
|
|
||||||
|
|
||||||
def dihrender(dih: Iterator[int]):
|
|
||||||
ar = "->"
|
|
||||||
cih = []
|
|
||||||
for i in dih:
|
|
||||||
cih.append(str(i))
|
|
||||||
yield " ".join(chain((ar,), reversed(cih)))
|
|
||||||
|
|
||||||
|
|
||||||
def main2():
|
|
||||||
e = list(
|
|
||||||
opcih(
|
|
||||||
1010101010101010101,
|
|
||||||
1100110011001100110011,
|
|
||||||
11100001111000011110000,
|
|
||||||
8,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return e
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print(main1())
|
|
||||||
print(main2())
|
|
||||||
print(*dihrender(main1(False)), sep="\n")
|
|
||||||
print(render(main1()))
|
|
||||||
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
def e1(eh=True):
|
|
||||||
a = 123
|
|
||||||
b = 543
|
|
||||||
ab = a ^ b
|
|
||||||
if not eh:
|
|
||||||
return a, b, ab
|
|
||||||
return f"{a = :b}, {b = :b}, {a^b = :b} ({a ^ b})"
|
|
||||||
|
|
||||||
|
|
||||||
def e2():
|
|
||||||
a = [
|
|
||||||
105,
|
|
||||||
110,
|
|
||||||
116,
|
|
||||||
114,
|
|
||||||
111,
|
|
||||||
50,
|
|
||||||
99,
|
|
||||||
114,
|
|
||||||
121,
|
|
||||||
112,
|
|
||||||
116,
|
|
||||||
111,
|
|
||||||
123,
|
|
||||||
119,
|
|
||||||
51,
|
|
||||||
108,
|
|
||||||
99,
|
|
||||||
48,
|
|
||||||
109,
|
|
||||||
51,
|
|
||||||
95,
|
|
||||||
116,
|
|
||||||
48,
|
|
||||||
95,
|
|
||||||
106,
|
|
||||||
48,
|
|
||||||
117,
|
|
||||||
114,
|
|
||||||
110,
|
|
||||||
51,
|
|
||||||
121,
|
|
||||||
125,
|
|
||||||
]
|
|
||||||
|
|
||||||
return "".join(map(chr, a))
|
|
||||||
|
|
||||||
|
|
||||||
def e3():
|
|
||||||
b = [
|
|
||||||
99,
|
|
||||||
100,
|
|
||||||
126,
|
|
||||||
120,
|
|
||||||
101,
|
|
||||||
56,
|
|
||||||
105,
|
|
||||||
120,
|
|
||||||
115,
|
|
||||||
122,
|
|
||||||
126,
|
|
||||||
101,
|
|
||||||
113,
|
|
||||||
59,
|
|
||||||
121,
|
|
||||||
126,
|
|
||||||
57,
|
|
||||||
122,
|
|
||||||
85,
|
|
||||||
58,
|
|
||||||
100,
|
|
||||||
85,
|
|
||||||
96,
|
|
||||||
58,
|
|
||||||
127,
|
|
||||||
120,
|
|
||||||
100,
|
|
||||||
57,
|
|
||||||
115,
|
|
||||||
119,
|
|
||||||
]
|
|
||||||
|
|
||||||
k = 10
|
|
||||||
|
|
||||||
return "".join(map(lambda n: chr(n ^ k), b))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import a51
|
|
||||||
|
|
||||||
print("#1:", e1())
|
|
||||||
print("#2:", e2())
|
|
||||||
print("#3:", e3())
|
|
||||||
print("#4:", a51.render(a51.main1()))
|
|
||||||
|
|
||||||
-198
@@ -1,198 +0,0 @@
|
|||||||
from math import gcd as mathgcd
|
|
||||||
from itertools import combinations
|
|
||||||
from functools import wraps
|
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
# home grown gcd
|
|
||||||
def gcd(a, b):
|
|
||||||
return a if b == 0 else gcd(b, a % b)
|
|
||||||
|
|
||||||
|
|
||||||
def cooked(s: set[int]):
|
|
||||||
for a, b in combinations(s, 2):
|
|
||||||
if mathgcd(a, b) == 1:
|
|
||||||
yield a, b
|
|
||||||
|
|
||||||
|
|
||||||
printingTs = False
|
|
||||||
|
|
||||||
|
|
||||||
def printTs(f):
|
|
||||||
@wraps(f)
|
|
||||||
def ff(*a, **kw):
|
|
||||||
if printingTs:
|
|
||||||
if a:
|
|
||||||
print(*a)
|
|
||||||
if kw:
|
|
||||||
print(*kw.items())
|
|
||||||
r = f(*a, *kw)
|
|
||||||
if printingTs:
|
|
||||||
print(r)
|
|
||||||
return r
|
|
||||||
|
|
||||||
return ff
|
|
||||||
|
|
||||||
|
|
||||||
@printTs
|
|
||||||
def smx(x, e, n, r=1): # this is not it cuh
|
|
||||||
return (
|
|
||||||
smx(inv(x, n), -e, n, r)
|
|
||||||
if e < 0
|
|
||||||
else r
|
|
||||||
if e == 0
|
|
||||||
else smx(x * x % n, e >> 1, n, r * x % n if e & 1 else r)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def realsmx(x, e, n):
|
|
||||||
r = x
|
|
||||||
for bro in map(int, bin(e)[3:]):
|
|
||||||
r = (r * r) % n
|
|
||||||
if bro:
|
|
||||||
r = (r * x) % n
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
@printTs
|
|
||||||
def theREALsmx(x, e, n):
|
|
||||||
"locked in"
|
|
||||||
return (
|
|
||||||
theREALsmx(inv(x, n), -e, n)
|
|
||||||
if e < 0
|
|
||||||
else x**e
|
|
||||||
if e <= 1
|
|
||||||
else theREALsmx(x, e >> 1, n) ** 2 % n
|
|
||||||
if not e & 1
|
|
||||||
else (x * theREALsmx(x, e & ~1, n)) % n
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def realest(x=None, y=None, n=None):
|
|
||||||
x = x or random.randint(-10e9, 10e9)
|
|
||||||
y = y or random.randint(-10e9, 10e9)
|
|
||||||
n = n or random.randint(2, 10e9)
|
|
||||||
# print(x, y, n)
|
|
||||||
if y < 0:
|
|
||||||
try:
|
|
||||||
p = pow(x, -1, n)
|
|
||||||
except ValueError:
|
|
||||||
p = None
|
|
||||||
else:
|
|
||||||
assert (p * x) % n == 1, f"{p = }, {(p*x)%n = }"
|
|
||||||
try:
|
|
||||||
i = inv(x, n)
|
|
||||||
except ValueError:
|
|
||||||
i = None
|
|
||||||
else:
|
|
||||||
assert (i * x) % n == 1, f"{i = }, {(i*x)%n = }"
|
|
||||||
|
|
||||||
assert i == p, f"{i = }, {p = }"
|
|
||||||
if gcd(x, n) != 1:
|
|
||||||
return # not doing ts
|
|
||||||
|
|
||||||
p = pow(x, y, n)
|
|
||||||
s = smx(x, y, n)
|
|
||||||
t = theREALsmx(x, y, n)
|
|
||||||
assert p == s == t, f"{p = }, {s = }, {t = }"
|
|
||||||
|
|
||||||
|
|
||||||
def xgcd(a, b) -> tuple[int, tuple[int, int]]:
|
|
||||||
"answer at index 0, wiki ripoff"
|
|
||||||
s, ps, r, pr = 0, 1, b, a
|
|
||||||
# shit = 0
|
|
||||||
while r != 0:
|
|
||||||
# shit += 1
|
|
||||||
# if shit > 1000:
|
|
||||||
# raise RecursionError("SHIT")
|
|
||||||
q = pr // r
|
|
||||||
pr, r = r, (pr - q * r)
|
|
||||||
ps, s = s, (ps - q * s)
|
|
||||||
bt = 0 if b == 0 else (pr - ps * a) % b
|
|
||||||
return (pr, (ps, bt))
|
|
||||||
|
|
||||||
|
|
||||||
def inv(a, n):
|
|
||||||
"also wiki ripoff"
|
|
||||||
t, nt, r, nr = 0, 1, n, a % n
|
|
||||||
# shit = 0
|
|
||||||
while nr != 0:
|
|
||||||
# shit += 1
|
|
||||||
# if shit > 1000:
|
|
||||||
# raise RecursionError("SHIT")
|
|
||||||
q = r // nr
|
|
||||||
r, nr = nr, (r - q * nr)
|
|
||||||
t, nt = nt, (t - q * nt)
|
|
||||||
# print(t, nt, r, nr)
|
|
||||||
if r > 1:
|
|
||||||
raise ValueError("base is not invertible for the given modulus")
|
|
||||||
if t < 0:
|
|
||||||
return t + n
|
|
||||||
return t
|
|
||||||
|
|
||||||
|
|
||||||
def fahhh(x, e, n):
|
|
||||||
"only works for this specific thing"
|
|
||||||
eb = f"{e:b}"
|
|
||||||
print(f"x^H mod n = {x}^{e} mod {n}")
|
|
||||||
print()
|
|
||||||
print(f"H = {e}_10 = {eb}_2")
|
|
||||||
print(
|
|
||||||
",\n".join(reversed([f"h_{i} = {ie}_2" for i, ie in enumerate(reversed(eb))]))
|
|
||||||
)
|
|
||||||
r = x
|
|
||||||
ee = 1
|
|
||||||
print()
|
|
||||||
print(f"x = x^{{{eb[0]}_2}}")
|
|
||||||
print(f"h_{len(eb)-1} = {eb[0]}")
|
|
||||||
print()
|
|
||||||
print(f"r <- {x = }")
|
|
||||||
print()
|
|
||||||
for ie, i in zip(range(len(eb) - 2, -1, -1), eb[1:]):
|
|
||||||
print(f"(x^{ee})^2 = x^{2*ee} = x^{{{2*ee:b}_2}}")
|
|
||||||
print(f"SQ, h_{ie}")
|
|
||||||
print()
|
|
||||||
ee *= 2
|
|
||||||
r *= r
|
|
||||||
print(f"r <- r*r = {r}")
|
|
||||||
print(f"r <- r mod n = {r%n}")
|
|
||||||
r %= n
|
|
||||||
print()
|
|
||||||
if int(i):
|
|
||||||
print(f"x^{ee}*x = x^{ee+1} = x^{{{ee+1:b}_2}}")
|
|
||||||
print(f"MUL, h_{ie} = 1")
|
|
||||||
print()
|
|
||||||
ee += 1
|
|
||||||
r *= x
|
|
||||||
print(f"r <- r * x = {r}")
|
|
||||||
print(f"r <- r mod n = {r%n}")
|
|
||||||
r %= n
|
|
||||||
else:
|
|
||||||
print(f"h_{ie} = 0")
|
|
||||||
print()
|
|
||||||
print(f"x^H mod n = {x}^{e} mod {n} = {r}")
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("#1:", f"{gcd(52698, 61430) = }")
|
|
||||||
E2 = {
|
|
||||||
290345,
|
|
||||||
218585,
|
|
||||||
143231,
|
|
||||||
164172,
|
|
||||||
155768,
|
|
||||||
423151,
|
|
||||||
239707,
|
|
||||||
153544,
|
|
||||||
287390,
|
|
||||||
480837,
|
|
||||||
}
|
|
||||||
dih = [f"gcd({m}, {n}) = {gcd(m,n)}" for m, n in cooked(E2)]
|
|
||||||
print("#2:", *dih, sep="\n")
|
|
||||||
print(f"pairs = {len(dih)}")
|
|
||||||
print("#3:")
|
|
||||||
fahhh(856, 25, 7)
|
|
||||||
print(f"{pow(856, 25, 7) = }")
|
|
||||||
print("#4:", f"{inv(65537, 35256) = }")
|
|
||||||
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
def e1():
|
|
||||||
s = "flow"
|
|
||||||
|
|
||||||
return int(bytes(s, encoding="ascii").hex(), 16)
|
|
||||||
|
|
||||||
|
|
||||||
def dec_ascii(i: int):
|
|
||||||
b = bytes.fromhex(f"{i:x}")
|
|
||||||
return b.decode("ascii")
|
|
||||||
|
|
||||||
|
|
||||||
def e2():
|
|
||||||
i = 14445
|
|
||||||
return dec_ascii(i)
|
|
||||||
|
|
||||||
|
|
||||||
from proto_rsa import LdaRsahh
|
|
||||||
|
|
||||||
|
|
||||||
def e3():
|
|
||||||
a = LdaRsahh(p=251, q=191, e=65537).enc(26729)
|
|
||||||
|
|
||||||
b = LdaRsahh(p=233, q=151, d=9473).dec(29600)
|
|
||||||
|
|
||||||
return a, b
|
|
||||||
|
|
||||||
|
|
||||||
def e4():
|
|
||||||
C = 412589464
|
|
||||||
M = LdaRsahh(p=81401, q=27109, e=65537).dec(C)
|
|
||||||
return dec_ascii(M)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("#1:", e1())
|
|
||||||
print("#2:", e2())
|
|
||||||
C, M = e3()
|
|
||||||
print("#3:", f"{C = }, {M = }")
|
|
||||||
print("#4:", e4())
|
|
||||||
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
def inv(a, n):
|
|
||||||
return pow(a, -1, n)
|
|
||||||
|
|
||||||
|
|
||||||
import math
|
|
||||||
from functools import cached_property
|
|
||||||
from typing import Callable
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
|
|
||||||
class LdaRsahh:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
p,
|
|
||||||
q,
|
|
||||||
d: int | None = None,
|
|
||||||
e: int | None = None,
|
|
||||||
ln_ops: Callable[[int, int], int] | None = None,
|
|
||||||
):
|
|
||||||
# operator.mul or math.lcm
|
|
||||||
self._lnops = ln_ops or math.lcm
|
|
||||||
self._p, self._q = p, q
|
|
||||||
if d is None and e is None:
|
|
||||||
raise ValueError("at least d or e needs to be math.gcd(e, phi)fed in")
|
|
||||||
ln = self.ln
|
|
||||||
if e is None:
|
|
||||||
e = inv(d, ln)
|
|
||||||
if not 1 < e < ln:
|
|
||||||
warnings.warn(
|
|
||||||
f"{e = }, {ln = }, 1 < e < ln not satisfied, e rebinds to e % ln",
|
|
||||||
UserWarning,
|
|
||||||
)
|
|
||||||
e %= ln
|
|
||||||
if not 1 < e < ln:
|
|
||||||
raise ValueError(f"{e = }, {ln = }, 1 < e < ln not satisfied")
|
|
||||||
if math.gcd(e, ln) != 1:
|
|
||||||
raise ValueError(
|
|
||||||
f"{e = }, {ln = }, gcd(e, ln) == 1 (coprime) not satisfied"
|
|
||||||
)
|
|
||||||
if d is None:
|
|
||||||
d = inv(e, ln)
|
|
||||||
self._d, self._e = d, e
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def n(self):
|
|
||||||
return self.p * self.q
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def ln(self):
|
|
||||||
return self._lnops((self.p - 1), (self.q - 1))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def p(self):
|
|
||||||
return self._p
|
|
||||||
|
|
||||||
@property
|
|
||||||
def q(self):
|
|
||||||
return self._q
|
|
||||||
|
|
||||||
@property
|
|
||||||
def d(self):
|
|
||||||
return self._d
|
|
||||||
|
|
||||||
@property
|
|
||||||
def e(self):
|
|
||||||
return self._e
|
|
||||||
|
|
||||||
def enc(self, m: int):
|
|
||||||
return pow(m, self.e, self.n)
|
|
||||||
|
|
||||||
def dec(self, c: int):
|
|
||||||
return pow(c, self.d, self.n)
|
|
||||||
|
|
||||||
Reference in new issue
Block a user