INNER CODE UNIT · Python
d
lschoe/mpyc · demos/SecretSantaExplained.py:62
c, d = a + b, a - b # c, d are placeholders of type secint
e = c * d # e is a placeholder of type secint
f = mpc.run(mpc.output(e)) # forces actual computation of c, d, e
print('Example: (5+13)*(5-13) =', f)
# Evaluation of this piece of code first creates three (empty) placeholders of type `secint`. The last but one line forces the actual computation of the (secret-shared) values of `c`, `d`, and `e`.
#
# In this notebook, however, we will not focus on the asynchronous aspects of MPyC. Readers not familiar with coroutines and asynchronous computation may therefore skip to the next section, ignoring all aspects of asynchronous computation. Most MPyC code is meant to be understandable without bothering about the (order of) execution of the code anyway!
#
# A Python coroutine, defined by the keyword `async` at the start of a function definition, is turned into an MPyC coroutine by using the decorator `mpc.coroutine`.
# When called, an MPyC coroutine will return immediately (nonblocking). The main difference with Python coroutines is that an MPyC coroutine will return a placeholder or, more generally, nested lists/tuples containing placeholders. The placeholders are typed (e.g., of type `secint`, or any other secure MPyC type), and the type of the placeholders is defined by the first `await` expression in the MPyC coroutine, using the `mpc.returnType` method.
# ## Random derangements from random permutations
#
# To generate a uniformly random derangement, we basically proceed in the traditional way: we randomly draw all numbers from a hat and start all over if there are any fixed points. A permutation $p$, viewed as a sequence of length $n$, is a derangement exactly when $\forall_{0\leq i<n} \ p_i \neq i$.
#
# We represent a secret derangement $p$ by a list with elements of type `secint`. The idea is to first generate a random permutation and then check if this permutation happens to be free of fixed points. If any fixed points are found, we start all over again.