changeset 1501:522203844397

BeDOZa: Better incapsulation of internal representation for KeyList.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Thu, 15 Jul 2010 11:37:49 +0200
parents 74ad8413cc11
children af533d7ae315
files viff/bedoza.py
diffstat 1 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/viff/bedoza.py	Thu Jul 15 11:37:48 2010 +0200
+++ b/viff/bedoza.py	Thu Jul 15 11:37:49 2010 +0200
@@ -64,18 +64,18 @@
     def add_public(self, c, my_id):
         if my_id == 1:
             self.value = self.value + c
-        self.keyList.get_keys()[0] = self.keyList.get_keys()[0] - self.keyList.alpha * c
+        self.keyList.set_key(0, self.keyList.get_key(0) - self.keyList.alpha * c)
         return self
     
     def sub_public(self, c, my_id):
         if my_id == 1:
             self.value = self.value - c
-        self.keyList.get_keys()[0] = self.keyList.get_keys()[0] + self.keyList.alpha * c
+        self.keyList.set_key(0, self.keyList.get_key(0) + self.keyList.alpha * c)
         return self
 
     def cmul(self, c):
         zi = c * self.value
-        zks = BeDOZaKeyList(self.keyList.alpha, map(lambda k: c * k, self.keyList.get_keys()))
+        zks = self.keyList.cmul(c)
         zms = BeDOZaMACList(map(lambda m: c * m, self.macs.get_macs()))
         return BeDOZaShareContents(zi, zks, zms)
 
@@ -106,13 +106,24 @@
         
 
 class BeDOZaKeyList(object):
+    """A list of keys, one for each player.
+
+    We assume that the key for player *i* is stored in
+    location *i - 1* in the *keys* list given as argument to the constructor.
+    """
 
     def __init__(self, alpha, keys):
         self.alpha = alpha
         self.keys = keys
 
-    def get_keys(self):
-        return self.keys
+    def get_key(self, player_id):
+        return self.keys[player_id]
+
+    def set_key(self, player_id, v):
+        self.keys[player_id] = v
+
+    def cmul(self, c):
+        return BeDOZaKeyList(self.alpha, map(lambda k: c * k, self.keys))
 
     def __add__(self, other):
         """Addition."""
@@ -264,10 +275,10 @@
             values = num_shares * [0]
             isOK = num_shares * [True]
             for iny in xrange(num_shares):
-                keys = keyLists[iny].get_keys()
+                keyList = keyLists[iny]
                 for inx, xs in enumerate(player_shares_codes):
                     xi, mi = xs[iny]
-                    beta = keys[inx]
+                    beta = keyList.get_key(inx)
                     values[iny] += xi
                     mi_prime = self.MAC(alpha, beta, xi)
                     isOK[iny] = isOK[iny] and mi == mi_prime
@@ -341,9 +352,7 @@
 
             n = len(self.players)
             alpha_a = keyList_a.alpha
-            keys_a = keyList_a.get_keys()
             alpha_b = keyList_b.alpha
-            keys_b = keyList_b.get_keys()
 
             a = 0
             b = 0
@@ -353,8 +362,8 @@
                 bi = shares_codes[2*n + inx]
                 mi_a = shares_codes[n + inx]
                 mi_b = shares_codes[3*n + inx]
-                beta_a = keys_a[inx]
-                beta_b = keys_b[inx]
+                beta_a = keyList_a.get_key(inx)
+                beta_b = keyList_b.get_key(inx)
                 a += ai
                 b += bi
                 mi_prime = self.MAC(alpha_a, beta_a, ai)
@@ -417,12 +426,11 @@
             isOK = True
             n = len(self.players)
             alpha = keyList.alpha
-            keys = keyList.get_keys()
             x = 0
             for inx in xrange(0, n):
                 xi = shares_codes[inx]
                 mi = shares_codes[n + inx]
-                beta = keys[inx]
+                beta = keyList.get_key(inx)
                 x += xi
                 mi_prime = self.MAC(alpha, beta, xi)
                 isOK = isOK and mi == mi_prime