Skip to content

feat: add Gronsfeld cipher implementation#1045

Merged
siriak merged 4 commits into
TheAlgorithms:masterfrom
qsrbit:feature/gronsfeld_cipher
Jun 15, 2026
Merged

feat: add Gronsfeld cipher implementation#1045
siriak merged 4 commits into
TheAlgorithms:masterfrom
qsrbit:feature/gronsfeld_cipher

Conversation

@qsrbit

@qsrbit qsrbit commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds a Gronsfeld Cipher implementation to the ciphers module.

About Gronsfeld Cipher

The Gronsfeld cipher, a Vigenère variant where the key is a digit sequence (0-9). Includes 20 tests covering key validation, case preservation, alphabet wrap-around, and round-trip correctness.

Implementation Details

The implementation provides two public functions:

  1. gronsfeld_encrypt(text: &str, key: &str) -> Result<String, &'static str>

    • Encrypts plaintext by shifting each letter forward by the corresponding key digit
    • Validates the key: must be non-empty and contain only digits (0–9)
    • Preserves the case of alphabetic characters
    • Passes non-alphabetic characters (spaces, punctuation, Unicode) through unchanged
    • Cycles the key if it is shorter than the plaintext
  2. gronsfeld_decrypt(text: &str, key: &str) -> Result<String, &'static str>

    • Decrypts ciphertext by shifting each letter backward by the corresponding key digit
    • Same key validation and passthrough rules as encryption
    • Inverse of gronsfeld_encrypt: decrypt(encrypt(text, key), key) == text

Algorithm Details

  • Character Set: Works with all ASCII alphabetic characters (A–Z, a–z)
  • Key Format: Digit string, e.g. "31415" or "9876543210"
  • Key Cycling: The key repeats cyclically when shorter than the text; only alphabetic characters consume a key digit
  • Shift Range: Each digit shifts by 0–9 positions (a strict subset of Vigenère's 0–25)
  • Case Preservation: Uppercase input stays uppercase, lowercase stays lowercase
  • Time Complexity: O(n) where n is the length of the input text
  • Space Complexity: O(n) for storing the result

Testing

All tests pass and the codebase is clean:

cargo test gronsfeld
cargo fmt --check
cargo clippy -- -D warnings

Test Coverage

  • ✅ Empty key returns error
  • ✅ Non-digit key returns error
  • ✅ Encrypt empty text
  • ✅ Encrypt basic: "abc" + "123" = "bdf"
  • ✅ Encrypt preserves uppercase
  • ✅ Encrypt mixed case
  • ✅ Encrypt passthrough non-alpha: spaces and punctuation unchanged
  • ✅ Encrypt key wraps cyclically
  • ✅ Encrypt zero shift: text unchanged
  • ✅ Encrypt wraps around alphabet: 'z' + 1 = 'a'
  • ✅ Encrypt with single-digit key: "Hello, World!" + "5" = "Mjqqt, Btwqi!"
  • ✅ Decrypt empty text
  • ✅ Decrypt basic: "bdf" + "123" = "abc"
  • ✅ Decrypt preserves case
  • ✅ Decrypt passthrough non-alpha
  • ✅ Decrypt zero shift
  • ✅ Decrypt wraps around alphabet: 'a' - 1 = 'z'
  • ✅ Round-trip basic
  • ✅ Round-trip long text (pangram)
  • ✅ Round-trip with Unicode passthrough

Examples

use the_algorithms_rust::ciphers::{gronsfeld_encrypt, gronsfeld_decrypt};

// Basic encryption
let ciphertext = gronsfeld_encrypt("Hello, World!", "31415").unwrap();
assert_eq!(ciphertext, "Kfnop, Yosnd!");

// Basic decryption
let plaintext = gronsfeld_decrypt("Kfnop, Yosnd!", "31415").unwrap();
assert_eq!(plaintext, "Hello, World!");

// Zero-shift key leaves text unchanged
let ciphertext = gronsfeld_encrypt("hello", "0").unwrap();
assert_eq!(ciphertext, "hello");

// Alphabet wrap-around
let ciphertext = gronsfeld_encrypt("z", "1").unwrap();
assert_eq!(ciphertext, "a");

// Invalid key returns Err
assert!(gronsfeld_encrypt("hello", "").is_err());
assert!(gronsfeld_encrypt("hello", "12a3").is_err());

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my implementation is correct
  • New and existing unit tests pass locally with my changes
  • I have checked my code with cargo fmt
  • I have checked my code with cargo clippy
  • Any dependent changes have been merged and published

Implements encrypt and decrypt functions for the Gronsfeld cipher,
a Vigenère variant where the key is a digit sequence (0-9). Includes
20 tests covering key validation, case preservation, alphabet wrap-around,
and round-trip correctness.
@qsrbit qsrbit requested a review from imp2002 as a code owner June 12, 2026 14:58
@qsrbit

qsrbit commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

@siriak Hi, can you take a look at this PR in your convenience time. Thank you

Copilot AI 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.

Pull request overview

Adds a new Gronsfeld cipher implementation to the ciphers module, exposing encrypt/decrypt APIs and accompanying unit tests.

Changes:

  • Introduce gronsfeld_encrypt / gronsfeld_decrypt with key validation and ASCII letter shifting.
  • Add unit tests for key validation, case preservation, wrap-around, and round-trip behavior.
  • Wire the new module into src/ciphers/mod.rs for compilation and public re-export.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/ciphers/mod.rs Registers the new gronsfeld module and re-exports its public functions.
src/ciphers/gronsfeld.rs Implements Gronsfeld encrypt/decrypt logic plus unit tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ciphers/gronsfeld.rs
Comment thread src/ciphers/gronsfeld.rs Outdated
@codecov-commenter

codecov-commenter commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.81%. Comparing base (7789289) to head (7967498).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1045      +/-   ##
==========================================
- Coverage   95.82%   95.81%   -0.02%     
==========================================
  Files         393      394       +1     
  Lines       29971    30084     +113     
==========================================
+ Hits        28721    28825     +104     
- Misses       1250     1259       +9     

☔ 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.

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

Please review Copilot's comments, everything else looks good

qsrbit and others added 2 commits June 15, 2026 17:43
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@qsrbit

qsrbit commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

@siriak I've just fixed. I think it's ready to be merged

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/ciphers/gronsfeld.rs Outdated
Comment thread src/ciphers/gronsfeld.rs
syntax error

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@siriak siriak 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, thanks!

@siriak siriak merged commit fa0aa80 into TheAlgorithms:master Jun 15, 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.

4 participants