Repository: bitcoin/bitcoin
File: src/key.h (C/C++)

Code before:
{
public:
    KeyPair() noexcept = default;
    KeyPair(KeyPair&&) noexcept = default;
    KeyPair& operator=(KeyPair&&) noexcept = default;
    KeyPair& operator=(const KeyPair& other)
    {
        if (this != &other) {
            if (other.m_keypair) {
                MakeKeyPairData();
                *m_keypair = *other.m_keypair;
            } else {
                ClearKeyPairData();
            }
        }
        return *this;
    }

    KeyPair(const KeyPair& other) { *this = other; }

    friend KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const;
    [[nodiscard]] bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256& aux) const;

    /**
     * This is provided as a read-only method for passing a KeyPair object to secp256k1 functions
     * expecting a `secp256k1_keypair`. This avoids need to create a `secp256k1_keypair` object and
     * allows for passing the KeyPair directly in the following manner:
     *
     *     reinterpret_cast<const secp256k1_keypair*>(keypair.data())
     *
     * Recall that secp256k1_keypair is an opaque datatype, so this method should only be used
     * for passing a KeyPair object as a secp256k1_keypair and should never be used to access the
     * underlying keypair bytes directly.
     */
    const unsigned char* data() const { return m_keypair ? m_keypair->data() : nullptr; }

    //! Check whether this keypair is valid.
    bool IsValid() const { return !!m_keypair; }

private:
    KeyPair(const CKey& key, const uint256* merkle_root);

    using KeyType = std::array<unsigned char, 96>;
    secure_unique_ptr<KeyType> m_keypair;

    void MakeKeyPairData()
    {
        if (!m_keypair) m_keypair = make_secure_unique<KeyType>();
    }

    void ClearKeyPairData()

Review comment (suggestion):
```suggestion
     * expecting a `secp256k1_keypair`. This avoids the need to create a `secp256k1_keypair` object and
 ```

Code after:
{
public:
    KeyPair() noexcept = default;
    KeyPair(KeyPair&&) noexcept = default;
    KeyPair& operator=(KeyPair&&) noexcept = default;
    KeyPair& operator=(const KeyPair& other)
    {
        if (this != &other) {
            if (other.m_keypair) {
                MakeKeyPairData();
                *m_keypair = *other.m_keypair;
            } else {
                ClearKeyPairData();
            }
        }
        return *this;
    }

    KeyPair(const KeyPair& other) { *this = other; }

    friend KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const;
    [[nodiscard]] bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256& aux) const;

    //! Check whether this keypair is valid.
    bool IsValid() const { return !!m_keypair; }

private:
    KeyPair(const CKey& key, const uint256* merkle_root);

    using KeyType = std::array<unsigned char, 96>;
    secure_unique_ptr<KeyType> m_keypair;

    void MakeKeyPairData()
    {
        if (!m_keypair) m_keypair = make_secure_unique<KeyType>();
    }

    void ClearKeyPairData()
    {
        m_keypair.reset();
    }
};

/** Check that required EC support is available at runtime. */
bool ECC_InitSanityCheck();

/**
 * RAII class initializing and deinitializing global state for elliptic curve support.
 * Only one instance may be initialized at a time.
 *
 * In the future global ECC state could be removed, and this class could contain