Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401
exclude = .??*, venv
ignore = E203, E266, E501
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
select = B,C,E,F,W,T4,B9
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ Bluetooth devices or backend services.

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

```
```bash
python3 -m venv venv
source venv/bin/activate
pip3 install -e "."
pip3 install -e .
```

## Running the example

After installing the project you can run the examples:

```
```bash
examples/run_lowcost.py
examples/run_unlinkable.py
```
Expand All @@ -90,7 +90,7 @@ examples/run_unlinkable.py

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

```
```bash
utils/testvectors_lowcost.py
utils/testvectors_unlinkable.py
```
Expand All @@ -105,7 +105,7 @@ venv/bin/ativate`) to ensure that the paths are picked up correctly.

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

```
```bash
pip install pre-commit
pre-commit install
```
Expand Down
19 changes: 15 additions & 4 deletions dp3t/protocols/lowcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def batch_start_from_time(time):
return (int(time.timestamp()) // SECONDS_PER_BATCH) * SECONDS_PER_BATCH


def secure_shuffle(items):
"""Perform a cryptographically secure shuffling of the given items

Args:
items (obj:list): A list of items to be shuffled

Returns:
Nothing. Items are shuffled in place
"""
random.shuffle(items, secrets.SystemRandom().random)


#########################################
### BASIC CRYPTOGRAPHIC FUNCTIONALITY ###
#########################################
Expand Down Expand Up @@ -130,8 +142,7 @@ def generate_ephids_for_day(current_day_key, shuffle=True):

# Shuffle the resulting ephids
if shuffle:
# WARNING: replace with a secure shuffle
random.shuffle(ephids)
secure_shuffle(ephids)

return ephids

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

# Shuffle observations to hide receive order
random.shuffle(self.observations[batch_start])
secure_shuffle(self.observations[batch_start])

def get_tracing_information(
self,
Expand Down Expand Up @@ -451,4 +462,4 @@ def housekeeping_after_batch(self, batch):
self.observations[day_time].extend(observations)

# Reshuffle to make sure we do not store ordering data
random.shuffle(self.observations[day_time])
secure_shuffle(self.observations[day_time])
1 change: 1 addition & 0 deletions examples/run_lowcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def main():
print(" * CORRECT: Alice's phone concludes she is at risk")
else:
print(" * ERROR: Alice's phone does not conclude she is at risk")
raise RuntimeError("Example code failed!")

print("\n[Alice] Runs housekeeping to update her observation store")
alice.housekeeping_after_batch(batch)
Expand Down
1 change: 1 addition & 0 deletions examples/run_unlinkable.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def main():
print(" * CORRECT: Alice's phone concludes she is at risk")
else:
print(" * ERROR: Alice's phone does not conclude she is at risk")
raise RuntimeError("Example code failed!")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"License :: OSI Approved :: Apache Software License"
"Operating System :: OS Independent",
],
install_requires=["pycryptodomex", "scalable-cuckoo-filter"],
test_requires=["pytest"],
python_requires=">=3.6",
install_requires=["pycryptodomex", "scalable-cuckoo-filter"],
extras_require={"dev": ["black", "flake8", "pre-commit"], "test": ["pytest"]},
)
27 changes: 27 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
__copyright__ = """
Copyright 2020 EPFL

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
__license__ = "Apache 2.0"

from pathlib import Path
Comment thread
jherland marked this conversation as resolved.
import pytest
import subprocess

example_scripts = (Path(__file__).parent.parent / "examples").glob("*.py")


@pytest.mark.parametrize("script", example_scripts)
def test_example_script(script):
subprocess.run([script], check=True)
2 changes: 0 additions & 2 deletions utils/testvectors_lowcost.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"""
__license__ = "Apache 2.0"

from datetime import datetime, timezone

from dp3t.protocols.lowcost import next_day_key, generate_ephids_for_day

KEY0 = bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000000")
Expand Down
2 changes: 1 addition & 1 deletion utils/testvectors_unlinkable.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""
__license__ = "Apache 2.0"

from binascii import hexlify, unhexlify
from binascii import unhexlify
from datetime import datetime, timezone

from dp3t.protocols.unlinkable import (
Expand Down