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 2, 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.
22 * @file consensus/ibf.h
23 * @brief invertible bloom filter
24 * @author Florian Dold
27 #ifndef GNUNET_CONSENSUS_IBF_H
28 #define GNUNET_CONSENSUS_IBF_H
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
37 #if 0 /* keep Emacsens' auto-indent happy */
43 * Size of one ibf bucket in bytes
45 #define IBF_BUCKET_SIZE (64+64+1)
49 * Invertible bloom filter (IBF).
51 * An IBF is a counting bloom filter that has the ability to restore
52 * the hashes of its stored elements with high probability.
54 struct InvertibleBloomFilter
57 * How many cells does this IBF have?
62 * In how many cells do we hash one element?
65 unsigned int hash_num;
68 * Salt for mingling hashes
73 * xor sums of the elements' hash codes, used to identify the elements.
75 struct GNUNET_HashCode *id_sum;
78 * xor sums of the "hash of the hash".
80 struct GNUNET_HashCode *hash_sum;
83 * How many times has a bucket been hit?
84 * Can be negative, as a result of IBF subtraction.
91 * Create an invertible bloom filter.
93 * @param size number of IBF buckets
94 * @param hash_num number of buckets one element is hashed in
95 * @param salt salt for mingling hashes, different salt may
96 * result in less (or more) collisions
97 * @return the newly created invertible bloom filter
99 struct InvertibleBloomFilter *
100 ibf_create(unsigned int size, unsigned int hash_num, uint32_t salt);
104 * Insert an element into an IBF.
107 * @param id the element's hash code
110 ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *id);
114 * Subtract ibf2 from ibf1, storing the result in ibf1.
115 * The two IBF's must have the same parameters size and hash_num.
117 * @param ibf1 IBF that is subtracted from
118 * @param ibf2 IBF that will be subtracted from ibf1
121 ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2);
125 * Decode and remove an element from the IBF, if possible.
127 * @param ibf the invertible bloom filter to decode
128 * @param side sign of the cell's count where the decoded element came from.
129 * A negative sign indicates that the element was recovered resides in an IBF
130 * that was previously subtracted from.
131 * @param ret_id the hash code of the decoded element, if successful
132 * @return GNUNET_YES if decoding an element was successful, GNUNET_NO if the IBF is empty,
133 * GNUNET_SYSERR if the decoding has faile
136 ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct GNUNET_HashCode *ret_id);
140 * Create a copy of an IBF, the copy has to be destroyed properly.
142 * @param ibf the IBF to copy
144 struct InvertibleBloomFilter *
145 ibf_dup (struct InvertibleBloomFilter *ibf);
148 * Destroy all resources associated with the invertible bloom filter.
149 * No more ibf_*-functions may be called on ibf after calling destroy.
151 * @param ibf the intertible bloom filter to destroy
154 ibf_destroy (struct InvertibleBloomFilter *ibf);
157 #if 0 /* keep Emacsens' auto-indent happy */