yoooo
This commit is contained in:
1 parent
353db5f566
commit
c91740669a
4 files changed
+50
-18
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
__pycache__/
|
||||||
Whitespace-only changes.
@@ -0,0 +1,28 @@
|
|||||||
|
class Person:
|
||||||
|
def __init__(self, p: int, g: int, k: int):
|
||||||
|
self.p, self.g, self.k = p, g, k
|
||||||
|
self.s = None
|
||||||
|
|
||||||
|
def public_key(self, recieved: int | None = None) -> int:
|
||||||
|
return pow(recieved or self.g, self.k, self.p)
|
||||||
|
|
||||||
|
def recieve_public_key(self, K: int) -> int:
|
||||||
|
self.s = self.public_key(K)
|
||||||
|
return self.s
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
p = 23
|
||||||
|
g = 5
|
||||||
|
alice = Person(p, g, 4)
|
||||||
|
bob = Person(p, g, 3)
|
||||||
|
assert (
|
||||||
|
alice.recieve_public_key(bob.public_key())
|
||||||
|
== bob.recieve_public_key(alice.public_key())
|
||||||
|
== 18
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
print("done")
|
||||||
+21
-18
@@ -13,20 +13,29 @@ class LdaRSA:
|
|||||||
p: int,
|
p: int,
|
||||||
q: int,
|
q: int,
|
||||||
*,
|
*,
|
||||||
d: int | None = None,
|
|
||||||
e: int | None = None,
|
e: int | None = None,
|
||||||
|
d: int | None = None,
|
||||||
ln_ops: Callable[[int, int], int] | None = None,
|
ln_ops: Callable[[int, int], int] | None = None,
|
||||||
):
|
):
|
||||||
|
"""
|
||||||
|
p and q is Prime.
|
||||||
|
|
||||||
|
ln = lcm(p-1,q-1)
|
||||||
|
|
||||||
|
e needs to be in range (1, ln) and be coprime with ln, and is the public key.
|
||||||
|
|
||||||
|
d s.t. d*e === 1 mod ln is the private key
|
||||||
|
"""
|
||||||
# operator.mul or math.lcm
|
# operator.mul or math.lcm
|
||||||
self._lnops: Callable[[int, int], int] = ln_ops or math.lcm
|
self._lnops: Callable[[int, int], int] = ln_ops or math.lcm
|
||||||
self._p, self._q = p, q
|
self.__p, self.__q = p, q
|
||||||
if d is None and e is None:
|
if d is None and e is None:
|
||||||
raise ValueError("at least d or e needs to be fed in")
|
raise ValueError("at least e or d needs to be fed in")
|
||||||
ln = self.ln
|
ln = self.ln
|
||||||
if e is None:
|
if e is None:
|
||||||
e = inv(d, ln) # type: ignore
|
e = inv(d, ln) # type: ignore
|
||||||
elif not 1 < e < ln:
|
# elif not 1 < e < ln:
|
||||||
e %= ln # bad
|
# 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:
|
||||||
@@ -35,34 +44,28 @@ class LdaRSA:
|
|||||||
)
|
)
|
||||||
if d is None:
|
if d is None:
|
||||||
d = inv(e, ln)
|
d = inv(e, ln)
|
||||||
self._d, self._e = d, e
|
self.__d, self.__e = d, e
|
||||||
|
|
||||||
@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
|
|
||||||
# def p(self):
|
|
||||||
# return self._p
|
|
||||||
|
|
||||||
# @property
|
|
||||||
# def q(self):
|
|
||||||
# return self._q
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def d(self):
|
def d(self):
|
||||||
return self._d
|
return self.__d
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def e(self):
|
def e(self):
|
||||||
return self._e
|
return self.__e
|
||||||
|
|
||||||
def enc(self, m: int):
|
def enc(self, m: int):
|
||||||
|
"encode message m"
|
||||||
return pow(m, self.e, self.n)
|
return pow(m, self.e, self.n)
|
||||||
|
|
||||||
def dec(self, c: int):
|
def dec(self, c: int):
|
||||||
|
"decode encoded msg c"
|
||||||
return pow(c, self.d, self.n)
|
return pow(c, self.d, self.n)
|
||||||
Reference in new issue
Block a user