GNUnet  0.11.x
ibf.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2012 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "ibf.h"
28 
33 #define IBF_KEY_HASH_VAL(k) (GNUNET_CRYPTO_crc32_n (&(k), sizeof(struct \
34  IBF_KeyHash)))
35 
42 struct IBF_Key
44 {
45  return *(struct IBF_Key *) hash;
46 }
47 
48 
56 void
58  struct GNUNET_HashCode *dst)
59 {
60  struct IBF_Key *p;
61  unsigned int i;
62  const unsigned int keys_per_hashcode = sizeof(struct GNUNET_HashCode)
63  / sizeof(struct IBF_Key);
64 
65  p = (struct IBF_Key *) dst;
66  for (i = 0; i < keys_per_hashcode; i++)
67  *p++ = key;
68 }
69 
70 
78 struct InvertibleBloomFilter *
79 ibf_create (uint32_t size, uint8_t hash_num)
80 {
81  struct InvertibleBloomFilter *ibf;
82 
83  GNUNET_assert (0 != size);
84 
85  ibf = GNUNET_new (struct InvertibleBloomFilter);
86  ibf->count = GNUNET_malloc_large (size * sizeof(uint8_t));
87  if (NULL == ibf->count)
88  {
89  GNUNET_free (ibf);
90  return NULL;
91  }
92  ibf->key_sum = GNUNET_malloc_large (size * sizeof(struct IBF_Key));
93  if (NULL == ibf->key_sum)
94  {
95  GNUNET_free (ibf->count);
96  GNUNET_free (ibf);
97  return NULL;
98  }
99  ibf->key_hash_sum = GNUNET_malloc_large (size * sizeof(struct IBF_KeyHash));
100  if (NULL == ibf->key_hash_sum)
101  {
102  GNUNET_free (ibf->key_sum);
103  GNUNET_free (ibf->count);
104  GNUNET_free (ibf);
105  return NULL;
106  }
107  ibf->size = size;
108  ibf->hash_num = hash_num;
109 
110  return ibf;
111 }
112 
113 
117 static void
119  struct IBF_Key key,
120  int *dst)
121 {
122  uint32_t filled;
123  uint32_t i;
124  uint32_t bucket;
125 
126  bucket = GNUNET_CRYPTO_crc32_n (&key, sizeof key);
127  for (i = 0, filled = 0; filled < ibf->hash_num; i++)
128  {
129  uint64_t x;
130 
131  for (unsigned int j = 0; j < filled; j++)
132  if (dst[j] == bucket % ibf->size)
133  goto try_next;
134  dst[filled++] = bucket % ibf->size;
135 try_next:
136  x = ((uint64_t) bucket << 32) | i;
137  bucket = GNUNET_CRYPTO_crc32_n (&x, sizeof x);
138  }
139 }
140 
141 
142 static void
144  struct IBF_Key key,
145  const int *buckets,
146  int side)
147 {
148  for (unsigned int i = 0; i < ibf->hash_num; i++)
149  {
150  const int bucket = buckets[i];
151 
152  ibf->count[bucket].count_val += side;
153  ibf->key_sum[bucket].key_val ^= key.key_val;
154  ibf->key_hash_sum[bucket].key_hash_val
155  ^= IBF_KEY_HASH_VAL (key);
156  }
157 }
158 
159 
166 void
168  struct IBF_Key key)
169 {
170  int buckets[ibf->hash_num];
171 
172  GNUNET_assert (ibf->hash_num <= ibf->size);
173  ibf_get_indices (ibf, key, buckets);
174  ibf_insert_into (ibf, key, buckets, 1);
175 }
176 
177 
184 void
186  struct IBF_Key key)
187 {
188  int buckets[ibf->hash_num];
189 
190  GNUNET_assert (ibf->hash_num <= ibf->size);
191  ibf_get_indices (ibf, key, buckets);
192  ibf_insert_into (ibf, key, buckets, -1);
193 }
194 
195 
199 static int
201 {
202  for (uint32_t i = 0; i < ibf->size; i++)
203  {
204  if (0 != ibf->count[i].count_val)
205  return GNUNET_NO;
206  if (0 != ibf->key_hash_sum[i].key_hash_val)
207  return GNUNET_NO;
208  if (0 != ibf->key_sum[i].key_val)
209  return GNUNET_NO;
210  }
211  return GNUNET_YES;
212 }
213 
214 
227 int
229  int *ret_side,
230  struct IBF_Key *ret_id)
231 {
232  struct IBF_KeyHash hash;
233  int buckets[ibf->hash_num];
234 
235  for (uint32_t i = 0; i < ibf->size; i++)
236  {
237  int hit;
238 
239  /* we can only decode from pure buckets */
240  if ( (1 != ibf->count[i].count_val) &&
241  (-1 != ibf->count[i].count_val) )
242  continue;
243 
244  hash.key_hash_val = IBF_KEY_HASH_VAL (ibf->key_sum[i]);
245 
246  /* test if the hash matches the key */
247  if (hash.key_hash_val != ibf->key_hash_sum[i].key_hash_val)
248  continue;
249 
250  /* test if key in bucket hits its own location,
251  * if not, the key hash was subject to collision */
252  hit = GNUNET_NO;
253  ibf_get_indices (ibf, ibf->key_sum[i], buckets);
254  for (int j = 0; j < ibf->hash_num; j++)
255  if (buckets[j] == i)
256  hit = GNUNET_YES;
257 
258  if (GNUNET_NO == hit)
259  continue;
260 
261  if (NULL != ret_side)
262  *ret_side = ibf->count[i].count_val;
263  if (NULL != ret_id)
264  *ret_id = ibf->key_sum[i];
265 
266  /* insert on the opposite side, effectively removing the element */
267  ibf_insert_into (ibf, ibf->key_sum[i], buckets, -ibf->count[i].count_val);
268 
269  return GNUNET_YES;
270  }
271 
272  if (GNUNET_YES == ibf_is_empty (ibf))
273  return GNUNET_NO;
274  return GNUNET_SYSERR;
275 }
276 
277 
287 void
289  uint32_t start,
290  uint32_t count, void *buf)
291 {
292  struct IBF_Key *key_dst;
293  struct IBF_KeyHash *key_hash_dst;
294  struct IBF_Count *count_dst;
295 
296  GNUNET_assert (start + count <= ibf->size);
297 
298  /* copy keys */
299  key_dst = (struct IBF_Key *) buf;
300  GNUNET_memcpy (key_dst,
301  ibf->key_sum + start,
302  count * sizeof *key_dst);
303  key_dst += count;
304  /* copy key hashes */
305  key_hash_dst = (struct IBF_KeyHash *) key_dst;
306  GNUNET_memcpy (key_hash_dst,
307  ibf->key_hash_sum + start,
308  count * sizeof *key_hash_dst);
309  key_hash_dst += count;
310  /* copy counts */
311  count_dst = (struct IBF_Count *) key_hash_dst;
312  GNUNET_memcpy (count_dst,
313  ibf->count + start,
314  count * sizeof *count_dst);
315 }
316 
317 
326 void
327 ibf_read_slice (const void *buf,
328  uint32_t start,
329  uint32_t count,
330  struct InvertibleBloomFilter *ibf)
331 {
332  struct IBF_Key *key_src;
333  struct IBF_KeyHash *key_hash_src;
334  struct IBF_Count *count_src;
335 
336  GNUNET_assert (count > 0);
337  GNUNET_assert (start + count <= ibf->size);
338 
339  /* copy keys */
340  key_src = (struct IBF_Key *) buf;
341  GNUNET_memcpy (ibf->key_sum + start,
342  key_src,
343  count * sizeof *key_src);
344  key_src += count;
345  /* copy key hashes */
346  key_hash_src = (struct IBF_KeyHash *) key_src;
347  GNUNET_memcpy (ibf->key_hash_sum + start,
348  key_hash_src,
349  count * sizeof *key_hash_src);
350  key_hash_src += count;
351  /* copy counts */
352  count_src = (struct IBF_Count *) key_hash_src;
353  GNUNET_memcpy (ibf->count + start,
354  count_src,
355  count * sizeof *count_src);
356 }
357 
358 
366 void
368  const struct InvertibleBloomFilter *ibf2)
369 {
370  GNUNET_assert (ibf1->size == ibf2->size);
371  GNUNET_assert (ibf1->hash_num == ibf2->hash_num);
372 
373  for (uint32_t i = 0; i < ibf1->size; i++)
374  {
375  ibf1->count[i].count_val -= ibf2->count[i].count_val;
376  ibf1->key_hash_sum[i].key_hash_val ^= ibf2->key_hash_sum[i].key_hash_val;
377  ibf1->key_sum[i].key_val ^= ibf2->key_sum[i].key_val;
378  }
379 }
380 
381 
387 struct InvertibleBloomFilter *
388 ibf_dup (const struct InvertibleBloomFilter *ibf)
389 {
390  struct InvertibleBloomFilter *copy;
391 
392  copy = GNUNET_malloc (sizeof *copy);
393  copy->hash_num = ibf->hash_num;
394  copy->size = ibf->size;
396  ibf->size * sizeof(struct IBF_KeyHash));
397  copy->key_sum = GNUNET_memdup (ibf->key_sum,
398  ibf->size * sizeof(struct IBF_Key));
399  copy->count = GNUNET_memdup (ibf->count,
400  ibf->size * sizeof(struct IBF_Count));
401  return copy;
402 }
403 
404 
411 void
413 {
414  GNUNET_free (ibf->key_sum);
415  GNUNET_free (ibf->key_hash_sum);
416  GNUNET_free (ibf->count);
417  GNUNET_free (ibf);
418 }
void ibf_remove(struct InvertibleBloomFilter *ibf, struct IBF_Key key)
Remove a key from an IBF.
Definition: ibf.c:184
int32_t GNUNET_CRYPTO_crc32_n(const void *buf, size_t len)
Compute the CRC32 checksum for the first len bytes of the buffer.
Definition: crypto_crc.c:106
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Invertible bloom filter (IBF).
Definition: ibf.h:82
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct IBF_Count * count
How many times has a bucket been hit? Can be negative, as a result of IBF subtraction.
Definition: ibf.h:112
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint64_t key_val
Definition: ibf.h:47
void ibf_write_slice(const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf)
Write buckets from an ibf to a buffer.
Definition: ibf.c:290
#define GNUNET_malloc_large(size)
Wrapper around malloc.
static void ibf_insert_into(struct InvertibleBloomFilter *ibf, struct IBF_Key key, const int *buckets, int side)
Definition: ibf.c:143
struct InvertibleBloomFilter * ibf_dup(const struct InvertibleBloomFilter *ibf)
Create a copy of an IBF, the copy has to be destroyed properly.
Definition: ibf.c:379
uint32_t size
How many cells does this IBF have?
Definition: ibf.h:87
static int ibf_is_empty(struct InvertibleBloomFilter *ibf)
Test is the IBF is empty, i.e.
Definition: ibf.c:200
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
uint32_t key_hash_val
Definition: ibf.h:56
#define IBF_KEY_HASH_VAL(k)
Compute the key&#39;s hash from the key.
Definition: ibf.c:33
int ibf_decode(struct InvertibleBloomFilter *ibf, int *ret_side, struct IBF_Key *ret_id)
Decode and remove an element from the IBF, if possible.
Definition: ibf.c:228
void ibf_hashcode_from_key(struct IBF_Key key, struct GNUNET_HashCode *dst)
Create a hashcode from a key, by replicating the key until the hascode is filled. ...
Definition: ibf.c:57
static char buf[2048]
uint8_t hash_num
In how many cells do we hash one element? Usually 4 or 3.
Definition: ibf.h:93
void ibf_subtract(struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2)
Subtract ibf2 from ibf1, storing the result in ibf1.
Definition: ibf.c:356
static void ibf_get_indices(const struct InvertibleBloomFilter *ibf, struct IBF_Key key, int *dst)
Store unique bucket indices for the specified key in dst.
Definition: ibf.c:118
A 512-bit hashcode.
Type of the count field of IBF buckets.
Definition: ibf.h:63
struct IBF_Key * key_sum
Xor sums of the elements&#39; keys, used to identify the elements.
Definition: ibf.h:99
void ibf_read_slice(const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf)
Read buckets from a buffer into an ibf.
Definition: ibf.c:323
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void ibf_destroy(struct InvertibleBloomFilter *ibf)
Destroy all resources associated with the invertible bloom filter.
Definition: ibf.c:403
static unsigned int hash_num
Hash of an IBF key.
Definition: ibf.h:54
struct InvertibleBloomFilter * ibf_create(uint32_t size, uint8_t hash_num)
Create an invertible bloom filter.
Definition: ibf.c:79
void ibf_insert(struct InvertibleBloomFilter *ibf, struct IBF_Key key)
Insert a key into an IBF.
Definition: ibf.c:167
int8_t count_val
Definition: ibf.h:65
struct IBF_KeyHash * key_hash_sum
Xor sums of the hashes of the keys of inserted elements.
Definition: ibf.h:105
struct IBF_Key ibf_key_from_hashcode(const struct GNUNET_HashCode *hash)
Create a key from a hashcode.
Definition: ibf.c:43
Keys that can be inserted into and removed from an IBF.
Definition: ibf.h:45
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.