<p>I posted this answer on <a href="https://bitcointalk.org/index.php?topic=1133575.msg11971722#msg11971722" rel="nofollow">BitcoinTalk</a> but will copy it here for those who don't want to chase links. The following is a direct paste from that link:</p>

<p>If you can, installing sage and using that rather than Python will make your life much easier. There is an example notebook on bitcoin.ninja which does some ECDSA stuff on Bitcoin's curve.</p>

<p>To directly answer your question, we can get x from y in basically the same way you got y from x. To see this, let's walk through why your method works.</p>

<p>We have from our curve equation that Y = y^2 = x^3 + 7. You can compute Y easily from x, then you're solving Y = y^2 for y. By Fermat's Little Theorem we can write 1 = y^(p - 1) = Y^(p - 1)/2. Write Q = (p - 1)/2; then we have Y^Q = 1, so Y^(Q + 1) = Y = y^2, so Y^((Q + 1)/2) = y. As it turns out, (Q + 1)/2 = (p + 1)/4, which is why you were able to solve for y by using an exponent of (p + 1)/4. Notice that this depends crucially on p being 3 mod 4; otherwise (p + 1)/4 would not be an integer and we wouldn't be able to compute this. Fortunately our choice of p satisfies this.</p>

<p>OK! So let's do the analogous thing for x. Let's write X = x^3 = y^2 - 7. X can be computed from y easily, so we need to solve X = x^3. Write Q = (p - 1)/3; then X^Q = x^(p - 1) = 1, so X^(Q + 1) = Q = x^3, so X^((Q + 1)/3) = x. As it turns out, (Q + 1)/3 = (p + 2)/9. This time we depend crucially on p being 7 mod 9, in order that this is an integer. Luckily it is! So there you go.</p>

<p>TL;DR use (p + 2)/9 in place of (p + 1)/4.</p>

<p>Oh, and to get the other two cube roots you multiply by a nontrivial cube root of 1. (Similar to you multiplying by -1 in your original code.) One such cube root is 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee.</p>

<p>Here is Python code analogous to yours. It takes one of your output y values and returns the input x value as x2.</p>

<pre><code>## Input
y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8

## Field parameters
# Field modulus
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
# Cube root of 1
beta = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee

## Actual code
xcubed = (y*y - 7) % p
print "xcubed = 0x%x" % xcubed

x = pow(xcubed, (p + 2) / 9, p)
print "x1 = 0x%x" % x
print "x2 = 0x%x" % (x * beta % p)
print "x3 = 0x%x" % (x * beta * beta % p)
</code></pre>

<p>Its output is</p>

<pre><code>xcubed = 0x4866d6a5ab41ab2c6bcc57ccd3735da5f16f80a548e5e20a44e4e9b8118c26eb
x1 = 0xc994b69768832bcbff5e9ab39ae8d1d3763bbf1e531bed98fe51de5ee84f50fb
x2 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
x3 = 0xbcace2e99da01887ab0102b696902325872844067f15e98da7bba04400b88fcb
</code></pre>

CC BY-SA 3.0
2015-07-26T04:20:04.220Z
Thank you Andrew Poelstra, development purpose I ask this question
Prabu r
CC BY-SA 3.0
2015-07-26T04:15:16.713Z
2015-07-26T04:20:38.870Z
2015-07-26T04:20:38.870Z
Andrew Poelstra
<p>As an addendum to Andrew's correct answer, when I run his Python code <code>x = pow(xcubed, (p + 2) / 9, p)</code> I get an error <code>TypeError: pow() 3rd argument not allowed unless all arguments are integers</code>, which is related to the fact that the exponent <code>(p + 2) / 9</code> is a floating point (because of division in Python). To avoid this error, you can use Fermat's Little Theorem on the exponent <em>itself</em>:</p>

<p><code>(p + 2) / 9</code> becomes:</p>

<p><code>(p + 2) * pow(9, -1, p)</code> which becomes:</p>

<p><code>(p + 2) * pow(9, -1 + (p - 1), p) % p</code> which simplifies to:</p>

<p><code>(p + 2) * pow(9, p - 2, p) % p</code></p>

<p>Andrew's code would then look like:</p>

<p><code>x = pow(xcubed, (p + 2) * pow(9, p - 2, p) % p , p)</code></p>

<p>This way, you avoid the division involved in <code>(p + 2) / 9</code> and instead are just doing multiplication and exponentiation on large numbers.</p>

CC BY-SA 4.0
2019-03-14T20:49:25.347Z
2019-03-14T20:49:25.347Z
AtAFork
<p>How to get X value from Y?</p>

<p><a href="https://bitcoin.stackexchange.com/questions/32634/ecdsa-x-y-coordinate-validity-verification-doesnt-seem-to-work#">ECDSA x,y coordinate validity verification doesn&#39;t seem to work</a></p>

<p>X = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798</p>

<p>Python code,</p>

<pre><code>p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
x = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
ysquared = ((x*x*x+7) % p)    
print "ysquared= %s " % hex(ysquared)    
y = pow(ysquared, (p+1)/4, p)
print "y1 = %s " % hex(y)
print "y2 = %s " % hex(y * -1 % p)

Output
Y1 = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
Y2 = 0xb7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777

print hex((x**3 + 7 - y1**2) % p)  // output 0

print hex((x**3 + 7 - y2**2) % p) // output 0
</code></pre>

<p>above python code to get Two possible y values from x</p>

<p>like the same how to get possible x values from y?,</p>

<p>Is it any formula or script available</p>

<p><strong>My question is how to get x value from y</strong></p>

<p>if y value </p>

<p>Y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8</p>

<p>above y value how to get x value </p>

