Skip to content

FEAT: Add PolybiusConverter (polybius converter) - #4443

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

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

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:35

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

Merge-letter validation accepts multi-character entries, allowing invalid grid coordinates or construction-time KeyErrors instead of the documented ValueError.

2 open issues
What changed in this PR

Adds PolybiusConverter to PyRIT’s text-encoding converters and makes it available in the Garak Encoding scenario.

Changes:

  • Implements configurable Polybius-square encoding with merged letters and coordinate separators.
  • Exports the converter and registers it as an ALL-only encoding technique.
  • Adds unit coverage, identifier samples, and a Python documentation example.
File Description
tests/​unit/​scenario/​garak/​test_encoding.py Updates technique counts and ALL-only expectations.
tests/​unit/​registry/​converter_identifier_samples.py Adds custom merge-letter samples for identifier checks.
tests/​unit/​converter/​test_polybius_converter.py Covers encoding, configuration, validation, and identifiers.
pyrit/​scenario/​scenarios/​garak/​encoding.py Registers the Polybius technique and converter variant.
pyrit/​converter/​polybius_converter.py Implements Polybius-square text encoding.
pyrit/​converter/​__init__.py Adds the public lazy export and typing import.
doc/​code/​converters/​1_text_to_text_converters.py Adds a Polybius conversion example.

Review effort: Balanced

first, second = merge_letters
first = first.upper()
second = second.upper()
if first not in _ALPHABET or second not in _ALPHABET:

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.

I agree. No caller sets merge_letters: the scenario, the docs, and the registry use the default, and the registry cannot set a tuple[str, str] from the API. So the smallest fix is to remove the option and keep I and J in one cell, as BifidConverter does; this also removes the merge_letters samples in converter_identifier_samples.py. At head 129744d the same check also accepts ("I", "") (a 26-cell grid, Z encodes to 61) and lets ("I", 5) raise AttributeError. If the option stays, require each normalized entry to be one letter A-Z, put the bad value in the ValueError message, and add one parametrized negative test for these inputs.

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("Polybius:", await PolybiusConverter().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.

I agree that this is required before merge. At head 129744d, 1_text_to_text_converters.ipynb has no Polybius import or print line, so the pair is not in sync. 0_converters.py needs no edit, because it builds the modality table from pyrit.converter.__all__, which now includes PolybiusConverter; only the saved output of 0_converters.ipynb must change. Current main already shows the Bifid and ColumnarTransposition rows in that table, so after you merge main, the Polybius row is the only missing row. Merge main first, then sync the notebook pair and refresh both saved outputs.

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 of 129744d. I reviewed all changed source and docs. The converter is a welcome named leaf, but two issues should be fixed before merge: word gaps and digits look like coordinate pairs (line 146), and the two separator options have no current need (line 46). The other inline comments are small P3 improvements. I also replied on both earlier Copilot threads. Note: the branch conflicts with main in encoding.py and test_encoding.py, and some comments refer to code that is on main now.

pending.append(self._encoding[upper])
else:
flush()
parts.append(char)

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.

[P2] Keep word gaps and digits apart from coordinate pairs

With the default settings, the converter puts a space between coordinate pairs, but it writes a space or a digit from the prompt with no separator (this line). Thus "AB CD" and "ABCD" both give 11 12 13 14, "A1" and "1A" both give 111, and "step 12", "step b" and "stepb" all give 43 44 15 35 12. A target cannot recover the words or the numbers, so the docstring claim that the encoded prompt "keeps its structure" is not true. In the garak Encoding scenario, DecodingScorer matches the seed text with its internal spaces, so a literal decode cannot match multi-word seeds (for example 9 of 56 garak_slur_terms_en seeds and most garak_web_html_js seeds). Build the converter as NatoConverter is built (the same fix as #2399): subclass WordLevelConverter, encode only ASCII letters, join the output of each character with one space, and override join_words to write a word token such as /. Then change test_polybius_converter_word_boundary_grouping and test_polybius_converter_preserves_non_letters to assert different outputs for these inputs, and correct the docstring.

self,
*,
merge_letters: tuple[str, str] = ("I", "J"),
coordinate_separator: str = "",

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.

[P2] Remove the coordinate_separator and letter_separator options

No code sets coordinate_separator or letter_separator: the Encoding scenario, the doc example, and the registry use the defaults, and only the tests in this PR change them. The framework says not to add options for hypothetical needs. Each option is an external string input, so the GUI and REST converter form show two free-text fields, the identifier records them, and a later removal needs a deprecation. Some values also make output that a target cannot decode: coordinate_separator="1" encodes "AB" as 111 112, and an empty letter_separator brings back the collisions from the comment on line 146. Remove both options and write one fixed format: two-digit pairs, one space between tokens, and the word token from the comment on line 146. Remove the separator tests and the separator asserts in the identifier test. The merge_letters option has the same problem; my reply on the existing thread at line 68 covers it.


# Build the 5x5 grid from the alphabet with the second merged letter removed, then
# point the removed letter at the cell of the first merged letter.
grid_letters = [letter for letter in _ALPHABET if letter != second]

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] Use one owner for the Polybius square

Lines 79-85 build the same 5x5 square (alphabet without J, index // 5, index % 5) that BifidConverter._build_square and its coordinate maps already build. Draft #4458 moves the Bifid square into a shared _polybius_square.py helper, but it does not change this file. If both merge, PyRIT has two square builders with different rules, and they can drift. Move the square and the index-to-coordinate map into one small shared helper that both converters use, in this PR or by adding PolybiusConverter to #4458. Do not call the private Bifid classmethod from this converter.

row = index // 5 + 1
column = index % 5 + 1
self._encoding[letter] = f"{row}{coordinate_separator}{column}"
self._encoding[second] = self._encoding[first]

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] Let DecodingScorer accept the J-to-I text for polybius

This line encodes J as the cell of I, so a target that decodes the prompt correctly answers with I for each J. DecodingScorer matches the original seed text, so a seed that contains J cannot score true: 4 of 56 garak_slur_terms_en seeds and 14 of 17 garak_web_html_js seeds (for example each javascript: payload). Main now has a hook for this (#4437): when a converter has to_ascii_equivalent(*, text), the Encoding scenario adds the returned text as decoded_text metadata, and DecodingScorer accepts it. After you merge main, add to_ascii_equivalent that returns the text with J changed to I (and j to i), or None when nothing changes, and add a scenario test for a seed with J.

AsciiSmuggler = ("ascii_smuggler", set[str]())
Bifid = ("bifid", set[str]())
ColumnarTransposition = ("columnar_transposition", set[str]())
Polybius = ("polybius", set[str]())

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 Polybius to the Encoding technique list in the scanner guide

This line adds the polybius technique, but doc/scanner/garak.py and its garak.ipynb pair do not list it. Operators who read the guide to choose --techniques values do not find polybius, and the count is wrong. On main the list now says "(21 encodings)"; #4448 and #4449 updated both files for their techniques. After you merge main, add Polybius to the list in both files and change the count to 22.

"ascii_smuggler",
"bifid",
"columnar_transposition",
"polybius",

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] Test that selecting polybius builds its attacks

The only scenario test change updates the technique count and the ALL-only set. If the _converter_variants row is deleted or its name is misspelled, EncodingTechnique.Polybius still exists, so all tests pass, but selecting polybius builds no attacks. Main now has row tests for this regression (test_disemvowel_technique_builds_registered_row and test_ubbi_dubbi_technique_builds_registered_row). After you merge main, parametrize that row test over the techniques and add polybius, with a check that the row converter is a PolybiusConverter.

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