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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.
54 uint32_t key_hash_val;
59 * Type of the count field of IBF buckets.
67 * Size of one ibf bucket in bytes
69 #define IBF_BUCKET_SIZE (sizeof(struct IBF_Count) + sizeof(struct IBF_Key) + \
70 sizeof(struct IBF_KeyHash))
74 * Invertible bloom filter (IBF).
76 * An IBF is a counting bloom filter that has the ability to restore
77 * the hashes of its stored elements with high probability.
79 struct InvertibleBloomFilter {
81 * How many cells does this IBF have?
86 * In how many cells do we hash one element?
92 * Xor sums of the elements' keys, used to identify the elements.
93 * Array of 'size' elements.
95 struct IBF_Key *key_sum;
98 * Xor sums of the hashes of the keys of inserted elements.
99 * Array of 'size' elements.
101 struct IBF_KeyHash *key_hash_sum;
104 * How many times has a bucket been hit?
105 * Can be negative, as a result of IBF subtraction.
106 * Array of 'size' elements.
108 struct IBF_Count *count;
113 * Write buckets from an ibf to a buffer.
114 * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
116 * @param ibf the ibf to write
117 * @param start with which bucket to start
118 * @param count how many buckets to write
119 * @param buf buffer to write the data to
122 ibf_write_slice(const struct InvertibleBloomFilter *ibf,
129 * Read buckets from a buffer into an ibf.
131 * @param buf pointer to the buffer to read from
132 * @param start which bucket to start at
133 * @param count how many buckets to read
134 * @param ibf the ibf to write to
137 ibf_read_slice(const void *buf,
140 struct InvertibleBloomFilter *ibf);
144 * Create a key from a hashcode.
146 * @param hash the hashcode
150 ibf_key_from_hashcode(const struct GNUNET_HashCode *hash);
154 * Create a hashcode from a key, by replicating the key
155 * until the hascode is filled
158 * @param dst hashcode to store the result in
161 ibf_hashcode_from_key(struct IBF_Key key, struct GNUNET_HashCode *dst);
165 * Create an invertible bloom filter.
167 * @param size number of IBF buckets
168 * @param hash_num number of buckets one element is hashed in, usually 3 or 4
169 * @return the newly created invertible bloom filter, NULL on error
171 struct InvertibleBloomFilter *
172 ibf_create(uint32_t size, uint8_t hash_num);
176 * Insert a key into an IBF.
179 * @param key the element's hash code
182 ibf_insert(struct InvertibleBloomFilter *ibf, struct IBF_Key key);
186 * Remove a key from an IBF.
189 * @param key the element's hash code
192 ibf_remove(struct InvertibleBloomFilter *ibf, struct IBF_Key key);
196 * Subtract ibf2 from ibf1, storing the result in ibf1.
197 * The two IBF's must have the same parameters size and hash_num.
199 * @param ibf1 IBF that is subtracted from
200 * @param ibf2 IBF that will be subtracted from ibf1
203 ibf_subtract(struct InvertibleBloomFilter *ibf1,
204 const struct InvertibleBloomFilter *ibf2);
208 * Decode and remove an element from the IBF, if possible.
210 * @param ibf the invertible bloom filter to decode
211 * @param ret_side sign of the cell's count where the decoded element came from.
212 * A negative sign indicates that the element was recovered
213 * resides in an IBF that was previously subtracted from.
214 * @param ret_id receives the hash code of the decoded element, if successful
215 * @return #GNUNET_YES if decoding an element was successful,
216 * #GNUNET_NO if the IBF is empty,
217 * #GNUNET_SYSERR if the decoding has failed
220 ibf_decode(struct InvertibleBloomFilter *ibf,
222 struct IBF_Key *ret_id);
226 * Create a copy of an IBF, the copy has to be destroyed properly.
228 * @param ibf the IBF to copy
230 struct InvertibleBloomFilter *
231 ibf_dup(const struct InvertibleBloomFilter *ibf);
235 * Destroy all resources associated with the invertible bloom filter.
236 * No more ibf_*-functions may be called on ibf after calling destroy.
238 * @param ibf the intertible bloom filter to destroy
241 ibf_destroy(struct InvertibleBloomFilter *ibf);
244 #if 0 /* keep Emacsens' auto-indent happy */