Arduino Cryptography Library
|
Utilities to assist with implementing big number arithmetic. More...
#include <BigNumberUtil.h>
Static Public Member Functions | |
static void | unpackLE (limb_t *limbs, size_t count, const uint8_t *bytes, size_t len) |
Unpacks the little-endian byte representation of a big number into a limb array. More... | |
static void | unpackBE (limb_t *limbs, size_t count, const uint8_t *bytes, size_t len) |
Unpacks the big-endian byte representation of a big number into a limb array. More... | |
static void | packLE (uint8_t *bytes, size_t len, const limb_t *limbs, size_t count) |
Packs the little-endian byte representation of a big number into a byte array. More... | |
static void | packBE (uint8_t *bytes, size_t len, const limb_t *limbs, size_t count) |
Packs the big-endian byte representation of a big number into a byte array. More... | |
static limb_t | add (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Adds two big numbers. More... | |
static limb_t | sub (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Subtracts one big number from another. More... | |
static void | mul (limb_t *result, const limb_t *x, size_t xcount, const limb_t *y, size_t ycount) |
Multiplies two big numbers. More... | |
static void | reduceQuick (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Reduces x modulo y using subtraction. More... | |
static limb_t | add_P (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Adds two big numbers where one of them is in program memory. More... | |
static limb_t | sub_P (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Subtracts one big number from another where one is in program memory. More... | |
static void | mul_P (limb_t *result, const limb_t *x, size_t xcount, const limb_t *y, size_t ycount) |
Multiplies two big numbers where one is in program memory. More... | |
static void | reduceQuick_P (limb_t *result, const limb_t *x, const limb_t *y, size_t size) |
Reduces x modulo y using subtraction where y is in program memory. More... | |
static limb_t | isZero (const limb_t *x, size_t size) |
Determine if a big number is zero. More... | |
Utilities to assist with implementing big number arithmetic.
Big numbers are represented as arrays of limb_t words, which may be 8 bits, 16 bits, or 32 bits in size depending upon how the library was configured. For AVR, 16 bit limbs usually give the best performance.
Limb arrays are ordered from the least significant word to the most significant.
Definition at line 72 of file BigNumberUtil.h.
|
static |
Adds two big numbers.
result | The result of the addition. This can be the same as either x or y. |
x | The first big number. |
y | The second big number. |
size | The size of the values in limbs. |
Definition at line 495 of file BigNumberUtil.cpp.
|
static |
Adds two big numbers where one of them is in program memory.
result | The result of the addition. This can be the same as x. |
x | The first big number. |
y | The second big number. This must point into program memory. |
size | The size of the values in limbs. |
Definition at line 628 of file BigNumberUtil.cpp.
|
static |
Determine if a big number is zero.
x | Points to the number to test. |
size | The number of limbs in x. |
This function attempts to make the determination in constant time.
Definition at line 761 of file BigNumberUtil.cpp.
|
static |
Multiplies two big numbers.
result | The result of the multiplication. The array must be xcount + ycount limbs in size. |
x | Points to the first value to multiply. |
xcount | The number of limbs in x. |
y | Points to the second value to multiply. |
ycount | The number of limbs in y. |
Definition at line 546 of file BigNumberUtil.cpp.
|
static |
Multiplies two big numbers where one is in program memory.
result | The result of the multiplication. The array must be xcount + ycount limbs in size. |
x | Points to the first value to multiply. |
xcount | The number of limbs in x. |
y | Points to the second value to multiply. This must point into program memory. |
ycount | The number of limbs in y. |
Definition at line 680 of file BigNumberUtil.cpp.
|
static |
Packs the big-endian byte representation of a big number into a byte array.
bytes | The byte array to pack into. |
len | The number of bytes in the destination bytes array. |
limbs | The limb array representing the big number, starting with the least significant word. |
count | The number of elements in the limbs array. |
If len is shorter than the length of limbs, then the number will be truncated to the least significant len bytes. If len is longer than the length of limbs, then the high bytes will be filled with zeroes.
Definition at line 375 of file BigNumberUtil.cpp.
|
static |
Packs the little-endian byte representation of a big number into a byte array.
bytes | The byte array to pack into. |
len | The number of bytes in the destination bytes array. |
limbs | The limb array representing the big number, starting with the least significant word. |
count | The number of elements in the limbs array. |
If len is shorter than the length of limbs, then the number will be truncated to the least significant len bytes. If len is longer than the length of limbs, then the high bytes will be filled with zeroes.
Definition at line 264 of file BigNumberUtil.cpp.
|
static |
Reduces x modulo y using subtraction.
result | The result of the reduction. This can be the same as x. |
x | The number to be reduced. |
y | The base to use for the modulo reduction. |
size | The size of the values in limbs. |
It is assumed that x is less than y * 2 so that a single conditional subtraction will bring it down below y. The reduction is performed in constant time.
Definition at line 598 of file BigNumberUtil.cpp.
|
static |
Reduces x modulo y using subtraction where y is in program memory.
result | The result of the reduction. This can be the same as x. |
x | The number to be reduced. |
y | The base to use for the modulo reduction. This must point into program memory. |
size | The size of the values in limbs. |
It is assumed that x is less than y * 2 so that a single conditional subtraction will bring it down below y. The reduction is performed in constant time.
Definition at line 734 of file BigNumberUtil.cpp.
|
static |
Subtracts one big number from another.
result | The result of the subtraction. This can be the same as either x or y. |
x | The first big number. |
y | The second big number to subtract from x. |
size | The size of the values in limbs. |
Definition at line 522 of file BigNumberUtil.cpp.
|
static |
Subtracts one big number from another where one is in program memory.
result | The result of the subtraction. This can be the same as x. |
x | The first big number. |
y | The second big number to subtract from x. This must point into program memory. |
size | The size of the values in limbs. |
Definition at line 655 of file BigNumberUtil.cpp.
|
static |
Unpacks the big-endian byte representation of a big number into a limb array.
limbs | The limb array, starting with the least significant word. |
count | The number of elements in the limbs array. |
bytes | The bytes to unpack. |
len | The number of bytes to unpack. |
If len is shorter than the length of limbs, then the high bytes will be filled with zeroes. If len is longer than the length of limbs, then the high bytes will be truncated and lost.
Definition at line 163 of file BigNumberUtil.cpp.
|
static |
Unpacks the little-endian byte representation of a big number into a limb array.
limbs | The limb array, starting with the least significant word. |
count | The number of elements in the limbs array. |
bytes | The bytes to unpack. |
len | The number of bytes to unpack. |
If len is shorter than the length of limbs, then the high bytes will be filled with zeroes. If len is longer than the length of limbs, then the high bytes will be truncated and lost.
Definition at line 55 of file BigNumberUtil.cpp.