viff

changeset 1448:3c21042745e7

Added a method for getting a random share.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Mon, 05 Jul 2010 11:33:58 +0200
parents cc963bc7ae5e
children 04a121c3b6a5
files viff/bedoza.py viff/test/test_bedoza_runtime.py
diffstat 2 files changed, 133 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/viff/bedoza.py	Mon Jul 05 11:33:58 2010 +0200
     1.2 +++ b/viff/bedoza.py	Mon Jul 05 11:33:58 2010 +0200
     1.3 @@ -47,8 +47,44 @@
     1.4          self.authentication_codes = authentication_codes
     1.5          Share.__init__(self, runtime, field, (value, authentication_codes))
     1.6  
     1.7 +class RandomShareGenerator:
     1.8  
     1.9 -class BeDOZaRuntime(Runtime, HashBroadcastMixin):
    1.10 +    def authentication_codes(self, alpha, betas, v):
    1.11 +        auth_codes = []
    1.12 +        for beta in betas:
    1.13 +            auth_codes.append(alpha*v + beta)
    1.14 +        return auth_codes
    1.15 +
    1.16 +    def generate_random_shares(self, field, number_of_shares):
    1.17 +        alpha = self.get_keys()[0]
    1.18 +        shares = []
    1.19 +        for i in xrange(0, number_of_shares):
    1.20 +            if self.id == 1:
    1.21 +                betas = map(lambda (alpha, betas): betas[0], self.keys.values())
    1.22 +                v = field(1)
    1.23 +                auth_codes= self.authentication_codes(alpha, betas, v)
    1.24 +                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.25 +            if self.id == 2:
    1.26 +                betas = map(lambda (alpha, betas): betas[1], self.keys.values())
    1.27 +                v = field(2)
    1.28 +                auth_codes= self.authentication_codes(alpha, betas, v)
    1.29 +                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.30 +            if self.id == 3:
    1.31 +                betas = map(lambda (alpha, betas): betas[2], self.keys.values())
    1.32 +                v = field(3)
    1.33 +                auth_codes= self.authentication_codes(alpha, betas, v)
    1.34 +                shares.append(BeDOZaShare(self, field, v, auth_codes))
    1.35 +        return shares
    1.36 +
    1.37 +class KeyLoader:
    1.38 +
    1.39 +    def load_keys(self, field):
    1.40 +        alpha = field(2)
    1.41 +        return {1: (alpha, [field(1), field(2), field(3)]),
    1.42 +                2: (alpha, [field(4), field(5), field(6)]),
    1.43 +                3: (alpha, [field(7), field(8), field(9)])}
    1.44 +
    1.45 +class BeDOZaRuntime(Runtime, HashBroadcastMixin, KeyLoader, RandomShareGenerator):
    1.46      """The BeDOZa runtime.
    1.47  
    1.48      The runtime is used for sharing values (:meth:`secret_share` or
    1.49 @@ -72,6 +108,27 @@
    1.50          """Initialize runtime."""
    1.51          Runtime.__init__(self, player, threshold, options)
    1.52          self.threshold = self.num_players - 1
    1.53 +        self.random_share_number = 100
    1.54 +        self.random_shares = []
    1.55 + 
    1.56 +    def MAC(self, alpha, beta, v):
    1.57 +         return alpha*v + beta
    1.58  
    1.59 -    def MAC(self, alpha, beta, v):
    1.60 -        return alpha*v + beta
    1.61 +    def random_share(self, field):
    1.62 +        """Retrieve a previously generated random share in the field, field.
    1.63 +
    1.64 +        If no more shares are left, generate self.random_share_number new ones.
    1.65 +        """
    1.66 +        self.keys = self.load_keys(field)
    1.67 +        if len(self.random_shares) == 0:
    1.68 +            self.random_shares = self.generate_random_shares(field, self.random_share_number)
    1.69 +
    1.70 +        return self.random_shares.pop()
    1.71 +
    1.72 +    def get_keys(self):
    1.73 +        if self.id == 1:
    1.74 +            return self.keys[1]
    1.75 +        if self.id == 2:
    1.76 +            return self.keys[2]
    1.77 +        if self.id == 3:
    1.78 +            return self.keys[3]
     2.1 --- a/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:33:58 2010 +0200
     2.2 +++ b/viff/test/test_bedoza_runtime.py	Mon Jul 05 11:33:58 2010 +0200
     2.3 @@ -26,6 +26,79 @@
     2.4  from viff.field import FieldElement, GF
     2.5  from viff.util import rand
     2.6  
     2.7 +class KeyLoaderTest(RuntimeTestCase):
     2.8 +    """Test of KeyLoader."""
     2.9 +
    2.10 +    # Number of players.
    2.11 +    num_players = 3
    2.12 +
    2.13 +    runtime_class = BeDOZaRuntime
    2.14 +
    2.15 +    @protocol
    2.16 +    def test_load_keys(self, runtime):
    2.17 +        """Test loading of keys."""
    2.18 +
    2.19 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.20 +
    2.21 +        keys = runtime.load_keys(self.Zp)
    2.22 +        keys1 = keys[1]
    2.23 +        keys2 = keys[2]
    2.24 +        keys3 = keys[3]
    2.25 +        if runtime.id == 1:
    2.26 +            alpha = keys1[0]
    2.27 +            self.assertEquals(alpha, 2)
    2.28 +            betas = keys1[1]
    2.29 +            self.assertEquals(betas[0], 1)
    2.30 +            self.assertEquals(betas[1], 2)
    2.31 +            self.assertEquals(betas[2], 3)
    2.32 +        if runtime.id == 2:
    2.33 +            alpha = keys2[0]
    2.34 +            self.assertEquals(alpha, 2)
    2.35 +            betas = keys2[1]
    2.36 +            self.assertEquals(betas[0], 4)
    2.37 +            self.assertEquals(betas[1], 5)
    2.38 +            self.assertEquals(betas[2], 6)
    2.39 +        if runtime.id == 3:
    2.40 +            alpha = keys3[0]
    2.41 +            self.assertEquals(alpha, 2)
    2.42 +            betas = keys3[1]
    2.43 +            self.assertEquals(betas[0], 7)
    2.44 +            self.assertEquals(betas[1], 8)
    2.45 +            self.assertEquals(betas[2], 9)
    2.46 +
    2.47 +    @protocol
    2.48 +    def test_authentication_codes(self, runtime):
    2.49 +        """Test generating random shares."""
    2.50 +
    2.51 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
    2.52 +
    2.53 +        runtime.keys = runtime.load_keys(self.Zp)
    2.54 +
    2.55 +        v = self.Zp(2)
    2.56 +        alpha = runtime.get_keys()[0]
    2.57 +        codes = self.num_players * [None]
    2.58 +
    2.59 +        for xid in runtime.players.keys():
    2.60 +            betas = map(lambda (alpha, betas): betas[xid-1], runtime.keys.values())
    2.61 +            codes[xid-1] = runtime.authentication_codes(alpha, betas, v)
    2.62 +        
    2.63 +        if runtime.id == 1:
    2.64 +            my_codes = codes[0]
    2.65 +            self.assertEquals(my_codes[0], self.Zp(5))
    2.66 +            self.assertEquals(my_codes[1], self.Zp(8))
    2.67 +            self.assertEquals(my_codes[2], self.Zp(11))
    2.68 +        if runtime.id == 2:
    2.69 +            my_codes = codes[1]
    2.70 +            self.assertEquals(my_codes[0], self.Zp(6))
    2.71 +            self.assertEquals(my_codes[1], self.Zp(9))
    2.72 +            self.assertEquals(my_codes[2], self.Zp(12))
    2.73 +        if runtime.id == 3:
    2.74 +            my_codes = codes[2]
    2.75 +            self.assertEquals(my_codes[0], self.Zp(7))
    2.76 +            self.assertEquals(my_codes[1], self.Zp(10))
    2.77 +            self.assertEquals(my_codes[2], self.Zp(13))
    2.78 +
    2.79 +
    2.80  class BeDOZaBasicCommandsTest(RuntimeTestCase):
    2.81      """Test for basic commands."""
    2.82