p3-binary-dft 0.9.0-rc.1

Additive NTT over F_2-linear subspaces of the Plonky3 binary tower fields
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! The butterfly of `GF(2^64)` in the polynomial basis, over wide carryless-multiply registers.
//!
//! The scalar field packs nothing, so each product would be one 64-bit carryless multiply.
//!
//! A wide register instead multiplies a whole lane group by the same twiddle in two instructions.

use p3_binary_field::Poly64;

use super::packed_butterfly;
#[cfg(all(
    target_arch = "aarch64",
    target_endian = "little",
    target_feature = "aes"
))]
mod neon;

/// The butterfly over two runs of `GF(2^64)` elements.
///
/// Whole registers take the wide kernel where the target has one.
///
/// The tail, and every run on other targets, takes the scalar kernel.
///
/// # Panics
///
/// Panics if the two runs have different lengths.
#[inline]
pub(super) fn butterfly<const INVERSE: bool>(lo: &mut [Poly64], hi: &mut [Poly64], t: Poly64) {
    // Both kernels stop at the shorter run, so a mismatch would silently drop work.
    assert_eq!(lo.len(), hi.len(), "butterfly lengths differ");

    // A zero twiddle is a bare addition, which the scalar kernel already shortcuts.
    let covered = if t.to_bits() == 0 {
        0
    } else {
        register_prefix::<INVERSE>(lo, hi, t)
    };
    packed_butterfly::<Poly64, INVERSE>(&mut lo[covered..], &mut hi[covered..], t);
}

/// Cover complete ARM lane pairs with the fused kernel, then handle scalar tails.
#[inline]
pub(super) fn radix8<const INVERSE: bool>(rows: &mut [&mut [Poly64]; 8], t: &[Poly64; 7]) {
    assert!(
        rows.iter().all(|row| row.len() == rows[0].len()),
        "radix-8 row lengths differ"
    );
    #[cfg(all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "aes"
    ))]
    let covered = neon::radix8::<INVERSE>(rows, t);
    #[cfg(all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "aes"
    ))]
    {
        if covered < rows[0].len() {
            radix8_scalar_tail::<INVERSE>(rows, t, covered);
        }
    }

    #[cfg(not(all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "aes"
    )))]
    {
        super::radix8::<Poly64, INVERSE>(rows, t);
    }
}

/// Apply the remaining single column without invoking twelve empty-prefix kernels.
#[cfg(all(
    target_arch = "aarch64",
    target_endian = "little",
    target_feature = "aes"
))]
#[inline]
fn radix8_scalar_tail<const INVERSE: bool>(
    rows: &mut [&mut [Poly64]; 8],
    t: &[Poly64; 7],
    column: usize,
) {
    let mut values: [Poly64; 8] = rows.each_ref().map(|row| row[column]);
    macro_rules! step {
        ($lo:literal, $hi:literal, $t:literal) => {{
            if INVERSE {
                values[$hi] += values[$lo];
            }
            if t[$t].to_bits() != 0 {
                values[$lo] += t[$t] * values[$hi];
            }
            if !INVERSE {
                values[$hi] += values[$lo];
            }
        }};
    }
    if INVERSE {
        step!(0, 1, 3);
        step!(2, 3, 4);
        step!(4, 5, 5);
        step!(6, 7, 6);
        step!(0, 2, 1);
        step!(1, 3, 1);
        step!(4, 6, 2);
        step!(5, 7, 2);
        step!(0, 4, 0);
        step!(1, 5, 0);
        step!(2, 6, 0);
        step!(3, 7, 0);
    } else {
        step!(0, 4, 0);
        step!(1, 5, 0);
        step!(2, 6, 0);
        step!(3, 7, 0);
        step!(0, 2, 1);
        step!(1, 3, 1);
        step!(4, 6, 2);
        step!(5, 7, 2);
        step!(0, 1, 3);
        step!(2, 3, 4);
        step!(4, 5, 5);
        step!(6, 7, 6);
    }
    for (row, value) in rows.iter_mut().zip(values) {
        row[column] = value;
    }
}

