floating_point.secure_floating_point module¶
Module that contains logic for secret-shared floating point numbers.
- floating_point.secure_floating_point.SecFlp(significand_bit_length=None, exponent_bit_length=None, significand_prime=None, exponent_prime=None, max_concurrent_additions=2, max_concurrent_multiplications=2)[source]¶
Function that generates a class for secure floating point numbers with certain bit lengths for the significand and the exponent.
- Parameters:
significand_bit_length (
None|int) – Bit length of the significand.exponent_bit_length (
None|int) – Bit length of the exponent.significand_prime (
None|int) – Prime number used as the modulus for the finite field of the significand.exponent_prime (
None|int) – Prime number used as the modulus for the finite field of the exponent.max_concurrent_additions (
int) – max number of default secure floating point addends.max_concurrent_multiplications (
int) – max number of secure floating point factors.
- Return type:
type[SecureFloatingPoint]- Returns:
A SecureFloatingPoint type initialised using the parameters given.
- class floating_point.secure_floating_point.SecureFloatingPoint(value=None, *, two_to_exponent=None, is_zero=None)[source]¶
Bases:
SecureNumberBase class for secure (secret-shared) floating-point numbers.
- __abs__()[source]¶
Function that securely computes the absolute value.
- Return type:
- Returns:
A secure floating-point number that represents the absolute value of the object it is called on.
- __add__(other)[source]¶
Function that securely computes the addition of a secure floating-point number with an integer, float or other secure floating-point number.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number- Return type:
- Returns:
A secure floating-point number representing the addition.
- __init__(value=None, *, two_to_exponent=None, is_zero=None)[source]¶
Initialize a secure floating-point number.
- Parameters:
value (
None|int|float|tuple[int,int] |tuple[PrimeFieldElement,PrimeFieldElement] |tuple[SecureInteger,SecureInteger]) – If value is None, this object is a dummy. The values for the significand and exponent and assumed to be set at a later time (e.g., by the input function or under the hood by the extended_coroutine decorator. Otherwise, it is assumed to be either an integer, a float or a 2-tuple of values representing the significand and exponent. If the given values in the tuple are already finite field elements or secure integers, they will be used as is. Otherwise they are assumed to be regular integers and their representation will be corrected before they are converted to finite field elements.two_to_exponent (
None|PrimeFieldElement|SecureInteger) – Exponentiation base two of the exponent of the provided floating point. This will be computed automatically if the provided argument to value is a (tuple of) number(s). If not provided, the value will be computed on-the-fly only when it is required by certain computations.is_zero (
None|PrimeFieldElement|SecureInteger) – Flag that signals whether the provided floating point has value zero. This will be set automatically if the provided argument to value is a (tuple of) number(s). If not provided, the value will be computed on-the-fly only when it is required by certain computations.
- Raises:
TypeError – Provided argument for value is not of any of the expected types.
ValueError – The required exponent to represent the provided float argument to value is too large.
- __mul__(other)[source]¶
function that securely computes the multiplication of a secure floating-point with an integer, float or other secure floating-point number.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number.- Return type:
- Returns:
A secure floating-point number representing the multiplication.
- __neg__()[source]¶
Function that securely computes the negative value.
- Return type:
- Returns:
A secure floating-point number that represents the additive inverse of the object it is called on.
- __radd__(other)¶
Function that securely computes the addition of a secure floating-point number with an integer, float or other secure floating-point number.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number- Return type:
- Returns:
A secure floating-point number representing the addition.
- __rand__(other)¶
Bitwise and.
- Return type:
Self
- __rdivmod__(other)[source]¶
Integer division (with reflected arguments).
- Return type:
tuple[Self,Self]
- __rmul__(other)¶
function that securely computes the multiplication of a secure floating-point with an integer, float or other secure floating-point number.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number.- Return type:
- Returns:
A secure floating-point number representing the multiplication.
- __ror__(other)¶
Bitwise or.
- Return type:
Self
- __rpow__(other)[source]¶
Exponentation (with reflected arguments) for secret exponent.
- Return type:
Self
- __rsub__(other)[source]¶
Function that securely computes the reflected subtraction of other from runtime.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number to be subtracted.- Return type:
- Returns:
A secure floating-point number representing the subtraction.
- __rtruediv__(other)[source]¶
Function that securely computes the division of a secure floating-point number with an integer, float or other secure floating-point number. Reflected arguments.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number- Raises:
ZeroDivionError – Attempted division by zero.
- Return type:
- Returns:
A secure floating-point number representing the division.
- __sub__(other)[source]¶
Function that securely computes the subtraction of other from runtime.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number to be subtracted.- Return type:
- Returns:
A secure floating-point number representing the subtraction.
- __truediv__(other)[source]¶
Function that securely computes the division of a secure floating-point number with an integer, float or other secure floating-point number.
It is the responsibility of the user to ensure that no ZeroDivisionError can occur. To prevent such errors, the user can explicitly check that the divisor is not zero by performing a secure public zero test on the significand.
- Parameters:
other (
SecureFloatingPoint|int|float) – integer, float or secure floating-point number- Return type:
- Returns:
A secure floating-point number representing the division.
- add(*values)[source]¶
Function that securely computes multiple addition of a secure floating-point with other integers, floats, or secure floating-point numbers.
Sequentially adding floating-point numbers will yield different results depending on the order of addition. This function is roughly equivalent to adding the input values in order of increasing modulus, e.g. similar to sum(sorted(values, key=abs)).
For the computation of two_to_max_exp we distinguish three different cases which influence whether the resulting output has its two_to_exponent set in the protocol:
All input values have a cached two_to_exponent. In this case we can efficiently compute two_to_max_exp. We can compute inv_two_to_emax via a secure multiplicative inverse and are able to efficiently compute two_to_exponent for the resulting output.
None of the input values has a cached two_to_exponent. In this case we can not efficiently compute two_to_max_exp, as this would require to perform a secure exponentiation for each input value. Using a neat trick the need of two_to_max_exp can be circumvented when computing $2^{e_i - e_{max}}$ by evaluating for each input a specific polynomial. Using Horner’s method this can be done using just secure multiplications. In this case two_to_exponent is not set for the resulting output.
Some input values have a cached two_to_exponent and some input values do not. In this case we compute two_to_max_exp ` from `max_exp, requiring one secure exponentiation. We can obtain inv_two_to_emax via secure multiplicative inversion. Using the same polynomial as before, for inputs that do not have cached two_to_exponent, we can compute $2^{e_i - e_{max}}$ without computing $2^{e_i}$. From the earlier computed two_to_max_exp we can efficiently compute two_to_exponent for the resulting output.
- Parameters:
values (
SecureFloatingPoint|int|float) – secure floating-point addends.- Return type:
- Returns:
A secure floating-point number representing the addition.
- Raises:
ValueError – If the number of addends is not compatible.
- static correct_floating_point_representation(significand, exponent, significand_bit_length)[source]¶
Change the significand and exponent value such that the significand is in the range [2^(l-1), 2^l - 1), where l is the significand bit length.
- Parameters:
significand (
int) – Integer representing the significand value.exponent (
int) – Integer representing the exponent value.significand_bit_length (
int) – The significand bit length
- Return type:
tuple[int,int]- Returns:
A tuple of integers representing the corrected significand and exponent.
- property exponent: SecureInteger¶
The exponent of the secure floating point.
- Returns:
Secret-shared exponent.
- exponent_bit_length:
int¶
- exponent_class:
type[SecureInteger]¶
- static find_floating_point_representation(value, significand_bit_length)[source]¶
Function that represents a value as s * 2**e and returns s and e such that s in [2^(l-1), 2^l - 1), where l is the significand bit length. :type value:
float:param value: floating-point number to be converted. :type significand_bit_length:int:param significand_bit_length: bit length of the significand. :rtype:tuple[int,int] :return: A tuple representing the significand and exponent value.
- async has_cached_is_zero()[source]¶
Indicate whether the is_zero value is cached.
- Return type:
bool- Returns:
True if the is_zero value is cached.
- async has_cached_two_to_exponent()[source]¶
Indicate whether the two_to_exponent value is cached.
- Return type:
bool- Returns:
True if the two_to_exponent value is cached.
- is_zero()[source]¶
Get secure indication of equality with zero.
The result is cached for efficient reuse.
- Return type:
SecureInteger- Returns:
Secret-shared 1 if the floating point equals zero, secret-shared 0 otherwise.
- max_concurrent_additions:
int¶
- max_concurrent_multiplications:
int¶
- mul(*values)[source]¶
Function that securely computes multiple multiplications of a secure floating-point with other integers, floats, or secure floating-point numbers.
- Parameters:
values (
SecureFloatingPoint|int|float) – secure floating-point factors, first element needs to be secflp type.- Return type:
- Returns:
A secure floating-point number representing the multiplication.
- Raises:
ValueError – If the number of factors is not compatible.
- classmethod output_conversion(sign, exp)[source]¶
Function that is called on the reconstructed finite field elements representing the significand and exponent to create interpretable output.
- Parameters:
sign (
PrimeFieldElement|int) – Significand finite field element.exp (
PrimeFieldElement|int) – Exponent finite field element.
- Return type:
float- Returns:
A regular float representing the same value.
- Raises:
ValueError – Provided exponent is too large to convert the inputs into a python float.
- static prod(*values)[source]¶
Function that securely computes multiple multiplications of a secure floating-point with other integers, floats, or secure floating-point numbers.
- Parameters:
values (
SecureFloatingPoint|int|float) – secure floating-point, int, float factors. One of them must be a secure floating-point.- Return type:
- Returns:
A secure floating-point number representing the multiplication.
- Raises:
TypeError – Provided list does not contain a SecureFloatingPoint object.
ValueError – If the number of factors is not compatible.
- sign(other=None)[source]¶
Function to securely calculate the sign. If no other value is provided, the input value is this object’s significand, otherwise the other value is used.
- Parameters:
other (
SecureInteger|None)- Return type:
SecureInteger- Returns:
- property significand: SecureInteger¶
The significand of the secure floating point.
- Returns:
Secret-shared significand.
- significand_bit_length:
int¶
- significand_class:
type[SecureInteger]¶
- static sum(*values)[source]¶
Function that securely computes multiple addition of a secure floating-point with other integers, floats, or secure floating-point numbers.
Sequentially adding floating-point numbers will yield different results depending on the order of addition. This function is roughly equivalent to adding the input values in order of increasing modulus, e.g. similar to sum(sorted(values, key=abs)).
- Parameters:
values (
SecureFloatingPoint|int|float) – secure floating-point addends, int or float addends. One of them must be a secure floating-point.- Return type:
- Returns:
A secure floating-point number representing the addition.
- Raises:
TypeError – Provided list does not contain a SecureFloatingPoint object.
ValueError – If the number addends is not compatible.
- two_to_exponent()[source]¶
Get base-2 exponentiation of the exponent of the secure floating point.
The result is cached for efficient reuse.
- Return type:
SecureInteger- Returns:
Secret-shared base-2 exponentiation.
- two_to_exponent_class:
type[SecureInteger]¶
- floating_point.secure_floating_point.secure_floating_point_helper(significand_bit_length, exponent_bit_length, significand_prime=None, exponent_prime=None, max_concurrent_additions=2, max_concurrent_multiplications=2)[source]¶
Helper function that generates a class for a secure floating point number with the given parameters.
- Parameters:
significand_bit_length (
int) – The bit length of the significand.exponent_bit_length (
int) – The bit length of the exponent.significand_prime (
None|int) – The prime number used as the modulus for the finite field of the significand.exponent_prime (
None|int) – The prime number used as the modulus for the finite field of the exponent.max_concurrent_additions (
int) – max number of secure floating point addends.max_concurrent_multiplications (
int) – max number of secure floating point factors.
- Return type:
type[SecureFloatingPoint]- Returns:
A SecureFloatingPoint type initialised using the parameters given.