Пример #1
0
    def _sign(self, data):
        '''
            Judge the sign of numbers in data

            argv:
                @data:
                    int / np.ndarray
            
            return:
                int -> int 
                np.ndarray -> np.ndarray

                1  ==> >0
                -1 ==> <0
                0  ==> ==0
        '''

        if type(data) is paillier.EncryptedNumber:
            t = 'sign'
        elif type(data) is np.ndarray:
            t = 'sign_batch'
        else:
            return None
        # log('_sign data size: %s'%str(data.__sizeof__()))
        req = {'type': t, 'content': data}
        # log('_sign req size: %s'%str(req.__sizeof__()))
        send(self.server, req)
        resp = receive(self.server)
        return resp['content']
Пример #2
0
    def initial_0(self, data):
        # 分发密钥
        t1_start = time.perf_counter()
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ip = config._S2_IP
        port = config._S2_PORT

        server.connect((ip, port))
        self.server = server
        send(server, self.prikey)
        print("分发密钥的时间:")
        print(time.perf_counter() - t1_start)
        self.init_center(data)
        return
Пример #3
0
def init_encryption(dec_server_ip, dec_server_port):
    '''
        Initialization of encryption:
            1. connect to decryption server
            2. deliver the private key
    '''
    global private_key
    start_ts = time.perf_counter()
    public_key, private_key = paillier.generate_paillier_keypair()
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.connect((dec_server_ip, dec_server_port))
    send(server, {'type': 'key', 'content': private_key})
    resp = receive(server)
    print('Time of delivering private key: %d, response: %s' %
          (time.perf_counter() - start_ts, resp['content']))
    return server, public_key
Пример #4
0
    def find_min_dis_batch(self, d):
        d = np.array(d)
        min_col = d[:, 0]

        for cidx in range(1, K):
            start = time.perf_counter()
            current_col = d[:, cidx]
            diff_col = current_col - min_col
            socket_start = time.perf_counter()
            print('find_min_dist,l=%d,send/receive size:' % cidx)
            send(self.server, diff_col)
            diff_sign = np.array(receive(self.server))
            print('find_min_dist,l=%d,socket_time:%f' %
                  (cidx, time.perf_counter() - socket_start))
            min_col[diff_sign <= 0] = current_col[diff_sign <= 0]
            print('find_min_dist,l=%d,time:%f' %
                  (cidx, time.perf_counter() - start))

        return min_col
Пример #5
0
    def cal_dis(self, data_enc_point):
        server = self.server
        pool = Pool(cpu_number)
        hx_Bi_eps_Bi_Bi = []
        cal_dis1_start = time.perf_counter()
        hx_Bi_eps_Bi_Bi.append(pool.map(self.cal_dis1, data_enc_point))
        pool.close()
        pool.join()
        hx_Bi = []
        eps_Bi = []
        Bi = []
        for i in hx_Bi_eps_Bi_Bi[0]:
            hx_Bi.append(i[0])
            eps_Bi.append(i[1])
            Bi.append(i[2])
        print("cal_dis,ciphertext_calculation_time:%f" %
              (time.perf_counter() - cal_dis1_start))

        # 将混淆的BI传输到S2
        tx_start = time.perf_counter()
        print('cal_dis,send/receive size:')
        send(server, hx_Bi)
        hx_Bi_sig = receive(server)
        print('cal_dis,socket_time:%f' % (time.perf_counter() - tx_start))

        cal_dis2_start = time.perf_counter()
        for i in range(len(hx_Bi_eps_Bi_Bi[0])):
            hx_Bi_eps_Bi_Bi[0][i] = list(hx_Bi_eps_Bi_Bi[0][i])

        for i in range(len(hx_Bi_eps_Bi_Bi[0])):
            hx_Bi_eps_Bi_Bi[0][i][0] = hx_Bi_sig[0][i]
        d_batch = []
        pool = Pool(cpu_number)
        d_batch.append(pool.map(self.cal_dis2, hx_Bi_eps_Bi_Bi[0]))
        pool.close()
        # pool.terminate()
        pool.join()
        print("cal_dis,ciphertext_sign_time:%f" %
              (time.perf_counter() - cal_dis2_start))
        return d_batch[0]
Пример #6
0
    def compare(self, e1, e2):
        server = self.server
        ec = e1 - e2
        r = [-1, 1]
        i = random.randint(0, 1)
        eps = r[i]
        ec1 = eps * ec
        send(server, ec1)
        sig = receive(server)
        if sig == -1:
            res = e1
        elif sig == 0:
            if eps == 1:
                res = e1
            else:
                res = e2
        elif sig == 1:
            if eps == 1:
                res = e2
            else:
                res = e1

        return res
Пример #7
0
 def run(self):
     '''
         Run the decryption server, there are three kinds of requests:
             1. {'type':'key', 'content': PaillierPrivateKey, 'text': str}
             2. {'type':'sign', 'content': float, 'text': str}
             3. {'type':'sign_batch', 'content': np.ndarray, 'text':str}
     '''
     connection, client_address = self.sock.accept()
     while True:
         request = receive(connection)
         req_type = request['type']
         print("[%s] %s" % (time.strftime(
             "%Y-%m-%d %H:%M:%S", time.localtime(int(
                 time.time()))), request['type']))
         if 'key' == req_type:
             response = self._assign_key(request)
         elif 'sign' == req_type:
             response = self._sign(request)
         elif 'sign_batch' == req_type:
             response = self._sign_batch(request)
         print("[%s] %s" % (time.strftime(
             "%Y-%m-%d %H:%M:%S", time.localtime(int(
                 time.time()))), 'process done'))
         send(connection, response)
Пример #8
0
                for ite in range(max_step + 1):
                    print('step=%d' % ite)
                    step = 0
                    tatol_size = 0
                    while (step < math.ceil(number / batch_size)):

                        print('batch index=%d' % step)
                        hx_Bi = receive(connection)

                        hx_Bi_sig = []
                        hx_Bi_start = time.perf_counter()
                        hx_Bi_sig.append(pool.map(ks2.hx_Bi_one, hx_Bi))
                        print("cal_dist_s2_time:%f" %
                              (time.perf_counter() - hx_Bi_start))

                        send(connection, hx_Bi_sig)
                        size = len(hx_Bi)
                        tatol_size += size
                        if size % batch_size != 0:
                            step -= 1

                        for l in range(1, k):
                            hx_dis = receive(connection)
                            start = time.perf_counter()
                            hx_dis_np = np.array(hx_dis).flatten()
                            hx_dis_np_sig = pool.map(ks2.dec, hx_dis_np)
                            hx_dis_np_sig_np = np.array(hx_dis_np_sig).reshape(
                                np.array(hx_dis).shape)
                            print('find_min_dist,l=%d,time:%f' %
                                  (l, time.perf_counter() - start))
                            send(connection, hx_dis_np_sig_np)