Skip to content

FEAT: Add RailFenceConverter (rail_fence converter) - #4442

Open
Roman Lutz (romanlutz) wants to merge 3 commits into
microsoft:mainfrom
romanlutz:gap1-railfence
Open

Roman Lutz (romanlutz) wants to merge 3 commits into
microsoft:mainfrom
romanlutz:gap1-railfence

Conversation

@romanlutz

@romanlutz Roman Lutz (romanlutz) commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Adds this converter as part of the gap-1 converter series (splitting the esoteric cipher / encoder set into per-converter and per-base PRs). Includes a unit test and garak Encoding-scenario registration; CI-validated locally.

Sibling PRs will be cross-linked once the series is open.

🤖 Generated with Claude Code

gap-1 converter series — splitting the esoteric cipher / encoder set into reviewable PRs (open each independently; they touch the same four registration files, so later ones rebase as earlier ones merge):

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 23:34

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 paired notebook is missing the new RailFenceConverter import, example, and executed output, leaving the two documentation formats out of sync.

1 open issue
What changed in this PR

Adds a configurable Rail Fence cipher converter to PyRIT and makes it available in the Garak Encoding scenario.

Changes:

  • Implements text transposition with configurable rails and exposes the converter through lazy package exports.
  • Registers rail_fence as an ALL-only encoding technique, leaving the default selection unchanged.
  • Adds converter tests, updates scenario expectations, and adds a Python documentation example.
File Description
tests/​unit/​scenario/​garak/​test_encoding.py Updates technique counts and ALL-only expectations.
tests/​unit/​converter/​test_rail_fence_converter.py Tests cipher vectors, edge cases, validation, and identifiers.
pyrit/​scenario/​scenarios/​garak/​encoding.py Registers the Rail Fence technique and converter variant.
pyrit/​converter/​rail_fence_converter.py Implements the configurable text-to-text cipher.
pyrit/​converter/​__init__.py Adds public lazy and type-checking exports.
doc/​code/​converters/​1_text_to_text_converters.py Adds a Rail Fence usage example.

Review effort: Balanced

print("ROT13:", await ROT13Converter().convert_async(prompt=prompt)) # type: ignore
print("Base64:", await Base64Converter().convert_async(prompt=prompt)) # type: ignore
print("Base2048:", await Base2048Converter().convert_async(prompt=prompt)) # type: ignore
print("Rail fence:", await RailFenceConverter().convert_async(prompt=prompt)) # type: ignore

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.

Agreed. At 8206f21 the notebook still has no RailFenceConverter import, example, or output, and the docs instructions require the .py and .ipynb pair to stay in sync. The notebook also lacks the ColumnarTranspositionConverter example that is already in the .py file, so one regeneration from the .py adds both.

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.

Reviewed at 8206f21. The converter is correct: an independent encoder and decoder matched it on 546 random cases. The comments below are small improvements (P3). One more item has no line in this diff:

[P3] Update the Encoding technique list and the converter modality table. doc/scanner/garak.py and garak.ipynb list the available Encoding techniques with a count, and the list does not include RailFence. A reader cannot learn that rail_fence can be selected. Add RailFence to the list and update the count against the current text on main (21 techniques today), or remove the hard-coded count. .github/instructions/converters.instructions.md also requires the modality table in doc/code/converters/0_converters.ipynb to include new converters. Re-run 0_converters.py so the table includes RailFenceConverter.

Note: the PR now conflicts with main because sibling converter PRs edit the same registration files.

"""
if rails < 2:
raise ValueError("rail fence rails value invalid, must be an integer greater than or equal to 2.")
self.rails = rails

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.

[P3] Add decode_prompt_description to RailFenceConverter

The Encoding scenario names the cipher in its decode prompts with the decode_prompt_description property. RailFenceConverter does not define it, so the two garak decode prompts that name the encoding say only "rail_fence" and never give the rail count. The rail count is the key of this cipher, and the scenario comment in encoding.py says a keyed cipher should disclose its key. Add a decode_prompt_description property, as in ColumnarTranspositionConverter, that states the rail count and the zigzag convention (start on the top rail, read rail by rail from the top). Add a converter test that the description contains the rail count.

Raises:
ValueError: If ``rails`` is less than 2.
"""
if rails < 2:

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.

