iPXE
|
Big integer support. More...
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <ipxe/bigint.h>
Go to the source code of this file.
Macros | |
#define | BIGINT_NTOA_LSB_MIN 16 |
Minimum number of least significant bytes included in transcription. More... | |
Functions | |
FILE_LICENCE (GPL2_OR_LATER_OR_UBDL) | |
const char * | bigint_ntoa_raw (const bigint_element_t *value0, unsigned int size) |
Transcribe big integer (for debugging) More... | |
void | bigint_swap_raw (bigint_element_t *first0, bigint_element_t *second0, unsigned int size, int swap) |
Conditionally swap big integers (in constant time) More... | |
void | bigint_multiply_raw (const bigint_element_t *multiplicand0, unsigned int multiplicand_size, const bigint_element_t *multiplier0, unsigned int multiplier_size, bigint_element_t *result0) |
Multiply big integers. More... | |
void | bigint_reduce_raw (const bigint_element_t *modulus0, bigint_element_t *result0, unsigned int size) |
Reduce big integer R^2 modulo N. More... | |
void | bigint_mod_invert_raw (const bigint_element_t *invertend0, bigint_element_t *inverse0, unsigned int size) |
Compute inverse of odd big integer modulo any power of two. More... | |
int | bigint_montgomery_relaxed_raw (const bigint_element_t *modulus0, bigint_element_t *value0, bigint_element_t *result0, unsigned int size) |
Perform relaxed Montgomery reduction (REDC) of a big integer. More... | |
void | bigint_montgomery_raw (const bigint_element_t *modulus0, bigint_element_t *value0, bigint_element_t *result0, unsigned int size) |
Perform classic Montgomery reduction (REDC) of a big integer. More... | |
void | bigint_ladder_raw (bigint_element_t *result0, bigint_element_t *multiple0, unsigned int size, const bigint_element_t *exponent0, unsigned int exponent_size, bigint_ladder_op_t *op, const void *ctx, void *tmp) |
Perform generalised exponentiation via a Montgomery ladder. More... | |
void | bigint_mod_exp_ladder (const bigint_element_t *multiplier0, bigint_element_t *result0, unsigned int size, const void *ctx, void *tmp) |
Perform modular multiplication as part of a Montgomery ladder. More... | |
void | bigint_mod_exp_raw (const bigint_element_t *base0, const bigint_element_t *modulus0, const bigint_element_t *exponent0, bigint_element_t *result0, unsigned int size, unsigned int exponent_size, void *tmp) |
Perform modular exponentiation of big integers. More... | |
Big integer support.
Definition in file bigint.c.
#define BIGINT_NTOA_LSB_MIN 16 |
FILE_LICENCE | ( | GPL2_OR_LATER_OR_UBDL | ) |
const char* bigint_ntoa_raw | ( | const bigint_element_t * | value0, |
unsigned int | size | ||
) |
Transcribe big integer (for debugging)
value0 | Element 0 of big integer to be transcribed |
size | Number of elements |
string | Big integer in string form (may be abbreviated) |
Definition at line 47 of file bigint.c.
References __attribute__, assert(), BIGINT_NTOA_LSB_MIN, bigint_t(), count, element, size, sprintf, tmp, value, and value0.
void bigint_swap_raw | ( | bigint_element_t * | first0, |
bigint_element_t * | second0, | ||
unsigned int | size, | ||
int | swap | ||
) |
void bigint_multiply_raw | ( | const bigint_element_t * | multiplicand0, |
unsigned int | multiplicand_size, | ||
const bigint_element_t * | multiplier0, | ||
unsigned int | multiplier_size, | ||
bigint_element_t * | result0 | ||
) |
Multiply big integers.
multiplicand0 | Element 0 of big integer to be multiplied |
multiplicand_size | Number of elements in multiplicand |
multiplier0 | Element 0 of big integer to be multiplied |
multiplier_size | Number of elements in multiplier |
result0 | Element 0 of big integer to hold result |
Definition at line 117 of file bigint.c.
References __attribute__, bigint_multiply_one(), bigint_t(), memset(), multiplier, and result.
void bigint_reduce_raw | ( | const bigint_element_t * | modulus0, |
bigint_element_t * | result0, | ||
unsigned int | size | ||
) |
Reduce big integer R^2 modulo N.
modulus0 | Element 0 of big integer modulus |
result0 | Element 0 of big integer to hold result |
size | Number of elements in modulus and result |
Reduce the value R^2 modulo N, where R=2^n and n is the number of bits in the representation of the modulus N, including any leading zero bits.
Definition at line 188 of file bigint.c.
References __attribute__, assert(), bigint_add, bigint_is_geq, bigint_max_set_bit, bigint_set_bit, bigint_shl, bigint_subtract, bigint_t(), carry, max, memset(), result, and size.
void bigint_mod_invert_raw | ( | const bigint_element_t * | invertend0, |
bigint_element_t * | inverse0, | ||
unsigned int | size | ||
) |
Compute inverse of odd big integer modulo any power of two.
invertend0 | Element 0 of odd big integer to be inverted |
inverse0 | Element 0 of big integer to hold result |
size | Number of elements in invertend and result |
Definition at line 317 of file bigint.c.
References __attribute__, assert(), bigint_add, bigint_bit_is_set, bigint_shr, bigint_t(), bit, memset(), and size.
int bigint_montgomery_relaxed_raw | ( | const bigint_element_t * | modulus0, |
bigint_element_t * | value0, | ||
bigint_element_t * | result0, | ||
unsigned int | size | ||
) |
Perform relaxed Montgomery reduction (REDC) of a big integer.
modulus0 | Element 0 of big integer odd modulus |
value0 | Element 0 of big integer to be reduced |
result0 | Element 0 of big integer to hold result |
size | Number of elements in modulus and result |
carry | Carry out |
The value to be reduced will be made divisible by the size of the modulus while retaining its residue class (i.e. multiples of the modulus will be added until the low half of the value is zero).
The result may be expressed as
tR = x + mN
where x is the input value, N is the modulus, R=2^n (where n is the number of bits in the representation of the modulus, including any leading zero bits), and m is the number of multiples of the modulus added to make the result tR divisible by R.
The maximum addend is mN <= (R-1)*N (and such an m can be proven to exist since N is limited to being odd and therefore coprime to R).
Since the result of this addition is one bit larger than the input value, a carry out bit is also returned. The caller may be able to prove that the carry out is always zero, in which case it may be safely ignored.
The upper half of the output value (i.e. t) will also be copied to the result pointer. It is permissible for the result pointer to overlap the lower half of the input value.
External knowledge of constraints on the modulus and the input value may be used to prove constraints on the result. The constraint on the modulus may be generally expressed as
R > kN
for some positive integer k. The value k=1 is allowed, and simply expresses that the modulus fits within the number of bits in its own representation.
For classic Montgomery reduction, we have k=1, i.e. R > N and a separate constraint that the input value is in the range x < RN. This gives the result constraint
tR < RN + (R-1)N < 2RN - N < 2RN t < 2N
A single subtraction of the modulus may therefore be required to bring it into the range t < N.
When the input value is known to be a product of two integers A and B, with A < aN and B < bN, we get the result constraint
tR < abN^2 + (R-1)N < (ab/k)RN + RN - N < (1 + ab/k)RN t < (1 + ab/k)N
If we have k=a=b=1, i.e. R > N with A < N and B < N, then the result is in the range t < 2N and may require a single subtraction of the modulus to bring it into the range t < N so that it may be used as an input on a subsequent iteration.
If we have k=4 and a=b=2, i.e. R > 4N with A < 2N and B < 2N, then the result is in the range t < 2N and may immediately be used as an input on a subsequent iteration, without requiring a subtraction.
Larger values of k may be used to allow for larger values of a and b, which can be useful to elide intermediate reductions in a calculation chain that involves additions and subtractions between multiplications (as used in elliptic curve point addition, for example). As a general rule: each intermediate addition or subtraction will require k to be doubled.
When the input value is known to be a single integer A, with A < aN (as used when converting out of Montgomery form), we get the result constraint
tR < aN + (R-1)N < RN + (a-1)N
If we have a=1, i.e. A < N, then the constraint becomes
tR < RN t < N
and so the result is immediately in the range t < N with no subtraction of the modulus required.
For any larger value of a, the result value t=N becomes possible. Additional external knowledge may potentially be used to prove that t=N cannot occur. For example: if the caller is performing modular exponentiation with a prime modulus (or, more generally, a modulus that is coprime to the base), then there is no way for a non-zero base value to end up producing an exact multiple of the modulus. If t=N cannot be disproved, then conversion out of Montgomery form may require an additional subtraction of the modulus.
Definition at line 487 of file bigint.c.
References __attribute__, assert(), bigint_add, bigint_bit_is_set, bigint_copy, bigint_mod_invert, bigint_multiply_one(), bigint_t(), carry, high, low, result, size, value, and value0.
void bigint_montgomery_raw | ( | const bigint_element_t * | modulus0, |
bigint_element_t * | value0, | ||
bigint_element_t * | result0, | ||
unsigned int | size | ||
) |
Perform classic Montgomery reduction (REDC) of a big integer.
modulus0 | Element 0 of big integer odd modulus |
value0 | Element 0 of big integer to be reduced |
result0 | Element 0 of big integer to hold result |
size | Number of elements in modulus and result |
Definition at line 564 of file bigint.c.
References __attribute__, assert(), bigint_is_geq, bigint_montgomery_relaxed, bigint_subtract, bigint_swap, bigint_t(), high, low, result, size, value, and value0.
void bigint_ladder_raw | ( | bigint_element_t * | result0, |
bigint_element_t * | multiple0, | ||
unsigned int | size, | ||
const bigint_element_t * | exponent0, | ||
unsigned int | exponent_size, | ||
bigint_ladder_op_t * | op, | ||
const void * | ctx, | ||
void * | tmp | ||
) |
Perform generalised exponentiation via a Montgomery ladder.
result0 | Element 0 of result (initialised to identity element) |
multiple0 | Element 0 of multiple (initialised to generator) |
size | Number of elements in result and multiple |
exponent0 | Element 0 of exponent |
exponent_size | Number of elements in exponent |
op | Montgomery ladder commutative operation |
ctx | Operation context (if needed) |
tmp | Temporary working space (if needed) |
The Montgomery ladder may be used to perform any operation that is isomorphic to exponentiation, i.e. to compute the result
r = g^e = g * g * g * g * .... * g
for an arbitrary commutative operation "*", generator "g" and exponent "e".
The result "r" is computed in constant time (assuming that the underlying operation is constant time) in k steps, where k is the number of bits in the big integer representation of the exponent.
The result "r" must be initialised to the operation's identity element, and the multiple must be initialised to the generator "g". On exit, the multiple will contain
m = r * g = g^(e+1)
Note that the terminology used here refers to exponentiation defined as repeated multiplication, but that the ladder may equally well be used to perform any isomorphic operation (such as multiplication defined as repeated addition).
Definition at line 631 of file bigint.c.
References __attribute__, bigint_bit_is_set, bigint_swap, bigint_t(), bit, ctx, op, result, size, and tmp.
void bigint_mod_exp_ladder | ( | const bigint_element_t * | multiplier0, |
bigint_element_t * | result0, | ||
unsigned int | size, | ||
const void * | ctx, | ||
void * | tmp | ||
) |
Perform modular multiplication as part of a Montgomery ladder.
operand | Element 0 of first input operand (may overlap result) |
result | Element 0 of second input operand and result |
size | Number of elements in operands and result |
ctx | Operation context (odd modulus, or NULL) |
tmp | Temporary working space |
Definition at line 719 of file bigint.c.
References __attribute__, bigint_montgomery, bigint_multiply, bigint_shrink, bigint_t(), ctx, multiplier, product, result, size, and tmp.
Referenced by bigint_mod_exp_raw(), and weierstrass_multiply().
void bigint_mod_exp_raw | ( | const bigint_element_t * | base0, |
const bigint_element_t * | modulus0, | ||
const bigint_element_t * | exponent0, | ||
bigint_element_t * | result0, | ||
unsigned int | size, | ||
unsigned int | exponent_size, | ||
void * | tmp | ||
) |
Perform modular exponentiation of big integers.
base0 | Element 0 of big integer base |
modulus0 | Element 0 of big integer modulus |
exponent0 | Element 0 of big integer exponent |
result0 | Element 0 of big integer to hold result |
size | Number of elements in base, modulus, and result |
exponent_size | Number of elements in exponent |
tmp | Temporary working space |
Definition at line 751 of file bigint.c.
References __attribute__, assert(), base, bigint_add, bigint_bit_is_set, bigint_copy, bigint_grow, bigint_init, bigint_ladder, bigint_max_set_bit, bigint_mod_exp_ladder(), bigint_mod_exp_tmp_len, bigint_mod_invert, bigint_montgomery, bigint_montgomery_relaxed, bigint_multiply, bigint_reduce, bigint_shr, bigint_subtract, bigint_t(), low, memset(), NULL, product, result, size, and tmp.