# Bitcoin Key Mechanism and Elliptic Curves Over Finite Fields

# Bitcoin Key Mechanism and Elliptic Curves Over Finite Fields

Want to learn more about using Bitcoin's key mechanism? Check out this post to learn more about using elliptic curve cryptography over finite fields.

Join the DZone community and get the full member experience.

Join For Free**Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.**

Bitcoin uses the Elliptic Curve Digital Signature Algorithm (**ECDSA**) based on elliptic curve cryptography. The particular elliptic curve is known as **secp256k1**, which is the curve:

*y*² = *x*³ + 7

This occurs over a finite field that we will describe shortly.

The addition on elliptic curves in the plane is defined geometrically in terms of where lines intercept the curve. We won’t go into the geometry here, except to say that it boils down to a set of equations involving real numbers. But, we’re not working over real numbers; we’re working over a finite field.

## Finite Field Modulus

The idea is to take the equations motivated by the geometry in the plane and then use those equations to define addition when you’re not working over real numbers but over a different field. In the case of secp256k1, the field is the finite field of integers mod, *p,* where:

*p* = 2^{256} – 2^{32} – 977

Here, *p* was chosen to be relatively close to 2^{256}. It’s not the largest prime less than 2^{256}; there are a lot of primes between *p* and 2^{256}. Other factors also went into the choice of *p*. Note that we’re not working in the integers mod *p* per se; we’re working in an Abelian group whose addition law is defined by an elliptic curve over the integers mod *p*.

## Base Point

Next, we pick a base point, *g, *on the elliptic curve. The standard defining secp256k1 says that *g* is

0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798

in “compressed form” or

040x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

in “uncompressed form.”

The base point is a specially chosen point on the elliptic curve, and so it is a *pair* of numbers mod *p*, not a single number. How do you extract *x* and *y* from these compressed or uncompressed forms? Let's take a look!

### Compressed Form

The compressed form only gives *x* and you’re supposed to solve for *y*. The uncompressed form gives you *x* and *y*. However, the numbers are slightly encoded. In the compressed form, the string either starts with “o2” or “o3,” and the rest of the string is the hexadecimal representation of *x*. There will be two values of *y,* satisfying:

*y*² = *x*³ + 7 mod *p*

And, the “o2” or “03” tells you which one to pick. If the compressed form starts with 02, pick the root whose least significant bit is even. And, if the compressed form starts with 03, pick the root whose least significant bit is odd. (The two roots will add to *p*, and *p* is odd, so one of the roots will be even and one will be odd.)

### Uncompressed Form

The uncompressed form will always start with 04. After this, follow the hex representations of *x* and *y* concatenated together.

In either case, we have:

x = 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798

and

y = 483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8

We can verify this with a little Python code:

```
x = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
y = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
assert((y*y - x*x*x - 7) % p == 0)
```

## Exponentiation Over an Elliptic Curve

Starting with our base point *g*, define *kg* to be *g* added to itself *k* times. Note again that the sense of “addition” here is an addition in the elliptic curve, not an addition in the field of integers mod *p*. **The key to elliptic curve cryptography** is that *kg* can be computed efficiently, but solving for *k* starting from the product *kg* cannot. You can compute *kg* using the fast exponentiation algorithm, but solving for *k* requires computing discrete logarithms. (This is the **ECDLP**: Elliptic Curve Discrete Logarithm Problem).

Why is this called “exponentiation” and not “multiplication?" Arithmetic on the elliptic curve is commutative, and in commutative (i.e. Abelian) groups, the group operation is usually denoted as an addition. And, the repeated addition is called multiplication.

But, in the general group theory, the group operation is denoted as multiplication, and the repeated application of the group operation is called exponentiation. It’s conventional to use the general term “exponentiation,” even though over an Abelian group it makes more sense to call it multiplication.

You undo exponentiation by taking logarithms, so the process of solving for *k* is called the discrete logarithm problem. The security of elliptic curve cryptography depends on the difficulty of computing discrete logarithms.

## Counting Bits of Security

The best algorithms for solving discrete logarithm problem in a group of size *n* currently require O(√*n*) operations. How big is *n* in our case?

The base point, *g,* was chosen to have a large order, and in fact, its order is approximately 2^{256}. Specifically, the order of *g* written in hexadecimal is:

*n* = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141.

This means that we get approximately 256/2 = 128 bits of security because of √(2^{256}) = 2^{128}.

## Related Posts

**Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.**

Published at DZone with permission of John Cook , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

{{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}