Skip to content

FEAT: Add ColumnarTranspositionConverter (classical transposition cipher) - #4327

Open
Roman Lutz (romanlutz) wants to merge 4 commits into
microsoft:mainfrom
romanlutz:claude/g1-columnar-transposition-ebba96
Open

Roman Lutz (romanlutz) wants to merge 4 commits into
microsoft:mainfrom
romanlutz:claude/g1-columnar-transposition-ebba96

Conversation

@romanlutz

Copy link
Copy Markdown
Contributor

Description

Adds ColumnarTranspositionConverter — the classical columnar transposition cipher — to PyRIT's converter catalog, filling a gap in the classical-cipher converters (joining CaesarConverter and VigenereConverter).

Plaintext is written row-wise into a grid whose number of columns equals the length of a keyword; the ciphertext is produced by reading the columns out in the alphabetical order of the keyword's letters. This is the standard incomplete-columns variant: the final row may be partially filled, columns can be of unequal length, and no padding characters are added. It is a pure permutation over all characters (spaces, digits, and punctuation included), so it is fully reversible given the keyword and message length.

Worked example (canonical Wikipedia vector): with key="ZEBRAS", WEAREDISCOVEREDFLEEATONCE → EVLNACDTESEAROFODEECWIREE.

Changes

  • pyrit/converter/columnar_transposition_converter.py — new converter with a configurable key (validated non-empty, ASCII-alphabetic; ordering case-insensitive, repeated letters broken by original left-to-right position via a stable sort). _build_identifier exposes key.
  • pyrit/converter/__init__.py — lazy export registration.
  • pyrit/scenario/scenarios/garak/encoding.py — registered as an ALL-only encoding technique (columnar_transposition) with a _converter_variants() row using key="ZEBRAS".
  • tests/unit/converter/test_columnar_transposition_converter.py — 13 unit tests, including the known Wikipedia vector, case-insensitivity, repeated-key-letter stability, permutation invariance, space/punctuation preservation, degenerate cases, identifier params, and input/key validation guards.

Notes

  • Standard library only — no new dependency.
  • References cited in the class docstring: Wikipedia "Transposition cipher" (public domain) and CipherChat (arXiv:2308.06463), which frames classical ciphers as a jailbreak technique.

Tests

  • pytest tests/unit/converter/test_columnar_transposition_converter.py — 13/13 passing.
  • All pre-commit hooks pass (ruff format/check, ty type-check, keyword-only, _async suffix, public-import).

🤖 Generated with Claude Code

…her)

Implements the standard incomplete-columns columnar transposition cipher as a
PyRIT converter: plaintext is written row-wise under a keyword and columns are
read out in the alphabetical order of the keyword's letters. Pure permutation
over all characters, no padding. Registered as an encoding technique in the
garak Encoding scenario. Stdlib only.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 01:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new technique breaks aggregate test expectations, and its decoding prompts omit the keyword and incomplete-column convention needed for reliable decoding.

2 open issues
What changed in this PR

Adds a configurable columnar transposition cipher to PyRIT’s converter catalog and integrates it into the Garak encoding scenario.

Changes:

  • Implements case-insensitive keyword ordering and padding-free transposition that preserves all input characters.
  • Adds a lazy public export and an ALL-only encoding technique using ZEBRAS.
  • Adds 13 unit tests covering known output, edge cases, identifiers, and validation.
File Description
tests/​unit/​converter/​test_columnar_transposition_converter.py Tests cipher behavior and input validation.
pyrit/​scenario/​scenarios/​garak/​encoding.py Registers the ALL-only technique and converter variant.
pyrit/​converter/​columnar_transposition_converter.py Implements the keyword-based transposition converter.
pyrit/​converter/​__init__.py Exposes the converter through lazy imports.

Review effort: Balanced

Zalgo = ("zalgo", set[str]())
LeetSpeak = ("leet_speak", {"default"})
AsciiSmuggler = ("ascii_smuggler", set[str]())
ColumnarTransposition = ("columnar_transposition", set[str]())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 68795f2: test_encoding.py now asserts 18 techniques and includes columnar_transposition in the ALL-only set, with DEFAULT expectations unchanged.

([ZalgoConverter()], "zalgo", "zalgo"),
([LeetspeakConverter()], "leet_speak", "leet_speak"),
([AsciiSmugglerConverter()], "ascii_smuggler", "ascii_smuggler"),
([ColumnarTranspositionConverter(key="ZEBRAS")], "columnar_transposition", "columnar_transposition"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in a2c55a9: the converter now exposes decode_prompt_description (built from its key), and the Encoding scenario feeds that into the decode templates only — disclosing the ZEBRAS keyword and incomplete-column read order — while the raw variant, technique slug, selection, and display grouping still key off the plain columnar_transposition name. Added a test asserting decode prompts disclose the key and the raw variant does not.

@varunj-msft varunj-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like the new converter still needs a mention in the converter notebooks and the 0_converters modality table (test_all_converters_are_documented fails), and the "(17 encodings)" list in doc/scanner/garak.py might want a refresh too.

Raises:
ValueError: If ``key`` is empty or contains non-alphabetic characters.
"""
if not key or not all(char in string.ascii_letters for char in key):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this rejects the test_value placeholder, test_build_instance_from_converter fails in CI — maybe add an entry next to VigenereConverter in the overrides map in test_converter_service.py?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 68795f2 — added "ColumnarTranspositionConverter": {"key": "testvalue"} next to VigenereConverter in the overrides map.

…rter

- test_converter_service: add key override so the auto-instantiation test
  passes an alphabetic key (mirrors VigenereConverter).
- test_encoding: bump ALL-aggregate count to 18 and add columnar_transposition
  to the ALL-only (non-default) set.
- doc notebook: document ColumnarTranspositionConverter in the text-to-text
  converters notebook so the documentation test passes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
… prompts

A keyed transposition cannot be reversed from the encoding name alone, so
decode prompts that only name the scheme make decoding failures ambiguous.
ColumnarTranspositionConverter now exposes a decode_prompt_description built
from its key, and the Encoding scenario feeds that description into the decode
templates (AskToDecodeConverter) while leaving the raw variant, technique slug,
selection, and display grouping unchanged. Addresses PR review feedback.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

This branch has not been deployed

No deployments
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.

3 participants