viff

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 diff
     1.1 --- a/viff/paillier.py	Tue Oct 27 19:25:33 2009 +0100
     1.2 +++ b/viff/paillier.py	Tue Oct 27 19:59:21 2009 +0100
     1.3 @@ -62,10 +62,19 @@
     1.4      nsq = n*n
     1.5      return (pow(g, m, nsq)*pow(r, n, nsq)) % nsq
     1.6  
     1.7 +#: Cache for ciphertext-independent factors.
     1.8 +_decrypt_factors = {}
     1.9 +
    1.10  def decrypt(c, (n, g, lm)):
    1.11      numer = L(pow(c, lm, n*n), n)
    1.12 -    denom = L(pow(g, lm, n*n), n)
    1.13 -    return (numer*gmpy.invert(denom, n)) % n
    1.14 +    key = (n, g, lm)
    1.15 +    try:
    1.16 +        factor = _decrypt_factors[key]
    1.17 +    except KeyError:
    1.18 +        denom = L(pow(g, lm, n*n), n)
    1.19 +        factor = gmpy.invert(denom, n)
    1.20 +        _decrypt_factors[key] = factor
    1.21 +    return (numer * factor) % n
    1.22  
    1.23  
    1.24  class PaillierRuntime(Runtime):