how can i store huge values?

R

RubenDV

I am trying to make a cipher with a 256-bit key but i have no idea how
the store this key without using arrays of ints or something like that.
I just need a type that is big enough to hold the entire key, because i
am going to use multiplication operations and such, which i can
impossibly use on arrays or so i think. Is there a way to treat an
array as a single, huge number without really being that? Any help is
appreciated!
 
W

Walter Roberson

I am trying to make a cipher with a 256-bit key but i have no idea how
the store this key without using arrays of ints or something like that.
I just need a type that is big enough to hold the entire key, because i
am going to use multiplication operations and such, which i can
impossibly use on arrays or so i think. Is there a way to treat an
array as a single, huge number without really being that?

Not within standard C.
Any help is appreciated!

You should get one of the portable "bignum" packages that provide
arithmetic operations on arbitrary precision integers.
 
D

David Resnick

RubenDV said:
I am trying to make a cipher with a 256-bit key but i have no idea how
the store this key without using arrays of ints or something like that.
I just need a type that is big enough to hold the entire key, because i
am going to use multiplication operations and such, which i can
impossibly use on arrays or so i think. Is there a way to treat an
array as a single, huge number without really being that? Any help is
appreciated!

General (not C specific) algorithm questions often get better answers
in comp.programming, so you might want to post such there rather than
here. I suggest you google for "bignum library", sounds like that
is what you want.

-David
 
G

Gordon Burditt

I am trying to make a cipher with a 256-bit key but i have no idea how
the store this key without using arrays of ints or something like that.
I just need a type that is big enough to hold the entire key, because i
am going to use multiplication operations and such, which i can
impossibly use on arrays or so i think. Is there a way to treat an
array as a single, huge number without really being that? Any help is
appreciated!

One way to do it is to store the key as an ASCII decimal number in
a 257-byte character array (including '\0' terminator. You can do
the multiplication like you learned to in elementary school, handling
a digit at a time. I didn't say it would be efficient. One advantage
is that the result is already formatted so you can print it.

You can also look at 'bignum' packages, which tend to represent
things as arrays of (unsigned) integers, but the work is done for
you. Many of them work a lot like the decimal number approach,
except they are using base-65536 or base-2**32 or some other large
number of bits which is more efficient.

Gordon L. Burditt
 
R

Richard Bos

RubenDV said:
I am trying to make a cipher with a 256-bit key but i have no idea how
the store this key without using arrays of ints or something like that.
I just need a type that is big enough to hold the entire key, because i
am going to use multiplication operations and such, which i can
impossibly use on arrays or so i think. Is there a way to treat an
array as a single, huge number without really being that?

Yes; primary school mathematics extended to larger bases; in this case,
you're probably doing base-2**32 math instead of base-10. All you need
is an array of 8 uint32_t's (or another typedef for an unsigned integer
you know is 32 bits large (or another known, fixed, size)), and an
understanding of long multiplication.

In primary school, when you were taught to multiply two numbers, say 123
and 234, how did you do that? Like this:

234
345
---*
1170
936
702
-----
80730

Now, instead of digits from 0 to 9, you're multiplying integers from 0
to (2**N)-1, but the principle remains the same. You don't even need the
sub-results; all you need are two source integers, if you wish two
working integers (though at a pinch you can do without), and the result
array.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top