aboutsummaryrefslogtreecommitdiff
path: root/src/set/ibf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/set/ibf.c')
-rw-r--r--src/set/ibf.c282
1 files changed, 143 insertions, 139 deletions
diff --git a/src/set/ibf.c b/src/set/ibf.c
index 43d2e7bce..a573ef6b4 100644
--- a/src/set/ibf.c
+++ b/src/set/ibf.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file set/ibf.c 22 * @file set/ibf.c
@@ -30,7 +30,7 @@
30 * Compute the key's hash from the key. 30 * Compute the key's hash from the key.
31 * Redefine to use a different hash function. 31 * Redefine to use a different hash function.
32 */ 32 */
33#define IBF_KEY_HASH_VAL(k) (GNUNET_CRYPTO_crc32_n (&(k), sizeof (struct IBF_KeyHash))) 33#define IBF_KEY_HASH_VAL(k) (GNUNET_CRYPTO_crc32_n(&(k), sizeof(struct IBF_KeyHash)))
34 34
35/** 35/**
36 * Create a key from a hashcode. 36 * Create a key from a hashcode.
@@ -39,9 +39,9 @@
39 * @return a key 39 * @return a key
40 */ 40 */
41struct IBF_Key 41struct IBF_Key
42ibf_key_from_hashcode (const struct GNUNET_HashCode *hash) 42ibf_key_from_hashcode(const struct GNUNET_HashCode *hash)
43{ 43{
44 return *(struct IBF_Key *) hash; 44 return *(struct IBF_Key *)hash;
45} 45}
46 46
47/** 47/**
@@ -52,14 +52,14 @@ ibf_key_from_hashcode (const struct GNUNET_HashCode *hash)
52 * @param dst hashcode to store the result in 52 * @param dst hashcode to store the result in
53 */ 53 */
54void 54void
55ibf_hashcode_from_key (struct IBF_Key key, 55ibf_hashcode_from_key(struct IBF_Key key,
56 struct GNUNET_HashCode *dst) 56 struct GNUNET_HashCode *dst)
57{ 57{
58 struct IBF_Key *p; 58 struct IBF_Key *p;
59 unsigned int i; 59 unsigned int i;
60 const unsigned int keys_per_hashcode = sizeof (struct GNUNET_HashCode) / sizeof (struct IBF_Key); 60 const unsigned int keys_per_hashcode = sizeof(struct GNUNET_HashCode) / sizeof(struct IBF_Key);
61 61
62 p = (struct IBF_Key *) dst; 62 p = (struct IBF_Key *)dst;
63 for (i = 0; i < keys_per_hashcode; i++) 63 for (i = 0; i < keys_per_hashcode; i++)
64 *p++ = key; 64 *p++ = key;
65} 65}
@@ -73,34 +73,34 @@ ibf_hashcode_from_key (struct IBF_Key key,
73 * @return the newly created invertible bloom filter, NULL on error 73 * @return the newly created invertible bloom filter, NULL on error
74 */ 74 */
75struct InvertibleBloomFilter * 75struct InvertibleBloomFilter *
76ibf_create (uint32_t size, uint8_t hash_num) 76ibf_create(uint32_t size, uint8_t hash_num)
77{ 77{
78 struct InvertibleBloomFilter *ibf; 78 struct InvertibleBloomFilter *ibf;
79 79
80 GNUNET_assert (0 != size); 80 GNUNET_assert(0 != size);
81 81
82 ibf = GNUNET_new (struct InvertibleBloomFilter); 82 ibf = GNUNET_new(struct InvertibleBloomFilter);
83 ibf->count = GNUNET_malloc_large (size * sizeof (uint8_t)); 83 ibf->count = GNUNET_malloc_large(size * sizeof(uint8_t));
84 if (NULL == ibf->count) 84 if (NULL == ibf->count)
85 { 85 {
86 GNUNET_free (ibf); 86 GNUNET_free(ibf);
87 return NULL; 87 return NULL;
88 } 88 }
89 ibf->key_sum = GNUNET_malloc_large (size * sizeof (struct IBF_Key)); 89 ibf->key_sum = GNUNET_malloc_large(size * sizeof(struct IBF_Key));
90 if (NULL == ibf->key_sum) 90 if (NULL == ibf->key_sum)
91 { 91 {
92 GNUNET_free (ibf->count); 92 GNUNET_free(ibf->count);
93 GNUNET_free (ibf); 93 GNUNET_free(ibf);
94 return NULL; 94 return NULL;
95 } 95 }
96 ibf->key_hash_sum = GNUNET_malloc_large (size * sizeof (struct IBF_KeyHash)); 96 ibf->key_hash_sum = GNUNET_malloc_large(size * sizeof(struct IBF_KeyHash));
97 if (NULL == ibf->key_hash_sum) 97 if (NULL == ibf->key_hash_sum)
98 { 98 {
99 GNUNET_free (ibf->key_sum); 99 GNUNET_free(ibf->key_sum);
100 GNUNET_free (ibf->count); 100 GNUNET_free(ibf->count);
101 GNUNET_free (ibf); 101 GNUNET_free(ibf);
102 return NULL; 102 return NULL;
103 } 103 }
104 ibf->size = size; 104 ibf->size = size;
105 ibf->hash_num = hash_num; 105 ibf->hash_num = hash_num;
106 106
@@ -112,45 +112,45 @@ ibf_create (uint32_t size, uint8_t hash_num)
112 * Store unique bucket indices for the specified key in dst. 112 * Store unique bucket indices for the specified key in dst.
113 */ 113 */
114static void 114static void
115ibf_get_indices (const struct InvertibleBloomFilter *ibf, 115ibf_get_indices(const struct InvertibleBloomFilter *ibf,
116 struct IBF_Key key, 116 struct IBF_Key key,
117 int *dst) 117 int *dst)
118{ 118{
119 uint32_t filled; 119 uint32_t filled;
120 uint32_t i; 120 uint32_t i;
121 uint32_t bucket; 121 uint32_t bucket;
122 122
123 bucket = GNUNET_CRYPTO_crc32_n (&key, sizeof key); 123 bucket = GNUNET_CRYPTO_crc32_n(&key, sizeof key);
124 for (i = 0, filled=0; filled < ibf->hash_num; i++) 124 for (i = 0, filled = 0; filled < ibf->hash_num; i++)
125 { 125 {
126 unsigned int j; 126 unsigned int j;
127 uint64_t x; 127 uint64_t x;
128 for (j = 0; j < filled; j++) 128 for (j = 0; j < filled; j++)
129 if (dst[j] == bucket) 129 if (dst[j] == bucket)
130 goto try_next; 130 goto try_next;
131 dst[filled++] = bucket % ibf->size; 131 dst[filled++] = bucket % ibf->size;
132 try_next: ; 132try_next:;
133 x = ((uint64_t) bucket << 32) | i; 133 x = ((uint64_t)bucket << 32) | i;
134 bucket = GNUNET_CRYPTO_crc32_n (&x, sizeof x); 134 bucket = GNUNET_CRYPTO_crc32_n(&x, sizeof x);
135 } 135 }
136} 136}
137 137
138 138
139static void 139static void
140ibf_insert_into (struct InvertibleBloomFilter *ibf, 140ibf_insert_into(struct InvertibleBloomFilter *ibf,
141 struct IBF_Key key, 141 struct IBF_Key key,
142 const int *buckets, int side) 142 const int *buckets, int side)
143{ 143{
144 int i; 144 int i;
145 145
146 for (i = 0; i < ibf->hash_num; i++) 146 for (i = 0; i < ibf->hash_num; i++)
147 { 147 {
148 const int bucket = buckets[i]; 148 const int bucket = buckets[i];
149 ibf->count[bucket].count_val += side; 149 ibf->count[bucket].count_val += side;
150 ibf->key_sum[bucket].key_val ^= key.key_val; 150 ibf->key_sum[bucket].key_val ^= key.key_val;
151 ibf->key_hash_sum[bucket].key_hash_val 151 ibf->key_hash_sum[bucket].key_hash_val
152 ^= IBF_KEY_HASH_VAL (key); 152 ^= IBF_KEY_HASH_VAL(key);
153 } 153 }
154} 154}
155 155
156 156
@@ -161,12 +161,13 @@ ibf_insert_into (struct InvertibleBloomFilter *ibf,
161 * @param key the element's hash code 161 * @param key the element's hash code
162 */ 162 */
163void 163void
164ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key) 164ibf_insert(struct InvertibleBloomFilter *ibf, struct IBF_Key key)
165{ 165{
166 int buckets[ibf->hash_num]; 166 int buckets[ibf->hash_num];
167 GNUNET_assert (ibf->hash_num <= ibf->size); 167
168 ibf_get_indices (ibf, key, buckets); 168 GNUNET_assert(ibf->hash_num <= ibf->size);
169 ibf_insert_into (ibf, key, buckets, 1); 169 ibf_get_indices(ibf, key, buckets);
170 ibf_insert_into(ibf, key, buckets, 1);
170} 171}
171 172
172 173
@@ -177,12 +178,13 @@ ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key)
177 * @param key the element's hash code 178 * @param key the element's hash code
178 */ 179 */
179void 180void
180ibf_remove (struct InvertibleBloomFilter *ibf, struct IBF_Key key) 181ibf_remove(struct InvertibleBloomFilter *ibf, struct IBF_Key key)
181{ 182{
182 int buckets[ibf->hash_num]; 183 int buckets[ibf->hash_num];
183 GNUNET_assert (ibf->hash_num <= ibf->size); 184
184 ibf_get_indices (ibf, key, buckets); 185 GNUNET_assert(ibf->hash_num <= ibf->size);
185 ibf_insert_into (ibf, key, buckets, -1); 186 ibf_get_indices(ibf, key, buckets);
187 ibf_insert_into(ibf, key, buckets, -1);
186} 188}
187 189
188 190
@@ -190,18 +192,19 @@ ibf_remove (struct InvertibleBloomFilter *ibf, struct IBF_Key key)
190 * Test is the IBF is empty, i.e. all counts, keys and key hashes are zero. 192 * Test is the IBF is empty, i.e. all counts, keys and key hashes are zero.
191 */ 193 */
192static int 194static int
193ibf_is_empty (struct InvertibleBloomFilter *ibf) 195ibf_is_empty(struct InvertibleBloomFilter *ibf)
194{ 196{
195 int i; 197 int i;
198
196 for (i = 0; i < ibf->size; i++) 199 for (i = 0; i < ibf->size; i++)
197 { 200 {
198 if (0 != ibf->count[i].count_val) 201 if (0 != ibf->count[i].count_val)
199 return GNUNET_NO; 202 return GNUNET_NO;
200 if (0 != ibf->key_hash_sum[i].key_hash_val) 203 if (0 != ibf->key_hash_sum[i].key_hash_val)
201 return GNUNET_NO; 204 return GNUNET_NO;
202 if (0 != ibf->key_sum[i].key_val) 205 if (0 != ibf->key_sum[i].key_val)
203 return GNUNET_NO; 206 return GNUNET_NO;
204 } 207 }
205 return GNUNET_YES; 208 return GNUNET_YES;
206} 209}
207 210
@@ -219,53 +222,53 @@ ibf_is_empty (struct InvertibleBloomFilter *ibf)
219 * GNUNET_SYSERR if the decoding has failed 222 * GNUNET_SYSERR if the decoding has failed
220 */ 223 */
221int 224int
222ibf_decode (struct InvertibleBloomFilter *ibf, 225ibf_decode(struct InvertibleBloomFilter *ibf,
223 int *ret_side, struct IBF_Key *ret_id) 226 int *ret_side, struct IBF_Key *ret_id)
224{ 227{
225 struct IBF_KeyHash hash; 228 struct IBF_KeyHash hash;
226 int i; 229 int i;
227 int buckets[ibf->hash_num]; 230 int buckets[ibf->hash_num];
228 231
229 GNUNET_assert (NULL != ibf); 232 GNUNET_assert(NULL != ibf);
230 233
231 for (i = 0; i < ibf->size; i++) 234 for (i = 0; i < ibf->size; i++)
232 { 235 {
233 int j; 236 int j;
234 int hit; 237 int hit;
235 238
236 /* we can only decode from pure buckets */ 239 /* we can only decode from pure buckets */
237 if ((1 != ibf->count[i].count_val) && (-1 != ibf->count[i].count_val)) 240 if ((1 != ibf->count[i].count_val) && (-1 != ibf->count[i].count_val))
238 continue; 241 continue;
239 242
240 hash.key_hash_val = IBF_KEY_HASH_VAL (ibf->key_sum[i]); 243 hash.key_hash_val = IBF_KEY_HASH_VAL(ibf->key_sum[i]);
241 244
242 /* test if the hash matches the key */ 245 /* test if the hash matches the key */
243 if (hash.key_hash_val != ibf->key_hash_sum[i].key_hash_val) 246 if (hash.key_hash_val != ibf->key_hash_sum[i].key_hash_val)
244 continue; 247 continue;
245 248
246 /* test if key in bucket hits its own location, 249 /* test if key in bucket hits its own location,
247 * if not, the key hash was subject to collision */ 250 * if not, the key hash was subject to collision */
248 hit = GNUNET_NO; 251 hit = GNUNET_NO;
249 ibf_get_indices (ibf, ibf->key_sum[i], buckets); 252 ibf_get_indices(ibf, ibf->key_sum[i], buckets);
250 for (j = 0; j < ibf->hash_num; j++) 253 for (j = 0; j < ibf->hash_num; j++)
251 if (buckets[j] == i) 254 if (buckets[j] == i)
252 hit = GNUNET_YES; 255 hit = GNUNET_YES;
253 256
254 if (GNUNET_NO == hit) 257 if (GNUNET_NO == hit)
255 continue; 258 continue;
256 259
257 if (NULL != ret_side) 260 if (NULL != ret_side)
258 *ret_side = ibf->count[i].count_val; 261 *ret_side = ibf->count[i].count_val;
259 if (NULL != ret_id) 262 if (NULL != ret_id)
260 *ret_id = ibf->key_sum[i]; 263 *ret_id = ibf->key_sum[i];
261 264
262 /* insert on the opposite side, effectively removing the element */ 265 /* insert on the opposite side, effectively removing the element */
263 ibf_insert_into (ibf, ibf->key_sum[i], buckets, -ibf->count[i].count_val); 266 ibf_insert_into(ibf, ibf->key_sum[i], buckets, -ibf->count[i].count_val);
264 267
265 return GNUNET_YES; 268 return GNUNET_YES;
266 } 269 }
267 270
268 if (GNUNET_YES == ibf_is_empty (ibf)) 271 if (GNUNET_YES == ibf_is_empty(ibf))
269 return GNUNET_NO; 272 return GNUNET_NO;
270 return GNUNET_SYSERR; 273 return GNUNET_SYSERR;
271} 274}
@@ -281,25 +284,25 @@ ibf_decode (struct InvertibleBloomFilter *ibf,
281 * @param buf buffer to write the data to 284 * @param buf buffer to write the data to
282 */ 285 */
283void 286void
284ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf) 287ibf_write_slice(const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf)
285{ 288{
286 struct IBF_Key *key_dst; 289 struct IBF_Key *key_dst;
287 struct IBF_KeyHash *key_hash_dst; 290 struct IBF_KeyHash *key_hash_dst;
288 struct IBF_Count *count_dst; 291 struct IBF_Count *count_dst;
289 292
290 GNUNET_assert (start + count <= ibf->size); 293 GNUNET_assert(start + count <= ibf->size);
291 294
292 /* copy keys */ 295 /* copy keys */
293 key_dst = (struct IBF_Key *) buf; 296 key_dst = (struct IBF_Key *)buf;
294 GNUNET_memcpy (key_dst, ibf->key_sum + start, count * sizeof *key_dst); 297 GNUNET_memcpy(key_dst, ibf->key_sum + start, count * sizeof *key_dst);
295 key_dst += count; 298 key_dst += count;
296 /* copy key hashes */ 299 /* copy key hashes */
297 key_hash_dst = (struct IBF_KeyHash *) key_dst; 300 key_hash_dst = (struct IBF_KeyHash *)key_dst;
298 GNUNET_memcpy (key_hash_dst, ibf->key_hash_sum + start, count * sizeof *key_hash_dst); 301 GNUNET_memcpy(key_hash_dst, ibf->key_hash_sum + start, count * sizeof *key_hash_dst);
299 key_hash_dst += count; 302 key_hash_dst += count;
300 /* copy counts */ 303 /* copy counts */
301 count_dst = (struct IBF_Count *) key_hash_dst; 304 count_dst = (struct IBF_Count *)key_hash_dst;
302 GNUNET_memcpy (count_dst, ibf->count + start, count * sizeof *count_dst); 305 GNUNET_memcpy(count_dst, ibf->count + start, count * sizeof *count_dst);
303} 306}
304 307
305 308
@@ -312,26 +315,26 @@ ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32
312 * @param ibf the ibf to read from 315 * @param ibf the ibf to read from
313 */ 316 */
314void 317void
315ibf_read_slice (const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf) 318ibf_read_slice(const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf)
316{ 319{
317 struct IBF_Key *key_src; 320 struct IBF_Key *key_src;
318 struct IBF_KeyHash *key_hash_src; 321 struct IBF_KeyHash *key_hash_src;
319 struct IBF_Count *count_src; 322 struct IBF_Count *count_src;
320 323
321 GNUNET_assert (count > 0); 324 GNUNET_assert(count > 0);
322 GNUNET_assert (start + count <= ibf->size); 325 GNUNET_assert(start + count <= ibf->size);
323 326
324 /* copy keys */ 327 /* copy keys */
325 key_src = (struct IBF_Key *) buf; 328 key_src = (struct IBF_Key *)buf;
326 GNUNET_memcpy (ibf->key_sum + start, key_src, count * sizeof *key_src); 329 GNUNET_memcpy(ibf->key_sum + start, key_src, count * sizeof *key_src);
327 key_src += count; 330 key_src += count;
328 /* copy key hashes */ 331 /* copy key hashes */
329 key_hash_src = (struct IBF_KeyHash *) key_src; 332 key_hash_src = (struct IBF_KeyHash *)key_src;
330 GNUNET_memcpy (ibf->key_hash_sum + start, key_hash_src, count * sizeof *key_hash_src); 333 GNUNET_memcpy(ibf->key_hash_sum + start, key_hash_src, count * sizeof *key_hash_src);
331 key_hash_src += count; 334 key_hash_src += count;
332 /* copy counts */ 335 /* copy counts */
333 count_src = (struct IBF_Count *) key_hash_src; 336 count_src = (struct IBF_Count *)key_hash_src;
334 GNUNET_memcpy (ibf->count + start, count_src, count * sizeof *count_src); 337 GNUNET_memcpy(ibf->count + start, count_src, count * sizeof *count_src);
335} 338}
336 339
337 340
@@ -343,19 +346,19 @@ ibf_read_slice (const void *buf, uint32_t start, uint32_t count, struct Invertib
343 * @param ibf2 IBF that will be subtracted from ibf1 346 * @param ibf2 IBF that will be subtracted from ibf1
344 */ 347 */
345void 348void
346ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2) 349ibf_subtract(struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2)
347{ 350{
348 int i; 351 int i;
349 352
350 GNUNET_assert (ibf1->size == ibf2->size); 353 GNUNET_assert(ibf1->size == ibf2->size);
351 GNUNET_assert (ibf1->hash_num == ibf2->hash_num); 354 GNUNET_assert(ibf1->hash_num == ibf2->hash_num);
352 355
353 for (i = 0; i < ibf1->size; i++) 356 for (i = 0; i < ibf1->size; i++)
354 { 357 {
355 ibf1->count[i].count_val -= ibf2->count[i].count_val; 358 ibf1->count[i].count_val -= ibf2->count[i].count_val;
356 ibf1->key_hash_sum[i].key_hash_val ^= ibf2->key_hash_sum[i].key_hash_val; 359 ibf1->key_hash_sum[i].key_hash_val ^= ibf2->key_hash_sum[i].key_hash_val;
357 ibf1->key_sum[i].key_val ^= ibf2->key_sum[i].key_val; 360 ibf1->key_sum[i].key_val ^= ibf2->key_sum[i].key_val;
358 } 361 }
359} 362}
360 363
361 364
@@ -365,15 +368,16 @@ ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFi
365 * @param ibf the IBF to copy 368 * @param ibf the IBF to copy
366 */ 369 */
367struct InvertibleBloomFilter * 370struct InvertibleBloomFilter *
368ibf_dup (const struct InvertibleBloomFilter *ibf) 371ibf_dup(const struct InvertibleBloomFilter *ibf)
369{ 372{
370 struct InvertibleBloomFilter *copy; 373 struct InvertibleBloomFilter *copy;
371 copy = GNUNET_malloc (sizeof *copy); 374
375 copy = GNUNET_malloc(sizeof *copy);
372 copy->hash_num = ibf->hash_num; 376 copy->hash_num = ibf->hash_num;
373 copy->size = ibf->size; 377 copy->size = ibf->size;
374 copy->key_hash_sum = GNUNET_memdup (ibf->key_hash_sum, ibf->size * sizeof (struct IBF_KeyHash)); 378 copy->key_hash_sum = GNUNET_memdup(ibf->key_hash_sum, ibf->size * sizeof(struct IBF_KeyHash));
375 copy->key_sum = GNUNET_memdup (ibf->key_sum, ibf->size * sizeof (struct IBF_Key)); 379 copy->key_sum = GNUNET_memdup(ibf->key_sum, ibf->size * sizeof(struct IBF_Key));
376 copy->count = GNUNET_memdup (ibf->count, ibf->size * sizeof (struct IBF_Count)); 380 copy->count = GNUNET_memdup(ibf->count, ibf->size * sizeof(struct IBF_Count));
377 return copy; 381 return copy;
378} 382}
379 383
@@ -385,10 +389,10 @@ ibf_dup (const struct InvertibleBloomFilter *ibf)
385 * @param ibf the intertible bloom filter to destroy 389 * @param ibf the intertible bloom filter to destroy
386 */ 390 */
387void 391void
388ibf_destroy (struct InvertibleBloomFilter *ibf) 392ibf_destroy(struct InvertibleBloomFilter *ibf)
389{ 393{
390 GNUNET_free (ibf->key_sum); 394 GNUNET_free(ibf->key_sum);
391 GNUNET_free (ibf->key_hash_sum); 395 GNUNET_free(ibf->key_hash_sum);
392 GNUNET_free (ibf->count); 396 GNUNET_free(ibf->count);
393 GNUNET_free (ibf); 397 GNUNET_free(ibf);
394} 398}