Bitcoin Forum
May 24, 2019, 10:46:30 PM *
News: Latest Bitcoin Core release: 0.18.0 [Torrent] (New!)
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: Convert priv key to compressed public key  (Read 210 times)
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1386
Merit: 1244


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 18, 2019, 06:08:30 PM
 #21

Can you post the first public key or make a random one?

1558737990
Hero Member
*
Offline Offline

Posts: 1558737990

View Profile Personal Message (Offline)

Ignore
1558737990
Reply with quote  #2

1558737990
Report to moderator
1558737990
Hero Member
*
Offline Offline

Posts: 1558737990

View Profile Personal Message (Offline)

Ignore
1558737990
Reply with quote  #2

1558737990
Report to moderator
GET 25 FREE SPINS AT REGISTRATION
GET 100% BONUS ON FIRST DEPOSIT
PLAY NOW
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
PietCoin97
Jr. Member
*
Offline Offline

Activity: 45
Merit: 1


View Profile
January 18, 2019, 06:11:06 PM
 #22

this is my first pub key

Code:
0489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3faab396827dea71071568548e9ddc0ffdefe649db9b8601e3db79a8d1e91c0797

and output is

03    4   89158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f

and the 4 is too much

jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1386
Merit: 1244


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 18, 2019, 06:22:45 PM
 #23

I have it!

As per what pooya said:
1. take the uncompressed public key and drop its first byte (which is 0x04)
2. split the remaining 64 bytes into 2x 32 bytes
3. take the first 32 byte, this is your (x coordinate) compressed public key after next step
4. take the second 32 bytes (y coordinate) if it was even add 0x02 otherwise 0x03 to the beginning of step 3

Code:
f = open(“keys.txt”,”r”)
even = ["0","2","4","6","8","A","C","E","a","c","e"]
for line in f:
    public_key_u=line
    public_key_x,public_key_y = public_key_u[2:67],public_key_u[67:132]
    if public_key_y[len(public_key_y)-1] in even:
        hex_compressed_public_key = "02"+public_key_x
    else:
        hex_compressed_public_key = "03"+public_key_x
    x = open(“compressedkeys.txt”,”a”)
    x.write(hex_compressed_public_key +”\n”)
    x.close()

I'm expecting the public keys to be in hex

I'm hoping my maths isn't off too.

Run the new code. I changed line 5.

Coding Enthusiast
Hero Member
*****
Offline Offline

Activity: 625
Merit: 851


Novice C♯ Coder


View Profile WWW
January 18, 2019, 06:23:17 PM
Merited by bones261 (2), ETFbitcoin (1)
 #24

change:
public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130]
to
public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130]

Projects List+Suggestion box
Donation link using BIP21
Bech32 Donation link!
BitcoinTransactionTool (0.9.2):  Ann - Source Code
Watch Only Bitcoin Wallet (supporting SegWit) (3.1.0):  Ann - Source Code
SharpPusher (broadcast transactions) (0.10.0): Ann - Source Code

PietCoin97
Jr. Member
*
Offline Offline

Activity: 45
Merit: 1


View Profile
January 18, 2019, 06:31:37 PM
 #25

yeah it is

Quote
public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130]
to
public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130]
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1386
Merit: 1244


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 18, 2019, 06:38:18 PM
 #26

change:
public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130]
to
public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130]

Well that went badly Smiley. Probably better than expected actually, I've been doing too much with signatures clearly Grin. Simple maths definitely isn't the strong point.


PietCoin97
Jr. Member
*
Offline Offline

Activity: 45
Merit: 1


View Profile
January 19, 2019, 08:59:21 AM
 #27

No problem thank you very much for your help guys.
Pages: « 1 [2]  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!