def poly(ciphertext): coeff = [1, 2, 3, 4, 5] result = heaan.Ciphertext() ciphertext_tmp0 = deepcopy(ciphertext) ciphertext_tmp1 = heaan.Ciphertext() ciphertext_tmp2 = heaan.Ciphertext() ciphertext_result = heaan.Ciphertext() for index, value in enumerate(coeff): if index == 0: coeff_zero = value elif index == 1: evaluator.mult(ciphertext, value, ciphertext_tmp1) else: evaluator.mult(ciphertext, ciphertext_tmp0, mult_key, ciphertext_tmp0) evaluator.mult(ciphertext_tmp0, value, ciphertext_tmp2) evaluator.add(ciphertext_tmp1, ciphertext_tmp2, ciphertext_tmp1) evaluator.add(ciphertext_tmp1, coeff_zero, ciphertext_result) return ciphertext_result
def const_basic(ciphertext, const): const_add = heaan.Ciphertext() const_sub = heaan.Ciphertext() const_mult = heaan.Ciphertext() evaluator.add(ciphertext, const, const_add) evaluator.sub(ciphertext, const, const_add) evaluator.mult(ciphertext, const, const_mult) return const_add, const_sub, const_mult
def ctxt_basic(ciphertext_a, ciphertext_b): ciphertext_add = heaan.Ciphertext() ciphertext_sub = heaan.Ciphertext() ciphertext_mult = heaan.Ciphertext() evaluator.add(ciphertext_a, ciphertext_b, ciphertext_add) evaluator.sub(ciphertext_a, ciphertext_b, ciphertext_sub) evaluator.mult(ciphertext_a, ciphertext_b, mult_key, ciphertext_mult) return ciphertext_add, ciphertext_sub, ciphertext_mult
def ctxt_rotate(ciphertext, rot_idx): ciphertext_leftrotate = heaan.Ciphertext() ciphertext_rightrotate = heaan.Ciphertext() evaluator.left_rotate( ciphertext, rot_idx, public_key_pack, ciphertext_leftrotate ) # we left rotated ciphertext by rot_idx and saved the result to the ciphertext_leftrotate. evaluator.right_rotate(ciphertext, rot_idx, public_key_pack, ciphertext_rightrotate ) # NOTE THAT rot_idx IS RESTRICTED BY POWER OF 2 return ciphertext_leftrotate, ciphertext_rightrotate
def ctxt_average(ciphertext, N): for i in range(int(math.log(N, 2))): ciphertext_tmp = heaan.Ciphertext() evaluator.left_rotate(ciphertext, 1 << i, public_key_pack, ciphertext_tmp) evaluator.add(ciphertext, ciphertext_tmp, ciphertext) pass ciphertext_avg = heaan.Ciphertext() evaluator.mult(ciphertext, 1.0 / N, ciphertext_avg) return ciphertext_avg
def ctxt_variance(self, ciphertext): ciphertext_squ = heaan.Ciphertext() evaluator.mult(ciphertext, ciphertext, mult_key, ciphertext_squ) ciphertext_squ_avg = self.ctxt_average(ciphertext_squ) ciphertext_avg = heaan.Ciphertext() ciphertext_avg = self.ctxt_average(ciphertext) ciphertext_avg_squ = heaan.Ciphertext() evaluator.mult(ciphertext_avg, ciphertext_avg, mult_key, ciphertext_avg_squ) ciphertext_var = heaan.Ciphertext() evaluator.sub(ciphertext_squ_avg, ciphertext_avg_squ, ciphertext_var) return ciphertext_var
def rotate_copy(self, ciphertext, num_vectors, num_slots_per_vec, vector_idx): # copy slots of the (vector_idx)-th vector and paste them to the other vectors num_vec_per_data = self.num_slots // num_slots_per_vec vector_idx_per_data = vector_idx % num_vec_per_data ctxt_out = heaan.Ciphertext() mask = heaan.Message( self._pad_z(num_slots_per_vec, num_vectors, vector_idx_per_data)) evaluator.mult(ciphertext, mask, mult_key, ctxt_out) log_num_vec_per_data = math.ceil(math.log2(num_vec_per_data)) ctxt_tmp = heaan.Ciphertext() for idx in range(int(log_num_vec_per_data)): rot_idx = num_slots_per_vec * (2**idx) evaluator.right_rotate(ctxt_out, rot_idx, public_key_pack, ctxt_tmp) evaluator.add(ctxt_out, ctxt_tmp, ctxt_out)
def rotate_partial_sum(self, ciphertext, num_slots_per_vec): # sum all the slots per row and place it on the first slot ctxt_out = deepcopy(ciphertext) mask = heaan.Message(self._pad_x(num_slots_per_vec, 0)) log_num_slots_per_vec = math.ceil(math.log2(num_slots_per_vec)) ctxt_tmp = heaan.Ciphertext() for idx in range(int(log_num_slots_per_vec)): rot_idx = 2**idx evaluator.left_rotate(ctxt_out, rot_idx, public_key_pack, ctxt_tmp) evaluator.add(ctxt_out, ctxt_tmp, ctxt_out) evaluator.mult(ctxt_out, mask, mult_key, ctxt_out) return ctxt_out
secret_key = heaan.SecretKey(context) public_key_path = "./public_key_path" public_key_pack = heaan.PublicKeyPack(context, secret_key, public_key_path) conj_key = public_key_pack.get_conj_key() enc_key = public_key_pack.get_enc_key() mult_key = public_key_pack.get_mult_key() encryptor = heaan.Encryptor(context) evaluator = heaan.HomEvaluator(context) # Step 3. Generating Messages _list = [i for i in range(128)] msg = heaan.Message(_list) # Step 4. Encrypt Message to Ciphertext ciphertext_a = heaan.Ciphertext() encryptor.encrypt(msg, enc_key, ciphertext_a) def ctxt_rotate(ciphertext, rot_idx): ciphertext_leftrotate = heaan.Ciphertext() ciphertext_rightrotate = heaan.Ciphertext() evaluator.left_rotate( ciphertext, rot_idx, public_key_pack, ciphertext_leftrotate ) # we left rotated ciphertext by rot_idx and saved the result to the ciphertext_leftrotate. evaluator.right_rotate(ciphertext, rot_idx, public_key_pack, ciphertext_rightrotate ) # NOTE THAT rot_idx IS RESTRICTED BY POWER OF 2
# 저장된 공개키 로드/ public_key_path = "./public_key_path" public_key_pack_new = heaan.PublicKeyPack(context_new) public_key_pack_new.load(public_key_path) # pi-heaan은 블록 단위로 수행되며 최대 2^16 슬롯까지 구성할 수 있다. message = heaan.Message([0.5, 2.3, 4.9, 10.2, 9.0, 5.2, 3.4, 8.6]) # 슬롯이 8개로 구성된 예시 메세지 리스트로 만들어야 작동이 된다. print(message) print(len(message)) # 암호화하기 위한 파라미터들 불러오기 encryptor = heaan.Encryptor(context) ciphertext = heaan.Ciphertext( ) # 암호 객체를 생성 및 저장하고 불러오기 / 메시지를 암호화하여 변수로 사용하기 위해서는 암호화 이전에 암호문으로 사용할 변수의 자료형을 반드시 heaan.Ciphertext로 설정해야 한다. enc_key = public_key.get_enc_key() encryptor.encrypt(message, enc_key, ciphertext) # ctxt의 슬롯의 길이는 확인할 수 있다. print(ciphertext.get_number_of_slots()) print(len(message)) # ctxt 저장 ciphertext_path = "./ciphertext.bin" ciphertext.save(ciphertext_path) # ctxt 로드 ciphertext_path = "./ciphertext.bin" ciphertext_new = heaan.Ciphertext() ciphertext_new.load(ciphertext_path)