changeset 1339:99ae184a2674

paillier: Optimized decryption by caching.
author Marcel Keller <mkeller@cs.au.dk>
date Tue, 27 Oct 2009 19:59:21 +0100
parents c7403cb56ace
children 614c32bfe8fb
files viff/paillier.py
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/viff/paillier.py	Tue Oct 27 19:25:33 2009 +0100
+++ b/viff/paillier.py	Tue Oct 27 19:59:21 2009 +0100
@@ -62,10 +62,19 @@
     nsq = n*n
     return (pow(g, m, nsq)*pow(r, n, nsq)) % nsq
 
+#: Cache for ciphertext-independent factors.
+_decrypt_factors = {}
+
 def decrypt(c, (n, g, lm)):
     numer = L(pow(c, lm, n*n), n)
-    denom = L(pow(g, lm, n*n), n)
-    return (numer*gmpy.invert(denom, n)) % n
+    key = (n, g, lm)
+    try:
+        factor = _decrypt_factors[key]
+    except KeyError:
+        denom = L(pow(g, lm, n*n), n)
+        factor = gmpy.invert(denom, n)
+        _decrypt_factors[key] = factor
+    return (numer * factor) % n
 
 
 class PaillierRuntime(Runtime):