Skip to content

ML-KEM assembly: AVX512F and AVX512VBMI - #10981

Open
SparkiDev wants to merge 1 commit into
wolfSSL:masterfrom
SparkiDev:mlkem_avx512
Open

ML-KEM assembly: AVX512F and AVX512VBMI#10981
SparkiDev wants to merge 1 commit into
wolfSSL:masterfrom
SparkiDev:mlkem_avx512

Conversation

@SparkiDev

@SparkiDev SparkiDev commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Description

New assembly for ML-KEM for Intel x64 machines that support AVX512 extensions.

Testing

Regression testing of ML-KEM.

Generator

https://github.com/wolfSSL/scripts/pull/638

@SparkiDev SparkiDev self-assigned this Jul 23, 2026
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

@SparkiDev
SparkiDev force-pushed the mlkem_avx512 branch 4 times, most recently from ab9e792 to ba111bc Compare July 23, 2026 08:48
@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Jul 23, 2026

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10981

Scan targets checked: wolfcrypt-bugs, wolfcrypt-rs-bugs, wolfcrypt-src, wolfssl-bugs, wolfssl-src

No new issues found in the changed files. ✅

@SparkiDev SparkiDev self-assigned this Jul 23, 2026
@SparkiDev SparkiDev removed their assignment Jul 23, 2026

@Frauschi Frauschi 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.

🐺 Skoll Code Review

Overall recommendation: APPROVE
Findings: 1 total — 1 posted, 0 skipped

Posted findings

  • [Medium] New AVX512 k2 noise path uses large unconditional stack buffers, unlike its AVX2 siblingwolfcrypt/src/wc_mlkem_poly.c:4754-4809

Review generated by Skoll via Claude/Codex

Comment thread wolfcrypt/src/wc_mlkem_poly.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10981

Scan targets checked: wolfcrypt-bugs, wolfcrypt-rs-bugs, wolfcrypt-src, wolfssl-bugs, wolfssl-src

No new issues found in the changed files. ✅

@SparkiDev SparkiDev assigned Frauschi and wolfSSL-Bot and unassigned SparkiDev Jul 28, 2026

@Frauschi Frauschi 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.

🐺 Skoll Code Review

Overall recommendation: APPROVE
Findings: 18 total — 5 posted, 13 skipped

Posted findings

  • [Medium] mlkem_get_noise_k3/k4_avx512 double the stack buffer and drop the WOLFSSL_SMALL_STACK handling used by the k2 siblingwolfcrypt/src/wc_mlkem_poly.c:4877
  • [Medium] NO_AVX512_SUPPORT no longer disables all AVX-512 code emission: the VBMI assembly block is not nested under the AVX-512 guardwolfssl/wolfcrypt/wc_mlkem.h:586-598
  • [Low] New AVX512 declarations are inconsistently const-qualified, named, and orderedwolfssl/wolfcrypt/wc_mlkem.h:696-726
  • [Low] Doc comments on the new x8 helpers omit @return and misstate lane countswolfcrypt/src/wc_mlkem_poly.c:4581-4587
  • [Low] NO_AVX512_VBMI_SUPPORT does not remove all VBMI-family instructions: vpcompressw is emitted from the plain AVX-512 blockwolfssl/wolfcrypt/wc_mlkem.h:591-594
Skipped findings
  • [Medium] AVX512BW-dependent ML-KEM assembly is dispatched on the AVX512F CPUID bit alone
  • [Medium] Four dispatch tiers added with no test that deterministically exercises each one
  • [Medium] ML-KEM AVX-512 dispatch gated on the AVX-512F CPUID bit while the assembly requires AVX-512BW (and AVX-512VL)
  • [Low] mlkem_csubq_avx512 is declared but never called and has no AVX2 counterpart declaration
  • [Low] New _ins suffix is undocumented and four helpers are generated by a function-defining macro
  • [Low] VBMI guard nesting in wc_mlkem.h does not actually match the assembly guard it claims to mirror
  • [Low] mlkem_get_noise_k3_avx512 / k4_avx512 use 1 KB plain stack arrays, ignoring WOLFSSL_SMALL_STACK
  • [Low] New AVX-512 CPUID flags are set from feature bits with no OSXSAVE/XCR0 state check
  • [Info] AVX-512 matrix generators roughly double the non-small-stack frame; k2 pays the full eight-lane cost to use four lanes
  • [Info] The four-lane mlkem_redistribute_*_rand_avx512 dispatch is unreachable in normal builds
  • [Info] mlkem_csubq_avx512 is newly declared in the header but has no caller
  • [Info] New x8 noise helpers can return MEMORY_E but their doc comments omit @return; k3 comment miscounts lanes
  • [Info] No test exercises the new CPUID dispatch variants, so cross-implementation output equivalence is unverified

Review generated by Skoll via Claude/Codex

Comment thread wolfcrypt/src/wc_mlkem_poly.c Outdated
Comment thread wolfssl/wolfcrypt/wc_mlkem.h
Comment thread wolfssl/wolfcrypt/wc_mlkem.h Outdated
Comment thread wolfcrypt/src/wc_mlkem_poly.c
Comment thread wolfssl/wolfcrypt/wc_mlkem.h Outdated
@Frauschi Frauschi assigned SparkiDev and unassigned wolfSSL-Bot and Frauschi Jul 28, 2026
@SparkiDev
SparkiDev force-pushed the mlkem_avx512 branch 2 times, most recently from e845d79 to 3510d2b Compare July 29, 2026 00:49
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

