Skip to content

Conversation

theStack
Copy link
Contributor

@theStack theStack commented Sep 5, 2023

In the course of reviewing BIP324 related PRs I noticed a frequent pattern of creating private keys (CKey instances) with data consumed from the fuzz data provider:

    auto key_data = provider.ConsumeBytes<unsigned char>(32);
    key_data.resize(32);
    CKey key;
    key.Set(key_data.begin(), key_data.end(), /*fCompressedIn=*/true);

This PR introduces a corresponding helper ConsumePrivateKey in order to deduplicate code. The compressed flag can either be set to a fixed value, or, if std::nullopt is passed (=default), is also consumed from the fuzz data provider via .ConsumeBool().

Note that this is not a pure refactor, as some of the replaced call-sites previously consumed a random length (ConsumeRandomLengthByteVector) instead of a fixed size of 32 bytes for key data. As far as I can see, there is not much value in using a random size, as in all those cases we can only proceed or do something useful with a valid private key, and key data with sizes other than 32 bytes always lead to invalid keys.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 5, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK sipa, brunoerg

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Sep 5, 2023
@theStack theStack force-pushed the 202309-fuzz-add_consumeprivkey_helper branch from 2d05cd2 to 583af18 Compare September 6, 2023 12:00
@brunoerg
Copy link
Contributor

brunoerg commented Sep 6, 2023

Concept ACK

@sipa
Copy link
Member

sipa commented Sep 6, 2023

utACK 583af18

Copy link
Contributor

@brunoerg brunoerg left a comment

Choose a reason for hiding this comment

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

crACK 583af18

@fanquake fanquake merged commit d98180a into bitcoin:master Sep 7, 2023
@theStack theStack deleted the 202309-fuzz-add_consumeprivkey_helper branch September 7, 2023 10:49
Frank-GER pushed a commit to syscoin/syscoin that referenced this pull request Sep 8, 2023
583af18 fuzz: introduce and use `ConsumePrivateKey` helper (Sebastian Falbesoner)

Pull request description:

  In the course of reviewing BIP324 related PRs I noticed a frequent pattern of creating private keys (`CKey` instances) with data consumed from the fuzz data provider:
  ```
      auto key_data = provider.ConsumeBytes<unsigned char>(32);
      key_data.resize(32);
      CKey key;
      key.Set(key_data.begin(), key_data.end(), /*fCompressedIn=*/true);
  ```
  This PR introduces a corresponding helper `ConsumePrivateKey` in order to deduplicate code. The compressed flag can either be set to a fixed value, or, if `std::nullopt` is passed (=default), is also consumed from the fuzz data provider via `.ConsumeBool()`.

  Note that this is not a pure refactor, as some of the replaced call-sites previously consumed a random length (`ConsumeRandomLengthByteVector`) instead of a fixed size of 32 bytes for key data. As far as I can see, there is not much value in using a random size, as in all those cases we can only proceed or do something useful with a valid private key, and key data with sizes other than 32 bytes always lead to invalid keys.

ACKs for top commit:
  sipa:
    utACK 583af18
  brunoerg:
    crACK 583af18

Tree-SHA512: 58a178432ba1eb0a2f7597b6700e96477e8b10f429ef642445a52db12e74d81aec307888315b772bfda9610f90df3e1d556cf024c2bef1d520303b12584feaaa
@bitcoin bitcoin locked and limited conversation to collaborators Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants