Merge branch 'maint-0.4.8'
[tor.git] / src / lib / crypt_ops / digestset.c
blobc5be83497bcd2c29b75df23de17cf5dae334f748
1 /* Copyright (c) 2018-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 /**
5 * \file digestset.c
6 * \brief Implementation for a set of digests
7 **/
9 #include "orconfig.h"
10 #include "lib/container/bloomfilt.h"
11 #include "lib/crypt_ops/crypto_rand.h"
12 #include "lib/defs/digest_sizes.h"
13 #include "lib/crypt_ops/digestset.h"
14 #include "ext/siphash.h"
16 /* Wrap our hash function to have the signature that the bloom filter
17 * needs. */
18 static uint64_t
19 bloomfilt_digest_hash(const struct sipkey *key,
20 const void *item)
22 return siphash24(item, DIGEST_LEN, key);
25 /**
26 * Allocate and return an digestset, suitable for holding up to
27 * <b>max_guess</b> distinct values.
29 digestset_t *
30 digestset_new(int max_guess)
32 uint8_t k[BLOOMFILT_KEY_LEN];
33 crypto_rand((void*)k, sizeof(k));
34 return bloomfilt_new(max_guess, bloomfilt_digest_hash, k);
37 /**
38 * Add <b>digest</b> to <b>set</b>.
40 * All future queries for <b>digest</b> in set will return true. Removing
41 * items is not possible.
43 void
44 digestset_add(digestset_t *set, const char *digest)
46 bloomfilt_add(set, digest);
49 /**
50 * Return true if <b>digest</b> is a member of <b>set</b>. (And probably,
51 * return false if <b>digest</b> is not a member of set.)
53 int
54 digestset_probably_contains(const digestset_t *set,
55 const char *digest)
57 return bloomfilt_probably_contains(set, digest);