#1 2024-09-20 21:42:34

Prometeus
Member
From: USA
Registered: 2020-11-20
Posts: 46

ECDSA private and public keys

I am trying the private/public key functions but the first conversion isn't working with the testing 'private_key' I have:

var PrivateKey   : TEccPrivateKey;
    PublicKey    : TEccPublicKey;
    private_key,
    public_key   : RawUtf8;  	

begin
  private_key := '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725';
  if mormot.core.text.HexToBin(PAnsiChar(private_key), @PrivateKey, SizeOf(PrivateKey)) then
    begin
      Ecc256r1PublicFromPrivate(PrivateKey, PublicKey);
      public_key := mormot.core.text.BinToHexDisplay(@PublicKey, SizeOf(PublicKey)); // I'm not sure if this conversion is the proper one
      // 'public_key' should be '50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352'
    end;
end;

Also, I'm unsure how to convert the 'PublicKey' variable to a readable one while in the testing phase. Any hint?

Last edited by Prometeus (2024-09-21 19:54:59)

Offline

#2 2024-09-21 07:54:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: ECDSA private and public keys

Try to understand a little bit how it works, and look at the documentation.
If HexToBin fails, it means that your private key hexa is not of the right size.

So how did you create your private_key content?
You can try DerToEcc() overloaded function, or another means.

Offline

#3 2024-09-21 14:53:34

Prometeus
Member
From: USA
Registered: 2020-11-20
Posts: 46

Re: ECDSA private and public keys

Ab,

I always try to understand what I am doing. Believe me, I avoid asking in this forum because of the intimidating way you respond to some people here, without knowing precisely what each user may have done before asking, but for this case, it simply didn't work. I also tried to google some responses using mormot stuff, but I also found nothing.  I didn't invent the private key. There is one page with some steps to find a final result, starting with a 256-bit private key given by them (the one used in my post, as well as the result public key from the original private key, also posted there). It is a 256-bit private key as it is supposed to be, the SAME SIZE of the one used in your 'test.core.ecc', line 89. I just need to calculate a 256-bit public key for a well-generated 256-bit private key, as is the case in my original question, and I am trying to use the mormot one because looks like it's fast.

Last edited by Prometeus (2024-09-21 19:32:53)

Offline

#4 2024-09-21 17:59:03

Coldzer0
Member
From: ::1
Registered: 2018-08-31
Posts: 35
Website

Re: ECDSA private and public keys

Prometeus wrote:

Ab,

I always try to understand what I am doing. Believe me, I avoid asking in this forum because of the intimidating way you respond to some people here, without knowing precisely what each user may have done before asking, but for this case, it simply didn't work. I also tried to google some responses using mormot stuff, but I also found nothing.  I didn't invent the private key. There is one page with some steps to find a final result, starting with a 256-bit private key given by them (the one used in my post, as well as the result public key from the original private key, also posted there). It is a 256-bit private key as it is supposed to be, the SAME SIZE of the one used in your 'test.core.ecc', line 89. In fact, I just realized that the key you used in that example also doesn't pass the test above ('DC5B79BD481E536DD8075D06C18D42B25B557B4671017BA2A26102B69FD9B70A'). So, I am using the same functions used in your tests and it is not working. I just need to calculate a 256-bit public key for a well-generated 256-bit private key, as is the case in my original question, and I am trying to use the mormot one because looks like they are fast.

I would recommend making sure that the one you are using is ecc256r1 and not ec25519, Because mORMot2 only support the ecc256r1.


Mac, Windows, Linux
FPC Trunk, Lazarus Trunk, Delphi 12.x Latest

Offline

#5 2024-09-21 18:01:23

Coldzer0
Member
From: ::1
Registered: 2018-08-31
Posts: 35
Website

Re: ECDSA private and public keys

And this is an example I created a while ago when I was researching mORMot2.

https://gist.github.com/Coldzer0/448f5b … 23a0de456f


Mac, Windows, Linux
FPC Trunk, Lazarus Trunk, Delphi 12.x Latest

Offline

#6 2024-09-21 18:36:08

Prometeus
Member
From: USA
Registered: 2020-11-20
Posts: 46

Re: ECDSA private and public keys

=Coldzer0

I would recommend making sure that the one you are using is ecc256r1 and not ec25519, Because mORMot2 only support the ecc256r1.

I'll investigate if the provided private key is of type 'ecc256r1', and also thank you for the sample!

Last edited by Prometeus (2024-09-21 18:47:11)

Offline

#7 2024-09-21 19:37:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: ECDSA private and public keys

My guess is that your private key comes from
https://bitcoin.stackexchange.com/quest … generation

And Bitcoin uses ecc256k1, not ecc256r1, AFAICT.

Offline

#8 2024-09-21 19:45:52

Prometeus
Member
From: USA
Registered: 2020-11-20
Posts: 46

Re: ECDSA private and public keys

ab wrote:

My guess is that your private key comes from
https://bitcoin.stackexchange.com/quest … generation

And Bitcoin uses ecc256k1, not ecc256r1, AFAICT.

   Thanks for pointing out the right 'ecc' format for this case, and unfortunately mormot can't handle it.

Last edited by Prometeus (2024-09-22 02:35:18)

Offline

Board footer

Powered by FluxBB