Esempio n. 1
0
    def test_pickle(self):
        xsecfld = mpc.SecFld(256)
        psecfld = mpc.SecFld(257)
        secint = mpc.SecInt()
        secfxp = mpc.SecFxp()
        # NB: mpc.transfer() calls pickle.dumps() and pickle.loads()
        self.assertEqual(
            mpc.run(mpc.output(mpc.run(mpc.transfer(xsecfld(12), senders=0)))),
            12)
        self.assertEqual(
            mpc.run(mpc.output(mpc.run(mpc.transfer(psecfld(12), senders=0)))),
            12)
        self.assertEqual(
            mpc.run(mpc.output(mpc.run(mpc.transfer(secint(12), senders=0)))),
            12)
        self.assertEqual(
            mpc.run(mpc.output(mpc.run(mpc.transfer(secfxp(12.5),
                                                    senders=0)))), 12.5)
        self.assertEqual(mpc.run(mpc.transfer(xsecfld.field(12), senders=0)),
                         12)
        self.assertEqual(mpc.run(mpc.transfer(psecfld.field(12), senders=0)),
                         12)
        self.assertEqual(mpc.run(mpc.transfer(secint.field(12), senders=0)),
                         12)
        self.assertEqual(mpc.run(mpc.transfer(secfxp.field(13), senders=0)),
                         13)
        self.assertEqual(mpc.run(mpc.transfer(xsecfld.field.modulus, 0)),
                         xsecfld.field.modulus)

        x = [(xsecfld(12), psecfld(12), secint(12), secfxp(12.5)),
             [
                 xsecfld.field(12),
                 psecfld.field(12),
                 secint.field(12),
                 secfxp.field(13)
             ], xsecfld.field.modulus]
        y = mpc.run(mpc.transfer(x, senders=0))
        self.assertTrue(
            all(mpc.run(mpc.output(a == b)) for a, b in zip(y[0], x[0])))
        self.assertEqual(y[1], x[1])
        self.assertEqual(y[2], x[2])
Esempio n. 2
0
to run the demo with m=3 parties on localhost.

When run with m parties, a total of m(m-1)/2 TCP connections will be created between
all parties, noting that TCP connections are full-duplex (bidirectional). So there are
no connections when run with m=1 party only, there is one connection for m=2 parties, and
there are three connections for m=3 parties.

With all m parties running on localhost, your OS may run out of available ports for large m,
and the program will therefore not terminate properly. For example, the default range for
dynamic (private) ports on Windows is 49152-65535, which will take you to around m=180 parties,
before exhausting the available ports. The range for dynamic ports can be increased like this,
requiring administrator privileges:

    netsh int ipv4 set dynamicport tcp start=16000 num=48000

Now run the demo (as a nice stress test) with m=300, for a total of 44850 TCP connections:

    python helloworld.py -M300 -T0

