模数实例(SEAL/smallmodulus.h 2.3.0)


文件原位置SEAL/smallmodulus.h

介绍

加密参数plain_modulus和coeff_modulus中的素数由SmallModulus的实例表示。 整数模数的表示高达62位。 此类的目的是执行和存储Barrett约简(Barrett reduction)所需的预计算。

@par 线程安全
通常,只要没有其他线程同时改变它,从SmallModulus读取就是线程安全的。

@see EncryptionParameters用于描述加密参数。

overview

overview

private

成员

SmallModulus(std::uint64_t value, std::array<std::uint64_t, 3> const_ratio, int bit_count, int uint64_count) :
            value_(value), const_ratio_(const_ratio), bit_count_(bit_count), uint64_count_(uint64_count)
        {
        }
std::uint64_t value_ = 0;

// C++11 compatibility
std::array<std::uint64_t, 3> const_ratio_{ { 0 } };

int bit_count_ = 0;

int uint64_count_ = 0;

set_value

void SmallModulus::set_value(uint64_t value)
{
  if (value == 0)
  {
      // Zero settings
      bit_count_ = 0;
      uint64_count_ = 1;
      value_ = 0;

      // C++11 compatibility
      const_ratio_ = { { 0 } };
  }
  else if (value >> 62 != 0 || value == 0x4000000000000000 || value == 1)
  {
      throw invalid_argument("value can be at most 62 bits and cannot be 1");
  }
  else
  {
      // All normal, compute const_ratio and set everything
      value_ = value;
      bit_count_ = get_significant_bit_count(value_);

      // Compute Barrett ratios for 64-bit words (barrett_reduce_128)
      uint64_t numerator[3]{ 0, 0, 1 };
      uint64_t quotient[3]{ 0 };

      // Use a special method to avoid using memory pool
      divide_uint192_uint64_inplace(numerator, value_, quotient);

      const_ratio_[0] = quotient[0];
      const_ratio_[1] = quotient[1];

      // We store also the remainder
      const_ratio_[2] = numerator[0];

      uint64_count_ = 1;
  }
}

public

构造函数1

/**
创建一个SmallModulus实例。 SmallModulus的值设置为
给定值,或默认为零。

@param [in] value整数模数
@throws std :: invalid_argument如果value为1或大于62位
*/
SmallModulus(std::uint64_t value = 0)
{
    set_value(value);
}

构造函数2

/**
通过复制给定的一个SmallModulus来创建一个新的SmallModulus。

@param [in] copy要复制的SmallModulus
*/
SmallModulus(const SmallModulus &copy) = default;

构造函数3

/**
通过复制给定的一个SmallModulus来创建一个新的SmallModulus。

@param [in] source SmallModulus来自
*/
SmallModulus(SmallModulus &&source) = default;

运算符重载1(=)

/**
将给定的SmallModulus复制到当前的SmallModulus。

@param [in]assign SmallModulus从中复制
*/
SmallModulus &operator =(const SmallModulus &assign) = default;

运算符重载2(=)

/**
将给定的SmallModulus移动到当前的SmallModulus。

@param [in]assign SmallModulus移动
*/
SmallModulus &operator =(SmallModulus &&assign) = default;

运算符重载3(=)

/**
设置SmallModulus的值。

@param [in] value新的整数模数
@throws std :: invalid_argument如果value为1或大于62位
*/
inline SmallModulus &operator =(std::uint64_t value)
{
    set_value(value);
    return *this;
}

bit_count()

/**
返回当前SmallModulus值的有效位数。
*/
inline int bit_count() const
{
    return bit_count_;
}

uint64_count()

/**
返回当前SmallModulus值的大小(以64位字为单位)。
*/
inline int uint64_count() const
{
    return uint64_count_;
}

*pointer()

/**
返回指向当前SmallModulus值的常量指针。
*/
inline const uint64_t *pointer() const
{
    return &value_;
}

value()

/**
返回当前SmallModulus的值。
*/
inline std::uint64_t value() const
{
    return value_;
}

const_ratio()

/**
返回为当前SmallModulus的值计算的Barrett比率。 
Barrett比率的前两个分量是2 ^ 128 /值的底限,第三个分量是余数。
*/
inline const std::array<std::uint64_t, 3> &const_ratio() const
{
    return const_ratio_;
}

is_zero()

/**
返回当前SmallModulus的值是否为零。
*/
inline bool is_zero() const
{
    return value_ == 0;
}

运算符重载4(==)

/**
比较两个SmallModulus实例。

@param[in] compare The SmallModulus to compare against
*/
inline bool operator ==(const SmallModulus &compare) const
{
    return value_ == compare.value_;
}

运算符重载5(==)

/**
将当前SmallModulus的值与给定值进行比较。

@param[in] compare The value to compare against
*/
inline bool operator ==(std::uint64_t compare) const
{
    return value_ == compare;
}

运算符重载6(!=)

/**
比较两个SmallModulus实例。

@param[in] compare The SmallModulus to compare against
*/
inline bool operator !=(const SmallModulus &compare) const
{
    return !(value_ == compare.value_);
}

运算符重载7(!=)

/**
将当前SmallModulus的值与给定值进行比较。

@param[in] compare The value to compare against
*/
inline bool operator !=(std::uint64_t compare) const
{
    return !(value_ == compare);
}

save()

/**
将SmallModulus保存到输出流。 模数的完整状态是序列化的。
输出是二进制格式,不是人类可读的。 输出流必须设置“二进制”标志。

@param [in] stream 将SmallModulus保存到的流
*/
void SmallModulus::save(ostream &stream) const
{
    int bit_count32 = bit_count_;
    stream.write(reinterpret_cast<const char*>(&bit_count32), sizeof(int32_t));
    int uint64_count32 = uint64_count_;
    stream.write(reinterpret_cast<const char*>(&uint64_count32), sizeof(int32_t));
    stream.write(reinterpret_cast<const char*>(&value_), bytes_per_uint64);
    stream.write(reinterpret_cast<const char*>(const_ratio_.data()), 3 * bytes_per_uint64);
}

load()

/**
从输入流加载SmallModulus,覆盖当前的SmallModulus。

@param [in] stream从中加载SmallModulus的流
*/
void SmallModulus::load(istream &stream)
{
    int bit_count32;
    stream.read(reinterpret_cast<char*>(&bit_count32), sizeof(int32_t));
    bit_count_ = bit_count32;
    int uint64_count32;
    stream.read(reinterpret_cast<char*>(&uint64_count32), sizeof(int32_t));
    uint64_count_ = uint64_count32;
    stream.read(reinterpret_cast<char*>(&value_), bytes_per_uint64);
    stream.read(reinterpret_cast<char*>(const_ratio_.data()), 3 * bytes_per_uint64);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值