MPN - string conversion difficulties

Zsolt Kalmandi seadefence at protonmail.com
Thu Dec 28 06:08:25 CET 2023


Hi,

I'm trying to rewrite my C code using MPN functions cleanly, instead of MPZ. I am interested the speed difference of course.
These functions are:

mpz_sub
mpz_mul
mpz_mod
mpz_invert

There are no import/export mpn functions, but fortunately mpn_get_str / mpn_set_str keep leading zeroes, if I'm not mistaken! My main problem is, I can't get back any usable value from any mp_limb_t array.

For example:
mpn_get_str(result, 16, a, 4);
I would like to store 32 byte (4 limb) hex in unsigned char[32] array, but I cant get it right! First of all, It looks like it is converting only one character or 4bit or whatever it is into one byte, and most of the data is just full of crap. I would like to see: like 0xAC; 0x85; etc. values per byte, I mean 8bit hex value per byte. I added +1 byte for the string termination.. nothing changed.

I tried to print out many values from memory, from the beginning of the mpn variable (pointer) to see how the stored data map looks like, copying the limbs straight.. no success.
I tried ChatGpt etc to make some kind of example.. more crap, what I did (they created functions what not even existing).

According to the documentation:

"There may be leading zeros in the string. The string is not in ASCII; to convert it to printable format, add the ASCII codes for0orA, depending on the base and range."
I have no idea what this means.

So long story short.. could someone explain me what is the solution?
I would like to get back unsigned char[32] array with proper 8 bit HEX values/ byte!

Thank you,
Zsolt

Sent with [Proton Mail](https://proton.me/) secure email.


More information about the gmp-discuss mailing list