Hello all. I am referring to
secp256k1 library in Python where you can signrec a message.
Following example:
privkey = '0000000000000000000000000000000000000000000000000000000000000001'
message = 'Hello, world!'
after signing it will produce the signature
b85d62928d63583f52b14995c9444a92e1b7998a3fcfd0c134f327d61b162c6e7ea40adb783bd4c 00f9cfdb829c7e7d5b8d8e25a797d8548aec6f41df461fab9
I am digging into the code and like to understand what the process looks in detail. I am stuck on the last point and hopefully someone can shed some light onto...
The signrec command line argument calls the function
ecdsa_sign_recoverable[...]
elif args.action == 'signrec':
priv, sig = sign('ecdsa_sign_recoverable', args)
[...]
the message is hashed one time through sha256 and stored into the variable name msg32. The sha256 from the message shown is
315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
[...]
def ecdsa_sign_recoverable(self, msg, raw=False, digest=hashlib.sha256):
if not HAS_RECOVERABLE:
raise Exception("secp256k1_recovery not enabled")
msg32 = _hash32(msg, raw, digest)
[...]
So far so good and understood. But on the next step the raw_sig variable is created by calling
ffi.new('secp256k1_ecdsa_recoverable_signature *')[...]
raw_sig = ffi.new('secp256k1_ecdsa_recoverable_signature *')
signed = lib.secp256k1_ecdsa_sign_recoverable(
secp256k1_ctx, raw_sig, msg32, self.private_key,
ffi.NULL, ffi.NULL)
assert signed == 1
return raw_sig
[...]
I am trying to understand what exactly is done in this step but I have no insight. I see that the called functions are derived from _libsecp256k1.
from ._libsecp256k1 import ffi, lib
I looked into the filesystem of that python module secp256k1, there is the file:
~/.local/lib/python3.10/site-packages/secp256k1/_libsecp256k1.cpython-310-x86_64-linux-gnu.so
but I have no clue what's inside. Can anyone explain to me, please?
What process in detail is done to get from the sha256 hash "315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3"
--> to the final signature ?b85d62928d63583f52b14995c9444a92e1b7998a3fcfd0c134f327d61b162c6e7ea40adb783bd4c 00f9cfdb829c7e7d5b8d8e25a797d8548aec6f41df461fab9
My next question ... If I'm not mistaken the shown final signature in base64 representation is:
IEkjQHms3Yy0+B8INBVgKozpZc1rf3OHf7MCk2CnrGorYk2TEnwnNSHnLuK8tRkBIAIR1c9i8NCO19EebEHCMak=
How do you convert this base64 to get to the hex representation and vice-versa ?
Thanks to all.