From c818f89db699145c1757f65d0081554022ef9b83 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 18 Oct 2023 15:03:00 -0400 Subject: [PATCH] Add inline annotations on conditional_select in p256, k256, and primeorder This seems to help situations where the Rust compiler otherwise will not completely inline conditional_select, causing algoritms which depend on this to become significantly slower than they would otherwise. See #940 for discussion. --- k256/src/arithmetic/field.rs | 1 + k256/src/arithmetic/field/field_10x26.rs | 1 + k256/src/arithmetic/field/field_5x52.rs | 1 + k256/src/arithmetic/field/field_impl.rs | 1 + p256/src/arithmetic/field.rs | 1 + primeorder/src/affine.rs | 1 + primeorder/src/projective.rs | 1 + 7 files changed, 7 insertions(+) diff --git a/k256/src/arithmetic/field.rs b/k256/src/arithmetic/field.rs index 6e97ee78..7e68f1b4 100644 --- a/k256/src/arithmetic/field.rs +++ b/k256/src/arithmetic/field.rs @@ -323,6 +323,7 @@ impl PrimeField for FieldElement { } impl ConditionallySelectable for FieldElement { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self(FieldElementImpl::conditional_select(&(a.0), &(b.0), choice)) } diff --git a/k256/src/arithmetic/field/field_10x26.rs b/k256/src/arithmetic/field/field_10x26.rs index d3c5706d..6ea525a0 100644 --- a/k256/src/arithmetic/field/field_10x26.rs +++ b/k256/src/arithmetic/field/field_10x26.rs @@ -674,6 +674,7 @@ impl Default for FieldElement10x26 { } impl ConditionallySelectable for FieldElement10x26 { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self([ u32::conditional_select(&a.0[0], &b.0[0], choice), diff --git a/k256/src/arithmetic/field/field_5x52.rs b/k256/src/arithmetic/field/field_5x52.rs index 06735ade..3653ec50 100644 --- a/k256/src/arithmetic/field/field_5x52.rs +++ b/k256/src/arithmetic/field/field_5x52.rs @@ -461,6 +461,7 @@ impl Default for FieldElement5x52 { } impl ConditionallySelectable for FieldElement5x52 { + #[inline(always)] fn conditional_select( a: &FieldElement5x52, b: &FieldElement5x52, diff --git a/k256/src/arithmetic/field/field_impl.rs b/k256/src/arithmetic/field/field_impl.rs index 4fa1f4f5..6c7820b1 100644 --- a/k256/src/arithmetic/field/field_impl.rs +++ b/k256/src/arithmetic/field/field_impl.rs @@ -142,6 +142,7 @@ impl Default for FieldElementImpl { } impl ConditionallySelectable for FieldElementImpl { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { // 1. It's debug only, so it shouldn't present a security risk // 2. Being normalized does is independent from the field element value; diff --git a/p256/src/arithmetic/field.rs b/p256/src/arithmetic/field.rs index ea153a88..86462f6a 100644 --- a/p256/src/arithmetic/field.rs +++ b/p256/src/arithmetic/field.rs @@ -473,6 +473,7 @@ impl PrimeField for FieldElement { } impl ConditionallySelectable for FieldElement { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self(U256::conditional_select(&a.0, &b.0, choice)) } diff --git a/primeorder/src/affine.rs b/primeorder/src/affine.rs index 3fb01d90..6801c5d3 100644 --- a/primeorder/src/affine.rs +++ b/primeorder/src/affine.rs @@ -96,6 +96,7 @@ impl ConditionallySelectable for AffinePoint where C: PrimeCurveParams, { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self { x: C::FieldElement::conditional_select(&a.x, &b.x, choice), diff --git a/primeorder/src/projective.rs b/primeorder/src/projective.rs index 5549543f..dc30f532 100644 --- a/primeorder/src/projective.rs +++ b/primeorder/src/projective.rs @@ -172,6 +172,7 @@ impl ConditionallySelectable for ProjectivePoint where C: PrimeCurveParams, { + #[inline(always)] fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Self { x: C::FieldElement::conditional_select(&a.x, &b.x, choice),