Skip to content

Commit 4dbd3d5

Browse files
committed
Update some READMEs and tests.
1 parent 2629c01 commit 4dbd3d5

10 files changed

+132
-70
lines changed

demos/OneWayHashChainsExplained.ipynb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"\n",
99
"[Hash chains](https://en.wikipedia.org/wiki/Hash_chain) are well-known as a simple and efficient solution for asymmetric identification and authentication. They appear in many guises, often in contexts with severe performance constraints. For example, the [TESLA](https://www.rfc-editor.org/info/rfc4082) scheme and friends use hash chains at their core and provide authentication in wireless sensor networks and satellite communication.\n",
1010
"\n",
11-
"In this notebook we construct an MPyC program for handling hash chains in a multiparty setting. There will be no single point of failure, as no single party will ever know the secret information from which the hash chains are built. This way of protecting secret keys is similar to what companies like [Sepior](https://sepior.com/) and [Unbound](https://www.unboundsecurity.com/) do using threshold cryptography and MPC-based hardware security modules (HSMs).\n",
11+
"In this notebook we construct an MPyC program for handling hash chains in a multiparty setting. There will be no single point of failure, as no single party will ever know the secret information from which the hash chains are built. This way of protecting secret keys is similar to what companies like [Sepior](https://sepior.com/) and [Unbound](https://github.com/unboundsecurity) do using threshold cryptography and MPC-based hardware security modules (HSMs).\n",
1212
"\n",
1313
"## Hash Chains\n",
1414
"\n",
@@ -184,21 +184,21 @@
184184
"output_type": "stream",
185185
"text": [
186186
"Order-0 hash chain of length 1 (1 rounds):\n",
187-
" 1 x0 = dce02350924adb59103b4000066ba9b2\n",
187+
" 1 x0 = 604fc4433bbbba2e5c4796510010ed48\n",
188188
"\n",
189189
"Order-1 hash chain of length 2 (3 rounds):\n",
190190
" 1 -\n",
191-
" 2 x1 = 1f25ad5f0aec7b7da294b0eaf8a197ad\n",
192-
" 3 x0 = dce02350924adb59103b4000066ba9b2\n",
191+
" 2 x1 = fe242be2c20da3cd87370d1614277d0d\n",
192+
" 3 x0 = 604fc4433bbbba2e5c4796510010ed48\n",
193193
"\n",
194194
"Order-2 hash chain of length 4 (7 rounds):\n",
195195
" 1 -\n",
196196
" 2 -\n",
197197
" 3 -\n",
198-
" 4 x3 = 86310b3f27481602941f8092771b6b45\n",
199-
" 5 x2 = 0f3d5981719b4410636824cb98433953\n",
200-
" 6 x1 = 1f25ad5f0aec7b7da294b0eaf8a197ad\n",
201-
" 7 x0 = dce02350924adb59103b4000066ba9b2\n",
198+
" 4 x3 = fb38d81680a23a43d8063ee81de10c90\n",
199+
" 5 x2 = 8b613cbcfaa17ff36b1823d599f535e2\n",
200+
" 6 x1 = fe242be2c20da3cd87370d1614277d0d\n",
201+
" 7 x0 = 604fc4433bbbba2e5c4796510010ed48\n",
202202
"\n",
203203
"Order-3 hash chain of length 8 (15 rounds):\n",
204204
" 1 -\n",
@@ -208,14 +208,14 @@
208208
" 5 -\n",
209209
" 6 -\n",
210210
" 7 -\n",
211-
" 8 x7 = d06f7022bce379c300c5522b5859be10\n",
212-
" 9 x6 = 6bd7f5507fe750c620e562b5740d1618\n",
213-
"10 x5 = 21f54ab687cd1e4d8fbfa1b25401887a\n",
214-
"11 x4 = 7cc31a0733a1c1f9dc4723d56976dc37\n",
215-
"12 x3 = 86310b3f27481602941f8092771b6b45\n",
216-
"13 x2 = 0f3d5981719b4410636824cb98433953\n",
217-
"14 x1 = 1f25ad5f0aec7b7da294b0eaf8a197ad\n",
218-
"15 x0 = dce02350924adb59103b4000066ba9b2\n",
211+
" 8 x7 = 104a0c631c5a3fdac68ddc47dc140bce\n",
212+
" 9 x6 = a5a807b0270387b6a599545bf23a455c\n",
213+
"10 x5 = 74ba842fc3c53005b3ba4a3d56ae3228\n",
214+
"11 x4 = 5f6d0953546e5fbdc4a4c1f3cd1dd380\n",
215+
"12 x3 = fb38d81680a23a43d8063ee81de10c90\n",
216+
"13 x2 = 8b613cbcfaa17ff36b1823d599f535e2\n",
217+
"14 x1 = fe242be2c20da3cd87370d1614277d0d\n",
218+
"15 x0 = 604fc4433bbbba2e5c4796510010ed48\n",
219219
"\n"
220220
]
221221
}
@@ -238,7 +238,7 @@
238238
"cell_type": "markdown",
239239
"metadata": {},
240240
"source": [
241-
"The Python program [onewayhashchains.py](onewayhashchains.py) follows the same approach as presented in this notebook. In addition to the recursive pebbler shown above, however, the optimal binary pebbler is also implemented as an iterative algorithm."
241+
"The Python program [onewayhashchains.py](onewayhashchains.py) follows the same approach as presented in this notebook. In addition to the recursive pebbler shown above, however, the optimal binary pebbler is also implemented as an iterative algorithm. Moreover, [np_onewayhashchains.py](np_onewayhashchains.py) demos the use of the faster Numpy-based [np_aes.py](np_aes.py) reimplementation of the AES demo as well as the use of the Numpy-based [sha3.py](sha3.py) threshold SHA-3 hash functions."
242242
]
243243
}
244244
],

