Skip to content

Conversation

@robin-nitrokey
Copy link
Member

This patch combines the operation traits that were previously used to call mechanism implementations into a single MechanismImpl trait. This has several advantages:

  • We can use a macro to implement the dispatch from the Mechanism enum, removing boilerplate code from the reply_to implementation.
  • To implement an operation for a mechanism, it is now sufficient to override the respective trait method. It is no longer necessary to also update reply_to.
  • The need to annotate all mechanism methods with #[inline(never)] to avoid producing a huge reply_to function (see the comment in mechanisms.rs) is reduced as we can just mark the methods generated by the macro as #[inline(never)].
  • This reduces the binary size required in the stable nitrokey-3-firmware by some kB.

@sosthene-nitrokey
Copy link
Contributor

I like that

@robin-nitrokey robin-nitrokey requested a review from daringer March 4, 2025 11:32
@robin-nitrokey robin-nitrokey marked this pull request as ready for review March 4, 2025 11:32
@robin-nitrokey
Copy link
Member Author

Rebased onto main and updated so that we now only define a single list of implemented mechanisms.

@robin-nitrokey
Copy link
Member Author

@sosthene-nitrokey @daringer Can you please review this so that we can include it in the next release?

This patch combines the operation traits that were previously used to
call mechanism implementations into a single MechanismImpl trait.  This
has several advantages:
- We can use a macro to implement the dispatch from the Mechanism enum,
  removing boilerplate code from the reply_to implementation.
- To implement an operation for a mechanism, it is now sufficient to
  override the respective trait method.  It is no longer necessary to
  also update reply_to.
- The need to annotate all mechanism methods with #[inline(never)] to
  avoid producing a huge reply_to function (see the comment in
  mechanisms.rs) is reduced as we can just mark the methods generated by
  the macro as #[inline(never)].
- This reduces the binary size required in the stable
  nitrokey-3-firmware by some kB.
This patch removes the manual mechanism enumeration in
IMPLEMENTED_MECHANISMS and changes the rpc_trait! macro to also generate
this constant so that it acts as a single source of truth.  As a side
effect, the IMPLEMENTED_MECHANISMS constant is moved from types to
service and only available if the crypto-client feature is enabled.
This patch makes the mechanisms module private.  The mechanism
implementation can still be accessed using the Mechanism enum.  This
leads to a clenaer public API and triggers a compiler warning if the
mapping between the Mechanism enum and the mechanism implementations is
wrong (as the implementation would then be unused).
@sosthene-nitrokey
Copy link
Contributor

sosthene-nitrokey commented Mar 10, 2025

I don't really like paste, the alternative that I tend to use is to have the same name written multiple times for all the versions. It's a bit more noisy but more readable, and I find the less dependencies the better.

Not a blocker.

The paste dependency is unmaintained and not really necessary for our
use case.
@robin-nitrokey
Copy link
Member Author

Unfortunately paste has been marked as unmaintained since I created the first version of this PR, so using it would probably lead to problems sooner or later anyway. I’ve updated the PR to remove the dependency.

@robin-nitrokey robin-nitrokey merged commit 7aea23b into trussed-dev:main Mar 10, 2025
2 checks passed
@robin-nitrokey robin-nitrokey deleted the mechanisms branch March 10, 2025 13:53
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.

2 participants