diff --git a/src/field/crypto_bigint_boxed_monty.rs b/src/field/crypto_bigint_boxed_monty.rs index 63d3230..36c19b5 100644 --- a/src/field/crypto_bigint_boxed_monty.rs +++ b/src/field/crypto_bigint_boxed_monty.rs @@ -32,12 +32,14 @@ impl BoxedMontyField { // impl Debug for BoxedMontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { Debug::fmt(&self.0, f) } } impl Display for BoxedMontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!( f, @@ -49,6 +51,7 @@ impl Display for BoxedMontyField { } impl PartialOrd for BoxedMontyField { + #[inline(always)] fn partial_cmp(&self, other: &Self) -> Option { if self.modulus() != other.modulus() { return None; @@ -58,6 +61,7 @@ impl PartialOrd for BoxedMontyField { } impl Hash for BoxedMontyField { + #[inline(always)] fn hash(&self, state: &mut H) { self.0.as_montgomery().hash(state) } @@ -70,6 +74,7 @@ impl Hash for BoxedMontyField { impl Neg for BoxedMontyField { type Output = Self; + #[inline(always)] fn neg(self) -> Self::Output { Self(self.0.neg()) } @@ -122,6 +127,7 @@ impl_basic_op!(Mul, mul); impl Div for BoxedMontyField { type Output = Self; + #[inline(always)] fn div(self, rhs: Self) -> Self::Output { self.div(&rhs) } @@ -130,6 +136,7 @@ impl Div for BoxedMontyField { impl Div<&Self> for BoxedMontyField { type Output = Self; + #[inline(always)] fn div(self, rhs: &Self) -> Self::Output { self.checked_div(rhs).expect("Division by zero") } @@ -138,6 +145,7 @@ impl Div<&Self> for BoxedMontyField { impl Div for &BoxedMontyField { type Output = BoxedMontyField; + #[inline(always)] fn div(self, rhs: Self) -> Self::Output { self.checked_div(rhs).expect("Division by zero") } @@ -146,6 +154,7 @@ impl Div for &BoxedMontyField { impl Div for &BoxedMontyField { type Output = BoxedMontyField; + #[inline(always)] fn div(self, rhs: BoxedMontyField) -> Self::Output { self.div(&rhs) } @@ -154,6 +163,7 @@ impl Div for &BoxedMontyField { impl Pow for BoxedMontyField { type Output = Self; + #[inline(always)] fn pow(self, rhs: u32) -> Self::Output { Self(self.0.pow(&BoxedUint::from(rhs))) } @@ -162,6 +172,7 @@ impl Pow for BoxedMontyField { impl Inv for BoxedMontyField { type Output = Option; + #[inline(always)] fn inv(self) -> Self::Output { Some(Self(Option::from(self.0.invert_vartime())?)) } @@ -170,6 +181,7 @@ impl Inv for BoxedMontyField { impl Inv for &BoxedMontyField { type Output = Option; + #[inline(always)] fn inv(self) -> Self::Output { Some(BoxedMontyField(Option::from(self.0.invert_vartime())?)) } @@ -182,6 +194,7 @@ impl Inv for &BoxedMontyField { impl CheckedDiv for BoxedMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn checked_div(&self, rhs: &Self) -> Option { Some(self * rhs.inv()?) } @@ -194,11 +207,13 @@ impl CheckedDiv for BoxedMontyField { macro_rules! impl_field_op_assign { ($trait:ident, $method:ident) => { impl $trait for BoxedMontyField { + #[inline(always)] fn $method(&mut self, rhs: Self) { self.0.$method(&rhs.0); } } impl $trait<&Self> for BoxedMontyField { + #[inline(always)] fn $method(&mut self, rhs: &Self) { self.0.$method(&rhs.0); } @@ -211,12 +226,14 @@ impl_field_op_assign!(SubAssign, sub_assign); impl_field_op_assign!(MulAssign, mul_assign); impl DivAssign for BoxedMontyField { + #[inline(always)] fn div_assign(&mut self, rhs: Self) { self.div_assign(&rhs); } } impl DivAssign<&Self> for BoxedMontyField { + #[inline(always)] fn div_assign(&mut self, rhs: &Self) { self.0.mul_assign(rhs.0.invert().expect("Division by zero")) } @@ -227,6 +244,7 @@ impl DivAssign<&Self> for BoxedMontyField { // impl Sum for BoxedMontyField { + #[inline(always)] fn sum>(mut iter: I) -> Self { let Some(BoxedMontyField(first)) = iter.next() else { panic!("Sum of an empty iterator is not defined for BoxedMontyField"); @@ -236,6 +254,7 @@ impl Sum for BoxedMontyField { } impl<'a> Sum<&'a Self> for BoxedMontyField { + #[inline(always)] fn sum>(mut iter: I) -> Self { let Some(BoxedMontyField(first)) = iter.next() else { panic!("Sum of an empty iterator is not defined for BoxedMontyField"); @@ -245,6 +264,7 @@ impl<'a> Sum<&'a Self> for BoxedMontyField { } impl Product for BoxedMontyField { + #[inline(always)] fn product>(mut iter: I) -> Self { let Some(BoxedMontyField(first)) = iter.next() else { panic!("Product of an empty iterator is not defined for BoxedMontyField"); @@ -255,6 +275,7 @@ impl Product for BoxedMontyField { impl<'a> Product<&'a Self> for BoxedMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn product>(mut iter: I) -> Self { let Some(BoxedMontyField(first)) = iter.next() else { panic!("Product of an empty iterator is not defined for BoxedMontyField"); @@ -282,6 +303,7 @@ impl From for BoxedMontyForm { } impl From<&BoxedMontyField> for BoxedMontyField { + #[inline(always)] fn from(value: &Self) -> Self { value.clone() } @@ -291,6 +313,7 @@ macro_rules! impl_from_unsigned { ($($t:ty),* $(,)?) => { $( impl FromWithConfig<$t> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: $t, cfg: &Self::Config) -> Self { let abs: BoxedUint = value.into(); let abs = abs.resize(cfg.modulus().bits_precision()); @@ -299,6 +322,7 @@ macro_rules! impl_from_unsigned { } impl FromWithConfig<&$t> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: &$t, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } @@ -312,6 +336,7 @@ macro_rules! impl_from_signed { $( #[allow(clippy::arithmetic_side_effects)] // False alert impl FromWithConfig<$t> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: $t, cfg: &Self::Config) -> Self { let magnitude = BoxedUint::from(value.abs_diff(0)).resize(cfg.modulus().bits_precision()); let form = BoxedMontyForm::new(magnitude, cfg.clone()); @@ -320,6 +345,7 @@ macro_rules! impl_from_signed { } impl FromWithConfig<&$t> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: &$t, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } @@ -332,6 +358,7 @@ impl_from_unsigned!(u8, u16, u32, u64, u128); impl_from_signed!(i8, i16, i32, i64, i128); impl FromWithConfig for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: bool, cfg: &Self::Config) -> Self { let magnitude: BoxedUint = if value { BoxedUint::one() @@ -344,24 +371,28 @@ impl FromWithConfig for BoxedMontyField { } impl FromWithConfig<&bool> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: &bool, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: Boolean, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig<&Boolean> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: &Boolean, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: Int, cfg: &Self::Config) -> Self { Self::from_with_cfg(&value, cfg) } @@ -369,6 +400,7 @@ impl FromWithConfig> for BoxedMontyField { impl FromWithConfig<&Int> for BoxedMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn from_with_cfg(value: &Int, cfg: &Self::Config) -> Self { let abs: BoxedUint = value.inner().abs().into(); let abs = abs.resize(cfg.modulus().bits_precision()); @@ -380,12 +412,14 @@ impl FromWithConfig<&Int> for BoxedMontyField { } impl FromWithConfig for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: BoxedUint, cfg: &Self::Config) -> Self { Self::from_with_cfg(&value, cfg) } } impl FromWithConfig<&BoxedUint> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: &BoxedUint, cfg: &Self::Config) -> Self { let value = value.resize(cfg.modulus().bits_precision()); Self(BoxedMontyForm::new(value, cfg.clone())) @@ -393,6 +427,7 @@ impl FromWithConfig<&BoxedUint> for BoxedMontyField { } impl FromWithConfig> for BoxedMontyField { + #[inline(always)] fn from_with_cfg(value: crypto_bigint::Uint, cfg: &Self::Config) -> Self { Self::from_with_cfg(&value, cfg) } @@ -400,6 +435,7 @@ impl FromWithConfig> for BoxedMon impl FromWithConfig<&crypto_bigint::Uint> for BoxedMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn from_with_cfg(value: &crypto_bigint::Uint, cfg: &Self::Config) -> Self { let value: BoxedUint = value.into(); let value = value.resize(cfg.modulus().bits_precision()); @@ -438,20 +474,24 @@ impl Field for BoxedMontyField { impl PrimeField for BoxedMontyField { type Config = BoxedMontyParams; + #[inline(always)] fn cfg(&self) -> &Self::Config { self.0.params() } + #[inline(always)] fn modulus(&self) -> Self::Inner { self.0.params().modulus().clone().get() } #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn modulus_minus_one_div_two(&self) -> Self::Inner { let value = self.0.params().modulus().clone().get(); (value - BoxedUint::one()) / NonZero::new(BoxedUint::from(2_u8)).unwrap() } + #[inline(always)] fn make_cfg(modulus: &Self::Inner) -> Result { let Some(modulus) = Odd::new(modulus.clone()).into_option() else { return Err(FieldError::InvalidModulus); @@ -459,22 +499,27 @@ impl PrimeField for BoxedMontyField { Ok(BoxedMontyParams::new(modulus)) } + #[inline(always)] fn new_with_cfg(inner: Self::Inner, cfg: &Self::Config) -> Self { Self(BoxedMontyForm::new(inner, cfg.clone())) } + #[inline(always)] fn new_unchecked_with_cfg(inner: Self::Inner, cfg: &Self::Config) -> Self { Self(BoxedMontyForm::from_montgomery(inner, cfg.clone())) } + #[inline(always)] fn zero_with_cfg(cfg: &Self::Config) -> Self { Self(BoxedMontyForm::zero(cfg.clone())) } + #[inline(always)] fn is_zero_with_cfg(&self, _cfg: &Self::Config) -> bool { self.0.is_zero().into() } + #[inline(always)] fn one_with_cfg(cfg: &Self::Config) -> Self { Self(BoxedMontyForm::one(cfg.clone())) } diff --git a/src/field/crypto_bigint_const_monty.rs b/src/field/crypto_bigint_const_monty.rs index ebb1a7e..166afca 100644 --- a/src/field/crypto_bigint_const_monty.rs +++ b/src/field/crypto_bigint_const_monty.rs @@ -54,41 +54,49 @@ impl, const LIMBS: usize> ConstMontyField { /// Retrieves the integer currently encoded in this [`ConstMontyForm`], /// guaranteed to be reduced. + #[inline(always)] pub const fn retrieve(&self) -> Uint { Uint::new(self.0.retrieve()) } /// Access the value in Montgomery form. + #[inline(always)] pub const fn as_montgomery(&self) -> &Uint { Uint::new_ref(self.0.as_montgomery()) } /// Mutably access the value in Montgomery form. + #[inline(always)] pub fn as_montgomery_mut(&mut self) -> &mut Uint { Uint::new_ref_mut(self.0.as_montgomery_mut()) } /// Create a `ConstMontyForm` from a value in Montgomery form. + #[inline(always)] pub const fn from_montgomery(integer: Uint) -> Self { Self(ConstMontyForm::from_montgomery(integer.into_inner())) } /// Extract the value from the `ConstMontyForm` in Montgomery form. + #[inline(always)] pub const fn to_montgomery(&self) -> Uint { Uint::new(self.0.to_montgomery()) } /// Performs division by 2, that is returns `x` such that `x + x = self`. + #[inline(always)] pub const fn div_by_2(&self) -> Self { Self(self.0.div_by_2()) } /// Double `self`. + #[inline(always)] pub const fn double(&self) -> Self { Self(self.0.double()) } /// See [ConstMontyForm::pow_bounded_exp]. + #[inline(always)] pub const fn pow_bounded_exp( &self, exponent: &Uint, @@ -103,12 +111,14 @@ impl, const LIMBS: usize> ConstMontyField { // impl, const LIMBS: usize> Debug for ConstMontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { Debug::fmt(&self.0, f) } } impl, const LIMBS: usize> Display for ConstMontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "{} (mod {})", self.0.retrieve(), Mod::PARAMS.modulus()) } @@ -122,18 +132,21 @@ impl, const LIMBS: usize> Default for ConstMontyField, const LIMBS: usize> PartialOrd for ConstMontyField { + #[inline(always)] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl, const LIMBS: usize> Ord for ConstMontyField { + #[inline(always)] fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(self.0.as_montgomery(), other.0.as_montgomery()) } } impl, const LIMBS: usize> Hash for ConstMontyField { + #[inline(always)] fn hash(&self, state: &mut H) { self.0.as_montgomery().hash(state) } @@ -142,6 +155,7 @@ impl, const LIMBS: usize> Hash for ConstMontyField, const LIMBS: usize> FromStr for ConstMontyField { type Err = (); + #[inline(always)] fn from_str(s: &str) -> Result { let uint = Uint::::from_str(s)?; Ok(Self(ConstMontyForm::new(uint.inner()))) @@ -250,6 +264,7 @@ impl, const LIMBS: usize> Div for ConstMontyField impl, const LIMBS: usize> Div<&Self> for ConstMontyField { type Output = Self; + #[inline(always)] fn div(self, rhs: &Self) -> Self::Output { self.checked_div(rhs).expect("Division by zero") } @@ -267,6 +282,7 @@ impl, const LIMBS: usize> Div for &ConstMontyField, const LIMBS: usize> Pow for ConstMontyField { type Output = Self; + #[inline(always)] fn pow(self, rhs: u32) -> Self::Output { Self(self.0.pow(&crypto_bigint::U64::from_u32(rhs))) } @@ -275,6 +291,7 @@ impl, const LIMBS: usize> Pow for ConstMontyField, const LIMBS: usize> Inv for ConstMontyField { type Output = Option; + #[inline(always)] fn inv(self) -> Self::Output { Some(Self(Option::from(self.0.invert_vartime())?)) } @@ -287,6 +304,7 @@ impl, const LIMBS: usize> Inv for ConstMontyField impl, const LIMBS: usize> CheckedDiv for ConstMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn checked_div(&self, rhs: &Self) -> Option { Some(self * rhs.inv()?) } @@ -299,12 +317,14 @@ impl, const LIMBS: usize> CheckedDiv for ConstMontyField { impl, const LIMBS: usize> $trait for ConstMontyField { + #[inline(always)] fn $method(&mut self, rhs: Self) { // Use reference for inner call to avoid moves of rhs.0 where not needed *self = self.$inner(&rhs); } } impl, const LIMBS: usize> $trait<&Self> for ConstMontyField { + #[inline(always)] fn $method(&mut self, rhs: &Self) { *self = self.$inner(rhs); } @@ -323,6 +343,7 @@ impl_field_op_assign!(DivAssign, div_assign, div); impl, const LIMBS: usize> Sum for ConstMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn sum>(iter: I) -> Self { iter.fold(Self::ZERO, |acc, x| acc + x) } @@ -330,6 +351,7 @@ impl, const LIMBS: usize> Sum for ConstMontyField impl<'a, Mod: Params, const LIMBS: usize> Sum<&'a Self> for ConstMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn sum>(iter: I) -> Self { iter.fold(Self::ZERO, |acc, x| acc + x) } @@ -337,6 +359,7 @@ impl<'a, Mod: Params, const LIMBS: usize> Sum<&'a Self> for ConstMontyFie impl, const LIMBS: usize> Product for ConstMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn product>(iter: I) -> Self { iter.fold(Self::ONE, |acc, x| acc * x) } @@ -344,6 +367,7 @@ impl, const LIMBS: usize> Product for ConstMontyField, const LIMBS: usize> Product<&'a Self> for ConstMontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn product>(iter: I) -> Self { iter.fold(Self::ONE, |acc, x| acc * x) } @@ -374,6 +398,7 @@ impl, const LIMBS: usize> From> impl, const LIMBS: usize> From<&ConstMontyField> for ConstMontyField { + #[inline(always)] fn from(value: &Self) -> Self { *value } @@ -384,6 +409,7 @@ macro_rules! impl_from_unsigned { ($($t:ty),* $(,)?) => { $( impl, const LIMBS: usize> From<$t> for ConstMontyField { + #[inline(always)] fn from(value: $t) -> Self { let value = Uint::from(value); Self(ConstMontyForm::new(value.inner())) @@ -391,6 +417,7 @@ macro_rules! impl_from_unsigned { } impl, const LIMBS: usize> From<&$t> for ConstMontyField { + #[inline(always)] fn from(value: &$t) -> Self { Self::from(*value) } @@ -405,6 +432,7 @@ macro_rules! impl_from_signed { $( impl, const LIMBS: usize> From<$t> for ConstMontyField { #![allow(clippy::arithmetic_side_effects)] + #[inline(always)] fn from(value: $t) -> Self { let magnitude = Uint::from(value.abs_diff(0)); let form = ConstMontyForm::new(magnitude.inner()); @@ -413,6 +441,7 @@ macro_rules! impl_from_signed { } impl, const LIMBS: usize> From<&$t> for ConstMontyField { + #[inline(always)] fn from(value: &$t) -> Self { Self::from(*value) } @@ -425,30 +454,35 @@ impl_from_unsigned!(u8, u16, u32, u64, u128); impl_from_signed!(i8, i16, i32, i64, i128); impl, const LIMBS: usize> From for ConstMontyField { + #[inline(always)] fn from(value: bool) -> Self { if value { Self::ONE } else { Self::ZERO } } } impl, const LIMBS: usize> From for ConstMontyField { + #[inline(always)] fn from(value: Boolean) -> Self { Self::from(*value) } } impl, const LIMBS: usize> From<&Boolean> for ConstMontyField { + #[inline(always)] fn from(value: &Boolean) -> Self { Self::from(**value) } } impl, const LIMBS: usize> From> for ConstMontyField { + #[inline(always)] fn from(value: Uint) -> Self { Self::from(&value) } } impl, const LIMBS: usize> From<&Uint> for ConstMontyField { + #[inline(always)] fn from(value: &Uint) -> Self { Self(ConstMontyForm::new(value.inner())) } @@ -457,6 +491,7 @@ impl, const LIMBS: usize> From<&Uint> for ConstMontyFi impl, const LIMBS: usize, const LIMBS2: usize> From> for ConstMontyField { + #[inline(always)] fn from(value: Int) -> Self { Self::from(value.inner()) } @@ -465,6 +500,7 @@ impl, const LIMBS: usize, const LIMBS2: usize> From, const LIMBS: usize, const LIMBS2: usize> From<&Int> for ConstMontyField { + #[inline(always)] fn from(value: &Int) -> Self { Self::from(value.inner()) } @@ -473,6 +509,7 @@ impl, const LIMBS: usize, const LIMBS2: usize> From<&Int, const LIMBS: usize, const LIMBS2: usize> From> for ConstMontyField { + #[inline(always)] fn from(value: crypto_bigint::Int) -> Self { Self::from(&value) } @@ -482,6 +519,7 @@ impl, const LIMBS: usize, const LIMBS2: usize> From<&crypto_b for ConstMontyField { #![allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn from(value: &crypto_bigint::Int) -> Self { assert!( LIMBS >= LIMBS2, @@ -551,6 +589,7 @@ impl, const LIMBS: usize> ConstPrimeField for ConstMontyField impl, const LIMBS: usize> Distribution> for StandardUniform { + #[inline(always)] fn sample(&self, rng: &mut R) -> ConstMontyField { crypto_bigint::Random::random(rng) } @@ -558,6 +597,7 @@ impl, const LIMBS: usize> Distribution, const LIMBS: usize> crypto_bigint::Random for ConstMontyField { + #[inline(always)] fn try_random(rng: &mut R) -> Result { ConstMontyForm::try_random(rng).map(Self) } @@ -602,6 +642,7 @@ where // impl, const LIMBS: usize> ConstantTimeEq for ConstMontyField { + #[inline(always)] fn ct_eq(&self, other: &Self) -> Choice { self.0.ct_eq(&other.0) } @@ -631,6 +672,7 @@ impl, const LIMBS: usize> crypto_bigint::One for ConstMontyFi } impl, const LIMBS: usize> crypto_bigint::Square for ConstMontyField { + #[inline(always)] fn square(&self) -> Self { Self(self.0.square()) } @@ -639,6 +681,7 @@ impl, const LIMBS: usize> crypto_bigint::Square for ConstMont impl, const LIMBS: usize> Retrieve for ConstMontyField { type Output = Uint; + #[inline(always)] fn retrieve(&self) -> Self::Output { self.retrieve() } diff --git a/src/field/crypto_bigint_monty.rs b/src/field/crypto_bigint_monty.rs index 0fc95b0..4659e5d 100644 --- a/src/field/crypto_bigint_monty.rs +++ b/src/field/crypto_bigint_monty.rs @@ -46,41 +46,49 @@ impl MontyField { /// Retrieves the integer currently encoded in this [`MontyForm`], /// guaranteed to be reduced. + #[inline(always)] pub const fn retrieve(&self) -> Uint { Uint::new(self.0.retrieve()) } /// Access the value in Montgomery form. + #[inline(always)] pub const fn as_montgomery(&self) -> &Uint { Uint::new_ref(self.0.as_montgomery()) } /// Mutably access the value in Montgomery form. + #[inline(always)] pub fn as_montgomery_mut(&mut self) -> &mut Uint { Uint::new_ref_mut(self.0.as_montgomery_mut()) } /// Create a `MontyField` from a value in Montgomery form. + #[inline(always)] pub const fn from_montgomery(integer: Uint, config: &MontyParams) -> Self { Self(MontyForm::from_montgomery(integer.into_inner(), *config)) } /// Extract the value from the `MontyForm` in Montgomery form. + #[inline(always)] pub const fn to_montgomery(&self) -> Uint { Uint::new(self.0.to_montgomery()) } /// Performs division by 2, that is returns `x` such that `x + x = self`. + #[inline(always)] pub const fn div_by_2(&self) -> Self { Self(self.0.div_by_2()) } /// Double `self`. + #[inline(always)] pub const fn double(&self) -> Self { Self(self.0.double()) } /// See [MontyForm::pow_bounded_exp]. + #[inline(always)] pub const fn pow_bounded_exp( &self, exponent: &Uint, @@ -95,12 +103,14 @@ impl MontyField { // impl Debug for MontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { Debug::fmt(&self.0, f) } } impl Display for MontyField { + #[inline(always)] fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!( f, @@ -112,6 +122,7 @@ impl Display for MontyField { } impl PartialOrd for MontyField { + #[inline(always)] fn partial_cmp(&self, other: &Self) -> Option { if self.modulus() != other.modulus() { return None; @@ -121,6 +132,7 @@ impl PartialOrd for MontyField { } impl Hash for MontyField { + #[inline(always)] fn hash(&self, state: &mut H) { self.0.as_montgomery().hash(state) } @@ -133,6 +145,7 @@ impl Hash for MontyField { impl Neg for MontyField { type Output = Self; + #[inline(always)] fn neg(self) -> Self::Output { Self(self.0.neg()) } @@ -185,6 +198,7 @@ impl_basic_op!(Mul, mul); impl Div for MontyField { type Output = Self; + #[inline(always)] fn div(self, rhs: Self) -> Self::Output { self.div(&rhs) } @@ -193,6 +207,7 @@ impl Div for MontyField { impl Div<&Self> for MontyField { type Output = Self; + #[inline(always)] fn div(self, rhs: &Self) -> Self::Output { self.checked_div(rhs).expect("Division by zero") } @@ -201,6 +216,7 @@ impl Div<&Self> for MontyField { impl Div for &MontyField { type Output = MontyField; + #[inline(always)] fn div(self, rhs: Self) -> Self::Output { self.checked_div(rhs).expect("Division by zero") } @@ -209,6 +225,7 @@ impl Div for &MontyField { impl Div> for &MontyField { type Output = MontyField; + #[inline(always)] fn div(self, rhs: MontyField) -> Self::Output { self.div(&rhs) } @@ -217,6 +234,7 @@ impl Div> for &MontyField { impl Pow for MontyField { type Output = Self; + #[inline(always)] fn pow(self, rhs: u32) -> Self::Output { Self(self.0.pow(&crypto_bigint::Uint::<1>::from(rhs))) } @@ -225,6 +243,7 @@ impl Pow for MontyField { impl Inv for MontyField { type Output = Option; + #[inline(always)] fn inv(self) -> Self::Output { Some(Self(Option::from(self.0.invert_vartime())?)) } @@ -233,6 +252,7 @@ impl Inv for MontyField { impl Inv for &MontyField { type Output = Option>; + #[inline(always)] fn inv(self) -> Self::Output { Some(MontyField(Option::from(self.0.invert_vartime())?)) } @@ -245,6 +265,7 @@ impl Inv for &MontyField { impl CheckedDiv for MontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn checked_div(&self, rhs: &Self) -> Option { Some(self * rhs.inv()?) } @@ -257,11 +278,13 @@ impl CheckedDiv for MontyField { macro_rules! impl_field_op_assign { ($trait:ident, $method:ident) => { impl $trait for MontyField { + #[inline(always)] fn $method(&mut self, rhs: Self) { self.0.$method(&rhs.0); } } impl $trait<&Self> for MontyField { + #[inline(always)] fn $method(&mut self, rhs: &Self) { self.0.$method(&rhs.0); } @@ -274,12 +297,14 @@ impl_field_op_assign!(SubAssign, sub_assign); impl_field_op_assign!(MulAssign, mul_assign); impl DivAssign for MontyField { + #[inline(always)] fn div_assign(&mut self, rhs: Self) { self.div_assign(&rhs); } } impl DivAssign<&Self> for MontyField { + #[inline(always)] fn div_assign(&mut self, rhs: &Self) { self.0.mul_assign(rhs.0.invert().unwrap()) } @@ -290,6 +315,7 @@ impl DivAssign<&Self> for MontyField { // impl Sum for MontyField { + #[inline(always)] fn sum>(mut iter: I) -> Self { let Some(MontyField(first)) = iter.next() else { panic!("Sum of an empty iterator is not defined for MontyField"); @@ -299,6 +325,7 @@ impl Sum for MontyField { } impl<'a, const LIMBS: usize> Sum<&'a Self> for MontyField { + #[inline(always)] fn sum>(mut iter: I) -> Self { let Some(MontyField(first)) = iter.next() else { panic!("Sum of an empty iterator is not defined for MontyField"); @@ -308,6 +335,7 @@ impl<'a, const LIMBS: usize> Sum<&'a Self> for MontyField { } impl Product for MontyField { + #[inline(always)] fn product>(mut iter: I) -> Self { let Some(MontyField(first)) = iter.next() else { panic!("Product of an empty iterator is not defined for MontyField"); @@ -318,6 +346,7 @@ impl Product for MontyField { impl<'a, const LIMBS: usize> Product<&'a Self> for MontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn product>(mut iter: I) -> Self { let Some(MontyField(first)) = iter.next() else { panic!("Product of an empty iterator is not defined for MontyField"); @@ -345,6 +374,7 @@ impl From> for MontyForm { } impl From<&MontyField> for MontyField { + #[inline(always)] fn from(value: &Self) -> Self { value.clone() } @@ -354,6 +384,7 @@ macro_rules! impl_from_unsigned { ($($t:ty),* $(,)?) => { $( implFromWithConfig<$t> for MontyField { + #[inline(always)] fn from_with_cfg(value: $t, cfg: &Self::Config) -> Self { let abs: crypto_bigint::Uint = value.into(); Self(MontyForm::::new(&abs, *cfg)) @@ -361,6 +392,7 @@ macro_rules! impl_from_unsigned { } implFromWithConfig<&$t> for MontyField { + #[inline(always)] fn from_with_cfg(value: &$t, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } @@ -374,6 +406,7 @@ macro_rules! impl_from_signed { $( #[allow(clippy::arithmetic_side_effects)] // False alert implFromWithConfig<$t> for MontyField { + #[inline(always)] fn from_with_cfg(value: $t, cfg: &Self::Config) -> Self { let magnitude = Uint::from(value.abs_diff(0)); let form = MontyForm::new(magnitude.inner(), cfg.clone()); @@ -382,6 +415,7 @@ macro_rules! impl_from_signed { } implFromWithConfig<&$t> for MontyField { + #[inline(always)] fn from_with_cfg(value: &$t, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } @@ -394,6 +428,7 @@ impl_from_unsigned!(u8, u16, u32, u64, u128); impl_from_signed!(i8, i16, i32, i64, i128); impl FromWithConfig for MontyField { + #[inline(always)] fn from_with_cfg(value: bool, cfg: &Self::Config) -> Self { let abs = if value { crypto_bigint::Uint::one() @@ -405,24 +440,28 @@ impl FromWithConfig for MontyField { } impl FromWithConfig<&bool> for MontyField { + #[inline(always)] fn from_with_cfg(value: &bool, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig for MontyField { + #[inline(always)] fn from_with_cfg(value: Boolean, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig<&Boolean> for MontyField { + #[inline(always)] fn from_with_cfg(value: &Boolean, cfg: &Self::Config) -> Self { Self::from_with_cfg(*value, cfg) } } impl FromWithConfig> for MontyField { + #[inline(always)] fn from_with_cfg(value: Int, cfg: &Self::Config) -> Self { Self::from_with_cfg(&value, cfg) } @@ -430,6 +469,7 @@ impl FromWithConfig> for Mo impl FromWithConfig<&Int> for MontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn from_with_cfg(value: &Int, cfg: &Self::Config) -> Self { let mut abs = value.inner().abs(); if LIMBS < LIMBS2 { @@ -444,6 +484,7 @@ impl FromWithConfig<&Int> for M } impl FromWithConfig> for MontyField { + #[inline(always)] fn from_with_cfg(value: Uint, cfg: &Self::Config) -> Self { Self::from_with_cfg(&value, cfg) } @@ -451,6 +492,7 @@ impl FromWithConfig> for M impl FromWithConfig<&Uint> for MontyField { #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn from_with_cfg(value: &Uint, cfg: &Self::Config) -> Self { if LIMBS >= LIMBS2 { Self::new(MontyForm::new(&value.inner().resize(), *cfg)) @@ -493,15 +535,18 @@ impl Field for MontyField { impl PrimeField for MontyField { type Config = MontyParams; + #[inline(always)] fn cfg(&self) -> &Self::Config { self.0.params() } + #[inline(always)] fn modulus(&self) -> Self::Inner { Uint::new(self.0.params().modulus().get()) } #[allow(clippy::arithmetic_side_effects)] // False alert + #[inline(always)] fn modulus_minus_one_div_two(&self) -> Self::Inner { let value = self.0.params().modulus().get(); Uint::new( @@ -510,6 +555,7 @@ impl PrimeField for MontyField { ) } + #[inline(always)] fn make_cfg(modulus: &Self::Inner) -> Result { let Some(modulus) = Odd::new(*modulus.inner()).into_option() else { return Err(FieldError::InvalidModulus); @@ -517,22 +563,27 @@ impl PrimeField for MontyField { Ok(MontyParams::new(modulus)) } + #[inline(always)] fn new_with_cfg(inner: Self::Inner, cfg: &Self::Config) -> Self { Self(MontyForm::new(inner.inner(), *cfg)) } + #[inline(always)] fn new_unchecked_with_cfg(inner: Self::Inner, cfg: &Self::Config) -> Self { Self(MontyForm::from_montgomery(inner.into_inner(), *cfg)) } + #[inline(always)] fn zero_with_cfg(cfg: &Self::Config) -> Self { Self(MontyForm::zero(*cfg)) } + #[inline(always)] fn is_zero_with_cfg(&self, _cfg: &Self::Config) -> bool { self.0.as_montgomery().is_zero() } + #[inline(always)] fn one_with_cfg(cfg: &Self::Config) -> Self { Self(MontyForm::one(*cfg)) } diff --git a/src/semiring/boolean.rs b/src/semiring/boolean.rs index e3eb90d..4b1144c 100644 --- a/src/semiring/boolean.rs +++ b/src/semiring/boolean.rs @@ -152,7 +152,7 @@ macro_rules! impl_from_boolean_for { impl From for $to { #[inline(always)] fn from(value: Boolean) -> Self { - value.0 as $to + <$to>::from(value.0) } } )*};