## viff

### changeset 1464:a28c7d75c09e

BeDOZa: Implemented multiplication by a constant.
author Janus Dam Nielsen Tue, 06 Jul 2010 16:17:16 +0200 2ac4e8f3b3d0 8779d6f6bad0 viff/bedoza.py viff/test/test_bedoza_runtime.py 2 files changed, 59 insertions(+), 5 deletions(-) [+]
line diff
```     1.1 --- a/viff/bedoza.py	Tue Jul 06 15:00:30 2010 +0200
1.2 +++ b/viff/bedoza.py	Tue Jul 06 16:17:16 2010 +0200
1.3 @@ -317,3 +317,37 @@
1.4          zks = xks - yks
1.5          zms = xms - yms
1.6          return (zi, zks, zms)
1.7 +
1.8 +
1.9 +    def _cmul(self, share_x, share_y, field):
1.10 +        """Multiplication of a share with a constant.
1.11 +
1.12 +        Either share_x or share_y must be a BeDOZaShare but not
1.13 +        both. Returns None if both share_x and share_y are
1.14 +        BeDOZaShares.
1.15 +        """
1.16 +        if not isinstance(share_x, Share):
1.17 +            # Then share_y must be a Share => local multiplication. We
1.18 +            # clone first to avoid changing share_y.
1.19 +            assert isinstance(share_y, Share), \
1.20 +                "At least one of the arguments must be a share."
1.21 +            result = share_y.clone()
1.23 +            return result
1.24 +        if not isinstance(share_y, Share):
1.25 +            # Likewise when share_y is a constant.
1.26 +            assert isinstance(share_x, Share), \
1.27 +                "At least one of the arguments must be a share."
1.28 +            result = share_x.clone()
1.30 +            return result
1.31 +        return None
1.32 +
1.33 +    def _constant_multiply(self, x, c):
1.34 +        """Multiplication of a share-tuple with a constant c."""
1.35 +        assert(isinstance(c, FieldElement))
1.36 +        xi, xks, xms = x
1.37 +        zi = c * xi
1.38 +        zks = BeDOZaKeyList(xks.alpha, map(lambda k: c * k, xks.keys))
1.39 +        zms = BeDOZaMessageList(map(lambda m: c * m, xms.auth_codes))
1.40 +        return (zi, zks, zms)
```
```     2.1 --- a/viff/test/test_bedoza_runtime.py	Tue Jul 06 15:00:30 2010 +0200
2.2 +++ b/viff/test/test_bedoza_runtime.py	Tue Jul 06 16:17:16 2010 +0200
2.3 @@ -276,19 +276,39 @@
2.4          return d
2.5
2.6      @protocol
2.7 -    def test_sub_constant_left(self, runtime):
2.8 -        """Test subtraction of a public number and secret shared number."""
2.9 +    def test_constant_multiplication_constant_left(self, runtime):
2.10 +        """Test multiplication of two numbers."""
2.11
2.12          self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
2.13
2.14 -        x1 = 42
2.15 +        x1 = 6
2.16          y1 = 7
2.17
2.18          def check(v):
2.19 -            self.assertEquals(v, 1)
2.20 +            self.assertEquals(v, x1 * y1)
2.21
2.22          x2 = runtime.random_share(self.Zp)
2.23 -        z2 = y1 - x2
2.24 +
2.25 +        z2 = runtime._cmul(self.Zp(y1), x2, self.Zp)
2.26          d = runtime.open(z2)
2.28          return d
2.29 +
2.30 +    @protocol
2.31 +    def test_constant_multiplication_constant_right(self, runtime):
2.32 +        """Test multiplication of two numbers."""
2.33 +
2.34 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
2.35 +
2.36 +        x1 = 6
2.37 +        y1 = 7
2.38 +
2.39 +        def check(v):
2.40 +            self.assertEquals(v, x1 * y1)
2.41 +
2.42 +        x2 = runtime.random_share(self.Zp)
2.43 +
2.44 +        z2 = runtime._cmul(x2, self.Zp(y1), self.Zp)
2.45 +        d = runtime.open(z2)