Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store large numbers?

Tags:

I have to make a RSA signature (on a state machine) in C on a 32bit board. I am limited on memory so I can not store decimals in a vector or something like that.

The best thing would be if I could store bits and to have easy access to them; what storage method would be best?

I made this one:

#if (CPU_TYPE == CPU_TYPE_32)

typedef uint32_t word;
#define word_length 32
typedef struct BigNumber {
    word words[64];
} BigNumber;

#elif (CPU_TYPE == CPU_TYPE_16)

typedef uint16_t word;
#define word_length 16
typedef struct BigNumber {
    word words[128];
} BigNumber;

#else  
#error Unsupported CPU_TYPE  
#endif

This seems hard to use. How can I simplify it?

like image 992
Popovici Sebi Avatar asked Aug 24 '16 12:08

Popovici Sebi


People also ask

How big a number can long store?

Longer integers: long The long data type stores integers like int , but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.

Which data structure is used to store very large numbers?

Which among the following data structures is best suited for storing very large numbers (numbers that cannot be stored in long long int). Following are the operations needed for these large numbers. Explanation: The only two choices that make sense are Array and Linked List.

Which type should you use to hold a value that can be extremely large?

Large Integers If you need to hold an integer larger than the Integer data type can hold, you can use the Long data type instead. Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.

How do you handle large numbers in C++?

In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.


1 Answers

You may simply use the BigNumber API from OpenSSL. You can find the full API here.

And, you can use this code sample as a start:

#include <stdio.h>

#include <openssl/crypto.h>
#include <openssl/bn.h>

int main(int argc, char *argv[])
{
  static const char num1[] = "18446744073709551616";
  static const char num2[] = "36893488147419103232";

  BIGNUM *bn1 = NULL;
  BIGNUM *bn2 = NULL;
  BN_CTX *ctx = BN_CTX_new();

  BN_dec2bn(&bn1, num1); // convert the string to BIGNUM
  BN_dec2bn(&bn2, num2);

  BN_add(bn1, bn1, bn2); // bn1 = bn1 + bn2

  char *result_str = BN_bn2dec(bn1);  // convert the BIGNUM back to string
  printf("%s + %s = %s\n", num1, num2, result_str);
  OPENSSL_free(result_str);

  BN_free(bn1);
  BN_free(bn2);
  BN_CTX_free(ctx);

  return 0;
}

Compile it with:

#> gcc -Wall -Wextra -g -o sample sample.c -lcrypto

You should get something like this when executing it:

18446744073709551616 + 36893488147419103232 = 55340232221128654848
like image 103
perror Avatar answered Oct 11 '22 03:10

perror