binius_field/
arithmetic_traits.rs

1// Copyright 2024-2025 Irreducible Inc.
2
3/// Value that can be multiplied by itself
4pub trait Square {
5	/// Returns the value multiplied by itself
6	fn square(self) -> Self;
7}
8
9/// Value that can be inverted
10pub trait InvertOrZero {
11	/// Returns the inverted value or zero in case when `self` is zero
12	fn invert_or_zero(self) -> Self;
13}
14
15/// Value that can be multiplied by alpha
16pub trait MulAlpha {
17	/// Multiply self by alpha
18	fn mul_alpha(self) -> Self;
19}
20
21/// Multiplication that is parameterized with some some strategy.
22pub trait TaggedMul<Strategy> {
23	fn mul(self, rhs: Self) -> Self;
24}
25
26macro_rules! impl_mul_with {
27	($name:ident @ $strategy:ty) => {
28		impl std::ops::Mul for $name {
29			type Output = Self;
30
31			#[inline]
32			fn mul(self, rhs: Self) -> Self {
33				$crate::tracing::trace_multiplication!($name);
34
35				$crate::arithmetic_traits::TaggedMul::<$strategy>::mul(self, rhs)
36			}
37		}
38	};
39	($name:ty => $bigger:ty) => {
40		impl std::ops::Mul for $name {
41			type Output = Self;
42
43			#[inline]
44			fn mul(self, rhs: Self) -> Self {
45				$crate::arch::portable::packed::mul_as_bigger_type::<_, $bigger>(self, rhs)
46			}
47		}
48	};
49}
50
51pub(crate) use impl_mul_with;
52
53/// Square operation that is parameterized with some some strategy.
54pub trait TaggedSquare<Strategy> {
55	fn square(self) -> Self;
56}
57
58macro_rules! impl_square_with {
59	($name:ident @ $strategy:ty) => {
60		impl $crate::arithmetic_traits::Square for $name {
61			#[inline]
62			fn square(self) -> Self {
63				$crate::arithmetic_traits::TaggedSquare::<$strategy>::square(self)
64			}
65		}
66	};
67	($name:ty => $bigger:ty) => {
68		impl $crate::arithmetic_traits::Square for $name {
69			#[inline]
70			fn square(self) -> Self {
71				$crate::arch::portable::packed::square_as_bigger_type::<_, $bigger>(self)
72			}
73		}
74	};
75}
76
77pub(crate) use impl_square_with;
78
79/// Invert or zero operation that is parameterized with some some strategy.
80pub trait TaggedInvertOrZero<Strategy> {
81	fn invert_or_zero(self) -> Self;
82}
83
84macro_rules! impl_invert_with {
85	($name:ident @ $strategy:ty) => {
86		impl $crate::arithmetic_traits::InvertOrZero for $name {
87			#[inline]
88			fn invert_or_zero(self) -> Self {
89				$crate::arithmetic_traits::TaggedInvertOrZero::<$strategy>::invert_or_zero(self)
90			}
91		}
92	};
93	($name:ty => $bigger:ty) => {
94		impl $crate::arithmetic_traits::InvertOrZero for $name {
95			#[inline]
96			fn invert_or_zero(self) -> Self {
97				$crate::arch::portable::packed::invert_as_bigger_type::<_, $bigger>(self)
98			}
99		}
100	};
101}
102
103pub(crate) use impl_invert_with;
104
105/// Multiply by alpha operation that is parameterized with some some strategy.
106pub trait TaggedMulAlpha<Strategy> {
107	fn mul_alpha(self) -> Self;
108}
109
110macro_rules! impl_mul_alpha_with {
111	($name:ident @ $strategy:ty) => {
112		impl $crate::arithmetic_traits::MulAlpha for $name {
113			#[inline]
114			fn mul_alpha(self) -> Self {
115				$crate::arithmetic_traits::TaggedMulAlpha::<$strategy>::mul_alpha(self)
116			}
117		}
118	};
119	($name:ty => $bigger:ty) => {
120		impl $crate::arithmetic_traits::MulAlpha for $name {
121			#[inline]
122			fn mul_alpha(self) -> Self {
123				$crate::arch::portable::packed::mul_alpha_as_bigger_type::<_, $bigger>(self)
124			}
125		}
126	};
127}
128
129pub(crate) use impl_mul_alpha_with;