implemented the invertible bloom filter
[oweals/gnunet.git] / src / consensus / gnunet-consensus-ibf.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file consensus/gnunet-consensus-ibf
23  * @brief tool for reconciling data with invertible bloom filters
24  * @author Florian Dold
25  */
26
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_util_lib.h"
32
33 #include "ibf.h"
34
35 static unsigned int asize = 10;
36 static unsigned int bsize = 10;
37 static unsigned int csize = 10;
38 static unsigned int hash_num = 3;
39 static unsigned int ibf_size = 80;
40
41
42 static struct GNUNET_CONTAINER_MultiHashMap *set_a;
43 static struct GNUNET_CONTAINER_MultiHashMap *set_b;
44 /* common elements in a and b */
45 static struct GNUNET_CONTAINER_MultiHashMap *set_c;
46
47 static struct InvertibleBloomFilter *ibf_a;
48 static struct InvertibleBloomFilter *ibf_b;
49
50
51
52 static int
53 insert_iterator (void *cls,
54                  const struct GNUNET_HashCode *key,
55                  void *value)
56 {
57   struct InvertibleBloomFilter *ibf = (struct InvertibleBloomFilter *) cls;
58   ibf_insert (ibf, key);
59   return GNUNET_YES;
60 }
61
62
63 static void
64 run (void *cls, char *const *args, const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_HashCode id;
68   int i;
69   int side;
70   int res;
71
72   set_a = GNUNET_CONTAINER_multihashmap_create (((asize == 0) ? 1 : (asize + csize)),
73                                                  GNUNET_NO);
74   set_b = GNUNET_CONTAINER_multihashmap_create (((bsize == 0) ? 1 : (bsize + csize)),
75                                                 GNUNET_NO);
76   set_c = GNUNET_CONTAINER_multihashmap_create (((csize == 0) ? 1 : csize),
77                                                 GNUNET_NO);
78
79   printf ("hash-num=%u, size=%u, #(A-B)=%u, #(B-A)=%u, #(A&B)=%u\n",
80           hash_num, ibf_size, asize, bsize, csize);
81
82   i = 0;
83   while (i < asize)
84   {
85     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id);
86     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
87       continue;
88     printf("A: %s\n", GNUNET_h2s (&id));
89     GNUNET_CONTAINER_multihashmap_put (
90         set_a, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
91     i++;
92   }
93   i = 0;
94   while (i < bsize)
95   {
96     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id);
97     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
98       continue;
99     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
100       continue;
101     printf("B: %s\n", GNUNET_h2s (&id));
102     GNUNET_CONTAINER_multihashmap_put (
103         set_b, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
104     i++;
105   }
106   i = 0;
107   while (i < csize)
108   {
109     GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &id);
110     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_a, &id))
111       continue;
112     if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (set_b, &id))
113       continue;
114     printf("C: %s\n", GNUNET_h2s (&id));
115     GNUNET_CONTAINER_multihashmap_put (
116         set_c, &id, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
117     i++;
118   }
119
120
121   ibf_a = ibf_create (ibf_size, hash_num, 0);
122   ibf_b = ibf_create (ibf_size, hash_num, 0);
123
124   GNUNET_CONTAINER_multihashmap_iterate (set_a, &insert_iterator, ibf_a);
125   GNUNET_CONTAINER_multihashmap_iterate (set_b, &insert_iterator, ibf_b);
126   GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_a);
127   GNUNET_CONTAINER_multihashmap_iterate (set_c, &insert_iterator, ibf_b);
128
129
130   printf ("-----------------\n");
131   ibf_subtract (ibf_a, ibf_b);
132
133   for (;;)
134   {
135     res = ibf_decode (ibf_a, &side, &id);
136     if (GNUNET_SYSERR == res) 
137     {
138       printf ("decode failed\n");
139       return;
140     }
141     if (GNUNET_NO == res)
142     {
143       if ((0 == GNUNET_CONTAINER_multihashmap_size (set_b)) &&
144           (0 == GNUNET_CONTAINER_multihashmap_size (set_a)))
145         printf ("decode succeeded\n");
146       else
147         printf ("decode missed elements\n");
148       return;
149     }
150
151     printf("R: %s\n", GNUNET_h2s (&id));
152     printf("s: %d\n", side);
153
154     if (side == 1)
155       res = GNUNET_CONTAINER_multihashmap_remove (set_a, &id, NULL);
156     if (side == -1)
157       res = GNUNET_CONTAINER_multihashmap_remove (set_b, &id, NULL);
158     if (GNUNET_YES != res)
159     {
160       printf ("decoded wrong element\n");
161       return;
162     }
163   }
164 }
165
166 int
167 main (int argc, char **argv)
168 {
169   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
170     {'A', "asize", NULL,
171      gettext_noop ("number of element in set A-B"), 1,
172      &GNUNET_GETOPT_set_uint, &asize},
173     {'B', "bsize", NULL,
174      gettext_noop ("number of element in set B-A"), 1,
175      &GNUNET_GETOPT_set_uint, &bsize},
176     {'C', "csize", NULL,
177      gettext_noop ("number of common elements in A and B"), 1,
178      &GNUNET_GETOPT_set_uint, &csize},
179     {'k', "hash-num", NULL,
180      gettext_noop ("hash num"), 1,
181      &GNUNET_GETOPT_set_uint, &hash_num},
182     {'s', "ibf-size", NULL,
183      gettext_noop ("ibf size"), 1,
184      &GNUNET_GETOPT_set_uint, &ibf_size},
185     GNUNET_GETOPT_OPTION_END
186   };
187   GNUNET_PROGRAM_run2 (argc, argv, "gnunet-consensus-ibf",
188                       "help",
189                       options, &run, NULL, GNUNET_YES);
190   return 0;
191 }
192