Skip to content

FEAT: Add Base91Converter (base91 binary-to-text encoding) - #4441

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

Roman Lutz (romanlutz) wants to merge 2 commits into
microsoft:mainfrom
romanlutz:gap1-base91

Conversation

@romanlutz

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

Copy link
Copy Markdown
Contributor

Adds a pure-Python Base91 binary-to-text converter (no third-party dependency) and registers it as an ALL-only technique in the garak Encoding scenario, with a unit test and a doc example.

Part of a series splitting the gap-1 converter set into reviewable per-converter / per-base PRs (template PR — opened as draft while the layout is confirmed).

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

Add a pure-Python Base91 converter (no third-party dependency) and register it as
an ALL-only technique in the garak Encoding scenario, with a unit test and a doc
example.

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

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

Expanding all prevents previously saved Encoding runs from resuming, and the Base91 encoder’s 14-bit branch lacks boundary and zero-value test coverage.

2 open issues
What changed in this PR

Adds dependency-free Base91 encoding to PyRIT’s converter library and makes it available in the garak Encoding scenario.

Changes:

  • Implements UTF-8 text-to-Base91 conversion and exposes it through lazy package exports.
  • Registers Base91 as an ALL-only encoding technique, leaving the default selection unchanged.
  • Adds converter unit tests and a Python-format documentation example.
File Description
tests/​unit/​scenario/​garak/​test_encoding.py Updates the technique count and ALL-only expectations.
tests/​unit/​converter/​test_base91_converter.py Tests known vectors, Unicode, empty input, and supported types.
pyrit/​scenario/​scenarios/​garak/​encoding.py Registers the Base91 technique and converter variant.
pyrit/​converter/​base91_converter.py Implements the pure-Python Base91 converter.
pyrit/​converter/​__init__.py Adds public lazy exports for Base91Converter.
doc/​code/​converters/​1_text_to_text_converters.py Adds a Base91 conversion example.

Review effort: Balanced

LeetSpeak = ("leet_speak", {"default"})
AsciiSmuggler = ("ascii_smuggler", set[str]())
Bifid = ("bifid", set[str]())
Base91 = ("base91", 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.

The resume problem is real, but this PR does not cause it and does not need to fix it. _restore_launch_request in scenario_run_service.py replays the saved ['all'] token, which re-expands against the current enum, while Scenario._build_scenario_identifier hashes the sorted resolved techniques. Every ALL-only addition triggers this, and earlier ones (Bifid, ColumnarTransposition) already did. The same happens to DEFAULT whenever its members change. This PR adds one more catalog member and no new path or caller. Fix it in a separate backend change: when the saved request held aggregate tokens, restore identifier.techniques. The failure is loud: the resume is refused, not run partially.

# Reference basE91 vector: "Hello World!" -> ">OwJh>Io0Tv!8PE".
converter = Base91Converter()
result = await converter.convert_async(prompt="Hello World!", input_type="text")
assert result.output_text == ">OwJh>Io0Tv!8PE"

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] Agree that the 14-bit branch is untested, but the zero-valued group does not close the gap. '\u0000\u0000' -> AAA and 'X\u0000' -> }AA give the same output when the branch is removed or the threshold changes to value >= 88, because the 14th bit is 0. Add a vector with the 14th bit set: 'X ' (0x58 0x20, low 13 bits = 88, value 8280) -> ""A; both mutants return }AB. Add 'Y ' (low 13 bits = 89) -> ~AB for the 13-bit side. Also add 'A' -> %A: it checks the num_bits > 7 flush condition, which 'a' -> GB does not catch. All three round-trip through the reference decoder. Put all exact vectors in one parametrized test; the alphabet-membership tests then add nothing and can go.

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.

Update: this is now [P3], not blocking. We ran the 14-bit branch and the num_bits > 7 flush on 200,000 random inputs, and a reference decoder got back every input (0 failures). The encoder is correct. Only the test is missing. Please still add the three vectors ('X ' -> ""A, 'Y ' -> ~AB, 'A' -> %A) as one parametrized test, so a later change to these paths fails a test.

# Conflicts:
#	pyrit/scenario/scenarios/garak/encoding.py
#	tests/unit/scenario/garak/test_encoding.py

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 d6690e8. The Base91 encoder matches the basE91 reference: two independent decoder ports round-tripped more than 190,000 inputs with no failures at this head. The remaining items are tests and docs. One P2 test-coverage item is a reply in the existing thread on test_base91_converter.py, and the resume concern on encoding.py has a reply that explains why it is out of scope here.