It it essential to use threshold t=0 (or, maybe t=1). Otherwise the time needed to set up
the PRSS keys, which is proportional to (m choose t) = m!/t!/(m-t)!, will be prohibitive.
"""

from mpyc.runtime import mpc

mpc.run(mpc.start())  # connect to all other parties
print(''.join(mpc.run(mpc.transfer('Hello world!'))))
mpc.run(mpc.shutdown()
        )  # disconnect, but only once all other parties reached this point
Esempio n. 3
0
    def test_io(self):
        x = ({4, 3}, [1 - 1j, 2.5], 0, range(7))
        self.assertEqual(mpc.run(mpc.transfer(x))[0], x)
        self.assertEqual(mpc.run(mpc.transfer(x, senders=0)), x)
        self.assertEqual(mpc.run(mpc.transfer(x, senders=[0]))[0], x)
        self.assertEqual(
            mpc.run(mpc.transfer(x, senders=iter(range(1))))[0], x)
        self.assertEqual(mpc.run(mpc.transfer(x, receivers=0))[0], x)
        self.assertEqual(mpc.run(mpc.transfer(x, receivers=[0]))[0], x)
        self.assertEqual(
            mpc.run(mpc.transfer(x, receivers=iter(range(1))))[0], x)
        self.assertEqual(mpc.run(mpc.transfer(x, senders=0, receivers=0)), x)
        self.assertEqual(
            mpc.run(mpc.transfer(x, senders=[0], receivers=[0]))[0], x)
        self.assertEqual(
            mpc.run(mpc.transfer(x, sender_receivers=[(0, 0)]))[0], x)
        self.assertEqual(
            mpc.run(mpc.transfer(x, sender_receivers={0: {0}}))[0], x)

        a = mpc.SecInt()(7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a)))[0], 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a)[0])), 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a, senders=0))), 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a, senders=[0])))[0], 7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a, senders=iter(range(1)))))[0], 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a, senders=[0])))[0], 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a, senders=[0])[0])), 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a), receivers=0))[0], 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a)[0], receivers=0)), 7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a), receivers=[0]))[0], 7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a), receivers=iter(range(1))))[0], 7)
        self.assertEqual(mpc.run(mpc.output(mpc.input(a)[0], receivers=[0])),
                         7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a, senders=0), receivers=0)), 7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a, senders=[0]), receivers=[0]))[0],
            7)
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(a, senders=[0])[0], receivers=[0])),
            7)

        x = [a, a]
        self.assertEqual(mpc.run(mpc.output(mpc.input(x)[0])), [7, 7])
        self.assertEqual(mpc.run(mpc.output(mpc.input(x, senders=0))), [7, 7])
        self.assertEqual(mpc.run(mpc.output(mpc.input(x, senders=[0])[0])),
                         [7, 7])
        self.assertEqual(mpc.run(mpc.output(mpc.input(x)[0], receivers=0)),
                         [7, 7])
        self.assertEqual(mpc.run(mpc.output(mpc.input(x)[0], receivers=[0])),
                         [7, 7])
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(x, senders=0), receivers=0)), [7, 7])
        self.assertEqual(
            mpc.run(mpc.output(mpc.input(x, senders=[0])[0], receivers=[0])),
            [7, 7])
Esempio n. 4
0
    def test_secint(self):
        secint = mpc.SecInt()
        s = seclist([], secint)
        self.assertEqual(s, [])
        s = seclist(sectype=secint)
        self.assertEqual(s, [])
        s.append(False)
        s.sort()
        s.append(secint(7))
        s[0] = secint(13)
        self.assertEqual(mpc.run(mpc.output(list(s))), [13, 7])
        i = [secint(0), secint(1)]
        s[i] = 5
        self.assertEqual(mpc.run(mpc.output(s[1])), 5)

        i0 = [secint(1), secint(0)]
        i1 = [secint(0), secint(1)]
        s[i0], s[i1] = s[i1], s[i0]
        self.assertEqual(mpc.run(mpc.output(list(s))), [5, 13])
        s[i0], s[i1] = s[i1], s[i0]
        self.assertEqual(mpc.run(mpc.output(list(s))), [13, 5])
        s[0], s[1] = s[1], s[0]
        self.assertEqual(mpc.run(mpc.output(list(s))), [5, 13])
        s.append(secint(8))  # s = [5, 13, 8]
        s.reverse()  # s = [8, 13, 5]
        s.insert(secint(0), 9)  # s = [9, 8, 13, 5]
        del s[secint(1)]  # s = [9, 13, 5]
        s.pop(secint(2))  # s = [9, 13]
        s.insert(0, 99)  # s = [99, 9, 13]
        s.pop(0)  # s = [9, 13]
        self.assertRaises(ValueError, s.remove, secint(11))
        s *= 2  # s = [9, 13, 9, 13]
        s.remove(9)  # s = [13, 9, 13]
        s[0:1] = []  # s = [9, 13]
        s = 1 * s + s * 0  # s = [9, 13]
        self.assertEqual(mpc.run(mpc.output(list(s))), [9, 13])
        self.assertEqual(mpc.run(mpc.output(s[secint(1)])), 13)
        s[secint(1)] = secint(21)
        self.assertEqual(mpc.run(mpc.output(s[1])), 21)
        self.assertRaises(IndexError, s.insert, [secint(1), secint(0)], 42)
        self.assertRaises(IndexError, s.pop, [secint(1)])
        self.assertRaises(IndexError, s.__getitem__, [secint(1)])
        self.assertRaises(TypeError, s.__setitem__, [secint(1)], 42.5)
        self.assertRaises(TypeError, s.__setitem__, slice(0, 2),
                          seclist([0], mpc.SecFxp()))
        self.assertRaises(IndexError, s.__setitem__, [secint(1)], 42)
        self.assertRaises(IndexError, s.__delitem__, [secint(1)])

        s = seclist([0] * 7, secint)
        for a in [secint(3)] * 3 + [secint(4)] * 4:
            s[a] += 1
        self.assertEqual(mpc.run(mpc.output(list(s))), [0, 0, 0, 3, 4, 0, 0])

        with self.assertRaises(NotImplementedError):
            0 in s
        self.assertTrue(mpc.run(mpc.output(s.contains(0))))
        self.assertFalse(mpc.run(mpc.output(s.contains(9))))
        self.assertEqual(mpc.run(mpc.output(s.count(0))), 5)
        self.assertEqual(mpc.run(mpc.output(s.find(3))), 3)
        self.assertEqual(mpc.run(mpc.output(s.index(4))), 4)
        self.assertRaises(ValueError, s.index, 9)
        self.assertEqual(mpc.run(mpc.output(seclist([], secint).find(9))), -1)
        self.assertRaises(ValueError, seclist([], secint).index, 0)
        s.sort(lambda a: -a**2, reverse=True)
        s.sort()
        self.assertEqual(mpc.run(mpc.output(list(s))), 5 * [0] + [3, 4])
        self.assertFalse(mpc.run(mpc.output(s < s)))
        self.assertTrue(mpc.run(mpc.output(s <= s)))
        self.assertTrue(mpc.run(mpc.output(s == s)))
        self.assertFalse(mpc.run(mpc.output(s > s)))
        self.assertTrue(mpc.run(mpc.output(s >= s)))
        self.assertFalse(mpc.run(mpc.output(s != s)))
        self.assertFalse(mpc.run(mpc.output(s < [])))
        self.assertFalse(mpc.run(mpc.output(s <= [])))
        self.assertTrue(mpc.run(mpc.output(s >= [])))
        self.assertTrue(mpc.run(mpc.output(s > [])))
        self.assertFalse(mpc.run(mpc.output(s < s[:-1])))
        self.assertTrue(mpc.run(mpc.output(s[:-1] < s)))
        self.assertTrue(mpc.run(mpc.output(s[:-1] != s)))
        t = s.copy()
        t[-1] += 1
        self.assertTrue(mpc.run(mpc.output(s < t)))
        t[1] -= 1
        self.assertFalse(mpc.run(mpc.output(s < t)))
        self.assertFalse(mpc.run(mpc.output(s[:-1] <= t)))
        s = seclist([1, 2, 3, 4], secint)
        t = mpc.run(mpc.transfer(s, senders=0))
        self.assertTrue(mpc.run(mpc.output(s == t)))