Repository navigation
FEAT: Add TapCodeConverter (tap_code converter) - #4444
Roman Lutz (romanlutz) wants to merge 3 commits into
Conversation
Co-Authored-By: Claude Opus 4.8 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The converter silently changes some non-ASCII characters into tap sequences because it uppercases them before checking for ASCII, violating its promised pass-through behavior.
1 open issue
What changed in this PR
Adds TapCodeConverter to PyRIT’s text transformation library and makes it available in the Garak Encoding scenario.
Changes:
- Implements tap-code encoding with configurable symbols and separators.
- Exports the converter and registers
tap_codeas an ALL-only encoding technique. - Adds converter tests, updates scenario expectations, and includes a Python documentation example.
| File | Description |
|---|---|
tests/unit/scenario/garak/test_encoding.py |
Updates technique counts and ALL-only expectations. |
tests/unit/converter/test_tap_code_converter.py |
Tests encoding, configuration, identifiers, and input validation. |
pyrit/scenario/scenarios/garak/encoding.py |
Registers the tap-code technique and converter variant. |
pyrit/converter/tap_code_converter.py |
Implements configurable tap-code conversion. |
pyrit/converter/__init__.py |
Adds the converter to public lazy exports. |
doc/code/converters/1_text_to_text_converters.py |
Adds a tap-code usage example. |
Review effort: Balanced
| """ | ||
| tokens: list[str] = [] | ||
| for char in text: | ||
| coordinate = self._coordinates.get(char.upper()) |
There was a problem hiding this comment.
[P2] Agree. Please also add a test.
The lookup uppercases first, so 'ı' (U+0131) becomes 'I' and 'ſ' (U+017F) becomes 'S'. A scan of all code points shows these are the only two that map to an ASCII letter. NatoConverter fixed the same cause with a char.isascii() check and a test over 'é', 'ß', 'ı' and 'ñ'. Do the same here: look up the coordinate only when char.isascii(). The current passthrough test uses only '1' and '!', so it would not catch this bug. Add a parametrized test for 'ı', 'ſ', 'é' and 'ß'.
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Reviewed head bda1b5e. The converter is a welcome named leaf. Three P2 issues need changes: unneeded unvalidated rendering options, the K to C fold that makes correct decodes score as failures in the Encoding scenario, and the non-ASCII lookup (reply in the existing thread). The P3 comments are smaller test and docs fixes. The branch also conflicts with main.
| def __init__( | ||
| self, | ||
| *, | ||
| tap_symbol: str = ".", |
There was a problem hiding this comment.
[P2] Remove the unneeded rendering options.
The constructor accepts tap_symbol, group_separator and letter_separator, but no caller sets them. Only two tests do. The registry passes user input to this constructor without checks. With tap_symbol='' the letters 'AB' and 'ZZ' both become four spaces. With equal separators, 'A' and '..' both become '. .'. The attack then sends text that cannot be decoded, and nothing raises. Remove the three options and keep '.', ' ' and ' ' as constants. The default output is uniquely decodable, so no caller loses anything. If you keep the options, validate them in init and raise ValueError.
| for index, letter in enumerate(self._GRID): | ||
| self._coordinates[letter] = (index // 5 + 1, index % 5 + 1) | ||
| # K shares C's cell. | ||
| self._coordinates["K"] = self._coordinates["C"] |
There was a problem hiding this comment.
[P2] Expose the K to C fold to the Encoding scenario.
The converter encodes K as C. A target that decodes the taps correctly returns 'CAFFIR' for the seed 'Kaffir'. DecodingScorer matches only the original text or the decoded_text metadata, so it scores this correct decode as FAILURE. About 16 of the 73 default garak seeds contain K. After you rebase on main, add to_ascii_equivalent(*, text) that returns the text with K folded to C. The scenario already reads this hook and attaches decoded_text to the seed copies. Add a test that a faithful decode of a K payload scores true.
| "ascii_smuggler", | ||
| "bifid", | ||
| "columnar_transposition", | ||
| "tap_code", |
There was a problem hiding this comment.
[P3] Add a test for the tap_code scenario row.
The only test change here edits the technique set and count. If the row ('tap_code') is dropped or misnamed in Encoding._converter_variants, the enum member stays but selecting it builds no attacks. The Encoding tests still pass in that case; this is likely during your merge with main, which edits the same rows. Add a test that selects EncodingTechnique.TapCode and checks that the tap_code attacks are built with a TapCodeConverter. Main has this test for disemvowel.
| from pyrit.converter import ConverterResult, TapCodeConverter | ||
|
|
||
|
|
||
| async def test_tap_code_converter_single_letter(): |
There was a problem hiding this comment.
[P3] Merge the copied letter tests into one parametrized test.
Four tests (single letter, WATER, AEVZ, C/K) check the same behavior: letter to tap groups. The 'A' case is already inside 'AEVZ'. The C/K test also compares two outputs that it has just checked. Use one pytest.mark.parametrize test with the inputs and expected outputs. Check output_type once, for example in the empty-input test.
| AsciiSmuggler = ("ascii_smuggler", set[str]()) | ||
| Bifid = ("bifid", set[str]()) | ||
| ColumnarTransposition = ("columnar_transposition", set[str]()) | ||
| TapCode = ("tap_code", set[str]()) |
There was a problem hiding this comment.
[P3] Add TapCode to the scanner guide.
This change adds the tap_code technique, but doc/scanner/garak.py and doc/scanner/garak.ipynb list the Encoding techniques with a count and do not include it. Readers of the guide will not learn that --techniques tap_code exists. After you merge main, add TapCode to the list in both files and set the count to 22.
| 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("Tap code:", await TapCodeConverter().convert_async(prompt=prompt)) # type: ignore |
There was a problem hiding this comment.
[P3] Update the paired notebook.
This change adds the tap code demo to the .py file only. The rendered page uses 1_text_to_text_converters.ipynb, so readers do not see the demo. The next jupytext run will also create an unrelated diff. Add the TapCodeConverter import, the print line and the 'Tap code:' output line to the notebook, so both files match.

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