Skip to content
This repository was archived by the owner on Apr 20, 2025. It is now read-only.

Commit 832b720

Browse files
committed
multiprime support
added fast CRT-based decryption to core added multiprime key support correction (see issue #205, PR #206) added multiprime tests
1 parent c4dc7be commit 832b720

File tree

6 files changed

+422
-70
lines changed

6 files changed

+422
-70
lines changed

rsa/core.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
This is the actual core RSA implementation, which is only defined
1818
mathematically on integers.
1919
"""
20+
import itertools
21+
import typing
2022

2123

2224
def assert_int(var: int, name: str) -> None:
@@ -51,3 +53,37 @@ def decrypt_int(cyphertext: int, dkey: int, n: int) -> int:
5153

5254
message = pow(cyphertext, dkey, n)
5355
return message
56+
57+
58+
def decrypt_int_fast(
59+
cyphertext: int,
60+
rs: typing.List[int],
61+
ds: typing.List[int],
62+
ts: typing.List[int],
63+
) -> int:
64+
"""Decrypts a cypher text more quickly using the Chinese Remainder Theorem."""
65+
66+
assert_int(cyphertext, "cyphertext")
67+
for r in rs:
68+
assert_int(r, "r")
69+
for d in ds:
70+
assert_int(d, "d")
71+
for t in ts:
72+
assert_int(t, "t")
73+
74+
p, q, rs = rs[0], rs[1], rs[2:]
75+
exp1, exp2, ds = ds[0], ds[1], ds[2:]
76+
coef, ts = ts[0], ts[1:]
77+
78+
M1 = pow(cyphertext, exp1, p)
79+
M2 = pow(cyphertext, exp2, q)
80+
h = ((M1 - M2) * coef) % p
81+
m = M2 + q * h
82+
83+
Ms = [pow(cyphertext, d, r) for d, r in zip(ds, rs)]
84+
Rs = list(itertools.accumulate([p, q] + rs, lambda x, y: x*y))
85+
for R, r, M, t in zip(Rs[1:], rs, Ms, ts):
86+
h = ((M - m) * t) % r
87+
m += R * h
88+
89+
return m

0 commit comments

Comments
 (0)