fgf 3.0.2

Faster Galois Fields: SIMD and portable finite-field arithmetic (binary towers and prime fields) and vector kernels
Documentation
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! The sealed storage family of extension rows.
//!
//! A storage decides the row's raw word and its byte, display, and component
//! forms: [`Components`] keeps one base element per component, while
//! [`Packed<W>`](Packed) and [`PackedPadded<W>`](PackedPadded) keep the
//! row's coordinate word in one integer, component `i` occupying the bits
//! `[i*B, (i+1)*B)` of the base degree `B`. The public [`ExtensionStorage`]
//! trait names the family and its raw word; the conversions the generic row
//! body runs on live on the crate-private [`StorageRepr`] subtrait and its
//! [`StorageBuffer`] and [`StorageDisplay`] extensions, in the row-hook
//! pattern: never as public methods.

use core::any::Any;
use core::fmt::Formatter;
use core::hash::Hash;
use core::marker::PhantomData;

use super::arithmetic;
use super::descriptor::Storage;
use super::private::Sealed;
use crate::field::binary::BinaryField;
use crate::field::private::RowAccess;
use crate::field::{Elem, Field, FieldBuffer, FieldElem};

/// The storage word types a packed extension row may select.
pub trait PackedWord: Sealed {}

impl Sealed for u8 {}
impl Sealed for u16 {}
impl Sealed for u32 {}
impl Sealed for u64 {}

impl PackedWord for u8 {}
impl PackedWord for u16 {}
impl PackedWord for u32 {}
impl PackedWord for u64 {}

/// One base element per component, in the base's canonical raw form.
pub struct Components;

/// The row's coordinate word fills the storage word exactly.
pub struct Packed<W: PackedWord>(PhantomData<W>);

/// The row's coordinate word sits below the storage word width; every
/// storage bit at or above the field degree stays zero.
pub struct PackedPadded<W: PackedWord>(PhantomData<W>);
// Manual marker impls: derives would bound the traits on `W`, which the
// storage contract does not supply. The markers implement `Clone`, `Copy`,
// `Default`, and `Debug` only.

#[allow(clippy::expl_impl_clone_on_copy)]
impl Clone for Components {
    fn clone(&self) -> Self {
        *self
    }
}

impl Copy for Components {}

impl Default for Components {
    fn default() -> Self {
        Self
    }
}

impl core::fmt::Debug for Components {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
        formatter.write_str("Components")
    }
}

macro_rules! storage_marker {
    ($storage:ident) => {
        impl<W: PackedWord> Clone for $storage<W> {
            fn clone(&self) -> Self {
                *self
            }
        }

        impl<W: PackedWord> Copy for $storage<W> {}

        impl<W: PackedWord> Default for $storage<W> {
            fn default() -> Self {
                Self(PhantomData)
            }
        }

        impl<W: PackedWord> core::fmt::Debug for $storage<W> {
            fn fmt(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
                formatter.write_str(core::stringify!($storage))
            }
        }
    };
}

storage_marker!(Packed);
storage_marker!(PackedPadded);

/// The element storage a relation node selects, sealed to the family above.
///
/// `Raw` is the row's raw word: the base-component array for
/// [`Components`], the coordinate word for [`Packed<W>`](Packed) and
/// [`PackedPadded<W>`](PackedPadded), which require a binary base.
pub trait ExtensionStorage<B: Field, const D: usize>: Sealed + 'static {
    /// The raw word of one element.
    type Raw: Copy + Eq + Ord + Hash + core::fmt::Debug + 'static;
}

impl Sealed for Components {}

impl<B: Field, const D: usize> ExtensionStorage<B, D> for Components {
    type Raw = [<B as Field>::Raw; D];
}

