<p>Basically, when creating a public key from a private key, what you're actually doing is multiplying some number by a point on the curve.
In most cases, you will be multiplying some very large number by what's called a Generator point.  It's just some point everybody knows and agrees upon.</p>

<p>Multiplying a number <code>d mod n</code> by a point <code>G</code>, or sometimes <code>dG</code> is defined intuitively as :</p>

<p><code>G + G + G + G...</code> and so on <code>d</code> times.</p>

<p>If you remember how to do point addition, the process here is very much the same.
To add two points <code>A</code> and <code>B</code>, with coordinates <code>(A_x, A_y), (B_x, B_y)</code>, to a make a third point <code>C</code> with coordinates<code>(C_x, C_y)</code>,
point addition is defined as :</p>

<ol>
<li>Find the slope (we'll call it <code>slp</code>) between the two points <code>A</code> and <code>B</code></li>
<li><code>C_x = slp^2 - A_x - B_x        mod p</code></li>
<li><code>C_y = slp * (A_x - C_x) - A_y  mod p</code></li>
</ol>

<p>And when adding the same point to itself, <code>A + A</code> (or <code>2*A</code> when <code>d == 2</code>), point doubling is defined as:</p>

<ol>
<li>Find the tangent (we'll call it <code>tgt</code>) to the curve at point <code>A</code></li>
<li><code>C_x = tgt^2 - 2*A_x            mod p</code></li>
<li><code>C_y = tgt * (A_x - C_x) - A_y  mod p</code></li>
</ol>

<p>Now, we only need to define how to find <code>slp</code> and <code>tgt</code>, and we can start creating public keys.
A slope between two points should look familiar :</p>

<pre><code>B_y - A_y
---------   mod p
B_x - A_x
</code></pre>

<p>And a tangent to the curve at a point is :</p>

<pre><code>3 * (A_x)^2 + a
---------------   mod p
    2 * A_y
</code></pre>

<p>* the number <code>a</code> is a parameter of the curve.  much like <code>n</code> and <code>p</code>.</p>

<p>You might already see the problem here, it's that division does not make a lot of sense in modular arithmetic.  Instead we sort of multiply the numerator by the modular inverse of the denominator, which makes sense.
To divide two numbers, we use the mod inverse function (which in turn might use the extended euclidean algorithm for example),
so what we would end up calculating would be :</p>

<p><code>(B_y - A_y) * modinv(B_x - A_x)      mod p</code>  for the slope <code>slp</code> and :</p>

<p><code>(3 * (A_x)^2 + a) * modinv(2 * A_y)  mod p</code>  for the tangent line <code>tgt</code>.</p>

<p>And this is where we use mod inverse when creating public keys, or really when adding points.</p>

CC BY-SA 3.0
2017-11-15T19:32:48.217Z
2017-11-15T19:38:03.130Z
2017-11-15T19:38:03.130Z
arubi
<p>The answer has to do with the math involved in elliptical curve cryptography, which is the basis for the cryptography used in Bitcoin.  To start with, a Bitcoin private key is simply a very large random integer.  To get the public key that corresponds with the private key, a pre-defined point on a pre-defined elliptical curve is multiplied by the private key (the large integer).  This elliptical curve point multiplication is where the inverse mod comes in.  The algorithm for elliptical curve point multiplication is several steps, and one of the steps involves an inverse mod operation.  For the details, see <a href="http://andrea.corbellini.name/2015/05/30/elliptic-curve-cryptography-ecdh-and-ecdsa/" rel="nofollow noreferrer">this article by Andrea Corbellini</a>, or take a look at <a href="https://github.com/andreacorbellini/ecc/blob/master/scripts/ecdhe.py" rel="nofollow noreferrer">this python script</a> by Andrea Corbellini.  If you follow the python script, you'll see exactly where the inverse mod operation fits in.  Finally, the Bitcoin address is derived from the public key, by way of <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow noreferrer">this procedure</a>.</p>

CC BY-SA 3.0
2017-11-15T19:00:25.500Z
2017-11-15T19:00:25.500Z
mti2935
<p>I understand the function of mod inverse, however I really have no idea why this is important with process of creating public key. What does this function do, what value do we get it? Can someone explain it like to a 5 year old please.</p>

<p>Thanks</p>

<private-key><public-key>
the real purpose of mod inverse in bitcoin for creating public key
Testing man
# the real purpose of mod inverse in bitcoin for creating public key

(Asked by: username_0 on 2017-11-14)

I understand the function of mod inverse, however I really have no idea why this is important with process of creating public key. What does this function do, what value do we get it? Can someone explain it like to a 5 year old please.

Thanks

**Tags:** private-key, public-key


---

## 2 answers

### Answer by username_1 on 2017-11-15. Score: 2

Basically, when creating a public key from a private key, what you're actually doing is multiplying some number by a point on the curve.
In most cases, you will be multiplying some very large number by what's called a Generator point. It's just some point everybody knows and agrees upon.

Multiplying a number `d mod n` by a point `G`, or sometimes `dG` is defined intuitively as :

`G + G + G + G...` and so on `d` times.

If you remember how to do point addition, the process here is very much the same.
To add two points `A` and `B`, with coordinates `(A_x, A_y), (B_x, B_y)`, to a make a third point `C` with coordinates`(C_x, C_y)`,
point addition is defined as :

1. Find the slope (we'll call it `slp`) between the two points `A` and `B`
2. `C_x = slp^2 - A_x - B_x mod p`
3. `C_y = slp * (A_x - C_x) - A_y mod p`

And when adding the same point to itself, `A + A` (or `2*A` when `d == 2`), point doubling is defined as:

1. Find the tangent (we'll call it `tgt`) to the curve at point `A`
2. `C_x = tgt^2 - 2*A_x mod p`
3. `C_y = tgt * (A_x - C_x) - A_y mod p`

Now, we only need to define how to find `slp` and `tgt`, and we can start creating public keys.
A slope between two points should look familiar :

```
B_y - A_y
---------   mod p
B_x - A_x

```

And a tangent to the curve at a point is :

```
3 * (A_x)^2 + a
---------------   mod p
    2 * A_y

```

\* the number `a` is a parameter of the curve. much like `n` and `p`.

You might already see the problem here, it's that division does not make a lot of sense in modular arithmetic. Instead we sort of multiply the numerator by the modular inverse of the denominator, which makes sense.
To divide two numbers, we use the mod inverse function (which in turn might use the extended euclidean algorithm for example),
so what we would end up calculating would be :

`(B_y - A_y) * modinv(B_x - A_x) mod p` for the slope `slp` and :

`(3 * (A_x)^2 + a) * modinv(2 * A_y) mod p` for the tangent line `tgt`.

And this is where we use mod inverse when creating public keys, or really when adding points.


---

### Answer by username_2 on 2017-11-15. Score: 1

The answer has to do with the math involved in elliptical curve cryptography, which is the basis for the cryptography used in Bitcoin. To start with, a Bitcoin private key is simply a very large random integer. To get the public key that corresponds with the private key, a pre-defined point on a pre-defined elliptical curve is multiplied by the private key (the large integer). This elliptical curve point multiplication is where the inverse mod comes in. The algorithm for elliptical curve point multiplication is several steps, and one of the steps involves an inverse mod operation. For the details, see [this article by Andrea Corbellini](http://andrea.corbellini.name/2015/05/30/elliptic-curve-cryptography-ecdh-and-ecdsa/), or take a look at [this python script](https://github.com/andreacorbellini/ecc/blob/master/scripts/ecdhe.py) by Andrea Corbellini. If you follow the python script, you'll see exactly where the inverse mod operation fits in. Finally, the Bitcoin address is derived from the public key, by way of [this procedure](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses).


---

