Skip to content

Commit

Permalink
Streamlined help -h -H.
Browse files Browse the repository at this point in the history
Added -H --HELP flag. Dropped mpc.args.
  • Loading branch information
lschoe authored Sep 27, 2018
1 parent 9897f6c commit 0b9face
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 291 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ See `demos` for usage examples.

1. Python 3.6 or higher required (Python 3.5 or lower is not sufficient).

2. Installing package `gmpy2` is optional, but will considerably benefit the performance of `mpyc`.
2. Installing package `gmpy2` is optional, but will considerably enhance the performance of `mpyc`.
On Linux, `pip install gmpy2` should do the job, but on Windows, this may fail with compiler errors.
Fortunately, ready-to-go Python wheels for `gmpy2` can be downloaded from Christoph Gohlke's excellent
[Unofficial Windows Binaries for Python Extension Packages](https://www.lfd.uci.edu/~gohlke/pythonlibs/) webpage.
Expand Down
343 changes: 157 additions & 186 deletions demos/4demos.ipynb

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions demos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ run -1 secretsanta.py
run -2 secretsanta.py
run 1 id3gini.py -h
run 1 id3gini.py -H
run-all 1
run-all 2
Expand Down
83 changes: 36 additions & 47 deletions demos/SecretSantaExplained.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2018-05-23 08:58:26,927 Start\n"
"2018-09-27 09:33:27,712 Start MPyC runtime v0.3.5\n"
]
}
],
Expand Down Expand Up @@ -286,7 +286,7 @@
"metadata": {},
"outputs": [],
"source": [
"m = 7"
"N = 7"
]
},
{
Expand All @@ -302,16 +302,16 @@
" 1 [1]\n",
" 2 [0, 1]\n",
" 3 [0, 0, 1]\n",
" 4 [0, 1, 0, 0]\n",
" 5 [0, 0, 1, 0, 0]\n",
" 6 [1, 0, 0, 0, 0, 0]\n",
" 7 [1, 0, 0, 0, 0, 0, 0]\n"
" 4 [0, 0, 0, 1]\n",
" 5 [0, 0, 0, 1, 0]\n",
" 6 [0, 0, 0, 0, 1, 0]\n",
" 7 [0, 0, 0, 0, 0, 1, 0]\n"
]
}
],
"source": [
"print('Random unit vectors:')\n",
"for n in range(1, m + 1):\n",
"for n in range(1, N + 1):\n",
" s = mpc.run(mpc.output(random_unit_vector(n)))\n",
" print('{0:2} {1}'.format(n, s))"
]
Expand All @@ -328,17 +328,17 @@
"Random permutations:\n",
" 1 [0]\n",
" 2 [1, 0]\n",
" 3 [0, 2, 1]\n",
" 4 [0, 3, 2, 1]\n",
" 5 [0, 1, 4, 2, 3]\n",
" 6 [0, 1, 3, 5, 4, 2]\n",
" 7 [4, 2, 3, 6, 5, 0, 1]\n"
" 3 [1, 0, 2]\n",
" 4 [2, 1, 3, 0]\n",
" 5 [1, 0, 3, 4, 2]\n",
" 6 [1, 2, 3, 0, 5, 4]\n",
" 7 [5, 4, 2, 6, 0, 1, 3]\n"
]
}
],
"source": [
"print('Random permutations:')\n",
"for n in range(1, m + 1):\n",
"for n in range(1, N + 1):\n",
" s = mpc.run(mpc.output(random_permutation(n)))\n",
" print('{0:2} {1}'.format(n, s))"
]
Expand All @@ -354,28 +354,21 @@
"text": [
"Random derangements:\n",
" 2 [1, 0]\n",
" 3 [1, 2, 0]\n",
" 4 [1, 2, 3, 0]\n",
" 5 [1, 0, 4, 2, 3]\n",
" 6 [5, 3, 4, 0, 1, 2]\n",
" 7 [5, 3, 0, 1, 6, 2, 4]\n"
" 3 [2, 0, 1]\n",
" 4 [2, 3, 1, 0]\n",
" 5 [4, 0, 3, 2, 1]\n",
" 6 [4, 5, 0, 2, 3, 1]\n",
" 7 [3, 4, 5, 2, 1, 6, 0]\n"
]
}
],
"source": [
"print('Random derangements:')\n",
"for n in range(2, m + 1):\n",
"for n in range(2, N + 1):\n",
" s = mpc.run(mpc.output(random_derangement(n)))\n",
" print('{0:2} {1}'.format(n, s))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This concludes the explanation of our MPyC protocol for the Secret Santa problem. The demo `secretsanta.py` can be run with relies on the same "
]
},
{
"cell_type": "code",
"execution_count": 12,
Expand All @@ -385,7 +378,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2018-05-23 08:58:27,896 Stop -- elapsed time: 0:00:00.953313\n"
"2018-09-27 09:33:28,959 Stop MPyC runtime -- elapsed time: 0:00:01.227762\n"
]
}
],
Expand Down Expand Up @@ -416,8 +409,8 @@
"output_type": "stream",
"text": [
"\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mpyc\\demos>python run.py 1 SecretSantaExplained.py \n",
"2018-05-23 08:58:28,496 Start\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mympyc\\demos>python run.py 1 SecretSantaExplained.py \n",
"2018-09-27 09:33:29,624 Start MPyC runtime v0.3.5\n",
"Example: (5+13)*(5-13) = -144\n",
"Random unit vectors:\n",
" 1 [1]\n",
Expand All @@ -442,7 +435,7 @@
" 5 [2, 3, 4, 0, 1]\n",
" 6 [5, 0, 3, 2, 1, 4]\n",
" 7 [1, 0, 6, 5, 2, 4, 3]\n",
"2018-05-23 08:58:28,761 Stop -- elapsed time: 0:00:00.249998\n"
"2018-09-27 09:33:29,889 Stop MPyC runtime -- elapsed time: 0:00:00.265625\n"
]
},
{
Expand Down Expand Up @@ -479,11 +472,9 @@
"output_type": "stream",
"text": [
"\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mpyc\\demos>python run.py 3 SecretSantaExplained.py \n",
"2018-05-23 08:58:29,416 Start\n",
"2018-05-23 08:58:29,416 Listening on port 11365\n",
"2018-05-23 08:58:29,416 Connecting to <_Party 1: localhost:11366>\n",
"2018-05-23 08:58:29,947 Connecting to <_Party 2: localhost:11367>\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mympyc\\demos>python run.py 3 SecretSantaExplained.py \n",
"2018-09-27 09:33:30,561 Start MPyC runtime v0.3.5\n",
"2018-09-27 09:33:31,092 Connected to all parties.\n",
"Example: (5+13)*(5-13) = -144\n",
"Random unit vectors:\n",
" 1 [1]\n",
Expand All @@ -508,7 +499,7 @@
" 5 [1, 0, 3, 4, 2]\n",
" 6 [1, 0, 3, 4, 5, 2]\n",
" 7 [3, 0, 1, 5, 6, 2, 4]\n",
"2018-05-23 08:58:30,744 Stop -- elapsed time: 0:00:01.328119\n"
"2018-09-27 09:33:31,905 Stop MPyC runtime -- elapsed time: 0:00:01.343741\n"
]
},
{
Expand Down Expand Up @@ -536,20 +527,18 @@
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mpyc\\demos>python run.py 5 SecretSantaExplained.py \n",
"2018-05-23 08:58:31,658 Start\n",
"2018-05-23 08:58:31,658 Listening on port 11365\n",
"2018-05-23 08:58:31,658 Connecting to <_Party 1: localhost:11366>\n",
"2018-05-23 08:58:32,184 Connecting to <_Party 2: localhost:11367>\n",
"2018-05-23 08:58:32,184 Connecting to <_Party 3: localhost:11368>\n",
"2018-05-23 08:58:32,184 Connecting to <_Party 4: localhost:11369>\n",
"C:\\Users\\Berry\\Documents\\GitHub\\mympyc\\demos>python run.py 5 SecretSantaExplained.py \n",
"2018-09-27 09:33:32,959 Start MPyC runtime v0.3.5\n",
"2018-09-27 09:33:33,522 Connected to all parties.\n",
"Example: (5+13)*(5-13) = -144\n",
"Random unit vectors:\n",
" 1 [1]\n",
Expand All @@ -574,7 +563,7 @@
" 5 [3, 0, 1, 4, 2]\n",
" 6 [4, 0, 3, 2, 5, 1]\n",
" 7 [1, 6, 5, 2, 3, 0, 4]\n",
"2018-05-23 08:58:34,811 Stop -- elapsed time: 0:00:03.153135\n"
"2018-09-27 09:33:35,725 Stop MPyC runtime -- elapsed time: 0:00:02.749991\n"
]
},
{
Expand All @@ -598,7 +587,7 @@
"source": [
"If desired, the code can also be run with two or four parties. The included test setup supports up to $m=5$ parties, of which at most $t$ can be corrupt, with $0\\leq t\\leq \\lfloor (m-1)/2 \\rfloor$.\n",
"\n",
"The Python script [secretsanta.py](secretsanta.py) contains a slightly more extensive demo."
"This concludes the explanation of our MPyC protocol for the Secret Santa problem. The Python script [secretsanta.py](secretsanta.py) contains a slightly more extensive demo, showing how secure fixed-point arithmetic or secure prime fields can be used instead of secure integers."
]
}
],
Expand All @@ -618,7 +607,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.6"
}
},
"nbformat": 4,
Expand Down
18 changes: 8 additions & 10 deletions demos/SecretSantaExplained.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ async def random_unit_vector(n): # returns list of n secint e
# In[8]:


