コード例 #1
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
            )
コード例 #2
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")
コード例 #3
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
            )
コード例 #4
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)
コード例 #5
0
    def test_mul_AB_public_private(self):
        tf.reset_default_graph()

        prot = ABY3()
        tfe.set_protocol(prot)

        x = tfe.define_constant(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)
コード例 #6
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")
コード例 #7
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")
コード例 #8
0
ファイル: test_aby3.py プロジェクト: shaweiwu/tf-encrypted
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")
コード例 #9
0
    def test_add_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
        z = x + y
        z = y + z

        with tfe.Session() as sess:
            # initialize variables
            sess.run(tfe.global_variables_initializer())
            # reveal result
            result = sess.run(z.reveal())
            expected = np.array([[2.2, 2.4], [2.6, 2.8]])
            np.testing.assert_allclose(result, expected, rtol=0.0, atol=0.01)
コード例 #10
0
ファイル: test_aby3.py プロジェクト: shaweiwu/tf-encrypted
def test_add_private_public():
    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
    z = x + y
    z = y + z

    with tfe.Session() as sess:
        # initialize variables
        sess.run(tfe.global_variables_initializer())
        # reveal result
        result = sess.run(z.reveal())
        close(result, np.array([[2.2, 2.4], [2.6, 2.8]]))
        print("test_add_private_public succeeds")