viff

changeset 1451:25c57c4f6947

BeDOZa: Added infrastructure for better handling of keys.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 05 Jul 2010 15:46:31 +0200
parents d910c8a78a2e
children b21e08471eb4
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 59 insertions(+), 44 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza.py	Mon Jul 05 11:55:20 2010 +0200
     1.2 +++ b/viff/bedoza.py	Mon Jul 05 15:46:31 2010 +0200
     1.3 @@ -45,47 +45,69 @@
     1.4      that runtime.
     1.5      """
     1.6  
     1.7 -    def __init__(self, runtime, field, value=None, authentication_codes=None):
     1.8 +    def __init__(self, runtime, field, value=None, keyList=None, authentication_codes=None):
     1.9          self.share = value
    1.10 +        self.keyList = keyList
    1.11          self.authentication_codes = authentication_codes
    1.12 -        Share.__init__(self, runtime, field, (value, authentication_codes))
    1.13 +        Share.__init__(self, runtime, field, (value, keyList, authentication_codes))
    1.14  
    1.15 +class BeDOZaKeyList(object):
    1.16 +
    1.17 +    def __init__(self, alpha, keys):
    1.18 +        self.alpha = alpha
    1.19 +        self.keys = keys
    1.20 +
    1.21 +    def __add__(self, other):
    1.22 +        """Addition."""
    1.23 +        keys = []
    1.24 +        for k1, k2 in zip(self.keys, other.keys):
    1.25 +            keys.append(k1 + k2)
    1.26 +        return BeDOZaKeyList(keys)
    1.27 +
    1.28 +    def __str__(self):
    1.29 +        return "(%s, %s)" % (self.alpha, str(self.keys))
    1.30 +    
    1.31  class RandomShareGenerator:
    1.32  
    1.33 -    def authentication_codes(self, alpha, betas, v):
    1.34 -        auth_codes = []
    1.35 -        for beta in betas:
    1.36 -            auth_codes.append(alpha*v + beta)
    1.37 -        return auth_codes
    1.38 -
    1.39      def generate_random_shares(self, field, number_of_shares):
    1.40 -        alpha = self.get_keys()[0]
    1.41 +        def keys_N_codes(value):
    1.42 +            auth_codes = self.generate_auth_codes(self.id, v)
    1.43 +            my_keys = self.generate_keys()
    1.44 +            return BeDOZaShare(self, field, v, my_keys, auth_codes)
    1.45          shares = []
    1.46          for i in xrange(0, number_of_shares):
    1.47              if self.id == 1:
    1.48 -                betas = map(lambda (alpha, betas): betas[0], self.keys.values())
    1.49                  v = field(1)
    1.50 -                auth_codes= self.authentication_codes(alpha, betas, v)
    1.51 -                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.52 +                shares.append(keys_N_codes(v))
    1.53              if self.id == 2:
    1.54 -                betas = map(lambda (alpha, betas): betas[1], self.keys.values())
    1.55                  v = field(2)
    1.56 -                auth_codes= self.authentication_codes(alpha, betas, v)
    1.57 -                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.58 +                shares.append(keys_N_codes(v))
    1.59              if self.id == 3:
    1.60 -                betas = map(lambda (alpha, betas): betas[2], self.keys.values())
    1.61                  v = field(3)
    1.62 -                auth_codes= self.authentication_codes(alpha, betas, v)
    1.63 -                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.64 +                shares.append(keys_N_codes(v))
    1.65          return shares
    1.66  
    1.67 +    def generate_auth_codes(self, playerId, value):
    1.68 +        keys = map(lambda (alpha, akeys): (alpha, akeys[playerId - 1]), self.keys.values())
    1.69 +        auth_codes = self.authentication_codes(keys, value)
    1.70 +        return auth_codes
    1.71 +
    1.72 +    def authentication_codes(self, keys, v):
    1.73 +        auth_codes = []
    1.74 +        for alpha, beta in keys:
    1.75 +            auth_codes.append(alpha * v + beta)
    1.76 +        return auth_codes
    1.77 +
    1.78 +    def generate_keys(self):
    1.79 +        alpha, betas = self.get_keys()
    1.80 +        return BeDOZaKeyList(alpha, betas)
    1.81 +
    1.82  class KeyLoader:
    1.83  
    1.84      def load_keys(self, field):
    1.85 -        alpha = field(2)
    1.86 -        return {1: (alpha, [field(1), field(2), field(3)]),
    1.87 -                2: (alpha, [field(4), field(5), field(6)]),
    1.88 -                3: (alpha, [field(7), field(8), field(9)])}
    1.89 +        return {1: (field(2), [field(1), field(2), field(3)]),
    1.90 +                2: (field(3), [field(4), field(5), field(6)]),
    1.91 +                3: (field(4), [field(7), field(8), field(9)])}
    1.92  
    1.93  class BeDOZaRuntime(Runtime, HashBroadcastMixin, KeyLoader, RandomShareGenerator):
    1.94      """The BeDOZa runtime.
    1.95 @@ -115,7 +137,7 @@
    1.96          self.random_shares = []
    1.97   
    1.98      def MAC(self, alpha, beta, v):
    1.99 -         return alpha*v + beta
   1.100 +        return alpha * v + beta
   1.101  
   1.102      def random_share(self, field):
   1.103          """Retrieve a previously generated random share in the field, field.
   1.104 @@ -150,11 +172,11 @@
   1.105  
   1.106          self.increment_pc()
   1.107  
   1.108 -        def recombine_value(shares_codes):
   1.109 +        def recombine_value(shares_codes, keyList):
   1.110              isOK = True
   1.111              n = len(self.players)
   1.112 -            alpha = self.get_keys()[0]
   1.113 -            keys = self.get_keys()[1]
   1.114 +            alpha = keyList.alpha
   1.115 +            keys = keyList.keys
   1.116              x = 0
   1.117              for inx in xrange(0, n):
   1.118                  xi = shares_codes[inx]
   1.119 @@ -166,13 +188,12 @@
   1.120                  raise BeDOZaException("Wrong commitment for value %s." % x)
   1.121              return x
   1.122  
   1.123 -        def exchange((xi, codes), receivers):
   1.124 +        def exchange((xi, keyList, codes), receivers):
   1.125              # Send share to all receivers.
   1.126              pc = tuple(self.program_counter)
   1.127              for other_id in receivers:
   1.128                  self.protocols[other_id].sendShare(pc, xi)
   1.129                  self.protocols[other_id].sendShare(pc, codes[other_id - 1])
   1.130 -
   1.131              if self.id in receivers:
   1.132                  num_players = len(self.players.keys())
   1.133                  values = num_players * [None]
   1.134 @@ -181,7 +202,7 @@
   1.135                      values[inx] =  self._expect_share(other_id, field)
   1.136                      codes[inx] = self._expect_share(other_id, field)
   1.137                  result = gatherResults(values + codes)
   1.138 -                result.addCallbacks(recombine_value, self.error_handler)
   1.139 +                result.addCallbacks(recombine_value, self.error_handler, callbackArgs=(keyList,))
   1.140                  return result
   1.141  
   1.142          result = share.clone()
     2.1 --- a/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:55:20 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_runtime.py	Mon Jul 05 15:46:31 2010 +0200
     2.3 @@ -22,7 +22,7 @@
     2.4  from viff.test.util import RuntimeTestCase, protocol
     2.5  from viff.runtime import gather_shares, Share
     2.6  from viff.config import generate_configs
     2.7 -from viff.bedoza import BeDOZaRuntime, BeDOZaShare
     2.8 +from viff.bedoza import BeDOZaRuntime, BeDOZaShare, BeDOZaKeyList
     2.9  from viff.field import FieldElement, GF
    2.10  from viff.util import rand
    2.11  
    2.12 @@ -45,22 +45,16 @@
    2.13          keys2 = keys[2]
    2.14          keys3 = keys[3]
    2.15          if runtime.id == 1:
    2.16 -            alpha = keys1[0]
    2.17 -            self.assertEquals(alpha, 2)
    2.18              betas = keys1[1]
    2.19              self.assertEquals(betas[0], 1)
    2.20              self.assertEquals(betas[1], 2)
    2.21              self.assertEquals(betas[2], 3)
    2.22          if runtime.id == 2:
    2.23 -            alpha = keys2[0]
    2.24 -            self.assertEquals(alpha, 2)
    2.25              betas = keys2[1]
    2.26              self.assertEquals(betas[0], 4)
    2.27              self.assertEquals(betas[1], 5)
    2.28              self.assertEquals(betas[2], 6)
    2.29          if runtime.id == 3:
    2.30 -            alpha = keys3[0]
    2.31 -            self.assertEquals(alpha, 2)
    2.32              betas = keys3[1]
    2.33              self.assertEquals(betas[0], 7)
    2.34              self.assertEquals(betas[1], 8)
    2.35 @@ -79,24 +73,24 @@
    2.36          codes = self.num_players * [None]
    2.37  
    2.38          for xid in runtime.players.keys():
    2.39 -            betas = map(lambda (alpha, betas): betas[xid-1], runtime.keys.values())
    2.40 -            codes[xid-1] = runtime.authentication_codes(alpha, betas, v)
    2.41 +            keys = map(lambda (alpha, akeys): (alpha, akeys[xid - 1]), runtime.keys.values())
    2.42 +            codes[xid-1] = runtime.authentication_codes(keys, v)
    2.43          
    2.44          if runtime.id == 1:
    2.45              my_codes = codes[0]
    2.46              self.assertEquals(my_codes[0], self.Zp(5))
    2.47 -            self.assertEquals(my_codes[1], self.Zp(8))
    2.48 -            self.assertEquals(my_codes[2], self.Zp(11))
    2.49 +            self.assertEquals(my_codes[1], self.Zp(10))
    2.50 +            self.assertEquals(my_codes[2], self.Zp(15))
    2.51          if runtime.id == 2:
    2.52              my_codes = codes[1]
    2.53              self.assertEquals(my_codes[0], self.Zp(6))
    2.54 -            self.assertEquals(my_codes[1], self.Zp(9))
    2.55 -            self.assertEquals(my_codes[2], self.Zp(12))
    2.56 +            self.assertEquals(my_codes[1], self.Zp(11))
    2.57 +            self.assertEquals(my_codes[2], self.Zp(16))
    2.58          if runtime.id == 3:
    2.59              my_codes = codes[2]
    2.60              self.assertEquals(my_codes[0], self.Zp(7))
    2.61 -            self.assertEquals(my_codes[1], self.Zp(10))
    2.62 -            self.assertEquals(my_codes[2], self.Zp(13))
    2.63 +            self.assertEquals(my_codes[1], self.Zp(12))
    2.64 +            self.assertEquals(my_codes[2], self.Zp(17))
    2.65  
    2.66  
    2.67  class BeDOZaBasicCommandsTest(RuntimeTestCase):