1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-12-05 00:55:43 +01:00

bloom eval

This commit is contained in:
Patrick Rathje 2022-05-31 04:20:40 +02:00
parent d1d5a97659
commit 643d15969a
4 changed files with 205 additions and 50 deletions

25
include/bloom.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef BLOOM_H
#define BLOOM_H
#include "record_storage.h"
#include "exposure-notification.h"
typedef struct {
uint8_t* data;
size_t size;
} bloom_filter_t;
/**
* Initialize the bloom filter on basis of the already registerred records.
*/
bloom_filter_t* bloom_init(size_t size);
void bloom_destroy(bloom_filter_t* bloom);
// TODO lome: maybe only use RPI (should be sufficient)
void bloom_add_record(bloom_filter_t* bloom, ENIntervalIdentifier* rpi);
// TODO lome: maybe only use RPI (should be sufficient)
bool bloom_probably_has_record(bloom_filter_t* bloom, ENIntervalIdentifier* rpi);
#endif

View File

@ -31,6 +31,9 @@ def load_plot_defaults():
#mpl.style.use('tableau-colorblind10') #mpl.style.use('tableau-colorblind10')
idle_avg = 0.00252
idle_max = 0.02325
IDLE_LABEL = 'idle' IDLE_LABEL = 'idle'
consumptions = {} consumptions = {}
durations = {} durations = {}
@ -99,39 +102,53 @@ add_consumption(IDLE_LABEL, idle_consumption, 1.0, 1.0)
# ADVERTISING # ADVERTISING
adv_interval = 0.250 adv_interval = 0.250
per_adv_consumption = { adv_consumptions = [
0: 2.23, 2.45,
-4: 1.49 , 2.47,
-8: 1.43, 2.41,
-16: 1.38, 2.35,
-20: 1.3, 2.47,
-40: 1.22, 2.39,
} 2.45,
2.47,
2.49,
2.45
]
adv_max_consumption = [
7.66,
8.31,
6.96,
7.3,
7.77,
7.6,
8.55,
6.94,
7.14,
6.85
]
# TODO: Add error bars if possible! # TODO: Add error bars if possible!
# measured_duration # measured_duration
# duration # duration
# repetitions # repetitions
adv_consumption = sum(list(per_adv_consumption.values())) / len(per_adv_consumption.values()) adv_consumption = sum(list(per_adv_consumption.values())) / len(per_adv_consumption.values())
add_consumption('adv', adv_consumption, 0.01, (24*3600)/0.25) add_consumption('adv', adv_consumption, 0.004, (24*3600)/0.25)
# SCANNING # SCANNING
scan_consumption = 5.440 scan_consumption = 6.01
add_consumption('scan', scan_consumption, 1.0, 24*60) scan_consumption_max = 8.71
add_consumption('scan', scan_consumption, 2.015, 24*60)
crypt_duration = 0.01 crypt_duration = 0.01
generate_tek_consumption = 2 crypt_consumption_avg = 3.2
derive_tek_consumption = 2 crypt_consumption_max = 5.96
derive_rpi_consumption = 2
encrypt_aem_consumption = 2
add_consumption('generate_tek', generate_tek_consumption, crypt_duration, 1) add_consumption('daily_crypto', crypt_consumption_avg, 0.22, 10)
add_consumption('derive_tek', derive_tek_consumption, crypt_duration, 1)
add_consumption('derive_rpi', derive_rpi_consumption, crypt_duration, 144)
add_consumption('encrypt_aem', encrypt_aem_consumption, crypt_duration, 144*len(per_adv_consumption.values()))
# A table for the timings of the cryptographic fundamentals # A table for the timings of the cryptographic fundamentals
# One detailed graph as a comparison of the advertisements # One detailed graph as a comparison of the advertisements

48
src/bloom.c Normal file
View File

@ -0,0 +1,48 @@
#include "bloom.h"
// 64 KByte!
static uint8_t bloom_data[1<<16];
bloom_filter_t* bloom_init(size_t size) {
bloom_filter_t* bloom = k_calloc(1, sizeof(bloom_filter_t));
if (!bloom) {
return NULL;
}
bloom->size = sizeof(bloom_data);
bloom->data = bloom_data;
if (!bloom->data) {
bloom->size = 0;
k_free(bloom);
return NULL;
}
return bloom;
}
void bloom_destroy(bloom_filter_t* bloom) {
if (bloom) {
k_free(bloom);
}
}
void bloom_add_record(bloom_filter_t* bloom, ENIntervalIdentifier* rpi) {
uint8_t* data = bloom->data;
for (int i = 0; i < sizeof(*rpi); i += 4) {
uint32_t hash = (rpi->b[i+3] << 24) | (rpi->b[i+2] << 16) | (rpi->b[i+1] << 8) | rpi->b[i];
hash %= bloom->size * 8;
data[hash / 8] |= 1 << (hash % 8);
}
}
bool bloom_probably_has_record(bloom_filter_t* bloom, ENIntervalIdentifier* rpi) {
uint8_t* data = bloom->data;
for (int i = 0; i < sizeof(*rpi); i += 4) {
uint32_t hash = (rpi->b[i+3] << 24) | (rpi->b[i+2] << 16) | (rpi->b[i+1] << 8) | rpi->b[i];
hash %= bloom->size * 8;
if (!(data[hash / 8] & (1 << (hash % 8)))) {
return false;
}
}
return true;
}

