[crypto] Profile the various stages of modular multiplication

Signed-off-by: Michael Brown <mcb30@ipxe.org>
pull/94/head
Michael Brown 2019-08-17 01:24:04 +01:00
parent 131635eac0
commit 0cc12f053c
1 changed files with 29 additions and 0 deletions

View File

@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <ipxe/profile.h>
#include <ipxe/bigint.h> #include <ipxe/bigint.h>
/** @file /** @file
@ -33,6 +34,22 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
* Big integer support * Big integer support
*/ */
/** Modular multiplication overall profiler */
static struct profiler bigint_mod_multiply_profiler __profiler =
{ .name = "bigint_mod_multiply" };
/** Modular multiplication multiply step profiler */
static struct profiler bigint_mod_multiply_multiply_profiler __profiler =
{ .name = "bigint_mod_multiply.multiply" };
/** Modular multiplication rescale step profiler */
static struct profiler bigint_mod_multiply_rescale_profiler __profiler =
{ .name = "bigint_mod_multiply.rescale" };
/** Modular multiplication subtract step profiler */
static struct profiler bigint_mod_multiply_subtract_profiler __profiler =
{ .name = "bigint_mod_multiply.subtract" };
/** /**
* Perform modular multiplication of big integers * Perform modular multiplication of big integers
* *
@ -63,31 +80,43 @@ void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
int rotation; int rotation;
int i; int i;
/* Start profiling */
profile_start ( &bigint_mod_multiply_profiler );
/* Sanity check */ /* Sanity check */
assert ( sizeof ( *temp ) == bigint_mod_multiply_tmp_len ( modulus ) ); assert ( sizeof ( *temp ) == bigint_mod_multiply_tmp_len ( modulus ) );
/* Perform multiplication */ /* Perform multiplication */
profile_start ( &bigint_mod_multiply_multiply_profiler );
bigint_multiply ( multiplicand, multiplier, &temp->result ); bigint_multiply ( multiplicand, multiplier, &temp->result );
profile_stop ( &bigint_mod_multiply_multiply_profiler );
/* Rescale modulus to match result */ /* Rescale modulus to match result */
profile_start ( &bigint_mod_multiply_rescale_profiler );
bigint_grow ( modulus, &temp->modulus ); bigint_grow ( modulus, &temp->modulus );
rotation = ( bigint_max_set_bit ( &temp->result ) - rotation = ( bigint_max_set_bit ( &temp->result ) -
bigint_max_set_bit ( &temp->modulus ) ); bigint_max_set_bit ( &temp->modulus ) );
for ( i = 0 ; i < rotation ; i++ ) for ( i = 0 ; i < rotation ; i++ )
bigint_rol ( &temp->modulus ); bigint_rol ( &temp->modulus );
profile_stop ( &bigint_mod_multiply_rescale_profiler );
/* Subtract multiples of modulus */ /* Subtract multiples of modulus */
profile_start ( &bigint_mod_multiply_subtract_profiler );
for ( i = 0 ; i <= rotation ; i++ ) { for ( i = 0 ; i <= rotation ; i++ ) {
if ( bigint_is_geq ( &temp->result, &temp->modulus ) ) if ( bigint_is_geq ( &temp->result, &temp->modulus ) )
bigint_subtract ( &temp->modulus, &temp->result ); bigint_subtract ( &temp->modulus, &temp->result );
bigint_ror ( &temp->modulus ); bigint_ror ( &temp->modulus );
} }
profile_stop ( &bigint_mod_multiply_subtract_profiler );
/* Resize result */ /* Resize result */
bigint_shrink ( &temp->result, result ); bigint_shrink ( &temp->result, result );
/* Sanity check */ /* Sanity check */
assert ( bigint_is_geq ( modulus, result ) ); assert ( bigint_is_geq ( modulus, result ) );
/* Stop profiling */
profile_stop ( &bigint_mod_multiply_profiler );
} }
/** /**