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 (8+4+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.
78 * xor sums of the "hash of the hash".
83 * How many times has a bucket been hit?
84 * Can be negative, as a result of IBF subtraction.
91 * Create a key from a hashcode.
93 * @param hash the hashcode
97 ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
101 * Create a hashcode from a key, by replicating the key
102 * until the hascode is filled
105 * @param dst hashcode to store the result in
108 ibf_hashcode_from_key (uint64_t key, struct GNUNET_HashCode *dst);
112 * Create an invertible bloom filter.
114 * @param size number of IBF buckets
115 * @param hash_num number of buckets one element is hashed in, usually 3 or 4
116 * @param salt salt for mingling hashes, different salt may
117 * result in less (or more) collisions
118 * @return the newly created invertible bloom filter
120 struct InvertibleBloomFilter *
121 ibf_create(uint32_t size, unsigned int hash_num, uint32_t salt);
125 * Insert an element into an IBF.
128 * @param id the element's hash code
131 ibf_insert (struct InvertibleBloomFilter *ibf, uint64_t id);
135 * Subtract ibf2 from ibf1, storing the result in ibf1.
136 * The two IBF's must have the same parameters size and hash_num.
138 * @param ibf1 IBF that is subtracted from
139 * @param ibf2 IBF that will be subtracted from ibf1
142 ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2);
146 * Decode and remove an element from the IBF, if possible.
148 * @param ibf the invertible bloom filter to decode
149 * @param side sign of the cell's count where the decoded element came from.
150 * A negative sign indicates that the element was recovered resides in an IBF
151 * that was previously subtracted from.
152 * @param ret_id the hash code of the decoded element, if successful
153 * @return GNUNET_YES if decoding an element was successful, GNUNET_NO if the IBF is empty,
154 * GNUNET_SYSERR if the decoding has faile
157 ibf_decode (struct InvertibleBloomFilter *ibf, int *side, uint64_t *ret_id);
161 * Create a copy of an IBF, the copy has to be destroyed properly.
163 * @param ibf the IBF to copy
165 struct InvertibleBloomFilter *
166 ibf_dup (struct InvertibleBloomFilter *ibf);
169 * Destroy all resources associated with the invertible bloom filter.
170 * No more ibf_*-functions may be called on ibf after calling destroy.
172 * @param ibf the intertible bloom filter to destroy
175 ibf_destroy (struct InvertibleBloomFilter *ibf);
178 #if 0 /* keep Emacsens' auto-indent happy */