Skip to content

Conversation

@willieyz
Copy link
Contributor

@willieyz willieyz commented Nov 24, 2025

  • Resolves: Port: Hoist default C backend into separate functions #732

  • Purpose: This porting aims to separate the native backend functions into two distinct sets:

    • Fallback C backend functions
    • Native backend functions
      This work is based on the runtime-dispatch changes introduced in PR #607.
  • Steps:

    • For each function improved by runtime dispatch in PR #607.:
      • Separate a additional fallback C backend function from the original implementation.
      • Add a CBMC proof for the C backend function.
      • Update the existing original implementation's CBMC USE_FUNCTION_CONTRACTS with the C backend function.
  • Tests:

    • Run all test scripts with all argument variations under both static ON and static OFF configurations.
    • Run all examples: tests examples.
    • Run all relevant CBMC proofs for each modified or newly added function.
  • The following is the checking table for fallback C backend and native backend:

Original Native C backend
mld_poly_ntt mld_ntt_native mld_poly_ntt_c
mld_poly_invntt_tomont mld_intt_native mld_poly_invntt_tomont_c
mld_polymat_permute_bitrev_to_custom mld_poly_permute_bitrev_to_custom ((void)mat);
/* Nothing to do */
mld_rej_uniform mld_rej_uniform_native mld_rej_uniform_c
mld_rej_eta mld_rej_uniform_eta2_native
mld_rej_uniform_eta4_native
mld_rej_eta_c
mld_poly_decompose mld_poly_decompose_32_native
mld_poly_decompose_88_native
mld_poly_decompose_c
mld_poly_caddq mld_poly_caddq_native mld_poly_caddq_c
mld_poly_use_hint mld_poly_use_hint_32_native
mld_poly_use_hint_88_native
mld_poly_use_hint_c
mld_poly_chknorm mld_poly_chknorm_native mld_poly_chknorm_c
mld_polyz_unpack mld_polyz_unpack_17_native
mld_polyz_unpack_19_native
mld_polyz_unpack_c
mld_poly_pointwise_montgomery mld_poly_pointwise_montgomery_native mld_poly_pointwise_montgomery_c
mld_polyvecl_pointwise_acc_montgomery mld_polyvecl_pointwise_acc_montgomery_l4_native
mld_polyvecl_pointwise_acc_montgomery_l5_native
mld_polyvecl_pointwise_acc_montgomery_l7_native
mld_polyvecl_pointwise_acc_montgomery_c
mld_keccakf1600_permute mld_keccak_f1600_x1_native mld_keccakf1600_permute_c
mld_keccakf1600x4_permute mld_keccak_f1600_x2_v84a_asm * 2
mld_keccak_f1600_x4_scalar_v8a_hybrid_asm
mld_keccak_f1600_x4_scalar_v8a_v84a_hybrid_asm
mld_keccakf1600_permute * 4

@willieyz willieyz force-pushed the hoist-C-separate branch 2 times, most recently from 91847f9 to d869805 Compare November 25, 2025 06:30
@willieyz willieyz marked this pull request as ready for review November 25, 2025 07:32
@willieyz willieyz requested a review from a team as a code owner November 25, 2025 07:32
@willieyz willieyz force-pushed the hoist-C-separate branch 4 times, most recently from 5004349 to 4b5aa71 Compare November 28, 2025 05:38
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. This looks very good!
Two small comments.

@willieyz willieyz force-pushed the hoist-C-separate branch 2 times, most recently from 79e3f3b to ddd184a Compare November 28, 2025 11:18
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. This looks excellent!
I only found one extra new line that you could still remove.

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 for the changes @willieyz. LGTM.

@hanno-becker, WDYT?

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

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

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

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>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Signed-off-by: willieyz <willie.zhao@chelpis.com>
Comment on lines +382 to +384
ret = mld_polyvecl_pointwise_acc_montgomery_l4_native(
w->coeffs, (const int32_t(*)[MLDSA_N])u->vec,
(const int32_t(*)[MLDSA_N])v->vec);
Copy link
Contributor

Choose a reason for hiding this comment

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

This cast business is not very nice. I wonder if we should modify the native API to streamline this. Not an issue for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

mlkem-native has

    ret = mlk_polyvec_basemul_acc_montgomery_cached_k2_native(
        r->coeffs, (const int16_t *)a, (const int16_t *)b,
        (const int16_t *)b_cache);

Is that better?

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.

LGTM, thank you @willieyz !

@mkannwischer mkannwischer merged commit f504c9a into main Dec 3, 2025
328 checks passed
@mkannwischer mkannwischer deleted the hoist-C-separate branch December 3, 2025 05:39
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: Hoist default C backend into separate functions

4 participants