viff

changeset 1357:ea45925aa060

Merged with Marcel.
author Janus Dam Nielsen Wed, 28 Oct 2009 07:05:35 +0100 c467cba12cfb eb9f159eea68 712cbf4c2e1b 5 files changed, 31 insertions(+), 22 deletions(-) [+]
line diff
```     1.1 --- a/apps/aes.py	Tue Oct 27 22:24:56 2009 +0100
1.2 +++ b/apps/aes.py	Wed Oct 28 07:05:35 2009 +0100
1.3 @@ -125,9 +125,9 @@
1.4
1.5      if options.active:
1.6          if options.exponentiation is False:
1.7 -            max = 461
1.8 -            js = [3 + i * 23 + j for i in range(20)
1.9 -                  for j in range(0, 14, 2) + [15]]
1.10 +            max = 621
1.11 +            js = [3 + i * 31 + j for i in range(20)
1.12 +                  for j in range(0, 21, 3) + [22]]
1.13          elif options.exponentiation == 0 or options.exponentiation == 3:
1.14              max = 821
1.15              js = [1 + i * 41 + j * 3 for i in range(20) for j in range(13)]
```
```     2.1 --- a/viff/active.py	Tue Oct 27 22:24:56 2009 +0100
2.2 +++ b/viff/active.py	Wed Oct 28 07:05:35 2009 +0100
2.3 @@ -19,7 +19,9 @@
2.4
2.5  from math import ceil
2.6
2.7 -from twisted.internet.defer import gatherResults, Deferred, succeed
2.8 +from gmpy import numdigits
2.9 +
2.10 +from twisted.internet.defer import gatherResults, Deferred
2.11
2.12  from viff import shamir
2.13  from viff.util import rand
2.14 @@ -419,7 +421,7 @@
2.15          result = self.generate_triples(field, quantity=1, gather=False)
2.16          return result[0]
2.17
2.18 -    def generate_triples(self, field, quantity=20, gather=True):
2.19 +    def generate_triples(self, field, quantity=1, gather=True):
2.20          """Generate *quantity* multiplication triples using PRSS.
2.21
2.22          These are random numbers *a*, *b*, and *c* such that ``c =
2.23 @@ -428,7 +430,9 @@
2.24          Returns a tuple with the number of triples generated and a
2.25          Deferred which will yield a singleton-list with a 3-tuple.
2.26          """
2.27 -        quantity = min(quantity, 20)
2.28 +
2.29 +        # This adjusted to the PRF based on SHA1 (160 bits).
2.30 +        quantity = min(quantity, max(int(160 /numdigits(field.modulus - 1, 2)), 1))
2.31
2.32          a_t = self.prss_share_random_multi(field, quantity)
2.33          b_t = self.prss_share_random_multi(field, quantity)
2.34 @@ -470,19 +474,15 @@
2.35          Preprocessing: 1 multiplication triple.
2.36          Communication: 2 openings.
2.37          """
2.38 -        assert isinstance(share_x, Share) or isinstance(share_y, Share), \
2.39 -            "At least one of share_x and share_y must be a Share."
2.40 +        assert isinstance(share_x, Share), \
2.41 +            "share_x must be a Share."
2.42
2.43 -        if not isinstance(share_x, Share):
2.44 -            # Then share_y must be a Share => local multiplication. We
2.45 -            # clone first to avoid changing share_y.
2.46 -            result = share_y.clone()
2.47 -            result.addCallback(lambda y: share_x * y)
2.48 -            return result
2.49          if not isinstance(share_y, Share):
2.50 -            # Likewise when share_y is a constant.
2.51 +            # Local multiplication. share_x always is a Share by
2.52 +            # operator overloading in Share. We clone share_x first
2.53 +            # to avoid changing it.
2.54              result = share_x.clone()
2.55 -            result.addCallback(lambda x: x * share_y)
2.56 +            result.addCallback(lambda x: share_y * x)
2.57              return result
2.58
2.59          # At this point both share_x and share_y must be Share
```
```     3.1 --- a/viff/paillier.py	Tue Oct 27 22:24:56 2009 +0100
3.2 +++ b/viff/paillier.py	Wed Oct 28 07:05:35 2009 +0100
3.3 @@ -62,10 +62,19 @@
3.4      nsq = n*n
3.5      return (pow(g, m, nsq)*pow(r, n, nsq)) % nsq
3.6
3.7 +#: Cache for ciphertext-independent factors.
3.8 +_decrypt_factors = {}
3.9 +
3.10  def decrypt(c, (n, g, lm)):
3.11      numer = L(pow(c, lm, n*n), n)
3.12 -    denom = L(pow(g, lm, n*n), n)
3.13 -    return (numer*gmpy.invert(denom, n)) % n
3.14 +    key = (n, g, lm)
3.15 +    try:
3.16 +        factor = _decrypt_factors[key]
3.17 +    except KeyError:
3.18 +        denom = L(pow(g, lm, n*n), n)
3.19 +        factor = gmpy.invert(denom, n)
3.20 +        _decrypt_factors[key] = factor
3.21 +    return (numer * factor) % n
3.22
3.23
3.24  class PaillierRuntime(Runtime):
```
```     4.1 --- a/viff/passive.py	Tue Oct 27 22:24:56 2009 +0100
4.2 +++ b/viff/passive.py	Wed Oct 28 07:05:35 2009 +0100
4.3 @@ -27,7 +27,7 @@
4.4  from viff.field import GF256, FieldElement
4.5  from viff.util import rand, profile
4.6
4.7 -from twisted.internet.defer import succeed, gatherResults
4.8 +from twisted.internet.defer import gatherResults
4.9
4.10
4.11  class PassiveRuntime(Runtime):
```
```     5.1 --- a/viff/runtime.py	Tue Oct 27 22:24:56 2009 +0100
5.2 +++ b/viff/runtime.py	Wed Oct 28 07:05:35 2009 +0100
5.3 @@ -39,14 +39,14 @@
5.4  import sys
5.5
5.6  from viff.field import GF256, FieldElement
5.7 -from viff.util import wrapper, rand, deep_wait, track_memory_usage, begin, end
5.8 +from viff.util import wrapper, rand, track_memory_usage, begin, end
5.9  from viff.constants import SHARE
5.10  import viff.reactor
5.11
5.12  from twisted.internet import reactor
5.13  from twisted.internet.task import LoopingCall
5.14  from twisted.internet.error import ConnectionDone, CannotListenError
5.15 -from twisted.internet.defer import Deferred, DeferredList, gatherResults, succeed
5.16 +from twisted.internet.defer import Deferred, DeferredList, gatherResults
5.17  from twisted.internet.defer import maybeDeferred
5.18  from twisted.internet.protocol import ReconnectingClientFactory, ServerFactory
5.19  from twisted.protocols.basic import Int16StringReceiver
5.20 @@ -950,7 +950,7 @@
5.21          # We must include at least one new-style class in bases. We
5.22          # include it last to avoid overriding __init__ from the other
5.23          # base classes.
5.24 -        bases = (runtime_class,) + tuple(mixins) + (object,)
5.25 +        bases = tuple(mixins) + (runtime_class, object)
5.26          return type("ExtendedRuntime", bases, {})
5.27
5.28  def create_runtime(id, players, threshold, options=None, runtime_class=None):
```