projection
projection

Reputation: 151

custom data type in C

I am working with cryptography and need to use some really large numbers. I am also using the new Intel instruction for carryless multiplication that requires m128i data type which is done by loading it with a function that takes in floating point data as its arguments.

I need to store 2^1223 integer and then square it and store that value as well.

I know I can use the GMP library but I think it would be faster to create two data types that both store values like 2^1224 and 2^2448. It will have less overhead.I am going to using karatsuba to multiply the numbers so the only operation I need to perform on the data type is addition as I will be breaking the number down to fit m128i.

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

Upvotes: 13

Views: 26702

Answers (1)

Wes Hardaker
Wes Hardaker

Reputation: 22262

If you need your own datatypes (regardless of whether it's for math, etc), you'll need to fall back to structures and functions. For example:

struct bignum_s {
    char bignum_data[1024];
}

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

Most people end up typedefing it as well:

typedef struct bignum_s bignum;

And then create functions that take two (or whatever) pointers to the 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 end up defining nearly every function you might need, and this frequently includes memory allocation functions (bignum_new), memory freeing functions (bignum_free) and init routines (bignum_init). Even if you don't need them now, doing it in advance will set you up for when the code needs to grow and develop later.

Upvotes: 13

Related Questions