Example #1
0
 def h_func(self, x, xsquare):
     """Computes the h-function as defined in Paillier's paper page.
     """
     return gmpy_math.invert(
         self.l_func(gmpy_math.powmod(self.public_key.g, x - 1, xsquare),
                     x), x)
Example #2
0
 def gen_obfuscator(self, random_value=None):
     r = random_value or random.SystemRandom().randrange(1, self.n)
     obfuscator = gmpy_math.powmod(r, self.n, self.nsquare)
     return obfuscator
Example #3
0
 def cal_host_ids_process_pair(self, data_instances):
     return data_instances.map(
         lambda k, v: (
             RsaIntersectionHost.hash(gmpy_math.powmod(int(RsaIntersectionHost.hash(k), 16), self.d, self.n)), k)
     )
Example #4
0
    def run(self, data_instances):
        LOGGER.info("Start rsa intersection")
        public_key = get(name=self.transfer_variable.rsa_pubkey.name,
                         tag=self.transfer_variable.generate_transferid(
                             self.transfer_variable.rsa_pubkey),
                         idx=0)

        LOGGER.info("Get RSA public_key:{} from Host".format(public_key))
        self.e = public_key["e"]
        self.n = public_key["n"]

        # generate random value and sent intersect guest ids to guest
        # table(sid, r)
        table_random_value = data_instances.mapValues(
            lambda v: random.SystemRandom().getrandbits(self.random_bit))

        # table(sid, hash(sid))
        table_hash_sid = data_instances.map(
            lambda k, v: (k, int(RsaIntersectionGuest.hash(k), 16)))
        # table(sid. r^e % n *hash(sid))
        table_guest_id = table_random_value.join(
            table_hash_sid,
            lambda r, h: h * gmpy_math.powmod(r, self.e, self.n))
        # table(r^e % n *hash(sid), 1)
        table_send_guest_id = table_guest_id.map(lambda k, v: (v, 1))
        remote(table_send_guest_id,
               name=self.transfer_variable.intersect_guest_ids.name,
               tag=self.transfer_variable.generate_transferid(
                   self.transfer_variable.intersect_guest_ids),
               role=consts.HOST,
               idx=0)
        LOGGER.info("Remote guest_id to Host")

        # table(r^e % n *hash(sid), sid)
        table_exchange_guest_id = table_guest_id.map(lambda k, v: (v, k))

        # Recv host_ids_process
        # table(host_id_process, 1)
        table_host_ids_process = get(
            name=self.transfer_variable.intersect_host_ids_process.name,
            tag=self.transfer_variable.generate_transferid(
                self.transfer_variable.intersect_host_ids_process),
            idx=0)
        LOGGER.info("Get host_ids_process from Host")

        # Recv process guest ids
        # table(r^e % n *hash(sid), guest_id_process)
        table_recv_guest_ids_process = get(
            name=self.transfer_variable.intersect_guest_ids_process.name,
            tag=self.transfer_variable.generate_transferid(
                self.transfer_variable.intersect_guest_ids_process),
            # role=consts.HOST,
            idx=0)
        LOGGER.info("Get guest_ids_process from Host")

        # table(r^e % n *hash(sid), sid, guest_ids_process)
        table_join_guest_ids_process = table_exchange_guest_id.join(
            table_recv_guest_ids_process, lambda sid, g: (sid, g))
        # table(sid, guest_ids_process)
        table_sid_guest_ids_process = table_join_guest_ids_process.map(
            lambda k, v: (v[0], v[1]))

        # table(sid, hash(guest_ids_process/r)))
        table_sid_guest_ids_process_final = table_sid_guest_ids_process.join(
            table_random_value, lambda g, r: hashlib.sha256(
                bytes(str(gmpy2.divm(int(g), int(r), self.n)),
                      encoding="utf-8")).hexdigest())

        # table(hash(guest_ids_process/r), sid)
        table_guest_ids_process_final_sid = table_sid_guest_ids_process_final.map(
            lambda k, v: (v, k))

        # intersect table(hash(guest_ids_process/r), sid)
        table_encrypt_intersect_ids = table_guest_ids_process_final_sid.join(
            table_host_ids_process, lambda sid, h: sid)

        # intersect table(hash(guest_ids_process/r), 1)
        table_send_intersect_ids = table_encrypt_intersect_ids.mapValues(
            lambda v: 1)
        LOGGER.info("Finish intersect_ids computing")

        # send intersect id
        if self.send_intersect_id_flag:
            remote(table_send_intersect_ids,
                   name=self.transfer_variable.intersect_ids.name,
                   tag=self.transfer_variable.generate_transferid(
                       self.transfer_variable.intersect_ids),
                   role=consts.HOST,
                   idx=0)
            LOGGER.info("Remote intersect ids to Host!")
        else:
            LOGGER.info("Not send intersect ids to Host!")

        # intersect table(sid, "intersect_id")
        intersect_ids = table_encrypt_intersect_ids.map(lambda k, v:
                                                        (v, "intersect_id"))
        return intersect_ids
