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
Esempio n. 2
0
    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)
Esempio n. 3
0
context = heaan.Context(params)

# Step 2. Generating Keys
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,
context = heaan.Context(params)

# Step 2. Generating Keys
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)


class RotateCopy:
    def __init__(self, num_slots):

        self.num_slots = num_slots

    def _pad_x(self, num_slots_per_vec, idx):
        # pad that the (idx_per_vec)-th slot in each vector is filled wtih 1, the other slots are 0

        pad = [0] * self.num_slots
params_new.load(param_path)
context_new = heaan.Context(params_new)

# 저장된 비밀키 로드
secret_key_path = "./secretkey.bin"
secret_key_new = heaan.SecretKey()
secret_key_new.load(secret_key_path)

# 저장된 공개키 로드/
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 저장
Esempio n. 6
0
context = heaan.Context(params)

# Step 2. Generating Keys
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_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
Esempio n. 7
0
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)
decryptor = heaan.Decryptor(context)

# Step 3. Generating Messages
list_a = [i for i in range(100)]
list_b = [i for i in range(0, 200, 2)]

msg_a = heaan.Message(list_a)
msg_b = heaan.Message(list_b)

# Step 4. Encrypt Message to Ciphertext
ciphertext_a = heaan.Ciphertext()
ciphertext_b = heaan.Ciphertext()

encryptor.encrypt(msg_a, enc_key, ciphertext_a)
encryptor.encrypt(msg_b, enc_key, ciphertext_b)


def ctxt_basic(ciphertext_a, ciphertext_b):

    ciphertext_add = heaan.Ciphertext()
    ciphertext_sub = heaan.Ciphertext()
    ciphertext_mult = heaan.Ciphertext()