viff

changeset 1028:54c25924882a

Convert PRF input using str. Using pack leads to different output on 32-bit and 64-bit machines.
author Martin Geisler <mg@daimi.au.dk>
date Fri, 21 Nov 2008 13:08:52 +0100
parents 38798af42814
children e31181b3c703 6f5c481e70f1
files viff/prss.py
diffstat 1 files changed, 13 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/viff/prss.py	Fri Nov 21 12:30:18 2008 +0100
     1.2 +++ b/viff/prss.py	Fri Nov 21 13:08:52 2008 +0100
     1.3 @@ -45,7 +45,6 @@
     1.4  
     1.5  import sha
     1.6  from math import ceil
     1.7 -from struct import pack
     1.8  from binascii import hexlify
     1.9  
    1.10  from gmpy import numdigits
    1.11 @@ -156,11 +155,11 @@
    1.12      ...         frozenset([1,3]): PRF("b", 7),
    1.13      ...         frozenset([2,3]): PRF("c", 7)}
    1.14      >>> prss_zero(3, 1, 1, Zp, prfs, "key")
    1.15 -    {4}
    1.16 +    {16}
    1.17      >>> prss_zero(3, 1, 2, Zp, prfs, "key")
    1.18 -    {0}
    1.19 +    {13}
    1.20      >>> prss_zero(3, 1, 3, Zp, prfs, "key")
    1.21 -    {11}
    1.22 +    {14}
    1.23  
    1.24      If we recombine 2t + 1 = 3 shares we can verify that this is
    1.25      indeed a zero-sharing:
    1.26 @@ -250,11 +249,11 @@
    1.27      Calling f return values between zero and the given maximum:
    1.28  
    1.29      >>> f(1)
    1.30 -    246L
    1.31 +    26L
    1.32      >>> f(2)
    1.33 -    254L
    1.34 +    69L
    1.35      >>> f(3)
    1.36 -    13L
    1.37 +    217L
    1.38      """
    1.39  
    1.40      def __init__(self, key, max):
    1.41 @@ -334,30 +333,27 @@
    1.42  
    1.43          >>> prf = PRF("key", 1000)
    1.44          >>> prf(1), prf(2), prf(3)
    1.45 -        (714L, 80L, 617L)
    1.46 +        (501L, 432L, 133L)
    1.47  
    1.48          Since prf is a function we can of course evaluate the same
    1.49          input to get the same output:
    1.50  
    1.51          >>> prf(1)
    1.52 -        714L
    1.53 +        501L
    1.54  
    1.55          The prf can take arbitrary input:
    1.56  
    1.57          >>> prf(("input", 123))
    1.58 -        474L
    1.59 +        284L
    1.60  
    1.61 -        but it must be hashable:
    1.62 -
    1.63 -        >>> prf(["input", 123])   # doctest: +IGNORE_EXCEPTION_DETAIL
    1.64 -        Traceback (most recent call last):
    1.65 -            ...
    1.66 -        TypeError: list objects are unhashable
    1.67 +        Non-string input will be converted with ``str``, which means
    1.68 +        that the input must have a deterministic ``__str__``
    1.69 +        method. This means that hashable instances are probably best.
    1.70          """
    1.71          # We can only feed str data to sha1 instance, so we must
    1.72          # convert the input.
    1.73          if not isinstance(input, str):
    1.74 -            input = pack("l", hash(input))
    1.75 +            input = str(input)
    1.76  
    1.77          # There is a chance that we generate a number that is too big,
    1.78          # so we must keep trying until we succeed.