/// The widest carryless-multiply register the build enables.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx512f",
    target_feature = "vpclmulqdq"
))]
type Wide = core::arch::x86_64::__m512i;

/// The widest carryless-multiply register the build enables.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx2",
    target_feature = "vpclmulqdq",
    not(target_feature = "avx512f")
))]
type Wide = core::arch::x86_64::__m256i;

/// Apply the butterfly to the whole registers of two runs.
///
/// # Returns
///
/// The number of elements covered, a multiple of the lane count.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx2",
    target_feature = "vpclmulqdq"
))]
#[inline]
fn register_prefix<const INVERSE: bool>(lo: &mut [Poly64], hi: &mut [Poly64], t: Poly64) -> usize {
    const LANES: usize = <Wide as ClmulRegister>::LANES;
    let t = Wide::splat(t.to_bits());

    // Whole registers only, so the remainder is the caller's to finish.
    let (lo, _) = lo.as_chunks_mut::<LANES>();
    let (hi, _) = hi.as_chunks_mut::<LANES>();
    let covered = lo.len() * LANES;

    for (lo, hi) in lo.iter_mut().zip(hi) {
        // SAFETY: each chunk is one register of transparent 64-bit words.
        unsafe {
            let u = Wide::load(lo.as_ptr().cast());
            let v = Wide::load(hi.as_ptr().cast());
            if INVERSE {
                // Recover the upper value, then remove its scaled copy from the lower one.
                let v = v.xor(u);
                Wide::store(hi.as_mut_ptr().cast(), v);
                Wide::store(lo.as_mut_ptr().cast(), u.xor(mul(v, t)));
            } else {
                // Scale the upper value into the lower one, then add the result to the upper one.
                let u = u.xor(mul(v, t));
                Wide::store(lo.as_mut_ptr().cast(), u);
                Wide::store(hi.as_mut_ptr().cast(), v.xor(u));
            }
        }
    }
    covered
}

/// The NEON kernel covers lane pairs, leaving an odd scalar tail.
#[cfg(all(
    target_arch = "aarch64",
    target_endian = "little",
    target_feature = "aes"
))]
#[inline]
fn register_prefix<const INVERSE: bool>(lo: &mut [Poly64], hi: &mut [Poly64], t: Poly64) -> usize {
    neon::butterfly::<INVERSE>(lo, hi, t)
}

/// Without a wide carryless multiply there is no register prefix.
#[cfg(not(any(
    all(
        target_arch = "x86_64",
        target_feature = "avx2",
        target_feature = "vpclmulqdq"
    ),
    all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "aes"
    )
)))]
#[inline]
const fn register_prefix<const INVERSE: bool>(
    _lo: &mut [Poly64],
    _hi: &mut [Poly64],
    _t: Poly64,
) -> usize {
    0
}

/// Multiply a register of elements by one broadcast twiddle, modulo `x^64 + x^4 + x^3 + x + 1`.
///
/// # Algorithm
///
/// The register kernel returns each 128-bit product as two words in element order.
///
/// The field polynomial gives `x^64 = r(x)` with `r = 1 + x + x^3 + x^4 = (1 + x)(1 + x^3)`.
///
/// So the high word folds back through the two factors, each a shift:
///
/// - The product is `lo + hi * x^64`, and `hi` has degree at most 62.
/// - `a = hi * (1 + x)` then has degree at most 63, so it fits one word.
/// - `a * (1 + x^3)` is `a + (a << 3)` in the word, plus `spill = a >> 61` times `x^64`.
/// - The spill has degree at most 2, so it folds back as `spill * (1 + x) * (1 + x^3)` with no bit lost.
/// - Writing `c = a + spill * (1 + x)`, the result is `lo + c + (c << 3)`.
///
/// Shifts stand in for more carryless multiplies, which are the scarcer unit.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx2",
    target_feature = "vpclmulqdq"
))]
#[inline(always)]
fn mul<L: ClmulRegister>(v: L, t: L) -> L {
    let (lo, hi) = v.products(t);

    // Multiplying by 1 + x: the product of two degree-63 polynomials leaves bit 63 of hi clear.
    let a = hi.xor(hi.double());

    // The top three bits of a, which the shift by three pushes out of the word.
    let spill = a.shr61();

    // Fold them back scaled by 1 + x, then apply the shared factor 1 + x^3 to both.
    let c = a.xor3(spill, spill.double());
    lo.xor3(c, c.shl3())
}