/// The component and raw-word conversions of one storage, behind the
/// `Field` seal.
///
/// `join` canonicalizes every component through the base and clears
/// padding, so a joined raw word is always canonical. `split` reads the
/// components of any raw word the base accepts.
pub(crate) trait StorageRepr<B: Field, const D: usize>: ExtensionStorage<B, D> {
    /// The descriptor's storage tag.
    const TAG: Storage;
    /// The additive identity's raw word: zero components.
    const ZERO: Self::Raw;
    /// The multiplicative identity's raw word: the base identity in
    /// component zero.
    const ONE: Self::Raw;
    /// The canonical form of a raw word.
    fn canonical(raw: Self::Raw) -> Self::Raw;
    /// The base components of a raw word, in component order.
    fn split(raw: Self::Raw) -> [<B as Field>::Raw; D];
    /// The raw word of base components, canonicalized on the way in.
    fn join(components: [<B as Field>::Raw; D]) -> Self::Raw;
    /// The shipped-instance word of a raw value: the coordinate word of a
    /// packed storage, and `re | im << 32` for the component limbs of the
    /// shipped prime extension.
    ///
    /// Read only for rows whose descriptor equals a shipped instance's,
    /// where the storage is the instance's own.
    ///
    /// # Panics
    ///
    /// Panics when a component storage's raw word is not a pair of `u32`
    /// limbs.
    fn to_pinned_word(raw: Self::Raw) -> u64;
    /// The raw value of a shipped-instance word; the inverse of
    /// [`Self::to_pinned_word`].
    ///
    /// # Panics
    ///
    /// Panics when a component storage's raw word is not a pair of `u32`
    /// limbs.
    fn from_pinned_word(word: u64) -> Self::Raw;
}

/// The fixed-width byte form of one storage, for the rows that carry one.
///
/// Implemented for [`Packed<W>`](Packed) — the little-endian word — and for
/// [`Components`] over a base with a byte form — the concatenated base
/// encodings. [`PackedPadded<W>`](PackedPadded) implements none: its
/// coordinate word is narrower than its storage word.
pub(crate) trait StorageBuffer<B: Field, const D: usize>: StorageRepr<B, D> {
    /// Width of one element's stable byte representation.
    const BYTES: usize;
    /// Storage bit width of one element: `8 * BYTES`.
    const STORAGE_BITS: u32;
    /// The raw word named by stable little-endian bytes.
    ///
    /// # Panics
    ///
    /// Panics when `bytes` holds the wrong width.
    fn decode(bytes: &[u8]) -> Self::Raw;
    /// Writes the stable little-endian bytes of `raw`.
    ///
    /// # Panics
    ///
    /// Panics when `bytes` holds the wrong width.
    fn encode(bytes: &mut [u8], raw: &Self::Raw);
}

/// The display form of one storage.
pub(crate) trait StorageDisplay<B: Field, const D: usize>: StorageRepr<B, D> {
    /// Writes one element's display form; `digits` is the hexadecimal digit
    /// count of a packed coordinate word.
    fn fmt(formatter: &mut Formatter<'_>, raw: &Self::Raw, digits: usize) -> core::fmt::Result;
}

impl<B: Field, const D: usize> StorageRepr<B, D> for Components {
    const TAG: Storage = Storage::Components;
    const ZERO: Self::Raw = [<B as Field>::ZERO_RAW; D];
    const ONE: Self::Raw = {
        let mut components = [<B as Field>::ZERO_RAW; D];
        components[0] = <B as Field>::ONE_RAW;
        components
    };

    fn canonical(raw: Self::Raw) -> Self::Raw {
        raw.map(<B as Field>::canonical_raw)
    }

    fn split(raw: Self::Raw) -> [<B as Field>::Raw; D] {
        raw
    }

    fn join(components: [<B as Field>::Raw; D]) -> Self::Raw {
        components.map(<B as Field>::canonical_raw)
    }

    // The shipped component instance stores two `u32` limbs; the checked
    // downcast is the type proof that this row's raw word is that pair.
    fn to_pinned_word(raw: Self::Raw) -> u64 {
        let limbs = (&raw as &dyn Any)
            .downcast_ref::<[u32; 2]>()
            .expect("pinned component storage holds two u32 limbs");
        u64::from(limbs[0]) | (u64::from(limbs[1]) << 32)
    }

    // Splitting the word into its two limbs: the truncation is the
    // operation.
    #[allow(clippy::cast_possible_truncation)]
    fn from_pinned_word(word: u64) -> Self::Raw {
        let limbs: [u32; 2] = [word as u32, (word >> 32) as u32];
        *(&limbs as &dyn Any)
            .downcast_ref::<Self::Raw>()
            .expect("pinned component storage holds two u32 limbs")
    }
}

impl<B: FieldBuffer, const D: usize> StorageBuffer<B, D> for Components {
    const BYTES: usize = D * <B as FieldBuffer>::BYTES;
    // The base byte width times the component count stays far below the
    // `u32` range.
    #[allow(clippy::cast_possible_truncation)]
    const STORAGE_BITS: u32 = (8 * D * <B as FieldBuffer>::BYTES) as u32;

