2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
21 * @brief invertible bloom filter
22 * @author Florian Dold
25 #ifndef GNUNET_CONSENSUS_IBF_H
26 #define GNUNET_CONSENSUS_IBF_H
29 #include "gnunet_util_lib.h"
34 #if 0 /* keep Emacsens' auto-indent happy */
41 * Keys that can be inserted into and removed from an IBF.
54 uint32_t key_hash_val;
59 * Type of the count field of IBF buckets.
68 * Size of one ibf bucket in bytes
70 #define IBF_BUCKET_SIZE (sizeof (struct IBF_Count) + sizeof (struct IBF_Key) + \
71 sizeof (struct IBF_KeyHash))
75 * Invertible bloom filter (IBF).
77 * An IBF is a counting bloom filter that has the ability to restore
78 * the hashes of its stored elements with high probability.
80 struct InvertibleBloomFilter
83 * How many cells does this IBF have?
88 * In how many cells do we hash one element?
94 * Xor sums of the elements' keys, used to identify the elements.
95 * Array of 'size' elements.
97 struct IBF_Key *key_sum;
100 * Xor sums of the hashes of the keys of inserted elements.
101 * Array of 'size' elements.
103 struct IBF_KeyHash *key_hash_sum;
106 * How many times has a bucket been hit?
107 * Can be negative, as a result of IBF subtraction.
108 * Array of 'size' elements.
110 struct IBF_Count *count;
115 * Write buckets from an ibf to a buffer.
116 * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
118 * @param ibf the ibf to write
119 * @param start with which bucket to start
120 * @param count how many buckets to write
121 * @param buf buffer to write the data to
124 ibf_write_slice (const struct InvertibleBloomFilter *ibf,
131 * Read buckets from a buffer into an ibf.
133 * @param buf pointer to the buffer to read from
134 * @param start which bucket to start at
135 * @param count how many buckets to read
136 * @param ibf the ibf to write to
139 ibf_read_slice (const void *buf,
142 struct InvertibleBloomFilter *ibf);
146 * Create a key from a hashcode.
148 * @param hash the hashcode
152 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
156 * Create a hashcode from a key, by replicating the key
157 * until the hascode is filled
160 * @param dst hashcode to store the result in
163 ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
167 * Create an invertible bloom filter.
169 * @param size number of IBF buckets
170 * @param hash_num number of buckets one element is hashed in, usually 3 or 4
171 * @return the newly created invertible bloom filter, NULL on error
173 struct InvertibleBloomFilter *
174 ibf_create (uint32_t size, uint8_t hash_num);
178 * Insert a key into an IBF.
181 * @param key the element's hash code
184 ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
188 * Remove a key from an IBF.
191 * @param key the element's hash code
194 ibf_remove (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
198 * Subtract ibf2 from ibf1, storing the result in ibf1.
199 * The two IBF's must have the same parameters size and hash_num.
201 * @param ibf1 IBF that is subtracted from
202 * @param ibf2 IBF that will be subtracted from ibf1
205 ibf_subtract (struct InvertibleBloomFilter *ibf1,
206 const struct InvertibleBloomFilter *ibf2);
210 * Decode and remove an element from the IBF, if possible.
212 * @param ibf the invertible bloom filter to decode
213 * @param ret_side sign of the cell's count where the decoded element came from.
214 * A negative sign indicates that the element was recovered
215 * resides in an IBF that was previously subtracted from.
216 * @param ret_id receives the hash code of the decoded element, if successful
217 * @return #GNUNET_YES if decoding an element was successful,
218 * #GNUNET_NO if the IBF is empty,
219 * #GNUNET_SYSERR if the decoding has failed
222 ibf_decode (struct InvertibleBloomFilter *ibf,
224 struct IBF_Key *ret_id);
228 * Create a copy of an IBF, the copy has to be destroyed properly.
230 * @param ibf the IBF to copy
232 struct InvertibleBloomFilter *
233 ibf_dup (const struct InvertibleBloomFilter *ibf);
237 * Destroy all resources associated with the invertible bloom filter.
238 * No more ibf_*-functions may be called on ibf after calling destroy.
240 * @param ibf the intertible bloom filter to destroy
243 ibf_destroy (struct InvertibleBloomFilter *ibf);
246 #if 0 /* keep Emacsens' auto-indent happy */