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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/// Small-element (ternary) encoding and decoding.
pub mod encoding {
/// Encode a small polynomial `f` of length `p` into `small_encode_size` bytes.
///
/// Packs 4 trits per byte (each trit shifted to {0,1,2} by adding 1).
/// The last byte holds `f[p-1] + 1`.
#[allow(clippy::cast_sign_loss)]
pub fn encode(f: &[i8], p: usize, small_encode_size: usize) -> Vec<u8> {
let mut c = vec![0u8; small_encode_size];
for (byte, chunk) in c[..small_encode_size - 1].iter_mut().zip(f.chunks(4)) {
let mut c0 = chunk[0] + 1;
c0 += (chunk[1] + 1) << 2;
c0 += (chunk[2] + 1) << 4;
c0 += (chunk[3] + 1) << 6;
*byte = c0 as u8;
}
c[small_encode_size - 1] = (f[p - 1] + 1) as u8;
c
}
/// Decode `small_encode_size` bytes into a small polynomial of length `p`.
///
/// Inverse of [`encode`]: unpacks 4 trits per byte, last element from last byte.
#[allow(clippy::cast_possible_wrap)]
pub fn decode(c: &[u8], p: usize) -> Vec<i8> {
let small_encode_size = c.len();
let mut f = vec![0i8; p];
for (byte, chunk) in c[..small_encode_size - 1].iter().zip(f.chunks_mut(4)) {
let mut c0 = *byte;
chunk[0] = ((c0 & 3) as i8) - 1;
c0 >>= 2;
chunk[1] = ((c0 & 3) as i8) - 1;
c0 >>= 2;
chunk[2] = ((c0 & 3) as i8) - 1;
c0 >>= 2;
chunk[3] = ((c0 & 3) as i8) - 1;
}
f[p - 1] = ((c[small_encode_size - 1] & 3) as i8) - 1;
f
}
}
/// Random polynomial generation and constant-time sorting.
pub mod random {
use rand::Rng;
use rand::RngExt;
/// Branchless constant-time min/max swap (djbsort int32_minmax).
/// Operates on a slice with two indices to avoid borrow issues.
///
/// Uses wrapping i32 subtraction (matching the original djbsort algorithm)
/// with an XOR fixup for overflow. The `>> 31` extracts the sign bit.
#[inline(always)]
#[allow(clippy::cast_possible_truncation)]
fn int32_minmax(x: &mut [i32], i: usize, j: usize) {
let ab = x[j] ^ x[i];
let mut c = x[j].wrapping_sub(x[i]);
c ^= ab & (c ^ x[j]);
c >>= 31;
c &= ab;
x[i] ^= c;
x[j] ^= c;
}
/// Batcher bitonic sort on `n` elements of `x`, dispatching to SIMD when available.
#[allow(unsafe_code)]
pub fn sort(x: &mut [i32], n: usize) {
#[cfg(all(
target_arch = "x86_64",
target_feature = "avx2",
not(feature = "force-scalar")
))]
// SAFETY: AVX2 verified by cfg
unsafe {
return sort_avx2(x, n);
}
#[cfg(all(target_arch = "aarch64", not(feature = "force-scalar")))]
// SAFETY: NEON is baseline on aarch64
unsafe {
return sort_neon(x, n);
}
#[allow(unreachable_code)]
sort_scalar(x, n);
}
fn sort_scalar(x: &mut [i32], n: usize) {
if n < 2 {
return;
}
let mut top = 1;
while top < (n - top) {
top += top;
}
let mut p = top;
while p > 0 {
for i in 0..(n - p) {
if i & p == 0 {
int32_minmax(x, i, i + p);
}
}
let mut q = top;
while q > p {
for i in 0..(n - q) {
if i & p == 0 {
int32_minmax(x, i + p, i + q);
}
}
q >>= 1;
}
p >>= 1;
}
}
/// AVX2-accelerated Batcher bitonic sort.
/// Uses _mm256_min/max_epi32 for 8 parallel comparators when stride >= 8.
#[cfg(all(
target_arch = "x86_64",
target_feature = "avx2",
not(feature = "force-scalar")
))]
#[target_feature(enable = "avx2")]
#[allow(unsafe_code)]
unsafe fn sort_avx2(x: &mut [i32], n: usize) {
unsafe {
if n < 2 {
return;
}
let mut top = 1;
while top < (n - top) {
top += top;
}
let mut p = top;
while p > 0 {
// First pass: comparators at stride p
minmax_pass_avx2(x, n, p, 0, p);
// Sub-passes
let mut q = top;
while q > p {
minmax_pass_avx2(x, n, p, p, q);
q >>= 1;
}
p >>= 1;
}
}
}
/// Process one pass of comparators: minmax(x[i+off0], x[i+off1])
/// for all i in 0..(n-off1) where i & p_mask == 0.
#[cfg(all(
target_arch = "x86_64",
target_feature = "avx2",
not(feature = "force-scalar")
))]
#[target_feature(enable = "avx2")]
#[allow(unsafe_code)]
unsafe fn minmax_pass_avx2(x: &mut [i32], n: usize, p_mask: usize, off0: usize, off1: usize) {
unsafe {
use core::arch::x86_64::*;
let end = n.saturating_sub(off1);
if p_mask >= 8 {
// When p_mask >= 8, the condition i & p_mask == 0 selects contiguous
// blocks of p_mask elements. Process 8 at a time with SIMD.
let mut i = 0;
while i < end {
if i & p_mask == 0 {
let block_end = (i + p_mask).min(end);
let mut j = i;
while j + 8 <= block_end {
let a = _mm256_loadu_si256(x.as_ptr().add(j + off0) as *const __m256i);
let b = _mm256_loadu_si256(x.as_ptr().add(j + off1) as *const __m256i);
_mm256_storeu_si256(
x.as_mut_ptr().add(j + off0) as *mut __m256i,
_mm256_min_epi32(a, b),
);
_mm256_storeu_si256(
x.as_mut_ptr().add(j + off1) as *mut __m256i,
_mm256_max_epi32(a, b),
);
j += 8;
}
// Scalar remainder for this block
while j < block_end {
int32_minmax(x, j + off0, j + off1);
j += 1;
}
i = block_end + p_mask; // skip the next block (i & p_mask != 0)
} else {
i += 1;
}
}
} else {
// Small strides: scalar
for i in 0..end {
if i & p_mask == 0 {
int32_minmax(x, i + off0, i + off1);
}
}
}
}
}
/// NEON-accelerated Batcher bitonic sort.
/// Uses vminq_s32/vmaxq_s32 for 4 parallel comparators when stride >= 4.
#[cfg(all(target_arch = "aarch64", not(feature = "force-scalar")))]
#[allow(unsafe_code)]
unsafe fn sort_neon(x: &mut [i32], n: usize) {
unsafe {
if n < 2 {
return;
}
let mut top = 1;
while top < (n - top) {
top += top;
}
let mut p = top;
while p > 0 {
// First pass: comparators at stride p
minmax_pass_neon(x, n, p, 0, p);
// Sub-passes
let mut q = top;
while q > p {
minmax_pass_neon(x, n, p, p, q);
q >>= 1;
}
p >>= 1;
}
}
}
/// Process one pass of comparators with NEON.
#[cfg(all(target_arch = "aarch64", not(feature = "force-scalar")))]
#[allow(unsafe_code)]
unsafe fn minmax_pass_neon(x: &mut [i32], n: usize, p_mask: usize, off0: usize, off1: usize) {
unsafe {
use core::arch::aarch64::*;
let end = n.saturating_sub(off1);
if p_mask >= 4 {
let mut i = 0;
while i < end {
if i & p_mask == 0 {
let block_end = (i + p_mask).min(end);
let mut j = i;
while j + 4 <= block_end {
let a = vld1q_s32(x.as_ptr().add(j + off0));
let b = vld1q_s32(x.as_ptr().add(j + off1));
vst1q_s32(x.as_mut_ptr().add(j + off0), vminq_s32(a, b));
vst1q_s32(x.as_mut_ptr().add(j + off1), vmaxq_s32(a, b));
j += 4;
}
// Scalar remainder for this block
while j < block_end {
int32_minmax(x, j + off0, j + off1);
j += 1;
}
i = block_end + p_mask;
} else {
i += 1;
}
}
} else {
// Small strides: scalar
for i in 0..end {
if i & p_mask == 0 {
int32_minmax(x, i + off0, i + off1);
}
}
}
}
}
/// Fill `g` with random small elements in {-1, 0, 1}.
#[allow(clippy::cast_sign_loss)]
pub fn random_small(g: &mut [i8], rng: &mut impl Rng) {
for val in g.iter_mut() {
let r: i32 = rng.random();
*val = ((((1_073_741_823 & (r as u32)) * 3) >> 30) as i8) - 1;
}
}
/// Unsigned sort: XOR with 0x80000000, signed sort, XOR back.
/// Matches PQClean's crypto_sort_uint32.
#[allow(clippy::cast_possible_wrap)]
fn sort_uint32(x: &mut [i32], n: usize) {
for val in x.iter_mut().take(n) {
*val ^= 0x80000000u32 as i32;
}
sort(x, n);
for val in x.iter_mut().take(n) {
*val ^= 0x80000000u32 as i32;
}
}
/// Generate a random ternary polynomial with exactly `w` non-zero entries out of `p`.
///
/// The first `w` positions get weight (odd), the remaining `p - w` get zero (even tag),
/// then a constant-time sort shuffles them.
#[allow(clippy::cast_possible_wrap)]
pub fn random_tsmall(f: &mut [i8], p: usize, w: usize, rng: &mut impl Rng) {
let mut r = vec![0i32; p];
for val in r.iter_mut() {
*val = rng.random();
}
for val in r[..w].iter_mut() {
*val &= -2;
}
for val in r[w..p].iter_mut() {
*val = (*val & -3) | 1
}
sort_uint32(&mut r, p);
for (fv, &rv) in f.iter_mut().zip(r.iter()) {
*fv = ((rv & 3) as i8) - 1;
}
}
}