Mercurial > viff
changeset 1464:a28c7d75c09e
BeDOZa: Implemented multiplication by a constant.
author | Janus Dam Nielsen <janus.nielsen@alexandra.dk> |
---|---|
date | Tue, 06 Jul 2010 16:17:16 +0200 |
parents | 2ac4e8f3b3d0 |
children | 8779d6f6bad0 |
files | viff/bedoza.py viff/test/test_bedoza_runtime.py |
diffstat | 2 files changed, 59 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/viff/bedoza.py Tue Jul 06 15:00:30 2010 +0200 +++ b/viff/bedoza.py Tue Jul 06 16:17:16 2010 +0200 @@ -317,3 +317,37 @@ zks = xks - yks zms = xms - yms return (zi, zks, zms) + + + def _cmul(self, share_x, share_y, field): + """Multiplication of a share with a constant. + + Either share_x or share_y must be a BeDOZaShare but not + both. Returns None if both share_x and share_y are + BeDOZaShares. + """ + if not isinstance(share_x, Share): + # Then share_y must be a Share => local multiplication. We + # clone first to avoid changing share_y. + assert isinstance(share_y, Share), \ + "At least one of the arguments must be a share." + result = share_y.clone() + result.addCallback(self._constant_multiply, share_x) + return result + if not isinstance(share_y, Share): + # Likewise when share_y is a constant. + assert isinstance(share_x, Share), \ + "At least one of the arguments must be a share." + result = share_x.clone() + result.addCallback(self._constant_multiply, share_y) + return result + return None + + def _constant_multiply(self, x, c): + """Multiplication of a share-tuple with a constant c.""" + assert(isinstance(c, FieldElement)) + xi, xks, xms = x + zi = c * xi + zks = BeDOZaKeyList(xks.alpha, map(lambda k: c * k, xks.keys)) + zms = BeDOZaMessageList(map(lambda m: c * m, xms.auth_codes)) + return (zi, zks, zms)
--- a/viff/test/test_bedoza_runtime.py Tue Jul 06 15:00:30 2010 +0200 +++ b/viff/test/test_bedoza_runtime.py Tue Jul 06 16:17:16 2010 +0200 @@ -276,19 +276,39 @@ return d @protocol - def test_sub_constant_left(self, runtime): - """Test subtraction of a public number and secret shared number.""" + def test_constant_multiplication_constant_left(self, runtime): + """Test multiplication of two numbers.""" self.Zp = GF(6277101735386680763835789423176059013767194773182842284081) - x1 = 42 + x1 = 6 y1 = 7 def check(v): - self.assertEquals(v, 1) + self.assertEquals(v, x1 * y1) x2 = runtime.random_share(self.Zp) - z2 = y1 - x2 + + z2 = runtime._cmul(self.Zp(y1), x2, self.Zp) d = runtime.open(z2) d.addCallback(check) return d + + @protocol + def test_constant_multiplication_constant_right(self, runtime): + """Test multiplication of two numbers.""" + + self.Zp = GF(6277101735386680763835789423176059013767194773182842284081) + + x1 = 6 + y1 = 7 + + def check(v): + self.assertEquals(v, x1 * y1) + + x2 = runtime.random_share(self.Zp) + + z2 = runtime._cmul(x2, self.Zp(y1), self.Zp) + d = runtime.open(z2) + d.addCallback(check) + return d