[crypto] Add bigint_msb_is_set() to clarify code

Add a dedicated bigint_msb_is_set() to reduce the amount of open
coding required in the common case of testing the sign of a two's
complement big integer.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2024-11-07 14:43:56 +00:00
parent e9a23a5b39
commit da6da6eb3b
3 changed files with 30 additions and 5 deletions

View File

@@ -162,14 +162,13 @@ void bigint_reduce_raw ( const bigint_element_t *minuend0,
bigint_t ( minuend_size ) modulus;
} *temp = tmp;
const unsigned int width = ( 8 * sizeof ( bigint_element_t ) );
const bigint_element_t msb_mask = ( 1UL << ( width - 1 ) );
bigint_element_t *element;
unsigned int minuend_max;
unsigned int modulus_max;
unsigned int subshift;
bigint_element_t msb;
int offset;
int shift;
int msb;
int i;
/* Start profiling */
@@ -289,7 +288,7 @@ void bigint_reduce_raw ( const bigint_element_t *minuend0,
} else {
bigint_subtract ( &temp->modulus, &temp->minuend );
}
msb = ( temp->minuend.element[ minuend_size - 1 ] & msb_mask );
msb = bigint_msb_is_set ( &temp->minuend );
if ( shift > 0 )
bigint_shr ( &temp->modulus );
}

View File

@@ -563,7 +563,6 @@ void x25519_invert ( const union x25519_oct258 *invertend,
* @v value Big integer to be subtracted from, if possible
*/
static void x25519_reduce_by ( const x25519_t *subtrahend, x25519_t *value ) {
unsigned int max_bit = ( ( 8 * sizeof ( *value ) ) - 1 );
x25519_t tmp;
/* Conditionally subtract subtrahend
@@ -573,7 +572,7 @@ static void x25519_reduce_by ( const x25519_t *subtrahend, x25519_t *value ) {
*/
bigint_copy ( value, &tmp );
bigint_subtract ( subtrahend, value );
bigint_swap ( value, &tmp, bigint_bit_is_set ( value, max_bit ) );
bigint_swap ( value, &tmp, bigint_msb_is_set ( value ) );
}
/**