prng.cc 1.8 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
// 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 "prng.h"

#include <cstring>

namespace psi {

PseudorandomNumberGenerator::PseudorandomNumberGenerator(const block &seed)
    : _ctr(0), _now_byte(0) {
  set_seed(seed);
}

void PseudorandomNumberGenerator::set_seed(const block &b) {
  _aes.set_key(b);
  _ctr = 0;

  refill_buffer();
}

void PseudorandomNumberGenerator::refill_buffer() {
  for (auto &ctr : _ctr64) {
    ctr = _mm_cvtsi64_si128(_ctr++);
  }

  _aes.ecb_enc_blocks(_ctr64.data(), _ctr64.size(), _buffer.data());

  _now_byte = 0;
}

void PseudorandomNumberGenerator::get_array(void *res, size_t len) {
  std::array<uint8_t, _s_byte_capacity> &view =
      *reinterpret_cast<std::array<uint8_t, _s_byte_capacity> *>(&_buffer);

  for (size_t write = 0; len > 0;) {
    auto step = std::min(len, _s_byte_capacity - _now_byte);
    std::memcpy(reinterpret_cast<char *>(res) + write, view.data() + _now_byte,
                step);

    write += step;
    _now_byte += step;
    len -= step;
    if (_now_byte == _s_byte_capacity) {
      refill_buffer();
    }
  }
}

H
He,Kai 已提交
61 62 63 64 65 66
template <>
bool PseudorandomNumberGenerator::get<bool>() {
    uint8_t data;
    get_array(&data, sizeof(data));
    return data & 1;
}
J
jingqinghe 已提交
67
} // namespace smc