Skip to content

Gate s2n-bignum _alt variants under OPENSSL_SMALL#3320

Open
justsmth wants to merge 3 commits into
aws:mainfrom
justsmth:openssl-small-gate-s2n-bignum
Open

Gate s2n-bignum _alt variants under OPENSSL_SMALL#3320
justsmth wants to merge 3 commits into
aws:mainfrom
justsmth:openssl-small-gate-s2n-bignum

Conversation

@justsmth

@justsmth justsmth commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Issues

Addresses: aws/aws-lc-rs#745, P353434599

Description of changes:

Under OPENSSL_SMALL on aarch64, AWS-LC currently still compiles both the primary and _alt s2n-bignum assembly for every EC and curve25519 operation and chooses between them at runtime. This change pins each operation to the single universally-compatible (non-_alt) variant at compile time and drops the _alt form from the build. That removes ~254 KB of object code on aarch64 in exchange for the wide-multiplier fast paths (Graviton 3, Apple M-series), which is an acceptable trade under the OPENSSL_SMALL contract.

On x86_64, OPENSSL_SMALL implies MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX (#3319), which disables s2n-bignum entirely — so no variant pinning is needed there.

The non-OPENSSL_SMALL runtime-dispatch path is left byte-for-byte identical, so only OPENSSL_SMALL builds change behavior. The only files touched are the AWS-LC-maintained selector header third_party/s2n-bignum/s2n-bignum_aws-lc.h and crypto/fipsmodule/CMakeLists.txt — no upstream-imported s2n-bignum assembly is modified, so this survives the next s2n-bignum import.

Call-outs:

  • This PR depends on OPENSSL_SMALL: imply MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX on x86_64 #3319 landing first. Without the OPENSSL_SMALL → MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX implication, s2n-bignum would still be compiled on x86_64 and the selectors here would call the non-_alt (BMI2/ADX-requiring) variants unconditionally, which is incorrect on older CPUs.
  • Performance under OPENSSL_SMALL regresses on aarch64 hardware that would have used the dropped _alt fast path (wide-multiplier cores like Graviton 3 / Apple M-series).

Testing:

Built crypto_test with -DOPENSSL_SMALL=ON on x86_64 and ran the EC (P-224/256/384/521), ECDH, ECDSA, X25519, and Ed25519 suites — all passing. New CI workflow (.github/workflows/openssl-small.yml) runs the full test suite under OPENSSL_SMALL on both x86_64 and aarch64 with gcc and clang, and asserts a minimum binary-size reduction threshold.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

Every s2n-bignum EC and curve25519 operation ships two assembly
implementations and dispatches between them at runtime:

  - x86_64: the primary uses BMI2+ADX; the "_alt" form is the generic
    fallback that runs on any x86_64 CPU.
  - aarch64: the primary is generic; the "_alt" form targets cores with
    higher multiplier throughput (Graviton 3, Apple M1+).

Under OPENSSL_SMALL, pin each operation to the single, universally
compatible variant at compile time and drop the other from the build:
x86_64 keeps "_alt", aarch64 keeps the non-"_alt" form. This removes
~18 object files (~363 KB on x86_64; aarch64 TBD) at the cost of the
microarchitecture-specific fast paths, which is acceptable under the
OPENSSL_SMALL contract.

The selector header pins the symbol (so only the kept variant is
referenced) and crypto/fipsmodule/CMakeLists.txt drops the unused .S
from BCM_ASM_SOURCES. The two are exact mirrors, so no build or link
references a dropped object on any platform.

Non-OPENSSL_SMALL builds are unchanged: the runtime dispatch path is
byte-for-byte identical to before.

This is independent of the OPENSSL_SMALL AVX-512 size reduction. When
that work is also present, OPENSSL_SMALL disables s2n-bignum on x86_64
entirely (Fiat fallback), so the x86_64 removal here becomes a no-op
and aarch64 is the sole beneficiary; when it is not present, the
x86_64 path above is operative.

Testing: built crypto_test with -DOPENSSL_SMALL=ON on x86_64 and ran
the EC (P-224/256/384/521), ECDH, ECDSA, X25519, and Ed25519 suites
(51 tests, all passing). Verified at the object level that the dropped
primaries are absent from libcrypto.a and all _alt references resolve.
aarch64 runtime testing still pending.
@codecov-commenter

codecov-commenter commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.17%. Comparing base (0b4e4ea) to head (a3a7b2a).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3320      +/-   ##
==========================================
- Coverage   78.17%   78.17%   -0.01%     
==========================================
  Files         693      693              
  Lines      123874   123900      +26     
  Branches    17200    17208       +8     
==========================================
+ Hits        96840    96856      +16     
- Misses      26116    26125       +9     
- Partials      918      919       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@justsmth justsmth marked this pull request as ready for review June 25, 2026 21:46
@justsmth justsmth requested a review from a team as a code owner June 25, 2026 21:46
@justsmth justsmth changed the title [DRAFT] Gate s2n-bignum _alt variants under OPENSSL_SMALL Gate s2n-bignum _alt variants under OPENSSL_SMALL Jun 25, 2026
Adds .github/workflows/openssl-small.yml with two jobs: (1) size-reduction (x86_64, advisory): link a minimal static consumer (tests/binary-size/main.c) against libcrypto built with and without OPENSSL_SMALL and assert a size reduction (~25% observed; threshold 15%); (2) small-tests (blocking): build with OPENSSL_SMALL and run the full suite (run_tests) across a matrix of x86_64 + aarch64 and gcc + clang. The size-reduction job is advisory (continue-on-error) until calibrated; the small-tests matrix is a correctness gate. The aarch64 legs use GitHub-hosted Arm64 runners (swap for CodeBuild if preferred).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread tests/binary-size/main.c
Comment thread tests/binary-size/main.c
@justsmth justsmth requested review from manastasova and sgmenda June 26, 2026 14:36

@sgmenda sgmenda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified the aarch64 parts, and once #3319 lands, the x86 parts can be a short comment pointing to the changes in #3319.

OPENSSL_SMALL implies MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX on x86_64
(aws#3319), which disables s2n-bignum entirely. Remove the now-unnecessary
x86_64 branches from the OPENSSL_SMALL selector header and CMakeLists,
leaving only the aarch64 variant pinning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants