Author

Topic: how to get pubkey of signature (Read 298 times)

newbie
Activity: 72
Merit: 0
January 05, 2023, 10:55:31 AM
#15
@yoshimitsu777: have a look at https://pypi.org/project/secp256k1/, there is a function "recpub" which you can use comfortably and it does exactly what you are looking for. Example:

Code:
$ python -m secp256k1 recpub \
-s 515fe95d0780b11633f3352deb064f1517d58f295a99131e9389da8bfacd64422513d0cd4e18a58d9f4873b592afe54cf63e8f294351d1e612c8a297b5255079 \
-i 1 \
-m hello

Public key: 02477ce3b986ab14d123d6c4167b085f4d08c1569963a0201b2ffc7d9d6086d2f3

but how works?look my message and signature is

Bitcoin address: "1NLbHuJebVwUZ1XqDjsAyfTRUPwDQbemfv"
Message: "Hello, world!"
Signature: "HxhJdJzdl0W7TeL/GWJ2bCp5gGE+kLNhRfZYKfPhQdWWcuGXkWx3W60lvCM/3bfnwdYL58ZNCcx4sgohPkCrwH4="

this is not accepted by tool i get error
Code:
python3 -m secp256k1 recpub -s HxhJdJzdl0W7TeL/GWJ2bCp5gGE+kLNhRfZYKfPhQdWWcuGXkWx3W60lvCM/3bfnwdYL58ZNCcx4sgohPkCrwH4= -i 1 -m "Hello, world!"
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/yin4/.local/lib/python3.10/site-packages/secp256k1/__main__.py", line 7, in
    main()
  File "/home/yin4/.local/lib/python3.10/site-packages/secp256k1/__init__.py", line 543, in main
    sys.exit(_main_cli(args, sys.stdout, enc))
  File "/home/yin4/.local/lib/python3.10/site-packages/secp256k1/__init__.py", line 491, in _main_cli
    sig_raw = bytes(bytearray.fromhex(args.signature))
ValueError: non-hexadecimal number found in fromhex() arg at position 0

please somebody explain what is correct way to use this tool correctly.
do i need to convert signature to something else before input into command line?
where do i enter bitcoin address?
please show instruction
newbie
Activity: 72
Merit: 0
January 03, 2023, 03:31:39 AM
#14
did you find? still looking for helpful answers.
anybody knows if secp256k1 from iceland can be used for this task?
member
Activity: 330
Merit: 34
January 02, 2023, 10:25:36 AM
#13
Bitcoin address: "1NLbHuJebVwUZ1XqDjsAyfTRUPwDQbemfv"
Message: "Hello, world!"
Signature: "HxhJdJzdl0W7TeL/GWJ2bCp5gGE+kLNhRfZYKfPhQdWWcuGXkWx3W60lvCM/3bfnwdYL58ZNCcx4sgohPkCrwH4="



long time ago i converted sign messages to transaction , RSZ generate, and then to pubkey, need to find python scripts inside my big bank,
you may study this old topic here
https://bitcointalksearch.org/topic/is-possible-get-public-key-from-signature-5192074
newbie
Activity: 72
Merit: 0
January 02, 2023, 09:49:05 AM
#12
Bitcoin address: "1NLbHuJebVwUZ1XqDjsAyfTRUPwDQbemfv"
Message: "Hello, world!"
Signature: "HxhJdJzdl0W7TeL/GWJ2bCp5gGE+kLNhRfZYKfPhQdWWcuGXkWx3W60lvCM/3bfnwdYL58ZNCcx4sgohPkCrwH4="

member
Activity: 330
Merit: 34
January 02, 2023, 09:32:41 AM
#11
i dont know what RSZ is.
i have signature,message and bitcoin address.
i want pubkey, if possible in small python script
write here
you have information , which you want convert to pubkey


write
signature,message and bitcoin address
member
Activity: 330
Merit: 34
January 02, 2023, 09:32:17 AM
#10
i dont know what RSZ is.
i have signature,message and bitcoin address.
i want pubkey, if possible in small python script
write here
you have information , which you want convert to pubkey
newbie
Activity: 72
Merit: 0
January 02, 2023, 09:21:26 AM
#9
i dont know what RSZ is.
i have signature,message and bitcoin address.
i want pubkey, if possible in small python script
member
Activity: 330
Merit: 34
January 02, 2023, 08:46:44 AM
#8
is this available for python?

thanks citb0in but i get the same error as you.
yes you are right.i do now know the private key only signature and message and the address.
how can we fix this to have a python program that can make this conversion to pubkey without knowing the private key?

you mean RSZ to pubkey ?
newbie
Activity: 72
Merit: 0
January 02, 2023, 04:36:03 AM
#7
is this available for python?

thanks citb0in but i get the same error as you.
yes you are right.i do now know the private key only signature and message and the address.
how can we fix this to have a python program that can make this conversion to pubkey without knowing the private key?
hero member
Activity: 630
Merit: 731
Bitcoin g33k
December 26, 2022, 03:11:08 AM
#6

My understanding is that the example demonstrated is not what OP is looking for. In the example shown, the private key is created first and everything that follows is based on it. But the OP does not know the private key, he wants to extract the pubkey from an existing signature without knowing the private key.

@yoshimitsu777: have a look at https://pypi.org/project/secp256k1/, there is a function "recpub" which you can use comfortably and it does exactly what you are looking for. Example:

Code:
$ python -m secp256k1 recpub \
-s 515fe95d0780b11633f3352deb064f1517d58f295a99131e9389da8bfacd64422513d0cd4e18a58d9f4873b592afe54cf63e8f294351d1e612c8a297b5255079 \
-i 1 \
-m hello
Quote
Public key: 02477ce3b986ab14d123d6c4167b085f4d08c1569963a0201b2ffc7d9d6086d2f3

I do not understand what the switch -i 1 stands for, but the result seems to be correct.

I tried to implement the whole thing in Python using the ecdsa library. Here is my approach:

Code:
#!/usr/bin/env python3
# 2022-Dec-26 by citb0in

import hashlib
from ecdsa import SigningKey, VerifyingKey, SECP256k1
from ecdsa.util import sigencode_der, sigdecode_der

# private key in hex, 32 bytes
privateKeyHex = "2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b"

# convert private key from hex to bytes
privateKeyBytes = bytes.fromhex(privateKeyHex)

# create a SigningKey object of the private key
privateKey = SigningKey.from_string(privateKeyBytes, curve=SECP256k1)

# get the associated public key
publicKey = privateKey.get_verifying_key()

# message to sign
message = "hello"

# calculate the hash of that message
messageHash = hashlib.sha256(message.encode("utf-8")).digest()

# create the signature
signature = privateKey.sign(messageHash, sigencode=sigencode_der)

# encode signatur in hex
signatureHex = signature.hex()

# show some output
print(f"privKey in hex: {privateKeyHex}")
print(f"SigningKey object: {privateKey}")
print(f"pubKey in hex: {publicKey}")
print(f"message: {message}")
print(f"messageHash: {messageHash}")
print(f"Signature: {signature}")
print(f"Signature in hex: {signatureHex}") # output is "3045022075ace5b099991073ef11947127249d0c43f16e1069762360ae1b892b13aeffb50221009792e26181f84f1882944626f669fdfc8eb0c63e80058e498ff56ee5ac1e6041" but is wrong

# the correct and expected signatureHex should be:
signatureHex = "304502210095ade2b0fd9caa90e4993e59232b774e4dc2082fdb8a30267abf21fc6a076715022016d762b4a23e30e8151f2852e88f6beebe65290266aafbbd0cbf2c1f6b3dc78c"
print(f"correct signature in hex should be: {signatureHex}")
# verify signature with pubKey
try:
    publicKey.verify(signature, message, hashlib.sha256, sigdecode=sigdecode_der)
    print("Signature is valid.")
except:
    print("Invalid signature!")

Unfortunately, the generated signature is not correct, I would have expected something else. Even if I write the correct signature into the variable, I still get "Invalid signature" as error message.

Where do I have a (thinking) error ?
newbie
Activity: 72
Merit: 0
December 25, 2022, 03:37:38 AM
#4
i stumbled over bit which is very fast.can I use bit library for this task?

https://github.com/ofek/bit/search?q=signature

I do not think it work with bit right?maybe python-ecdsa and keys.py but i have no clue how to run and use it.any example?
legendary
Activity: 3472
Merit: 10611
December 20, 2022, 11:16:22 PM
#3
Look inside any bitcoin library that has the "message verification" function implemented (which is almost all of them) then follow that code to find a method that performs public key recovery using the signature and the message (hash) before it verifies the signature against that public key. That is how you recover public keys from ECDSA signatures.
legendary
Activity: 1512
Merit: 7340
Farewell, Leo
December 20, 2022, 03:47:41 PM
#2
I'm not a cryptography expert, but I know that you can't do that with just the signature. You need at least the signature and the message that was signed (curve details asides).

If there's no library that does this operation (I highly doubt, but if), you'll have to implement the paragraph 4.1.6 from this paper: https://www.secg.org/sec1-v2.pdf. You're already starting to sweat, I can feel it. You better do some good search for already implemented code.  Tongue

How about this?
newbie
Activity: 72
Merit: 0
December 20, 2022, 01:47:43 PM
#1
it should be possible but i did not find a python lib that supports this kind of data extraction any clues?
Jump to: