Friday, December 16, 2016

Numbers larger than 64-bits in BASH

Necessity is the mother of invention. When dealing with number theory, particularly cryptography, you have large numbers. I have many tools that I have written to support my semiconductor work, and many of these output "large numbers". BASH has limitations to numbers size. I have created a BASH library that allows me to do arbitrary bit-width operations on binary representations of strings.

The first thing that is required is my "bashbignumbers.sh" function library. You then can do simple arithmetic. In the following example, I create a 4096 bit number that is full of "0"s. I then increment the number, negate it, and convert it back into hex.
source "bashbignumbers.sh"
RESULT=$(bashUTILzerowidth 4096)
RESULT=$(bashINCbinstring $RESULT)
RESULT=$(bashNEGbinstring $RESULT)
RESULT=$(bashUTILbin2hex $RESULT)
echo "$RESULT"
The result is:
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
And that is the 2's compliment representation of -1

No comments:

Post a Comment