distributed_keygen.utils module¶
Useful functions for the distributed keygen module.
- class distributed_keygen.utils.AdditiveVariable(label, modulus, owner=-1)[source]¶
Bases:
Variable
Simple additive secret sharing scheme.
- clone()[source]¶
Return a new instance of this class using the given instance. Essentially a shallow copy.
- Return type:
Any
- Returns:
new instance of this class with the same label and owner, but no shares or plaintext value
Return the share of party index.
- Parameters:
index (
int
) – index of the party to get the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.- Return type:
Any
- Returns:
the share of party index
- reconstruct()[source]¶
Reconstruct the secret value of this variable. See
ShamirShares.reconstruct_secret()
for more details.- Return type:
Any
- Returns:
the reconstructed secret value stored in this variable
Set the share of party index with the value share.
- Parameters:
index (
int
) – index of the party to set the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.share (
Any
) – the share to set for party index
- Return type:
None
Create and store a sharing of this variable. Only the owner of the variable can share if the _input field is set.
- Parameters:
index (
int
) – index of this party- Return type:
None
- class distributed_keygen.utils.Batched(var, batch_size)[source]¶
Bases:
Generic
[V
],Variable
A Batched Variable is a list of variables all representing copies of the same variable (with different values).
This class allows one to easily set and get the shares of all variables in the batch, useful when sending and receiving messages about this batch. Furthermore, the class implements arithmetic operations for operating on batches.
This class is useful if a calculation needs to be performed many times (e.g. try until succeed). The batch allows one to describe the computation as if one were using a single variable.
- __getitem__(index)[source]¶
Return the Variable at the given index.
- Parameters:
index (
int
) – index of the Variable to return- Return type:
TypeVar
(V
, bound=Variable
)- Returns:
Variable at the given index
- __init__(var, batch_size)[source]¶
Create a Batched Variable from a single Variable.
A list of ‘batch_size’ Variables is created, where each variable is instantiated as a shallow copy of the given ‘var’, which can be seen as the blueprint. Note this will only copy over the meta-data of the variable, not the value.
- Parameters:
var (
TypeVar
(V
, bound=Variable
)) – Variable to batchbatch_size (
int
) – number of copies of the Variable
- batch_size:
int
¶
- clone()[source]¶
Return a new instance of this class using the given instance. Essentially a shallow copy.
- Return type:
Any
- Returns:
new instance of this class with the same label and owner, but no shares or plaintext value
Return the share of party index.
- Parameters:
index (
int
) – index of the party to get the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.- Return type:
list
[Any
]- Returns:
the share of party index
- reconstruct()[source]¶
Reconstruct the secret value of this variable.
- Return type:
Any
- Returns:
the reconstructed secret value stored in this variable
- set_plaintext(value)[source]¶
Set the value of this variable to the given value. Only the owner of the variable can set the plaintext value.
- Parameters:
value (
int
) – value to set this variable to- Return type:
None
- set_plaintexts(values)[source]¶
Set the value of all variables in the batch. Each variable is set individually.
- Parameters:
values (
list
[int
]) – list of values to set- Return type:
None
Set the share of party index with the value share.
- Parameters:
index (
int
) – index of the party to set the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.share (
Any
) – the share to set for party index
- Return type:
None
Create and store a sharing of this variable. Only the owner of the variable can share if the _input field is set.
- Parameters:
index (
int
) – index of this party- Return type:
None
- class distributed_keygen.utils.ShamirVariable(shamir, label, owner=-1)[source]¶
Bases:
Variable
Implementation of a secret-shared Variable using the Shamir Secret Sharing scheme.
- clone()[source]¶
Return a new instance of this class using the given instance. Essentially a shallow copy.
- Return type:
Any
- Returns:
new instance of this class with the same label and owner, but no shares or plaintext value
Return the share of party index.
- Parameters:
index (
int
) – index of the party to get the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.- Raises:
ValueError – if there is no share for party index
- Return type:
Any
- Returns:
the share of party index
Return all shares of this variable.
- Return type:
dict
[int
,int
]- Returns:
a dictionary mapping party indices to shares
- reconstruct()[source]¶
Reconstruct the secret value of this variable. See
ShamirShares.reconstruct_secret()
for more details.- Return type:
Any
- Returns:
the reconstructed secret value stored in this variable
Set the share of party index with the value share.
- Parameters:
index (
int
) – index of the party to set the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.share (
Any
) – the share to set for party index
- Return type:
None
Create and store a sharing of this variable. Only the owner of the variable can share if the _input field is set.
- Parameters:
index (
int
) – index of this party- Return type:
None
Bases:
object
Shares contains all shares of this party. Every subclass contains an object for that element, such as $p$ or $q$. These objects contain up to two entries: “additive” and “shares”, in “additive”, the local additive share of that element is stored, in “shares”, the shamir shares of the local additive share are stored.
To support the batching of messages for compute_modulus, we store lists of $P$’s and $Q$’s.
Bases:
object
Shares of $beta$.
Bases:
object
Shares of $lambda$.
Bases:
object
Shares of $n$.
Bases:
object
Shares of $p$.
Bases:
object
Shares of $q$.
Bases:
object
Shares of the secret key.
- class distributed_keygen.utils.Variable(label, owner=-1)[source]¶
Bases:
object
This class represents a variable that is secret shared between parties and eases tracking the shares.
A variable has a label and an owner. Each party needs to create an instance of Variable with the same label for each variable used in the computation. The label should uniquely identify the variable in the computation. The owner is the index of the party that owns the variable. The owner is the only party that can set the plaintext value of the variable (the party providing the input).
It is possible for the variable to not have an owner (owner=-1) if no party knows the secret value (e.g. after a multiplication with another Variable).
A Variable stores its value in two fields, _input and _sharing. The _input field stores the original plaintext value of this variable. This field can only be read and written if the party is the owner of the variable. The _sharing is the object used to store the shares this party has of the variable.
- __add__(other)[source]¶
Add this variable with another variable. The other variable must be of the same type as this variable.
- Parameters:
other (
Any
) – variable to add with this variable- Return type:
Any
- Returns:
a new variable storing the result of adding the two variables
- __init__(label, owner=-1)[source]¶
Create a new Variable with the given label and owner.
- Parameters:
label (
str
) – label of this variableowner (
int
) – index of the party that owns this variable
- __mul__(other)[source]¶
Multiply this variable with another variable. The other variable must be of the same type as this variable.
- Parameters:
other (
Any
) – variable to multiply with this variable- Return type:
Any
- Returns:
a new variable storing the result of multiplying the two variables
- clone()[source]¶
Return a new instance of this class using the given instance. Essentially a shallow copy.
- Return type:
Any
- Returns:
new instance of this class with the same label and owner, but no shares or plaintext value
- get_plaintext()[source]¶
Return the original plaintext used as input to set this variable. Only the owner of the variable can know this value.
- Raises:
ValueError – if the plaintext value of this variable is not known
- Return type:
int
- Returns:
the plaintext value of this variable
Return the share of party index.
- Parameters:
index (
int
) – index of the party to get the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.- Return type:
Any
- Returns:
the share of party index
- reconstruct()[source]¶
Reconstruct the secret value of this variable.
- Return type:
Any
- Returns:
the reconstructed secret value stored in this variable
- set_plaintext(value)[source]¶
Set the value of this variable to the given value. Only the owner of the variable can set the plaintext value.
- Parameters:
value (
int
) – value to set this variable to- Return type:
None
Set the share of party index with the value share.
- Parameters:
index (
int
) – index of the party to set the share of. index must be in the range $[0, n-1]$ where $n$ is the number of parties.share (
Any
) – the share to set for party index
- Return type:
None
Create and store a sharing of this variable. Only the owner of the variable can share if the _input field is set.
- Parameters:
index (
int
) – index of this party- Return type:
None
- async distributed_keygen.utils.exchange_reconstruct(variables, index, pool, party_indices, msg_id)[source]¶
Exchange shares of this variable with the other parties in the pool to allow all parties to locally reconstruct the variable.
All parties send, for the variables in the group they own, to the other parties the shares intended for said party. Each party receives the shares intended for them.
Note: This mutates the variables in the group.
- Parameters:
group (
list
[TypeVar
(V
, bound=Variable
)]) – a list of variables to consider in this exchangeindex (
int
) – index of this partypool (
Pool
) – network of involved partiesparty_indices (
dict
[str
,int
]) – mapping from party names to indicesmsg_id (
str
) – Optional message id.
- Raises:
ValueError – if a variable is received with an unknown label
- Return type:
None