Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

GMP library usage questions

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
User avatar
ruwolf
Posts: 643
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 41 times
Been thanked: 30 times

Re: GMP library usage questions

#21 Post by ruwolf »

Adding zeroes in front of number can be done e.g. by gmp_printf function, where is optional width parameter (which can be also argument of function, of course, in this case asterix “*” is used).

Code: Select all

// +4 = +1+1+2: +1 for added digit, +1 for added zero and +2 for "0x" prefix
gmp_printf("number=%#0*Zx\n", (int)sizein16 +4, number);
I think, that error is cuased by double including of ecdsa_arithmetic.h:
1st directly from main.c, 2nd by chain through comp_pubkey.h.
It can by avoided by standard trick, include guard, which is used in your system files (stdio.h etc.), too.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#22 Post by PsySc0rpi0n »

ruwolf wrote: 2024-01-06 21:59 Adding zeroes in front of number can be done e.g. by gmp_printf function, where is optional width parameter (which can be also argument of function, of course, in this case asterix “*” is used).

Code: Select all

// +4 = +1+1+2: +1 for added digit, +1 for added zero and +2 for "0x" prefix
gmp_printf("number=%#0*Zx\n", (int)sizein16 +4, number);
I think, that error is cuased by double including of ecdsa_arithmetic.h:
1st directly from main.c, 2nd by chain through comp_pubkey.h.
It can by avoided by standard trick, include guard, which is used in your system files (stdio.h etc.), too.
Ok, I'm going to tackle one problem at a time.
First the problem of the double include.

I knew I woul come to this point when I start creating source files and correspondent header files.
So, in source file comp_pubkey.c I need the data type I defined in ecdsa_arithmetic.h, so I include this ecdsa_arithmetic.h in comp_pubkey.h.
In main.c file I need thhe function declared in comp_pubkey.h and also the data type defined in ecdsa_arithmetic.h.

So, what would be the best approach to deal with this situation? I'm not sure If I should create a file only for the data type declaration or if the include guard would be of use here and where to put it, because, tbh, I never used include guards. Everything I've been doing are very small and simple programs. Never needed the include guards!

User avatar
ruwolf
Posts: 643
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 41 times
Been thanked: 30 times

Re: GMP library usage questions

#23 Post by ruwolf »

