Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom data type in C

Tags:

c

types

bignum

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.

like image 556
projection Avatar asked Mar 11 '12 11:03

projection


People also ask

What is custom data types in C?

ANSI C provides three types of data types: Primary(Built-in) Data Types: void , int , char , double , and float . Derived Data Types: Array, References, and Pointers. User Defined Data Types: Structure, Union, and Enumeration.

What is custom data type?

A custom data type (CDT) is a designer-defined data structure that represents a logical grouping of related data, such as Employee and Contract. CDTs can be used to read from and write to a database table, to store information within a process, or to define inputs or outputs of a web service or Appian plug-in.

Can we create own data type in C?

Let us take a look at an example where we define a structure in a C program. In this case, we have created a structure that will further allow us to create a data type of our own.

What are the 5 data types in C?

Types of Data Types in CFloating-point, integer, double, character. Union, structure, array, etc.


1 Answers

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.

like image 162
Wes Hardaker Avatar answered Sep 30 '22 14:09

Wes Hardaker