Example #1
0
    def test_iterate_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            return tf.reshape(tf.range(0, 8), [4, 2])

        # define inputs
        x = tfe.define_private_input('input-provider', provide_input)

        write_op = x.write("x.tfrecord")

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sess.run(write_op)

        x = tfe.read("x.tfrecord", batch_size=5, n_columns=2)
        y = tfe.iterate(x, batch_size=3, repeat=True, shuffle=False)
        z = tfe.iterate(x, batch_size=3, repeat=True, shuffle=True)
        with tfe.Session() as sess:
            sess.run(tfe.global_variables_initializer())
            print(sess.run(x.reveal()))
            print(sess.run(y.reveal()))
            print(sess.run(y.reveal()))
            print(sess.run(x.reveal()))
            print(sess.run(z.reveal()))
Example #2
0
    def test_iterate_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            return tf.reshape(tf.range(0, 8), [4, 2])

        # define inputs
        x = tfe.define_private_input("input-provider", provide_input)

        _, tmp_filename = tempfile.mkstemp()
        write_op = x.write(tmp_filename)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sess.run(write_op)

        x = tfe.read(tmp_filename, batch_size=5, n_columns=2)
        y = tfe.iterate(x, batch_size=3, repeat=True, shuffle=False)
        z = tfe.iterate(x, batch_size=3, repeat=True, shuffle=True)
        with tfe.Session() as sess:
            sess.run(tfe.global_variables_initializer())
            # TODO: fix this test
            print(sess.run(x.reveal()))
            print(sess.run(y.reveal()))
            print(sess.run(y.reveal()))
            print(sess.run(x.reveal()))
            print(sess.run(z.reveal()))

        os.remove(tmp_filename)
Example #3
0
    def test_add_private_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            # normal TensorFlow operations can be run locally
            # as part of defining a private input, in this
            # case on the machine of the input provider
            return tf.ones(shape=(2, 2)) * 1.3

        # define inputs
        x = tfe.define_private_variable(tf.ones(shape=(2, 2)))
        y = tfe.define_private_input('input-provider', provide_input)

        # define computation
        z = x + y

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z.reveal())
            # Should be [[2.3, 2.3], [2.3, 2.3]]
            np.testing.assert_allclose(result,
                                       np.array([[2.3, 2.3], [2.3, 2.3]]),
                                       rtol=0.0,
                                       atol=0.01)
            print("test_add_private_private succeeds")
Example #4
0
    def test_mul_private_public(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        # define inputs
        x = tfe.define_private_variable(tf.ones(shape=(2, 2)) * 2)
        y = tfe.define_constant(np.array([[0.6, 0.7], [0.8, 0.9]]))
        w = tfe.define_constant(np.array([[2, 2], [2, 2]]))

        # define computation
        z1 = y * x  # mul_public_private
        z2 = z1 * w  # mul_private_public

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z2.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[2.4, 2.8], [3.2, 3.6]]),
                                       rtol=0.0,
                                       atol=0.01)
            print("test_mul_private_public succeeds")
Example #5
0
    def test_neg(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        # define inputs
        x = tfe.define_private_variable(np.array([[0.6, -0.7], [-0.8, 0.9]]))
        y = tfe.define_constant(np.array([[0.6, -0.7], [-0.8, 0.9]]))

        # define computation
        z1 = -x
        z2 = -y

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[-0.6, 0.7], [0.8, -0.9]]),
                                       rtol=0.0,
                                       atol=0.01)
            result = sess.run(z2)
            np.testing.assert_allclose(result,
                                       np.array([[-0.6, 0.7], [0.8, -0.9]]),
                                       rtol=0.0,
                                       atol=0.01)
            print("test_neg succeeds")
Example #6
0
    def test_read_private(self):

        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            return tf.reshape(tf.range(0, 8), [4, 2])

        # define inputs
        x = tfe.define_private_input('input-provider', provide_input)

        write_op = x.write("x.tfrecord")

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sess.run(write_op)

        x = tfe.read("x.tfrecord", batch_size=5, n_columns=2)
        with tfe.Session() as sess:
            result = sess.run(x.reveal())
            np.testing.assert_allclose(result,
                                       np.array(list(range(0, 8)) +
                                                [0, 1]).reshape([5, 2]),
                                       rtol=0.0,
                                       atol=0.01)
            print("test_read_private succeeds")
