mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-05-19 11:22:16 +00:00
Use less memory in fix_negative()
This commit is contained in:
parent
cae6f3ed45
commit
b21c81fb41
|
@ -1736,12 +1736,20 @@ static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helpers for the main 'loop'
|
* Helpers for the main 'loop'
|
||||||
|
* (see fix_negative for the motivation of C)
|
||||||
*/
|
*/
|
||||||
#define INIT( b ) \
|
#define INIT( b ) \
|
||||||
int ret; \
|
int ret; \
|
||||||
signed char c = 0, cc; \
|
signed char c = 0, cc; \
|
||||||
uint32_t cur; \
|
uint32_t cur; \
|
||||||
size_t i = 0, bits = b; \
|
size_t i = 0, bits = b; \
|
||||||
|
mpi C; \
|
||||||
|
t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ]; \
|
||||||
|
\
|
||||||
|
C.s = 1; \
|
||||||
|
C.n = b / 8 / sizeof( t_uint) + 1; \
|
||||||
|
C.p = Cp; \
|
||||||
|
memset( Cp, 0, C.n * sizeof( t_uint ) ); \
|
||||||
\
|
\
|
||||||
MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
|
MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
|
||||||
LOAD32;
|
LOAD32;
|
||||||
|
@ -1758,32 +1766,28 @@ static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry )
|
||||||
STORE32; i++; \
|
STORE32; i++; \
|
||||||
cur = c > 0 ? c : 0; STORE32; \
|
cur = c > 0 ? c : 0; STORE32; \
|
||||||
cur = 0; while( ++i < MAX32 ) { STORE32; } \
|
cur = 0; while( ++i < MAX32 ) { STORE32; } \
|
||||||
if( c < 0 ) fix_negative( N, c, bits );
|
if( c < 0 ) fix_negative( N, c, &C, bits );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If the result is negative, we get it in the form
|
* If the result is negative, we get it in the form
|
||||||
* c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
|
* c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
|
||||||
*/
|
*/
|
||||||
static inline int fix_negative( mpi *N, signed char c, size_t bits )
|
static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
mpi C;
|
|
||||||
t_uint Cp[ 384 / 8 / sizeof( t_uint) + 1 ];
|
|
||||||
|
|
||||||
/* C = - c * 2^(bits + 32) */
|
/* C = - c * 2^(bits + 32) */
|
||||||
C.s = 1;
|
#if !defined(POLARSSL_HAVE_INT64)
|
||||||
C.n = bits / 8 / sizeof( t_uint ) + 1;
|
((void) bits);
|
||||||
C.p = Cp;
|
#else
|
||||||
memset( Cp, 0, C.n * sizeof( t_uint ) );
|
|
||||||
#if defined(POLARSSL_HAVE_INT64)
|
|
||||||
if( bits == 224 )
|
if( bits == 224 )
|
||||||
Cp[ C.n - 1 ] = ((t_uint) -c) << 32;
|
C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
Cp[ C.n - 1 ] = (t_uint) -c;
|
C->p[ C->n - 1 ] = (t_uint) -c;
|
||||||
|
|
||||||
/* N = - ( C - N ) */
|
/* N = - ( C - N ) */
|
||||||
MPI_CHK( mpi_sub_abs( N, &C, N ) );
|
MPI_CHK( mpi_sub_abs( N, C, N ) );
|
||||||
N->s = -1;
|
N->s = -1;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
|
Loading…
Reference in a new issue