consensus now implemented with primitive conclusion group selection
[oweals/gnunet.git] / src / consensus / ibf.h
index 26f10190563450e6b538d66fb9f9d4c25d4d0117..cafe55c8d805367a32d593add593e3a646ceee10 100644 (file)
@@ -39,11 +39,27 @@ extern "C"
 #endif
 #endif
 
+
+struct IBF_Key
+{
+  uint64_t key_val;
+};
+
+struct IBF_KeyHash
+{
+  uint32_t key_hash_val;
+};
+
+struct IBF_Count
+{
+  int8_t count_val;
+};
+
 /**
  * Size of one ibf bucket in bytes
  */
-#define IBF_BUCKET_SIZE (64+64+1)
-
+#define IBF_BUCKET_SIZE (sizeof (struct IBF_Count) + sizeof (struct IBF_Key) + \
+    sizeof (struct IBF_KeyHash))
 
 /**
  * Invertible bloom filter (IBF).
@@ -56,13 +72,13 @@ struct InvertibleBloomFilter
   /**
    * How many cells does this IBF have?
    */
-  unsigned int size;
+  uint32_t size;
 
   /**
    * In how many cells do we hash one element?
    * Usually 4 or 3.
    */
-  unsigned int hash_num;
+  uint8_t hash_num;
 
   /**
    * Salt for mingling hashes
@@ -70,44 +86,126 @@ struct InvertibleBloomFilter
   uint32_t salt;
 
   /**
-   * xor sums of the elements' hash codes, used to identify the elements.
+   * Xor sums of the elements' keys, used to identify the elements.
+   * Array of 'size' elements.
    */
-  struct GNUNET_HashCode *id_sum;
+  struct IBF_Key *key_sum;
 
   /**
-   * xor sums of the "hash of the hash".
+   * Xor sums of the hashes of the keys of inserted elements.
+   * Array of 'size' elements.
    */
-  struct GNUNET_HashCode *hash_sum;
+  struct IBF_KeyHash *key_hash_sum;
 
   /**
    * How many times has a bucket been hit?
    * Can be negative, as a result of IBF subtraction.
+   * Array of 'size' elements.
    */
-  int8_t *count;
+  struct IBF_Count *count;
 };
 
 
+/**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write_slice (const struct InvertibleBloomFilter *ibf, uint32_t start, uint32_t count, void **buf, size_t *size);
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read_slice (void **buf, size_t *size, uint32_t start, uint32_t count, struct InvertibleBloomFilter *dst);
+
+
+/**
+ * Write an ibf.
+ * 
+ * @param ibf the ibf to write
+ * @param start with which bucket to start
+ * @param count how many buckets to write
+ * @param buf buffer to write the data to, will be updated to point to the
+ *            first byte after the written data
+ * @param size pointer to the size of the buffer, will be updated, can be NULL
+ */
+void
+ibf_write (const struct InvertibleBloomFilter *ibf, void **buf, size_t *size);
+
+
+/**
+ * Read an ibf.
+ *
+ * @param buf pointer to the buffer to write to, will point to first
+ *            byte after the written data
+ * @param size size of the buffer, will be updated
+ * @param start which bucket to start at
+ * @param count how many buckets to read
+ * @param dst ibf to write buckets to
+ * @return GNUNET_OK on success
+ */
+int
+ibf_read (void **buf, size_t *size, struct InvertibleBloomFilter *dst);
+
+
+/**
+ * Create a key from a hashcode.
+ *
+ * @param hash the hashcode
+ * @return a key
+ */
+struct IBF_Key
+ibf_key_from_hashcode (const struct GNUNET_HashCode *hash);
+
+
+/**
+ * Create a hashcode from a key, by replicating the key
+ * until the hascode is filled
+ *
+ * @param key the key
+ * @param dst hashcode to store the result in
+ */
+void
+ibf_hashcode_from_key (struct IBF_Key key, struct GNUNET_HashCode *dst);
+
+
 /**
  * Create an invertible bloom filter.
  *
  * @param size number of IBF buckets
- * @param hash_num number of buckets one element is hashed in
+ * @param hash_num number of buckets one element is hashed in, usually 3 or 4
  * @param salt salt for mingling hashes, different salt may
  *        result in less (or more) collisions
  * @return the newly created invertible bloom filter
  */
 struct InvertibleBloomFilter *
-ibf_create(unsigned int size, unsigned int hash_num, uint32_t salt);
+ibf_create (uint32_t size, uint8_t hash_num, uint32_t salt);
 
 
 /**
  * Insert an element into an IBF.
  *
  * @param ibf the IBF
- * @param id the element's hash code
+ * @param key the element's hash code
  */
 void
-ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *id);
+ibf_insert (struct InvertibleBloomFilter *ibf, struct IBF_Key key);
 
 
 /**
@@ -118,7 +216,7 @@ ibf_insert (struct InvertibleBloomFilter *ibf, const struct GNUNET_HashCode *id)
  * @param ibf2 IBF that will be subtracted from ibf1
  */
 void
-ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *ibf2);
+ibf_subtract (struct InvertibleBloomFilter *ibf1, const struct InvertibleBloomFilter *ibf2);
 
 
 /**
@@ -133,7 +231,7 @@ ibf_subtract (struct InvertibleBloomFilter *ibf1, struct InvertibleBloomFilter *
  *         GNUNET_SYSERR if the decoding has faile
  */
 int
-ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct GNUNET_HashCode *ret_id);
+ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct IBF_Key *ret_key);
 
 
 /**
@@ -142,7 +240,7 @@ ibf_decode (struct InvertibleBloomFilter *ibf, int *side, struct GNUNET_HashCode
  * @param ibf the IBF to copy
  */
 struct InvertibleBloomFilter *
-ibf_dup (struct InvertibleBloomFilter *ibf);
+ibf_dup (const struct InvertibleBloomFilter *ibf);
 
 /**
  * Destroy all resources associated with the invertible bloom filter.