[P3] Reject non-integer rails in the constructor

The constructor checks only rails < 2. A float such as 2.5 or 3.0 is accepted and stored in the identifier. A REST or GUI request can pass it, because the registry coerces only strings. Every later conversion then fails with a TypeError from range(self.rails). Check that rails is an int and not a bool before the range check, and include the value in the ValueError, as RepeatTokenConverter and GridCompositeConverter do. Add 2.5 to the invalid-rails test.

assert result.output_type == "text"


def test_rail_fence_converter_invalid_rails_too_few():

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.

[P3] Parametrize the copied rail fence tests

The invalid-rails tests for 1 and 0 differ only in the value. The default-rails test repeats the known-vector assertion with the default constructor. Replace the two invalid-rails tests with one parametrized test, and add 2.5 when the constructor rejects non-integers. Build the known-vector test with RailFenceConverter() so it also proves the default, and delete test_rail_fence_converter_default_rails_is_three.

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.

Automated review completed. No blocking concerns remain. This review records source coverage; merging is handled separately.

[P3] inline:4636564174: Copilot: paired notebook 1_text_to_text_converters.ipynb lacks the RailFenceConverter import/example/output. Supported and unfixed at head (0 'RailFence' matches in the .ipynb; the .py adds lines 67 and 88). Framework Docs: .py/.ipynb pairs stay in sync. Stale generated docs, P3. Fix: regenerate the notebook from the .py (this also adds the already-merged ColumnarTransposition example). Our agreement reply inline:4690678701 is in this thread; local F5 is tracked here.

[P3] inline:4690678462: Posted F1: RailFenceConverter has no decode_prompt_description, so the two garak decode templates that use {encoding_name} (indexes 1 and 2 of 4; raw/decode0/decode3 name no encoding by design) say only 'rail_fence' without the rail count, unlike ColumnarTranspositionConverter. Supported, unfixed. P3 by concrete impact: no wrong verdict, crash or mislabelled error (DecodingScorer still judges what the target produced); effect is a possibly lower decode rate on two hint variants; rails=3 is the textbook default and BifidConverter already runs at conventional parameters without a description. Fix: add a decode_prompt_description stating the rail count and zigzag convention, plus a converter unit test.

[P3] inline:4690678466: Posted F2: constructor (rail_fence_converter.py:48) checks only rails < 2; registry coerces only strings (resolution.py:736), so a REST/GUI float 2.5 or 3.0 constructs, enters the identifier, then every conversion raises TypeError from range(self.rails) (reproduced). Framework Fail fast and Registry place validation in the constructor. Loud failure on invalid input: P3. Fix: isinstance(rails, int) and not bool, value in the ValueError, add 2.5 to the test. Same pattern in BifidConverter.period predates the PR and is not reached by it (follow-up).

[P3] inline:4690678471: Posted F3: invalid-rails tests for 1 and 0 differ only in the value, and the default-rails test repeats the known vector. Framework Tests: parametrize instead of copying. Supported, unfixed, P3. Fix: one parametrized invalid-rails test (0, 1, 2.5) and build the known-vector test with the default constructor.

[P3] local:F4: doc/scanner/garak.py and garak.ipynb list Encoding techniques with a count (head 18, origin/main 21) and omit RailFence, which this PR makes selectable; origin/main 0_converters.ipynb modality table has ColumnarTranspositionConverter but no RailFenceConverter, and converters.instructions.md requires new converters there. Catalog/generated docs omission, P3. Files are outside the PR diff, so it was posted only in our review body; this local ID is its only tracker. Fix against main's text: add RailFence (22) or drop the count, and re-run 0_converters.py into its .ipynb.

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