From 548e8abffe5a9028ee1327235e4b9959787210c3 Mon Sep 17 00:00:00 2001 From: Lucca Hirschi Date: Wed, 15 Apr 2020 16:11:55 +0200 Subject: [PATCH] Fix #12 and modify mock example to reflect the new at risk test --- LowCostDP3T.py | 26 +++++++++++++++----------- example_run.py | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/LowCostDP3T.py b/LowCostDP3T.py index 6ebda8c..b97e6c6 100644 --- a/LowCostDP3T.py +++ b/LowCostDP3T.py @@ -224,27 +224,31 @@ def process_epoch(self): # Update infected and local risk scoring ######################################## - def check_infected(self, inf_SK0, date, now=None): + def check_infected(self, inf_SK0, dateInfectious, datePublished): ''' Checks if our database was exposed to an infected SK starting on date. NOTE: this implementation uses the date of the SK_t to reduce the - number of comparisons. The backend needs to store tuples. + number of comparisons. The backend needs to store tuples. Check if we recorded a contact with a given SK0 across in our database of contact records. This implementation assumes we are - given a date of infection and checks on a per-day basis. + given a date of infection and a date of publication and checks + on a per-day basis. Arguments infSK0(b[]): SK_t of infected - date(str): date of SK_t (i.e., the t in the form 2020-04-23). - now(datetime): current date for mock testing. + dateInfectious(str): date of SK_t (i.e., the t in the + form 2020-04-23). + datePublish(datetime): date of publication of SK_t (same + format). ''' - if now is None: - now = datetime.now(timezone.utc) - infect_date = datetime.strptime(date, "%Y-%m-%d") - days_infected = (now-infect_date).days + infectious_date = datetime.strptime(dateInfectious, "%Y-%m-%d") + published_date = datetime.strptime(datePublished, "%Y-%m-%d") + days_infected = (published_date-infectious_date).days inf_SK = inf_SK0 - for day in range(days_infected, -1, -1): + # We do not compute the SK key of the day of the publication + for day in range(days_infected, 0, -1): # Create infected EphIDs and rotate infected SK infected_ephIDs = KeyStore.create_ephIDs(inf_SK) inf_SK = KeyStore.get_SKt1(inf_SK) @@ -259,7 +263,7 @@ def check_infected(self, inf_SK0, date, now=None): if inf_ephID in self.contacts[day]: duration = self.contacts[day][inf_ephID] print( - "At risk, observed {} on day -{} for {}".format(inf_ephID.hex(), day, duration)) + "At risk, observed {} on day ({})-{} day(s) for {} seconds".format(inf_ephID.hex(), datePublished, day, duration)) # Mock Application that ties contact manager and keystore together diff --git a/example_run.py b/example_run.py index 9e51d3c..258452e 100644 --- a/example_run.py +++ b/example_run.py @@ -64,6 +64,10 @@ bob.next_day() isidor.next_day() + print("Isidor is now infectious.") + infectious_date = datetime.utcfromtimestamp(epotime) + infections_SK = isidor.keystore.SKt[0] + print("Day: Bob and Isidor meet for dinner.") for hour in range(17, 20): for epoch in range(60//LowCostDP3T.EPOCH_LENGTH): @@ -81,16 +85,18 @@ bob.next_epoch() isidor.next_epoch() - print("Isidor is tested positive.") - infectious_date = datetime.utcfromtimestamp(epotime) - infections_SK = isidor.keystore.SKt[0] - # Tik Tok epotime += 24*60*60 alice.next_day() bob.next_day() isidor.next_day() + print("Isidor is tested positive and publish infectious_SK (from yesterday).") + published_date = datetime.utcfromtimestamp(epotime) + + # Tik Tok + epotime += 2*60*60 + # Alice receives a replay and should not be positive because of that now = datetime.utcfromtimestamp(epotime) bob_ephID = bob.keystore.get_current_ephID(now) @@ -106,7 +112,7 @@ print("Check exposure of Alice and Bob.") print("Alice: (not positive)") alice.ctmgr.check_infected(infections_SK, infectious_date.strftime( - "%Y-%m-%d"), datetime.utcfromtimestamp(epotime)) + "%Y-%m-%d"), published_date.strftime("%Y-%m-%d")) print("Bob: (at risk)") bob.ctmgr.check_infected(infections_SK, infectious_date.strftime( - "%Y-%m-%d"), datetime.utcfromtimestamp(epotime)) + "%Y-%m-%d"), published_date.strftime("%Y-%m-%d"))