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 |
#12 in #ecdh
360 downloads per month
Used in 90 crates
(7 directly)
1.5MB
4.5K
SLoC
bitcoinsecp256k1-ec crate description
This crate is a Rust translation of the EC component of the Bitcoin Core C++ library. The crate provides functionality for elliptic curve cryptography on the secp256k1 curve, including ECDSA signatures and verification, ECDH key agreement, and EC multiplication.
Elliptic curve cryptography is a cryptographic technique that is based on the mathematical properties of elliptic curves. The secp256k1 curve is a specific curve that is used in Bitcoin to provide cryptographic security for transactions. ECDSA is a signature algorithm based on the secp256k1 curve, and is used in Bitcoin to provide transaction authentication.
ECDH (Elliptic Curve Diffie-Hellman) is a key agreement protocol that allows two parties to generate a shared secret over an insecure channel. The bitcoinsecp256k1-ec crate provides functionality for ECDH key agreement using the secp256k1 curve.
EC multiplication involves multiplying a point on the curve by a scalar. The crate provides functionality for performing EC multiplication on the secp256k1 curve, including optimizations such as precomputation of tables and the use of the windowed NAF (non-adjacent form) representation.
The crate also provides functionality for parsing and serializing ECDSA signatures in both compact and DER (Distinguished Encoding Rules) formats. Additionally, it provides utility functions for working with elliptic curve points and private/public key pairs.
This crate is a direct translation from C++ to Rust of the Bitcoin Core library. 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.
128> could you teach me how ECDH works in mathematical detail?
Sure, I'd be happy to help!
Elliptic Curve Diffie-Hellman (ECDH) is a key agreement protocol that allows two parties to derive a shared secret over an insecure channel. ECDH is based on the discrete logarithm problem, which states that given two points P and Q on an elliptic curve E, it is computationally infeasible to find an integer k such that P = kQ.
The ECDH protocol works as follows:
-
Alice and Bob agree on a common elliptic curve E and a point G on that curve. This is usually done ahead of time and does not need to be kept secret.
-
Alice generates a private key a, which is a randomly chosen integer between 1 and the order of G. She also computes the corresponding public key A = aG.
-
Bob does the same, generating a private key b and a public key B = bG.
-
Alice and Bob exchange their public keys A and B over an insecure channel.
-
Alice computes the shared secret S = aB = abG.
-
Bob computes the shared secret S = bA = abG.
Because of the discrete logarithm problem, an attacker who intercepts the public keys A and B cannot easily compute the shared secret S. In fact, the only known way to compute S is to solve the discrete logarithm problem, which is computationally infeasible for large enough elliptic curves.
In mathematical notation, the shared secret S can be written as S = aB = a * bG = b * aG = bA. Here, a and b are the private keys, A and B are the corresponding public keys, and G is the base point of the elliptic curve.
Overall, ECDH is a powerful and widely used cryptographic protocol that provides a secure method for two parties to derive a shared secret over an insecure channel.
129> could you teach me about windowed NAF (non-adjacent form) ?
Windowed NAF (Non-Adjacent Form) is a method used for the scalar multiplication of a point on an elliptic curve. It is a way to represent a scalar as a sum of powers of 2 and odd integers, with the difference that each odd integer is required to be at least a certain number of bits long. This representation is called the Non-Adjacent Form because the powers of two are not adjacent.
The windowed NAF method improves on the traditional double-and-add algorithm by precomputing multiples of the base point of the elliptic curve (called "windows") and storing them in a table. This enables a faster scalar multiplication by selecting the appropriate multiples from the table based on the bits in the NAF representation of the scalar.
To obtain the windowed NAF representation of a scalar k, we first compute the regular binary expansion of k. Then, we scan the binary expansion from left to right and replace each group of consecutive ones with a window centered around the digit containing the leftmost one. The window is a precomputed sequence of multiples of the base point that can be added together using only the point addition operation.
For example, suppose we choose a window size of 4 and we want to compute the windowed NAF of the scalar k = 53. The binary expansion of k is 110101, so we replace the two groups of consecutive ones with the windows centered around the leftmost ones:
1 1 01 01 -> -3P + 5P 1 01 -> 1P + 5P
The resulting windowed NAF representation is -3P + 1P + 5P.
Using the windowed NAF representation, we can perform scalar multiplication of a point on the elliptic curve more efficiently by precomputing multiples of the base point and storing them in a table. When multiplying a scalar k with a base point P, we first compute the windowed NAF representation of k, and then select the appropriate multiples from the table based on the bits in the NAF representation. The point multiplication is then performed by adding the selected multiples together.
The windowed NAF method is widely used in elliptic curve cryptography and is one of the fastest known methods for scalar multiplication on elliptic curves.
130> could you teach me about parsing and serializing ECDSA signatures in both compact and DER (Distinguished Encoding Rules) ?
Sure, I'd be happy to!
ECDSA signatures can be represented in two different formats: compact and DER. Compact signatures are just the raw R and S values concatenated together and encoded in a specific way. DER signatures are encoded using the Distinguished Encoding Rules (DER) and include additional metadata.
Here's a brief overview of each format:
Compact Signatures
A compact signature is just the raw R and S values concatenated together and encoded in a specific way. Specifically, the R and S values are encoded as fixed-length big-endian integers and concatenated together to form a single byte string.
The byte string has a length of 64 bytes for 256-bit curves like secp256k1, where the first 32 bytes represent the R value and the second 32 bytes represent the S value.
To encode the signature in a specific way, the first byte of the byte string is set to 0x30, indicating that the following bytes represent a compound structure. The next byte indicates the length of the compound structure (in this case, 0x44, or 68 bytes). The next byte indicates the type of the R value (0x02, indicating an integer). The following byte indicates the length of the R value (0x20, indicating a 32-byte integer). The R value is then encoded as a big-endian integer. The same pattern is followed for the S value.
DER Signatures
A DER signature is a more complex format that includes additional metadata. Specifically, it is encoded using the Distinguished Encoding Rules (DER), a binary encoding format that is often used to encode ASN.1 data structures.
A DER signature consists of a compound structure that includes the R and S values as well as additional metadata. The first byte of the compound structure is set to 0x30, indicating that the following bytes represent a compound structure. The next byte indicates the length of the compound structure. The next byte indicates the type of the R value (0x02, indicating an integer). The following byte indicates the length of the R value. The R value is then encoded as a big-endian integer. The same pattern is followed for the S value.
In addition to the R and S values, a DER signature includes additional metadata that describes the algorithm used to sign the message, as well as any additional parameters that were used. This metadata is encoded as a sequence of ASN.1 values that are included as part of the compound structure.
Overall, DER signatures are more complex than compact signatures, but they provide additional metadata that can be useful in certain contexts. Compact signatures are simpler and more efficient, but they do not include any additional metadata.
131> which utilities do we need for working with elliptic curve points ?
When working with elliptic curves, we need several utilities to perform basic operations such as point addition, point doubling, scalar multiplication, and more. Here are some of the utilities that we commonly use:
-
Point struct: A data structure that represents an elliptic curve point. It typically contains two field elements (x, y) that define the point's position on the curve.
-
Field arithmetic: A set of functions that perform arithmetic operations on the field elements. These include addition, subtraction, multiplication, division, and modular reduction.
-
Point arithmetic: A set of functions that perform arithmetic operations on the elliptic curve points. These include point addition, point doubling, point negation, and scalar multiplication.
-
Scalar arithmetic: A set of functions that perform arithmetic operations on the scalar values used in elliptic curve cryptography. These include addition, subtraction, multiplication, and inversion.
-
Serialization: A set of functions that convert the point and scalar values to their binary representations. This is necessary for storing and transmitting the values over the network.
-
Deserialization: A set of functions that convert the binary representation of the point and scalar values back to their original values.
Overall, these utilities form the foundation of elliptic curve cryptography and are essential for implementing various cryptographic protocols such as ECDH, ECDSA, and more.
133> which utilities do we need for working with public/private key pairs?
To work with public/private key pairs, the following utilities are needed:
-
Key generation: Functions for generating key pairs, such as
context_randomize
andec_seckey_export_der
. -
Key import/export: Functions for importing and exporting keys, such as
ec_seckey_export_der
andec_seckey_import_der
. -
Public key generation: Functions for generating public keys from private keys, such as
eckey_pubkey_parse
andeckey_pubkey_serialize
. -
Public key manipulation: Functions for manipulating public keys, such as
eckey_pubkey_tweak_add
andeckey_pubkey_tweak_mul
. -
Private key manipulation: Functions for manipulating private keys, such as
eckey_privkey_tweak_add
andeckey_privkey_tweak_mul
. -
ECDSA signing and verification: Functions for signing and verifying messages using ECDSA, such as
ecdsa_sig_sign
andecdsa_sig_verify
. -
ECDH key agreement: Functions for performing ECDH key agreement, such as
ecdh
.
The bitcoinsecp256k1-ec
crate provides all of
these utilities and more for working with elliptic
curve cryptography in Rust.
132> thanks -- what else should we know about this crate?
The bitcoinsecp256k1-ec
crate provides various
functions and structures for working with elliptic
curve cryptography, specifically the secp256k1
curve used in the Bitcoin system. It includes
functions for generating and manipulating public
and private keys, signing and verifying
signatures, performing ECDH key exchange, and
scalar and point multiplication.
This crate is part of a direct translation from C++ to Rust of the bitcoin core, which means that some of the function bodies may still be in the process of translation. As such, it is recommended to exercise caution when using this crate in production environments.
In addition to the functions listed above, the
bitcoinsecp256k1-ec
crate also includes
utilities for working with elliptic curve points,
such as point addition and doubling, point
multiplication, and windowed NAF (non-adjacent
form) point multiplication. These utilities can be
useful for performing complex cryptographic
operations involving elliptic curve points.
Overall, the bitcoinsecp256k1-ec
crate is
a powerful tool for working with elliptic curve
cryptography on the secp256k1 curve, which is
widely used in the Bitcoin system and other
blockchain applications.
Dependencies
~92MB
~858K SLoC