Skip to content

std::span for Buffered_Computation#3681

Merged
reneme merged 2 commits intomasterfrom
span/bufcomp
Sep 11, 2023
Merged

std::span for Buffered_Computation#3681
reneme merged 2 commits intomasterfrom
span/bufcomp

Conversation

@reneme
Copy link
Copy Markdown
Collaborator

@reneme reneme commented Aug 26, 2023

Pull Request Dependencies

This interferes with the Keccak refactoring, KMAC and the work done in MDx_HashFunction. It shouldn't be too hard to fix those conflicts in any order, though.

Description

This is upgrading the internal virtual methods of Buffered_Computation (i.e. add_data() and final_result()) to use std::span<>. Also some of the implementations in the "hash" and "mac" modules benefit from BufferSlicer and BufferStuffer to abstract some of the pointer/offset mangling.

... courtesy of a long boring flight. :)

Future Work

  • make buffer_insert, copy_mem, xor_buf, and friends std::span-aware to further improve on the expressiveness of those methods
  • look into std::spanifying MDx_HashFunction (see also Remove MDx_HashFunction #3550)
  • Symmetric_Algorithm::key_schedule() (see std::span for SymmetricAlgorithm::key_schedule() #3684)
  • A bit of a long-shot: Maybe we could spike an abstraction over the recurring pattern of the buffer alignment code: i.e. "process data until alignment is reached, process data in buffer alignment, process remaining bytes causing disalignment"

@reneme reneme force-pushed the span/bufcomp branch 2 times, most recently from 2d21ffd to 4474ac2 Compare August 26, 2023 11:28
@coveralls
Copy link
Copy Markdown

coveralls commented Aug 26, 2023

Coverage Status

coverage: 91.71% (+0.003%) from 91.707% when pulling 23bdc9c on span/bufcomp into 88fbc40 on master.

@reneme reneme force-pushed the span/bufcomp branch 3 times, most recently from 5734789 to 23bdc9c Compare August 26, 2023 18:29
@randombit
Copy link
Copy Markdown
Owner

Maybe we could spike an abstraction over the recurring pattern of the buffer alignment code: i.e. "process data until alignment is reached, process data in buffer alignment, process remaining bytes causing disalignment"

That would be pretty nice. I've made several attempts over the years at this and never got anything that worked (ie was actually cleaner/easier to understand and did not introduce perf issues somwhere)

@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented Sep 4, 2023

I've made several attempts over the years at this and never got anything that worked

Just a pure gut feeling: it might be a good playground for C++20 coroutines.

@randombit
Copy link
Copy Markdown
Owner

I would be concerned about using coroutines wrt baremetal systems, I don't know what degree of runtime support this requires.

Copy link
Copy Markdown
Owner

@randombit randombit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems basically fine. I checked and it does not seem to introduce any performance regressions.

I wonder for final if we should be checking the length of the span. This would be a new check in that the current approach just writes to the provided pointer and if you don't provide enough data we'll overwrite something we shouldn't. And I guess we can continue that here; it just feels a little unfriendly to do so given that we do know the length of the output span.

@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented Sep 10, 2023

I wonder for final if we should be checking the length of the span.

I deliberately didn't add checks to the private virtual add_data() and final_result() implementations. They should all be invoked via the public update() and final() calls in Buffered_Computation. Those public wrappers have asserts for the lengths where possible. Or did I miss the check somewhere?

Maybe we could replace the asserts with BOTAN_ARG_CHECK, though.

@randombit
Copy link
Copy Markdown
Owner

They should all be invoked via the public update() and final() calls in Buffered_Computation. Those public wrappers have asserts for the lengths where possible.

Sorry I missed this. Looks fine then.

Maybe we could replace the asserts with BOTAN_ARG_CHECK, though.

Yeah concur on this. BOTAN_ASSERT should be only for conditions where we believe it could never fail but want to double check. But BOTAN_ASSERT has been around a lot longer than BOTAN_ARG_CHECK etc and so we have a number of cases where we are using assert to check user inputs.

@reneme reneme merged commit f906177 into master Sep 11, 2023
@reneme reneme deleted the span/bufcomp branch September 11, 2023 08:48
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