@@ -60,16 +60,19 @@ MINISKETCH_API uint32_t minisketch_implementation_max(void);
60
60
*/
61
61
MINISKETCH_API int minisketch_implementation_supported (uint32_t bits, uint32_t implementation);
62
62
63
- /* * Construct a sketch for a given element size, implementation and capacity .
63
+ /* * Construct a sketch for a given element size, implementation, capacity, and RNG seed .
64
64
*
65
65
* If the combination of `bits` and `implementation` is unavailable, or when
66
66
* OOM occurs, NULL is returned. If minisketch_implementation_supported
67
67
* returns 1 for the specified bits and implementation, this will always succeed
68
68
* (except when allocation fails).
69
69
*
70
+ * To protect against bad performance on maliciously-created sketches, it is
71
+ * to use strong randomness for the provided seed value.
72
+ *
70
73
* If the result is not NULL, it must be destroyed using minisketch_destroy.
71
74
*/
72
- MINISKETCH_API minisketch* minisketch_create (uint32_t bits, uint32_t implementation, size_t capacity);
75
+ MINISKETCH_API minisketch* minisketch_create (uint32_t bits, uint32_t implementation, size_t capacity, uint64_t seed );
73
76
74
77
/* * Get the element size of a sketch in bits. */
75
78
MINISKETCH_API uint32_t minisketch_bits (const minisketch* sketch);
@@ -82,14 +85,11 @@ MINISKETCH_API uint32_t minisketch_implementation(const minisketch* sketch);
82
85
83
86
/* * Set the seed for randomizing algorithm choices to a fixed value.
84
87
*
85
- * By default, sketches are initialized with a random seed. This is important
86
- * to avoid scenarios where an attacker could force worst-case behavior.
87
- *
88
- * This function initializes the seed to a user-provided value (any 64-bit
89
- * integer is acceptable, regardless of field size).
88
+ * This is equivalent to recreating the sketch with a different RNG seed.
90
89
*
91
90
* When seed is -1, a fixed internal value with predictable behavior is
92
- * used. It is only intended for testing.
91
+ * used. It is only intended for testing. Note that minisketch_create does
92
+ * assign special meaning to seed = -1.
93
93
*/
94
94
MINISKETCH_API void minisketch_set_seed (minisketch* sketch, uint64_t seed);
95
95
@@ -263,16 +263,16 @@ class Minisketch
263
263
* ImplementationSupported(), or OOM occurs internally, an invalid Minisketch
264
264
* object will be constructed. Use operator bool() to check that this isn't the
265
265
* case before performing any other operations. */
266
- Minisketch (uint32_t bits, uint32_t implementation, size_t capacity) noexcept
266
+ Minisketch (uint32_t bits, uint32_t implementation, size_t capacity, uint64_t seed ) noexcept
267
267
{
268
- m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create (bits, implementation, capacity));
268
+ m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create (bits, implementation, capacity, seed ));
269
269
}
270
270
271
271
/* * Create a Minisketch object sufficiently large for the specified number of elements at given fpbits.
272
272
* It may construct an invalid object, which you may need to check for. */
273
- static Minisketch CreateFP (uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept
273
+ static Minisketch CreateFP (uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits, uint64_t seed ) noexcept
274
274
{
275
- return Minisketch (bits, implementation, ComputeCapacity (bits, max_elements, fpbits));
275
+ return Minisketch (bits, implementation, ComputeCapacity (bits, max_elements, fpbits), seed );
276
276
}
277
277
278
278
/* * Return the field size for a (valid) Minisketch object. */
0 commit comments