#public-key #key-pair #elliptic-curve #key-derivation #bitcoin #system

bitcoinsecp256k1-keys

structures to represent elliptic curve keys

5 releases

0.1.16-alpha.0 Mar 31, 2023
0.1.14-alpha.0 Mar 18, 2023
0.1.13-alpha.0 Mar 17, 2023
0.1.12-alpha.0 Jan 19, 2023
0.1.10-alpha.0 Jan 18, 2023

#49 in #key-derivation

Download history 187/week @ 2023-12-13 225/week @ 2023-12-20 89/week @ 2023-12-27 121/week @ 2024-01-03 161/week @ 2024-01-10 188/week @ 2024-01-17 68/week @ 2024-01-24 62/week @ 2024-01-31 97/week @ 2024-02-07 174/week @ 2024-02-14 141/week @ 2024-02-21 170/week @ 2024-02-28 127/week @ 2024-03-06 128/week @ 2024-03-13 236/week @ 2024-03-20 273/week @ 2024-03-27

804 downloads per month
Used in 85 crates (5 directly)

MIT license

1.5MB
4K SLoC

bitcoinsecp256k1-keys

This Rust crate is a component of the Bitcoin system and provides functionality for working with public-private key pairs and X-only public keys on the secp256k1 elliptic curve.

This crate is part of a direct translation from C++ to Rust of the Bitcoin Core. As such, some of the function bodies may still be in the process of translation. Please note that this system will become testable once the translation process is complete.

Mathematical ideas

The functions provided in this crate involve operations on the secp256k1 elliptic curve, which is defined by the equation:

y^2 = x^3 + 7 (mod p)

where p is a large prime number. The following mathematical concepts are relevant to the functionality provided by this crate:

  • Key pair generation and storage

  • Public key generation and storage

  • X-only public key generation, comparison, and tweaking

Key components

The tokens in this crate include:

  • KeyPair: A struct representing a secp256k1 public-private key pair, with functions for creating, loading, saving, and accessing the public and private key components.

  • XOnlyPubKey: A struct representing a secp256k1 X-only public key, with functions for generating, loading, parsing, saving, comparing, and tweaking keys.

  • api_test_context: A context for running tests on the functionality provided by this crate.

  • extrakeys_ge_even_y: An array of precomputed secp256k1 points with even y-coordinates, used for testing and generating key pairs.

  • Various test functions for verifying the correctness of the key pair and X-only public key functionality provided by this crate.

Usage

This crate provides functionality for generating and working with secp256k1 key pairs and X-only public keys, which are essential components of the Bitcoin system. The KeyPair struct can be used to generate, store, and access public-private key pairs, while the XOnlyPubKey struct can be used to generate, store, and modify X-only public keys.

It's important to note that this crate is still in the process of being translated from C++ to Rust, and as such some function bodies may not be fully implemented.

159> what is an X-only public key? could you please describe this construct in detail?

An X-only public key is a compressed representation of an elliptic curve public key that only includes the x-coordinate of the point on the curve. This reduces the size of the public key from 64 bytes to 32 bytes, making it more efficient to use in certain contexts such as in the blockchain space, where storage and transaction sizes are important considerations.

To understand how an X-only public key works, it's important to first understand how public keys are typically represented in elliptic curve cryptography. In most cases, a public key consists of two coordinates, x and y, that identify a point on the elliptic curve. However, for certain curves, including the secp256k1 curve used in Bitcoin, the y-coordinate can be derived from the x-coordinate and some other information. This allows for a compressed representation of the public key that only includes the x-coordinate.

The x-only public key is formed by taking the x-coordinate of the elliptic curve point and applying a bit operation to determine the sign of the y-coordinate. This sign bit is then appended to the x-coordinate, resulting in a 33-byte X-only public key. This compressed representation can be used in place of the full public key in certain cryptographic operations, such as signature verification.

It's worth noting that while X-only public keys are more efficient in terms of space, they do come with some limitations. In particular, they can only be used for certain cryptographic operations and cannot be used to derive a shared secret in ECDH (Elliptic Curve Diffie-Hellman) key exchange. Additionally, due to the compressed representation, it's important to ensure that the y-coordinate can be correctly reconstructed during cryptographic operations. This requires some additional verification steps that are not required for full public keys.

160> what else should we know about this crate?

The bitcoinsecp256k1-keys crate provides functions for working with public and private keys, as well as key pairs and X-only public keys in the context of the secp256k1 elliptic curve.

One important concept to keep in mind when working with keys is key derivation, which is the process of generating new keys from an existing one. This can be done by applying a deterministic function to the existing key, such as a hash function, which produces a new key that is cryptographically related to the original one. This is useful in situations where a new key is needed for each transaction or for each use case in general.

Another important concept is key management, which involves securely storing and protecting keys from unauthorized access. This can be done through various techniques such as using hardware wallets or by following best practices for secure key storage.

Overall, the bitcoinsecp256k1-keys crate provides useful functions for key generation, management, and derivation in the context of secp256k1 elliptic curve cryptography, which is widely used in Bitcoin and other cryptocurrencies.

161> could you teach us about key derivation?

Yes, I'd be happy to explain key derivation!

In cryptography, key derivation refers to the process of deriving one or more cryptographic keys from a shared secret or other input material. The derived keys can then be used for various cryptographic purposes, such as encryption, decryption, message authentication, and digital signature generation or verification.

There are many different key derivation algorithms and protocols that can be used, depending on the specific requirements of the application. One of the most commonly used key derivation functions is the Key Derivation Function 2 (KDF2), which is based on the Hashed Message Authentication Code (HMAC) construction.

The KDF2 takes a shared secret or other input material, along with an optional salt and other parameters, and outputs one or more derived keys of a specified length. The algorithm is designed to be secure against various attacks, including brute force attacks and dictionary attacks.

Other key derivation algorithms and protocols include the Password-Based Key Derivation Function 2 (PBKDF2), which is specifically designed for deriving keys from passwords, and the HKDF (HMAC-based Key Derivation Function), which is a more flexible and secure key derivation algorithm that can be used in a wide range of applications.

In the context of bitcoin, key derivation is used extensively in the process of generating and managing bitcoin addresses and private keys. For example, the hierarchical deterministic (HD) wallet protocol is based on a key derivation algorithm that allows a master seed to be used to derive a virtually unlimited number of child keys, each of which can be used to generate a unique bitcoin address.

Overall, key derivation is an important concept in cryptography that is used in a wide range of applications, including bitcoin and other cryptocurrencies. By deriving cryptographic keys from shared secrets or other input material, key derivation algorithms enable secure and efficient communication and information exchange.

Dependencies

~90MB
~809K SLoC