Skip to content

Conversation

@tarcieri
Copy link
Member

Take two for #2092

Adds a trait providing fallible key initialization, similar to the existing KeyInit trait, but designed to handle the case that not all bytestrings of a given length represent valid keys.

This is primarily useful in the context of public-key cryptography, e.g. scalars representing elliptic curve private keys.

The API and method names are duplicated from KeyInit. It is assumed that KeyInit and TryKeyInit have an either-or relationship, i.e. types will not impl both KeyInit and TryKeyInit, and consumers of code which is generic over these traits will not be attempting to abstract over the KeyInit/TryKeyInit distinction, but one or the other will make sense in a given context (e.g. symmetric cryptography uses KeyInit, ECC uses TryKeyInit)

Adds a trait providing fallible key initialization, similar to the
existing `KeyInit` trait, but designed to handle the case that not all
bytestrings of a given length represent valid keys.

This is primarily useful in the context of public-key cryptography, e.g.
scalars representing elliptic curve private keys.

The API and method names are duplicated from `KeyInit`. It is assumed
that `KeyInit` and `TryKeyInit` have an either-or relationship, i.e.
types will not impl both `KeyInit` and `TryKeyInit`, and consumers of
code which is generic over these traits will not be attempting to
abstract over the `KeyInit`/`TryKeyInit` distinction, but one or the
other will make sense in a given context (e.g. symmetric cryptography
uses `KeyInit`, ECC uses `TryKeyInit`)
@tarcieri tarcieri requested a review from newpavlov November 23, 2025 22:50
Copy link
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

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

It may be worth to add a note about random key generation and that we do not recommend to use naive rejection sampling in generic code. Instead users should prefer using implementation of the Generate trait on Self.

@tarcieri
Copy link
Member Author

The KeyInit and TryKeyInit APIs are now both completely free of key generation functionality. I think such comments belong on the relevant key generation APIs instead.

@tarcieri tarcieri merged commit a0819e2 into master Nov 26, 2025
11 checks passed
@tarcieri tarcieri deleted the crypto-common/trykeyinit-trait branch November 26, 2025 03:14
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