Repository navigation
FEAT: Add Base32Converter on a shared byte-encoding base with Base64Converter - #4452
Roman Lutz (romanlutz) wants to merge 3 commits into
Conversation
Replaces the Base64Converter(encoding_func="b32encode") variant in the garak Encoding scenario with a dedicated Base32Converter that also exposes the RFC 4648 Extended-Hex (b32hex) alphabet. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Saved Encoding runs that include Base32 can no longer resume because the converter change alters group IDs without a corresponding scenario version bump.
1 open issue
What changed in this PR
Adds a dedicated RFC 4648 Base32 converter to PyRIT’s prompt-transformation components, supporting both standard and Extended Hex alphabets.
Changes:
- Implements
Base32Converterwith UTF-8 encoding and variant-specific identifiers. - Exports it through the converter package and uses its default variant in the garak Encoding scenario.
- Adds unit tests and a Python documentation example.
| File | Description |
|---|---|
tests/unit/converter/test_base32_converter.py |
Tests encoding vectors, Unicode, modalities, and identifiers. |
pyrit/scenario/scenarios/garak/encoding.py |
Replaces the existing Base32 catalog implementation. |
pyrit/converter/base32_converter.py |
Implements standard and Extended Hex Base32 encoding. |
pyrit/converter/__init__.py |
Adds lazy and type-checking exports. |
doc/code/converters/1_text_to_text_converters.py |
Adds a Base32 usage example. |
Review effort: Balanced
| ([Base2048Converter()], "base2048", "base2048"), | ||
| ([Base64Converter(encoding_func="b16encode")], "base16", "base16"), | ||
| ([Base64Converter(encoding_func="b32encode")], "base32", "base32"), | ||
| ([Base32Converter()], "base32", "base32"), |
There was a problem hiding this comment.
Preserve the existing Base32 catalog entry
I agree with the saved-run concern. Resuming a saved Encoding run that includes Base32 now fails because the new converter class and parameters change its planned group IDs, even though the encoded text is the same. Restore Base64Converter(encoding_func="b32encode") in this catalog entry and remove the unused import. A VERSION bump would still reject the old runs; it would not preserve resume. This fix does not require removing the new Base32Converter class.
Varun Poojari (Varunpoojari)
left a comment
There was a problem hiding this comment.
Thanks for this. The RFC 4648 test vectors are a good way to pin the behaviour, and the converter reads cleanly. I read the diff but did not run the code or the tests, so these are review notes, not test results.
-
Validate
variantin__init__. An unsupported value (for exampleBase32Converter(variant="b64")) is only caught later, inconvert_async, asValueError("Unsupported Base32 variant"). TheLiteraltype helps static checkers, but at runtime a typo only surfaces mid-run. Checking in the constructor would fail early, which is also whatMathematicalAlphanumericConverterdoes forstylein #4451. A small test for the invalid-variant case would round this out, since the current tests don't cover that branch. -
Overlap with
Base64Converter(encoding_func="b32encode"). The Garak encoding scenario now usesBase32Converter()instead, with the same output for the standard alphabet. Is theb32encodeoption onBase64Convertermeant to stay for backwards compatibility? If so, the new converter's docstring could mention it. Also, because converter identifiers include the class name, results stored from earlier scenario runs will get a different converter identity from now on. That is probably acceptable, but worth a line in the PR description. -
Docs.
doc/code/converters/1_text_to_text_converters.pyis updated. If this repo keeps a paired.ipynbfor that file, it would need to be synced too. I could not tell from the diff whether it does. -
Test style. The input and output support tests check
"image_path". That is fine, but a second unsupported type such as"audio_path"would make the "text only" claim a bit stronger. Optional.
The b32 and b32hex paths both map directly onto the standard library functions, and the UTF-8 multi-byte test is a good addition.
There was a problem hiding this comment.
Please simplify this rather than introduce a second public converter. Standard Base32 already works with Base64Converter(encoding_func="b32encode"); its output matches the new standard variant. Base32hex is also already expressible by following that converter with LetterBijectionConverter(mapping=dict(zip("abcdefghijklmnopqrstuvwxyz234567", "0123456789ABCDEFGHIJKLMNOPQRSTUV", strict=True))). I checked that composition through the real public components on 105 empty, RFC-vector, Unicode, multiline and varied ASCII inputs against the standard library.
A small b32hexencode selector on the existing Base64Converter would be a useful convenience: one supported value and dispatch branch instead of a second instance and a 32-symbol mapping. A synchronized guide example of the existing composition is also an adequate smaller form. Neither needs another public converter class/export or a replacement of the standard-Base32 entry in garak.encoding. Keep that entry's current converter identity. This follows the framework's reuse-before-adding principle; the benefit is operator simplicity, not previously unavailable encoding capability.
This resolves the API-overlap question in the earlier review; the existing saved-run identity concern remains in its own thread. This is an inclusion decision, not a complete implementation review. I stopped implementation review at this gate and have not approved the PR.
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Keep the named converter and complete the fixes
Updated maintainer review policy welcomes named converters even when existing configuration gives the same output, so I withdraw the inclusion objection in my earlier review. This review replaces it. Keep Base32Converter and its public export, but reuse Base64Converter underneath it. I agree with the constructor and notebook notes in the earlier review. Direct Python construction still accepts Base32Converter(variant="b64") and fails only on conversion; reject unsupported variants with ValueError in the constructor and add a negative test. The linked text-to-text notebook still lacks the new import and example: synchronize its code and saved output, and refresh the dynamic modality-table output in 0_converters.ipynb so it includes Base32Converter.
The saved-run resume concern stays open in its inline thread, and the new inline note below covers how to build the class on Base64Converter.
| from pyrit.models import ComponentIdentifier, PromptDataType | ||
|
|
||
|
|
||
| class Base32Converter(Converter): |
There was a problem hiding this comment.
[P2] Build on Base64Converter
Please keep the named converter, but build it on Base64Converter. That class already owns the input check, UTF-8 conversion, standard Base32 encoding, and result construction copied here. Apply the framework's reuse-before-adding principle to the implementation: add b32hexencode to the base encoder's supported functions and dispatch, then subclass it and map variant to encoding_func in the constructor. Inherit convert_async and retain the leaf's variant-based identifier.
…ters Per maintainer discussion, the standard base-N byte encodings (base16/32/64/85, all backed by the stdlib base64/binascii encoders) now share a private _ByteEncodingConverter base that dispatches on encoding_func. Base64Converter and Base32Converter are thin named members of that family; the custom-alphabet Base58/Base62/Base91 encoders stay standalone and do NOT inherit from it. Base32Converter loses its bespoke 'variant' arg (redundant with the shared encoding_func selection) and is now a no-arg standard-RFC-4648 converter; the niche b32hex alphabet is dropped. Output and the public Base64Converter identity are unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
The reuse and variant notes are addressed at this head
The shared byte-encoding implementation now addresses the duplication in the earlier reuse note: both named converters use one encoder, input guard, UTF-8 conversion, and result implementation, and the source review confirms Base64 output and identity compatibility. A direct Base64Converter subclass is an alternative, not a further required rewrite. Base32Converter() also takes no arguments, so the invalid-variant path in the earlier constructor note is gone. These current-head decisions update the combined review; keep the named standard-only leaf.
| from pyrit.models import ComponentIdentifier, PromptDataType | ||
|
|
||
|
|
||
| class _ByteEncodingConverter(Converter): |
There was a problem hiding this comment.
[P2] Keep the private base out of converter discovery
ConverterRegistry discovers every concrete Converter subclass under pyrit.converter, including this unexported base. The source review found _ByteEncodingConverter in get_class_names(), confirmed that create_instance("_ByteEncodingConverter", encoding_func="b32encode") succeeds, and traced it into the backend converter-type catalog. This makes an implementation-only class a public buildable type with its own persisted identity. Make the base non-buildable in the registry while keeping the two named converters discoverable, and add a focused catalog regression test. The shared encoding implementation can remain; this does not require rewriting both converters.

Adds
Base32Converter(standard RFC 4648 Base32) as part of the gap-1 converter series.Per maintainer discussion, the standard base-N byte encodings (base16/32/64/85, all backed by the stdlib
base64/binasciiencoders) now share a private_ByteEncodingConverterbase that dispatches onencoding_func.Base64ConverterandBase32Converterare thin named members of that family. The custom-alphabet encoders (Base58/Base62/Base91) are algorithmically unrelated and stay standalone — they do not inherit from this base.Base32Converter()takes no args (standard alphabet only). The earliervariant/b32hexsurface was dropped: it was redundant with the sharedencoding_funcselection, and b32hex is niche. Output and the publicBase64Converteridentity are unchanged.Includes unit tests and garak Encoding-scenario registration; CI-validated locally.
🤖 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):