1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2026 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to furnish to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/** Division-free, overflow-safe binomial randomness extractor.
 *
 * The provided code avoids data-dependent branches on the produced bits, making
 * it appropriate in settings where timing side-channels may matter.
 */

#ifndef EXTRACTOR_H
#define EXTRACTOR_H

#include <cstdint>
#include <optional>
#include <span>

struct UniformSpan {
    /** The number S of possible outcomes, taken modulo 2^64. */
    uint64_t span{1};
    /** The number V uniformly distributed in [0, S), taken modulo 2^64. */
    uint64_t value{0};
};

/** Encode a batch of binary events (0 or 1) into a UniformSpan. Constant-time
 *  in the event values. */
UniformSpan process_batch(std::span<const uint8_t> events) noexcept;

/** Encode a batch of events drawn from [0, m) into a UniformSpan. Constant-time
 *  in the event values. Requires m <= 256. */
UniformSpan process_batch_multi(std::span<const uint8_t> events, unsigned m) noexcept;

/** Encode a batch of binary events given as sorted 0-indexed positions of True
 *  events and the total event count n. Leaks information about the returned
 *  value through timing. */
UniformSpan process_batch_low_k(std::span<const unsigned> positions, unsigned n) noexcept;

/** Encode a batch of binary events using the precomputed factorial table.
 *  Requires n < 4096. Leaks information about the returned value through
 *  timing. */
UniformSpan process_batch_table(std::span<const unsigned> positions, unsigned n) noexcept;

/** Combine entropy from two uniform spans into one. */
UniformSpan merge(const UniformSpan& hi, const UniformSpan& lo) noexcept;

/** Extract one uniform bit from state, mutating it. Returns std::nullopt if no
 *  bit could be produced. Overflows in the input are allowed. */
std::optional<bool> extract_bit(UniformSpan& state) noexcept;

#endif // EXTRACTOR_H