2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @file consensus/ibf.c
24 * @brief implementation of the invertible bloom filter
25 * @author Florian Dold
32 * Opaque handle to an invertible bloom filter (IBF).
34 * An IBF is a counting bloom filter that has the ability to restore
35 * the hashes of its stored elements with high probability.
37 struct InvertibleBloomFilter
40 * How many cells does this IBF have?
45 * In how many cells do we hash one element?
48 unsigned int hash_num;
51 * Salt for mingling hashes
56 * How many times has a bucket been hit?
57 * Can be negative, as a result of IBF subtraction.
62 * xor sums of the elements' hash codes, used to identify the elements.
64 struct GNUNET_HashCode *id_sum;
67 * xor sums of the "hash of the hash".
69 struct GNUNET_HashCode *hash_sum;
75 * Create an invertible bloom filter.
77 * @param size number of IBF buckets
78 * @param hash_num number of buckets one element is hashed in
79 * @param salt salt for mingling hashes, different salt may
80 * result in less (or more) collisions
81 * @return the newly created invertible bloom filter
83 struct InvertibleBloomFilter *
84 ibf_create (unsigned int size, unsigned int hash_num, uint32_t salt)
86 struct InvertibleBloomFilter *ibf;
88 ibf = GNUNET_malloc (sizeof (struct InvertibleBloomFilter));
89 ibf->count = GNUNET_malloc (size * sizeof (uint8_t));
90 ibf->id_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
91 ibf->hash_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
93 ibf->hash_num = hash_num;
101 * Insert an element into an IBF, with either positive or negative sign.
104 * @param id the element's hash code
105 * @param side the sign of side determines the sign of the
109 ibf_insert_on_side (struct InvertibleBloomFilter *ibf,
110 const struct GNUNET_HashCode *key,
113 struct GNUNET_HashCode bucket_indices;
114 struct GNUNET_HashCode key_copy;
115 struct GNUNET_HashCode key_hash;
119 GNUNET_assert ((1 == side) || (-1 == side));
120 GNUNET_assert (NULL != ibf);
123 int used_buckets[ibf->hash_num];
125 /* copy the key, if key and an entry in the IBF alias */
128 bucket_indices = key_copy;
129 GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), &key_hash);
131 for (i = 0; i < ibf->hash_num; i++)
138 GNUNET_CRYPTO_hash (&bucket_indices, sizeof (struct GNUNET_HashCode),
141 bucket = bucket_indices.bits[i%16] % ibf->size;
142 collided = GNUNET_NO;
143 for (j = 0; j < i; j++)
144 if (used_buckets[j] == bucket)
145 collided = GNUNET_YES;
146 if (GNUNET_YES == collided)
148 used_buckets[i] = -1;
151 used_buckets[i] = bucket;
153 ibf->count[bucket] += side;
155 GNUNET_CRYPTO_hash_xor (&key_copy, &ibf->id_sum[bucket],
156 &ibf->id_sum[bucket]);
157 GNUNET_CRYPTO_hash_xor (&key_hash, &ibf->hash_sum[bucket],
158 &ibf->hash_sum[bucket]);
164 * Insert an element into an IBF.
167 * @param id the element's hash code
170 ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *key)
172 ibf_insert_on_side (ibf, key, 1);
176 ibf_is_empty (struct InvertibleBloomFilter *ibf)
179 for (i = 0; i < ibf->size; i++)
182 if (0 != ibf->count[i])
184 for (j = 0; j < 16; ++j)
186 if (0 != ibf->hash_sum[i].bits[j])
188 if (0 != ibf->id_sum[i].bits[j])
197 * Decode and remove an element from the IBF, if possible.
199 * @param ibf the invertible bloom filter to decode
200 * @param side sign of the cell's count where the decoded element came from.
201 * A negative sign indicates that the element was recovered
202 * resides in an IBF that was previously subtracted from.
203 * @param ret_id the hash code of the decoded element, if successful
204 * @return GNUNET_YES if decoding an element was successful,
205 * GNUNET_NO if the IBF is empty,
206 * GNUNET_SYSERR if the decoding has failed
209 ibf_decode (struct InvertibleBloomFilter *ibf,
210 int *ret_side, struct GNUNET_HashCode *ret_id)
212 struct GNUNET_HashCode hash;
215 GNUNET_assert (NULL != ibf);
216 GNUNET_assert (NULL != ret_id);
217 GNUNET_assert (NULL != ret_side);
219 for (i = 0; i < ibf->size; i++)
221 if ((1 != ibf->count[i]) && (-1 != ibf->count[i]))
224 GNUNET_CRYPTO_hash (&ibf->id_sum[i], sizeof (struct GNUNET_HashCode), &hash);
226 if (0 != memcmp (&hash, &ibf->hash_sum[i], sizeof (struct GNUNET_HashCode)))
229 *ret_side = ibf->count[i];
230 *ret_id = ibf->id_sum[i];
232 /* insert on the opposite side, effectively removing the element */
233 ibf_insert_on_side (ibf, &ibf->id_sum[i], -ibf->count[i]);
238 if (GNUNET_YES == ibf_is_empty (ibf))
240 return GNUNET_SYSERR;
246 * Subtract ibf2 from ibf1, storing the result in ibf1.
247 * The two IBF's must have the same parameters size and hash_num.
249 * @param ibf1 IBF that is subtracted from
250 * @param ibf2 IBF that will be subtracted from ibf1
253 ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2)
257 GNUNET_assert (ibf1->size == ibf2->size);
258 GNUNET_assert (ibf1->hash_num == ibf2->hash_num);
259 GNUNET_assert (ibf1->salt == ibf2->salt);
261 for (i = 0; i < ibf1->size; i++)
263 ibf1->count[i] -= ibf2->count[i];
264 GNUNET_CRYPTO_hash_xor (&ibf1->id_sum[i], &ibf2->id_sum[i],
266 GNUNET_CRYPTO_hash_xor (&ibf1->hash_sum[i], &ibf2->hash_sum[i],