Skip to content

Feature/lwe encryption - #7631

Merged
alxkm merged 3 commits into
TheAlgorithms:masterfrom
dilaraacetin:feature/lwe-encryption
Oct 4, 2026
Merged

alxkm merged 3 commits into
TheAlgorithms:masterfrom
dilaraacetin:feature/lwe-encryption

Conversation

@dilaraacetin

Copy link
Copy Markdown
Contributor

Fixes #7630

Description

Adds an educational implementation of Learning With Errors (LWE) public-key encryption (Regev, 2005) to the ciphers package. This is the first lattice-based algorithm in the repository and shows the mathematical foundation of ML-KEM (Kyber, FIPS 203).

How it works

  • Key generation: a secret s ∈ Z_q^n, a random matrix A ∈ Z_q^{m×n} and a small error e ∈ [-B, B]^m are sampled. The public key is (A, b = A·s + e mod q), and the private key is s.
  • Encryption of a bit: a random r ∈ {0,1}^m is chosen, then u = rᵀ·A (mod q) and v = r·b + bit·⌊q/2⌋ (mod q) are computed. Encryption is randomized.
  • Decryption: d = v − u·s (mod q) equals r·e + bit·⌊q/2⌋. The bit is 1 if q/4 ≤ d < 3q/4, otherwise 0.
  • Messages: byte[] messages are encrypted bit by bit, MSB first.

Changes

  • src/main/java/com/thealgorithms/ciphers/LWEEncryption.java: implementation with a nested immutable Ciphertext class
  • src/test/java/com/thealgorithms/ciphers/LWEEncryptionTest.java: JUnit 5 tests

No other files are modified.

Design notes

  • Toy parameters: n = 32, q = 3329 (the ML-KEM modulus), m = 128, B = 3.
  • Correctness is guaranteed: the accumulated noise is at most m·B = 384 < q/4 ≈ 832, so decryption never fails. This condition is documented and checked in the code.
  • Math.floorMod is used for all modular reductions, and inner products are accumulated in long to avoid overflow.
  • SecureRandom is used for all sampling.
  • Ciphertext is immutable; arrays are defensively copied.
  • For simplicity, one instance holds both the public and private key; the Javadoc notes that these are separate in real usage.
  • Educational only: the parameters are not secure. Real schemes use discrete Gaussian noise and much larger dimensions, as stated in the Javadoc.

Tests

  • Bit roundtrip: bits 0 and 1, 100 repetitions each, all correct.
  • Byte roundtrip: "Hello, PQC!" and a random 1 KB message.
  • Randomized encryption: encrypting the same bit twice gives different ciphertexts.
  • Wrong key: decrypting an 8+ byte message with another instance does not recover the original.
  • Bit ordering, including negative byte values such as 0xFF and 0x80.
  • Input validation (invalid bits, null/empty inputs, malformed ciphertexts) and Ciphertext immutability.

mvn clean verify passes locally, and all files are formatted with clang-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

@codecov-commenter

codecov-commenter commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.74%. Comparing base (922c4d3) to head (2eb9fbd).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 2fdcde7 into TheAlgorithms:master Oct 4, 2026
7 checks passed
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.

[FEATURE REQUEST] Add Learning With Errors (LWE) encryption (Regev)

3 participants