    fn decode(bytes: &[u8]) -> Self::Raw {
        let width = <B as FieldBuffer>::BYTES;
        core::array::from_fn(|i| {
            let component = &bytes[i * width..(i + 1) * width];
            <B as FieldBuffer>::decode(component).to_raw()
        })
    }

    fn encode(bytes: &mut [u8], raw: &Self::Raw) {
        let width = <B as FieldBuffer>::BYTES;
        for (i, component) in raw.iter().enumerate() {
            let element = Elem::<B> { raw: *component };
            <B as FieldBuffer>::encode(&mut bytes[i * width..(i + 1) * width], element);
        }
    }
}

impl<B: Field, const D: usize> StorageDisplay<B, D> for Components
where
    Elem<B>: core::fmt::Display,
{
    fn fmt(formatter: &mut Formatter<'_>, raw: &Self::Raw, _digits: usize) -> core::fmt::Result {
        formatter.write_str("[")?;
        for (i, component) in raw.iter().enumerate() {
            if i > 0 {
                formatter.write_str(", ")?;
            }
            let element = Elem::<B> { raw: *component };
            core::fmt::Display::fmt(&element, formatter)?;
        }
        formatter.write_str("]")
    }
}

/// Emit one packed storage over one word type.
///
/// `Packed` fills its word exactly; `PackedPadded` keeps the coordinate word
/// below the word width, with every bit at or above the field degree
/// cleared. `join` rebuilds the word from canonical components only, so the
/// padding bits of a padded word are zero after every join, and `canonical`
/// runs the same round trip to clear the padding of an arbitrary word.
macro_rules! packed_storage {
    ($storage:ident, $word:ty, $bits:literal) => {
        impl Sealed for $storage<$word> {}

        impl<B: BinaryField, const D: usize> ExtensionStorage<B, D> for $storage<$word> {
            type Raw = $word;
        }

        impl<B: BinaryField, const D: usize> StorageRepr<B, D> for $storage<$word> {
            const TAG: Storage = Storage::$storage { width: $bits };
            const ZERO: Self::Raw = 0;
            // The base identity word holds at most the base's degree bits,
            // below the coordinate word of the row.
            #[allow(clippy::cast_possible_truncation)]
            const ONE: Self::Raw = {
                let base = <B as RowAccess>::DESCRIPTOR;
                arithmetic::word_one(base, base.root_index()) as $word
            };

            fn canonical(raw: Self::Raw) -> Self::Raw {
                <Self as StorageRepr<B, D>>::join(<Self as StorageRepr<B, D>>::split(raw))
            }

            #[allow(clippy::cast_lossless)]
            fn split(raw: Self::Raw) -> [<B as Field>::Raw; D] {
                let word = raw as u64;
                let degree = <B as Field>::DEGREE as usize;
                core::array::from_fn(|i| {
                    let component = extract_bits(word, i * degree, degree);
                    // The extracted word holds at most the base's degree
                    // bits, so the index conversion cannot see excess bits.
                    Elem::<B>::from_index(component as u128)
                        .expect("packed component word holds excess bits")
                        .to_raw()
                })
            }

            #[allow(clippy::cast_possible_truncation)]
            fn join(components: [<B as Field>::Raw; D]) -> Self::Raw {
                let degree = <B as Field>::DEGREE as usize;
                let mut word = 0u64;
                for (i, component) in components.iter().enumerate() {
                    let element = Elem::<B> {
                        raw: <B as Field>::canonical_raw(*component),
                    };
                    // A packed row places component `i` at the bits
                    // `[i*degree, (i+1)*degree)`; the base index is a
                    // coordinate word, so it fits `u64`, and the shift stays
                    // at or below the word width.
                    let component =
                        u64::try_from(element.to_index()).expect("base index fits the word");
                    word |= component << (i * degree);
                }
                word as $word
            }

            #[allow(clippy::cast_lossless)]
            fn to_pinned_word(raw: Self::Raw) -> u64 {
                raw as u64
            }

            // A pinned word is the row's own coordinate word, which fits
            // the storage word.
            #[allow(clippy::cast_possible_truncation)]
            fn from_pinned_word(word: u64) -> Self::Raw {
                word as $word
            }
        }

        impl<B: BinaryField, const D: usize> StorageDisplay<B, D> for $storage<$word> {
            fn fmt(
                formatter: &mut Formatter<'_>,
                raw: &Self::Raw,
                digits: usize,
            ) -> core::fmt::Result {
                write!(formatter, "{:0width$x}", *raw, width = digits)
            }
        }
    };
}

packed_storage!(Packed, u8, 8);
packed_storage!(Packed, u16, 16);
packed_storage!(Packed, u32, 32);
packed_storage!(Packed, u64, 64);
packed_storage!(PackedPadded, u8, 8);
packed_storage!(PackedPadded, u16, 16);
packed_storage!(PackedPadded, u32, 32);
packed_storage!(PackedPadded, u64, 64);

/// Emit the little-endian byte form of one packed word.
macro_rules! packed_buffer {
    ($word:ty, $bytes:literal, $bits:literal) => {
        impl<B: BinaryField, const D: usize> StorageBuffer<B, D> for Packed<$word> {
            const BYTES: usize = $bytes;
            const STORAGE_BITS: u32 = $bits;

            fn decode(bytes: &[u8]) -> Self::Raw {
                let bytes: [u8; $bytes] = bytes
                    .try_into()
                    .expect("packed element has the wrong byte width");
                <$word>::from_le_bytes(bytes)
            }

            fn encode(bytes: &mut [u8], raw: &Self::Raw) {
                bytes.copy_from_slice(&raw.to_le_bytes());
            }
        }
    };
}

packed_buffer!(u8, 1, 8);
packed_buffer!(u16, 2, 16);
packed_buffer!(u32, 4, 32);
packed_buffer!(u64, 8, 64);

/// The `bits` bits of `word` at offset `offset`.
///
/// `offset + bits` stays at or below 64, so the shifts never wrap.
pub(crate) const fn extract_bits(word: u64, offset: usize, bits: usize) -> u64 {
    if bits >= 64 {
        word
    } else {
        (word >> offset) & ((1 << bits) - 1)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::field::finite_field::FiniteField;
    use crate::field::{AES, Polynomial};

    type Aes8 = FiniteField<2, 8, Polynomial<AES>>;
    type Gf4 = FiniteField<2, 2, Polynomial<7>>;
    type FanPaar4Row = FiniteField<2, 4, crate::field::Extension<crate::field::FanPaar4, 2>>;
    type FanPaar8Row = FiniteField<2, 8, crate::field::Extension<crate::field::FanPaar8, 2>>;
    type Rijndael16Row = FiniteField<2, 16, crate::field::Extension<crate::field::Rijndael16, 2>>;
    type Rijndael32Row = FiniteField<2, 32, crate::field::Extension<crate::field::Rijndael32, 2>>;

    /// One packed storage joins its components, splits back, canonicalizes
    /// padding, and carries the base identity in component zero.
    macro_rules! packed_case {
        ($storage:ident, $word:ty, $base:ty, $low:expr, $high:expr, $word_value:expr) => {{
            let components = [
                Elem::<$base>::from_raw($low).raw,
                Elem::<$base>::from_raw($high).raw,
            ];
            let joined = <$storage<$word> as StorageRepr<$base, 2>>::join(components);
            assert_eq!(joined, $word_value);
            assert_eq!(
                <$storage<$word> as StorageRepr<$base, 2>>::split(joined),
                components
            );
            assert_eq!(<$storage<$word> as StorageRepr<$base, 2>>::ONE, 1);
            assert_eq!(<$storage<$word> as StorageRepr<$base, 2>>::ZERO, 0);
            // The canonical form of the exact word is itself; a padded
            // word clears the bits above the field degree.
            assert_eq!(
                <$storage<$word> as StorageRepr<$base, 2>>::canonical(joined),
                joined
            );
        }};
    }

    #[test]
    fn packed_storages_round_trip() {
        // Exact-width words: `Packed<u8>` over GF(2^4), `Packed<u16>` over
        // GF(2^8), `Packed<u32>` over GF(2^16), `Packed<u64>` over GF(2^32).
        packed_case!(Packed, u8, FanPaar4Row, 0b1001, 0b0101, 0b0101_1001);
        packed_case!(Packed, u16, Aes8, 0x53, 0xC1, 0xC153);
        packed_case!(Packed, u32, Rijndael16Row, 0x00FF, 0x1234, 0x1234_00FF);
        packed_case!(
            Packed,
            u64,
            Rijndael32Row,
            0x00FF_FFFF,
            0x1234_5678,
            0x1234_5678_00FF_FFFF
        );
        // Padded words below their width.
        packed_case!(PackedPadded, u8, Gf4, 0b10, 0b01, 0b0110);
        packed_case!(PackedPadded, u16, FanPaar4Row, 0b1001, 0b0101, 0b0101_1001);
        packed_case!(PackedPadded, u32, FanPaar8Row, 0x53, 0xC1, 0xC153);
        packed_case!(PackedPadded, u64, FanPaar8Row, 0x53, 0xC1, 0xC153);
    }

    #[test]
    fn padded_canonical_clears_high_bits() {
        // The degree-four row over GF(4) holds four bits; the byte above
        // them is padding.
        let components = [
            Elem::<Gf4>::from_raw(0b10).raw,
            Elem::<Gf4>::from_raw(0b01).raw,
        ];
        let joined = <PackedPadded<u8> as StorageRepr<Gf4, 2>>::join(components);
        assert_eq!(joined, 0b0110);
        assert_eq!(
            <PackedPadded<u8> as StorageRepr<Gf4, 2>>::canonical(0xF6),
            joined
        );
    }

    /// The byte form of a packed word is its little-endian encoding, at
    /// every word width.
    #[test]
    fn packed_bytes_round_trip_every_word() {
        macro_rules! buffer_case {
            ($word:ty, $value:expr) => {{
                let mut bytes = [0u8; core::mem::size_of::<$word>()];
                <Packed<$word> as StorageBuffer<Aes8, 2>>::encode(&mut bytes, &$value);
                assert_eq!(bytes, $value.to_le_bytes());
                assert_eq!(
                    <Packed<$word> as StorageBuffer<Aes8, 2>>::decode(&bytes),
                    $value
                );
            }};
        }
        // Every word width carries the GF(2^16) pair `(0x53, 0xC1)` over
        // the AES base in its low bits.
        buffer_case!(u8, 0x53u8);
        buffer_case!(u16, 0xC153u16);
        buffer_case!(u32, 0x0000_C153u32);
        buffer_case!(u64, 0x0000_0000_0000_C153u64);
    }

    #[test]
    fn components_storage_round_trip() {
        let components = [
            Elem::<Aes8>::from_raw(0x53).raw,
            Elem::<Aes8>::from_raw(0xC1).raw,
        ];
        assert_eq!(
            <Components as StorageRepr<Aes8, 2>>::split(
                <Components as StorageRepr<Aes8, 2>>::join(components)
            ),
            components
        );
        assert_eq!(<Components as StorageRepr<Aes8, 2>>::ZERO, [0, 0]);
        assert_eq!(<Components as StorageRepr<Aes8, 2>>::ONE, [1, 0]);
        // The identity in component zero comes from the base identity,
        // which a tower row may spell differently from one.
        assert_eq!(
            <Components as StorageRepr<FanPaar4Row, 2>>::ONE,
            [Elem::<FanPaar4Row>::ONE.raw, 0]
        );
    }

    #[test]
    fn components_bytes_concatenate_base_encodings() {
        assert_eq!(<Components as StorageBuffer<Aes8, 2>>::BYTES, 2);
        assert_eq!(<Components as StorageBuffer<Aes8, 2>>::STORAGE_BITS, 16);
        let components = [
            Elem::<Aes8>::from_raw(0x53).raw,
            Elem::<Aes8>::from_raw(0xC1).raw,
        ];
        let mut bytes = [0u8; 2];
        <Components as StorageBuffer<Aes8, 2>>::encode(&mut bytes, &components);
        assert_eq!(bytes, [0x53, 0xC1]);
        assert_eq!(
            <Components as StorageBuffer<Aes8, 2>>::decode(&bytes),
            components
        );
    }
}