custom data type in C
Asked Answered
D

1

13

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.

Dramaturgy answered 11/3, 2012 at 11:46 Comment(1)
Do you think that code you've rolled yourself is going to be faster than code that has been already been highly highly optimised (and debugged!)? (Although, if you are doing this out of interest, continue as you were. :) )Filtrate
T
13

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.

Transonic answered 11/3, 2012 at 15:10 Comment(1)
The answer has been updated, so now bignum_or uses pass-by-reference.Lumpkin

© 2022 - 2024 — McMap. All rights reserved.