Skip to main content

binius_field/
extension.rs

1// Copyright 2023-2025 Irreducible Inc.
2
3use std::{
4	iter,
5	ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
6};
7
8use super::Field;
9
10pub trait ExtensionField<F: Field>:
11	Field
12	+ From<F>
13	+ TryInto<F>
14	+ Add<F, Output = Self>
15	+ Sub<F, Output = Self>
16	+ Mul<F, Output = Self>
17	+ AddAssign<F>
18	+ SubAssign<F>
19	+ MulAssign<F>
20{
21	/// Base-2 logarithm of the extension degree.
22	const LOG_DEGREE: usize;
23
24	/// Extension degree.
25	///
26	/// `DEGREE` is guaranteed to equal `2^LOG_DEGREE`.
27	const DEGREE: usize = 1 << Self::LOG_DEGREE;
28
29	/// For `0 <= i < DEGREE`, returns `i`-th basis field element.
30	///
31	/// # Preconditions
32	///
33	/// * `i` must be in the range [0, `Self::DEGREE`).
34	fn basis(i: usize) -> Self;
35
36	/// Create an extension field element from a slice of base field elements in order
37	/// consistent with `basis(i)` return values.
38	/// Potentially faster than taking an inner product with a vector of basis elements.
39	///
40	/// # Preconditions
41	///
42	/// * `base_elems` must have at most `DEGREE` elements.
43	#[inline]
44	fn from_bases(base_elems: impl IntoIterator<Item = F>) -> Self {
45		Self::from_bases_sparse(base_elems, 0)
46	}
47
48	/// A specialized version of `from_bases` which assumes that only base field
49	/// elements with indices dividing `2^log_stride` can be nonzero.
50	///
51	/// `base_elems` should have length at most `ceil(DEGREE / 2^LOG_STRIDE)`. Note that
52	/// [`ExtensionField::from_bases`] is a special case of `from_bases_sparse` with `log_stride =
53	/// 0`.
54	///
55	/// # Preconditions
56	///
57	/// * `log_stride` must be at most `LOG_DEGREE`.
58	/// * `base_elems` must have at most `ceil(DEGREE / 2^log_stride)` elements.
59	fn from_bases_sparse(base_elems: impl IntoIterator<Item = F>, log_stride: usize) -> Self;
60
61	/// Iterator over base field elements.
62	fn iter_bases(&self) -> impl Iterator<Item = F>;
63
64	/// Returns the i-th base field element.
65	#[inline]
66	fn get_base(&self, i: usize) -> F {
67		assert!(i < Self::DEGREE, "index out of bounds");
68		unsafe { self.get_base_unchecked(i) }
69	}
70
71	/// Returns the i-th base field element without bounds checking.
72	///
73	/// # Safety
74	/// `i` must be less than `DEGREE`.
75	unsafe fn get_base_unchecked(&self, i: usize) -> F;
76
77	/// Transpose square block of subfield elements within `values` in place.
78	///
79	/// # Preconditions
80	///
81	/// * `values.len()` must equal `DEGREE`.
82	fn square_transpose(values: &mut [Self]);
83}
84
85impl<F: Field> ExtensionField<F> for F {
86	const LOG_DEGREE: usize = 0;
87
88	#[inline(always)]
89	fn basis(i: usize) -> Self {
90		assert!(i == 0, "index {i} out of range for degree 1");
91		Self::ONE
92	}
93
94	#[inline(always)]
95	fn from_bases_sparse(base_elems: impl IntoIterator<Item = F>, log_stride: usize) -> Self {
96		assert!(log_stride == 0, "log_stride must be 0 for degree-1 extension");
97		let mut base_elems = base_elems.into_iter();
98		base_elems.next().unwrap_or(Self::ZERO)
99	}
100
101	#[inline(always)]
102	fn iter_bases(&self) -> impl Iterator<Item = F> {
103		iter::once(*self)
104	}
105
106	#[inline(always)]
107	unsafe fn get_base_unchecked(&self, i: usize) -> F {
108		debug_assert_eq!(i, 0);
109		*self
110	}
111
112	#[inline]
113	fn square_transpose(values: &mut [Self]) {
114		assert!(values.len() == 1, "values.len() must be 1 for degree-1 extension");
115	}
116}