bee-san / Ares

Automated decoding of encrypted text without knowing the key or ciphers used
MIT License
564 stars 25 forks source link

[BUG] [Tech Debt] Caesar cipher is a bit slow #111

Open bee-san opened 1 year ago

bee-san commented 1 year ago

814 decoding / 5 second for everything apart from Caesar (it can get it too)

222 decodings / 5 seconds with caesar (doesnt get it)

Input: https://gchq.github.io/CyberChef/#recipe=Label(%27%27)To_Base64(%27A-Za-z0-9%2B/%3D%27)Jump(%27%27,20)&input=SGVsbG8

I believe this is because Caesar uses .clone() here:

decoded_strings.push(decoded_text.clone());

Just a guess, I am not sure! We should use lifetimes here :)

bee-san commented 1 year ago

It's not super slow:

hahahahahahahahahahahah i foudn the problem ares is not faster or slower with caesar significantly Without caesar:

Ares has decoded 144 times SUCCESSFUL ๐Ÿ˜ PLAINTEXT: ["hello"] DECODERS USED: base64_url โ†’ base64_url โ†’ base64_url โ†’ base64_url โ†’ base64 โ†’ base64 โ†’ base64 โ†’ base64_url โ†’ base64_url โ†’ base64_url โ†’ base64

With caesar:

Ares has decoded 180 times [2022-11-29T16:32:51Z ERROR ares::searchers::bfs] Ares failed to decrypt the text you have provided within 5 seconds, it is unlikely to be decoded. FAILED ๐Ÿ˜ญ

The numbers being wrong were because they are hard-coded and I forgot to remove them when removing caesar, inflating the non-caesar number it is ok ares cant get it within 5 seconds with caesar, caesar generates 24 more permutations of each text to be decoded 20 seconds and it still can't get it: Ares has decoded 216 times [2022-11-29T16:36:09Z ERROR ares::searchers::bfs] Ares failed to decrypt the text you have provided within 20 seconds, it is unlikely to be decoded. FAILED ๐Ÿ˜ญ

My suggestion of basic base decoders first must be a good idea round about now, but not a priority ๐Ÿ˜„ likewise if we want to increase speed, we should look at checkers (particularly regex) With lemmeknow:

216 decodings

Without:

396 decodings

And likewise we should tell filter_and_get_decoders to focus on Base decoders first etc, per the proposal in Notion ๐Ÿ™‚ โค๏ธ

bee-san commented 1 year ago

From Chat GPT:

One way to make this function faster is to use the built-in char::shift_right() method to shift the characters by the specified amount, instead of manually calculating the shift using modulo and addition. This eliminates the need for the conditional check for ASCII alphabetic characters, and reduces the number of operations performed.

The updated function would look like this:

fn caesar(cipher: &str, shift: u8) -> String {
    cipher
        .chars()
        .map(|c| c.shift_right(shift as u32))
        .collect()
}

shift_right() is just >>

fn caesar(cipher: &str, shift: u8) -> String {
    cipher
        .chars()
        .map(|c| ((c as u8) >> shift) as char)
        .collect()
}

We measured it and got a 50% speed improvement:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running benches\benchmark_crackers.rs (target\release\deps\benchmark_crackers-95d856c18cfc82b7.exe)
caesar successful decoding
                        time:   [34.822 ยตs 35.051 ยตs 35.279 ยตs]
                        change: [-50.079% -48.573% -47.047%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 21 outliers among 100 measurements (21.00%)
  6 (6.00%) low mild
  7 (7.00%) high mild
  8 (8.00%) high severe

However it fails when it cannot shift right and panics, we'd like it not to panic and to pass our tests <3

bee-san commented 1 year ago

https://discord.com/channels/754001738184392704/902967642158141520/1048539781249892352