## viff

### changeset 1227:5636b02c6bef

Implementation of subtraction command.
author Janus Dam Nielsen Tue, 06 Oct 2009 10:05:24 +0200 94086392cb3c a62e12c9947a viff/orlandi.py viff/test/test_orlandi_runtime.py 2 files changed, 128 insertions(+), 0 deletions(-) [+]
line diff
```     1.1 --- a/viff/orlandi.py	Tue Oct 06 10:05:24 2009 +0200
1.2 +++ b/viff/orlandi.py	Tue Oct 06 10:05:24 2009 +0200
1.3 @@ -350,6 +350,39 @@
1.5          return result
1.6
1.7 +    def sub(self, share_a, share_b):
1.8 +        """Subtraction of shares.
1.9 +
1.10 +        Communication cost: none.
1.11 +
1.12 +        Each party ``P_i`` computes:
1.13 +        ``[z]_i = [x]_i - [y]_i
1.14 +                = (x_i - y_i mod p, rho_x,i - rho_y,i mod p, C_x * C_y)``.
1.15 +
1.16 +        """
1.17 +        def is_share(s, field):
1.18 +            if not isinstance(s, Share):
1.19 +                if not isinstance(s, FieldElement):
1.20 +                    s = field(s)
1.21 +                (v, rhov, Cv) = self._additive_constant(field(0), s)
1.22 +                return OrlandiShare(self, field, v, rhov, Cv)
1.23 +            return s
1.24 +
1.25 +        # Either share_a or share_b must have an attribute called "field".
1.26 +        field = getattr(share_a, "field", getattr(share_b, "field", None))
1.27 +
1.28 +        share_a = is_share(share_a, field)
1.29 +        share_b = is_share(share_b, field)
1.30 +
1.31 +        # Subtract xi and yi, rhoxi and rhoyi, and compute the commitment
1.32 +        def compute_subs((x, y)):
1.33 +            zi, (rhozi1, rhozi2), Cz = self._minus(x, y)
1.34 +            return OrlandiShare(self, field, zi, (rhozi1, rhozi2), Cz)
1.35 +
1.36 +        result = gather_shares([share_a, share_b])
1.38 +        return result
1.39 +
1.42
1.43 @@ -380,6 +413,23 @@
1.44          Cz = Cx * Cy
1.45          return (zi, (rhozi1, rhozi2), Cz)
1.46
1.47 +    def _minus(self, x, y):
1.48 +        """Subtraction of share-tuples *x* and *y*.
1.49 +
1.50 +        Each party ``P_i`` computes:
1.51 +        ``[x]_i = (x_i, rho_x,i, C_x)``
1.52 +        ``[y]_i = (y_i, rho_y,i, C_y)``
1.53 +        ``[z]_i = [x]_i - [y]_i
1.54 +                = (x_i - y_i mod p, rho_x,i - rho_y,i mod p, C_x / C_y)``.
1.55 +        """
1.56 +        xi, (rhoxi1, rhoxi2), Cx = x
1.57 +        yi, (rhoyi1, rhoyi2), Cy = y
1.58 +        zi = xi - yi
1.59 +        rhozi1 = rhoxi1 - rhoyi1
1.60 +        rhozi2 = rhoxi2 - rhoyi2
1.61 +        Cz = Cx / Cy
1.62 +        return (zi, (rhozi1, rhozi2), Cz)
1.63 +
1.64      def error_handler(self, ex):
1.65          print "Error: ", ex
1.66          return ex
```
```     2.1 --- a/viff/test/test_orlandi_runtime.py	Tue Oct 06 10:05:24 2009 +0200
2.2 +++ b/viff/test/test_orlandi_runtime.py	Tue Oct 06 10:05:24 2009 +0200
2.3 @@ -161,3 +161,81 @@
2.4          d = runtime.open(z2)
2.6          return d
2.7 +
2.8 +    @protocol
2.9 +    def test_sub(self, runtime):
2.10 +        """Test subtration of two numbers."""
2.11 +
2.12 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
2.13 +
2.14 +        x1 = 42
2.15 +        y1 = 7
2.16 +
2.17 +        def check(v):
2.18 +            self.assertEquals(v, x1 - y1)
2.19 +
2.20 +        if 1 == runtime.id:
2.21 +            x2 = runtime.secret_share([1], self.Zp, x1)
2.22 +        else:
2.23 +            x2 = runtime.secret_share([1], self.Zp)
2.24 +
2.25 +        if 3 == runtime.id:
2.26 +            y2 = runtime.secret_share([3], self.Zp, y1)
2.27 +        else:
2.28 +            y2 = runtime.secret_share([3], self.Zp)
2.29 +
2.30 +        z2 = runtime.sub(x2, y2)
2.31 +        d = runtime.open(z2)
2.33 +        return d
2.34 +
2.35 +    @protocol
2.36 +    def test_sub_minus(self, runtime):
2.37 +        """Test subtration of two numbers."""
2.38 +
2.39 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
2.40 +
2.41 +        x1 = 42
2.42 +        y1 = 7
2.43 +
2.44 +        def check(v):
2.45 +            self.assertEquals(v, x1 - y1)
2.46 +
2.47 +        if 1 == runtime.id:
2.48 +            x2 = runtime.secret_share([1], self.Zp, x1)
2.49 +        else:
2.50 +            x2 = runtime.secret_share([1], self.Zp)
2.51 +
2.52 +        if 3 == runtime.id:
2.53 +            y2 = runtime.secret_share([3], self.Zp, y1)
2.54 +        else:
2.55 +            y2 = runtime.secret_share([3], self.Zp)
2.56 +
2.57 +        z2 = x2 - y2
2.58 +        d = runtime.open(z2)
2.60 +        return d
2.61 +
2.62 +    @protocol
2.63 +    def test_sub_constant(self, runtime):
2.64 +        """Test subtration of two numbers."""
2.65 +
2.66 +        self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
2.67 +
2.68 +        x1 = 42
2.69 +        y1 = 7
2.70 +
2.71 +        def check(v):
2.72 +            self.assertEquals(v, x1 - y1)
2.73 +
2.74 +        if 1 == runtime.id:
2.75 +            x2 = runtime.secret_share([1], self.Zp, x1)
2.76 +        else:
2.77 +            x2 = runtime.secret_share([1], self.Zp)
2.78 +
2.79 +        z2 = x2 - y1
2.80 +        d = runtime.open(z2)