Example #7
0
    def test_assign_synchronization(self):
        # from https://github.com/tf-encrypted/tf-encrypted/pull/665

        prot = tfe.protocol.Pond()
        tfe.set_protocol(prot)

        def poc(x, y):
            x_shares = x.unwrapped
            y_shares = y.unwrapped
            z_shares = [None, None]

            with tf.name_scope("fabricated_test"):
                with tf.device(prot.server_0.device_name):
                    z_shares[0] = x_shares[1] + y_shares[1]
                with tf.device(prot.server_1.device_name):
                    z_shares[1] = x_shares[0] + y_shares[0]

            return tfe.protocol.pond.PondPrivateTensor(prot, z_shares[0],
                                                       z_shares[1],
                                                       x.is_scaled)

        a = prot.define_private_variable(tf.ones(shape=(1, 1)))
        b = prot.define_private_variable(tf.ones(shape=(1, 1)))

        op = prot.assign(a, poc(a, b))

        with tfe.Session() as sess:
            sess.run(tfe.global_variables_initializer())

            for _ in range(100):
                sess.run(op)

            result = sess.run(a.reveal())
            assert result == np.array([101.])
Example #8
0
    def test_fp_sqrt_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)
        r = np.random.rand(16) * 100  # random in [0, 100)

        # define inputs
        x = tfe.define_private_input("input-provider", lambda: tf.constant(r))

        # define computation
        sqrt_x = prot.fp_sqrt(x).reveal()
        sqrt_inv_x = prot.fp_sqrt_inv(x).reveal()

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sqrt_x, sqrt_inv_x = sess.run([sqrt_x, sqrt_inv_x], tag="fp_sqrt")
            np.testing.assert_allclose(sqrt_x,
                                       np.sqrt(r),
                                       rtol=0.03,
                                       atol=0.05)
            np.testing.assert_allclose(sqrt_inv_x,
                                       1. / np.sqrt(r),
                                       rtol=0.03,
                                       atol=0.05)
Example #9
0
def test_polynomial_piecewise():
    tf.reset_default_graph()

    import time
    start = time.time()
    prot = ABY3()
    tfe.set_protocol(prot)

    x = tfe.define_private_variable(
        tf.constant([[-1, -0.5, -0.25], [0, 0.25, 2]]))

    # This is the approximation of the sigmoid function by using a piecewise function:
    # f(x) = (0 if x<-0.5), (x+0.5 if -0.5<=x<0.5), (1 if x>=0.5)
    z1 = tfe.polynomial_piecewise(
        x,
        (-0.5, 0.5),
        (
            (0, ), (0.5, 1), (1, )
        )  # Should use tuple because list is not hashable for the memoir cache key
    )
    # Or, simply use the pre-defined sigmoid API which includes a different approximation
    z2 = tfe.sigmoid(x)

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(z1.reveal())
        close(result, np.array([[0, 0, 0.25], [0.5, 0.75, 1]]))
        result = sess.run(z2.reveal())
        close(result, np.array([[0.33, 0.415, 0.4575], [0.5, 0.5425, 0.84]]))
        print("test_polynomial_piecewise succeeds")

    end = time.time()
    print("Elapsed time: {} seconds".format(end - start))
Example #10
0
def test_bit_extract():
    tf.reset_default_graph()

    prot = ABY3()
    tfe.set_protocol(prot)

    x = tfe.define_private_variable(np.array([[1, -2, 3], [-4, -5, 6]]),
                                    share_type=ARITHMETIC)
    y = tfe.define_private_variable(np.array([[1, -2, 3], [-4, -5, 6]]),
                                    share_type=ARITHMETIC,
                                    apply_scaling=False)

    z = tfe.bit_extract(
        x, 63
    )  # The sign bit. Since x is scaled, you should be more careful about extracting other bits.
    w = tfe.bit_extract(y, 1)  # y is not scaled
    s = tfe.msb(x)  # Sign bit

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(z.reveal())
        close(result.astype(int), np.array([[0, 1, 0], [1, 1, 0]]))
        result = sess.run(w.reveal())
        close(result.astype(int), np.array([[0, 1, 1], [0, 1, 1]]))
        result = sess.run(s.reveal())
        close(result.astype(int), np.array([[0, 1, 0], [1, 1, 0]]))
        print("test_bit_extract succeeds")