One item has no inline location because the PR does not change the file:

  • [P3] Add Base91 to the 'Available techniques' list and its count in doc/scanner/garak.py and doc/scanner/garak.ipynb. The PR adds the base91 technique, but the list does not name it, so readers cannot find --techniques base91 on that page. Update both files when you rebase, or refresh the list once when the converter series lands.

"ascii_smuggler",
"bifid",
"columnar_transposition",
"base91",

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 a test that the base91 variant row is registered. A deleted or misnamed base91 row in Encoding._converter_variants() leaves the enum member in place, so techniques=['all'] builds no base91 attacks and all tests still pass. The other converter PRs in this series edit the same list, so a row can be lost during a rebase. After the rebase, follow the origin/main tests test_disemvowel_technique_builds_registered_row and test_ubbi_dubbi_technique_builds_registered_row, but parametrize them: select EncodingTechnique.Base91, assert that the base91_raw and decode attacks are built and that the raw attack uses a Base91Converter. Alternatively, assert that the variant names equal the enum values.

Encode raw bytes to a basE91 string using Joachim Henke's algorithm.

Bits are accumulated little-endian in an integer bit buffer. Whenever at
least 13 bits are buffered, the low 13 bits are inspected: if their value

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] Correct the _base91_encode docstring. It says groups are emitted when at least 13 bits are buffered, but the code uses num_bits > 13, which needs at least 14 bits. It also says 14 bits keep every value below 8192 representable. The actual reason is that when the low 13 bits are 88 or less, the 14-bit value is at most 8280 (91*91-1) and still fits two base-91 digits. A maintainer who follows the docstring can change the threshold and break interoperability. Cite the reference in one line, or fix both clauses. Also fix the comment at tests/unit/converter/test_base91_converter.py:26: one byte leaves 8 bits, which flush as two characters (GB), not one.

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("Base91:", await Base91Converter().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.

[P3] Update doc/code/converters/1_text_to_text_converters.ipynb with this example. The PR changes only the .py file, and the docs instructions require the .py and .ipynb pair to match. The published page is built from the notebook, so it shows no Base91 example. Add Base91Converter to the import cell, add this print line to the code cell, and add the output line Base91: text: fPwJ/Wwe#F!&WF<oUBUf][TX2$g%9MjL^zmfZ after the Base2048 output.

"AzureSpeechTextToAudioConverter": "pyrit.converter.azure_speech_text_to_audio_converter",
"Base2048Converter": "pyrit.converter.base2048_converter",
"Base64Converter": "pyrit.converter.base64_converter",
"Base91Converter": "pyrit.converter.base91_converter",

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] Update the modality table in doc/code/converters/0_converters.ipynb. This export adds Base91Converter to the list that get_converter_modalities() reads, but the stored table output in that notebook does not include it, and the converter instructions require this update for new converters. Readers of the published catalog will not find the converter. Re-execute the notebook, or add the row text text Base91Converter after Base64Converter and renumber. If the series will refresh the table once at the end, say so in the PR.

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:4636360934: Rerated P2 -> P3 under the changed priority rule (policy.md at e3bdbf1: missing tests are P3; P2 needs wrong behavior shown by a reproduction or trace). Same cause as local:F1, supported and not fixed: no PR test input takes the 14-bit branch (value <= 88), and the head test file still has only 'test', 'Hello World!', 'a' and empty vectors; earlier mutants of the branch pass all 10 PR tests. Impact: the untested path is correct today. Run at d6690e8 with scratch_f1_impact.py (rerate output folder, worktree .venv, run from outside the checkout): 200,000 random inputs through the head encoder; 75,863 took the 14-bit branch (115,163 groups) and 26,041 took the num_bits>7-only flush; 0 round-trip failures through an independent port of Henke's reference decoder and 0 mismatches against an independent reference-style encoder. Vectors match the reference via convert_async: 'X ' -> '""A' (14-bit), 'A' -> '%A' (flush), 'Y ' -> '~AB'. No wrong result, swallowed error or structural violation, so this is a missing test: P3, kept for the repair pass. Copilot's zero-valued-group vector alone does not discriminate the mutants; use 'X ', 'Y ' and 'A'.

[P3] inline:4690711803: Rerated P2 -> P3 under the changed priority rule. Our reply carrying F1 (same cause as inline:4636360934 and local:F1); supported and not fixed. Its P2 basis, 'a missing test for risky changed behavior', is no longer a P2 rule, and its own evidence said the code is correct today. Run at d6690e8 with scratch_f1_impact.py (rerate output folder, worktree .venv, run from outside the checkout): 200,000 random inputs through the head encoder; 75,863 took the 14-bit branch (115,163 groups) and 26,041 took the num_bits>7-only flush; 0 round-trip failures through an independent port of Henke's reference decoder and 0 mismatches against an independent reference-style encoder. Vectors match the reference via convert_async: 'X ' -> '""A' (14-bit), 'A' -> '%A' (flush), 'Y ' -> '~AB'. P3 nonblocking; the repair pass adds one parametrized test with these vectors.