m = 7
N = 7


# In[9]:


print('Random unit vectors:')
for n in range(1, m + 1):
for n in range(1, N + 1):
s = mpc.run(mpc.output(random_unit_vector(n)))
print('{0:2} {1}'.format(n, s))

Expand All @@ -213,7 +213,7 @@ async def random_unit_vector(n): # returns list of n secint e


print('Random permutations:')
for n in range(1, m + 1):
for n in range(1, N + 1):
s = mpc.run(mpc.output(random_permutation(n)))
print('{0:2} {1}'.format(n, s))

Expand All @@ -222,13 +222,11 @@ async def random_unit_vector(n): # returns list of n secint e


print('Random derangements:')
for n in range(2, m + 1):
for n in range(2, N + 1):
s = mpc.run(mpc.output(random_derangement(n)))
print('{0:2} {1}'.format(n, s))


# This concludes the explanation of our MPyC protocol for the Secret Santa problem. The demo `secretsanta.py` can be run with relies on the same

# In[12]:


Expand All @@ -237,9 +235,9 @@ async def random_unit_vector(n): # returns list of n secint e

# ## Deploying the Python code with multiple parties
#
# All the Python code contained in this notebook can be saved as Python script using the `Download as` option from the `File` menu in Jupyter notebooks. Choose to download the notebook as `Python (.py)`.
# All the Python code contained in this notebook can be saved as a Python script using the `Download as` option from the `File` menu in Jupyter notebooks. Choose to download the notebook as `Python (.py)`.
#
# We did so, using the file name `SecretSantaExplained.py` and stored this file in the same directory as the present notebook. Then we can run the Python script, as shown below.
# We have done so and the resulting file [SecretSantaExplained.py](SecretSantaExplained.py) is stored in the same directory as the present notebook. Now we can run the Python script, as shown below.
#
# First we show a run with one party only. However, this time the code runs outside the Jupyter notebook, using its own Python interpreter.

