def _update_gradients(self): # enc_y_overlap_2_phi_uB_overlap_2 was calculated by host if self.is_trace: self.logger.debug("enc_y_overlap_2_phi_uB_overlap_2 shape" + str(self.enc_y_overlap_2_phi_uB_overlap_2.shape)) if self.host_public_key is not None and self.public_key != self.host_public_key: # TODO: decrypt enc_y_overlap_2_phi_uB_overlap_2 self.enc_y_overlap_2_phi_uB_overlap_2 = decrypt_matrix( self.private_key, self.enc_y_overlap_2_phi_uB_overlap_2) y_overlap = np.tile(self.y_overlap, (1, self.enc_uB_overlap.shape[-1])) enc_y_overlap_uB_overlap = compute_sum_XY(y_overlap * 0.5, self.enc_uB_overlap) enc_const = np.sum(self.enc_y_overlap_2_phi_uB_overlap_2, axis=0) - enc_y_overlap_uB_overlap enc_const_overlap = np.tile(enc_const, (len(self.overlap_indexes), 1)) enc_const_nonoverlap = np.tile(enc_const, (len(self.non_overlap_indexes), 1)) y_non_overlap = np.tile(self.y[self.non_overlap_indexes], (1, self.enc_uB_overlap.shape[-1])) if self.is_trace: self.logger.debug("enc_const shape:" + str(enc_const.shape)) self.logger.debug("enc_const_overlap shape" + str(enc_const_overlap.shape)) self.logger.debug("enc_const_nonoverlap shape" + str(enc_const_nonoverlap.shape)) self.logger.debug("y_non_overlap shape" + str(y_non_overlap.shape)) enc_grad_A_nonoverlap = compute_XY( self.alpha * y_non_overlap / len(self.y), enc_const_nonoverlap) enc_grad_A_overlap = compute_XY_plus_Z( self.alpha * y_overlap / len(self.y), enc_const_overlap, self.enc_mapping_comp_B) if self.is_trace: self.logger.debug("enc_grad_A_nonoverlap shape" + str(enc_grad_A_nonoverlap.shape)) self.logger.debug("enc_grad_A_overlap shape" + str(enc_grad_A_overlap.shape)) enc_loss_grad_A = [[0 for _ in range(self.enc_uB_overlap.shape[1])] for _ in range(len(self.y))] for i, j in enumerate(self.non_overlap_indexes): enc_loss_grad_A[j] = enc_grad_A_nonoverlap[i] for i, j in enumerate(self.overlap_indexes): enc_loss_grad_A[j] = enc_grad_A_overlap[i] enc_loss_grad_A = np.array(enc_loss_grad_A) if self.is_trace: self.logger.debug("enc_loss_grad_A shape" + str(enc_loss_grad_A.shape)) self.logger.debug("enc_loss_grad_A" + str(enc_loss_grad_A)) self.loss_grads = enc_loss_grad_A self.enc_grads_W, self.enc_grads_b = self.localModel.compute_encrypted_params_grads( self.X, enc_loss_grad_A)
def __test(self, matrix): paillierEncrypt = PaillierEncrypt() paillierEncrypt.generate_key() publickey = paillierEncrypt.get_public_key() privatekey = paillierEncrypt.get_privacy_key() result = encrypt_matrix(publickey, matrix) decrypted_result = decrypt_matrix(privatekey, result) assert_matrix(matrix, decrypted_result)
def __compute_encrypt_loss_y(self, enc_uB_overlap, y_overlap, phi): if self.host_public_key is not None and self.public_key != self.host_public_key: self.enc_phi_uB_overlap_2_phi = decrypt_matrix( self.private_key, self.enc_phi_uB_overlap_2_phi) enc_uB_phi = encrypt_matmul_2_ob(enc_uB_overlap, phi.transpose()) enc_loss_y = (-0.5 * compute_sum_XY(y_overlap, enc_uB_phi)[0] + 1.0 / 8 * np.sum(self.enc_phi_uB_overlap_2_phi) ) + len(y_overlap) * np.log(2) return enc_loss_y
def __test_matrix(self, matrix): paillierEncrypt = PaillierEncrypt() paillierEncrypt.generate_key() publickey = paillierEncrypt.get_public_key() privatekey = paillierEncrypt.get_privacy_key() enc_matrix = encrypt_matrix(publickey, matrix) masked_enc_matrix, mask = add_random_mask(enc_matrix) cleared_enc_matrix = remove_random_mask(masked_enc_matrix, mask) cleared_matrix = decrypt_matrix(privatekey, cleared_enc_matrix) assert_matrix(matrix, cleared_matrix)
def _update_gradients(self): # enc_uB_overlap_y_overlap_2_phi_2 was calculated by guest if self.guest_public_key is not None and self.public_key != self.guest_public_key: self.enc_uB_overlap_y_overlap_2_phi_2 = decrypt_matrix( self.private_key, self.enc_uB_overlap_y_overlap_2_phi_2) pass enc_l1_grad_B = compute_X_plus_Y(self.enc_uB_overlap_y_overlap_2_phi_2, self.enc_y_overlap_phi) enc_loss_grad_B = compute_X_plus_Y(self.alpha * enc_l1_grad_B, self.enc_mapping_comp_A) self.loss_grads = enc_loss_grad_B self.enc_grads_W, self.enc_grads_b = self.localModel.compute_encrypted_params_grads( self.X[self.overlap_indexes], enc_loss_grad_B)
def __decrypt_gradients(self, encrypt_gradients): return decrypt_matrix(self.private_key, encrypt_gradients[0]), decrypt_array( self.private_key, encrypt_gradients[1])