demos/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ Use `-H`, `--HELP` option with any demo to see the MPyC help message.
77
`python secretsanta.py -H`
88

99
```
10-
usage: secretsanta.py [-H] [-h] [-C ini] [-P addr] [-M m] [-I i] [-T t] [-B b]
11-
[--ssl] [-L l] [-K k] [--no-log] [--no-async]
12-
[--no-barrier] [--no-gmpy2] [--no-prss] [--mix32-64bit]
10+
usage: secretsanta.py [-H] [-h] [-C ini] [-P addr] [-M m] [-I i] [-T t]
11+
[-B b] [--ssl] [-L l] [-K k] [--log-level ll]
12+
[--no-log] [--no-async] [--no-barrier] [--no-gmpy2]
13+
[--no-numpy] [--no-prss] [--mix32-64bit]
1314
[--output-windows] [--output-file] [-f F]
1415
15-
optional arguments:
16+
MPyC help:
1617
-H, --HELP show this help message for MPyC and exit
17-
-h, --help show secretsanta.py help message (if any)
18+
-h, --help show help message for this MPyC program (if any)
1819
1920
MPyC configuration:
2021
-C ini, --config ini use ini file, defining all m parties
@@ -28,16 +29,18 @@ MPyC configuration:
2829
MPyC parameters:
2930
-L l, --bit-length l default bit length l for secure numbers
3031
-K k, --sec-param k security parameter k, leakage probability 2**-k
32+
--log-level ll logging level ll=debug/info(default)/warning/error
3133
--no-log disable logging messages
3234
--no-async disable asynchronous evaluation
3335
--no-barrier disable barriers
3436
--no-gmpy2 disable use of gmpy2 package
37+
--no-numpy disable use of numpy package
3538
--no-prss disable use of PRSS (pseudorandom secret sharing)
3639
--mix32-64bit enable mix of 32-bit and 64-bit platforms
3740
3841
MPyC misc:
39-
--output-windows screen output for parties i>0 (only on Windows)
40-
--output-file append output for parties i>0 to party{m}_{i}.log
42+
--output-windows screen output for parties 0<i<m (one window each)
43+
--output-file append output of parties 0<i<m to party{m}_{i}.log
4144
-f F consume IPython's -f argument F
4245
```
4346

@@ -89,7 +92,7 @@ optional arguments:
8992

9093
`python cnnmnist.py 1 0`
9194

92-
`python cnnmnist.py 3`
95+
`python np_cnnmnist.py 3.5`
9396

9497
`python cnnmnist.py -M1 1.5 0`
9598

@@ -121,6 +124,6 @@ optional arguments:
121124

122125
[KaplanMeierSurvivalExplained](KaplanMeierSurvivalExplained.ipynb) presents privacy-preserving Kaplan-Meier survival analysis, featuring aggregate Kaplan-Meier curves and secure logrank tests.
123126

124-
[4demos](4demos.ipynb) gives quick access to demos secretsanta.py, id3gini.py, lpsolver.py, cnnmnist.py.
127+
[4demos](4demos.ipynb) gives quick access to demos secretsanta.py, id3gini.py, lpsolver.py, np_cnnmnist.py.
125128

126129
[OneWayHashChainsExplained](OneWayHashChainsExplained.ipynb) shows a more advanced MPyC program.

