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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use blake2b_simd::blake2b;
use std::mem;
pub const FEISTEL_ROUNDS: usize = 3;
pub type Index = u64;
pub type FeistelPrecomputed = (Index, Index, Index);
pub fn precompute(num_elements: Index) -> FeistelPrecomputed {
let mut next_pow4: Index = 4;
let mut log4 = 1;
while next_pow4 < num_elements {
next_pow4 *= 4;
log4 += 1;
}
let left_mask = ((1 << log4) - 1) << log4;
let right_mask = (1 << log4) - 1;
let half_bits = log4;
(left_mask, right_mask, half_bits)
}
pub fn permute(
num_elements: Index,
index: Index,
keys: &[Index],
precomputed: FeistelPrecomputed,
) -> Index {
let mut u = encode(index, keys, precomputed);
while u >= num_elements {
u = encode(u, keys, precomputed)
}
u
}
pub fn invert_permute(
num_elements: Index,
index: Index,
keys: &[Index],
precomputed: FeistelPrecomputed,
) -> Index {
let mut u = decode(index, keys, precomputed);
while u >= num_elements {
u = decode(u, keys, precomputed);
}
u
}
fn common_setup(index: Index, precomputed: FeistelPrecomputed) -> (Index, Index, Index, Index) {
let (left_mask, right_mask, half_bits) = precomputed;
let left = (index & left_mask) >> half_bits;
let right = index & right_mask;
(left, right, right_mask, half_bits)
}
fn encode(index: Index, keys: &[Index], precomputed: FeistelPrecomputed) -> Index {
let (mut left, mut right, right_mask, half_bits) = common_setup(index, precomputed);
for key in keys.iter().take(FEISTEL_ROUNDS) {
let (l, r) = (right, left ^ feistel(right, *key, right_mask));
left = l;
right = r;
}
(left << half_bits) | right
}
fn decode(index: Index, keys: &[Index], precomputed: FeistelPrecomputed) -> Index {
let (mut left, mut right, right_mask, half_bits) = common_setup(index, precomputed);
for i in (0..FEISTEL_ROUNDS).rev() {
let (l, r) = ((right ^ feistel(left, keys[i], right_mask)), left);
left = l;
right = r;
}
(left << half_bits) | right
}
const HALF_FEISTEL_BYTES: usize = mem::size_of::<Index>();
const FEISTEL_BYTES: usize = 2 * HALF_FEISTEL_BYTES;
fn feistel(right: Index, key: Index, right_mask: Index) -> Index {
let mut data: [u8; FEISTEL_BYTES] = [0; FEISTEL_BYTES];
let r = if FEISTEL_BYTES <= 8 {
data[0] = (right >> 24) as u8;
data[1] = (right >> 16) as u8;
data[2] = (right >> 8) as u8;
data[3] = right as u8;
data[4] = (key >> 24) as u8;
data[5] = (key >> 16) as u8;
data[6] = (key >> 8) as u8;
data[7] = key as u8;
let raw = blake2b(&data);
let hash = raw.as_bytes();
Index::from(hash[0]) << 24
| Index::from(hash[1]) << 16
| Index::from(hash[2]) << 8
| Index::from(hash[3])
} else {
data[0] = (right >> 56) as u8;
data[1] = (right >> 48) as u8;
data[2] = (right >> 40) as u8;
data[3] = (right >> 32) as u8;
data[4] = (right >> 24) as u8;
data[5] = (right >> 16) as u8;
data[6] = (right >> 8) as u8;
data[7] = right as u8;
data[8] = (key >> 56) as u8;
data[9] = (key >> 48) as u8;
data[10] = (key >> 40) as u8;
data[11] = (key >> 32) as u8;
data[12] = (key >> 24) as u8;
data[13] = (key >> 16) as u8;
data[14] = (key >> 8) as u8;
data[15] = key as u8;
let raw = blake2b(&data);
let hash = raw.as_bytes();
Index::from(hash[0]) << 56
| Index::from(hash[1]) << 48
| Index::from(hash[2]) << 40
| Index::from(hash[3]) << 32
| Index::from(hash[4]) << 24
| Index::from(hash[5]) << 16
| Index::from(hash[6]) << 8
| Index::from(hash[7])
};
r & right_mask
}
#[cfg(test)]
mod tests {
use super::*;
const BAD_NS: &[Index] = &[5, 6, 8, 12, 17];
fn encode_decode(n: Index, expect_success: bool) {
let mut failed = false;
let precomputed = precompute(n);
for i in 0..n {
let p = encode(i, &[1, 2, 3, 4], precomputed);
let v = decode(p, &[1, 2, 3, 4], precomputed);
let equal = i == v;
let in_range = p <= n;
if expect_success {
assert!(equal, "failed to permute (n = {})", n);
assert!(in_range, "output number is too big (n = {})", n);
} else {
if !equal || !in_range {
failed = true;
}
}
}
if !expect_success {
assert!(failed, "expected failure (n = {})", n);
}
}
#[test]
fn test_feistel_power_of_4() {
let mut n = 1;
for _ in 0..4 {
n *= 4;
encode_decode(n, true);
}
for i in BAD_NS.iter() {
encode_decode(*i, false);
}
}
#[test]
fn test_feistel_on_arbitrary_set() {
for n in BAD_NS.iter() {
let precomputed = precompute(*n as Index);
for i in 0..*n {
let p = permute(*n, i, &[1, 2, 3, 4], precomputed);
let v = invert_permute(*n, p, &[1, 2, 3, 4], precomputed);
assert_eq!(i, v, "failed to permute");
assert!(p <= *n, "output number is too big");
}
}
}
}