Repository navigation
Feature/lwe encryption - #7631
Merged
Merged
Conversation
dilaraacetin
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
October 4, 2026 16:41
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7631 +/- ##
============================================
+ Coverage 81.68% 81.74% +0.05%
- Complexity 8192 8223 +31
============================================
Files 838 839 +1
Lines 25734 25799 +65
Branches 5027 5042 +15
============================================
+ Hits 21022 21089 +67
+ Misses 3924 3923 -1
+ Partials 788 787 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alxkm
approved these changes
Oct 4, 2026
alxkm
left a comment
Member
There was a problem hiding this comment.
Looks good. Thank you for the contribution.
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.
Fixes #7630
Description
Adds an educational implementation of Learning With Errors (LWE) public-key encryption (Regev, 2005) to the
cipherspackage. This is the first lattice-based algorithm in the repository and shows the mathematical foundation of ML-KEM (Kyber, FIPS 203).How it works
s ∈ Z_q^n, a random matrixA ∈ Z_q^{m×n}and a small errore ∈ [-B, B]^mare sampled. The public key is(A, b = A·s + e mod q), and the private key iss.r ∈ {0,1}^mis chosen, thenu = rᵀ·A (mod q)andv = r·b + bit·⌊q/2⌋ (mod q)are computed. Encryption is randomized.d = v − u·s (mod q)equalsr·e + bit·⌊q/2⌋. The bit is 1 ifq/4 ≤ d < 3q/4, otherwise 0.byte[]messages are encrypted bit by bit, MSB first.Changes
src/main/java/com/thealgorithms/ciphers/LWEEncryption.java: implementation with a nested immutableCiphertextclasssrc/test/java/com/thealgorithms/ciphers/LWEEncryptionTest.java: JUnit 5 testsNo other files are modified.
Design notes
n = 32,q = 3329(the ML-KEM modulus),m = 128,B = 3.m·B = 384 < q/4 ≈ 832, so decryption never fails. This condition is documented and checked in the code.Math.floorModis used for all modular reductions, and inner products are accumulated inlongto avoid overflow.SecureRandomis used for all sampling.Ciphertextis immutable; arrays are defensively copied.Tests
"Hello, PQC!"and a random 1 KB message.0xFFand0x80.Ciphertextimmutability.mvn clean verifypasses locally, and all files are formatted withclang-format -i --style=file.References
https://en.wikipedia.org/wiki/Learning_with_errors
O. Regev, "On Lattices, Learning with Errors, Random Linear Codes, and Cryptography", STOC 2005
FIPS 203 (ML-KEM): https://csrc.nist.gov/pubs/fips/203/final
I have read CONTRIBUTING.md.
This pull request is all my own work -- I have not plagiarized it.
All filenames are in PascalCase.
All functions and variable names follow Java naming conventions.
All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
All new algorithms include a corresponding test class that validates their functionality.
All new code is formatted with
clang-format -i --style=file path/to/your/file.java