Numbers & Points
At the base of both symmetric and asymmetric algorithms lie the concept of numbers, large enough to represent a key that can be used in various operations. Further, asymmetric systems based in elliptic curves rely on a way to represent the points on these curves. Both numbers and points have various mathematical operations that enable these algorithms, and this guide will provide an overview of the SDK's implementations of each.
Big Numbers
In JavaScript and TypeScript, natural numbers are limited to 53 bits of precision. This means that it's not possible to have numbers larger than 2 to the power of 53. However, the keys used in secure algorithms, such as those used in Bitcoin, are on the order of 256 bits in length. This means that we need a specialized representation of these large numbers, and the necessary mathematical operations on them.
The SDK's BigNumber
class provides this capability. Here's a simple example:
Here, we're creating two "big" numbers and adding them together. These numbers aren't actually very large, but they illustrate the point. We can use larger numbers, generating them randomly, and convert them into hex:
It's also possible to do many othe operations, like subtraction, division, and conversion into various formats:
Private keys and symmetric keys are just an extension of the BigNumber
class, enabling access to various specialized algorithms like digital signatures and encryption, respectively. When considering public keys, we need to go further and talk about elliptic curves and points.
Curves and Points
An elliptic curve has various properties and mathematical primitives. The curve used in Bitcoin is called secp256k1
, and the SDK provides an implementation in the Curve
class. In order to get started using a curve, you will need the generator point. The generator point can be multiplied with a private key in order to get a corresponding public key.
Let's start by importing the Curve
class, generating a private key (a random number), and multiplying it by the generator point (called G
) to get a point representing the corresponding public key:
Now, the public key can be shared with the world, and the private key remains secure. This is because the process of point multiplication is impractical to reverse (there is no "point division" or "dividing by the generator point").
Point Addition and Multiplication
Points are represented (wrapped) at a higher level by the SDK's PublicKey
class, which simply adds some extra helper functions. Conversely, the SDK wraps Big Numbers with the PrivateKey
class to extend their functionality.
When dealing with points, we can perform other useful operations. For example, the type 42 scheme talks about adding one point to another. We will demonstrate, and then we'll go further to show the multiplication of a point by a number, to show the process behind ECDH:
These lower-level constructs are useful when dealing with key derivation schemes, or when you need to perform specific mathematical operations on public, private or symmetric keys. Keep in mind that because public keys are an extension of Point
, and because private and symmetric keys extend BigNumber
, all of the low-level tools from these powerful base classes are always available for your use.
Last updated