Custom data type in C

I work with cryptography and should use some really big numbers. I also use the new Intel bottomless multiplication instruction, which requires the m128i data type, which is executed by loading it with a function that takes floating point data as arguments.

I need to save an integer 2 ^ 1223, and then put it in a square and save that value.

I know that I can use the GMP library, but I think it would be faster to create two types of data that store values ​​like 2 ^ 1224 and 2 ^ 2448. It will have less overhead. I am going to use karatsuba to multiply numbers, so the only operation I need to do for the data type is adding, since I will split the number until m128i matches.

Can someone direct me towards material that can help me create the size of the integer that I need.

+4
source share
1 answer

If you need your own data types (regardless of whether it is suitable for mathematics, etc.), you need to return to structures and functions. For instance:

struct bignum_s { char bignum_data[1024]; } 

(obviously, you want to get the right sizing, this is just an example)

Most people end up also typing it:

 typedef struct bignum_s bignum; 

And then create functions that take two (or any) pointers to numbers to do what you want:

 /* takes two bignums and ORs them together, putting the result back into a */ void bignum_or(bignum *a, bignum *b) { int i; for(i = 0; i < sizeof(a->bignum_data); i++) { a->bignum_data[i] |= b->bignum_data[i]; } } 

You really want to define almost all the functions that you might need, and this often includes memory allocation functions ( bignum_new ), memory deallocation functions ( bignum_free ), and initialization procedures ( bignum_init ). Even if you don’t need them now, do it in advance to determine when the code should grow and develop later.

+4
source

Source: https://habr.com/ru/post/1400820/


All Articles