{"id":49359101,"url":"https://github.com/timothymeadows/ml-kem.netcore","last_synced_at":"2026-04-27T15:03:05.446Z","repository":{"id":344265287,"uuid":"1181249547","full_name":"TimothyMeadows/ML-KEM.NetCore","owner":"TimothyMeadows","description":"ML-KEM.NetCore is a pure managed .NET implementation of the NIST-standardized ML-KEM (Kyber) post-quantum key encapsulation mechanism.","archived":false,"fork":false,"pushed_at":"2026-03-13T23:13:22.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-14T09:38:05.210Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TimothyMeadows.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-13T23:03:06.000Z","updated_at":"2026-03-13T23:32:21.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/TimothyMeadows/ML-KEM.NetCore","commit_stats":null,"previous_names":["timothymeadows/ml-kem.netcore"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/TimothyMeadows/ML-KEM.NetCore","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyMeadows%2FML-KEM.NetCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyMeadows%2FML-KEM.NetCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyMeadows%2FML-KEM.NetCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyMeadows%2FML-KEM.NetCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimothyMeadows","download_url":"https://codeload.github.com/TimothyMeadows/ML-KEM.NetCore/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimothyMeadows%2FML-KEM.NetCore/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32341455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-04-27T15:02:43.484Z","updated_at":"2026-04-27T15:03:05.442Z","avatar_url":"https://github.com/TimothyMeadows.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ML-KEM.NetCore\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![nuget](https://img.shields.io/nuget/v/ML-KEM.NetCore.svg)](https://www.nuget.org/packages/ML-KEM.NetCore/)\n\n`ML-KEM.NetCore` is a pure managed .NET implementation of the NIST-standardized ML-KEM (Kyber) post-quantum key encapsulation mechanism.\n\nThe library targets **.NET 8** and uses [`PinnedMemory`](https://github.com/TimothyMeadows/PinnedMemory) for secret key and shared secret material to improve lifecycle control.\n\n---\n\n## Table of contents\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Quick start](#quick-start)\n  - [Pinned-memory encapsulation flow](#pinned-memory-encapsulation-flow)\n  - [Alice/Bob key exchange flow](#alicebob-key-exchange-flow)\n- [API reference](#api-reference)\n  - [`MLKemParameterSet`](#mlkemparameterset)\n  - [`MLKem`](#mlkem)\n  - [Result types](#result-types)\n- [Parameter sizes](#parameter-sizes)\n- [Best practices](#best-practices)\n- [Validation and testing](#validation-and-testing)\n- [Development](#development)\n- [Security notes](#security-notes)\n- [License](#license)\n\n---\n\n## Requirements\n\n- **.NET 8 SDK** for building and running tests.\n- Project target framework: **`net8.0`**.\n\n---\n\n## Installation\n\n### From source\n\nClone the repository and reference the `MLKEM.NetCore` project from your solution.\n\n### NuGet (when published)\n\n```bash\ndotnet add package ML-KEM.NetCore\n```\n\n---\n\n## Quick start\n\n### Pinned-memory encapsulation flow\n\n```csharp\nusing MLKEM.NetCore;\n\nvar kem = new MLKem(MLKemParameterSet.MLKem768);\nvar keyPair = kem.GenerateKeyPair();\n\nusing (keyPair.SecretKey)\n{\n    var encapsulation = kem.Encapsulate(keyPair.PublicKey);\n    using (encapsulation.SharedSecret)\n    {\n        var secretKey = keyPair.SecretKey.Read().AsSpan(0, kem.SecretKeyBytes);\n        using var sharedSecretBob = kem.Decapsulate(secretKey, encapsulation.CipherText);\n\n        // Compare or use shared secrets, then dispose to scrub.\n    }\n}\n```\n\n### Alice/Bob key exchange flow\n\n```csharp\nusing System.Security.Cryptography;\nusing MLKEM.NetCore;\n\nvar kem = new MLKem(MLKemParameterSet.MLKem768);\n\n// Alice creates a long-term key pair and shares her public key.\nvar aliceKeyPair = kem.GenerateKeyPair();\nusing (aliceKeyPair.SecretKey)\n{\n    // Bob encapsulates to Alice's public key.\n    var bobEncapsulation = kem.Encapsulate(aliceKeyPair.PublicKey);\n    using (bobEncapsulation.SharedSecret)\n    {\n        var bobSharedSecret = bobEncapsulation.SharedSecret.Read().AsSpan(0, kem.SharedSecretBytes);\n\n        // Alice decapsulates Bob's ciphertext using her secret key.\n        var aliceSecretKey = aliceKeyPair.SecretKey.Read().AsSpan(0, kem.SecretKeyBytes);\n        using var aliceSharedSecretPinned = kem.Decapsulate(aliceSecretKey, bobEncapsulation.CipherText);\n        var aliceSharedSecret = aliceSharedSecretPinned.Read().AsSpan(0, kem.SharedSecretBytes);\n\n        var sameSecret = CryptographicOperations.FixedTimeEquals(bobSharedSecret, aliceSharedSecret);\n        Console.WriteLine($\"Alice and Bob share the same secret: {sameSecret}\");\n    }\n}\n```\n\n---\n\n## API reference\n\n## `MLKemParameterSet`\n\n```csharp\nenum MLKemParameterSet\n{\n    MLKem512,\n    MLKem768,\n    MLKem1024\n}\n```\n\nSelects the NIST parameter set used by an `MLKem` instance.\n\n---\n\n## `MLKem`\n\n### Constructor\n\n```csharp\nMLKem(MLKemParameterSet parameterSet)\n```\n\n### Size properties\n\n```csharp\nint PublicKeyBytes { get; }\nint SecretKeyBytes { get; }\nint CipherTextBytes { get; }\nint SharedSecretBytes { get; } // always 32\n```\n\n### Key generation\n\n```csharp\nSecureKeyPair GenerateKeyPair()\nSecureKeyPair GenerateKeyPair(ReadOnlySpan\u003cbyte\u003e d, ReadOnlySpan\u003cbyte\u003e z)\n```\n\n- Deterministic generation requires `d` and `z` to be exactly 32 bytes each.\n\n### Encapsulation\n\n```csharp\nEncapsulationResult Encapsulate(ReadOnlySpan\u003cbyte\u003e publicKey)\nEncapsulationResult EncapsulateDeterministic(ReadOnlySpan\u003cbyte\u003e publicKey, ReadOnlySpan\u003cbyte\u003e m)\n```\n\n- `EncapsulateDeterministic(...)` requires `m` to be exactly 32 bytes.\n\n### Decapsulation\n\n```csharp\nPinnedMemory\u003cbyte\u003e Decapsulate(ReadOnlySpan\u003cbyte\u003e secretKey, ReadOnlySpan\u003cbyte\u003e cipherText)\n```\n\n- Input lengths are validated and must match the selected parameter set sizes.\n\n---\n\n## Result types\n\n```csharp\nsealed class SecureKeyPair\n{\n    byte[] PublicKey { get; }\n    PinnedMemory\u003cbyte\u003e SecretKey { get; }\n}\n\nsealed class EncapsulationResult\n{\n    byte[] CipherText { get; }\n    PinnedMemory\u003cbyte\u003e SharedSecret { get; }\n}\n```\n\n---\n\n## Parameter sizes\n\nPer instantiated `MLKem` object:\n\n- `MLKem512`\n  - Public key: 800 bytes\n  - Secret key: 1632 bytes\n  - Ciphertext: 768 bytes\n  - Shared secret: 32 bytes\n- `MLKem768`\n  - Public key: 1184 bytes\n  - Secret key: 2400 bytes\n  - Ciphertext: 1088 bytes\n  - Shared secret: 32 bytes\n- `MLKem1024`\n  - Public key: 1568 bytes\n  - Secret key: 3168 bytes\n  - Ciphertext: 1568 bytes\n  - Shared secret: 32 bytes\n\n---\n\n## Best practices\n\n1. **Use secure APIs for secret-bearing values**\n   - Use `GenerateKeyPair`, `Encapsulate`, and `Decapsulate` to keep key material in `PinnedMemory\u003cbyte\u003e`.\n   - Dispose `PinnedMemory\u003cbyte\u003e` instances promptly.\n\n2. **Treat parameter sets as protocol constants**\n   - Do not mix parameter sets between peers.\n   - Validate all serialized key/ciphertext lengths before use.\n\n3. **Prefer deterministic APIs only for tests/vectors**\n   - In production, use randomized `GenerateKeyPair()` and `Encapsulate()`.\n\n4. **Keep secret data lifetime short**\n   - Zero and dispose sensitive buffers as soon as possible.\n\n---\n\n## Validation and testing\n\nThe test suite includes:\n\n- Deterministic known-answer-style vector checks\n- Encapsulation/decapsulation round-trip checks across parameter sets\n- Ciphertext tamper rejection behavior\n- Secure API behavior and memory handling coverage\n\nRun all tests:\n\n```bash\ndotnet test MLKEM.sln\n```\n\n---\n\n## Development\n\n### Build\n\n```bash\ndotnet build MLKEM.sln\n```\n\n### Test\n\n```bash\ndotnet test MLKEM.sln\n```\n\n---\n\n## Security notes\n\nThis implementation follows ML-KEM constructions in **RFC 9936**, including a rejection-sampling path that continues SHAKE output generation until enough coefficients are produced.\n\nCurrent hardening in this repository:\n\n- Decapsulation uses constant-time ciphertext validation with fallback to `z` on invalid ciphertext.\n- Sensitive intermediates in decapsulation are explicitly zeroed before returning.\n- All secret key and shared secret APIs expose `PinnedMemory\u003cbyte\u003e` for deterministic cleanup of secret-bearing values.\n- Public keys and ciphertext remain `byte[]` for interoperability, while key material is pinned.\n\n---\n\n## License\n\nMIT License. See [LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothymeadows%2Fml-kem.netcore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimothymeadows%2Fml-kem.netcore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothymeadows%2Fml-kem.netcore/lists"}