Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: BufferTransformer and Blowfish Refactor #4151

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

reneme
Copy link
Collaborator

@reneme reneme commented Jun 26, 2024

This picks up the work started in #3942 by proposing a BufferTransformer class; which is essentially an orchestrator for BufferSlicer and BufferStuffer. As mentioned yesterday, I added some template fanciness, to handle the "block processing" necessary for various block ciphers. As suggested previously, this uses Blowfish to demonstrate the functionality (+ spanifying the blowfish implementation).

@randombit Is the process_blocks_of<> implementation in line with what you had in mind previously:

I wonder if we can fit in some other common functionality there, namely the word level loads/stores. That's a very common task (encrypt+decrypt for N ciphers plus alternative implementations)

Here's the idea of BufferTransformer::process_blocks_of, where the multi-block loop handling and buffer management is entirely abstracted. Also, as an additional goodie, we now have explicit compile-time knowledge of the blocked data length inside the processing lambdas. Whether the compiler uses this or not has to be seen. FWIW: I don't see a difference in the performance of Blowfish on my Mac.

void encrypt_n(const uint8_t inb[], uint8_t outb[], size_t blocks) {
   std::span<const uint8_t> in(inb, inb + blocks*BS);
   std::span<uint8_t> out(outb, outb + blocks*BS);
   
   BufferTransformer bt(in, out);
   bt.process_blocks_of<BS*4, BS>(
      overloaded{
         [](std::span<const uint8_t, BS*4> in, std::span<uint8_t, BS*4> out) {
            // encrypt four blocks at a time
         },
         [](std::span<const uint8_t, BS> in, std::span<uint8_t, BS> out) {
            // encrypt a single block
         }
      }
   );
}

No hard feelings if that doesn't make it into 3.5.0 at all. I just nerd-sniped myself yesterday and wanted to sketch this out. And frankly, I think the BufferTransformer implementation is quite concise. 😄

@reneme reneme added the enhancement Enhancement or new feature label Jun 26, 2024
@reneme reneme requested a review from randombit June 26, 2024 15:16
@reneme reneme force-pushed the rene/buffer_transformer branch 2 times, most recently from 4feb67d to 87d4035 Compare June 27, 2024 07:15
@reneme reneme mentioned this pull request Jun 27, 2024
@coveralls
Copy link

Coverage Status

coverage: 91.742% (+0.02%) from 91.72%
when pulling 7b3c762 on Rohde-Schwarz:rene/buffer_transformer
into 66216e1 on randombit:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement or new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants