From 22697403f7212a9c0926f15347c87df2fc35dc50 Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Fri, 10 Dec 2021 13:51:56 +0100 Subject: [PATCH] Finalize refactorings of contacts and bloom module --- src/bloom.h | 2 +- src/contacts.c | 85 +++++++++++++++++++---------------------------- src/contacts.h | 41 +++++++++++++---------- src/covid_types.h | 21 ++++++------ src/ens/storage.c | 2 +- src/ens/storage.h | 2 +- zephyr/Kconfig | 15 +++++++-- 7 files changed, 84 insertions(+), 84 deletions(-) diff --git a/src/bloom.h b/src/bloom.h index 8d259e1..e7c3d7d 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -4,7 +4,7 @@ #include "ens/storage.h" #include "exposure-notification.h" -typedef struct bloom_filter { +typedef struct { uint8_t* data; size_t size; } bloom_filter_t; diff --git a/src/contacts.c b/src/contacts.c index 72ccdbc..666adae 100644 --- a/src/contacts.c +++ b/src/contacts.c @@ -37,7 +37,7 @@ void print_key(_ENBaseKey* key) { void print_rpi(rolling_proximity_identifier_t* rpi) { for (int i = 0; i < sizeof(rolling_proximity_identifier_t); i++) { - printk("%02x", rpi->data[i]); + printk("%02x", rpi->b[i]); } } @@ -48,12 +48,7 @@ void print_aem(associated_encrypted_metadata_t* aem) { } int register_record(record_t* record) { - int rc = add_record(record); - if (rc) { - return rc; - } - // rc = bloom_add_record(record); - return rc; + return add_record(record); } /** @@ -68,7 +63,7 @@ int get_number_of_infected_for_multiple_intervals_simple(infected_for_interval_i continue; } record_t* current; - while (current = ens_records_iterator_next(&iterator)) { + while ((current = ens_records_iterator_next(&iterator))) { if (memcmp(&(current->rolling_proximity_identifier), &ctx[i].interval_identifier, sizeof(rolling_proximity_identifier_t)) == 0) { ctx[i].infected++; @@ -92,7 +87,7 @@ void fill_bloom_with_stored_records(bloom_filter_t* bloom) { // fill bloom filter with records record_t* current; - while (current = ens_records_iterator_next(&iterator)) { + while ((current = ens_records_iterator_next(&iterator))) { bloom_add_record(bloom, &(current->rolling_proximity_identifier)); } } @@ -100,7 +95,7 @@ void fill_bloom_with_stored_records(bloom_filter_t* bloom) { /** * Fill the bloom filter with flash records and test passed RPIs against it. */ -int64_t test_bloom_performance(infected_for_interval_ident_ctx_t* ctx, int count) { +int64_t bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) { bloom_filter_t* bloom = bloom_init(get_num_records() * 2); if (!bloom) { printk("bloom init failed\n"); @@ -211,7 +206,7 @@ int reverse_bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) { } record_t* current; - while (current = ens_records_iterator_next(&iterator)) { + while ((current = ens_records_iterator_next(&iterator))) { if (bloom_probably_has_record(bloom, &(current->rolling_proximity_identifier))) { for (int i = 0; i < count; i++) { if (memcmp(&(current->rolling_proximity_identifier), &ctx[i].interval_identifier, @@ -220,10 +215,6 @@ int reverse_bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) { amount++; break; } - // TODO lome: maybe dont require orderred array, so this is not useful - if (current->timestamp > ctx[i].search_end) { - break; - } } } } @@ -266,52 +257,46 @@ void setup_test_data() { } } } - -#define INFECTED_INTERVALS_COUNT 2000 - // setup our ordered array with infected RPIs - static infected_for_interval_ident_ctx_t infectedIntervals[INFECTED_INTERVALS_COUNT]; - - printk("Starting measurements with %d RPIs to seach and an infection rate of %d\n", INFECTED_INTERVALS_COUNT, - CONFIG_TEST_INFECTED_RATE); - - // measure_perf(get_number_of_infected_for_multiple_intervals_dumb, "dumb", infectedIntervals, - // INFECTED_INTERVALS_COUNT); - // measure_perf(get_number_of_infected_for_multiple_intervals_simple, "simple", infectedIntervals, - // INFECTED_INTERVALS_COUNT); - // measure_perf(get_number_of_infected_for_multiple_intervals_optimized, "optimized", infectedIntervals, - // INFECTED_INTERVALS_COUNT); - - // measure_perf(test_bloom_performance, "bloom", infectedIntervals, INFECTED_INTERVALS_COUNT); - - measure_perf(reverse_bloom_filter, "bloom reverse", infectedIntervals, INFECTED_INTERVALS_COUNT); } int init_contacts() { #if CONFIG_CONTACTS_PERFORM_RISC_CHECK_TEST + reset_record_storage(); setup_test_data(); + + // setup our ordered array with infected RPIs + static infected_for_interval_ident_ctx_t infectedIntervals[CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT]; + + printk("Starting measurements with %d RPIs to seach and an infection rate of %d\n", + CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT, CONFIG_TEST_INFECTED_RATE); + + measure_perf(check_possible_contacts_for_intervals, "bloom reverse", infectedIntervals, + CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT); #endif return 0; } -/** - * Check for a list of specified interval identifiers, whether they were probably met or not. - * @param ctx list of interval identifiers to check - * @param count amount of identifiers to check - * @return the amount of met intervals, -ERRNO on error - */ int check_possible_contacts_for_intervals(infected_for_interval_ident_ctx_t* ctx, int count) { +#if CONFIG_CONTACTS_BLOOM_REVERSE return reverse_bloom_filter(ctx, count); +#else + return bloom_filter(ctx, count); +#endif } -typedef struct { - ENPeriodKey periodKey; - time_t start; - time_t end; - int met; -} period_key_information_t; - -// TODO lome: needed? -int check_possible_contacts_for_periods(period_key_information_t* periodKey, int count) { - infected_for_interval_ident_ctx_t intervalIdents[count * 144]; - return check_possible_contacts_for_intervals(intervalIdents, 0); +int check_possible_contacts_for_periods(period_key_information_t periodKeyInformation[], int count) { + for (int i = 0; i < count; i++) { + static infected_for_interval_ident_ctx_t intervalIdents[EN_TEK_ROLLING_PERIOD]; + int periodStart = en_get_interval_number(periodKeyInformation[i].start); + for (int interval = 0; interval < EN_TEK_ROLLING_PERIOD; interval++) { + en_derive_interval_identifier(&intervalIdents[interval].interval_identifier, + &periodKeyInformation[i].periodKey, periodStart + interval); + } + int rc = check_possible_contacts_for_intervals(intervalIdents, 0); + if (rc < 0) { + return rc; + } + periodKeyInformation[i].met = rc; + } + return 0; } diff --git a/src/contacts.h b/src/contacts.h index e9e99a9..d42965a 100644 --- a/src/contacts.h +++ b/src/contacts.h @@ -22,6 +22,12 @@ typedef struct { time_t search_end; } __packed infected_for_interval_ident_ctx_t; +typedef struct { + ENPeriodKey periodKey; + time_t start; + int met; +} __packed period_key_information_t; + typedef int (*test_func_t)(infected_for_interval_ident_ctx_t* infectedIntervals, int count); void print_key(_ENBaseKey* key); @@ -37,25 +43,24 @@ void print_aem(associated_encrypted_metadata_t* aem); int register_record(record_t* record); /** - * Get the number of infected records for a given PeriodKey. - * - * @param key the PeriodKey - * @param timestamp the timestamp of the period - * - * @returns the number of infected records or -ERRNO in case of an error + * Initialize the contacts module. */ -int get_number_of_infected_for_period(ENPeriodKey* key, time_t timestamp); - -/** - * Get the number of infected records for multiple IntervalIdentifier. - * - * @param ctx array of period keys and timestamps with field "infected" for the number of infected records - * @param count the number of periods in the array - * - * @returns 0 in case of success, -ERRNO in case of an error - */ -// int get_number_of_infected_for_multiple_intervals(infected_for_period_key_ctx_t* ctx, int count); - int init_contacts(); +/** + * Check for a list of specified interval identifiers, whether they were probably met or not. + * @param ctx list of interval identifiers to check + * @param count amount of identifiers to check + * @return the amount of met intervals, -ERRNO on error + */ +int check_possible_contacts_for_intervals(infected_for_interval_ident_ctx_t* ctx, int count); + +/** + * Check for a list of specified period keys, whether they were probably met or not. + * @param ctx list of period keys and their meta information to check + * @param count amount of period keys to check + * @return -ERRNO on error, 0 otherwise + */ +int check_possible_contacts_for_periods(period_key_information_t periodKeyInformation[], int count); + #endif diff --git a/src/covid_types.h b/src/covid_types.h index c2f5ebb..f643ff3 100644 --- a/src/covid_types.h +++ b/src/covid_types.h @@ -7,26 +7,25 @@ #ifndef COVID_TYPES_H #define COVID_TYPES_H +#include #include #include #define COVID_ROLLING_PROXIMITY_IDENTIFIER_LEN 16 -typedef struct rolling_proximity_identifier { - uint8_t data[COVID_ROLLING_PROXIMITY_IDENTIFIER_LEN]; -} __packed rolling_proximity_identifier_t; +typedef ENIntervalIdentifier rolling_proximity_identifier_t; typedef struct bt_metadata { - uint8_t version; - uint8_t tx_power; - uint8_t rsv1; - uint8_t rsv2; -} __packed bt_metadata_t; + uint8_t version; + uint8_t tx_power; + uint8_t rsv1; + uint8_t rsv2; +} __packed bt_metadata_t; -//typedef struct bt_metadata bt_metadata_t; +// typedef struct bt_metadata bt_metadata_t; -typedef struct associated_encrypted_metadata { - uint8_t data[sizeof(bt_metadata_t)]; +typedef struct associated_encrypted_metadata { + uint8_t data[sizeof(bt_metadata_t)]; } __packed associated_encrypted_metadata_t; typedef uint8_t rssi_t; diff --git a/src/ens/storage.c b/src/ens/storage.c index 0c61d35..651b1b6 100644 --- a/src/ens/storage.c +++ b/src/ens/storage.c @@ -108,7 +108,7 @@ int init_record_storage(bool clean) { return rc; } -int reset_record_storage() { +void reset_record_storage() { k_mutex_lock(&info_fs_lock, K_FOREVER); record_information.count = 0; record_information.oldest_contact = 0; diff --git a/src/ens/storage.h b/src/ens/storage.h index 8f6741b..4b059b8 100644 --- a/src/ens/storage.h +++ b/src/ens/storage.h @@ -32,7 +32,7 @@ int init_record_storage(bool clean); /** * Reset state of record storage. */ -int reset_record_storage(); +void reset_record_storage(); /** * Loads the record with number sn into the destination struct diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 307de82..29e3683 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -41,6 +41,17 @@ config CONTACTS_PERFORM_RISC_CHECK_TEST bool "" default n help - Flag for performing tests. - + Flag for performing tests. WARNING: This deletes all currently stored records. + +config CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT + int "" + default 1024 + help + Amount of interval identifiers to test against stored test data. + +config CONFIG_CONTACTS_BLOOM_REVERSE + bool "" + default n + help + Flag for toggling between bloom variants. Yes means, that the reverse bloom filter is used. endmenu