CC BY-SA 3.0
2015-07-23T19:53:56.633Z
Well, you would have to solve the equation "y^2 = x^3 + ax + b", but keep in mind that this equation doesn't operate on normal numbers, it operates on integers modulo some large prime that is specified in the definition of secp256k1.  I don't know how.
David Grayson
<transactions><bitcoind><address><signature><ecdsa>
Bitcoin how to get X value from Y
Prabu r
# Bitcoin how to get X value from Y

(Asked by: username_0 on 2015-07-23)

How to get X value from Y?

[ECDSA x,y coordinate validity verification doesn't seem to work](https://bitcoin.stackexchange.com/questions/32634/ecdsa-x-y-coordinate-validity-verification-doesnt-seem-to-work#)

X = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798

Python code,

```
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
x = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
ysquared = ((x*x*x+7) % p)    
print "ysquared= %s " % hex(ysquared)    
y = pow(ysquared, (p+1)/4, p)
print "y1 = %s " % hex(y)
print "y2 = %s " % hex(y * -1 % p)

Output
Y1 = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
Y2 = 0xb7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777

print hex((x**3 + 7 - y1**2) % p)  // output 0

print hex((x**3 + 7 - y2**2) % p) // output 0

```

above python code to get Two possible y values from x

like the same how to get possible x values from y?,

Is it any formula or script available

**My question is how to get x value from y**

if y value

Y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8

above y value how to get x value

**Tags:** transactions, bitcoind, address, signature, ecdsa

> **Comments:**
> **username_1**: Well, you would have to solve the equation "y^2 = x^3 + ax + b", but keep in mind that this equation doesn't operate on normal numbers, it operates on integers modulo some large prime that is specified in the definition of secp256k1.  I don't know how.  

---

## 2 answers

### Answer by username_2 on 2015-07-26. Score: 2 | Accepted answer

I posted this answer on [BitcoinTalk](https://bitcointalk.org/index.php?topic=1133575.msg11971722#msg11971722) but will copy it here for those who don't want to chase links. The following is a direct paste from that link:

If you can, installing sage and using that rather than Python will make your life much easier. There is an example notebook on bitcoin.ninja which does some ECDSA stuff on Bitcoin's curve.

To directly answer your question, we can get x from y in basically the same way you got y from x. To see this, let's walk through why your method works.

We have from our curve equation that Y = y^2 = x^3 + 7. You can compute Y easily from x, then you're solving Y = y^2 for y. By Fermat's Little Theorem we can write 1 = y^(p - 1) = Y^(p - 1)/2. Write Q = (p - 1)/2; then we have Y^Q = 1, so Y^(Q + 1) = Y = y^2, so Y^((Q + 1)/2) = y. As it turns out, (Q + 1)/2 = (p + 1)/4, which is why you were able to solve for y by using an exponent of (p + 1)/4. Notice that this depends crucially on p being 3 mod 4; otherwise (p + 1)/4 would not be an integer and we wouldn't be able to compute this. Fortunately our choice of p satisfies this.

OK! So let's do the analogous thing for x. Let's write X = x^3 = y^2 - 7. X can be computed from y easily, so we need to solve X = x^3. Write Q = (p - 1)/3; then X^Q = x^(p - 1) = 1, so X^(Q + 1) = Q = x^3, so X^((Q + 1)/3) = x. As it turns out, (Q + 1)/3 = (p + 2)/9. This time we depend crucially on p being 7 mod 9, in order that this is an integer. Luckily it is! So there you go.

TL;DR use (p + 2)/9 in place of (p + 1)/4.

Oh, and to get the other two cube roots you multiply by a nontrivial cube root of 1. (Similar to you multiplying by -1 in your original code.) One such cube root is 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee.

Here is Python code analogous to yours. It takes one of your output y values and returns the input x value as x2.

```
## Input
y = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8

## Field parameters
# Field modulus
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
# Cube root of 1
beta = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee

## Actual code
xcubed = (y*y - 7) % p
print "xcubed = 0x%x" % xcubed

x = pow(xcubed, (p + 2) / 9, p)
print "x1 = 0x%x" % x
print "x2 = 0x%x" % (x * beta % p)
print "x3 = 0x%x" % (x * beta * beta % p)

```

Its output is

```
xcubed = 0x4866d6a5ab41ab2c6bcc57ccd3735da5f16f80a548e5e20a44e4e9b8118c26eb
x1 = 0xc994b69768832bcbff5e9ab39ae8d1d3763bbf1e531bed98fe51de5ee84f50fb
x2 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
x3 = 0xbcace2e99da01887ab0102b696902325872844067f15e98da7bba04400b88fcb

```

> **Comments:**
> **username_0**: Thank you username_2 username_2, development purpose I ask this question  

---

### Answer by username_3 on 2019-03-14. Score: 0

As an addendum to username_2's correct answer, when I run his Python code `x = pow(xcubed, (p + 2) / 9, p)` I get an error `TypeError: pow() 3rd argument not allowed unless all arguments are integers`, which is related to the fact that the exponent `(p + 2) / 9` is a floating point (because of division in Python). To avoid this error, you can use Fermat's Little Theorem on the exponent *itself*:

`(p + 2) / 9` becomes:

`(p + 2) * pow(9, -1, p)` which becomes:

`(p + 2) * pow(9, -1 + (p - 1), p) % p` which simplifies to:

`(p + 2) * pow(9, p - 2, p) % p`

username_2's code would then look like:

`x = pow(xcubed, (p + 2) * pow(9, p - 2, p) % p , p)`

This way, you avoid the division involved in `(p + 2) / 9` and instead are just doing multiplication and exponentiation on large numbers.


---