demos/np_onewayhashchains.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
This demo is an extended reimplementation of the onewayhashchain.py demo for
44
generating and reversing one-way hash chains in a multiparty setting.
55
6-
In addition to the Matyas-Meyer-Oseas one-way function based on AES, the SHAKE128
7-
oneway function from the SHA3 faimlty is also provided as an option.
6+
Next to the Matyas-Meyer-Oseas one-way function based on AES, the SHAKE128
7+
one-way function from the SHA3 family is also provided as an option.
88
99
Note that in the output stage the hashes pertaining to different pebbles are
1010
evaluated in parallel, without increasing the overall round complexity. Multiple
1111
hashes pertaining to the same pebble, however, are necessarily evaluated in series,
12-
increasing the overall round complxity accordingly.
12+
increasing the overall round complexity accordingly.
1313
1414
See demo onewayhashchain.py for more information.
1515
"""

docs/mpyc.numpy.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2+
<html><head><title>Python: module mpyc.numpy</title>
3+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
4+
</head><body bgcolor="#f0f0f8">
5+
6+
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
7+
<tr bgcolor="#7799ee">
8+
<td valign=bottom>&nbsp;<br>
9+
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="mpyc.html"><font color="#ffffff">mpyc</font></a>.numpy</strong></big></big></font></td
10+
><td align=right valign=bottom
11+
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="https://github.com/lschoe/mpyc/blob/master/mpyc/numpy.py">github.com/lschoe/mpyc/blob/master/mpyc/numpy.py</a></font></td></tr></table>
12+
<p><tt>This&nbsp;module&nbsp;acts&nbsp;as&nbsp;a&nbsp;stub&nbsp;to&nbsp;avoid&nbsp;a&nbsp;dependency&nbsp;for&nbsp;the&nbsp;numpy&nbsp;package.<br>
13+
&nbsp;<br>
14+
If&nbsp;the&nbsp;numpy&nbsp;package&nbsp;is&nbsp;not&nbsp;available,&nbsp;MPyC&nbsp;still&nbsp;runs&nbsp;but&nbsp;with&nbsp;less&nbsp;functionality.<br>
15+
Use&nbsp;of&nbsp;NumPy&nbsp;can&nbsp;be&nbsp;disabled&nbsp;to&nbsp;avoid&nbsp;loading&nbsp;the&nbsp;numpy&nbsp;package.<br>
16+
&nbsp;<br>
17+
If&nbsp;NumPy&nbsp;is&nbsp;enabled&nbsp;(available&nbsp;and&nbsp;not&nbsp;disabled),&nbsp;the&nbsp;MPyC&nbsp;runtime&nbsp;supports&nbsp;array<br>
18+
types---along&nbsp;with&nbsp;vectorized&nbsp;implementations---for&nbsp;secure&nbsp;numbers&nbsp;and&nbsp;the&nbsp;underlying<br>
19+
finite&nbsp;field&nbsp;types.&nbsp;The&nbsp;array&nbsp;types&nbsp;are&nbsp;accessible&nbsp;through&nbsp;the&nbsp;'array'&nbsp;attribute,<br>
20+
e.g.,&nbsp;for&nbsp;secint48=mpc.SecInt(48),&nbsp;the&nbsp;array&nbsp;type&nbsp;is&nbsp;secint48.array&nbsp;and&nbsp;the&nbsp;array&nbsp;type<br>
21+
for&nbsp;the&nbsp;underlying&nbsp;prime&nbsp;field&nbsp;is&nbsp;secint48.field.array.<br>
22+
&nbsp;<br>
23+
...&nbsp;work&nbsp;in&nbsp;progress&nbsp;for&nbsp;MPyC&nbsp;version&nbsp;0.9</tt></p>
24+
<p>
25+
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
26+
<tr bgcolor="#aa55cc">
27+
<td colspan=3 valign=bottom>&nbsp;<br>
28+
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
29+
30+
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
31+
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="logging.html">logging</a><br>
32+
</td><td width="25%" valign=top><a href="numpy.html">numpy</a><br>
33+
</td><td width="25%" valign=top><a href="os.html">os</a><br>
34+
</td><td width="25%" valign=top></td></tr></table></td></tr></table>
35+
</body></html>

mpyc/README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
## Synopsis
22

3-
[MPyC](https://lschoe.github.io/mpyc) currently consists of 13 modules (all in pure Python):
3+
[MPyC](https://lschoe.github.io/mpyc) currently consists of 14 modules (all in pure Python):
44

55
1. [gmpy](https://lschoe.github.io/mpyc/mpyc.gmpy.html): some basic number theoretic algorithms (using GMP via Python package gmpy2, if installed)
6-
2. [gfpx](https://lschoe.github.io/mpyc/mpyc.gfpx.html): polynomial arithmetic over arbitrary prime fields
7-
3. [finfields](https://lschoe.github.io/mpyc/mpyc.finfields.html): arbitrary finite fields, including binary fields and prime fields
8-
4. [fingroups](https://lschoe.github.io/mpyc/mpyc.fingroups.html): finite groups, in particular for use in cryptography (elliptic curves, Schnorr groups, etc.)
9-
5. [thresha](https://lschoe.github.io/mpyc/mpyc.thresha.html): threshold Shamir (and also pseudorandom) secret sharing
10-
6. [sectypes](https://lschoe.github.io/mpyc/mpyc.sectypes.html): SecInt/Fld/Fxp/Flt types for secure (secret-shared) integer/finite-field/fixed-/floating-point values
11-
7. [asyncoro](https://lschoe.github.io/mpyc/mpyc.asyncoro.html): asynchronous communication and computation of secret-shared values
12-
8. [runtime](https://lschoe.github.io/mpyc/mpyc.runtime.html): core MPC protocols (many hidden by Python's operator overloading)
13-
9. [mpctools](https://lschoe.github.io/mpyc/mpyc.mpctools.html): reduce and accumulate with log round complexity
14-
10. [seclists](https://lschoe.github.io/mpyc/mpyc.seclists.html): secure lists with oblivious access and updates
15-
11. [secgroups](https://lschoe.github.io/mpyc/mpyc.secgroups.html): SecGrp types for secure (secret-shared) finite group elements
16-
12. [random](https://lschoe.github.io/mpyc/mpyc.random.html): securely mimicking Python’s [random](https://docs.python.org/3/library/random.html) module
17-
13. [statistics](https://lschoe.github.io/mpyc/mpyc.statistics.html): securely mimicking Python’s [statistics](https://docs.python.org/3/library/statistics.html) module
6+
2. [numpy](https://lschoe.github.io/mpyc/mpyc.numpy.html): stub to avoid dependency on NumPy package (also handling version issues, etc.)
7+
3. [gfpx](https://lschoe.github.io/mpyc/mpyc.gfpx.html): polynomial arithmetic over arbitrary prime fields
8+
4. [finfields](https://lschoe.github.io/mpyc/mpyc.finfields.html): arbitrary finite fields, including binary fields and prime fields
9+
5. [fingroups](https://lschoe.github.io/mpyc/mpyc.fingroups.html): finite groups, in particular for use in cryptography (elliptic curves, Schnorr groups, etc.)
10+
6. [thresha](https://lschoe.github.io/mpyc/mpyc.thresha.html): threshold Shamir (and also pseudorandom) secret sharing
11+
7. [sectypes](https://lschoe.github.io/mpyc/mpyc.sectypes.html): SecInt/Fld/Fxp/Flt types for secure (secret-shared) integer/finite-field/fixed-/floating-point values
12+
8. [asyncoro](https://lschoe.github.io/mpyc/mpyc.asyncoro.html): asynchronous communication and computation of secret-shared values
13+
9. [runtime](https://lschoe.github.io/mpyc/mpyc.runtime.html): core MPC protocols (many hidden by Python's operator overloading)
14+
10. [mpctools](https://lschoe.github.io/mpyc/mpyc.mpctools.html): reduce and accumulate with log round complexity
15+
11. [seclists](https://lschoe.github.io/mpyc/mpyc.seclists.html): secure lists with oblivious access and updates
16+
12. [secgroups](https://lschoe.github.io/mpyc/mpyc.secgroups.html): SecGrp types for secure (secret-shared) finite group elements
17+
13. [random](https://lschoe.github.io/mpyc/mpyc.random.html): securely mimicking Python’s [random](https://docs.python.org/3/library/random.html) module
18+
14. [statistics](https://lschoe.github.io/mpyc/mpyc.statistics.html): securely mimicking Python’s [statistics](https://docs.python.org/3/library/statistics.html) module
1819

1920
The modules are listed in topological order w.r.t. internal dependencies:
2021

21-
- Modules 1-4 are basic modules which can also be used outside an MPC context
22-
- Modules 5-8 form the core of MPyC
23-
- Modules 9-11 form the extended core of MPyC
24-
- Modules 12-13 are small libraries on top of the (extended) core
22+
- Modules 1-5 are basic modules which can also be used outside an MPC context
23+
- Modules 6-9 form the core of MPyC
24+
- Modules 10-12 form the extended core of MPyC
25+
- Modules 13-14 are small libraries on top of the (extended) core

mpyc/runtime.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ def block_shape(a):
23732373
return tuple(_block_shape(a, block_ndim(a))[0]) # TODO: move this to mpyc.numpy module
23742374

23752375
await self.returnType((sectype, block_shape(arrays)))
2376-
arrays = await self.gather(arrays)
2376+
arrays = await self.gather(arrays) # TODO: handle secfxp
23772377
return np.block(arrays)
23782378

23792379
@mpc_coro_no_pc
@@ -2520,14 +2520,18 @@ async def np_roll(self, a, shift, axis=None):
25202520
return np.roll(a, shift, axis)
25212521

25222522
@mpc_coro_no_pc
2523-
async def np_neg(self, a):
2523+
async def np_negative(self, a):
25242524
if not a.frac_length:
25252525
await self.returnType((type(a), a.shape))
25262526
else:
25272527
await self.returnType((type(a), a.integral, a.shape))
25282528
a = await self.gather(a)
25292529
return -a
25302530

2531+
def np_absolute(self, a, l=None):
2532+
"""Secure absolute value of a."""
2533+
return (-2*self.np_sgn(a, l=l, LT=True) + 1) * a
2534+
25312535
def np_less(self, a, b):
25322536
"""Secure comparison a < b."""
25332537
return self.np_sgn(a - b, LT=True)

mpyc/sectypes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,11 @@ def _coerce2(self, other):
10901090

10911091
def __neg__(self):
10921092
"""Matrix negation."""
1093-
return runtime.np_neg(self)
1093+
return runtime.np_negative(self)
1094+
1095+
def __abs__(self):
1096+
"""Matrix absolute value."""
1097+
return runtime.np_absolute(self)
10941098

10951099
def __add__(self, other):
10961100
"""Matrix addition."""
@@ -1381,8 +1385,6 @@ def _coerce(self, other):
13811385

13821386
def _coerce2(self, other):
13831387
if isinstance(other, float):
1384-
if other.is_integer():
1385-
other = round(other)
13861388
return other # TODO: consider returning np.array(other) here
13871389

13881390
return super()._coerce2(other)

tests/test_finfields.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,13 @@ def test_array_ufunc(self):
354354
np.negative.at(a, (1, 1)) # NB: in-place
355355
np.assertEqual(np.add.reduce(a, 1), [16, 0])
356356
np.assertEqual(np.add.reduce(np.add.reduce(a, 1)), 16)
357+
a != a
357358
a += 2
358359
a -= 2
359360
a *= 3
361+
a >>= 2
362+
a <<= 1
363+
a = np.right_shift(np.left_shift(a, 2), 1)
360364
np.add(np.array([1], dtype=np.int32), a)
361365
np.add(a, np.array([1], dtype=np.int64))
362366
self.assertRaises(TypeError, np.add, np.array([1], dtype=np.float64), a)
@@ -382,8 +386,13 @@ def test_array_ufunc(self):
382386
np.assertEqual(np.reciprocal(F_b) * F_b, np.ones(b.shape, dtype='O'))
383387
np.assertEqual(np.sqrt(F_a**2)**2, F_a**2)
384388

385-
F81_b = self.f81.array(b)
386-
np.assertEqual(np.sqrt(F81_b**2)**2, F81_b**2)
389+
F27_b = self.f27.array(b)
390+
F27_b = 1 / (1 / F27_b)
391+
np.assertEqual(np.sqrt(F27_b**2)**2, F27_b**2)
392+
393+
F81_b2 = self.f81.array(b)**2
394+
self.assertTrue((F81_b2).is_sqr().all())
395+
np.assertEqual(F81_b2.sqrt(INV=True)**2, 1/F81_b2)
387396

388397
@unittest.skipIf(not np, 'NumPy not available or inside MPyC disabled')
389398
def test_ndarray(self):

tests/test_gmpy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def te_s_t(a, b):
4343
self.assertTrue(te_s_t(1234, -2*1234))
4444
self.assertTrue(te_s_t(-2*12364, 12364))
4545

46-
# self.assertEqual(gmpy.invert(3, -1), 0) # pending gmpy2 issue if modulus is 1 or -1
46+
self.assertEqual(gmpy.invert(3, -1), 0)
4747
self.assertEqual(gmpy.invert(3, 257), 86)
4848
self.assertRaises(ZeroDivisionError, gmpy.invert, 2, 0)
4949
self.assertRaises(ZeroDivisionError, gmpy.invert, 2, 4)

0 commit comments

Comments
 (0)