p3-binary-field 0.9.0-rc.1

Characteristic-2 tower fields GF(2) ⊂ GF(4) ⊂ … ⊂ GF(2^128) for Plonky3
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
//! Polynomial-basis arithmetic in `GF(2)[x] / (x^64 + x^4 + x^3 + x + 1)`.
//!
//! One carryless product covers a whole multiplication, and no routine reads a table.

use super::sqrt::compact_even;
use super::{clmul_64x64, reduce_64};

/// The square root of the polynomial variable modulo this field's polynomial.
const ROOT_X: u64 = 0xffff_ffff_0000_000a;

/// Multiplication, taking and returning the polynomial representation.
//
// Only the composed route is `const`, so the signature stays uniform across targets.
#[allow(clippy::missing_const_for_fn)]
#[inline]
pub(crate) fn poly_mul_64(a: u64, b: u64) -> u64 {
    #[cfg(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ))]
    {
        super::register::poly_mul_64::<super::Register>(a, b)
    }
    #[cfg(not(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    )))]
    {
        composed_mul_64(a, b)
    }
}

/// Squaring, taking and returning the polynomial representation.
#[allow(clippy::missing_const_for_fn)]
#[inline]
pub(crate) fn poly_square_64(a: u64) -> u64 {
    #[cfg(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ))]
    {
        super::register::poly_square_64::<super::Register>(a)
    }
    #[cfg(not(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    )))]
    {
        composed_square_64(a)
    }
}

/// Sum unreduced products before paying for one reduction.
#[inline]
pub(crate) fn poly_dot_64(pairs: impl Iterator<Item = (u64, u64)>) -> u64 {
    #[cfg(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ))]
    {
        super::register::poly_dot_64::<super::Register>(pairs)
    }
    #[cfg(not(any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    )))]
    {
        composed_dot_64(pairs)
    }
}

/// Multiplication assembled from the shared carryless product and the shared fold.
///
/// Compiled everywhere, so its tests run even where a backend supersedes it.
//
// Only one carryless-product backend is `const`, so the signature stays uniform.
#[allow(clippy::missing_const_for_fn)]
#[cfg_attr(
    any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ),
    allow(dead_code)
)]
#[inline]
fn composed_mul_64(a: u64, b: u64) -> u64 {
    reduce_64(clmul_64x64(a, b))
}

/// Squaring assembled the same way.
#[allow(clippy::missing_const_for_fn)]
#[cfg_attr(
    any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ),
    allow(dead_code)
)]
#[inline]
fn composed_square_64(a: u64) -> u64 {
    reduce_64(clmul_64x64(a, a))
}

/// A dot product assembled the same way.
#[cfg_attr(
    any(
        all(target_arch = "x86_64", target_feature = "pclmulqdq"),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "aes"
        ),
    ),
    allow(dead_code)
)]
#[inline]
fn composed_dot_64(pairs: impl Iterator<Item = (u64, u64)>) -> u64 {
    // Reduction is `F_2`-linear, so the whole sum folds the modulus once.
    reduce_64(pairs.fold(0, |sum, (a, b)| sum ^ clmul_64x64(a, b)))
}

/// Bits `0 .. 32` of `v`, each moved to twice its position: the carryless square of `v`.
#[cfg_attr(
    not(any(
        all(
            target_arch = "x86_64",
            target_feature = "pclmulqdq",
            target_feature = "gfni",
            target_feature = "avx512f",
            target_feature = "avx512bw",
            target_feature = "avx512vbmi"
        ),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "neon"
        ),
    )),
    allow(dead_code)
)]
const fn spread_32(v: u64) -> u64 {
    let mut out = 0;
    let mut i = 0;
    while i < 32 {
        // Bit i of v becomes the coefficient of x^(2i), since (x^i)^2 = x^(2i).
        out |= ((v >> i) & 1) << (2 * i);
        i += 1;
    }
    out
}

