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
30 struct InvertibleBloomFilter
33 * How many cells does this IBF have?
38 * In how many cells do we hash one element?
41 unsigned int hash_num;
44 * Salt for mingling hashes
49 * How many times has a bucket been hit?
50 * Can be negative, as a result of IBF subtraction.
55 * xor sums of the elements' hash codes, used to identify the elements.
57 struct GNUNET_HashCode *id_sum;
60 * xor sums of the "hash of the hash".
62 struct GNUNET_HashCode *hash_sum;
68 * Create an invertible bloom filter.
70 struct InvertibleBloomFilter *
71 ibf_create (unsigned int size, unsigned int hash_num, uint32_t salt)
73 struct InvertibleBloomFilter *ibf;
75 ibf = GNUNET_malloc (sizeof (struct InvertibleBloomFilter));
76 ibf->count = GNUNET_malloc (size * sizeof (uint8_t));
77 ibf->id_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
78 ibf->hash_sum = GNUNET_malloc (size * sizeof (struct GNUNET_HashCode));
80 ibf->hash_num = hash_num;
88 * Insert an element into an IBF.
91 ibf_insert_on_side (struct InvertibleBloomFilter *ibf,
92 const struct GNUNET_HashCode *key,
95 struct GNUNET_HashCode bucket_indices;
96 struct GNUNET_HashCode remove_key;
97 struct GNUNET_HashCode key_hash;
100 /* copy the key, if key and an entry in the IBF alias */
103 GNUNET_assert ((1 == side) || (-1 == side));
105 bucket_indices = remove_key;
106 GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), &key_hash);
108 for (i = 0; i < ibf->hash_num; i++)
112 GNUNET_CRYPTO_hash (&bucket_indices, sizeof (struct GNUNET_HashCode),
115 bucket = bucket_indices.bits[i%16] % ibf->size;
117 printf ("inserting %s in bucket %u side %d\n",
118 GNUNET_h2s (&remove_key), bucket, side);
120 ibf->count[bucket] += side;
122 GNUNET_CRYPTO_hash_xor (&remove_key, &ibf->id_sum[bucket],
123 &ibf->id_sum[bucket]);
124 GNUNET_CRYPTO_hash_xor (&key_hash, &ibf->hash_sum[bucket],
125 &ibf->hash_sum[bucket]);
130 * Insert an element into an IBF.
133 ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *key)
135 ibf_insert_on_side (ibf, key, 1);
139 ibf_is_empty (struct InvertibleBloomFilter *ibf)
142 for (i = 0; i < ibf->size; i++)
145 if (0 != ibf->count[i])
147 for (j = 0; j < 16; ++j)
149 if (0 != ibf->hash_sum[i].bits[j])
151 if (0 != ibf->id_sum[i].bits[j])
160 * Decode and remove an element from the IBF, if possible.
162 * @param ibf the invertible bloom filter to decode
163 * @param ret_id the hash code of the decoded element, if successful
164 * @param side sign of the cell's count where the decoded element came from.
165 * A negative sign indicates that the element was recovered
166 * resides in an IBF that was previously subtracted from.
167 * @return GNUNET_YES if decoding an element was successful,
168 * GNUNET_NO if the IBF is empty,
169 * GNUNET_SYSERR if the decoding has failed
172 ibf_decode (struct InvertibleBloomFilter *ibf,
173 int *ret_side, struct GNUNET_HashCode *ret_id)
175 struct GNUNET_HashCode hash;
178 GNUNET_assert (NULL != ibf);
179 GNUNET_assert (NULL != ret_id);
180 GNUNET_assert (NULL != ret_side);
182 for (i = 0; i < ibf->size; i++)
184 if ((1 != ibf->count[i]) && (-1 != ibf->count[i]))
187 GNUNET_CRYPTO_hash (&ibf->id_sum[i], sizeof (struct GNUNET_HashCode), &hash);
189 if (0 != memcmp (&hash, &ibf->hash_sum[i], sizeof (struct GNUNET_HashCode)))
192 printf ("%d pure\n", i);
193 printf ("%d count\n", ibf->count[i]);
195 *ret_side = ibf->count[i];
196 *ret_id = ibf->id_sum[i];
198 /* insert on the opposite side, effectively removing the element */
199 ibf_insert_on_side (ibf, &ibf->id_sum[i], -ibf->count[i]);
204 if (GNUNET_YES == ibf_is_empty (ibf))
206 return GNUNET_SYSERR;
212 * Subtract ibf2 from ibf1, storing the result in ibf1.
213 * The two IBF's must have the same parameters size and hash_num.
215 * @return a newly allocated invertible bloom filter
218 ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2)
222 GNUNET_assert (ibf1->size == ibf2->size);
223 GNUNET_assert (ibf1->hash_num == ibf2->hash_num);
224 GNUNET_assert (ibf1->salt == ibf2->salt);
226 for (i = 0; i < ibf1->size; i++)
228 printf ("ibf1->count[%d]=%d, ibf2->count[%d]=%d\n", i, ibf1->count[i], i,
230 ibf1->count[i] -= ibf2->count[i];
231 printf("diff: %d\n", ibf1->count[i]);
232 GNUNET_CRYPTO_hash_xor (&ibf1->id_sum[i], &ibf2->id_sum[i],
234 GNUNET_CRYPTO_hash_xor (&ibf1->hash_sum[i], &ibf2->hash_sum[i],