openvm_algebra_guest/field/
mod.rs

1use alloc::vec::Vec;
2use core::{
3    fmt::Debug,
4    ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
5};
6
7use crate::{DivAssignUnsafe, DivUnsafe};
8
9// 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{
33    type 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>
37    where
38        Self: 'a;
39
40    /// The zero element of the field, the additive identity.
41    const ZERO: Self;
42
43    /// The one element of the field, the multiplicative identity.
44    const ONE: Self;
45
46    /// Doubles `self` in-place.
47    fn double_assign(&mut self);
48
49    /// Square `self` in-place
50    fn square_assign(&mut self);
51
52    /// Unchecked inversion. See [DivUnsafe].
53    ///
54    /// ## Panics
55    /// If `self` is zero.
56    fn invert(&self) -> Self {
57        Self::ONE.div_unsafe(self)
58    }
59}
60
61/// Field extension trait. BaseField is the base field of the extension field.
62pub trait FieldExtension<BaseField> {
63    /// Extension field degree.
64    const D: usize;
65    /// This should be [BaseField; D]. It is an associated type due to rust const generic limitations.
66    type Coeffs: Sized;
67
68    /// Create an extension field element from its base field coefficients.
69    fn from_coeffs(coeffs: Self::Coeffs) -> Self;
70
71    /// Create an extension field element from little-endian bytes.
72    fn from_bytes(bytes: &[u8]) -> Self;
73
74    /// Convert an extension field element to its base field coefficients.
75    fn to_coeffs(self) -> Self::Coeffs;
76
77    /// Convert an extension field element to little-endian bytes.
78    fn to_bytes(&self) -> Vec<u8>;
79
80    /// Embed a base field element into an extension field element.
81    fn embed(base_elem: BaseField) -> Self;
82
83    /// Frobenius map: take `self` to the `p^power`th power, where `p` is the prime characteristic of the field.
84    fn frobenius_map(&self, power: usize) -> Self;
85
86    /// Multiply an extension field element by an element in the base field
87    fn mul_base(&self, rhs: &BaseField) -> Self;
88}
89
90pub trait ComplexConjugate {
91    /// Conjugate an extension field element.
92    fn conjugate(self) -> Self;
93
94    /// Replace `self` with its conjugate.
95    fn conjugate_assign(&mut self);
96}