def test(task_id): rtt.py_protocol_handler.set_loglevel(0) np.set_printoptions(suppress=True) rtt.activate("SecureNN", task_id=task_id) print('begin get io wrapper', task_id) node_id = rtt.get_current_node_id(task_id=task_id) print('end get io wrapper', task_id) dg = tf.Graph() with dg.as_default(): # Get private data from Alice (input x), Bob (input y) w = tf.Variable(rtt.private_input(0, [[1, 2], [2, 3]], task_id=task_id)) x = tf.Variable(rtt.private_input(1, [[1, 2], [2, 3]], task_id=task_id)) y = tf.Variable(rtt.private_input(2, [[1, 2], [2, 3]], task_id=task_id)) # Define matmul operation res = tf.matmul(tf.matmul(w, x), y) init = tf.global_variables_initializer() config = tf.ConfigProto(inter_op_parallelism_threads=16, intra_op_parallelism_threads=16) with tf.Session(task_id=task_id, config=config) as sess: sess.run(init) #rW, rb = sess.run([reveal_W, reveal_b]) #print("init weight:{} \nbias:{}".format(rW, rb)) #Y_pred = sess.run(reveal_Y, feed_dict={X: real_X, Y: real_Y}) #print("Y_pred:", Y_pred) sess.run(res) print(rtt.get_perf_stats(pretty=True, task_id=task_id)) rtt.deactivate(task_id=task_id)
def get_binary_inputs(lh_is_const=False, rh_is_const=False, dims=2): if dims == 2: v1 = [[100000, 2], [3, 4]] v2 = [[200000, 2], [2, 2]] elif dims == 1: v1 = [[1, 2]] v2 = [[2, 2]] elif dims == 0: v1 = 1 v2 = 2 else: assert False, 'dims: {} not support!'.format(dims) in1 = tf.constant(v1, name="in1") if lh_is_const else tf.Variable( rtt.private_input(0, v1), name="in1") in2 = tf.constant(v2, name="in2") if rh_is_const else tf.Variable( rtt.private_input(0, v2), name="in2") return in1, in2
def load_cifar10(args): start_time = time.time() (train_data, train_labels), (test_data, test_labels) = cifar10.load_data() train_data = train_data / 255.0 test_data = test_data / 255.0 train_data, test_data = normalize(train_data, test_data) train_labels = to_categorical(train_labels, 10) test_labels = to_categorical(test_labels, 10) seed = 777 np.random.seed(seed) np.random.shuffle(train_data) np.random.seed(seed) np.random.shuffle(train_labels) print('train_data.shape', train_data.shape) print('test_data.shape', test_data.shape) train_data = train_data[:args.train_size, :] train_labels = train_labels[:args.train_size, :] test_data = test_data[:args.test_size, :] test_labels = test_labels[:args.test_size, :] print("pystats load_plain elapse:{0} s".format(time.time() - start_time)) # print(test_data[:1, :]) start_time = time.time() if args.input_public: train_data = rtt.public_input(0, train_data) train_labels = rtt.public_input(0, train_labels) test_data = rtt.public_input(0, test_data) test_labels = rtt.public_input(0, test_labels) print("pystats rtt.public_input elapse:{0} s".format(time.time() - start_time)) else: train_data = rtt.private_input(0, train_data) train_labels = rtt.private_input(0, train_labels) test_data = rtt.private_input(0, test_data) test_labels = rtt.private_input(0, test_labels) print("pystats rtt.private_input elapse:{0} s".format(time.time() - start_time)) return train_data, train_labels, test_data, test_labels
def get_reduce_input(is_const=False, dims=2): if dims == 2: v1 = [[1, 2, 3], [1, 2, 1]] elif dims == 1: v1 = [1, 2, 3] else: v1 = 1 in1 = tf.constant(v1, name="in1") if is_const else tf.Variable( rtt.private_input(0, v1), name="in1") return in1
def test_protocol(protocol_name="SecureNN"): rst.activate(protocol_name) PRI_LOGITS = rst.private_input(0, np_a) PRI_LABELS = rst.private_input(1, np_b) PRI_logits = tf.Variable(PRI_LOGITS, dtype=tf.string) PRI_labels = tf.Variable(PRI_LABELS, dtype=tf.string) init = tf.compat.v1.global_variables_initializer() PRI_sess = tf.compat.v1.Session() PRI_sess.run(init) start_t = time.time() result_mpc = rst.secure_sigmoid_cross_entropy_with_logits( logits=PRI_logits, labels=PRI_labels) PRI_sess.run(result_mpc) end_t = time.time() reveal_op = rst.SecureReveal(result_mpc) xcc = PRI_sess.run(reveal_op) print(xcc) print("{} elapsed: {} ".format(protocol_name, end_t - start_t)) rst.deactivate()
#!/usr/bin/env python3 # Import rosetta package import latticex.rosetta as rtt import tensorflow as tf # You can activate a backend protocol, here use SecureNN rtt.activate("SecureNN") # Get private data from Alice (input x), Bob (input y) x = tf.Variable(rtt.private_input(0, [[1, 2], [2, 3]])) y = tf.Variable(rtt.private_input(1, [[1, 2], [2, 3]])) # Define matmul operation res = tf.matmul(x, y) # Start execution with tf.Session() as sess: sess.run(tf.global_variables_initializer()) res = sess.run(res) # Get the result of Rosetta matmul # ret: [[b'14.000000' b'20.000000'] [b'20.000000' b'29.000000']] print('matmul:', sess.run(rtt.SecureReveal(res))) rtt.deactivate()
print(curr_case) print("-------------------------------------------------") ######### 2. We perform the same functionality one by one with Rosetta import latticex.rosetta as rtt # all logs will be printed # rtt.py_protocol_handler.set_loglevel(0) # rtt.activate("Helix") rtt.activate("SecureNN") #### 2.1 first case case_id = 0 rtt_case = TEST_CASES[case_id] rtt_res_1 = TEST_CASES[case_id] cipher_var_a = tf.Variable(rtt.private_input(0, rtt_case["input"][0])) cipher_var_b = tf.Variable(rtt.private_input(1, rtt_case["input"][1])) cipher_var_c = cipher_var_a / cipher_var_b cipher_var_c_1 = rtt.SecureReciprocaldiv(cipher_var_a, cipher_var_b) init = tf.compat.v1.global_variables_initializer() with tf.compat.v1.Session() as rtt_sess: rtt_sess.run(init) rtt_res = rtt_sess.run(cipher_var_c) rtt_res_1 = rtt_sess.run(cipher_var_c_1) # print("local cipher res:", rtt_res) # reveal to get the plaintext result rtt_res = rtt_sess.run(rtt.SecureReveal(rtt_res)) rtt_res_1 = rtt_sess.run(rtt.SecureReveal(rtt_res_1)) rtt_case["rtt_res"] = np.array(rtt_res, dtype=np.float) rtt_case["rtt_res_1"] = np.array(rtt_res_1, dtype=np.float)
print("***** test_cases use ", os.environ["ROSETTA_TEST_PROTOCOL"]) protocol = os.environ["ROSETTA_TEST_PROTOCOL"] else: print("***** test_cases use default helix protocol ") rtt.activate(protocol) rtt.activate(protocol) print("rtt.get_protocol_name():", rtt.get_protocol_name()) patyid = rtt.get_party_id() # float * tf.Variable() num_a = np.array([[2000.0, 20000.0], [20000 * 100.0, 200.0]], dtype=np.float_) num_b = np.array([[50000, -50000.0], [-50000, -0.02]], dtype=np.float_) X = tf.Variable(rtt.private_input(0, num_a)) Y = tf.Variable(rtt.private_input(1, num_b)) CX = tf.constant(num_a) CY = tf.constant(num_b) expected = num_a / num_b floor_expected = np.floor_divide(num_a, num_b) a1 = X / Y floor1 = tf.floordiv(X, Y) a2 = X / CY floor2 = tf.floordiv(X, CY) # in this case, it is hard to get the correct result from big shared right!
import math import time import numpy as np import pandas as pd import latticex.rosetta as rtt import tensorflow as tf start_time = time.time() rtt.activate("SecureNN") mpc_player_id = rtt.py_protocol_handler.get_party_id() # ######## a = [1.2] * 10 a_ = tf.Variable(rtt.private_input(0, a)) with tf.compat.v1.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) aa = sess.run(a_) print('bb:', aa) print('type(aa):', type(aa)) b = [0.2] * 10 bb = rtt.private_input(0, b) print('bb:', bb) print('type(bb):', type(bb)) # if (isinstance(bb, np.ndarray)): # print("bb is np.ndarray type") # else: # print("------bb is not np.ndarray type-------")
import latticex.rosetta as rtt import tensorflow as tf Alice = tf.Variable(rtt.private_input(0, 2000001)) Bob = tf.Variable(rtt.private_input(1, 2000000)) res = tf.greater(Alice, Bob) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) res = sess.run(res) print('ret:', sess.run(rtt.MpcReveal(res))) # ret: 1.0
import latticex.rosetta as rst import tensorflow as tf import sys import numpy as np np.set_printoptions(suppress=True) # rst.set_backend_loglevel(1) # rst.backend_log_to_stdout(True) rst.activate("SecureNN") rst.set_backend_loglevel(1) rst.backend_log_to_stdout(True) # rst.set_backend_logfile("log/addn.{}".format(rst.get_party_id())) xa = tf.Variable( rst.private_input( 0, [[-102.12345678], [-0.12345678], [0.234567890], [0.34567890]])) # xa = tf.Variable(0, # [ # [-102.12345678], # [-0.12345678], # [0.234567890], # [0.34567890] # ] # ) # xb = tf.Variable([[1],[2],[2],[1]]) # print("xa:\n", xa) # # init = tf.compat.v1.global_variables_initializer() sess = tf.compat.v1.Session()
#!/usr/bin/env python3 import latticex.rosetta as rtt import tensorflow as tf import sys import numpy as np np.set_printoptions(suppress=True) protocol = "Helix" rtt.activate(protocol) input0 = rtt.private_input(0, 1.234) input1 = rtt.private_input(1, 5.432) input2 = rtt.private_input(2, 2.222) print('input0:', input0) print('input1:', input1) print('input2:', input2) i0 = tf.Variable(input0) i1 = tf.Variable(input1) i2 = tf.Variable(input2) ii = rtt.SecureAdd(i0, i1) ii = rtt.SecureAdd(ii, i2) ir_add = rtt.SecureReveal(ii) # i0 + i1 + i2 init = tf.global_variables_initializer() with tf.Session() as sess1: sess1.run(init) print('rosetta add:', sess1.run(ir_add))
#!/usr/bin/env python3 import latticex.rosetta as rtt 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:
def get_random(low, high, size): xi = np.random.randint(low, high, size) xd = np.array(xi, dtype=np.float_) xs = rtt.private_input(0, xd) return xs
k = np.array([[0, 1, 2, 2], [1, 0, 1, 4], [2, 1, 0, 2]]) print('x.shape', x.shape, "\n", x, "\n", x.flatten()) print('k.shape', k.shape, "\n", k, "\n", k.flatten()) s = '' for i in x.flatten(): s += str(i) s += ',' print('x:', s) s = '' for i in k.flatten(): s += str(i) s += ',' print('k:', s) x = tf.Variable(rtt.private_input(0, x)) kernel = tf.Variable(rtt.private_input(0, k)) out_mat = tf.matmul(x, kernel) init = tf.compat.v1.global_variables_initializer() with tf.Session() as sess: sess.run(init) print('rout_mat:', sess.run(rtt.SecureReveal(out_mat))) # 1,5,5,3 (NHWC) x = np.array([ # N=0 [[[0, 1, 2], [1, 0, 4], [0, 2, 2], [1, 1, 2], [2, 1, 2]], [[1, 2, 1], [5, 2, 0], [4, 0, 5], [5, 2, 0], [0, 3, 1]], [[1, 2, 3], [5, 0, 0], [4, 2, 1], [5, 2, 0], [0, 3, 2]], [[1, 2, 0], [0, 4, 1], [5, 3, 0], [0, 0, 0], [3, 2, 4]], [[1, 0, 1], [0, 0, 5], [0, 4, 1], [0, 5, 0], [1, 0, 1]]]
rtt.activate(protocol) patyid = rtt.get_party_id() print( "rtt.get_protocol_name: ", rtt.get_protocol_name(), "party: ", ) # float * tf.Variable() num_a = np.ones([10, 10]) num_b = np.ones([10, 10]) * 2 num_neg_a = -num_a # only ALICE could own private data X = tf.Variable(rtt.private_input(0, num_a)) Y = tf.Variable(rtt.private_input(0, num_b)) P = tf.Variable(rtt.private_input(0, num_neg_a)) CX = tf.constant(num_a) CY = tf.constant(num_b) A3 = tf.Variable(rtt.private_input(0, [[[1, 2], [2, 3]], [[2, 3], [4, 5]]])) B3 = tf.Variable(rtt.private_input(0, [[[1, 2], [2, 3]], [[2, 3], [4, 5]]])) AB3 = A3 + B3 A4 = tf.Variable( rtt.private_input(0, [[ [[1, 2], [2, 3]], [[2, 3], [4, 5]], ], [[[1, 2], [2, 3]], [[2, 3], [4, 5]]]])) B4 = tf.Variable(
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) plain_a = np.array([[1.2345, -5.4321], [-2.3456, 6.5432]]) plain_b = np.array([[7.6543], [-0.1234]]) const_v = np.array([0.666]) print(plain_a) print(plain_b) print(const_v) share_a = tf.Variable(rtt.private_input(0, plain_a)) share_b = tf.Variable(rtt.private_input(1, plain_b)) const_v = tf.constant(const_v) print(share_a) print(share_b) print(const_v) # init init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # ######### binary op # Add share_c = rtt.SecureAdd(share_a, share_b) share_d = rtt.SecureAdd(share_c, share_a)
real_y[i] = 0 real_Y = real_y oreal_X = real_X[:TEST_DATA_SIZE, :] oreal_Y = real_Y[:TEST_DATA_SIZE, :] real_X = oreal_X real_Y = oreal_Y start_time = time.time() if args.input_public: real_X = rtt.public_input(0, real_X) real_Y = rtt.public_input(0, real_Y) print("pystats rtt.public_input elapse:{0} s".format(time.time() - start_time)) else: real_X = rtt.private_input(0, real_X) real_Y = rtt.private_input(0, real_Y) print("pystats rtt.private_input elapse:{0} s".format(time.time() - start_time)) # ######################################## DIM_NUM = real_X.shape[1] print("DIM_NUM:", DIM_NUM) start_time = time.time() X = tf.placeholder(tf.float64, [None, DIM_NUM]) Y = tf.placeholder(tf.float64, [None, 1]) # print(X) # print(Y) # initialize W & b W = tf.Variable(tf.zeros([DIM_NUM, 1], dtype=tf.float64), name='w')
real_X = real_X[:100, :] real_Y = real_Y[:100, :] print(real_X) DIM_NUM = real_X.shape[1] X = tf.placeholder(tf.float64, [None, DIM_NUM]) Y = tf.placeholder(tf.float64, [None, 1]) print(X) print(Y) # initialize W & b #W = tf.Variable(tf.ones([DIM_NUM, 1], dtype=tf.float64)) #b = tf.Variable(tf.ones([1], dtype=tf.float64)) w0 = np.ones([DIM_NUM, 1]) b0 = np.ones([1]) W = tf.Variable(rtt.private_input(2, w0)) b = tf.Variable(rtt.private_input(2, b0)) print(W) print(b) # predict pred_Y = tf.matmul(X, W) + b print(pred_Y) XR = rtt.MpcReveal(X) YR = rtt.MpcReveal(Y) RR = rtt.MpcReveal(pred_Y) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) xr = sess.run(RR, feed_dict={X: real_X, Y: real_Y})
np.set_printoptions(suppress=True) protocol="helix" 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))
import latticex.rosetta as rtt import tensorflow as tf import sys import numpy as np np.set_printoptions(suppress=True) tf.set_random_seed(0) protocol = "SecureNN" protocol = "Helix" protocol = "Wolverine" rtt.activate(protocol) print("rtt.get_protocol_name():", rtt.get_protocol_name()) patyid = rtt.get_party_id() ############################### # single-samples ############################### x = [1.21933774, 1.66604676, 1.26618940, 1.29514586, 1.71182663, 1.44599081, 1.95028897, 1.29123498, 1.81044357, 1.13242592] logits = tf.Variable(rtt.private_input(0, x)) res1 = tf.nn.sigmoid(logits) init = tf.compat.v1.global_variables_initializer() with tf.Session() as sess: sess.run(init) res1 = sess.run(rtt.SecureReveal(res1)) print('rtt tf.nn.sigmoid res1:\n', res1) print('====================================================') rtt.deactivate()
print('data nodes', rtt.get_data_node_ids()) print('computation nodes', rtt.get_computation_node_ids()) print('result nodes', rtt.get_result_node_ids()) nodes = rtt.get_connected_node_ids() node = rtt.get_current_node_id() msgid = 'test' for n in nodes: if n != node: rtt.send_msg(n, msgid, node + " to " + n) for n in nodes: if n != node: msg = rtt.recv_msg(n, msgid, 2 * len(node) + 4) print('get msg from ', n, " msg:", msg) # Get private data from Alice (input x), Bob (input y) w = tf.Variable(rtt.private_input(0, [[1, 2], [2, 3]])) x = tf.Variable(rtt.private_input(1, [[1, 2], [2, 3]])) y = tf.Variable(rtt.private_input(2, [[1, 2], [2, 3]])) z = tf.Variable(rtt.private_input('p9', [[1, 2], [2, 3]])) # Define matmul operation res = tf.matmul(tf.matmul(w, x), tf.matmul(y, z)) # Start execution with tf.Session() as sess: sess.run(tf.global_variables_initializer()) res = sess.run(res) # Get the result of Rosetta matmul # ret: [[b'14.000000' b'20.000000'] [b'20.000000' b'29.000000']] receivers = (0, 1, 'p9', 2)
#!/bin/python3 # Example of Matmul for Rosetta import latticex.rosetta as rtt # import Rosetta import tensorflow as tf import numpy as np rtt.activate( "SecureNN" ) # activate the SecureNN protocol computation exectution environment x = tf.Variable(rtt.private_input(0, [[1, 2], [2, 3]])) # Alice private_input x y = tf.Variable(rtt.private_input(1, [[1, 2], [2, 3]])) # Bob private_input y res = tf.matmul(x, y) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) res = sess.run(res) print('Rosetta.matmul:', sess.run(rtt.SecureReveal(res)) ) # ret: [[b'14.000000' b'20.000000'] [b'20.000000' b'29.000000']] print('numpy.matmul:', np.matmul([[1, 2], [2, 3]], [[1, 2], [2, 3]]))
rtt.activate("SecureNN") # xa = tf.Variable(rtt.private_input(0, # [ # [0.12345678, 0.234567890], # [0.34567890, 0.03], # [1.3, 1.89], # [-1.3, -1.89], # [2.2, -2.2], # [-4.0, 4.0] # ] # )) xa = tf.constant(np.full(102400, 2.2)) wa = tf.Variable(rtt.private_input(0, np.full(102400, 2.2))) ret = wa # tf.multiply(xa, wa) print("xa:\n", xa) # init = tf.compat.v1.global_variables_initializer() sess = tf.compat.v1.Session() sess.run(init) # print("X:\n", sess.run(xa * 2)) ########### print("=========================== tf op sigmoid 1") xc = tf.sigmoid(ret) # xc = tf.nn.sigmoid(ret)
dataset_x = rtt.PrivateTextLineDataset(file_x, data_owner=0) # owner is p0 dataset_y = rtt.PrivateTextLineDataset(file_y, data_owner=1) # owner is p1 dataset_x = dataset_x\ .map(decode_p0)\ .batch(batch_size) dataset_y = dataset_y\ .map(decode_p1)\ .batch(batch_size) iter_x = dataset_x.make_initializable_iterator() iter_y = dataset_y.make_initializable_iterator() v = tf.Variable(rtt.private_input(0, np.ones([4, 1]))) batch_x = iter_x.get_next() batch_y = iter_y.get_next() data_vertical = tf.concat([batch_x, batch_y], axis=1) data_v_reveal = rtt.SecureReveal(data_vertical) result = rtt.SecureReveal(tf.matmul(data_vertical, v)) result_reveal = rtt.SecureReveal(result) init = tf.global_variables_initializer() batch = 0 with tf.compat.v1.Session() as sess: sess.run(init)