Skip to content

Conversation

@willieyz
Copy link
Contributor

@willieyz willieyz commented Nov 4, 2025

This PR made following changes:

  • Port runtime dispatch mechanism for AArch64, x86 native, and FIPS202 APIs.
  • Port configuration for static ON/OFF, AVX2 and AA64PFR1_EL1, as well as the corresponding config-variations tests in CI.

CBMC-related changes, including adding the CBMC proof for sys_check_capability, will be added in a separate PR (tracked by issue #723).

Additional changes discovered during PR progress, related with mld_poly_chknorm:

  • The return value of mld_poly_chknorm has been adjusted to properly signal fallback, instead of directly using 0 or -1.
    This distinction allows the runtime to detect when to fallback to the C implementation.
    • 0 → all coefficients are within the bound
    • 1 → at least one coefficient exceeds the bound
    • -1 (MLD_NATIVE_FUNC_FALLBACK) → the platform does not have the required capabilities to run the native function
  • Add a declassification of (ret == FALLBACK) to make this code pass the constant time tests.

@willieyz willieyz changed the title Runtime dispatch Add Runtime dispatch based on custom CPU capabilities function Nov 4, 2025
@willieyz willieyz force-pushed the runtime-dispatch branch 5 times, most recently from 6332a01 to b0ac532 Compare November 4, 2025 12:24
@willieyz willieyz force-pushed the runtime-dispatch branch 22 times, most recently from 74996f5 to 3e7a817 Compare November 6, 2025 11:47
Copy link
Contributor

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

Thanks a lot @willieyz for all your work!

Overall, it looks good, but there are a few points we may want to refine:

  1. As @mkannwischer mentions, some SHA3 guards in the AArch64 FIPS202 are unnecessary.
  2. We need documentation in the native backend that the decision whether to fall back or not must not depend on the input data. This is part of the contract between frontend and backend.
  3. There is a lot of duplicated post-conditions now which we may want to remove.

@mkannwischer
Copy link
Contributor

  1. There is a lot of duplicated post-conditions now which we may want to remove.

The next step is going to be to hoist out the C versions to separate functions to unblock the backend unit tests.
Then you will definitely need the asserations twice - the position of those can be argued about, but I'd want it to align with mlkem-native.

Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

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

Thanks @willieyz. Two small changes and then I think this is good to go.

@willieyz willieyz force-pushed the runtime-dispatch branch 3 times, most recently from 315c4ef to ac5ced9 Compare November 21, 2025 03:34
Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

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

Thank you @willieyz. This is a great improvement and will enable integrating the x86_64 backend into AWS-LC eventually.

willieyz and others added 11 commits November 21, 2025 05:04
…te_bitrev_to_custom)

- Change mld_ntt_native() return type from void to int
- Add runtime capability checking with fallback support
- Implement dispatch logic in mld_poly_ntt() to try native first,
  fallback to C
- Add MLD_NATIVE_FUNC_SUCCESS/FALLBACK return codes
- Add mld_sys_check_capability() for system capability detection
- Add test configuration for AVX2, static ON/OFF, add to CI test.

Signed-off-by: willieyz <willie.zhao@chelpis.com>
…a4_native)

Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
The inputs to chknorm are potentially secret. However, it is fine (and
unavoidable) to leak if chknorm return 0 or 1 (i.e., if all coeffs are within
bound or not). The declassification of that currently happens in sign.c.
However, for the run-time dispatch we require to branch depending on whether
mld_poly_chknorm_native returns MLD_NATIVE_FUNC_FALLBACK (-1) or not to signal
that the platform does not have the required capabilities to run the native
code (and we should hence fallback to the C code).

This commit adds a declassification of (ret == FALLBACK) which is sufficient
to make this code pass the constant time tests. The declassification of the
actual value (0/1) remains in sign.c to be consistent with the C
implementation.

Co-authored-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
…native)

Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>

add changes

Signed-off-by: willieyz <willie.zhao@chelpis.com>
Copy link
Contributor

@hanno-becker hanno-becker left a comment

Choose a reason for hiding this comment

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

Thank you @willieyz, this is great. LGTM

@mkannwischer mkannwischer merged commit 6a96f62 into main Nov 21, 2025
269 checks passed
@mkannwischer mkannwischer deleted the runtime-dispatch branch November 21, 2025 06:56
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.

Port run-time dispatch from mlkem-native

4 participants