Example #11
0
def test_ot():
    tf.reset_default_graph()

    prot = ABY3()
    tfe.set_protocol(prot)

    m0 = prot.define_constant(np.array([[1, 2, 3], [4, 5, 6]]),
                              apply_scaling=False).unwrapped[0]
    m1 = prot.define_constant(np.array([[2, 3, 4], [5, 6, 7]]),
                              apply_scaling=False).unwrapped[0]
    c_on_receiver = prot.define_constant(
        np.array([[1, 0, 1], [0, 1, 0]]),
        apply_scaling=False,
        factory=prot.bool_factory).unwrapped[0]
    c_on_helper = prot.define_constant(np.array([[1, 0, 1], [0, 1, 0]]),
                                       apply_scaling=False,
                                       factory=prot.bool_factory).unwrapped[0]

    m_c = prot._ot(prot.servers[1], prot.servers[2], prot.servers[0], m0, m1,
                   c_on_receiver, c_on_helper, prot.pairwise_keys[1][0],
                   prot.pairwise_keys[0][1], prot.pairwise_nonces[0])

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(prot._decode(m_c, False))
        close(result, np.array([[2, 2, 4], [4, 6, 6]]))
        print("test_ot succeeds")
Example #12
0
    def test_sub_private_public(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        # define inputs
        x = tfe.define_private_variable(tf.ones(shape=(2, 2)))
        y = tfe.define_constant(np.array([[0.6, 0.7], [0.8, 0.9]]))

        # define computation
        z1 = x - y
        z2 = y - x

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[0.4, 0.3], [0.2, 0.1]]),
                                       rtol=0.0,
                                       atol=0.01)
            result = sess.run(z2.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[-0.4, -0.3], [-0.2, -0.1]]),
                                       rtol=0.0,
                                       atol=0.01)
            print("test_sub_private_public succeeds")
Example #13
0
    def test_3d_matmul_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        # 3-D matrix mult
        x = tfe.define_private_variable(
            tf.constant(np.arange(1, 13), shape=[2, 2, 3]))
        y = tfe.define_private_variable(
            tf.constant(np.arange(13, 25), shape=[2, 3, 2]))

        z = tfe.matmul(x, y)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z.reveal())
            np.testing.assert_allclose(
                result,
                np.array([[[94, 100], [229, 244]], [[508, 532], [697, 730]]]),
                rtol=0.0,
                atol=0.01,
            )
Example #14
0
    def test_mul_trunc2_private_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            # normal TensorFlow operations can be run locally
            # as part of defining a private input, in this
            # case on the machine of the input provider
            return tf.ones(shape=(2, 2)) * 1.3

        # define inputs
        x = tfe.define_private_variable(tf.ones(shape=(2, 2)) * 2)
        y = tfe.define_private_input("input-provider", provide_input)

        # define computation
        z = tfe.mul_trunc2(x, y)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z.reveal(), tag="mul_trunc2")
            np.testing.assert_allclose(
                result, np.array([[2.6, 2.6], [2.6, 2.6]]), rtol=0.0, atol=0.01
            )
Example #15
0
def test_matmul_public_private():
    tf.reset_default_graph()

    prot = ABY3()
    tfe.set_protocol(prot)

    def provide_input():
        # normal TensorFlow operations can be run locally
        # as part of defining a private input, in this
        # case on the machine of the input provider
        return tf.constant(np.array([[1.1, 1.2], [1.3, 1.4], [1.5, 1.6]]))

    # define inputs
    x = tfe.define_private_variable(tf.ones(shape=(2, 2)))
    y = tfe.define_public_input('input-provider', provide_input)
    v = tfe.define_constant(np.ones((2, 2)))

    # define computation
    w = y.matmul(x)  # matmul_public_private
    z = w.matmul(v)  # matmul_private_public

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(w.reveal())
        close(result, np.array([[2.3, 2.3], [2.7, 2.7], [3.1, 3.1]]))
        result = sess.run(z.reveal())
        close(result, np.array([[4.6, 4.6], [5.4, 5.4], [6.2, 6.2]]))
        print("test_matmul_public_private succeeds")
