Skip to content

CRTP for MDx_Hash#3553

Closed
reneme wants to merge 2 commits intorandombit:jack/remove-mdx-hashfrom
reneme:rene/mdx_as_crtp
Closed

CRTP for MDx_Hash#3553
reneme wants to merge 2 commits intorandombit:jack/remove-mdx-hashfrom
reneme:rene/mdx_as_crtp

Conversation

@reneme
Copy link
Copy Markdown
Collaborator

@reneme reneme commented May 19, 2023

That's still somewhat sketchy, but I think it makes my point clear about having a CRTP-Adapter to move shared code (i.e. HashFunction interface implementation) into a central location. As I said: That's not a must at all. Albeit, I do believe it cleans things up quite nicely.

@reneme reneme force-pushed the rene/mdx_as_crtp branch from 15b6b50 to 6b29234 Compare May 19, 2023 16:01
@reneme reneme mentioned this pull request May 19, 2023
@reneme reneme force-pushed the rene/mdx_as_crtp branch from 6b29234 to 8d21aa5 Compare May 22, 2023 07:50
@reneme reneme force-pushed the rene/mdx_as_crtp branch from 8d21aa5 to 5daeaac Compare May 22, 2023 08:00
Comment on lines +139 to +158
template<typename DerivedT, mdx_hash_implementation HashImplT>
class MD_Hash_Adapter : public HashFunction
{
public:
std::string name() const override { return HashImplT::NAME; }
size_t output_length() const override { return HashImplT::FINAL_DIGEST_BYTES; }
size_t hash_block_size() const override { return HashImplT::BLOCK_BYTES; }

std::unique_ptr<HashFunction> new_object() const override { return std::make_unique<DerivedT>(); }
std::unique_ptr<HashFunction> copy_state() const override { return std::make_unique<DerivedT>(*dynamic_cast<const DerivedT*>(this)); }

void clear() override { m_md.clear(); }

private:
void add_data(const uint8_t input[], size_t length) override { m_md.add_data(input, length); }
void final_result(uint8_t output[]) override { m_md.final_result(output); }

private:
MD_Hash<HashImplT> m_md;
};
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Obviously, this is a departure from the idea that inheritance shouldn't be used for implementation but merely to communicate interface relationship.

However, I'd like to argue that this nicely separates the API-concerns of HashFunction from the implementation of MDx-hashes (::init(), compress_n() and their specific parameters). The implementations don't need to worry about the top-level interface at all (except for the provider() method when ISA-specific implementations are available).

Separating MD_Hash and MD_Hash_Adapter could even be reverted (as the latter is really just a wrapper, IMO). Though, also thinking about Keccak[c]: a similar separation would likely allow further code reuse via additional adapters such as KMAC or XOFs.

@randombit @falko-strenzke Is that something we could work with?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I fear I don't capture the whole design idea for MD_Hash from this sketch. In any case, I don't agree with the principle that inheritance should only be used for interfaces but not for implementation. If the latter doesn't bring the risk of an increased complexity and difficult maintenance but allows to reduce code duplication, it is fine in my view.

@reneme reneme changed the title [Sketch] CRTP for MDx_Hash CRTP for MDx_Hash May 22, 2023
@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented May 23, 2023

I fear I don't capture the whole design idea for MD_Hash from this sketch.

I tried capturing the design idea as a class diagram (click to enlarge).

image

@falko-strenzke
Copy link
Copy Markdown
Collaborator

To me this design makes sense and would probably be transferable to the Keccak-derived hash- and XOF functions.

@reneme reneme added this to the Botan 3.1.0 milestone Jun 20, 2023
@randombit randombit modified the milestones: Botan 3.1.0, Botan 3.2.0 Jul 11, 2023
@reneme
Copy link
Copy Markdown
Collaborator Author

reneme commented Aug 30, 2023

Now that we are going with the pure membership approach over an adapter base class, I'll close this. Let's try to move forward with the MDx and Keccak refactorings. :)

@reneme reneme closed this Aug 30, 2023
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