From 2bf16c6ffca1e294bb8233d19c9c36e43b31f041 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 15 Oct 2024 13:50:51 +0100 Subject: [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 --- src/include/ipxe/bigint.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/include') diff --git a/src/include/ipxe/bigint.h b/src/include/ipxe/bigint.h index c556afbc1..c56b2155f 100644 --- a/src/include/ipxe/bigint.h +++ b/src/include/ipxe/bigint.h @@ -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, -- cgit v1.2.3-55-g7522