aboutsummaryrefslogtreecommitdiff
path: root/src/service/setu/ibf.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/setu/ibf.h')
-rw-r--r--src/service/setu/ibf.h310
1 files changed, 310 insertions, 0 deletions
diff --git a/src/service/setu/ibf.h b/src/service/setu/ibf.h
new file mode 100644
index 000000000..5628405dc
--- /dev/null
+++ b/src/service/setu/ibf.h
@@ -0,0 +1,310 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file set/ibf.h
23 * @brief invertible bloom filter
24 * @author Florian Dold
25 * @author Elias Summermatter
26 */
27
28#ifndef GNUNET_CONSENSUS_IBF_H
29#define GNUNET_CONSENSUS_IBF_H
30
31#include "platform.h"
32#include "gnunet_util_lib.h"
33
34#ifdef __cplusplus
35extern "C"
36{
37#if 0 /* keep Emacsens' auto-indent happy */
38}
39#endif
40#endif
41
42
43/**
44 * Keys that can be inserted into and removed from an IBF.
45 */
46struct IBF_Key
47{
48 uint64_t key_val;
49};
50
51
52/**
53 * Hash of an IBF key.
54 */
55struct IBF_KeyHash
56{
57 uint32_t key_hash_val;
58};
59
60
61/**
62 * Type of the count field of IBF buckets.
63 */
64struct IBF_Count
65{
66 int64_t count_val;
67};
68
69
70/**
71 * Size of one ibf bucket in bytes
72 */
73#define IBF_BUCKET_SIZE (sizeof(struct IBF_Count) + sizeof(struct IBF_Key) \
74 + sizeof(struct IBF_KeyHash))
75
76
77/**
78 * Invertible bloom filter (IBF).
79 *
80 * An IBF is a counting bloom filter that has the ability to restore
81 * the hashes of its stored elements with high probability.
82 */
83struct InvertibleBloomFilter
84{
85 /**
86 * How many cells does this IBF have?
87 */
88 uint32_t size;
89
90 /**
91 * In how many cells do we hash one element?
92 * Usually 4 or 3.
93 */
94 uint8_t hash_num;
95
96 /**
97 * If an IBF is decoded this count stores how many
98 * elements are on the local site. This is used
99 * to estimate the set difference on a site
100 */
101 int local_decoded_count;
102
103 /**
104 * If an IBF is decoded this count stores how many
105 * elements are on the remote site. This is used
106 * to estimate the set difference on a site
107 */
108 int remote_decoded_count;
109
110 /**
111 * Xor sums of the elements' keys, used to identify the elements.
112 * Array of 'size' elements.
113 */
114 struct IBF_Key *key_sum;
115
116 /**
117 * Xor sums of the hashes of the keys of inserted elements.
118 * Array of 'size' elements.
119 */
120 struct IBF_KeyHash *key_hash_sum;
121
122 /**
123 * How many times has a bucket been hit?
124 * Can be negative, as a result of IBF subtraction.
125 * Array of 'size' elements.
126 */
127 struct IBF_Count *count;
128};
129
130
131/**
132 * Write buckets from an ibf to a buffer.
133 * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
134 *
135 * @param ibf the ibf to write
136 * @param start with which bucket to start
137 * @param count how many buckets to write
138 * @param buf buffer to write the data to
139 */
140void
141ibf_write_slice (const struct InvertibleBloomFilter *ibf,
142 uint32_t start,
143 uint64_t count,
144 void *buf,
145 uint8_t counter_max_length);
146
147
148/**
149 * Read buckets from a buffer into an ibf.
150 *
151 * @param buf pointer to the buffer to read from
152 * @param start which bucket to start at
153 * @param count how many buckets to read
154 * @param ibf the ibf to write to
155 */
156void
157ibf_read_slice (const void *buf,
158 uint32_t start,
159 uint64_t count,
160 struct InvertibleBloomFilter *ibf,
161 uint8_t counter_max_length);
162
163
164/**
165 * Create a key from a hashcode.
166 *
167 * @param hash the hashcode
168 * @return a key
169 */
170struct IBF_Key
171ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
172
173
174/**
175 * Create a hashcode from a key, by replicating the key
176 * until the hascode is filled
177 *
178 * @param key the key
179 * @param dst hashcode to store the result in
180 */
181void
182ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
183
184
185/**
186 * Create an invertible bloom filter.
187 *
188 * @param size number of IBF buckets
189 * @param hash_num number of buckets one element is hashed in, usually 3 or 4
190 * @return the newly created invertible bloom filter, NULL on error
191 */
192struct InvertibleBloomFilter *
193ibf_create (uint32_t size, uint8_t hash_num);
194
195
196/**
197 * Insert a key into an IBF.
198 *
199 * @param ibf the IBF
200 * @param key the element's hash code
201 */
202void
203ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
204
205
206/**
207 * Remove a key from an IBF.
208 *
209 * @param ibf the IBF
210 * @param key the element's hash code
211 */
212void
213ibf_remove (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
214
215
216/**
217 * Subtract ibf2 from ibf1, storing the result in ibf1.
218 * The two IBF's must have the same parameters size and hash_num.
219 *
220 * @param ibf1 IBF that is subtracted from
221 * @param ibf2 IBF that will be subtracted from ibf1
222 */
223void
224ibf_subtract (struct InvertibleBloomFilter *ibf1,
225 const struct InvertibleBloomFilter *ibf2);
226
227
228/**
229 * Decode and remove an element from the IBF, if possible.
230 *
231 * @param ibf the invertible bloom filter to decode
232 * @param ret_side sign of the cell's count where the decoded element came from.
233 * A negative sign indicates that the element was recovered
234 * resides in an IBF that was previously subtracted from.
235 * @param ret_id receives the hash code of the decoded element, if successful
236 * @return #GNUNET_YES if decoding an element was successful,
237 * #GNUNET_NO if the IBF is empty,
238 * #GNUNET_SYSERR if the decoding has failed
239 */
240int
241ibf_decode (struct InvertibleBloomFilter *ibf,
242 int *ret_side,
243 struct IBF_Key *ret_id);
244
245
246/**
247 * Create a copy of an IBF, the copy has to be destroyed properly.
248 *
249 * @param ibf the IBF to copy
250 */
251struct InvertibleBloomFilter *
252ibf_dup (const struct InvertibleBloomFilter *ibf);
253
254
255/**
256 * Destroy all resources associated with the invertible bloom filter.
257 * No more ibf_*-functions may be called on ibf after calling destroy.
258 *
259 * @param ibf the intertible bloom filter to destroy
260 */
261void
262ibf_destroy (struct InvertibleBloomFilter *ibf);
263
264uint8_t
265ibf_get_max_counter (struct InvertibleBloomFilter *ibf);
266
267
268/**
269 * Packs the counter to transmit only the smallest possible amount of bytes and
270 * preventing overflow of the counter
271 * @param ibf the ibf to write
272 * @param start with which bucket to start
273 * @param count how many buckets to write
274 * @param buf buffer to write the data to
275 * @param max bit length of a counter for unpacking
276 */
277
278void
279pack_counter (const struct InvertibleBloomFilter *ibf,
280 uint32_t start,
281 uint64_t count,
282 uint8_t *buf,
283 uint8_t counter_max_length);
284
285/**
286 * Unpacks the counter to transmit only the smallest possible amount of bytes and
287 * preventing overflow of the counter
288 * @param ibf the ibf to write
289 * @param start with which bucket to start
290 * @param count how many buckets to write
291 * @param buf buffer to write the data to
292 * @param max bit length of a counter for unpacking
293 */
294
295void
296unpack_counter (const struct InvertibleBloomFilter *ibf,
297 uint32_t start,
298 uint64_t count,
299 uint8_t *buf,
300 uint8_t counter_max_length);
301
302
303#if 0 /* keep Emacsens' auto-indent happy */
304{
305#endif
306#ifdef __cplusplus
307}
308#endif
309
310#endif