Skip to content

Commit 20e1564

Browse files
authored
Merge pull request #17 from jherland/cleanup_v2
Python cleanup v2
2 parents 5fcab62 + eabe7cf commit 20e1564

9 files changed

Lines changed: 55 additions & 16 deletions

File tree

.flake8

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[flake8]
2-
ignore = E203, E266, E501, W503, F403, F401
2+
exclude = .??*, venv
3+
ignore = E203, E266, E501
34
max-line-length = 88
45
max-complexity = 18
5-
select = B,C,E,F,W,T4,B9
6+
select = B,C,E,F,W,T4,B9

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ Bluetooth devices or backend services.
7171

7272
You'll need to install the project. For example as follows:
7373

74-
```
74+
```bash
7575
python3 -m venv venv
7676
source venv/bin/activate
77-
pip3 install -e "."
77+
pip3 install -e .
7878
```
7979

8080
## Running the example
8181

8282
After installing the project you can run the examples:
8383

84-
```
84+
```bash
8585
examples/run_lowcost.py
8686
examples/run_unlinkable.py
8787
```
@@ -90,7 +90,7 @@ examples/run_unlinkable.py
9090

9191
After installing the project you can obtain test vectors by running:
9292

93-
```
93+
```bash
9494
utils/testvectors_lowcost.py
9595
utils/testvectors_unlinkable.py
9696
```
@@ -105,7 +105,7 @@ venv/bin/ativate`) to ensure that the paths are picked up correctly.
105105

106106
Please install the proper pre-commit-hooks so that the files stay formatted:
107107

108-
```
108+
```bash
109109
pip install pre-commit
110110
pre-commit install
111111
```

dp3t/protocols/lowcost.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ def batch_start_from_time(time):
7777
return (int(time.timestamp()) // SECONDS_PER_BATCH) * SECONDS_PER_BATCH
7878

7979

80+
def secure_shuffle(items):
81+
"""Perform a cryptographically secure shuffling of the given items
82+
83+
Args:
84+
items (obj:list): A list of items to be shuffled
85+
86+
Returns:
87+
Nothing. Items are shuffled in place
88+
"""
89+
random.shuffle(items, secrets.SystemRandom().random)
90+
91+
8092
#########################################
8193
### BASIC CRYPTOGRAPHIC FUNCTIONALITY ###
8294
#########################################
@@ -130,8 +142,7 @@ def generate_ephids_for_day(current_day_key, shuffle=True):
130142

131143
# Shuffle the resulting ephids
132144
if shuffle:
133-
# WARNING: replace with a secure shuffle
134-
random.shuffle(ephids)
145+
secure_shuffle(ephids)
135146

136147
return ephids
137148

@@ -310,7 +321,7 @@ def add_observation(self, ephid, time):
310321
self.observations[batch_start].append(ephid)
311322

312323
# Shuffle observations to hide receive order
313-
random.shuffle(self.observations[batch_start])
324+
secure_shuffle(self.observations[batch_start])
314325

315326
def get_tracing_information(
316327
self,
@@ -451,4 +462,4 @@ def housekeeping_after_batch(self, batch):
451462
self.observations[day_time].extend(observations)
452463

453464
# Reshuffle to make sure we do not store ordering data
454-
random.shuffle(self.observations[day_time])
465+
secure_shuffle(self.observations[day_time])

examples/run_lowcost.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def main():
125125
print(" * CORRECT: Alice's phone concludes she is at risk")
126126
else:
127127
print(" * ERROR: Alice's phone does not conclude she is at risk")
128+
raise RuntimeError("Example code failed!")
128129

129130
print("\n[Alice] Runs housekeeping to update her observation store")
130131
alice.housekeeping_after_batch(batch)

examples/run_unlinkable.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def main():
127127
print(" * CORRECT: Alice's phone concludes she is at risk")
128128
else:
129129
print(" * ERROR: Alice's phone does not conclude she is at risk")
130+
raise RuntimeError("Example code failed!")
130131

131132

132133
if __name__ == "__main__":

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"License :: OSI Approved :: Apache Software License"
1919
"Operating System :: OS Independent",
2020
],
21-
install_requires=["pycryptodomex", "scalable-cuckoo-filter"],
22-
test_requires=["pytest"],
2321
python_requires=">=3.6",
22+
install_requires=["pycryptodomex", "scalable-cuckoo-filter"],
23+
extras_require={"dev": ["black", "flake8", "pre-commit"], "test": ["pytest"]},
2424
)

tests/test_examples.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
__copyright__ = """
2+
Copyright 2020 EPFL
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
__license__ = "Apache 2.0"
17+
18+
from pathlib import Path
19+
import pytest
20+
import subprocess
21+
22+
example_scripts = (Path(__file__).parent.parent / "examples").glob("*.py")
23+
24+
25+
@pytest.mark.parametrize("script", example_scripts)
26+
def test_example_script(script):
27+
subprocess.run([script], check=True)

utils/testvectors_lowcost.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
"""
2020
__license__ = "Apache 2.0"
2121

22-
from datetime import datetime, timezone
23-
2422
from dp3t.protocols.lowcost import next_day_key, generate_ephids_for_day
2523

2624
KEY0 = bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000")

utils/testvectors_unlinkable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""
2020
__license__ = "Apache 2.0"
2121

22-
from binascii import hexlify, unhexlify
22+
from binascii import unhexlify
2323
from datetime import datetime, timezone
2424

2525
from dp3t.protocols.unlinkable import (

0 commit comments

Comments
 (0)