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`
10// trait.
11/// This is a simplified trait for field elements.
12pub trait Field:
13    Sized
14    + Eq
15    + Clone
16    + Debug
17    + Neg<Output = Self>
18    + Add<Output = Self>
19    + Sub<Output = Self>
20    + Mul<Output = Self>
21    + for<'a> Add<&'a Self, Output = Self>
22    + for<'a> Sub<&'a Self, Output = Self>
23    + for<'a> Mul<&'a Self, Output = Self>
24    + for<'a> DivUnsafe<&'a Self, Output = Self>
25    + AddAssign
26    + SubAssign
27    + MulAssign
28    + DivAssignUnsafe
29    + for<'a> AddAssign<&'a Self>
30    + for<'a> SubAssign<&'a Self>
31    + for<'a> MulAssign<&'a Self>
32    + for<'a> DivAssignUnsafe<&'a Self>
33{
34    type SelfRef<'a>: Add<&'a Self, Output = Self>
35        + Sub<&'a Self, Output = Self>
36        + Mul<&'a Self, Output = Self>
37        + DivUnsafe<&'a Self, Output = Self>
38    where
39        Self: 'a;
40
41    /// The zero element of the field, the additive identity.
42    const ZERO: Self;
43
44    /// The one element of the field, the multiplicative identity.
45    const ONE: Self;
46
47    /// Doubles `self` in-place.
48    fn double_assign(&mut self);
49
50    /// Square `self` in-place
51    fn square_assign(&mut self);
52
53    /// Unchecked inversion. See [DivUnsafe].
54    ///
55    /// ## Panics
56    /// If `self` is zero.
57    fn invert(&self) -> Self {
58        Self::ONE.div_unsafe(self)
59    }
60}
61
62/// Field extension trait. BaseField is the base field of the extension field.
63pub trait FieldExtension<BaseField> {
64    /// Extension field degree.
65    const D: usize;
66    /// This should be [BaseField; D]. It is an associated type due to rust const generic
67    /// limitations.
68    type Coeffs: Sized;
69
70    /// Create an extension field element from its base field coefficients.
71    fn from_coeffs(coeffs: Self::Coeffs) -> Self;
72
73    /// Create an extension field element from little-endian bytes.
74    fn from_bytes(bytes: &[u8]) -> Self;
75
76    /// Convert an extension field element to its base field coefficients.
77    fn to_coeffs(self) -> Self::Coeffs;
78
79    /// Convert an extension field element to little-endian bytes.
80    fn to_bytes(&self) -> Vec<u8>;
81
82    /// Embed a base field element into an extension field element.
83    fn embed(base_elem: BaseField) -> Self;
84
85    /// Frobenius map: take `self` to the `p^power`th power, where `p` is the prime characteristic
86    /// of the field.
87    fn frobenius_map(&self, power: usize) -> Self;
88
89    /// Multiply an extension field element by an element in the base field
90    fn mul_base(&self, rhs: &BaseField) -> Self;
91}
92
93pub trait ComplexConjugate {
94    /// Conjugate an extension field element.
95    fn conjugate(self) -> Self;
96
97    /// Replace `self` with its conjugate.
98    fn conjugate_assign(&mut self);
99}