Example #16
0
    def test_transpose(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(tf.constant([[1, 2, 3], [4, 5, 6]]))
        y = tfe.define_constant(np.array([[1, 2, 3], [4, 5, 6]]))

        z1 = x.transpose()
        z2 = tfe.transpose(y)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[1, 4], [2, 5], [3, 6]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2)
            np.testing.assert_allclose(
                result, np.array([[1, 4], [2, 5], [3, 6]]), rtol=0.0, atol=0.01
            )
Example #17
0
    def test_concat(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x1 = tfe.define_private_variable(tf.constant([[1, 2], [4, 5]]))
        x2 = tfe.define_private_variable(tf.constant([[3], [6]]))
        y1 = tfe.define_constant(np.array([[1, 2, 3]]))
        y2 = tfe.define_constant(np.array([[4, 5, 6]]))

        z1 = tfe.concat([x1, x2], axis=1)
        z2 = tfe.concat([y1, y2], axis=0)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[1, 2, 3], [4, 5, 6]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2)
            np.testing.assert_allclose(
                result, np.array([[1, 2, 3], [4, 5, 6]]), rtol=0.0, atol=0.01
            )
Example #18
0
    def test_polynomial_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(tf.constant([[1, 2, 3], [4, 5, 6]]))

        # Friendly version
        y = 1 + 1.2 * x + 3 * (x ** 2) + 0.5 * (x ** 3)
        # More optimized version: No truncation for multiplying integer coefficients (e.g., '3' in this example)
        z = tfe.polynomial(x, [1, 1.2, 3, 0.5])

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(y.reveal())
            np.testing.assert_allclose(
                result,
                np.array([[5.7, 19.4, 45.1], [85.8, 144.5, 224.2]]),
                rtol=0.0,
                atol=0.01,
            )

            result = sess.run(z.reveal())
            np.testing.assert_allclose(
                result,
                np.array([[5.7, 19.4, 45.1], [85.8, 144.5, 224.2]]),
                rtol=0.0,
                atol=0.01,
            )
Example #19
0
    def test_polynomial_piecewise(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(tf.constant([[-1, -0.5, -0.25], [0, 0.25, 2]]))

        # This is the approximation of the sigmoid function by using a piecewise function:
        # f(x) = (0 if x<-0.5), (x+0.5 if -0.5<=x<0.5), (1 if x>=0.5)
        z1 = tfe.polynomial_piecewise(
            x,
            (-0.5, 0.5),
            ((0,), (0.5, 1), (1,)),  # use tuple because list is not hashable
        )
        # Or, simply use the pre-defined sigmoid API which includes a different approximation
        z2 = tfe.sigmoid(x)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[0, 0, 0.25], [0.5, 0.75, 1]]), rtol=0.0, atol=0.01
            )
            result = sess.run(z2.reveal())
            np.testing.assert_allclose(
                result,
                np.array([[0.33, 0.415, 0.4575], [0.5, 0.5425, 0.84]]),
                rtol=0.0,
                atol=0.01,
            )
Example #20
0
    def test_ppa_private_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(
            tf.constant([[1, 2, 3], [4, 5, 6]]), share_type=BOOLEAN
        )
        y = tfe.define_private_variable(
            tf.constant([[7, 8, 9], [10, 11, 12]]), share_type=BOOLEAN
        )

        # Parallel prefix adder. It is simply an adder for boolean sharing.
        z1 = tfe.B_ppa(x, y, topology="sklansky")
        z2 = tfe.B_ppa(x, y, topology="kogge_stone")

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[8, 10, 12], [14, 16, 18]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2.reveal())
            np.testing.assert_allclose(
                result, np.array([[8, 10, 12], [14, 16, 18]]), rtol=0.0, atol=0.01
            )
Example #21
0
    def test_mul_AB_private_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(
            np.array([[1, 2, 3], [4, 5, 6]]), share_type=ARITHMETIC,
        )
        y = tfe.define_private_variable(
            tf.constant([[1, 0, 0], [0, 1, 0]]),
            apply_scaling=False,
            share_type=BOOLEAN,
            factory=prot.bool_factory,
        )

        z = tfe.mul_AB(x, y)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z.reveal())
            np.testing.assert_allclose(
                result, np.array([[1, 0, 0], [0, 5, 0]]), rtol=0.0, atol=0.01
            )