[P3] inline:4690712388: Local finding F2, supported and not fixed. Dropping or renaming the base91 row in Encoding._converter_variants leaves test_encoding.py (32 passed) and related files (98 passed) green, so a lost row after a rebase builds no base91 attacks silently while the enum member stays. origin/main has the equivalent row tests for disemvowel and ubbi_dubbi. P3: the row is trivial and correct today; this is a coverage improvement.

[P3] inline:4690712399: Local finding F3, supported and not fixed. base91_converter.py:44-45 says groups are emitted at least 13 bits buffered, but line 65 is if num_bits > 13 (at least 14); the stated reason for the 14-bit case is wrong (a low-13-bit value of 88 or less gives a 14-bit value of at most 8280, still two base-91 digits). test_base91_converter.py:26 says one output char but asserts 'GB' (two). A docstring-aligned >= 13 edit raises ValueError on random input, so the risk is real. P3 documentation accuracy.

[P3] inline:4690712408: Local finding F4, supported and not fixed. Only doc/code/converters/1_text_to_text_converters.py of the jupytext pair changes; the .ipynb has Base2048 (import, print, output) and no Base91, and doc/myst.yml publishes the .ipynb, so the published page lacks the example. docs.instructions.md requires both files of the pair. Computed output for the doc prompt: fPwJ/Wwe#F!&WF<oUBUf][TX2$g%9MjL^zmfZ. P3 documentation omission.

[P3] inline:4690712416: Local finding F5, supported and not fixed. converters.instructions.md requires updating the modality table in doc/code/converters/0_converters.ipynb for new converters; the stored table has no Base91Converter at head or on origin/main (it belongs after Base64Converter). Later refreshes of that table by sibling PRs lower the impact. P3 documentation omission.

[P3] review:6052231452: Our COMMENT review body at this head. It summarises the thread replies and carries finding F6 (no inline location because the file is unchanged): doc/scanner/garak.py and doc/scanner/garak.ipynb 'Available techniques' list and count do not name base91; they are published via doc/myst.yml, and sibling PRs (#4448, #4449) updated them for their techniques. Supported and not fixed at the unchanged head. P3 documentation omission, also tracked as local:F6. Its other items refer to inline:4690711803 and inline:4690712043 assessed above.

[P3] local:F1: Rerated P2 -> P3 under the changed priority rule (feedback.json in the rerate folder). Supported and not fixed: the 10 PR tests pass for mutants of the 14-bit branch (always-13-bit, >=88, >87, >89, mask 8191) and of the flush condition, so no test guards them. The previous P2 rested on 'risky changed behavior without a test'; the new rule makes missing tests P3 and directs running the untested path instead. Run at d6690e8 with scratch_f1_impact.py (rerate output folder, worktree .venv, run from outside the checkout): 200,000 random inputs through the head encoder; 75,863 took the 14-bit branch (115,163 groups) and 26,041 took the num_bits>7-only flush; 0 round-trip failures through an independent port of Henke's reference decoder and 0 mismatches against an independent reference-style encoder. Vectors match the reference via convert_async: 'X ' -> '""A' (14-bit), 'A' -> '%A' (flush), 'Y ' -> '~AB'. The previous review also had 0 failures in more than 190,000 round trips. The output is correct, so no user-relied result changes; the fix (one parametrized vector test) does not reshape the change. P3 nonblocking for the repair pass. Extends Copilot thread 4636360934 (same cause).

[P3] local:F2: Supported P3, not fixed (feedback.json F2). Dropping or renaming the base91 row in Encoding._converter_variants leaves test_encoding.py (32 passed) and 98 related tests green; origin/main has row tests for disemvowel and ubbi_dubbi that this branch lacks until it rebases.

[P3] local:F3: Supported P3, not fixed (feedback.json F3). Docstring says at least 13 bits but code is num_bits > 13; the stated reason for the 14-bit case is wrong; test comment at line 26 says one char but asserts 'GB'.

[P3] local:F4: Supported P3, not fixed (feedback.json F4). Only the .py of the jupytext pair changes; the published .ipynb has no Base91 example.

[P3] local:F5: Supported P3, not fixed (feedback.json F5). The modality table in doc/code/converters/0_converters.ipynb has no Base91Converter, which converters.instructions.md requires for new converters.

[P3] local:F6: Supported P3, not fixed (feedback.json F6). doc/scanner/garak.py and garak.ipynb technique list and count omit base91 (file unchanged by the PR, so reported in the review body).

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