1use alloc::vec::Vec;
2use core::{
3 fmt::Debug,
4 ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
5};
67use crate::{DivAssignUnsafe, DivUnsafe};
89// TODO[jpw]: the shared parts of Field and IntMod should be moved into a new `IntegralDomain` trait.
10/// This is a simplified trait for field elements.
11pub trait Field:
12 Sized
13 + Eq
14 + Clone
15 + Debug
16 + Neg<Output = Self>
17 + Add<Output = Self>
18 + Sub<Output = Self>
19 + Mul<Output = Self>
20 + for<'a> Add<&'a Self, Output = Self>
21 + for<'a> Sub<&'a Self, Output = Self>
22 + for<'a> Mul<&'a Self, Output = Self>
23 + for<'a> DivUnsafe<&'a Self, Output = Self>
24 + AddAssign
25 + SubAssign
26 + MulAssign
27 + DivAssignUnsafe
28 + for<'a> AddAssign<&'a Self>
29 + for<'a> SubAssign<&'a Self>
30 + for<'a> MulAssign<&'a Self>
31 + for<'a> DivAssignUnsafe<&'a Self>
32{
33type SelfRef<'a>: Add<&'a Self, Output = Self>
34 + Sub<&'a Self, Output = Self>
35 + Mul<&'a Self, Output = Self>
36 + DivUnsafe<&'a Self, Output = Self>
37where
38Self: 'a;
3940/// The zero element of the field, the additive identity.
41const ZERO: Self;
4243/// The one element of the field, the multiplicative identity.
44const ONE: Self;
4546/// Doubles `self` in-place.
47fn double_assign(&mut self);
4849/// Square `self` in-place
50fn square_assign(&mut self);
5152/// Unchecked inversion. See [DivUnsafe].
53 ///
54 /// ## Panics
55 /// If `self` is zero.
56fn invert(&self) -> Self {
57Self::ONE.div_unsafe(self)
58 }
59}
6061/// Field extension trait. BaseField is the base field of the extension field.
62pub trait FieldExtension<BaseField> {
63/// Extension field degree.
64const D: usize;
65/// This should be [BaseField; D]. It is an associated type due to rust const generic limitations.
66type Coeffs: Sized;
6768/// Create an extension field element from its base field coefficients.
69fn from_coeffs(coeffs: Self::Coeffs) -> Self;
7071/// Create an extension field element from little-endian bytes.
72fn from_bytes(bytes: &[u8]) -> Self;
7374/// Convert an extension field element to its base field coefficients.
75fn to_coeffs(self) -> Self::Coeffs;
7677/// Convert an extension field element to little-endian bytes.
78fn to_bytes(&self) -> Vec<u8>;
7980/// Embed a base field element into an extension field element.
81fn embed(base_elem: BaseField) -> Self;
8283/// Frobenius map: take `self` to the `p^power`th power, where `p` is the prime characteristic of the field.
84fn frobenius_map(&self, power: usize) -> Self;
8586/// Multiply an extension field element by an element in the base field
87fn mul_base(&self, rhs: &BaseField) -> Self;
88}
8990pub trait ComplexConjugate {
91/// Conjugate an extension field element.
92fn conjugate(self) -> Self;
9394/// Replace `self` with its conjugate.
95fn conjugate_assign(&mut self);
96}