Example #5
0
    def run(self, data_instances):
        LOGGER.info("Start rsa intersection")
        public_keys = self.transfer_variable.rsa_pubkey.get(-1)
        LOGGER.info("Get RSA public_key:{} from Host".format(public_keys))
        self.e = [int(public_key["e"]) for public_key in public_keys]
        self.n = [int(public_key["n"]) for public_key in public_keys]

        cache_version_match_info = self.get_cache_version_match_info()

        # generate random value and sent intersect guest ids to guest
        # table(sid, r)
        random_value = data_instances.mapValues(
            lambda v: random.SystemRandom().getrandbits(self.random_bit))

        # table(sid, hash(sid))
        hash_sid = data_instances.map(
            lambda k, v: (k, int(RsaIntersectionGuest.hash(k), 16)))
        # table(sid. r^e % n *hash(sid)) for each host
        guest_id_list = []
        for i in range(len(self.e)):
            guest_id_list.append(
                random_value.join(
                    hash_sid, lambda r, h: h * gmpy_math.powmod(
                        r, self.e[i], self.n[i])))

        # table(r^e % n *hash(sid), 1)
        for i, guest_id in enumerate(guest_id_list):
            mask_guest_id = guest_id.map(lambda k, v: (v, 1))

            self.transfer_variable.intersect_guest_ids.remote(mask_guest_id,
                                                              role=consts.HOST,
                                                              idx=i)
            LOGGER.info("Remote guest_id to Host {}".format(i))

        # table(r^e % n *hash(sid), sid)
        exchange_guest_id_kv = [
            guest_id.map(lambda k, v: (v, k)) for guest_id in guest_id_list
        ]

        host_ids_process_list = self.get_host_id_process(
            cache_version_match_info)
        LOGGER.info("Get host_ids_process")

        # Recv process guest ids
        # table(r^e % n *hash(sid), guest_id_process)
        recv_guest_ids_process = self.transfer_variable.intersect_guest_ids_process.get(
            idx=-1)
        LOGGER.info("Get guest_ids_process from Host")

        # table(r^e % n *hash(sid), sid, guest_ids_process)
        join_guest_ids_process = [
            v.join(recv_guest_ids_process[i], lambda sid, g: (sid, g))
            for i, v in enumerate(exchange_guest_id_kv)
        ]

        # table(sid, guest_ids_process)
        sid_guest_ids_process = [
            e.map(lambda k, v: (v[0], v[1])) for e in join_guest_ids_process
        ]

        # table(sid, hash(guest_ids_process/r)))
        sid_guest_ids_process_final = [
            v.join(
                random_value, lambda g, r: RsaIntersectionGuest.hash(
                    gmpy2.divm(int(g), int(r), self.n[i])))
            for i, v in enumerate(sid_guest_ids_process)
        ]

        # table(hash(guest_ids_process/r), sid)
        guest_ids_process_final_kv_exchange = [
            e.map(lambda k, v: (v, k)) for e in sid_guest_ids_process_final
        ]

        # intersect table(hash(guest_ids_process/r), sid)
        encrypt_intersect_ids = [
            v.join(host_ids_process_list[i], lambda sid, h: sid)
            for i, v in enumerate(guest_ids_process_final_kv_exchange)
        ]
        raw_intersect_ids = [
            e.map(lambda k, v: (v, 1)) for e in encrypt_intersect_ids
        ]
        intersect_ids = self.get_common_intersection(raw_intersect_ids)
        LOGGER.info("Finish intersect_ids computing")

        # send intersect id
        if self.sync_intersect_ids:
            for i, host_party_id in enumerate(self.host_party_id_list):
                remote_intersect_id = self.map_raw_id_to_encrypt_id(
                    intersect_ids, encrypt_intersect_ids[i])
                self.transfer_variable.intersect_ids.remote(
                    remote_intersect_id, role=consts.HOST, idx=i)
                LOGGER.info(
                    "Remote intersect ids to Host {}!".format(host_party_id))
        else:
            LOGGER.info("Not send intersect ids to Host!")

        if not self.only_output_key:
            intersect_ids = self._get_value_from_data(intersect_ids,
                                                      data_instances)

        return intersect_ids
