remove CYGWIN codeblocks, drop vendored Windows openvpn, drop win32 specific files.
[oweals/gnunet.git] / src / set / ibf.h
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  */
26
27 #ifndef GNUNET_CONSENSUS_IBF_H
28 #define GNUNET_CONSENSUS_IBF_H
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41
42 /**
43  * Keys that can be inserted into and removed from an IBF.
44  */
45 struct IBF_Key {
46   uint64_t key_val;
47 };
48
49
50 /**
51  * Hash of an IBF key.
52  */
53 struct IBF_KeyHash {
54   uint32_t key_hash_val;
55 };
56
57
58 /**
59  * Type of the count field of IBF buckets.
60  */
61 struct IBF_Count {
62   int8_t count_val;
63 };
64
65
66 /**
67  * Size of one ibf bucket in bytes
68  */
69 #define IBF_BUCKET_SIZE (sizeof(struct IBF_Count) + sizeof(struct IBF_Key) + \
70                          sizeof(struct IBF_KeyHash))
71
72
73 /**
74  * Invertible bloom filter (IBF).
75  *
76  * An IBF is a counting bloom filter that has the ability to restore
77  * the hashes of its stored elements with high probability.
78  */
79 struct InvertibleBloomFilter {
80   /**
81    * How many cells does this IBF have?
82    */
83   uint32_t size;
84
85   /**
86    * In how many cells do we hash one element?
87    * Usually 4 or 3.
88    */
89   uint8_t hash_num;
90
91   /**
92    * Xor sums of the elements' keys, used to identify the elements.
93    * Array of 'size' elements.
94    */
95   struct IBF_Key *key_sum;
96
97   /**
98    * Xor sums of the hashes of the keys of inserted elements.
99    * Array of 'size' elements.
100    */
101   struct IBF_KeyHash *key_hash_sum;
102
103   /**
104    * How many times has a bucket been hit?
105    * Can be negative, as a result of IBF subtraction.
106    * Array of 'size' elements.
107    */
108   struct IBF_Count *count;
109 };
110
111
112 /**
113  * Write buckets from an ibf to a buffer.
114  * Exactly (IBF_BUCKET_SIZE*ibf->size) bytes are written to buf.
115  *
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
120  */
121 void
122 ibf_write_slice(const struct InvertibleBloomFilter *ibf,
123                 uint32_t start,
124                 uint32_t count,
125                 void *buf);
126
127
128 /**
129  * Read buckets from a buffer into an ibf.
130  *
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
135  */
136 void
137 ibf_read_slice(const void *buf,
138                uint32_t start,
139                uint32_t count,
140                struct InvertibleBloomFilter *ibf);
141
142
143 /**
144  * Create a key from a hashcode.
145  *
146  * @param hash the hashcode
147  * @return a key
148  */
149 struct IBF_Key
150 ibf_key_from_hashcode(const struct GNUNET_HashCode *hash);
151
152
153 /**
154  * Create a hashcode from a key, by replicating the key
155  * until the hascode is filled
156  *
157  * @param key the key
158  * @param dst hashcode to store the result in
159  */
160 void
161 ibf_hashcode_from_key(struct IBF_Key key, struct GNUNET_HashCode *dst);
162
163
164 /**
165  * Create an invertible bloom filter.
166  *
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
170  */
171 struct InvertibleBloomFilter *
172 ibf_create(uint32_t size, uint8_t hash_num);
173
174
175 /**
176  * Insert a key into an IBF.
177  *
178  * @param ibf the IBF
179  * @param key the element's hash code
180  */
181 void
182 ibf_insert(struct InvertibleBloomFilter *ibf, struct IBF_Key key);
183
184
185 /**
186  * Remove a key from an IBF.
187  *
188  * @param ibf the IBF
189  * @param key the element's hash code
190  */
191 void
192 ibf_remove(struct InvertibleBloomFilter *ibf, struct IBF_Key key);
193
194
195 /**
196  * Subtract ibf2 from ibf1, storing the result in ibf1.
197  * The two IBF's must have the same parameters size and hash_num.
198  *
199  * @param ibf1 IBF that is subtracted from
200  * @param ibf2 IBF that will be subtracted from ibf1
201  */
202 void
203 ibf_subtract(struct InvertibleBloomFilter *ibf1,
204              const struct InvertibleBloomFilter *ibf2);
205
206
207 /**
208  * Decode and remove an element from the IBF, if possible.
209  *
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
218  */
219 int
220 ibf_decode(struct InvertibleBloomFilter *ibf,
221            int *ret_side,
222            struct IBF_Key *ret_id);
223
224
225 /**
226  * Create a copy of an IBF, the copy has to be destroyed properly.
227  *
228  * @param ibf the IBF to copy
229  */
230 struct InvertibleBloomFilter *
231 ibf_dup(const struct InvertibleBloomFilter *ibf);
232
233
234 /**
235  * Destroy all resources associated with the invertible bloom filter.
236  * No more ibf_*-functions may be called on ibf after calling destroy.
237  *
238  * @param ibf the intertible bloom filter to destroy
239  */
240 void
241 ibf_destroy(struct InvertibleBloomFilter *ibf);
242
243
244 #if 0                           /* keep Emacsens' auto-indent happy */
245 {
246 #endif
247 #ifdef __cplusplus
248 }
249 #endif
250
251 #endif