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/// Value that can be filled with `Scalar`
22pub trait Broadcast<Scalar> {
23	/// Set `scalar`` value to all the positions
24	fn broadcast(scalar: Scalar) -> Self;
25}
26
27/// Multiplication that is parameterized with some some strategy.
28pub trait TaggedMul<Strategy> {
29	fn mul(self, rhs: Self) -> Self;
30}
31
32macro_rules! impl_mul_with {
33	($name:ident @ $strategy:ty) => {
34		impl std::ops::Mul for $name {
35			type Output = Self;
36
37			#[inline]
38			fn mul(self, rhs: Self) -> Self {
39				$crate::tracing::trace_multiplication!($name);
40
41				$crate::arithmetic_traits::TaggedMul::<$strategy>::mul(self, rhs)
42			}
43		}
44	};
45	($name:ty => $bigger:ty) => {
46		impl std::ops::Mul for $name {
47			type Output = Self;
48
49			#[inline]
50			fn mul(self, rhs: Self) -> Self {
51				$crate::arch::portable::packed::mul_as_bigger_type::<_, $bigger>(self, rhs)
52			}
53		}
54	};
55}
56
57pub(crate) use impl_mul_with;
58
59/// Square operation that is parameterized with some some strategy.
60pub trait TaggedSquare<Strategy> {
61	fn square(self) -> Self;
62}
63
64macro_rules! impl_square_with {
65	($name:ident @ $strategy:ty) => {
66		impl $crate::arithmetic_traits::Square for $name {
67			#[inline]
68			fn square(self) -> Self {
69				$crate::arithmetic_traits::TaggedSquare::<$strategy>::square(self)
70			}
71		}
72	};
73	($name:ty => $bigger:ty) => {
74		impl $crate::arithmetic_traits::Square for $name {
75			#[inline]
76			fn square(self) -> Self {
77				$crate::arch::portable::packed::square_as_bigger_type::<_, $bigger>(self)
78			}
79		}
80	};
81}
82
83pub(crate) use impl_square_with;
84
85/// Invert or zero operation that is parameterized with some some strategy.
86pub trait TaggedInvertOrZero<Strategy> {
87	fn invert_or_zero(self) -> Self;
88}
89
90macro_rules! impl_invert_with {
91	($name:ident @ $strategy:ty) => {
92		impl $crate::arithmetic_traits::InvertOrZero for $name {
93			#[inline]
94			fn invert_or_zero(self) -> Self {
95				$crate::arithmetic_traits::TaggedInvertOrZero::<$strategy>::invert_or_zero(self)
96			}
97		}
98	};
99	($name:ty => $bigger:ty) => {
100		impl $crate::arithmetic_traits::InvertOrZero for $name {
101			#[inline]
102			fn invert_or_zero(self) -> Self {
103				$crate::arch::portable::packed::invert_as_bigger_type::<_, $bigger>(self)
104			}
105		}
106	};
107}
108
109pub(crate) use impl_invert_with;
110
111/// Multiply by alpha operation that is parameterized with some some strategy.
112pub trait TaggedMulAlpha<Strategy> {
113	fn mul_alpha(self) -> Self;
114}
115
116macro_rules! impl_mul_alpha_with {
117	($name:ident @ $strategy:ty) => {
118		impl $crate::arithmetic_traits::MulAlpha for $name {
119			#[inline]
120			fn mul_alpha(self) -> Self {
121				$crate::arithmetic_traits::TaggedMulAlpha::<$strategy>::mul_alpha(self)
122			}
123		}
124	};
125	($name:ty => $bigger:ty) => {
126		impl $crate::arithmetic_traits::MulAlpha for $name {
127			#[inline]
128			fn mul_alpha(self) -> Self {
129				$crate::arch::portable::packed::mul_alpha_as_bigger_type::<_, $bigger>(self)
130			}
131		}
132	};
133}
134
135pub(crate) use impl_mul_alpha_with;