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::{error::Error, 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	/// ## Pre-conditions
32	///
33	/// * `i` must be in the range [0, `Self::DEGREE`).
34	#[inline]
35	fn basis(i: usize) -> Self {
36		Self::basis_checked(i).expect("pre-condition: 0 <= i < DEGREE")
37	}
38
39	/// For `0 <= i < DEGREE`, returns `i`-th basis field element.
40	///
41	/// This is the same operation as [`Self::basis`], but it returns an error result instead of
42	/// panicking if the index is out of range.
43	///
44	/// ## Throws
45	///
46	/// * `Error::ExtensionDegreeMismatch` unless `i` is in the range [0, `Self::DEGREE`).
47	fn basis_checked(i: usize) -> Result<Self, Error>;
48
49	/// Create an extension field element from a slice of base field elements in order
50	/// consistent with `basis(i)` return values.
51	/// Potentially faster than taking an inner product with a vector of basis elements.
52	#[inline]
53	fn from_bases(base_elems: impl IntoIterator<Item = F>) -> Result<Self, Error> {
54		Self::from_bases_sparse(base_elems, 0)
55	}
56
57	/// A specialized version of `from_bases` which assumes that only base field
58	/// elements with indices dividing `2^log_stride` can be nonzero.
59	///
60	/// `base_elems` should have length at most `ceil(DEGREE / 2^LOG_STRIDE)`. Note that
61	/// [`ExtensionField::from_bases`] is a special case of `from_bases_sparse` with `log_stride = 0`.
62	fn from_bases_sparse(
63		base_elems: impl IntoIterator<Item = F>,
64		log_stride: usize,
65	) -> Result<Self, Error>;
66
67	/// Iterator over base field elements.
68	fn iter_bases(&self) -> impl Iterator<Item = F>;
69
70	/// Convert into an iterator over base field elements.
71	fn into_iter_bases(self) -> impl Iterator<Item = F>;
72
73	/// Returns the i-th base field element.
74	#[inline]
75	fn get_base(&self, i: usize) -> F {
76		assert!(i < Self::DEGREE, "index out of bounds");
77		unsafe { self.get_base_unchecked(i) }
78	}
79
80	/// Returns the i-th base field element without bounds checking.
81	///
82	/// # Safety
83	/// `i` must be less than `DEGREE`.
84	unsafe fn get_base_unchecked(&self, i: usize) -> F;
85}
86
87impl<F: Field> ExtensionField<F> for F {
88	const LOG_DEGREE: usize = 0;
89
90	#[inline(always)]
91	fn basis_checked(i: usize) -> Result<Self, Error> {
92		if i != 0 {
93			return Err(Error::ExtensionDegreeMismatch);
94		}
95		Ok(Self::ONE)
96	}
97
98	#[inline(always)]
99	fn from_bases_sparse(
100		base_elems: impl IntoIterator<Item = F>,
101		log_stride: usize,
102	) -> Result<Self, Error> {
103		let mut base_elems = base_elems.into_iter();
104		if log_stride != 0 {
105			return Err(Error::ExtensionDegreeMismatch);
106		}
107
108		match base_elems.next() {
109			Some(elem) => Ok(elem),
110			None => Ok(Self::ZERO),
111		}
112	}
113
114	#[inline(always)]
115	fn iter_bases(&self) -> impl Iterator<Item = F> {
116		iter::once(*self)
117	}
118
119	#[inline(always)]
120	fn into_iter_bases(self) -> impl Iterator<Item = F> {
121		iter::once(self)
122	}
123
124	#[inline(always)]
125	unsafe fn get_base_unchecked(&self, i: usize) -> F {
126		debug_assert_eq!(i, 0);
127		*self
128	}
129}