Title: Private key to Public key Post by: qqq123 on April 26, 2012, 04:52:46 AM Hello,
I am here looking for an understanding of the math behind converting a Bitcoin private key to a Bitcoin public key for a website I am creating. The website will be capable of dynamically creating a private key and the corresponding public key and Bitcoin address (much like bitaddress.org). If anyone can help with this understanding, I'd appreciate it. Thanks. Title: Re: Private key to Public key Post by: Red Emerald on April 26, 2012, 05:11:46 AM The wiki is an amazing resource.
https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses Title: Re: Private key to Public key Post by: qqq123 on April 26, 2012, 01:44:10 PM I've read that article more times than I can count. It still doesn't explain the equation(s) used to go from the private key to the public key. It just says "1) Have a private ECDSA key. 2) Take the public key generated with it (65 bytes, 1 byte 0x04, 32 bytes corresponding to X coordinate, 32 bytes corresponding to Y coordinate)." I do not understand how that public key is generated, despite reading several articles on elliptic curve cryptography.
Title: Re: Private key to Public key Post by: payb.tc on April 26, 2012, 01:51:30 PM if the private key is a hex string, try following the below functions.
(apologies to the author, i don't remember where i found this code) Code: define("ADDRESSVERSION","00"); //this is a hex byte Title: Re: Private key to Public key Post by: qqq123 on April 26, 2012, 02:06:57 PM That's from here (https://github.com/mikegogulski/bitcoin-php), and I don't think it can be used to go from a private key to a public key.
Title: Re: Private key to Public key Post by: Red Emerald on April 26, 2012, 05:56:04 PM So you want to know how the initial private and public key are created with ECDSA? "Complex Math" is as far as I've gone into that one personally.
http://en.wikipedia.org/wiki/Elliptic_Curve_DSA Title: Re: Private key to Public key Post by: Stephen Gornick on April 27, 2012, 02:29:33 AM The website will be capable of dynamically creating a private key and the corresponding public key and Bitcoin address (much like bitaddress.org) Doesn't explain the math behind it, but an example of it is in the Javascript from the HTML source here: - http://BitAddress.org [Update: More info here? - http://bitcointalk.org/index.php?topic=3238.0 - http://bitcointalk.org/index.php?topic=53177.0 - https://bitcointalk.org/index.php?topic=74136.0 ] Title: Re: Private key to Public key Post by: payb.tc on April 27, 2012, 02:49:26 AM That's from here (https://github.com/mikegogulski/bitcoin-php), and I don't think it can be used to go from a private key to a public key. Quote * @author theymos (functionality) ah yeah, from before mike put it in a class. Title: Re: Private key to Public key Post by: qqq123 on April 27, 2012, 07:09:55 PM Thanks for the replies. From what I've read I've gathered that:
PuK = G * PrK where PuK is the public key, G is the set of (x, y) coordinates for the secp256k1 elliptic curve, which are, in hex, (x = 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798, y = 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8), PrK is the private key, and * stands for point multiplication. I am, however, having trouble implementing point multiplication. I've read the wiki article here (http://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication), and understand that it's basically doubling and adding the point, but I can't quite wrap my head around the pseudocode they give. Can anyone help with that? I'm working in php, but anything would help really. Thanks. Title: Re: Private key to Public key Post by: qqq123 on April 28, 2012, 03:04:44 AM If anyone has any test problems I can run for point multiplication on the secp256k1 curve that would be great. I think I have the implementation down, but the fact that everything is in hex is giving me fits. I would love to be able to verify that the point multiplication works in decimal before moving on the tackling hex.
Doing the math by hand, I know that 2 * (4, 3) = (56, -419). But doing large multiplications by hand is impossible. For example, 100 * (4, 3) = ??? And for actually doing the point multiplication required by bitcoin... Let's say privatekey (in hex) = 059E2BF5E2C7A4098C164B29A91CF70508D2FD1A256A60656FD2593BDB980FAA so privatekey in dec = 2541029483313867478679144795827841690699430215057902992954466247848698056618 Gx (in hex) = 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798 so Gx in dec = 55066263022277343669578718895168534326250603453777594175500187360389116729240 Gy (in hex) = 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8 so Gy in dec = 32670510020758816978083085130507043184471273380659243275938904335757337482424 so, given that privatekey * (Gx, Gy) = publickey the math is (in dec): 2541029483313867478679144795827841690699430215057902992954466247848698056618 * (55066263022277343669578718895168534326250603453777594175500187360389116729240, 32670510020758816978083085130507043184471273380659243275938904335757337482424). Having a real hard time seeing if my function works with numbers that big... Title: Re: Private key to Public key Post by: dansmith on June 23, 2013, 11:12:53 PM Since this thread comes up on top when searching for priv key to pub key conversion, here is a link to a self-contained python file where you can see all the background mechanics.
https://bitcointalk.org/index.php?topic=23241.0 (https://bitcointalk.org/index.php?topic=23241.0) Title: Re: Private key to Public key Post by: thedrs on April 04, 2016, 10:59:22 PM This is an old thread.
But just for people who land here and actually want to understand how to code the calculation from private to public key See here: http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript Title: Re: Private key to Public key Post by: bitdumper on April 05, 2016, 04:20:02 AM If your backend is python
https://github.com/weex/addrgen If it's php https://github.com/RobKohr/PHP-Bitcoin-Address-Creator If it's nodejs Then This is an old thread. But just for people who land here and actually want to understand how to code the calculation from private to public key See here: http://procbits.com/2013/08/27/generating-a-bitcoin-address-with-javascript |