def test_add_diff(): secret_key = b'1' * 48 encryptor1 = generate_onetime_pad_encryptor(secret_key) encryptor2 = generate_onetime_pad_encryptor(secret_key) x = np.array([random.random()], dtype=np.float32) y = np.array([random.random()], dtype=np.float32) enc_x = encryptor1.encrypt(x, alpha=-1) enc_y = encryptor2.encrypt(y, alpha=2) assert almost_equal(encryptor1.decrypt(enc_x + enc_y, alpha=1), x + y) x = np.random.uniform(-1, 1, (512, )).astype(np.float32) y = np.random.uniform(-1, 1, (512, )).astype(np.float32) enc_x = encryptor1.encrypt(x, alpha=-1) enc_y = encryptor2.encrypt(y, alpha=2) assert almost_equal(encryptor1.decrypt(enc_x + enc_y, alpha=1), (x + y)) x = torch.Tensor(512, ).uniform_(-1, 1) y = torch.Tensor(512, ).uniform_(-1, 1) enc_x = encryptor1.encrypt(x, alpha=-1) enc_y = encryptor2.encrypt(y, alpha=2) assert almost_equal(encryptor1.decrypt(enc_x + enc_y, alpha=1), (x + y)) x = [[np.random.uniform(-1, 1, (512, )).astype(np.float32)], [torch.Tensor(512, ).uniform_(-1, 1)]] y = [[np.random.uniform(-1, 1, (512, )).astype(np.float32)], [torch.Tensor(512, ).uniform_(-1, 1)]] enc_x = encryptor1.encrypt(x, alpha=-1) enc_y = encryptor2.encrypt(y, alpha=2) assert almost_equal(encryptor1.decrypt(enc_x + enc_y, alpha=1), iterative_add(x, y))
def test_mul(): x1 = random.random() x2 = random.random() y1 = np.random.random(100).astype(np.float32) y2 = np.random.random(100).astype(np.float32) enc_x1 = pe.encrypt(x1) enc_y1 = pe.encrypt(y1) assert almost_equal(pd.decrypt(enc_x1 * x2), x1 * x2) # float * float assert almost_equal(pd.decrypt(enc_y1 * x1), y1 * x1) # float * array assert almost_equal(pd.decrypt(enc_y1 * y2), y1 * y2) # array * array
def test_add_mul_numpy_parallel(): x = np.random.random(100).astype(np.float32) y = np.random.random(100).astype(np.float32) en_x = pe.encrypt(x) en_y = pe.encrypt(y) en_result = parallel_ops.add(en_x, y) result = pd.decrypt(en_result) assert almost_equal(x + y, result) en_result = parallel_ops.add(en_x, en_y) result = pd.decrypt(en_result) assert almost_equal(x + y, result) en_result = parallel_ops.mul(en_x, y) result = pd.decrypt(en_result) assert almost_equal(x * y, result)
def test_encrypt_decrypt(): secret_key = b'1' * 48 encryptor = generate_onetime_pad_encryptor(secret_key) x = np.random.uniform(-1, 1, (512, )).astype(np.float32) en_x = encryptor.encrypt(x, alpha=1) z = encryptor.decrypt(en_x, alpha=1) assert almost_equal(z, x) x = torch.Tensor(512, ).uniform_(-1, 1) en_x = encryptor.encrypt(x, alpha=1) z = encryptor.decrypt(en_x, alpha=1) assert almost_equal(z, x) x = [[np.random.uniform(-1, 1, (512, )).astype(np.float32)], [torch.Tensor(512, ).uniform_(-1, 1)]] en_x = encryptor.encrypt(x, alpha=1) z = encryptor.decrypt(en_x, alpha=1) assert almost_equal(z, x)
def test_encrypt_decrypt(): # float plain_param1 = random.random() encrypted_param1 = pe.encrypt(plain_param1) plain_param2 = pd.decrypt(encrypted_param1) assert almost_equal(plain_param1, plain_param2) # one dim array plain_param1 = np.random.random(100).astype(np.float32) encrypted_param1 = pe.encrypt(plain_param1) plain_param2 = pd.decrypt(encrypted_param1) assert almost_equal(plain_param1, plain_param2) # two dim array plain_param1 = np.random.random((128, 4)).astype(np.float32) encrypted_param1 = pe.encrypt(plain_param1) plain_param2 = pd.decrypt(encrypted_param1) assert almost_equal(plain_param1, plain_param2)
def test_he_otp_lr_ft1(): federal_info = fed_conf_guest sec_param = {"he_algo": 'paillier', "he_key_length": 1024} prng = RandomState(0) guest_theta = prng.uniform(-1, 1, (6, )) guest_features = prng.uniform(-1, 1, (32, 6)) guest_labels = prng.randint(0, 2, (32, )) host_theta = prng.uniform(-1, 1, (6, )) host_features = prng.uniform(-1, 1, (32, 6)) def calu_grad(guest_theta, guest_features, guest_labels, host_theta, host_features): u2 = host_theta.dot(host_features.T) u1 = guest_theta.dot(guest_features.T) u = u1 + u2 h_x = 1 / (1 + np.exp(-u)) batch_size = guest_features.shape[0] grads = (-1 / batch_size) * ((guest_labels - h_x).dot(guest_features)) return h_x, grads # print(guest_theta, guest_features, guest_labels) trainer = make_protocol(HE_OTP_LR_FT1, federal_info, sec_param, algo_param=None) # 联邦计算结果 fed_h_x, fed_grads = trainer.exchange(guest_theta, guest_features, guest_labels) # 本地计算结果 local_h_x, local_grads = calu_grad(guest_theta, guest_features, guest_labels, host_theta, host_features) assert almost_equal(fed_h_x, local_h_x) assert almost_equal(fed_grads, local_grads)
def test_add_decode(): secret_key = b'1' * 48 encryptor1 = generate_onetime_pad_encryptor(secret_key) encryptor2 = generate_onetime_pad_encryptor(secret_key) x = [[np.random.uniform(-1, 1, (512, )).astype(np.float32)], [torch.Tensor(512, ).uniform_(-1, 1)]] y = [[np.random.uniform(-1, 1, (512, )).astype(np.float32)], [torch.Tensor(512, ).uniform_(-1, 1)]] enc_x = encryptor1.encrypt(x, alpha=-1) enc_y = encryptor2.encrypt(y, alpha=1) assert almost_equal((enc_x + enc_y).decode(), iterative_add(x, y))
def test_he_linear_ft(): # host和guest的随机初始状态相同 prng = RandomState(0) guest_u = np.array(prng.uniform(-1, 1, (8, ))) host_u = np.array(prng.uniform(-1, 1, (8, ))) guest_labels = np.array(prng.randint(0, 2, (8, ))) federal_info = fed_conf_host sec_param = {"he_algo": 'paillier', "he_key_length": 1024} trainer = make_protocol(HE_LINEAR_FT, federal_info, sec_param, algo_param=None) result = trainer.exchange(host_u) assert almost_equal(result, guest_u + host_u - guest_labels)
def test_predict(): u1 = [ np.array([0.1, 0.05, -3.6, 25.8], dtype=np.float32), np.array([-0.5, 11.2, 9.5], dtype=np.float32) ] u2 = [ np.array([0.3, -14, -2.5, 1.7], dtype=np.float32), np.array([0.2, 1.2, -5.6], dtype=np.float32) ] expected_u = [u1[i] + u2[i] for i in range(len(u1))] federal_info = fed_conf_guest sec_param = {"he_algo": 'paillier', "he_key_length": 1024} protocol = make_protocol(HE_LR_FP, federal_info, sec_param, None) for i in range(len(u1)): u = protocol.exchange(u1[i]) assert almost_equal(u, expected_u[i])
def test(): theta = [[ np.random.uniform(-1, 1, (2, 4)).astype(np.float32), np.random.uniform(-1, 1, (2, 6)).astype(np.float32) ], [np.random.uniform(-1, 1, (2, 8)).astype(np.float32)]] print(theta) federal_info = fed_conf_host sec_param = {"key_exchange_size": 2048} trainer = make_protocol(OTP_SA_FT, federal_info, sec_param) result = trainer.exchange(theta) var_chan = make_variable_channel('test_otp_sa_ft', fed_conf_host["federation"]["host"][0], fed_conf_host["federation"]["guest"][0]) guest_theta = var_chan.recv(tag='theta') sum_theta = iterative_add(theta, guest_theta) # 本地计算的平均梯度 avg_theta = iterative_divide(sum_theta, 2.0) assert almost_equal(result, avg_theta)
def test_add(): x1 = random.random() x2 = random.random() y1 = np.random.random(100).astype(np.float32) y2 = np.random.random(100).astype(np.float32) z1 = np.random.random((128, 4)).astype(np.float32) z2 = np.random.random((128, 4)).astype(np.float32) enc_x1 = pe.encrypt(x1) enc_x2 = pe.encrypt(x2) enc_y1 = pe.encrypt(y1) enc_y2 = pe.encrypt(y2) enc_z1 = pe.encrypt(z1) enc_z2 = pe.encrypt(z2) assert almost_equal(pd.decrypt(enc_x1 + enc_x2), x1 + x2) assert almost_equal(pd.decrypt(enc_y1 + enc_y2), y1 + y2) assert almost_equal(pd.decrypt(enc_z1 + enc_z2), z1 + z2) assert almost_equal(pd.decrypt(enc_x1 + x2), x1 + x2) assert almost_equal(pd.decrypt(enc_y1 + y2), y1 + y2) assert almost_equal(pd.decrypt(enc_z1 + z2), z1 + z2)