psi_api.cc 8.3 KB
Newer Older
J
jingqinghe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "psi_api.h"

#include <algorithm>
#include <atomic>
#include <memory>
#include <mutex>

#include "net_io.h"
#include "psi.h"
#include "rand_utils.h"

namespace psi {

class PsiApi {
public:
  PsiApi() = default;

  ~PsiApi(){};

  static void set_psi_timeout(int timeout_s) { _s_timeout_s = timeout_s; }

  void psi_send(const std::set<std::string> &in,
                std::atomic<int> *psi_progress) {

    std::atomic<int> psi_prog(0);

    if (!psi_progress) {
      psi_progress = &psi_prog;
    }

    *psi_progress = 0;

    size_t local_size = in.size();
    size_t remote_size = 0;

    _io->send_data(&local_size, sizeof(size_t));

    _io->recv_data_with_timeout(&remote_size, sizeof(size_t));

    if (local_size == 0 || remote_size == 0) {
      *psi_progress = 100;
      return;
    }

    auto random_seed = block_from_dev_urandom();

    std::unique_ptr<PsiSender> sender;
    {
      std::lock_guard<std::mutex> guard(_s_init_mutex);
      sender = std::unique_ptr<PsiSender>(
          new PsiSender(local_size, remote_size, random_seed));
    }

    // 512 for ot size
    std::array<std::array<std::array<uint8_t, g_point_buffer_len>, 2>, 512>
        recv_input;

    _io->recv_data_with_timeout(&recv_input, sizeof(recv_input));

    std::array<std::array<uint8_t, g_point_buffer_len>, 512> send_back;
    for (size_t i = 0; i < 512; ++i) {
      send_back[i] = sender->np_ot().recv(i, recv_input[i]);
    }

    _io->send_data(&send_back, sizeof(send_back));

    *psi_progress = 2;

    sender->init_offline(in);

    *psi_progress = 18;

    sender->sync();

    *psi_progress = 30;

    size_t cuckoo_size = 0;

    _io->recv_data_with_timeout(&cuckoo_size, sizeof(size_t));

    double recv_times = cuckoo_size / (_s_recv_step_len / sizeof(Block512));

    double prog_ = 30;

    for (size_t offset = 0; offset < cuckoo_size;) {

      auto recv_len =
          std::min(_s_recv_step_len / sizeof(Block512), cuckoo_size - offset);

      std::vector<Block512> masks(recv_len);

      _io->recv_data_with_timeout(masks.data(), recv_len * sizeof(Block512));

      sender->recv_masks(offset, offset + recv_len, masks);

      prog_ += 45.0 / recv_times;

      *psi_progress = prog_;

      offset += recv_len;
    }

    *psi_progress = 75;

    for (size_t idx = 0; idx < 3; ++idx) {
      const auto &vec = sender->send_oprf_outputs(idx);

      const uint8_t *data = vec.data();

      size_t len = vec.size();

      _io->send_data(&len, sizeof(len));
      _io->send_data(data, len);

      *psi_progress += 7;
    }

    size_t stash_size = 0;

    _io->recv_data_with_timeout(&stash_size, sizeof(size_t));

    for (size_t i = 0; i < stash_size; ++i) {
      auto bin_idx = cuckoo_size + i;
      size_t hash_idx = 3 + i;

      std::vector<Block512> masks(1);
      _io->recv_data_with_timeout(masks.data(),
                                  masks.size() * sizeof(Block512));

      sender->recv_masks(bin_idx, bin_idx + 1, masks);

      const auto &vec = sender->send_oprf_outputs(hash_idx);

      size_t len = vec.size();
      _io->send_data(&len, sizeof(size_t));
      _io->send_data(vec.data(), vec.size());
    }

    *psi_progress = 100;
    return;
  }

  int psi_recv(const std::set<std::string> &in, std::vector<std::string> *out,
               std::atomic<int> *psi_progress) {

    std::atomic<int> psi_prog(0);

    if (out) {
      out->clear();
    }

    if (!psi_progress) {
      psi_progress = &psi_prog;
    }

    *psi_progress = 0;

    size_t local_size = in.size();
    size_t remote_size = 0;

    _io->recv_data_with_timeout(&remote_size, sizeof(size_t));

    _io->send_data(&local_size, sizeof(size_t));

    if (local_size == 0 || remote_size == 0) {
      *psi_progress = 100;
      return 0;
    }

    auto random_seed = block_from_dev_urandom();

    std::unique_ptr<PsiReceiver> recver;
    {
      std::lock_guard<std::mutex> guard(_s_init_mutex);
      recver = std::unique_ptr<PsiReceiver>(
          new PsiReceiver(remote_size, local_size, random_seed));
    }

    // ot size = 512
    std::array<std::array<std::array<uint8_t, g_point_buffer_len>, 2>, 512>
        to_send;

    for (size_t i = 0; i < 512; ++i) {
      to_send[i] = recver->np_ot().send_pre(i);
    }
    _io->send_data(&to_send, sizeof(to_send));
    std::array<std::array<uint8_t, g_point_buffer_len>, 512> recved;
    _io->recv_data_with_timeout(&recved, sizeof(recved));
    for (size_t i = 0; i < 512; ++i) {
      recver->np_ot().send_post(i, recved[i]);
    }

    *psi_progress = 2;

    recver->init_offline(in);

    *psi_progress = 18;

    recver->sync();

    *psi_progress = 30;

    size_t cuckoo_size = recver->cuckoo_bins_num();
    size_t stash_size = recver->stash_bins_num();

    _io->send_data(&cuckoo_size, sizeof(size_t));

    auto masks = recver->send_masks(0, cuckoo_size);

    *psi_progress = 75;

    _io->send_data(masks.data(), masks.size() * sizeof(Block512));

    const auto oprf_len = recver->oprf_output_len();

    double prog_ = 75;

    for (size_t idx = 0; idx < 3; ++idx) {
      size_t len = 0;
      _io->recv_data_with_timeout(&len, sizeof(len));
      std::vector<char> buf(oprf_len);

      double recv_times = len * 1.0 / _s_recv_step_len;
      for (size_t offset = 0; offset < len;) {
        std::vector<std::string> output_buf;
        size_t round_len = std::min(_s_recv_step_len, size_t(len - offset));
        size_t round_end = offset + round_len;
        for (; offset < round_end; offset += oprf_len) {
          _io->recv_data_with_timeout(buf.data(), oprf_len);
          output_buf.emplace_back(std::string(buf.data(), oprf_len));
        }
        recver->recv_oprf_outputs(idx, output_buf);

        prog_ += 7.0 / recv_times;
        *psi_progress = prog_;
      }
    }

    *psi_progress = 96;

    _io->send_data(&stash_size, sizeof(size_t));

    for (size_t i = 0; i < stash_size; ++i) {
      auto bin_idx = cuckoo_size + i;
      size_t hash_idx = 3 + i;
      auto masks = recver->send_masks(bin_idx, bin_idx + 1);

      _io->send_data(masks.data(), masks.size() * sizeof(Block512));

      size_t len = 0;
      _io->recv_data_with_timeout(&len, sizeof(len));
      std::vector<std::string> output_buf;
      for (size_t offset = 0; offset < len; offset += oprf_len) {
        std::vector<char> buf(oprf_len);
        _io->recv_data_with_timeout(buf.data(), oprf_len);
        output_buf.emplace_back(std::string(buf.data(), oprf_len));
      }
      recver->recv_oprf_outputs(hash_idx, output_buf);
    }
    *psi_progress = 99;

    if (out) {
      *out = recver->output();
    }

    *psi_progress = 100;

    return 0;
  }

  NetIO *_io;

  static int _s_timeout_s;

private:
  static std::mutex _s_init_mutex;

  static const size_t _s_recv_step_len;
};

std::mutex PsiApi::_s_init_mutex;
const size_t PsiApi::_s_recv_step_len = 0x1000000;

// default sync sock, no timeout
int PsiApi::_s_timeout_s = 0;

int psi_send(int port, const std::set<std::string> &in,
             std::atomic<int> *psi_progress) {
  try {
    PsiApi sender;

    NetIO io(nullptr, port, true, PsiApi::_s_timeout_s);

    sender._io = &io;

    sender.psi_send(in, psi_progress);

  } catch (const std::exception &e) {
    if (psi_progress) {
      *psi_progress = -1;
    }
J
jhjiangcs 已提交
316
    throw;
J
jingqinghe 已提交
317
  }
J
jhjiangcs 已提交
318
  return 0;
J
jingqinghe 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
}

int psi_recv(const std::string &remote_ip, int port,
             const std::set<std::string> &in, std::vector<std::string> *out,
             std::atomic<int> *psi_progress) {
  try {
    PsiApi recver;

    NetIO io(remote_ip.c_str(), port, true, PsiApi::_s_timeout_s);

    recver._io = &io;

    recver.psi_recv(in, out, psi_progress);

  } catch (const std::exception &e) {
    if (psi_progress) {
      *psi_progress = -1;
    }
J
jhjiangcs 已提交
337
    throw;
J
jingqinghe 已提交
338
  }
J
jhjiangcs 已提交
339
  return 0;
J
jingqinghe 已提交
340 341 342 343
}

void set_psi_timeout(int timeout_s) { PsiApi::set_psi_timeout(timeout_s); }
} // namespace psi