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

Use constant size generic parameter for random bytes generation #2910

Merged
merged 2 commits into from
Nov 27, 2022

Conversation

samueltardieu
Copy link
Contributor

All uses of get_random() were in the form of:

&get_random(vec![0u8; SIZE])

with SIZE being a constant.

Building a Vec is unnecessary for two reasons. First, it uses a very short-lived dynamic memory allocation. Second, a Vec is a resizable object, which is useless in those context when random data have a fixed size and will only be read.

get_random_bytes() takes a constant as a generic parameter and returns an array with the requested number of random bytes.

Stack safety analysis: the random bytes will be allocated on the caller stack for a very short time (until the encoding function has been called on the data). In some cases, the random bytes take less room than the Vec did (a Vec is 24 bytes on a 64 bit computer). The maximum used size is 180 bytes, which makes it for 0.008% of the default stack size for a Rust thread (2MiB), so this is a non-issue.

Also, most of the uses of those random bytes are to encode them using an Encoding. The function crypto::encode_random_bytes() generates random bytes and encode them with the provided Encoding, leading to code deduplication.

generate_id() has also been converted to use a constant generic parameter as well since the length of the requested String is always a constant.

@samueltardieu samueltardieu force-pushed the const-crypto branch 2 times, most recently from 05c10ac to 12194b3 Compare November 11, 2022 10:58
All uses of `get_random()` were in the form of:

  `&get_random(vec![0u8; SIZE])`

with `SIZE` being a constant.

Building a `Vec` is unnecessary for two reasons. First, it uses a
very short-lived dynamic memory allocation. Second, a `Vec` is a
resizable object, which is useless in those context when random
data have a fixed size and will only be read.

`get_random_bytes()` takes a constant as a generic parameter and
returns an array with the requested number of random bytes.

Stack safety analysis: the random bytes will be allocated on the
caller stack for a very short time (until the encoding function has
been called on the data). In some cases, the random bytes take
less room than the `Vec` did (a `Vec` is 24 bytes on a 64 bit
computer). The maximum used size is 180 bytes, which makes it
for 0.008% of the default stack size for a Rust thread (2MiB),
so this is a non-issue.

Also, most of the uses of those random bytes are to encode them
using an `Encoding`. The function `crypto::encode_random_bytes()`
generates random bytes and encode them with the provided
`Encoding`, leading to code deduplication.

`generate_id()` has also been converted to use a constant generic
parameter as well since the length of the requested String is always
a constant.
src/crypto.rs Outdated Show resolved Hide resolved
Its uses are replaced by get_randm_bytes() or encode_random_bytes().
@dani-garcia dani-garcia merged commit dfe1e30 into dani-garcia:main Nov 27, 2022
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.

4 participants