View File

@ -16,6 +16,27 @@
#include "tek_storage.h" #include "tek_storage.h"
#include "sync_service.h" #include "sync_service.h"
#include "tracing.h" #include "tracing.h"
#include "bloom.h"
/**
* Fill the bloom filter with all stored records.
*/
void fill_bloom_with_stored_records(bloom_filter_t* bloom) {
// init iterator for filling bloom filter
record_iterator_t iterator;
int rc = ens_records_iterator_init_timerange(&iterator, NULL, NULL);
if (rc) {
printk("init iterator failed0 (err %d)\n", rc);
return;
}
// fill bloom filter with records
record_t* current;
while ((current = ens_records_iterator_next(&iterator))) {
bloom_add_record(bloom, &(current->rolling_proximity_identifier));
}
}
void main(void) { void main(void) {
@ -29,50 +50,94 @@ void main(void) {
return; return;
} }
err = record_storage_init(false); err = record_storage_init(true);
if (err) { if (err) {
printk("init record storage failed (err %d)\n", err); printk("init record storage failed (err %d)\n", err);
return; return;
} }
err = tek_storage_init(false); err = tek_storage_init(false);
if (err) { if (err) {
printk("init key storage failed (err %d)\n", err); printk("init key storage failed (err %d)\n", err);
return; return;
} }
/* Initialize the Bluetooth Subsystem */ k_msleep(10000);
err = bt_enable(NULL);
if (err) { printk("Filling record storage...\n");
printk("Bluetooth init failed (err %d)\n", err); reset_record_storage();
return;
tek_t tek;
ENPeriodIdentifierKey pik;
tek_storage_get_latest_at_ts(&tek, 0);
en_derive_period_identifier_key(&pik, &tek.tek);
bloom_filter_t* bf = bloom_init(0); // size not used atm
/// we now fill the whole flash memory with stupid data
for (int i = 0; i < CONFIG_ENS_MAX_CONTACTS; ++i) {
record_t dummy_record;
en_derive_interval_identifier((ENIntervalIdentifier*)&dummy_record.rolling_proximity_identifier, &pik, i);
dummy_record.timestamp = i; // very clever! ;)
bloom_add_record(bf, &dummy_record.rolling_proximity_identifier);
if (!bloom_probably_has_record(bf, &dummy_record.rolling_proximity_identifier)) {
printk("error! bloom_probably_has_record\n");
}
} }
printk("Done...\n");
k_msleep(2000);
/* Initialize the Tracing Subsystem */ printk("Creating bloom filter...\n");
err = tracing_init(); fill_bloom_with_stored_records(bf);
if (err) { printk("Done...\n");
printk("Tracing init failed (err %d)\n", err); k_msleep(3000);
return;
}
/* Initialize the Gatt Subsystem */
err = sync_service_init();
if (err) {
printk("Sync Service init failed (err %d)\n", err);
return;
}
printk("Components initialized! Starting Tracing and Gatt...\n");
// We sleep for one second just
k_sleep(K_MSEC(1000));
do { do {
uint32_t tracing_sleep_ms = tracing_run(); printk("Checking keys...\n");
uint32_t sync_sleep_ms = sync_service_run(); for(int k = 0; k < 144; k++) {
uint32_t sleep_ms = MIN(tracing_sleep_ms, sync_sleep_ms); ENIntervalIdentifier rpi;
//printk("Sleeping a bit (%u ms)...\n", sleep_ms); en_derive_period_identifier_key(&pik, &tek.tek);
k_sleep(K_MSEC(sleep_ms)); // TODO: what to put here? // derive all RPIs
for (int j = 0; j < 144; j++) {
// we assume that at least one out of 144 RPIs was met (this is still a lot actually!)
uint32_t interval = j == 0 ? (CONFIG_ENS_MAX_CONTACTS / 144)*k : (CONFIG_ENS_MAX_CONTACTS+1);
en_derive_interval_identifier(&rpi, &pik, interval); //one of each is actually
uint32_t num_met = 0;
if (bloom_probably_has_record(bf, &rpi)) {
num_met++;
/*
record_iterator_t iterator;
time_t start = interval == 0 ? 0 : (interval+(CONFIG_ENS_MAX_CONTACTS-1)) % CONFIG_ENS_MAX_CONTACTS;
time_t end = (interval+(CONFIG_ENS_MAX_CONTACTS+1)) % CONFIG_ENS_MAX_CONTACTS;
int rc = ens_records_iterator_init_timerange(&iterator, &start, &end);
if (rc) {
// on error, skip this rpi
printk("iterator error! %d\n", rc);
continue;
}
record_t* current;
while ((current = ens_records_iterator_next(&iterator))) {
if (memcmp(&(current->rolling_proximity_identifier), rpi.b,
sizeof(current->rolling_proximity_identifier)) == 0) {
num_met++;
}
}
ens_record_iterator_clear(&iterator);*/
}
if(j == 0 && num_met == 0) {
printk("Met mismatch! %u\n", num_met);
}
}
}
k_sleep(K_MSEC(2000));
} while (1); } while (1);
} }