secure_learning.utils.random module
The generate method generates a set of unique indicator vectors. It internally chooses between a direct and an indirect method, where the direct method is efficient for small sets and the indirect method is efficient for large sets. A demo of the different methods is presented by running the file as a script.
Usage (Python 3.6):
>>> from mpyc.runtime import mpc
>>> import random_unit_vectors
>>>
>>> masked_vectors = random_unit_vectors.generate(5,2)
>>> unmasked_vectors = list(map(lambda x: mpc.run(mpc.output(x)), masked_vectors))
>>> print(unmasked_vectors)
[[0, 1, 0, 0, 0], [0, 0, 0, 1, 0]]
- secure_learning.utils.random.decompose_random_binary_permutation(s, b)[source]
Extract a set of indicator vectors from a vector with ones and zeroes. Every indicator vector denotes the position of a one in the input vector.
Assume that the input vector contains exactly 1 one. We extract an indicator vector from this vector ‘s’ as follows:
>>> eps = 0 >>> delta = [0]*n >>> for i in range(n): >>> delta[i] = (1-eps)*s[i] >>> .delta = eps + delta[i]
The first line in the for-loop copies the i-th element of s as long as eps equals zero. This remains true until the loop reaches the index i* such that s[i] = 1. Now, delta[i*] is set to one and subsequently eps is set to one. As a consequence, all elements of delta with index at least i* are set to zero. We have thus obtained a indicator vector delta that has 1-bit on the index that corresponds to the first 1-bit of s.
The above approach can be generalized as to obtain b indicator vectors from a permutation of b 1-bits. Note that all these indicator vectors are unique and, by construction, the indicator vectors are sorted according to the index of their 1-bit.
- Parameters:
s (
List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]) – Vector with zeros and onesb (
int
) – Number of ones in s
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Unique set of random indicator vectors
- secure_learning.utils.random.generate_unique_unit_vectors(sectype, n, b=1)[source]
Generate a set of unique indicator vectors.
It internally chooses between a direct and an indirect method, where the direct method is efficient for small sets and the indirect method is efficient for large sets.
- Parameters:
sectype (
Type
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]) – Secure type of unit vector elementsn (
int
) – Length of unit vectorb (
int
) – Number of unit vectors
- Raises:
ValueError – Occurs when 0 <= b <= n when generating indicator vectors
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Unique set of random indicator vectors
- secure_learning.utils.random.generate_unique_unit_vectors_direct(sectype, n, b=1)[source]
Direct approach for generating random indicator vectors. In this approach, we generate sets of random indicator vectors until all vectors are unique.
Remark: if this method is used often and b is fixed, then the number of retries leaks statistical information about the total number of samples. Use the indirect method if this is an issue.
- Parameters:
sectype (
Type
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]) – Secure type of unit vector elementsn (
int
) – Length of unit vectorb (
int
) – Number of unit vectors
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Set of random unit vectors
- secure_learning.utils.random.generate_unique_unit_vectors_indirect(sectype, n, b=1)[source]
Generate a random binary permutation and then extract unique random indicator vectors from the given permutation.
- Parameters:
sectype (
Type
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]) – Secure type of unit vector elementsn (
int
) – Length of unit vectorb (
int
) – Number of unit vectors
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Unique set of random indicator vectors
- secure_learning.utils.random.permute_matrix(matrix)[source]
Permute matrix randomly.
- Parameters:
matrix (
Sequence
[Sequence
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]) – Matrix to be permuted- Raises:
TypeError – Input is not a matrix
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Permutated matrix
- secure_learning.utils.random.random_matrix_permutation(sectype, row_length, rows=None)[source]
Return a random permutation matrix.
- Parameters:
sectype (
Type
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]) – Secure type of unit vector elementsrow_length (
int
) – (Row) length of desired matrixrows (
Optional
[int
]) – Number of rows to return
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Random permutation matrix
- async secure_learning.utils.random.random_unit_vectors_are_unique(p)[source]
Verify whether all passed unit vectors are unique.
- Parameters:
p (
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]) – List of indicator vectors- Return type:
Coroutine
[Any
,Any
,Awaitable
[bool
]]- Returns:
True if all indicator vectors are unique, False otherwise