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

crypto, refactor: add new KeyPair class #30051

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Commits on Aug 3, 2024

  1. bench: add benchmark for signing with a taptweak

    Add benchmarks for signing with null and non-null merkle_root arguments.
    Null and non-null merkle_root arguments will apply the taptweaks
    H_TapTweak(P) and H_TapTweak(P | merkle_root), respectively, to the
    private key during signing.
    
    This benchmark is added to verify there are no significant performance
    changes after moving the taptweak signing logic in a later commit.
    
    Co-authored-by: l0rinc <pap.lorinc@gmail.com>
    josibake and paplorinc committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    f14900b View commit details
    Browse the repository at this point in the history
  2. tests: add key tweak smoke test

    Sanity check that using CKey/CPubKey directly vs using secp256k1_keypair objects
    returns the same results for BIP341 key tweaking.
    
    Co-authored-by: l0rinc <pap.lorinc@gmail.com>
    josibake and paplorinc committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    5d507a0 View commit details
    Browse the repository at this point in the history
  3. crypto: add KeyPair wrapper class

    Add a `KeyPair` class which wraps the `secp256k1_keypair`. This keeps
    the secret data in secure memory and enables passing the
    `KeyPair` object directly to libsecp256k1 functions expecting a
    `secp256k1_keypair`.
    
    Motivation: when passing `CKeys` for taproot outputs to libsecp256k1 functions,
    the first step is to create a `secp256k1_keypair` data type and use that
    instead. This is so the libsecp256k1 function can determine if the key
    needs to be negated, e.g., when signing.
    
    This is a bit clunky in that it creates an extra step when using a `CKey`
    for a taproot output and also involves copying the secret data into a
    temporary object, which the caller must then take care to cleanse. In
    addition, the logic for applying the merkle_root tweak currently
    only exists in the `SignSchnorr` function.
    
    In a later commit, we will add the merkle_root tweaking logic to this
    function, which will make the merkle_root logic reusable outside of
    signing by using the `KeyPair` class directly.
    
    Co-authored-by: Cory Fields <cory-nospam-@coryfields.com>
    josibake and theuni committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    c39fd39 View commit details
    Browse the repository at this point in the history
  4. refactor: use KeyPair in SignSchnorr

    Use `KeyPair` instead of creating a `secp256k1_keypair` object. The
    main change here is creating a `KeyPair` instead of a
    `secp256k1_keypair` and then passing it to the libsec256k1 functions
    using `reinterpret_cast<secp256k1_keypair*>(keypair)`.
    
    The variable name `keypair` is used for the reinterpret_cast to simplify the
    diff in a later commit when all of the logic in SignSchnorr is moved into the
    KeyPair class.
    
    Note: we no longer need to call memory_cleanse since `KeyPair` is now
    using a secure allocator (same as CKey). See src/support/allocator/secure.h
    josibake committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    4cb66de View commit details
    Browse the repository at this point in the history
  5. refactor: move SignSchnorr logic to KeyPair

    Move `SignSchnorr` to `KeyPair`. This makes `CKey::SignSchnorr` now
    compute a `KeyPair` object and then call `KeyPair::SignSchorr`. The
    signing logic is move-only with the exception of changing
    `keypair.data()` to `my_keypair->data()`, since we now have access to
    the private member `m_keypair`.
    josibake committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    6ee338e View commit details
    Browse the repository at this point in the history
  6. tests: add tests for KeyPair

    Reuse existing BIP340 tests, as there should be
    no behavior change between the two
    josibake committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    d2d4d21 View commit details
    Browse the repository at this point in the history
  7. refactor: remove un-tested early returns

    Replace early returns in KeyPair::KeyPair() with asserts.
    
    The if statements imply there is an error we are handling, but keypair_xonly_pub
    and xonly_pubkey_serialize can only fail if the keypair object is malformed, i.e.,
    it was created with a bad secret key. Since we check that the keypair was created
    successfully before attempting to extract the public key, using asserts more
    accurately documents what we expect here and removes untested branches from the code.
    josibake committed Aug 3, 2024
    Configuration menu
    Copy the full SHA
    bfb2e6b View commit details
    Browse the repository at this point in the history