Example #22
0
    def test_boolean_sharing(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(
            tf.constant([[1, 2, 3], [4, 5, 6]]), share_type=BOOLEAN
        )
        y = tfe.define_private_variable(
            tf.constant([[7, 8, 9], [10, 11, 12]]), share_type=BOOLEAN
        )

        z1 = tfe.B_xor(x, y)

        z2 = tfe.B_and(x, y)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[6, 10, 10], [14, 14, 10]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2.reveal())
            np.testing.assert_allclose(
                result, np.array([[1, 0, 1], [0, 1, 4]]), rtol=0.0, atol=0.01
            )
Example #23
0
    def test_not_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(
            tf.constant([[1, 2, 3], [4, 5, 6]]), share_type=BOOLEAN, apply_scaling=False
        )
        y = tfe.define_private_variable(
            tf.constant([[1, 0, 0], [0, 1, 0]]),
            apply_scaling=False,
            share_type=BOOLEAN,
            factory=prot.bool_factory,
        )
        z1 = ~x
        z2 = ~y

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(
                result, np.array([[-2, -3, -4], [-5, -6, -7]]), rtol=0.0, atol=0.01
            )

            result = sess.run(z2.reveal())
            np.testing.assert_allclose(
                result, np.array([[0, 1, 1], [1, 0, 1]]), rtol=0.0, atol=0.01
            )
Example #24
0
    def test_read_private(self):

        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            return tf.reshape(tf.range(0, 8), [4, 2])

        # define inputs
        x = tfe.define_private_input("input-provider", provide_input)

        _, tmp_filename = tempfile.mkstemp()
        write_op = x.write(tmp_filename)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sess.run(write_op)

        x = tfe.read(tmp_filename, batch_size=5, n_columns=2)
        with tfe.Session() as sess:
            result = sess.run(x.reveal())
            np.testing.assert_allclose(
                result,
                np.array(list(range(0, 8)) + [0, 1]).reshape([5, 2]),
                rtol=0.0,
                atol=0.01,
            )

        os.remove(tmp_filename)
Example #25
0
    def test_write_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        def provide_input():
            # normal TensorFlow operations can be run locally
            # as part of defining a private input, in this
            # case on the machine of the input provider
            return tf.ones(shape=(2, 2)) * 1.3

        # define inputs
        x = tfe.define_private_input("input-provider", provide_input)

        _, tmp_filename = tempfile.mkstemp()
        write_op = x.write(tmp_filename)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            sess.run(write_op)

        os.remove(tmp_filename)
Example #26
0
    def test_reduce_sum(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(tf.constant([[1, 2, 3], [4, 5, 6]]))
        y = tfe.define_constant(np.array([[1, 2, 3], [4, 5, 6]]))

        z1 = x.reduce_sum(axis=1, keepdims=True)
        z2 = tfe.reduce_sum(y, axis=0, keepdims=False)

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z1.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[6], [15]]),
                                       rtol=0.0,
                                       atol=0.01)

            result = sess.run(z2)
            np.testing.assert_allclose(result,
                                       np.array([5, 7, 9]),
                                       rtol=0.0,
                                       atol=0.01)
Example #27
0
    def test_pow_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_private_variable(tf.constant([[1, 2, 3], [4, 5, 6]]))

        y = x**2
        z = x**3

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(y.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[1, 4, 9], [16, 25, 36]]),
                                       rtol=0.0,
                                       atol=0.01)

            result = sess.run(z.reveal())
            np.testing.assert_allclose(result,
                                       np.array([[1, 8, 27], [64, 125, 216]]),
                                       rtol=0.0,
                                       atol=0.01)
Example #28
0
def test_rshift_private():
    tf.reset_default_graph()

    prot = ABY3()
    tfe.set_protocol(prot)

    x = tfe.define_private_variable(tf.constant([[1, 2, 3], [4, 5, 6]]),
                                    share_type=BOOLEAN)
    y = tfe.define_private_variable(tf.constant([[-1, -2, -3], [-4, 5, 6]]),
                                    share_type=BOOLEAN,
                                    apply_scaling=False)

    z = x >> 1
    w = y >> 1
    s = y.logical_rshift(1)

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(z.reveal())
        close(result, np.array(
            [[0.5, 1, 1.5],
             [2, 2.5,
              3]]))  # NOTE: x is scaled and treated as fixed-point number
        result = sess.run(w.reveal())
        close(result, np.array([[-1, -1, -2], [-2, 2, 3]]))
        result = sess.run(s.reveal())
        close(
            result,
            np.array([[(-1 & ((1 << prot.nbits) - 1)) >> 1,
                       (-2 & ((1 << prot.nbits) - 1)) >> 1,
                       (-3 & ((1 << prot.nbits) - 1)) >> 1],
                      [(-4 & ((1 << prot.nbits) - 1)) >> 1, 2, 3]]))
        print("test_rshift_private succeeds")
Example #29
0
def main(server):

    num_rows = 7000
    num_features = 32
    num_epoch = 5
    batch_size = 200
    num_batches = (num_rows // batch_size) * num_epoch

    #who shall receive the output
    model_owner = ModelOwner('alice')

    data_schema0 = DataSchema([tf.float64] * 16, [0.0] * 16)
    data_schema1 = DataSchema([tf.int64] + [tf.float64] * 16, [0] + [0.0] * 16)
    data_owner_0 = DataOwner('alice',
                             'aliceTrainFile.csv',
                             data_schema0,
                             batch_size=batch_size)
    data_owner_1 = DataOwner('bob',
                             'bobTrainFileWithLabel.csv',
                             data_schema1,
                             batch_size=batch_size)

    tfe.set_protocol(
        tfe.protocol.Pond(
            tfe.get_config().get_player(data_owner_0.player_name),
            tfe.get_config().get_player(data_owner_1.player_name)))

    x_train_0 = tfe.define_private_input(data_owner_0.player_name,
                                         data_owner_0.provide_data)
    x_train_1 = tfe.define_private_input(data_owner_1.player_name,
                                         data_owner_1.provide_data)
    y_train = tfe.gather(x_train_1, 0, axis=1)
    y_train = tfe.reshape(y_train, [batch_size, 1])

    #Remove bob's first column (which is label)
    x_train_1 = tfe.strided_slice(x_train_1, [0, 1],
                                  [x_train_1.shape[0], x_train_1.shape[1]],
                                  [1, 1])

    x_train = tfe.concat([x_train_0, x_train_1], axis=1)

    model = LogisticRegression(num_features)
    reveal_weights_op = model_owner.receive_weights(model.weights)
    with tfe.Session() as sess:
        sess.run(tfe.global_variables_initializer(), tag='init')
        start_time = time.time()
        model.fit(sess, x_train, y_train, num_batches)
        end_time = time.time()
        # TODO(Morten)
        # each evaluation results in nodes for a forward pass being added to the graph;
        # maybe there's some way to avoid this, even if it means only if the shapes match
        model.evaluate(sess, x_train, y_train, data_owner_0)
        model.evaluate(sess, x_train, y_train, data_owner_1)

        print(sess.run(reveal_weights_op, tag='reveal'),
              ((end_time - start_time) * 1000))
Example #30
0
def test_simple_lr_model():
    tf.reset_default_graph()

    import time
    start = time.time()
    prot = ABY3()
    tfe.set_protocol(prot)

    # define inputs
    x_raw = tf.random.uniform(minval=-0.5,
                              maxval=0.5,
                              shape=[99, 10],
                              seed=1000)
    x = tfe.define_private_variable(x_raw, name="x")
    y_raw = tf.cast(tf.reduce_mean(x_raw, axis=1, keepdims=True) > 0,
                    dtype=tf.float32)
    y = tfe.define_private_variable(y_raw, name="y")
    w = tfe.define_private_variable(tf.random_uniform([10, 1],
                                                      -0.01,
                                                      0.01,
                                                      seed=100),
                                    name="w")
    b = tfe.define_private_variable(tf.zeros([1]), name="b")
    learning_rate = 0.01

    with tf.name_scope("forward"):
        out = tfe.matmul(x, w) + b
        y_hat = tfe.sigmoid(out)

    with tf.name_scope("loss-grad"):
        dy = y_hat - y
    batch_size = x.shape.as_list()[0]
    with tf.name_scope("backward"):
        dw = tfe.matmul(tfe.transpose(x), dy) / batch_size
        db = tfe.reduce_sum(dy, axis=0) / batch_size
        upd1 = dw * learning_rate
        upd2 = db * learning_rate
        assign_ops = [tfe.assign(w, w - upd1), tfe.assign(b, b - upd2)]

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        for i in range(1):
            sess.run(assign_ops)

        print(sess.run(w.reveal()))
    end = time.time()
    print("Elapsed time: {} seconds".format(end - start))