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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
//! The n-of-m selection: within each window of `m` inputs, the `n`
//! whose position hashes lowest are selected; the body of `n_of` and
//! of its native lowering.
/// Core n-of-m evaluation: hash the input's position within its window
/// and check whether its rank falls within the selected n.
///
/// Algorithm: within each window of m consecutive inputs, hash each
/// position (0..m) and sort by hash. The n positions with the smallest
/// hashes are selected. To avoid sorting at runtime, we count how many
/// of the m positions hash lower than the current one — if fewer than
/// n do, this position is selected.
///
/// Preconditions: `1 <= m <= 65536` and `n <= m`. `n_of` states both
/// to the factory — `m` through a range constraint and the relation
/// through a node validator — so a program that names bad values is
/// refused before this runs, on every engine. They are not re-checked
/// here, because this is the inner loop of the native lowering as well
/// as of the body. The cost is `m` hashes a call, which is why `m` is
/// bounded.