binius_field/
binary_field_arithmetic.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2023-2024 Irreducible Inc.

use crate::{arithmetic_traits::MulAlpha, PackedField};

use super::{arithmetic_traits::InvertOrZero, binary_field::*};

pub(crate) trait TowerFieldArithmetic: TowerField {
	fn multiply(self, rhs: Self) -> Self;

	fn multiply_alpha(self) -> Self;

	fn square(self) -> Self;
}

macro_rules! impl_arithmetic_using_packed {
	($name:ident) => {
		impl InvertOrZero for $name {
			#[inline]
			fn invert_or_zero(self) -> Self {
				use $crate::as_packed_field::AsPackedField;

				$crate::binary_field_arithmetic::invert_or_zero_using_packed::<
					<Self as AsPackedField<$name>>::Packed,
				>(self)
			}
		}

		impl TowerFieldArithmetic for $name {
			#[inline]
			fn multiply(self, rhs: Self) -> Self {
				use $crate::as_packed_field::AsPackedField;

				$crate::binary_field_arithmetic::multiple_using_packed::<
					<Self as AsPackedField<$name>>::Packed,
				>(self, rhs)
			}

			#[inline]
			fn multiply_alpha(self) -> Self {
				use $crate::as_packed_field::AsPackedField;

				$crate::binary_field_arithmetic::mul_alpha_using_packed::<
					<Self as AsPackedField<$name>>::Packed,
				>(self)
			}

			#[inline]
			fn square(self) -> Self {
				use $crate::as_packed_field::AsPackedField;

				$crate::binary_field_arithmetic::square_using_packed::<
					<Self as AsPackedField<$name>>::Packed,
				>(self)
			}
		}
	};
}

pub(crate) use impl_arithmetic_using_packed;

// TODO: try to get rid of `TowerFieldArithmetic` and use `impl_arithmetic_using_packed` here
impl TowerField for BinaryField1b {
	type Canonical = Self;

	#[inline]
	fn mul_primitive(self, _: usize) -> Result<Self, crate::Error> {
		Err(crate::Error::ExtensionDegreeMismatch)
	}
}

impl InvertOrZero for BinaryField1b {
	#[inline]
	fn invert_or_zero(self) -> Self {
		self
	}
}

impl TowerFieldArithmetic for BinaryField1b {
	#[inline]
	fn multiply(self, rhs: Self) -> Self {
		Self(self.0 & rhs.0)
	}

	#[inline]
	fn multiply_alpha(self) -> Self {
		self
	}

	#[inline]
	fn square(self) -> Self {
		self
	}
}

impl_arithmetic_using_packed!(BinaryField2b);
impl_arithmetic_using_packed!(BinaryField4b);
impl_arithmetic_using_packed!(BinaryField8b);
impl_arithmetic_using_packed!(BinaryField16b);
impl_arithmetic_using_packed!(BinaryField32b);
impl_arithmetic_using_packed!(BinaryField64b);
impl_arithmetic_using_packed!(BinaryField128b);

/// For some architectures it may be faster to used SIM versions for packed fields than to use portable
/// single-element arithmetics. That's why we need these functions
#[inline]
pub(super) fn multiple_using_packed<P: PackedField>(lhs: P::Scalar, rhs: P::Scalar) -> P::Scalar {
	(P::set_single(lhs) * P::set_single(rhs)).get(0)
}

#[inline]
pub(super) fn square_using_packed<P: PackedField>(value: P::Scalar) -> P::Scalar {
	P::set_single(value).square().get(0)
}

#[inline]
pub(super) fn invert_or_zero_using_packed<P: PackedField>(value: P::Scalar) -> P::Scalar {
	P::set_single(value).invert_or_zero().get(0)
}

#[inline]
pub(super) fn mul_alpha_using_packed<P: PackedField + MulAlpha>(value: P::Scalar) -> P::Scalar {
	P::set_single(value).mul_alpha().get(0)
}

// `MulPrimitive` implementation for binary tower

/// Multiply `val` by alpha as a packed field with `smaller_type` scalar
macro_rules! mul_alpha_as_repacked {
	($val:ident, $source_type:ty, $smaller_type:ty) => {{
		use $crate::as_packed_field::AsPackedField;

		let repacked_value = <$source_type as AsPackedField<$smaller_type>>::to_packed($val);
		<$source_type as AsPackedField<$smaller_type>>::from_packed(
			$crate::arithmetic_traits::MulAlpha::mul_alpha(repacked_value),
		)
	}};
}

pub(super) use mul_alpha_as_repacked;

macro_rules! impl_mul_primitive {
	($name:ty, $(mul_by $height_0:literal => $expr:expr,)* $(repack $height_1:literal => $subtype:ty,)*) => {
		impl $crate::binary_field::MulPrimitive for $name {
			#[inline]
			fn mul_primitive(self, iota: usize) -> Result<Self, $crate::Error> {
				match iota {
					$($height_0 => Ok(self * $expr),)*
					$($height_1 => {
						let result = $crate::binary_field_arithmetic::mul_alpha_as_repacked!(self, $name, $subtype);
						Ok(result)
					},)*
					_ => Err($crate::Error::ExtensionDegreeMismatch),
				}
			}
		}
	};
}

pub(super) use impl_mul_primitive;

impl_mul_primitive!(BinaryField2b,
	repack 0 => BinaryField2b,
);
impl_mul_primitive!(BinaryField4b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
);
impl_mul_primitive!(BinaryField8b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
	repack 2 => BinaryField8b,
);
impl_mul_primitive!(BinaryField16b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
	repack 2 => BinaryField8b,
	repack 3 => BinaryField16b,
);
impl_mul_primitive!(BinaryField32b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
	repack 2 => BinaryField8b,
	repack 3 => BinaryField16b,
	repack 4 => BinaryField32b,
);
impl_mul_primitive!(BinaryField64b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
	repack 2 => BinaryField8b,
	repack 3 => BinaryField16b,
	repack 4 => BinaryField32b,
	repack 5 => BinaryField64b,
);
impl_mul_primitive!(BinaryField128b,
	repack 0 => BinaryField2b,
	repack 1 => BinaryField4b,
	repack 2 => BinaryField8b,
	repack 3 => BinaryField16b,
	repack 4 => BinaryField32b,
	repack 5 => BinaryField64b,
	repack 6 => BinaryField128b,
);