Visual Studio: CRL monitor test failed.

@SparkiDev SparkiDev assigned Frauschi and wolfSSL-Bot and unassigned SparkiDev Jul 29, 2026

@Frauschi Frauschi 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.

🐺 Skoll Code Review

Overall recommendation: REQUEST_CHANGES
Findings: 15 total — 1 posted, 14 skipped

Posted findings

  • [High] AVX512-VBMI/VBMI2 dispatch branches never test CPUID_AVX512, so cpuid_clear_flag(CPUID_AVX512) no longer disables AVX512 codewolfcrypt/src/wc_mlkem_poly.c:5799-5812
Skipped findings
  • [Medium] New WOLFSSL_MLKEM_ASM_TEST macro gates assembly with no prototypes and no in-tree consumer
  • [Medium] No test exercises the four new rejection-sampler variants or the AVX512 paths on non-AVX512 CI
  • [Medium] mlkem_get_noise_k2_avx512 does not advance seed[WC_ML_KEM_SYM_SZ] while its AVX2 sibling does
  • [Medium] AVX512 matrix generators double the stack frame in default (non-small-stack) builds
  • [Medium] No configure-time probe for the new NO_AVX512_VBMI_SUPPORT / NO_AVX512_VBMI2_SUPPORT opt-outs
  • [Medium] AVX-512 VBMI/VBMI2 dispatch bypasses the CPUID_AVX512 master flag, defeating cpuid_clear_flag(CPUID_AVX512)
  • [Medium] New AVX-512 matrix generators roughly double single-frame stack usage in non-WOLFSSL_SMALL_STACK builds
  • [Low] mlkem_csubq_avx512 is declared and assembled but never called
  • [Low] New CPUID flag defines break column alignment and are missing from the benchmark feature banner
  • [Low] Unused lanes 4..7 in mlkem_gen_matrix_k2_avx512 duplicate lane 0's XOF input and omit the (word32) cast
  • [Low] The _ins suffix on the new dispatch helpers is undocumented and novel to the codebase
  • [Low] New static dispatch wrappers are unreferenced in single-parameter-set builds
  • [Info] SGX CPUID path cannot express the new VBMI/VBMI2 flags
  • [Info] AVX-512 and AVX-2 ML-KEM-512 noise paths now differ in whether they mutate seed[WC_ML_KEM_SYM_SZ]

Review generated by Skoll via Claude/Codex

void mlkem_vec_compress_10(byte* r, sword16* v, unsigned int k)
{
#ifdef USE_INTEL_SPEEDUP
#ifdef WOLFSSL_MLKEM_HAVE_INTEL_AVX512_VBMI

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.

🟠 [High] AVX512-VBMI/VBMI2 dispatch branches never test CPUID_AVX512, so cpuid_clear_flag(CPUID_AVX512) no longer disables AVX512 code
🚫 BLOCK bug

Every new VBMI/VBMI2 dispatch branch added by this diff gates only on the sub-feature flag and never on IS_INTEL_AVX512(cpuid_flags). The affected sites are mlkem_vec_compress_10 (~5799), mlkem_vec_decompress_10 (~6024), mlkem_vec_decompress_11 (~6126), mlkem_compress_4 (~6309), mlkem_compress_5 (~6401), mlkem_from_bytes (~6858), mlkem_to_bytes (~6951), plus mlkem_rej_uniform_n_ins/mlkem_rej_uniform_ins (2326-2369). Because the VBMI branch is tested before the AVX512 branch, it wins whenever the VBMI CPUID bit is set.

wolfSSL exposes cpuid_clear_flag() as a WOLFSSL_API (declared wolfssl/wolfcrypt/cpuid.h:197, implemented wolfcrypt/src/cpuid.c:710) precisely so integrators can switch an instruction set off at run time. After this diff, cpuid_clear_flag(CPUID_AVX512) leaves CPUID_AVX512_VBMI/CPUID_AVX512_VBMI2 set, and ML-KEM keeps executing AVX512 instructions through the *_vbmi/*_vbmi2 routines. The AVX512-only branch immediately below is correctly gated, so the behaviour is inconsistent within the same function.

This also diverges from the established repo convention. wolfcrypt/src/wc_frodokem_mat.c:1723-1735 nests the sub-feature test (IS_INTEL_VAES) inside a block already gated on IS_INTEL_AVX512(cpuid_flags) && (SAVE_VECTOR_REGISTERS2() == 0). The ML-KEM code inverts that structure.

(The narrow architectural point that VBMI implies AVX512F on real silicon does not save this: the flags are software state that callers are allowed to modify, and the code depends on an unstated invariant.)

Suggestion:

Suggested change
#ifdef WOLFSSL_MLKEM_HAVE_INTEL_AVX512_VBMI
#ifdef WOLFSSL_MLKEM_HAVE_INTEL_AVX512_VBMI
if (IS_INTEL_AVX512(cpuid_flags) && IS_INTEL_AVX512_VBMI(cpuid_flags) &&
(SAVE_VECTOR_REGISTERS2() == 0)) {
mlkem_compress_10_avx512_vbmi(r, v, (int)k);
RESTORE_VECTOR_REGISTERS();
}
else
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@Frauschi Frauschi assigned SparkiDev and unassigned wolfSSL-Bot and Frauschi Jul 29, 2026
New assembly for ML-KEM for Intel x64 machines that support AVX512 extensions.
Update how much is added to the stack of others.
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.

4 participants