Bitcoin Forum
May 02, 2024, 02:39:27 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to calculate public key manually?  (Read 1426 times)
apxu (OP)
Member
**
Offline Offline

Activity: 229
Merit: 13


View Profile
April 17, 2016, 04:30:34 PM
 #1

I have some troubles with understanding bitcoin math.
For example, my private key is '2'
So, the public key should be (Gx,Gy) * 2
Gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
So, Gx * 2 will be 0x79... + 0x79... = 0xf3... (I can calculate it in mind)
But the public key for '2' is 04C6047F9... (or 02C6047F9... in compressed form)
What is wrong with my calculations?
How to multiply the base point 0x79be66... by two and receive 0xC6047F9 ?
1714660767
Hero Member
*
Offline Offline

Posts: 1714660767

View Profile Personal Message (Offline)

Ignore
1714660767
Reply with quote  #2

1714660767
Report to moderator
"In a nutshell, the network works like a distributed timestamp server, stamping the first transaction to spend a coin. It takes advantage of the nature of information being easy to spread but hard to stifle." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714660767
Hero Member
*
Offline Offline

Posts: 1714660767

View Profile Personal Message (Offline)

Ignore
1714660767
Reply with quote  #2

1714660767
Report to moderator
1714660767
Hero Member
*
Offline Offline

Posts: 1714660767

View Profile Personal Message (Offline)

Ignore
1714660767
Reply with quote  #2

1714660767
Report to moderator
1714660767
Hero Member
*
Offline Offline

Posts: 1714660767

View Profile Personal Message (Offline)

Ignore
1714660767
Reply with quote  #2

1714660767
Report to moderator
DannyHamilton
Legendary
*
Offline Offline

Activity: 3388
Merit: 4613



View Profile
April 17, 2016, 06:22:52 PM
Last edit: April 17, 2016, 07:03:44 PM by DannyHamilton
 #2

https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_addition
Quote
With 2 distinct points, P and Q, addition is defined as the negation of the point resulting from the intersection of the curve, E, and the straight line defined by the points P and Q, giving the point, R.



Assuming the elliptic curve, E, is given by y2 = x3 + ax + b, this can be calculated as:


Evil-Knievel
Legendary
*
Offline Offline

Activity: 1260
Merit: 1168



View Profile
April 18, 2016, 06:27:22 PM
 #3

Exactly as Hamilton has pointed it out.
What has not yet be mentioned is that all operations are performed in the factor ring Z/nZ with n being the prime modulus of the secp256k1 curve.

Example: if n was 7 (which it isn't) then 4*4 = 16 mod 7 == 2
apxu (OP)
Member
**
Offline Offline

Activity: 229
Merit: 13


View Profile
April 18, 2016, 08:27:34 PM
 #4

Exactly as Hamilton has pointed it out.
What has not yet be mentioned is that all operations are performed in the factor ring Z/nZ with n being the prime modulus of the secp256k1 curve.

Example: if n was 7 (which it isn't) then 4*4 = 16 mod 7 == 2
The modulo operations were clear for me.
I just needed a formula for point multiplication G by the private key.
Now I understand my error.
I haven't checked it yet, but I will try to calculate the public key manually and compare it with bitaddress.org
Evil-Knievel
Legendary
*
Offline Offline

Activity: 1260
Merit: 1168



View Profile
April 18, 2016, 09:35:19 PM
 #5

apxu, it is not that hard. You just have to take care of the different operations in such factor rings.

For example the division is not a real division but the multiplication with the multiplicative inverse in the factor ring (you will need the extended euclid for that).
gmaxwell
Moderator
Legendary
*
expert
Offline Offline

Activity: 4158
Merit: 8382



View Profile WWW
April 18, 2016, 10:30:30 PM
Last edit: April 19, 2016, 03:21:19 AM by gmaxwell
 #6

A simplistic construction that does an inversion for every add, even using extgcd (which is far from constant time), is going to be horribly slow. And probably infeasible to do by hand. Though it's more complex to explain, protective coordinates would likely be needed to have any hope of performing a point-scalar multiply by hand, along with a big precomputed table.
kn_b_y
Newbie
*
Offline Offline

Activity: 26
Merit: 3


View Profile
April 19, 2016, 11:10:28 PM
 #7

Here's some working python code that generates the point of the public key for private key = 2
Code:
# Inversion using Extended Euclidean algorithm.
def EEA_invert(a, b):
    r = {}; s = {}; r[0] = a; r[1] = b; s[0] = 1; s[1] = 0
    i = 1;
    while True:
        if r[i]==0: break
        q = r[i-1]//r[i]; r[i+1] = r[i-1] - q*r[i]; s[i+1] = s[i-1] - q*s[i]
        i += 1
    return s[i-1] % b

# Bitcoin's EC parameters: see https://en.bitcoin.it/wiki/Secp256k1
G = '79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8'.replace(' ', '')
gx, gy = int(G[:64], 16), int(G[64:], 16)
p = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1

# Calculate differential at the generator point.      
slope = (3*gx**2)*EEA_invert(2*gy, p) % p

# Calculate x, y.
x = (slope**2 - 2*gx) % p
y = (slope*(gx - x) - gy) % p

print(hex(x), hex(y))
which yields:

Code:
('0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5',
 '0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a')
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!