def test_tensor_op(self): arr1 = np.ones((10, 1, 3)) arr1[0] = np.array([[2, 3, 4]]) arr2 = np.ones((10, 3, 3)) arr3 = np.ones([1, 1, 3]) arr4 = np.ones([50, 1]) arr5 = np.ones([32]) pt = PaillierTensor(ori_data=arr1) pt2 = PaillierTensor(ori_data=arr2) pt3 = PaillierTensor(ori_data=arr3) pt4 = PaillierTensor(ori_data=arr4) pt5 = PaillierTensor(ori_data=arr5) encrypter = PaillierEncrypt() encrypter.generate_key(EncryptParam().key_length) encrypted_calculator = EncryptModeCalculator( encrypter, EncryptedModeCalculatorParam().mode, EncryptedModeCalculatorParam().re_encrypted_rate) rs1 = pt * arr2 rs2 = pt * pt2 rs3 = pt.matmul_3d(pt2) enpt = pt2.encrypt(encrypted_calculator) enrs = enpt.matmul_3d(arr1, multiply='right') rng_generator = random_number_generator.RandomNumberGenerator() enpt2 = pt4.encrypt(encrypted_calculator) random_num = rng_generator.generate_random_number(enpt2.shape)
def forward(self, host_input, epoch=0, batch=0): if batch >= len(self.train_encrypted_calculator): self.train_encrypted_calculator.append( self.generated_encrypted_calculator()) LOGGER.info( "forward propagation: encrypt host_bottom_output of epoch {} batch {}" .format(epoch, batch)) host_input = PaillierTensor(ori_data=host_input, partitions=self.partitions) encrypted_host_input = host_input.encrypt( self.train_encrypted_calculator[batch]) self.send_host_encrypted_forward_to_guest( encrypted_host_input.get_obj(), epoch, batch) encrypted_guest_forward = PaillierTensor( tb_obj=self.get_guest_encrypted_forwrad_from_guest(epoch, batch)) decrypted_guest_forward = encrypted_guest_forward.decrypt( self.encrypter) if self.acc_noise is None: self.input_shape = host_input.shape[1] self.output_unit = encrypted_guest_forward.shape[1] self.acc_noise = np.zeros((self.input_shape, self.output_unit)) """some bugs here""" decrypted_guest_forward_with_noise = decrypted_guest_forward + host_input * self.acc_noise self.send_decrypted_guest_forward_with_noise_to_guest( decrypted_guest_forward_with_noise.get_obj(), epoch, batch)
def encrypt_tensor(self, components, return_dtable=True): """ transform numpy array into Paillier tensor and encrypt """ if len(self.encrypt_calculators) == 0: self.encrypt_calculators = [self.generated_encrypted_calculator() for i in range(3)] encrypted_tensors = [] for comp, calculator in zip(components, self.encrypt_calculators): encrypted_tensor = PaillierTensor(ori_data=comp, partitions=self.partitions) if return_dtable: encrypted_tensors.append(encrypted_tensor.encrypt(calculator).get_obj()) else: encrypted_tensors.append(encrypted_tensor.encrypt(calculator)) return encrypted_tensors
def forward(self, host_input, epoch=0, batch=0, train=True): if batch >= len(self.train_encrypted_calculator): self.train_encrypted_calculator.append( self.generated_encrypted_calculator()) LOGGER.info( "forward propagation: encrypt host_bottom_output of epoch {} batch {}" .format(epoch, batch)) host_input = PaillierTensor(ori_data=host_input, partitions=self.partitions) encrypted_host_input = host_input.encrypt( self.train_encrypted_calculator[batch]) self.send_host_encrypted_forward_to_guest( encrypted_host_input.get_obj(), epoch, batch) encrypted_guest_forward = PaillierTensor( tb_obj=self.get_guest_encrypted_forwrad_from_guest(epoch, batch)) decrypted_guest_forward = encrypted_guest_forward.decrypt( self.encrypter) if self.fixed_point_encoder: decrypted_guest_forward = decrypted_guest_forward.decode( self.fixed_point_encoder) if self.acc_noise is None: self.input_shape = host_input.shape[1] self.output_unit = self.get_interactive_layer_output_unit() self.acc_noise = np.zeros((self.input_shape, self.output_unit)) mask_table = None if train and self.drop_out_keep_rate and self.drop_out_keep_rate < 1: mask_table = self.get_interactive_layer_drop_out_table( epoch, batch) if mask_table: decrypted_guest_forward_with_noise = decrypted_guest_forward + ( host_input * self.acc_noise).select_columns(mask_table) self.mask_table = mask_table else: decrypted_guest_forward_with_noise = decrypted_guest_forward + ( host_input * self.acc_noise) self.send_decrypted_guest_forward_with_noise_to_guest( decrypted_guest_forward_with_noise.get_obj(), epoch, batch)
class TestPaillierTensor(unittest.TestCase): def setUp(self): session.init(str(random.randint(0, 10000000)), 0) self.data1 = np.ones((1000, 10)) self.data2 = np.ones((1000, 10)) self.paillier_tensor1 = PaillierTensor(ori_data=self.data1, partitions=10) self.paillier_tensor2 = PaillierTensor(ori_data=self.data2, partitions=10) def test_tensor_add(self): paillier_tensor = self.paillier_tensor1 + self.paillier_tensor2 self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == self.paillier_tensor1.shape) arr = paillier_tensor.numpy() self.assertTrue(abs(arr.sum() - 20000) < consts.FLOAT_ZERO) def test_ndarray_add(self): paillier_tensor = self.paillier_tensor1 + np.ones(10) self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == self.paillier_tensor1.shape) arr = paillier_tensor.numpy() self.assertTrue(abs(arr.sum() - 20000) < consts.FLOAT_ZERO) def test_tensor_sub(self): paillier_tensor = self.paillier_tensor1 - self.paillier_tensor2 self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == self.paillier_tensor1.shape) arr = paillier_tensor.numpy() self.assertTrue(abs(arr.sum()) < consts.FLOAT_ZERO) def test_tensor_sub(self): paillier_tensor = self.paillier_tensor1 - np.ones(10) self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == self.paillier_tensor1.shape) arr = paillier_tensor.numpy() self.assertTrue(abs(arr.sum()) < consts.FLOAT_ZERO) def test_constant_mul(self): paillier_tensor = self.paillier_tensor1 * 10 self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == self.paillier_tensor1.shape) arr = paillier_tensor.numpy() self.assertTrue(abs(arr.sum() - 100000) < consts.FLOAT_ZERO) def test_inverse(self): paillier_tensor = self.paillier_tensor2.T self.assertTrue(isinstance(paillier_tensor, PaillierTensor)) self.assertTrue(paillier_tensor.shape == tuple([10, 1000])) def test_get_partition(self): self.assertTrue(self.paillier_tensor1.partitions == 10) def test_mean(self): self.assertTrue(abs(self.paillier_tensor1.mean() - 1.0) < consts.FLOAT_ZERO) def test_encrypt_and_decrypt(self): from federatedml.secureprotol import PaillierEncrypt from federatedml.secureprotol.encrypt_mode import EncryptModeCalculator encrypter = PaillierEncrypt() encrypter.generate_key(1024) encrypted_calculator = EncryptModeCalculator(encrypter, "fast") encrypter_tensor = self.paillier_tensor1.encrypt(encrypted_calculator) decrypted_tensor = encrypter_tensor.decrypt(encrypter) self.assertTrue(isinstance(encrypter_tensor, PaillierTensor)) self.assertTrue(isinstance(decrypted_tensor, PaillierTensor)) arr = decrypted_tensor.numpy() self.assertTrue(abs(arr.sum() - 10000) < consts.FLOAT_ZERO)