[crypto] Separate out bigint_reduce() from bigint_mod_multiply()

Faster modular multiplication algorithms such as Montgomery
multiplication will still require the ability to perform a single
direct modular reduction.

Neaten up the implementation of direct reduction and split it out into
a separate bigint_reduce() function, complete with its own unit tests.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-10-15 13:50:51 +01:00
parent f78c5a763c
commit 2bf16c6ffc
3 changed files with 296 additions and 37 deletions

View File

@@ -217,6 +217,35 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
multiplier_size, (result)->element ); \
} while ( 0 )
/**
* Reduce big integer
*
* @v minuend Big integer to be reduced
* @v modulus Big integer modulus
* @v result Big integer to hold result
* @v tmp Temporary working space
*/
#define bigint_reduce( minuend, modulus, result, tmp ) do { \
unsigned int minuend_size = bigint_size (minuend); \
unsigned int modulus_size = bigint_size (modulus); \
bigint_reduce_raw ( (minuend)->element, minuend_size, \
(modulus)->element, modulus_size, \
(result)->element, tmp ); \
} while ( 0 )
/**
* Calculate temporary working space required for reduction
*
* @v minuend Big integer to be reduced
* @ret len Length of temporary working space
*/
#define bigint_reduce_tmp_len( minuend ) ( { \
unsigned int size = bigint_size (minuend); \
sizeof ( struct { \
bigint_t ( size ) temp_minuend; \
bigint_t ( size ) temp_modulus; \
} ); } )
/**
* Perform modular multiplication of big integers
*
@@ -339,6 +368,11 @@ void bigint_multiply_raw ( const bigint_element_t *multiplicand0,
const bigint_element_t *multiplier0,
unsigned int multiplier_size,
bigint_element_t *result0 );
void bigint_reduce_raw ( const bigint_element_t *minuend0,
unsigned int minuend_size,
const bigint_element_t *modulus0,
unsigned int modulus_size,
bigint_element_t *result0, void *tmp );
void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
const bigint_element_t *multiplier0,
const bigint_element_t *modulus0,