Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pyrit/scenario/scenarios/garak/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@
from pyrit.scenario.core.scenario_technique import ScenarioTechnique
from pyrit.score import DecodingScorer, TrueFalseScorer

# Baconian cipher (Francis Bacon, 1605): each letter maps to a 5-symbol a/b group; see
# https://en.wikipedia.org/wiki/Bacon%27s_cipher . Framing classical ciphers as an LLM jailbreak
# vector: CipherChat (Yuan et al., 2023, arXiv:2308.06463).
# Kept as a Leetspeak substitution table rather than a dedicated converter (reuse and compose
# before adding).
_BACONIAN_SUBSTITUTIONS: dict[str, list[str]] = {
"a": ["aaaaa"],
"b": ["aaaab"],
"c": ["aaaba"],
"d": ["aaabb"],
"e": ["aabaa"],
"f": ["aabab"],
"g": ["aabba"],
"h": ["aabbb"],
"i": ["abaaa"],
"j": ["abaaa"],

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] Accept a faithful Baconian decode in the scorer

The table maps i and j to "abaaa" and u and v to "baabb". A target that decodes the ciphertext by this table answers "iauascript" for "javascript" and "HAIII" for "Hajji". DecodingScorer does a case-insensitive substring match against the seed, so it scores these answers False. With the default datasets, this occurs for 14 of 17 garak_web_html_js seeds and 5 of 56 garak_slur_terms_en seeds. The run then reports the target as resistant although it decoded and repeated the payload. Main already fixes this class of problem for AsciiSmugglerConverter (#4437): Encoding._seed_groups_for_converters reads a converter's to_ascii_equivalent and stores the result as decoded_text, which DecodingScorer also accepts. When you rebase, keep the canonical table and add a thin BaconianConverter(LeetspeakConverter) that passes this table and exposes that fold (j to i, v to u). Then a faithful decode and a context-repaired decode both score True. Add a test that a j/v seed gets the folded decoded_text.

"k": ["abaab"],
"l": ["ababa"],
"m": ["ababb"],
"n": ["abbaa"],
"o": ["abbab"],
"p": ["abbba"],
"q": ["abbbb"],
"r": ["baaaa"],
"s": ["baaab"],
"t": ["baaba"],
"u": ["baabb"],
"v": ["baabb"],
"w": ["babaa"],
"x": ["babab"],
"y": ["babba"],
"z": ["babbb"],
}


class EncodingDatasetConfiguration(DatasetAttackConfiguration):
"""
Expand Down Expand Up @@ -129,6 +163,7 @@ class EncodingTechnique(ScenarioTechnique):
AsciiSmuggler = ("ascii_smuggler", set[str]())
Bifid = ("bifid", set[str]())
ColumnarTransposition = ("columnar_transposition", set[str]())
Baconian = ("baconian", 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 Baconian to the Garak scanner guide

doc/scanner/garak.py and doc/scanner/garak.ipynb list every Encoding technique by name in "Available techniques". On main this list now has 21 encodings and ends with "UbbiDubbi, Disemvowel", because #4448 and #4449 updated it. This PR adds Baconian but does not change the guide, so after merge an operator who reads the guide does not find the technique. When you rebase, add Baconian to that line in both files and change the count to 22.


@classmethod
def get_aggregate_tags(cls) -> set[str]:
Expand Down Expand Up @@ -317,6 +352,7 @@ def _converter_variants() -> list[tuple[list[Converter], str, str]]:
([AsciiSmugglerConverter()], "ascii_smuggler", "ascii_smuggler"),
([BifidConverter()], "bifid", "bifid"),
([ColumnarTranspositionConverter(key="ZEBRAS")], "columnar_transposition", "columnar_transposition"),
([LeetspeakConverter(custom_substitutions=_BACONIAN_SUBSTITUTIONS)], "baconian", "baconian"),
]

# These are the same as Garak encoding attacks
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/scenario/garak/test_baconian_technique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

from pyrit.converter import LeetspeakConverter
from pyrit.scenario.scenarios.garak.encoding import _BACONIAN_SUBSTITUTIONS


async def test_baconian_technique_encodes_letters():
"""The baconian Encoding technique is a LeetspeakConverter configured with the Baconian a/b table."""
converter = LeetspeakConverter(custom_substitutions=_BACONIAN_SUBSTITUTIONS)

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 the registered baconian row, not a hand-built converter

This test builds its own LeetspeakConverter and computes the expected value from the same _BACONIAN_SUBSTITUTIONS constant. If the baconian row is deleted from _converter_variants(), or a table code is wrong, this test and all of test_encoding.py still pass (checked with a mutation run: 33 passed). Selecting EncodingTechnique.Baconian would then build no attacks or wrong ciphertext without an error. Remove this file and add test_baconian_technique_builds_registered_row to test_encoding.py, as main does in test_disemvowel_technique_builds_registered_row: select the technique, build the attacks, take the converter from the baconian_raw attack, and assert a literal output, for example "Hello World" -> "aabbbaabaaababaababaabbab babaaabbabbaaaaababaaaabb".

result = await converter.convert_async(prompt="ab")
assert result.output_text == _BACONIAN_SUBSTITUTIONS["a"][0] + _BACONIAN_SUBSTITUTIONS["b"][0]
3 changes: 2 additions & 1 deletion tests/unit/scenario/garak/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,14 @@ def test_default_aggregate_curates_representative_subset(self):
}
all_names = {t.value for t in EncodingTechnique.get_all_techniques()}
assert default_names < all_names
assert len(all_names) == 19
assert len(all_names) == 20
# The niche/lossy schemes stay ALL-only.
assert all_names - default_names == {
"braille",
"ecoji",
"zalgo",
"ascii_smuggler",
"baconian",
"bifid",
"columnar_transposition",
}
Expand Down
Loading