Example #6
0
 def decrypt(self, value):
     if self.d is not None and self.n is not None:
         return gmpy_math.powmod(value, self.d, self.n)
     else:
         return None
Example #7
0
 def guest_id_process(sid, random_bit, rsa_e, rsa_n):
     r = random.SystemRandom().getrandbits(random_bit)
     re_hash = gmpy_math.powmod(r, rsa_e, rsa_n) * int(
         RsaIntersectionGuest.hash(sid), 16) % rsa_n
     return re_hash, (sid, r)
Example #8
0
    def run(self, data_instances):
        LOGGER.info("Start rsa intersection")

        encrypt_operator = RsaEncrypt()
        encrypt_operator.generate_key(rsa_bit=1028)
        self.e, self.d, self.n = encrypt_operator.get_key_pair()
        LOGGER.info("Generate rsa keys.")
        public_key = {"e": self.e, "n": self.n}
        remote(public_key,
               name=self.transfer_variable.rsa_pubkey.name,
               tag=self.transfer_variable.generate_transferid(
                   self.transfer_variable.rsa_pubkey),
               role=consts.GUEST,
               idx=0)
        LOGGER.info("Remote public key to Guest.")

        # (host_id_process, 1)
        host_ids_process_pair = data_instances.map(
            lambda k, v: (RsaIntersectionHost.hash(
                gmpy_math.powmod(int(RsaIntersectionHost.hash(k), 16), self.d,
                                 self.n)), k))

        host_ids_process = host_ids_process_pair.mapValues(lambda v: 1)
        remote(host_ids_process,
               name=self.transfer_variable.intersect_host_ids_process.name,
               tag=self.transfer_variable.generate_transferid(
                   self.transfer_variable.intersect_host_ids_process),
               role=consts.GUEST,
               idx=0)
        LOGGER.info("Remote host_ids_process to Guest.")

        # Recv guest ids
        guest_ids = get(name=self.transfer_variable.intersect_guest_ids.name,
                        tag=self.transfer_variable.generate_transferid(
                            self.transfer_variable.intersect_guest_ids),
                        idx=0)
        LOGGER.info("Get guest_ids from guest")

        # Process guest ids and return to guest
        guest_ids_process = guest_ids.map(
            lambda k, v: (k, gmpy_math.powmod(int(k), self.d, self.n)))
        remote(guest_ids_process,
               name=self.transfer_variable.intersect_guest_ids_process.name,
               tag=self.transfer_variable.generate_transferid(
                   self.transfer_variable.intersect_guest_ids_process),
               role=consts.GUEST,
               idx=0)
        LOGGER.info("Remote guest_ids_process to Guest.")

        # recv intersect ids
        intersect_ids = None
        if self.get_intersect_ids_flag:
            encrypt_intersect_ids = get(
                name=self.transfer_variable.intersect_ids.name,
                tag=self.transfer_variable.generate_transferid(
                    self.transfer_variable.intersect_ids),
                idx=0)

            intersect_ids_pair = encrypt_intersect_ids.join(
                host_ids_process_pair, lambda e, h: h)
            intersect_ids = intersect_ids_pair.map(lambda k, v:
                                                   (v, "intersect_id"))

            LOGGER.info("Get intersect ids from Guest")
        return intersect_ids
Example #9
0
 def pubkey_id_generate(k, pair):
     r = random.SystemRandom().getrandbits(random_bit)
     r_e = gmpy_math.powmod(r, rsa_e, rsa_n)
     for hash_sid, v in pair:
         processed_id = r_e * hash_sid % rsa_n
         yield processed_id, (v[0], r)
 def calculate_commitment(self, coefficient):
     return gmpy_math.powmod(self.g, coefficient, self.p)
Example #11
0
 def sign_id(hash_sid, rsa_d, rsa_n):
     return gmpy_math.powmod(hash_sid, rsa_d, rsa_n)