It is quite easy trick.
In the header of the affected file (but it can be in any .h because it doesn't hurt) you add 2 lines at the beginning and 1 line at the end.
You can see it in mentioned link:
https://en.wikipedia.org/wiki/Include_guard#File_%22grandparent.h%22_2 wrote:File "grandparent.h"

Code: Select all

#ifndef GRANDPARENT_H
#define GRANDPARENT_H

struct foo {
    int member;
};

#endif /* GRANDPARENT_H */
In the second line, there is definition (#define) of macro as file name (i.e. macro-name GRANDPARENT_H is the same as file-name grandparent.h, except capitalised and dot changed to underline).
The whole file is enclosed in a conditional preprocessing (#ifndef = #if not definded), i.e. it will only be processed if that macro is not yet defined.
But the preprocessing of the file defines the macro, so the second and every later processing attempt will be excluded by this very condition.

I am sure you can add those 3 lines analogically to all you header (*.h) files.

(The only risk is using the same file-name in different directories, but it is not you case.)

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#24 Post by PsySc0rpi0n »

Ok, now the second issue is that I need to have 0x02, 0x03 and/or 0x04 instead of just 0x2,0x3 and/or 0x4. So, I'm missing a 0 while printing. I tried a few ways playing with the specifiers and with and etc from GMP ocs but I couldn't achieve the result I wanted!

So, for instance, if I ave a pub key like:
PubKey ==> Q.x = 0x100016a48cf2dde9b42c4cf866a3824a191d914c160a8c93642ffabdd50ffe53
PubKey ==> Q.y = 0x756cca6deca622301992d5070c1e5112fbc43ad7e44cfe8a8499a0006a52e1d3
I test if Q.y is odd or even and according to the result, I need to prepend 02 if it is even, and 03 if it is odd and later I'll add yet another situation, but for now, this is what I need to do. The part of checking if Q.y is odd or even it's done, the part of adding the 2 or the 3 is also done, but now I need to include the 02 instead of 2 and te 03 instead of 3. This is only for printing, so the solution is in the gmp_printf() function.

Edited;
Ok, I got it. Later I'll say what is the other sitaution I need to add!

I got it like:

Code: Select all

gmp_printf("Compressed Public Key: %#.66Zx\n", compressed_pubKey);

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#25 Post by PsySc0rpi0n »

I think I'm almost done and I'm only missing one thing.

I'm not sure I want to keep going after this next step. We will see.
What I need now is to concatenate the Q.x and Q.y coordinates and prepend a "4". Will prepend "04" only for printing purposes! The part needed now is to concatenate Q.x and Q.y.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#26 Post by PsySc0rpi0n »

Well, I got to this point:

Code: Select all

#include <stdio.h>
#include <gmp.h>

#include "../inc/comp_pubkey.h"

int compress_pubkey(gmp_coord raw_pbk, mpz_t* comp_pbk, mpz_t* uncomp_pbk) {
    size_t sizein16 = mpz_sizeinbase(raw_pbk.x, 16);
    mp_bitcnt_t exp2 = (mp_bitcnt_t) sizein16 << 2;
    mpz_t prefix_z;


    // check if Y coordinate of raw pub key is odd or even
    if(mpz_tstbit(raw_pbk.y, 0))
        // prefix 03 for odd coord
        mpz_init_set_ui(prefix_z, 3);
    else
        // prefix 02 for even coord
        mpz_init_set_ui(prefix_z, 2);

    // Add the prefix to the X coordinate of pub key
    mpz_mul_2exp(prefix_z, prefix_z, exp2);
    mpz_add(*comp_pbk, raw_pbk.x, prefix_z);
    
    // Concatenate the 2 coordinates of the raw pub key
    mpz_mul_2exp(*uncomp_pbk, raw_pbk.x, exp2);
    mpz_add(*uncomp_pbk, *uncomp_pbk, raw_pbk.y);

    // Add 04 prefix to the uncompressed pub key
    mpz_set_ui(prefix_z, 4);
    mpz_mul_2exp(prefix_z, prefix_z, exp2);
    mpz_add(*uncomp_pbk, *uncomp_pbk, prefix_z);

    mpz_clears(prefix_z, NULL);
    return 0;
}
But the part of adding the prefix 4 to the uncompressed version of the key is not working. That's the only part missing now! I get the concatenated numbers but not the prefix.
$ bin/./btctest 
PrivKey = 0x17c2315ded1f388bc63ae816f263b76a8ff6dddeea466e71f9fa7e35fd9f43ba
PubKey Q[x, y] = privKey * [G.x, G.y]
PubKey ==> Q.x = 0x916c35c05c469282b5ddd5c4d70f0e60a2fc38d048480d7f1db413dc0bf06fd9
PubKey ==> Q.y = 0x37869a45fea338e90f74b3e6d33239b14ebff7eb4ad685cbc0f9a3552abc4fd2
Compressed Public Key: 0x02916c35c05c469282b5ddd5c4d70f0e60a2fc38d048480d7f1db413dc0bf06fd9
Uncompressed Public Key: 0x00916c35c05c469282b5ddd5c4d70f0e60a2fc38d048480d7f1db413dc0bf06fdd37869a45fea338e90f74b3e6d33239b14ebff7eb4ad685cbc0f9a3552abc4fd2
I should get:
Uncompressed Public Key: 0x04916c35c05c469282b5ddd5c4d70f0e60a2fc38d048480d7f1db413dc0bf06fdd37869a45fea338e90f74b3e6d33239b14ebff7eb4ad685cbc0f9a3552abc4fd2

User avatar
ruwolf
Posts: 643
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 41 times
Been thanked: 30 times

Re: GMP library usage questions

#27 Post by ruwolf »

You have to compute sizein16 and exp2 for every number, to which you want to add digit as prefix to.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#28 Post by PsySc0rpi0n »

ruwolf wrote: 2024-01-13 00:30 You have to compute sizein16 and exp2 for every number, to which you want to add digit as prefix to.
Ok, you're awsome man. It's working as intended, I guess.

By the way, are you used to work with GMP library or you just saw it for the first time as me?

User avatar
ruwolf
Posts: 643
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 41 times
Been thanked: 30 times

Re: GMP library usage questions

#29 Post by ruwolf »

Rarely. If I want to compute something with high precision.
I love its derivatives MPFR and MPFI, too. Both are included in Debian.

User avatar
PsySc0rpi0n
Posts: 322
Joined: 2012-10-24 13:54
Location: Bitcoin World
Has thanked: 8 times
Been thanked: 1 time

Re: GMP library usage questions

#30 Post by PsySc0rpi0n »

ruwolf wrote: 2024-01-13 20:45 Rarely. If I want to compute something with high precision.
I love its derivatives MPFR and MPFI, too. Both are included in Debian.

Oh, ok, nice. I didn't know those either.
Anyway, next step would be to get to the point where I can use the Pub Key and Priv Key to get the bitcoin address, also just for learning purposes, but I not sure I want to go there! I have to see what is needed and then decide!

Post Reply