【DiscreteGaussianGenerator】

本文详细介绍了Palisade库中用于离散高斯分布生成的BitGenerator和预计算概率矩阵的实现。在随机比特生成过程中,通过Knuth-Yao方法计算概率矩阵,并用DDG树进行高效采样。代码解析展示了如何根据标准差和均值生成概率矩阵,并利用这些信息构建DDG树,以进行快速的离散高斯整数生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pwd

palisade官方实例位置:/home/caorenlong/下载/palisade/src/core/examples
编译后exe位置:/home/caorenlong/下载/palisade/build/bin/examples/core

代码解析

随机比特生成(BitGenerator)

预计算概率矩阵

函数位于:discretegaussiangeneratorgeneric.cpp
参数1:标准差:34
参数2:均值:[0,1]范围中的数

/**
 *Generates the probability matrix of given distribution, which is used in
 *Knuth-Yao method
 */
 
void BaseSampler::GenerateProbMatrix(double stddev, double mean) {
  /*if (DDGColumn != nullptr) {
          delete[] DDGColumn;
  }*/
  std::vector<uint64_t> probMatrix;
  
  b_matrixSize = 2 * fin + 1;
  hammingWeights.resize(64, 0);
  //将概率数组resize为两倍fin+1
  //mr=20
  //fin = (int)ceil(m_std * mr);
	//即:fin为20倍的标准差
  probMatrix.resize(b_matrixSize);
  //概率矩阵临时数组
  double* probs = new double[b_matrixSize];
  double S = 0.0;
  b_std = stddev;
  double error = 1.0;
  //求解定义域中的所有值的概率
  //并将所有概率求和:S
  for (int i = -1 * fin; i <= fin; i++) {
    double prob = pow(M_E, -pow((i - mean), 2) / (2. * stddev * stddev));
    S += prob;
    probs[i + fin] = prob;
  }
  probMatrix[b_matrixSize - 1] = error * pow(2, 64);
  for (int i = 0; i < b_matrixSize; i++) {
    error -= probs[i] * (1.0 / S);
    //我们将小数左移64位变成整数
    probMatrix[i] = probs[i] * (1.0 / S) * /*(1<<64)*/ pow(2, 64);
    for (int j = 0; j < 64; j++) {
      hammingWeights[j] += ((probMatrix[i] >> (63 - j)) & 1);
    }
  }
  delete[] probs;
  GenerateDDGTree(probMatrix);
}
 
  DiscreteGaussianGeneratorGeneric dgg3(ky_samplers, stdBase, base,
                                        SMOOTHING_PARAMETER);
  start = currentDateTime();
  for (int k = 0; k < CENTER_COUNT; k++) {
    double center = k / static_cast<double>(CENTER_COUNT);
    for (size_t i = 0; i < count; i++) {
      dgg3.GenerateInteger(center, std);
      // dgg3.GenerateIntegerKnuthYaoAlt(0);
    }
  }
  finish = currentDateTime();
  std::cout << "Sampling " << std::to_string(count)
            << " integers (Generic - Knuth Yao): "
            << (finish - start) / CENTER_COUNT << " ms\n";

生成DDG树

void BaseSampler::GenerateDDGTree(const std::vector<uint64_t>& probMatrix) {
  for (unsigned int i = 0; i < probMatrix.size(); i++) {
  }
  firstNonZero = -1;
  for (int i = 0; i < 64 && firstNonZero == -1; i++)
    if (hammingWeights[i] != 0) firstNonZero = i;
  endIndex = firstNonZero;
  int32_t iNodeCount = 1;
  for (int i = 0; i < firstNonZero; i++) {
    iNodeCount *= 2;
  }
  bool end = false;
  unsigned int maxNodeCount = iNodeCount;
  for (int i = firstNonZero; i < MAX_TREE_DEPTH && !end; i++) {
    iNodeCount *= 2;
    endIndex++;
    if ((uint32_t)iNodeCount >= maxNodeCount) maxNodeCount = iNodeCount;
    iNodeCount -= hammingWeights[i];
    if (iNodeCount <= 0) {
      end = true;
      if (iNodeCount < 0) {
        endIndex--;
      }
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值