def encrypt(self, value, precision=None, r_value=None): """Encode and Paillier encrypt a real number *value*. Args: value: an int or float to be encrypted. If int, it must satisfy abs(*value*) < :attr:`n`/3. If float, it must satisfy abs(*value* / *precision*) << :attr:`n`/3 (i.e. if a float is near the limit then detectable overflow may still occur) precision (float): Passed to :meth:`EncodedNumber.encode`. If *value* is a float then *precision* is the maximum **absolute** error allowed when encoding *value*. Defaults to encoding *value* exactly. r_value (int): obfuscator for the ciphertext; by default (i.e. if *r_value* is None), a random value is used. Returns: EncryptedNumber: An encryption of *value*. Raises: ValueError: if *value* is out of range or *precision* is so high that *value* is rounded to zero. """ if isinstance(value, EncodedNumber): encoding = value else: encoding = EncodedNumber.encode(self, value, precision) return self.encrypt_encoded(encoding, r_value)
def encrypt(self, value, precision=None, r_value=None): """Encode and Paillier encrypt a real number *value*. #加密实数 Args: value: an int or float to be encrypted.一个整型或浮点型实数 If int, it must satisfy abs(*value*) < :attr:`n`/3.整型绝对值小于n/3 If float, it must satisfy abs(*value* / *precision*) << :attr:`n`/3 浮点型满足(value/精度)绝对值小于n/3 (i.e. if a float is near the limit then detectable overflow may still occur) precision (float): Passed to :meth:`EncodedNumber.encode`. If *value* is a float then *precision* is the maximum **absolute** error allowed when encoding *value*. Defaults to encoding *value* exactly. 如果值是浮点数,那么精度就是编码值时允许的最大绝对误差。默认为编码值。 r_value (int): obfuscator for the ciphertext; by default (i.e. if *r_value* is None), a random value is used. 密文模糊处理;默认情况下(即,如果*r_value*为None),则使用随机值。 Returns: EncryptedNumber: An encryption of *value*. Raises: ValueError: if *value* is out of range or *precision* is so high that *value* is rounded to zero. 异常:如果值超出范围或精度高,则值为零。 """ if isinstance(value, EncodedNumber): encoding = value #整型处理 else: encoding = EncodedNumber.encode(self, value, precision) #浮点型处理 return self.encrypt_encoded(encoding, r_value)
def encrypt(self, value, precision=None, r_value=None): if isinstance(value, EncodedNumber): encoding = value else: encoding = EncodedNumber.encode(self, value, precision) return self.encrypt_encoded(encoding, r_value)
def __mul__(self, other): """Multiply by an int, float, or EncodedNumber.""" if isinstance(other, EncryptedNumber): raise NotImplementedError('Good luck with that...') if isinstance(other, EncodedNumber): encoding = other else: encoding = EncodedNumber.encode(self.public_key, other) product = self._raw_mul(encoding.encoding) exponent = self.exponent + encoding.exponent return EncryptedNumber(self.public_key, product, exponent)
def _add_scalar(self, scalar): """Returns E(a + b), given self=E(a) and b. Args: scalar: an int or float b, to be added to `self`. Returns: EncryptedNumber: E(a + b), calculated by encrypting b and taking the product of E(a) and E(b) modulo :attr:`~PaillierPublicKey.n` ** 2. Raises: ValueError: if scalar is out of range or precision. """ encoded = EncodedNumber.encode(self.public_key, scalar, max_exponent=self.exponent) return self._add_encoded(encoded)
def mul_enc(self, other): """Multiply by an int, float, or EncodedNumber.""" if isinstance(other, EncryptedNumber): raise NotImplementedError('Good luck with that...') if isinstance(other, EncodedNumber): encoding = other else: encoding = EncodedNumber.encode(self.public_key, other) if (self.public_key.ri != 1) & (self.public_key.enc_ri != 0) & (self.public_key.enc_ri_neg != 0): enc_msg = self.__add__(self.public_key.enc_ri_neg) product = enc_msg._raw_mul(encoding.encoding) exponent = enc_msg.exponent + encoding.exponent sum_enc = EncryptedNumber(enc_msg.public_key, product, exponent).__add__(self.public_key.enc_ri) sum_enc.obfuscate() return sum_enc product = self._raw_mul(encoding.encoding) exponent = self.exponent + encoding.exponent return EncryptedNumber(self.public_key, product, exponent)
def _add_scalar(self, scalar): encoded = EncodedNumber.encode(self.public_key, scalar, max_exponent=self.exponent) return self._add_encoded(encoded)