/// `x^(2^k)`, one squaring at a time, evaluated at compile time.
///
/// The bit-matrix backends build their columns from it.
#[cfg_attr(
    not(any(
        all(
            target_arch = "x86_64",
            target_feature = "pclmulqdq",
            target_feature = "gfni",
            target_feature = "avx512f",
            target_feature = "avx512bw",
            target_feature = "avx512vbmi"
        ),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "neon"
        ),
    )),
    allow(dead_code)
)]
pub(super) const fn repeated_square(mut x: u64, k: usize) -> u64 {
    let mut i = 0;
    while i < k {
        // The carryless square of a 64-bit value is its two halves, each spread.
        let square = (spread_32(x >> 32) as u128) << 64 | spread_32(x) as u128;
        x = reduce_64(square);
        i += 1;
    }
    x
}

/// The longest run that plain `PMULL` squarings finish sooner than the bit matrix.
///
/// The inversion chain takes two runs of this length, and four longer ones.
#[cfg(all(
    target_arch = "aarch64",
    target_endian = "little",
    target_feature = "neon"
))]
const SHORT_RUN: usize = 3;

/// Squaring repeated a fixed number of times.
///
/// Squaring is `F_2`-linear, so the whole power is one fixed bit matrix.
///
/// - With `GFNI`, that matrix is eight affine products.
/// - On AArch64, it is 64 masked columns summed by exclusive or, except for the shortest runs.
/// - Otherwise it is `K` dependent squarings.
#[inline]
fn square_times<const K: usize>(x: u64) -> u64 {
    #[cfg(all(
        target_arch = "x86_64",
        target_feature = "pclmulqdq",
        target_feature = "gfni",
        target_feature = "avx512f",
        target_feature = "avx512bw",
        target_feature = "avx512vbmi"
    ))]
    {
        // One broadcast, eight affine products and a byte permute, for any K.
        super::x86_64::square_times::<K>(x)
    }
    #[cfg(all(
        target_arch = "aarch64",
        target_endian = "little",
        target_feature = "neon"
    ))]
    {
        // Three `PMULL` squarings finish before the matrix does.
        //
        // Without `aes` a squaring is software, and the matrix wins at every K.
        if cfg!(target_feature = "aes") && K <= SHORT_RUN {
            return (0..K).fold(x, |y, _| poly_square_64(y));
        }

        // Thirty-two masked register pairs, for any K.
        super::aarch64::square_times::<K>(x)
    }
    #[cfg(not(any(
        all(
            target_arch = "x86_64",
            target_feature = "pclmulqdq",
            target_feature = "gfni",
            target_feature = "avx512f",
            target_feature = "avx512bw",
            target_feature = "avx512vbmi"
        ),
        all(
            target_arch = "aarch64",
            target_endian = "little",
            target_feature = "neon"
        ),
    )))]
    {
        // K dependent squarings, each one carryless product and one fold.
        (0..K).fold(x, |y, _| poly_square_64(y))
    }
}

/// The square root, which every element of a binary field has exactly one of.
///
/// Splitting by the parity of the exponents gives it in one field product:
///
/// ```text
///     a        = even(x)^2 + x * odd(x)^2
///     sqrt(a)  = even(x) + sqrt(x) * odd(x)
/// ```
#[inline]
pub(crate) fn poly_sqrt_64(a: u64) -> u64 {
    let even = compact_even(a);
    let odd = compact_even(a >> 1);
    even ^ poly_mul_64(ROOT_X, odd)
}

