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 3, 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 * @brief invertible bloom filter
24 * @author Florian Dold
27 #ifndef GNUNET_CONSENSUS_IBF_H
28 #define GNUNET_CONSENSUS_IBF_H
31 #include "gnunet_util_lib.h"
36 #if 0 /* keep Emacsens' auto-indent happy */
43 * Keys that can be inserted into and removed from an IBF.
56 uint32_t key_hash_val;
61 * Type of the count field of IBF buckets.
70 * Size of one ibf bucket in bytes
72 #define IBF_BUCKET_SIZE (sizeof (struct IBF_Count) + sizeof (struct IBF_Key) + \
73 sizeof (struct IBF_KeyHash))
77 * Invertible bloom filter (IBF).
79 * An IBF is a counting bloom filter that has the ability to restore
80 * the hashes of its stored elements with high probability.
82 struct InvertibleBloomFilter
85 * How many cells does this IBF have?
90 * In how many cells do we hash one element?
96 * Xor sums of the elements' keys, used to identify the elements.
97 * Array of 'size' elements.
99 struct IBF_Key *key_sum;
102 * Xor sums of the hashes of the keys of inserted elements.
103 * Array of 'size' elements.
105 struct IBF_KeyHash *key_hash_sum;
108 * How many times has a bucket been hit?
109 * Can be negative, as a result of IBF subtraction.
110 * Array of 'size' elements.
112 struct IBF_Count *count;
117 * Write buckets from an ibf to a buffer.
118 * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
120 * @param ibf the ibf to write
121 * @param start with which bucket to start
122 * @param count how many buckets to write
123 * @param buf buffer to write the data to
126 ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void *buf);
130 * Read buckets from a buffer into an ibf.
132 * @param buf pointer to the buffer to read from
133 * @param start which bucket to start at
134 * @param count how many buckets to read
135 * @param ibf the ibf to read from
138 ibf_read_slice (const void *buf, uint32_t start, uint32_t count, struct InvertibleBloomFilter *ibf);
142 * Create a key from a hashcode.
144 * @param hash the hashcode
148 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
152 * Create a hashcode from a key, by replicating the key
153 * until the hascode is filled
156 * @param dst hashcode to store the result in
159 ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
163 * Create an invertible bloom filter.
165 * @param size number of IBF buckets
166 * @param hash_num number of buckets one element is hashed in, usually 3 or 4
167 * @return the newly created invertible bloom filter
169 struct InvertibleBloomFilter *
170 ibf_create (uint32_t size, uint8_t hash_num);
174 * Insert a key into an IBF.
177 * @param key the element's hash code
180 ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
184 * Remove a key from an IBF.
187 * @param key the element's hash code
190 ibf_remove (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
194 * Subtract ibf2 from ibf1, storing the result in ibf1.
195 * The two IBF's must have the same parameters size and hash_num.
197 * @param ibf1 IBF that is subtracted from
198 * @param ibf2 IBF that will be subtracted from ibf1
201 ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2);
205 * Decode and remove an element from the IBF, if possible.
207 * @param ibf the invertible bloom filter to decode
208 * @param ret_side sign of the cell's count where the decoded element came from.
209 * A negative sign indicates that the element was recovered
210 * resides in an IBF that was previously subtracted from.
211 * @param ret_id receives the hash code of the decoded element, if successful
212 * @return GNUNET_YES if decoding an element was successful,
213 * GNUNET_NO if the IBF is empty,
214 * GNUNET_SYSERR if the decoding has failed
217 ibf_decode (struct InvertibleBloomFilter *ibf, int *ret_side, struct IBF_Key *ret_id);
221 * Create a copy of an IBF, the copy has to be destroyed properly.
223 * @param ibf the IBF to copy
225 struct InvertibleBloomFilter *
226 ibf_dup (const struct InvertibleBloomFilter *ibf);
230 * Destroy all resources associated with the invertible bloom filter.
231 * No more ibf_*-functions may be called on ibf after calling destroy.
233 * @param ibf the intertible bloom filter to destroy
236 ibf_destroy (struct InvertibleBloomFilter *ibf);
239 #if 0 /* keep Emacsens' auto-indent happy */