Skip to content

Atbash Cipher

Encode and decode text by swapping each letter with its mirror in the alphabet.

Adapted from Exercism’s problem specification (MIT). Listed under Encoding & Ciphers.

// FILE: atbash_cipher.nomi
//# The Atbash cipher: each letter swaps with its mirror in the alphabet, `a`
//# with `z`, `b` with `y`, and so on. Digits pass through unchanged, and
//# everything else is dropped.

/// Each lowercase letter and its mirror.
once mirror = Iter.zip('a'..='z', Iter.reverse('a'..='z')) |> Iter.to_map()

/// The ciphertext of `phrase`, in lowercase groups of five.
pub fn encode(phrase: String) -> String
    phrase
    |> translate()
    |> Iter.chunks(5)
    |> Iter.map(String.join)
    |> String.join(" ")
end

/// The plaintext of `phrase`, with no spaces.
pub fn decode(phrase: String) -> String
    phrase |> translate() |> String.join()
end

/// Each letter and digit of `text` in cipher order. The cipher is its own
/// inverse, so this both encodes and decodes.
fn translate(text: String) -> List<String>
    text
    |> String.lowercase()
    |> String.to_codepoints()
    |> Iter.flat_map(swap)
    |> Iter.map(Codepoint.to_string)
    |> Iter.to_list()
end

fn swap(cp: Codepoint) -> Maybe<Codepoint>
    if Codepoint.is_digit(cp) then
        Some(cp)
    else
        Map.get(mirror, cp)
    end
end

// FILE: atbash_cipher_test.nomi
import atbash_cipher

test "encode"
    [
        ("encode yes",           "yes",                     "bvh"),
        ("encode no",            "no",                      "ml"),
        ("encode OMG",           "OMG",                     "lnt"),
        ("encode spaces",        "O M G",                   "lnt"),
        ("encode mindblowingly", "mindblowingly",           "nrmwy oldrm tob"),
        ("encode numbers",       "Testing,1 2 3, testing.", "gvhgr mt123 gvhgr mt"),
        ("encode deep thought",  "Truth is fiction.",       "gifgs rhurx grlm"),
        (
            "encode all the letters",
            "The quick brown fox jumps over the lazy dog.",
            "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
        ),
    ]
    |> subtests (description, phrase, expected), description
        assert atbash_cipher.encode(phrase) == expected
    end
end

test "decode"
    [
        ("decode exercism", "vcvix rhn", "exercism"),
        (
            "decode a sentence",
            "zmlyh gzxov rhlug vmzhg vkkrm thglm v",
            "anobstacleisoftenasteppingstone",
        ),
        ("decode numbers", "gvhgr mt123 gvhgr mt", "testing123testing"),
        (
            "decode all the letters",
            "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt",
            "thequickbrownfoxjumpsoverthelazydog",
        ),
        ("decode with too many spaces", "vc vix    r hn", "exercism"),
        (
            "decode with no spaces",
            "zmlyhgzxovrhlugvmzhgvkkrmthglmv",
            "anobstacleisoftenasteppingstone",
        ),
    ]
    |> subtests (description, phrase, expected), description
        assert atbash_cipher.decode(phrase) == expected
    end
end
  • The cipher is a map from each letter to its mirror, Iter.zip('a'..='z', Iter.reverse('a'..='z')) |> Iter.to_map(), built once. Arithmetic on code point numbers (219 - n) was the other option; the map shows the two alphabets the description shows.
  • translate works on code points, since the cipher is ASCII. swap keeps a digit and looks a letter up in the map, so anything else returns None, and Iter.flat_map(swap) drops it: a Maybe iterates as zero or one value.
  • The cipher is its own inverse, so encode and decode share translate and differ only in how they join the letters: groups of five from Iter.chunks(5), or one run.
  • Each canonical group is a test named by the group’s description, holding one subtests table; a group block is for shared clock, boot or setup, which none needs.