Skip to content

Commit

Permalink
Use Chinese Remainder Theorem when decrypting with private key
Browse files Browse the repository at this point in the history
Use the Chinese Remainder Theorem when decrypting with private key, as that
makes the decryption 2-4x faster.

This fixes #163.
  • Loading branch information
sybrenstuvel committed Mar 29, 2021
1 parent 35e962d commit 483700a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- Added marker file for PEP 561. This will allow type checking tools in dependent projects
to use type annotations from Python-RSA
([#136](https://github.com/sybrenstuvel/python-rsa/pull/136)).
- Use the Chinese Remainder Theorem when decrypting with a private key. This
makes decryption 2-4x faster
([#163](https://github.com/sybrenstuvel/python-rsa/pull/163)).

## Version 4.7.2 - released 2021-02-24

Expand Down
11 changes: 10 additions & 1 deletion rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,16 @@ def blinded_decrypt(self, encrypted: int) -> int:

# Blinding and un-blinding should be using the same factor
blinded, blindfac_inverse = self.blind(encrypted)
decrypted = rsa.core.decrypt_int(blinded, self.d, self.n)

# Instead of using the core functionality, use the Chinese Remainder
# Theorem and be 2-4x faster. This the same as:
#
# decrypted = rsa.core.decrypt_int(blinded, self.d, self.n)
s1 = pow(blinded, self.exp1, self.p)
s2 = pow(blinded, self.exp2, self.q)
h = ((s1 - s2) * self.coef) % self.p
decrypted = s2 + self.q * h

return self.unblind(decrypted, blindfac_inverse)

def blinded_encrypt(self, message: int) -> int:
Expand Down

0 comments on commit 483700a

Please sign in to comment.