bignum_mod_raw: Added modular negation.

This patch adds the `mpi_mod_raw_neg()` method.

Co-authored-by: Hanno Becker <hanno.becker@arm.com>
Co-authored-by: Minos Galanakis <minos.galanakis@arm.com>

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Minos Galanakis 2022-12-07 18:06:05 +00:00
parent 98be95563d
commit 21fe8bdeac
2 changed files with 30 additions and 0 deletions

View file

@ -182,6 +182,18 @@ int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
mbedtls_free( T ); mbedtls_free( T );
return( 0 ); return( 0 );
} }
void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *A,
const mbedtls_mpi_mod_modulus *m )
{
mbedtls_mpi_core_sub( X, m->p, A, m->limbs );
/* If A=0 initially, then X=N now. Detect this by
* subtracting N and catching the carry. */
mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, X, m->p, m->limbs );
(void) mbedtls_mpi_core_add_if( X, m->p, m->limbs, (unsigned) borrow );
}
/* END MERGE SLOT 7 */ /* END MERGE SLOT 7 */
/* BEGIN MERGE SLOT 8 */ /* BEGIN MERGE SLOT 8 */

View file

@ -233,6 +233,24 @@ int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
*/ */
int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
const mbedtls_mpi_mod_modulus *m ); const mbedtls_mpi_mod_modulus *m );
/** \brief Perform fixed width modular negation.
*
* The size of the operation is determined by \p N. \p A must have
* the same number of limbs as \p N.
*
* \p X may be aliased to \p A.
*
* \param[out] X The result of the modular negation.
* This must be initialized. Must have enough limbs to
* store the full value of the result.
* \param[in] A Little-endian presentation of the input operand. This
* must be smaller or equal to \p N.
* \param[in] m The address of the modulus related to \p A.
*/
void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *A,
const mbedtls_mpi_mod_modulus *m);
/* END MERGE SLOT 7 */ /* END MERGE SLOT 7 */
/* BEGIN MERGE SLOT 8 */ /* BEGIN MERGE SLOT 8 */