/// The inverse of a nonzero element, sending zero to zero.
///
/// Itoh-Tsujii on the exponent `2^64 - 2`, writing `b_k` for `x^(2^k - 1)`:
///
/// ```text
///     b_(a+b)  = b_a^(2^b) * b_b
///     chain      1, 2, 3, 6, 12, 24, 48, 60, 63
/// ```
///
/// Nine exponents is eight steps, so eight products and sixty-three squarings.
///
/// Where the target allows, each run of squarings is one bit-matrix product instead.
///
/// None of them is indexed by the operand.
#[inline]
pub(crate) fn poly_inverse_64(x: u64) -> u64 {
    let b2 = poly_mul_64(poly_square_64(x), x);
    let b3 = poly_mul_64(poly_square_64(b2), x);
    let b6 = poly_mul_64(square_times::<3>(b3), b3);

    // Doubling the exponent index reuses the same intermediate on both sides.
    let b12 = poly_mul_64(square_times::<6>(b6), b6);
    let b24 = poly_mul_64(square_times::<12>(b12), b12);
    let b48 = poly_mul_64(square_times::<24>(b24), b24);

    // Finish 48 + 12 = 60, then 60 + 3 = 63.
    let b60 = poly_mul_64(square_times::<12>(b48), b12);
    let b63 = poly_mul_64(square_times::<3>(b60), b3);

    // One more squaring turns `2^63 - 1` into `2^64 - 2`.
    poly_square_64(b63)
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::{
        ROOT_X, composed_dot_64, composed_mul_64, composed_square_64, poly_dot_64, poly_inverse_64,
        poly_mul_64, poly_sqrt_64, poly_square_64,
    };

    /// Multiplication from the modulus alone, with no carryless product and no fold.
    fn schoolbook(a: u64, b: u64) -> u64 {
        let mut acc = 0u64;
        for i in (0..64).rev() {
            let overflowed = acc & (1 << 63) != 0;
            acc <<= 1;
            if overflowed {
                acc ^= 0b1_1011;
            }
            if (b >> i) & 1 == 1 {
                acc ^= a;
            }
        }
        acc
    }

    #[test]
    fn the_modulus_reduces_the_way_the_polynomial_says() {
        // x^63 * x = x^64 = x^4 + x^3 + x + 1, the tail spelled 0b1_1011.
        assert_eq!(poly_mul_64(1 << 63, 2), 0b1_1011);

        // x * x = x^2, nowhere near the modulus.
        assert_eq!(poly_mul_64(2, 2), 4);

        // (x + 1)^2 = x^2 + 1, since the cross term doubles to zero.
        assert_eq!(poly_square_64(3), 5);
    }

    #[test]
    fn the_square_is_linear_on_every_basis_vector() {
        // Squaring is `F_2`-linear in characteristic 2, so 64 vectors settle all 2^64 inputs.
        for i in 0..64 {
            let a = 1u64 << i;
            assert_eq!(poly_square_64(a), schoolbook(a, a), "vector {i}");
        }
    }

    #[test]
    fn the_square_root_of_the_variable_squares_back() {
        // This constant is the one thing in the root that is not derived at run time.
        assert_eq!(poly_mul_64(ROOT_X, ROOT_X), 2);
    }

    #[test]
    fn the_product_is_exact_on_the_extremes() {
        // Corners a random search is unlikely to reach.
        //
        // - the identities, and the operand whose every product coefficient is live,
        // - the highest degree, where the fold spills furthest, and the tail itself.
        const CORNERS: [u64; 6] = [0, 1, u64::MAX, 1 << 63, 0b1_1011, (1 << 32) - 1];
        for a in CORNERS {
            assert_eq!(poly_square_64(a), schoolbook(a, a), "{a:#x} squared");
            for b in CORNERS {
                assert_eq!(poly_mul_64(a, b), schoolbook(a, b), "{a:#x} * {b:#x}");
            }
        }
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(4000))]

        /// Both the selected route and the composed one must match the modulus itself.
        #[test]
        fn the_product_matches_the_schoolbook_reduction(a: u64, b: u64) {
            let expected = schoolbook(a, b);
            prop_assert_eq!(poly_mul_64(a, b), expected);
            prop_assert_eq!(composed_mul_64(a, b), expected);
            prop_assert_eq!(poly_square_64(a), schoolbook(a, a));
            prop_assert_eq!(composed_square_64(a), schoolbook(a, a));
        }

        /// A dot product must be the sum of the individual products, on both routes.
        #[test]
        fn the_dot_product_sums_the_individual_products(a: u64, b: u64, c: u64, d: u64) {
            let expected = schoolbook(a, b) ^ schoolbook(c, d);
            let pairs = [(a, b), (c, d)];
            prop_assert_eq!(poly_dot_64(pairs.into_iter()), expected);
            prop_assert_eq!(composed_dot_64(pairs.into_iter()), expected);
        }

        #[test]
        fn the_square_root_squares_back(a: u64) {
            let root = poly_sqrt_64(a);
            prop_assert_eq!(schoolbook(root, root), a);
        }

        /// The inverse must satisfy the defining identity against the independent product.
        #[test]
        fn the_inverse_multiplies_back_to_one(a: u64) {
            prop_assert_eq!(schoolbook(a, poly_inverse_64(a)), u64::from(a != 0));
        }
    }
}