Conversation
gdams
approved these changes
Nov 20, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes ML-KEM operations by reducing memory allocations through pre-allocated buffers of known maximum sizes. The main change is to allocate buffers upfront using the maximum size needed (ML-KEM 1024 sizes) instead of making two allocation calls per operation.
Key changes:
- Pre-allocate buffers using ML-KEM 1024 sizes to avoid allocation overhead for both 768 and 1024 parameter sets
- Simplify error handling by converting helper functions from returning errors to panicking (errors are still properly propagated from public APIs)
- Remove unnecessary
runtime.KeepAlivecalls and optimize function signatures
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cng/mlkem.go | Core optimization changes: added buffer size constants, refactored blob creation functions to use pre-allocated buffers, simplified error handling in internal helpers |
| cng/mlkem_test.go | Added t.Parallel() calls to test functions to enable parallel test execution |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
gdams
approved these changes
Nov 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Most of MLKEM allocations can be avoided by using big-enough buffer sizes when encoding/decoding CNG blobs. We know those max sizes, so use.
goos: windows goarch: arm64 pkg: github.com/microsoft/go-crypto-winnative/cng │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ MLKEMKeyGen-12 91.96µ ± 16% 81.21µ ± 7% -11.69% (p=0.001 n=7) MLKEMEncaps-12 32.45µ ± 16% 28.75µ ± 2% -11.39% (p=0.001 n=7) MLKEMDecaps-12 45.30µ ± 1% 44.02µ ± 2% -2.84% (p=0.002 n=7) MLKEMRoundTrip/Alice-12 132.7µ ± 3% 124.8µ ± 0% -5.98% (p=0.001 n=7) MLKEMRoundTrip/Bob-12 29.63µ ± 3% 28.70µ ± 0% -3.14% (p=0.001 n=7) geomean 55.61µ 51.66µ -7.09% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ MLKEMKeyGen-12 1.438Ki ± 0% 0.000Ki ± 0% -100.00% (p=0.001 n=7) MLKEMEncaps-12 3.656Ki ± 0% 2.406Ki ± 0% -34.19% (p=0.001 n=7) MLKEMDecaps-12 128.00 ± 0% 32.00 ± 0% -75.00% (p=0.001 n=7) MLKEMRoundTrip/Alice-12 1600.00 ± 0% 32.00 ± 0% -98.00% (p=0.001 n=7) MLKEMRoundTrip/Bob-12 3.656Ki ± 0% 2.406Ki ± 0% -34.19% (p=0.001 n=7) geomean 1.303Ki ? ¹ ² ¹ summaries must be >0 to compute geomean ² ratios must be >0 to compute geomean │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ MLKEMKeyGen-12 3.000 ± 0% 0.000 ± 0% -100.00% (p=0.001 n=7) MLKEMEncaps-12 4.000 ± 0% 3.000 ± 0% -25.00% (p=0.001 n=7) MLKEMDecaps-12 2.000 ± 0% 1.000 ± 0% -50.00% (p=0.001 n=7) MLKEMRoundTrip/Alice-12 5.000 ± 0% 1.000 ± 0% -80.00% (p=0.001 n=7) MLKEMRoundTrip/Bob-12 4.000 ± 0% 3.000 ± 0% -25.00% (p=0.001 n=7) geomean 3.438 ? ¹ ² ¹ summaries must be >0 to compute geomean ² ratios must be >0 to compute geomean