Expand Down Expand Up @@ -267,6 +265,6 @@ async def random_unit_vector(n): # returns list of n secint e
get_ipython().system('run 5 SecretSantaExplained.py')


# If desired, the code can also be run with two or four parties. The inlcuded test setup supports up to $m=5$ parties, of which at most $t$ can be corrupt, with $0\leq t\leq \lfloor (m-1)/2 \rfloor$.
# If desired, the code can also be run with two or four parties. The included test setup supports up to $m=5$ parties, of which at most $t$ can be corrupt, with $0\leq t\leq \lfloor (m-1)/2 \rfloor$.
#
# The included Python script `secretsanta.py` contains a slightly more extensive demo.
# This concludes the explanation of our MPyC protocol for the Secret Santa problem. The Python script [secretsanta.py](secretsanta.py) contains a slightly more extensive demo, showing how secure fixed-point arithmetic or secure prime fields can be used instead of secure integers.
20 changes: 14 additions & 6 deletions demos/SecureSortingNetsExplained.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2018-09-27 10:05:29,486 Start MPyC runtime v0.3.5\n"
]
}
],
"source": [
"from mpyc.runtime import mpc # load MPyC\n",
"secint = mpc.SecInt() # 32-bit secure MPyC integers\n",
Expand Down Expand Up @@ -136,7 +144,7 @@
" for i in oddeven_merge_sort(len(x)): compare_and_swap(x, *i)\n",
" File \"<ipython-input-2-a99b48746a99>\", line 30, in compare_and_swap\n",
" if x[a] > x[b]:\n",
" File \"c:\\users\\berry\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\mpyc-0.29.0-py3.6.egg\\mpyc\\sectypes.py\", line 33, in __bool__\n",
" File \"c:\\users\\berry\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\mpyc-0.3.5-py3.6.egg\\mpyc\\sectypes.py\", line 33, in __bool__\n",
" raise TypeError('cannot use secure type in Boolean expressions')\n",
"TypeError: cannot use secure type in Boolean expressions\n"
]
Expand Down Expand Up @@ -166,7 +174,7 @@
"outputs": [],
"source": [
"def compare_and_swap(x, a, b):\n",
" c = x[a] > x[b] # secure comparison, c is a secint representing a secret-shared bit\n",
" c = x[a] > x[b] # secure comparison, secint c represents a secret-shared bit\n",
" d = c * (x[b] - x[a]) # secure subtraction\n",
" x[a], x[b] = x[a] + d, x[b] - d # secure swap: x[a], x[b] swapped if only if c=1"
]
Expand Down Expand Up @@ -294,7 +302,7 @@
" bitonic_compare(up, x)\n",
" File \"<ipython-input-7-8a5c3e13ac49>\", line 22, in bitonic_compare\n",
" if (x[i] > x[i + dist]) == up:\n",
" File \"c:\\users\\berry\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\mpyc-0.29.0-py3.6.egg\\mpyc\\sectypes.py\", line 33, in __bool__\n",
" File \"c:\\users\\berry\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\mpyc-0.3.5-py3.6.egg\\mpyc\\sectypes.py\", line 33, in __bool__\n",
" raise TypeError('cannot use secure type in Boolean expressions')\n",
"TypeError: cannot use secure type in Boolean expressions\n"
]
Expand Down Expand Up @@ -363,7 +371,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"2018-05-23 08:50:13,318 Stop -- elapsed time: 0:00:01.114068\n"
"2018-09-27 10:05:30,790 Stop MPyC runtime -- elapsed time: 0:00:01.287664\n"
]
}
],
Expand Down Expand Up @@ -395,7 +403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.6"
}
},
"nbformat": 4,
Expand Down
7 changes: 4 additions & 3 deletions demos/cnnmnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import os
import sys
import logging
import random
import gzip
Expand Down Expand Up @@ -125,19 +126,19 @@ def argmax(x):
def main():
global secnum

k = 1 if len(mpc.args) == 0 else float(mpc.args[0])
k = 1 if len(sys.argv) == 1 else float(sys.argv[1])
if k - int(k) == 0.5:
secnum = mpc.SecFxp(10, 4)
else:
secnum = mpc.SecInt(37)
batch_size = round(k - 0.01)
if len(mpc.args) <= 1:
if len(sys.argv) <= 2:
if len(mpc.parties) == 1:
offset = random.randrange(10001 - batch_size)
else:
offset = 0
else:
offset = int(mpc.args[1])
offset = int(sys.argv[2])
f = 6

mpc.start()
Expand Down
5 changes: 2 additions & 3 deletions demos/id3gini.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ def main():
global secint, S, C, attr_names

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data', help='Input data.')
parser.add_argument('options', nargs='*')
parser.add_argument('-d', '--data', help='input data')
parser.set_defaults(data='tennis')
args = parser.parse_args(mpc.args)
args = parser.parse_args()

secint = mpc.SecInt()

Expand Down
Loading

0 comments on commit 0b9face

Please sign in to comment.