def test_matmul_grad(X, Y, out_g, protocol="Helix"): cb.activate(protocol) global sess if sess is not None: sess.close() init = tf.compat.v1.global_variables_initializer() sess = tf.compat.v1.Session() sess.run(init) # =========================== # run mpc matmul # =========================== print("===========================") print("run mpc matmul(X * Y) grad") mpc_Z = cb.SecureMatMul(X, Y) mpc_g = tf.gradients( mpc_Z, [common.get_var_from_rtt_tensor(X), common.get_var_from_rtt_tensor(Y)]) print(sess.run(mpc_g)) print("===========================") # =========================== # check mpcmatmul grads value # =========================== mpc_out_g = [] for i in range(len(mpc_g)): print("---------- Reveal mpcmatmul grad------------") mpc_out_g.append(sess.run(cb.SecureReveal(mpc_g[i]))) print(mpc_out_g) print("--------------------------------------------") global res_flag res_flag = res_flag and common.check_mpc_op_grads(out_g, mpc_out_g)
import tensorflow as tf import sys import numpy as np np.set_printoptions(suppress=True) protocol = "Helix" rtt.activate(protocol) x = np.array([[2.0, 2.1], [2.2, 2.3]]) y = np.array([[1, 2.0], [1, 3.0]]) z = np.array([[[0, 1, 2.4, 0, 1, 2.9]], [[0.3, 1, 2, 0, 1.2, 2]]]) input0 = rtt.private_input(0, x) input1 = rtt.private_input(1, y) input2 = rtt.private_input(2, z) print('input0:', type(input0), input0) print('input1:', type(input1), input1) print('input2:', type(input2), input2) i0 = tf.Variable(input0) i1 = tf.Variable(input1) i2 = tf.Variable(input2) ii = rtt.SecureMatMul(i0, i1) ir_matmul = rtt.SecureReveal(ii) init = tf.global_variables_initializer() with tf.Session() as sess1: sess1.run(init) print('rosetta matmul:', sess1.run(ir_matmul))
import numpy as np np.set_printoptions(suppress=True) protocol = "Helix" rtt.activate(protocol) patyid = str(rtt.get_party_id()) rtt.set_backend_loglevel(0) rtt.set_backend_logfile("/tmp/wo-men-dou-shi-hao-hai-zi.log" + patyid) print("rtt.get_protocol_name():", rtt.get_protocol_name()) X = tf.Variable([[1., 1.], [2., 1.]]) Y = tf.Variable([[1., 3.], [1., 1.]]) z = rtt.SecureMatMul(X, Y) zr = rtt.SecureReveal(z) init = tf.global_variables_initializer() with tf.Session() as sess1: sess1.run(init) print('zzzzzr:', sess1.run(zr)) rtt.set_backend_loglevel(3) rtt.set_backend_logfile("/tmp/wo-men-dou-shi-hao-hai-zi-ma.log" + patyid) with tf.Session() as sess1: sess1.run(init) print('zzzzzr:', sess1.run(zr)) exit(0)
if "TEST_PROTOCOL" in os.environ.keys(): print("***** test_cases use ", os.environ["TEST_PROTOCOL"]) protocol = os.environ["TEST_PROTOCOL"] else: print("***** test_cases use default helix protocol ") rtt.activate(protocol) sess = None xaa = np.array([[1.92, 0.2, 3], [-0.43, .0091, 1.3]]) xbb = np.array([[.2, 0.3], [-2, .3], [-1.111, -0.3]]) xa = tf.Variable(rtt.private_input(0, xaa)) xb = tf.Variable(rtt.private_input(1, xbb)) z1 = rtt.SecureMatMul(xa, xb) z0 = rtt.SecureReveal(z1) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(sess.run(z1)) print(sess.run(z0)) xaa = np.array([[1.0, 2.0], [3.0, 4.0]]) xbb = np.array([[1.0, 2.0], [3.0, 4.0]]) xa = tf.Variable(rtt.private_input(0, xaa)) xb = tf.Variable(rtt.private_input(1, xbb)) z1 = rtt.SecureMatMul(xa, xb) z0 = rtt.SecureReveal(z1)
# ######### log/log1p/sigmoid/matmul/ # Log share_f = rtt.SecureLog(share_a) reveal0 = rtt.SecureReveal(share_f) with tf.Session() as sess: sess.run(init) print('SecureLog:', sess.run(reveal0)) # Log1p share_f = rtt.SecureLog1p(share_a) reveal0 = rtt.SecureReveal(share_f) with tf.Session() as sess: sess.run(init) print('SecureLog1p:', sess.run(reveal0)) # Sigmoid share_f = rtt.SecureSigmoid(share_a) reveal0 = rtt.SecureReveal(share_f) with tf.Session() as sess: sess.run(init) print('SecureSigmoid:', sess.run(reveal0)) # MatMul share_e = rtt.SecureMatMul(share_a, share_a) share_f = rtt.SecureMatMul(share_a, share_e) reveal0 = rtt.SecureReveal(share_f) with tf.Session() as sess: sess.run(init) print('SecureMatMul:', sess.run(reveal0))
if "ROSETTA_TEST_PROTOCOL" in os.environ.keys(): print("***** test_cases use ", os.environ["ROSETTA_TEST_PROTOCOL"]) protocol = os.environ["ROSETTA_TEST_PROTOCOL"] else: print("***** test_cases use default helix protocol ") cb.activate(protocol) sess = None xaa = np.array([[1.92, 0.2, 3], [-0.43, .0091, 1.3]]) xbb = np.array([[.2, 0.3], [-2, .3], [-1.111, -0.3]]) xa = tf.Variable(cb.private_input(0, xaa)) xb = tf.Variable(cb.private_input(1, xbb)) z1 = cb.SecureMatMul(xa, xb) z0 = cb.SecureReveal(z1) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print(sess.run(z1)) print(sess.run(z0)) xaa = np.array([[1.0, 2.0], [3.0, 4.0]]) xbb = np.array([[1.0, 2.0], [3.0, 4.0]]) xa = tf.Variable(cb.private_input(0, xaa)) xb = tf.Variable(cb.private_input(1, xbb)) z1 = cb.SecureMatMul(xa, xb) z0 = cb.SecureReveal(z1) init = tf.global_variables_initializer()