def test_sife_dynamic(): logger.info('test dynamic sife in separate roles ...') eta = 1000 sec_param = 256 max_test_value = 100 x = [random.randint(0, max_test_value) for i in range(eta)] y = [random.randint(0, max_test_value) for i in range(eta)] logger.debug("x: %s" % str(x)) logger.debug("y: %s" % str(y)) check_prod = sum(map(lambda i: x[i] * y[i], range(eta))) logger.debug('original dot product <x,y>: %d' % check_prod) logger.info('loading dlog configuration ...') with timer('load dlog config, cost time:', logger) as t: dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(role='enc') sife_dec_client = SIFEDynamicClient(role='dec', dlog=dlog) pk = sife_tpa.generate_public_key(len(x)) ct = sife_enc_client.encrypt(pk, x) sk = sife_tpa.generate_private_key(y) max_interprod = max_test_value * max_test_value * eta with timer('total decryption time:', logger) as t: dec_prod = sife_dec_client.decrypt(pk, sk, y, ct, max_interprod) logger.debug('decrypted dot product <x,y>: %d' % dec_prod)
def test_sife_basic_with_config(): logger.info("testing the correctness of sife using config file.") eta = 785 # prepare the test data max_test_value = 10 x = [random.randint(0, max_test_value) for i in range(eta)] y = [random.randint(0, max_test_value) for i in range(eta)] logger.debug("x: %s" % str(x)) logger.debug("y: %s" % str(y)) check_prod = sum(map(lambda i: x[i] * y[i], range(eta))) logger.debug('original dot product <x,y>: %d' % check_prod) logger.info('loading dlog configuration ...') with timer('load dlog config, cost time:', logger) as t: dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') sife = SIFEDynamic(eta, sec_param=256, sec_param_config=sec_param_config_file, dlog=dlog) sife.setup() pk = sife.generate_public_key(len(x)) ct = sife.encrypt(pk, x) sk = sife.generate_private_key(y) max_interprod = max_test_value * max_test_value * eta with timer('total decryption time:', logger) as t: dec_prod = sife.decrypt(pk, sk, y, ct, max_interprod) logger.debug('decrypted dot product <x,y>: %d' % dec_prod)
def test_mife_dynamic_separate(): logger.info('test dynamic mife in separate roles ...') setup_parties = { 'idx-1': 2, 'idx-2': 3, 'idx-3': 4, 'idx-4': 4, 'idx-5': 1, 'idx-6': 2 } logger.info('loading dlog configuration ...') with timer('load dlog config, cost time:', logger) as t: dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') mife_tpa = MIFEDynamicTPA(sec_param=256, parties=setup_parties, sec_param_config=sec_param_config_file) mife_tpa.setup() mife_enc_client = MIFEDynamicClient(sec_param=256, role='enc') mife_dec_client = MIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) enrolled_parties = {'idx-2': 3, 'idx-3': 4, 'idx-5': 1} # prepare the test data max_test_value = 100 x_dict = dict() x_vec_count = 0 x_vec = [] for idx in enrolled_parties.keys(): x_dict[idx] = [ random.randint(0, max_test_value) for m in range(enrolled_parties[idx]) ] x_vec_count = x_vec_count + enrolled_parties[idx] x_vec = x_vec + x_dict[idx] y_vec = [random.randint(0, max_test_value) for i in range(x_vec_count)] logger.debug("x: %s" % str(x_vec)) logger.debug("y: %s" % str(y_vec)) logger.debug('original dot product <x,y>: %d' % int(sum(np.array(x_vec) * np.array(y_vec)))) ct = dict() ct['parties'] = enrolled_parties ct['ct_dict'] = dict() for idx in enrolled_parties.keys(): pk = mife_tpa.generate_public_key(idx) ct['ct_dict'][idx] = mife_enc_client.encrypt(pk, x_dict[idx]) common_pk = mife_tpa.generate_common_public_key() sk = mife_tpa.generate_private_key(y_vec, enrolled_parties) max_inner_prod = 1000000 with timer('total decryption time:', logger) as t: dec_prod = mife_dec_client.decrypt(common_pk, sk, y_vec, ct, max_inner_prod) logger.debug('decrypted dot product <x,y>: %d' % dec_prod)
def test_secure2pc(): logger.info('initialize the crypto system ...') sec_param_config_file = 'config/sec_param.json' # indicate kernel size 5 dlog_table_config_file = 'config/dlog_b8.json' with timer('load sife config file, cost time', logger) as t: eta = 1000 sec_param = 256 dlog = load_dlog_table_config(dlog_table_config_file) sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(sec_param=256, role='enc') sife_dec_client = SIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) logger.info('the crypto system initialization done!') precision_data = 3 precision_weight = 3 secure2pc_client = Secure2PCClient(crypto=(sife_tpa, sife_enc_client), precision=precision_data) secure2pc_server = Secure2PCServer(crypto=(sife_tpa, sife_dec_client), precision=(precision_data, precision_weight)) x = np.array([[ [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., -0.41558442, -0.41558442, -0.41558442], [0., 0., -0.41558442, -0.41558442, -0.41558442], [0., 0., -0.41558442, -0.41558442, -0.41558442], ]]) y = np.array( [[[0.25199915, -0.27933214, -0.25121472, -0.01450092, -0.39264217], [-0.13325853, 0.02372098, -0.1099066, 0.07761139, -0.14452457], [0.18324971, 0.07725119, -0.05726616, 0.18969544, 0.0127556], [-0.08113805, 0.19654118, -0.37077826, 0.20517105, -0.35461632], [-0.0618344, -0.07832903, -0.02575814, -0.20281196, -0.31189417]]]) ct = secure2pc_client.execute(x) sk = secure2pc_server.request_key(y) dec = secure2pc_server.execute(sk, ct, y) logger.info('expected result %f' % np.sum(x * y)) logger.info('executed result %f' % dec)
def test_mife_basic_with_config(): logger.info("testing the correctness of mife using config file.") parties = {'idx-1': 2, 'idx-2': 3, 'idx-3': 4} # prepare the test data max_test_value = 100 x_dict = dict() x_vec_count = 0 x_vec = [] for idx in parties.keys(): x_dict[idx] = [ random.randint(0, max_test_value) for m in range(parties[idx]) ] x_vec_count = x_vec_count + parties[idx] x_vec = x_vec + x_dict[idx] y_vec = [random.randint(0, max_test_value) for i in range(x_vec_count)] logger.debug("x: %s" % str(x_vec)) logger.debug("y: %s" % str(y_vec)) logger.debug('original dot product <x,y>: %d' % int(sum(np.array(x_vec) * np.array(y_vec)))) logger.info('loading dlog configuration ...') with timer('load dlog config, cost time:', logger) as t: dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') mife = MIFEDynamic(sec_param=256, parties=parties, sec_param_config=sec_param_config_file, dlog=dlog) mife.setup() ct = dict() ct['parties'] = parties ct['ct_dict'] = dict() for idx in parties.keys(): pk = mife.generate_public_key(idx) ct['ct_dict'][idx] = mife.encrypt(pk, x_dict[idx]) common_pk = mife.generate_common_public_key() sk = mife.generate_private_key(y_vec, parties) max_inner_prod = 1000000 with timer('total decryption time:', logger) as t: dec_prod = mife.decrypt(common_pk, sk, y_vec, ct, max_inner_prod) logger.debug('decrypted dot product <x,y>: %d' % dec_prod)
def test_enhanced_secure2pc_sife(): logger.info('test enhanced secure2pc in sife setting') logger.info('initialize the crypto system ...') sec_param_config_file = 'config/sec_param.json' # indicate kernel size 5 dlog_table_config_file = 'config/dlog_b8.json' with timer('initialize crypto system, cost time', logger) as t: eta = 1000 sec_param = 256 setup_parties = {'id_1': 2, 'id_2': 5, 'id_3': 5, 'id_4': 3, 'id_5': 5} logger.info('loading dlog configuration ...') dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(sec_param=256, role='enc') sife_dec_client = SIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) mife_tpa = MIFEDynamicTPA(sec_param=256, parties=setup_parties, sec_param_config=sec_param_config_file) mife_tpa.setup() mife_enc_client = MIFEDynamicClient(sec_param=256, role='enc') mife_dec_client = MIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) logger.info('the crypto system initialization done!') precision_data = 3 precision_weight = 3 es2pc_client = EnhancedSecure2PCClient(sife=(sife_tpa, sife_enc_client), mife=(mife_tpa, mife_enc_client), precision=precision_data) es2pc_server = EnhancedSecure2PCServer(sife=(sife_tpa, sife_dec_client), mife=(mife_tpa, mife_dec_client), precision=(precision_data, precision_weight)) x = np.array([[ [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., -0.41558442, -0.41558442, -0.41558442], [0., 0., -0.41558442, -0.41558442, -0.41558442], [0., 0., -0.41558442, -0.41558442, -0.41558442], ]]) y = np.array( [[[0.25199915, -0.27933214, -0.25121472, -0.01450092, -0.39264217], [-0.13325853, 0.02372098, -0.1099066, 0.07761139, -0.14452457], [0.18324971, 0.07725119, -0.05726616, 0.18969544, 0.0127556], [-0.08113805, 0.19654118, -0.37077826, 0.20517105, -0.35461632], [-0.0618344, -0.07832903, -0.02575814, -0.20281196, -0.31189417]]]) ct = es2pc_client.execute(x, {'type': 'sife'}) sk = es2pc_server.request_key(y, {'type': 'sife'}) dec = es2pc_server.execute(sk, ct, y, {'type': 'sife'}) logger.info('expected result %f' % np.sum(x * y)) logger.info('executed result %f' % dec)
def test_enhanced_secure2pc_mife(): logger.info('test enhanced secure2pc in mife setting') logger.info('initialize the crypto system ...') sec_param_config_file = 'config/sec_param.json' # indicate kernel size 5 dlog_table_config_file = 'config/dlog_b8.json' with timer('initialize crypto system, cost time', logger) as t: eta = 1000 sec_param = 256 setup_parties = {'id_1': 2, 'id_2': 5, 'id_3': 5, 'id_4': 3, 'id_5': 5} logger.info('loading dlog configuration ...') dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(sec_param=256, role='enc') sife_dec_client = SIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) mife_tpa = MIFEDynamicTPA(sec_param=256, parties=setup_parties, sec_param_config=sec_param_config_file) mife_tpa.setup() mife_enc_client = MIFEDynamicClient(sec_param=256, role='enc') mife_dec_client = MIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) logger.info('the crypto system initialization done!') precision_data = 3 precision_weight = 3 es2pc_client = EnhancedSecure2PCClient(sife=(sife_tpa, sife_enc_client), mife=(mife_tpa, mife_enc_client), precision=precision_data) es2pc_server = EnhancedSecure2PCServer(sife=(sife_tpa, sife_dec_client), mife=(mife_tpa, mife_dec_client), precision=(precision_data, precision_weight)) x = np.array([ [-0.41558442, -0.41558442], [0., -0.41558442], [0., 0., -0.41558442], ]) y = np.array([[0.25199915, -0.25121472], [0.18324971, 0.07725119], [-0.0618344, -0.07832903, -0.02575814]]) expt_res = 0 for i in range(3): expt_res = expt_res + sum(np.array(x[i]) * np.array(y[i])) enrolled_parties = {'id_1': 2, 'id_3': 2, 'id_4': 3} ct = dict() ct['parties'] = enrolled_parties ct['ct_dict'] = dict() ct['ct_dict']['id_1'] = es2pc_client.execute(np.array(x[0]), { 'type': 'mife', 'id': 'id_1' }) ct['ct_dict']['id_3'] = es2pc_client.execute(np.array(x[1]), { 'type': 'mife', 'id': 'id_3' }) ct['ct_dict']['id_4'] = es2pc_client.execute(np.array(x[2]), { 'type': 'mife', 'id': 'id_4' }) y_prime = np.array(y[0] + y[1] + y[2]) sk = es2pc_server.request_key(y_prime, { 'type': 'mife', 'parties': enrolled_parties }) dec = es2pc_server.execute(sk, ct, y_prime, {'type': 'mife'}) logger.info('expected result %f' % expt_res) logger.info('executed result %f' % dec)
def test_nn_shallow_mnist_smc(): logger.info('test nn shallow mnist with secure 2pc setting') logger.info('initialize the crypto system ...') sec_param_config_file = 'config/sec_param.json' # indicate kernel size 5 dlog_table_config_file = 'config/dlog_b8.json' with timer('load sife config file, cost time', logger) as t: eta = 1250 sec_param = 256 dlog = load_dlog_table_config(dlog_table_config_file) sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(role='enc') sife_dec_client = SIFEDynamicClient(role='dec', dlog=dlog) logger.info('the crypto system initialization done!') precision_data = 0 precision_weight = 3 secure2pc_client = Secure2PCClient(sife=(sife_tpa, sife_enc_client), precision=precision_data) secure2pc_server = Secure2PCServer(sife=(sife_tpa, sife_dec_client), precision=(precision_data, precision_weight)) X_train, y_train = load_mnist_size('datasets/mnist', size=600) X_test, y_test = load_mnist_size('datasets/mnist', size=100, kind='t10k') # X_train, y_train = load_mnist('datasets/mnist') # X_test, y_test = load_mnist('datasets/mnist', kind='t10k') # shuffle X_data, y_data = X_train.copy(), y_train.copy() idx = np.random.permutation(X_data.shape[0]) X_data, y_data = X_data[idx], y_data[idx] total_mini_batches = 50 nn_client = CryptoNNClient(n_output=10, mini_batches=total_mini_batches, n_features=X_data.shape[1], smc=secure2pc_client, random_seed=520) nn_server = CryptoNNServer(n_output=10, n_features=X_data.shape[1], hidden_layers=[64], l2=0.1, l1=0.0, epochs=1, eta=0.001, alpha=0.001, decrease_const=0.0001, mini_batches=total_mini_batches, smc=secure2pc_server) logger.info('client start to encrypt dataset ...') ct_feedforward_lst, ct_backpropagation_lst, y_onehot_lst = nn_client.pre_process( X_data, y_data) logger.info('client encrypting DONE') logger.info('server start to train ...') with timer('training using secure2pc setting - 10 batches', logger) as t: (train_loss_hist, test_acc_hist, train_batch_time_hist, train_time_hist) = nn_server.fit( (ct_feedforward_lst, ct_backpropagation_lst), y_onehot_lst, X_test, y_test) logger.info('server training DONE') logger.info('training loss: \n\r' + str(train_loss_hist)) logger.info('test acc: \n\r' + str(test_acc_hist))
def test_nn_shallow_mnist_smc_enhanced(): logger.info('test nn shallow in mnist using enhanced smc') logger.info('initialize the crypto system ...') sec_param_config_file = 'config/sec_param.json' # indicate kernel size 5 dlog_table_config_file = 'config/dlog_b8.json' with timer('initialize crypto system, cost time', logger) as t: eta = 1250 sec_param = 256 setup_parties = { 'id_1': 200, 'id_2': 200, 'id_3': 200, 'id_4': 200, 'id_5': 200 } logger.info('loading dlog configuration ...') dlog = load_dlog_table_config(dlog_table_config_file) logger.info('load dlog configuration DONE') sife_tpa = SIFEDynamicTPA(eta, sec_param=sec_param, sec_param_config=sec_param_config_file) sife_tpa.setup() sife_enc_client = SIFEDynamicClient(sec_param=256, role='enc') sife_dec_client = SIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) mife_tpa = MIFEDynamicTPA(sec_param=256, parties=setup_parties, sec_param_config=sec_param_config_file) mife_tpa.setup() mife_enc_client = MIFEDynamicClient(sec_param=256, role='enc') mife_dec_client = MIFEDynamicClient(sec_param=256, role='dec', dlog=dlog) logger.info('the crypto system initialization done!') precision_data = 0 precision_weight = 4 es2pc_client = EnhancedSecure2PCClient(sife=(sife_tpa, sife_enc_client), mife=(mife_tpa, mife_enc_client), precision=precision_data) es2pc_server = EnhancedSecure2PCServer(sife=(sife_tpa, sife_dec_client), mife=(mife_tpa, mife_dec_client), precision=(precision_data, precision_weight)) X_train, y_train = load_mnist_size('datasets/mnist', size=600) X_test, y_test = load_mnist_size('datasets/mnist', size=100, kind='t10k') # X_train, y_train = load_mnist('datasets/mnist') # X_test, y_test = load_mnist('datasets/mnist', kind='t10k') # shuffle X_data, y_data = X_train.copy(), y_train.copy() idx = np.random.permutation(X_data.shape[0]) X_data, y_data = X_data[idx], y_data[idx] features_splits = np.array_split(range(X_data.shape[1]), len(setup_parties)) X_data_lst = [X_data[:, idx] for idx in features_splits] total_mini_batches = 50 nn_server = CryptoNNServer(n_output=10, n_features=X_data.shape[1], hidden_layers=[64], l2=0.1, l1=0.0, epochs=50, eta=0.001, alpha=0.001, decrease_const=0.0001, mini_batches=total_mini_batches, smc=es2pc_server) logger.info('client start to encrypt dataset ...') ct_ff_lst_dict = dict() ct_bp_lst_dict = dict() x_idx_count = 0 final_y_onehot_lst = None for id in setup_parties.keys(): if x_idx_count == (len(setup_parties) - 1): n_features = X_data_lst[x_idx_count].shape[1] + 1 nn_client = CryptoNNClient(n_output=10, mini_batches=total_mini_batches, n_features=n_features, smc=es2pc_client, random_seed=520, id=id) nn_server.register(nn_client) ct_feedforward_lst, ct_backpropagation_lst, y_onehot_lst = nn_client.pre_process( X_data_lst[x_idx_count], y_data) ct_ff_lst_dict[id] = ct_feedforward_lst ct_bp_lst_dict[id] = ct_backpropagation_lst final_y_onehot_lst = y_onehot_lst else: n_features = X_data_lst[x_idx_count].shape[1] nn_client = CryptoNNClient(n_output=10, mini_batches=total_mini_batches, n_features=n_features, smc=es2pc_client, random_seed=520, id=id) nn_server.register(nn_client) ct_feedforward_lst, ct_backpropagation_lst = nn_client.pre_process( X_data_lst[x_idx_count]) ct_ff_lst_dict[id] = ct_feedforward_lst ct_bp_lst_dict[id] = ct_backpropagation_lst x_idx_count = x_idx_count + 1 logger.info('client encrypting DONE') logger.info('server start to train ...') (train_loss_hist, test_acc_hist, train_batch_time_hist, train_time_hist) = nn_server.fit((ct_ff_lst_dict, ct_bp_lst_dict), final_y_onehot_lst, X_test, y_test) logger.info('server training DONE') logger.info('training loss: \n\r' + str(train_loss_hist)) logger.info('test acc: \n\r' + str(test_acc_hist))