/// A register of 64-bit words with a carryless multiply.
///
/// Every operation acts on each word alone, except the product.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx2",
    target_feature = "vpclmulqdq"
))]
trait ClmulRegister: Copy {
    /// Words one register holds.
    const LANES: usize;

    /// Every word set to the same value.
    fn splat(word: u64) -> Self;

    /// Read one register, at any alignment.
    ///
    /// # Safety
    ///
    /// The address must be readable for one register.
    unsafe fn load(from: *const u64) -> Self;

    /// Write one register, at any alignment.
    ///
    /// # Safety
    ///
    /// The address must be writable for one register.
    unsafe fn store(to: *mut u64, value: Self);

    /// Bitwise exclusive or.
    fn xor(self, other: Self) -> Self;

    /// Three-way bitwise exclusive or.
    fn xor3(self, b: Self, c: Self) -> Self;

    /// Each word shifted left by one, as an addition.
    fn double(self) -> Self;

    /// Each word shifted right by 61.
    fn shr61(self) -> Self;

    /// Each word shifted left by 3.
    fn shl3(self) -> Self;

    /// The carryless products of every word with the broadcast twiddle.
    ///
    /// Returns the low halves and the high halves, each in element order.
    fn products(self, t: Self) -> (Self, Self);
}

/// The 512-bit register, over `AVX-512F` and `VPCLMULQDQ`.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx512f",
    target_feature = "vpclmulqdq"
))]
mod avx512 {
    use core::arch::x86_64::{
        __m512i, _mm512_add_epi64, _mm512_clmulepi64_epi128, _mm512_loadu_si512, _mm512_set1_epi64,
        _mm512_slli_epi64, _mm512_srli_epi64, _mm512_storeu_si512, _mm512_ternarylogic_epi64,
        _mm512_unpackhi_epi64, _mm512_unpacklo_epi64, _mm512_xor_si512,
    };

    use super::ClmulRegister;

    // SAFETY: every intrinsic below needs only the instruction sets this module is gated on.
    impl ClmulRegister for __m512i {
        const LANES: usize = 8;

        #[inline(always)]
        fn splat(word: u64) -> Self {
            unsafe { _mm512_set1_epi64(word as i64) }
        }

        #[inline(always)]
        unsafe fn load(from: *const u64) -> Self {
            // SAFETY: the readability of the address is the caller's obligation.
            unsafe { _mm512_loadu_si512(from.cast()) }
        }

        #[inline(always)]
        unsafe fn store(to: *mut u64, value: Self) {
            // SAFETY: the writability of the address is the caller's obligation.
            unsafe { _mm512_storeu_si512(to.cast(), value) }
        }

        #[inline(always)]
        fn xor(self, other: Self) -> Self {
            unsafe { _mm512_xor_si512(self, other) }
        }

        #[inline(always)]
        fn xor3(self, b: Self, c: Self) -> Self {
            // 0x96 is the truth table of a three-way exclusive or.
            unsafe { _mm512_ternarylogic_epi64::<0x96>(self, b, c) }
        }

        #[inline(always)]
        fn double(self) -> Self {
            unsafe { _mm512_add_epi64(self, self) }
        }

