From 378f5cfd08faae99da939d99ae4d0ff1c3ade725 Mon Sep 17 00:00:00 2001 From: Tomas Krnak Date: Wed, 17 Aug 2022 17:39:04 +0200 Subject: [PATCH 1/3] Add `uninline-portable` feature --- CHANGELOG.md | 4 ++++ Cargo.toml | 1 + src/fields/fp.rs | 10 +++++----- src/fields/fq.rs | 10 +++++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 937e25c..153c895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to Rust's notion of [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `uninline-portable` feature flag, which disables inlining of some functions. + This is useful for tiny microchips (such as ARM Cortex-M0), where inlining + can hurt performance and blow up binary size. ## [0.4.0] - 2022-05-05 ### Changed diff --git a/Cargo.toml b/Cargo.toml index 071faa0..2e4859e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,3 +64,4 @@ bits = ["ff/bits"] gpu = ["alloc", "ec-gpu"] sqrt-table = ["alloc", "lazy_static"] repr-c = [] +uninline-portable = [] diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 6d8db0d..ee1fe39 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -293,7 +293,7 @@ impl Fp { } /// Squares this element. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn square(&self) -> Fp { let (r1, carry) = mac(0, self.0[0], self.0[1], 0); let (r2, carry) = mac(0, self.0[0], self.0[2], carry); @@ -373,7 +373,7 @@ impl Fp { } /// Multiplies `rhs` by `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn mul(&self, rhs: &Self) -> Self { // Schoolbook multiplication @@ -401,7 +401,7 @@ impl Fp { } /// Subtracts `rhs` from `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn sub(&self, rhs: &Self) -> Self { let (d0, borrow) = sbb(self.0[0], rhs.0[0], 0); let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow); @@ -419,7 +419,7 @@ impl Fp { } /// Adds `rhs` to `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn add(&self, rhs: &Self) -> Self { let (d0, carry) = adc(self.0[0], rhs.0[0], 0); let (d1, carry) = adc(self.0[1], rhs.0[1], carry); @@ -432,7 +432,7 @@ impl Fp { } /// Negates `self`. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn neg(&self) -> Self { // Subtract `self` from `MODULUS` to negate. Ignore the final // borrow because it cannot underflow; self is guaranteed to diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 0a08718..85227dd 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -293,7 +293,7 @@ impl Fq { } /// Squares this element. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn square(&self) -> Fq { let (r1, carry) = mac(0, self.0[0], self.0[1], 0); let (r2, carry) = mac(0, self.0[0], self.0[2], carry); @@ -373,7 +373,7 @@ impl Fq { } /// Multiplies `rhs` by `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn mul(&self, rhs: &Self) -> Self { // Schoolbook multiplication @@ -401,7 +401,7 @@ impl Fq { } /// Subtracts `rhs` from `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn sub(&self, rhs: &Self) -> Self { let (d0, borrow) = sbb(self.0[0], rhs.0[0], 0); let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow); @@ -419,7 +419,7 @@ impl Fq { } /// Adds `rhs` to `self`, returning the result. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn add(&self, rhs: &Self) -> Self { let (d0, carry) = adc(self.0[0], rhs.0[0], 0); let (d1, carry) = adc(self.0[1], rhs.0[1], carry); @@ -432,7 +432,7 @@ impl Fq { } /// Negates `self`. - #[inline] + #[cfg_attr(inline, not(feature = "uninline-portable"))] pub const fn neg(&self) -> Self { // Subtract `self` from `MODULUS` to negate. Ignore the final // borrow because it cannot underflow; self is guaranteed to From 52af8293a8e31d27ae0cb712505cf6daf2f36549 Mon Sep 17 00:00:00 2001 From: Tomas Krnak Date: Thu, 13 Oct 2022 10:54:30 +0200 Subject: [PATCH 2/3] fixup! Add `uninline-portable` feature --- src/fields/fp.rs | 10 +++++----- src/fields/fq.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/fields/fp.rs b/src/fields/fp.rs index ee1fe39..332598b 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -293,7 +293,7 @@ impl Fp { } /// Squares this element. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn square(&self) -> Fp { let (r1, carry) = mac(0, self.0[0], self.0[1], 0); let (r2, carry) = mac(0, self.0[0], self.0[2], carry); @@ -373,7 +373,7 @@ impl Fp { } /// Multiplies `rhs` by `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn mul(&self, rhs: &Self) -> Self { // Schoolbook multiplication @@ -401,7 +401,7 @@ impl Fp { } /// Subtracts `rhs` from `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn sub(&self, rhs: &Self) -> Self { let (d0, borrow) = sbb(self.0[0], rhs.0[0], 0); let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow); @@ -419,7 +419,7 @@ impl Fp { } /// Adds `rhs` to `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn add(&self, rhs: &Self) -> Self { let (d0, carry) = adc(self.0[0], rhs.0[0], 0); let (d1, carry) = adc(self.0[1], rhs.0[1], carry); @@ -432,7 +432,7 @@ impl Fp { } /// Negates `self`. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn neg(&self) -> Self { // Subtract `self` from `MODULUS` to negate. Ignore the final // borrow because it cannot underflow; self is guaranteed to diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 85227dd..c0296c4 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -293,7 +293,7 @@ impl Fq { } /// Squares this element. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn square(&self) -> Fq { let (r1, carry) = mac(0, self.0[0], self.0[1], 0); let (r2, carry) = mac(0, self.0[0], self.0[2], carry); @@ -373,7 +373,7 @@ impl Fq { } /// Multiplies `rhs` by `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn mul(&self, rhs: &Self) -> Self { // Schoolbook multiplication @@ -401,7 +401,7 @@ impl Fq { } /// Subtracts `rhs` from `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn sub(&self, rhs: &Self) -> Self { let (d0, borrow) = sbb(self.0[0], rhs.0[0], 0); let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow); @@ -419,7 +419,7 @@ impl Fq { } /// Adds `rhs` to `self`, returning the result. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn add(&self, rhs: &Self) -> Self { let (d0, carry) = adc(self.0[0], rhs.0[0], 0); let (d1, carry) = adc(self.0[1], rhs.0[1], carry); @@ -432,7 +432,7 @@ impl Fq { } /// Negates `self`. - #[cfg_attr(inline, not(feature = "uninline-portable"))] + #[cfg_attr(not(feature = "uninline-portable"), inline)] pub const fn neg(&self) -> Self { // Subtract `self` from `MODULUS` to negate. Ignore the final // borrow because it cannot underflow; self is guaranteed to From b889295e3126841a6a24675d3815bb4c96949d3c Mon Sep 17 00:00:00 2001 From: Tomas Krnak Date: Thu, 13 Oct 2022 11:07:19 +0200 Subject: [PATCH 3/3] fixup! Add `uninline-portable` feature --- src/fields/fp.rs | 2 +- src/fields/fq.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 332598b..0c0028c 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -325,7 +325,7 @@ impl Fp { } #[allow(clippy::too_many_arguments)] - #[inline(always)] + #[cfg_attr(not(feature = "uninline-portable"), inline(always))] const fn montgomery_reduce( r0: u64, r1: u64, diff --git a/src/fields/fq.rs b/src/fields/fq.rs index c0296c4..4d80c69 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -325,7 +325,7 @@ impl Fq { } #[allow(clippy::too_many_arguments)] - #[inline(always)] + #[cfg_attr(not(feature = "uninline-portable"), inline(always))] const fn montgomery_reduce( r0: u64, r1: u64,