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

Commit 483700a

Browse files
committed
Use Chinese Remainder Theorem when decrypting with private key
Use the Chinese Remainder Theorem when decrypting with private key, as that makes the decryption 2-4x faster. This fixes #163.
1 parent 35e962d commit 483700a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
- Added marker file for PEP 561. This will allow type checking tools in dependent projects
99
to use type annotations from Python-RSA
1010
([#136](https://github.com/sybrenstuvel/python-rsa/pull/136)).
11+
- Use the Chinese Remainder Theorem when decrypting with a private key. This
12+
makes decryption 2-4x faster
13+
([#163](https://github.com/sybrenstuvel/python-rsa/pull/163)).
1114

1215
## Version 4.7.2 - released 2021-02-24
1316

rsa/key.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,16 @@ def blinded_decrypt(self, encrypted: int) -> int:
473473

474474
# Blinding and un-blinding should be using the same factor
475475
blinded, blindfac_inverse = self.blind(encrypted)
476-
decrypted = rsa.core.decrypt_int(blinded, self.d, self.n)
476+
477+
# Instead of using the core functionality, use the Chinese Remainder
478+
# Theorem and be 2-4x faster. This the same as:
479+
#
480+
# decrypted = rsa.core.decrypt_int(blinded, self.d, self.n)
481+
s1 = pow(blinded, self.exp1, self.p)
482+
s2 = pow(blinded, self.exp2, self.q)
483+
h = ((s1 - s2) * self.coef) % self.p
484+
decrypted = s2 + self.q * h
485+
477486
return self.unblind(decrypted, blindfac_inverse)
478487

479488
def blinded_encrypt(self, message: int) -> int:

0 commit comments

Comments
 (0)