        #[inline(always)]
        fn shr61(self) -> Self {
            unsafe { _mm512_srli_epi64::<61>(self) }
        }

        #[inline(always)]
        fn shl3(self) -> Self {
            unsafe { _mm512_slli_epi64::<3>(self) }
        }

        #[inline(always)]
        fn products(self, t: Self) -> (Self, Self) {
            unsafe {
                // The multiply reads one word of each 128-bit lane, so even and odd words take one each.
                //
                //     even = [ p_0 | p_2 | p_4 | p_6 ]      each p_i a 128-bit product (lo_i, hi_i)
                //     odd  = [ p_1 | p_3 | p_5 | p_7 ]
                let even = _mm512_clmulepi64_epi128::<0x00>(self, t);
                let odd = _mm512_clmulepi64_epi128::<0x01>(self, t);

                // Two unpacks sort the halves back into element order.
                //
                //     lo = [ lo_0 lo_1 | lo_2 lo_3 | ... ]
                //     hi = [ hi_0 hi_1 | hi_2 hi_3 | ... ]
                (
                    _mm512_unpacklo_epi64(even, odd),
                    _mm512_unpackhi_epi64(even, odd),
                )
            }
        }
    }
}

/// The 256-bit register, over `AVX2` and `VPCLMULQDQ`, for cores without `AVX-512`.
#[cfg(all(
    target_arch = "x86_64",
    target_feature = "avx2",
    target_feature = "vpclmulqdq",
    not(target_feature = "avx512f")
))]
mod avx2 {
    use core::arch::x86_64::{
        __m256i, _mm256_add_epi64, _mm256_clmulepi64_epi128, _mm256_loadu_si256,
        _mm256_set1_epi64x, _mm256_slli_epi64, _mm256_srli_epi64, _mm256_storeu_si256,
        _mm256_unpackhi_epi64, _mm256_unpacklo_epi64, _mm256_xor_si256,
    };

    use super::ClmulRegister;

    // SAFETY: every intrinsic below needs only the instruction sets this module is gated on.
    impl ClmulRegister for __m256i {
        const LANES: usize = 4;

        #[inline(always)]
        fn splat(word: u64) -> Self {
            unsafe { _mm256_set1_epi64x(word as i64) }
        }

        #[inline(always)]
        unsafe fn load(from: *const u64) -> Self {
            // SAFETY: the readability of the address is the caller's obligation.
            unsafe { _mm256_loadu_si256(from.cast()) }
        }

        #[inline(always)]
        unsafe fn store(to: *mut u64, value: Self) {
            // SAFETY: the writability of the address is the caller's obligation.
            unsafe { _mm256_storeu_si256(to.cast(), value) }
        }

        #[inline(always)]
        fn xor(self, other: Self) -> Self {
            unsafe { _mm256_xor_si256(self, other) }
        }

        #[inline(always)]
        fn xor3(self, b: Self, c: Self) -> Self {
            // No ternary logic below AVX-512, so two plain exclusive ors.
            unsafe { _mm256_xor_si256(_mm256_xor_si256(self, b), c) }
        }

        #[inline(always)]
        fn double(self) -> Self {
            unsafe { _mm256_add_epi64(self, self) }
        }

        #[inline(always)]
        fn shr61(self) -> Self {
            unsafe { _mm256_srli_epi64::<61>(self) }
        }

        #[inline(always)]
        fn shl3(self) -> Self {
            unsafe { _mm256_slli_epi64::<3>(self) }
        }

        #[inline(always)]
        fn products(self, t: Self) -> (Self, Self) {
            unsafe {
                // Same layout as the 512-bit register, over two 128-bit lanes.
                let even = _mm256_clmulepi64_epi128::<0x00>(self, t);
                let odd = _mm256_clmulepi64_epi128::<0x01>(self, t);
                (
                    _mm256_unpacklo_epi64(even, odd),
                    _mm256_unpackhi_epi64(even, odd),
                )
            }
        }
    }
}