From 122cce3a3f9995da670cfc74dbf441d52f2b8ed1 Mon Sep 17 00:00:00 2001 From: Tom Cosgrove Date: Mon, 11 Jul 2022 15:24:19 +0100 Subject: [PATCH] Tidy up a bit, removing the MPI_CORE() macro Signed-off-by: Tom Cosgrove --- library/bignum_core.h | 38 ++++++++++++++++------------------ library/bignum_new.c | 48 +++++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/library/bignum_core.h b/library/bignum_core.h index 71bb266907a4..1c9a7cdecee6 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -26,6 +26,8 @@ #include "mbedtls/bignum.h" #endif +#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ + /** Perform a known-size multiply accumulate operation * * Add \p b * \p s to \p d. @@ -47,10 +49,6 @@ mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, const mbedtls_mpi_uint *s, size_t s_len, mbedtls_mpi_uint b ); -#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ - -#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal - /** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36) * * \param[in,out] A Big endian presentation of first operand. @@ -69,11 +67,11 @@ mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, * Its initial content is unused and * its final content is indeterminate. */ -void MPI_CORE(montmul)( mbedtls_mpi_uint *X, - const mbedtls_mpi_uint *A, const mbedtls_mpi_uint *B, - size_t B_len, const mbedtls_mpi_uint *N, - size_t n, mbedtls_mpi_uint mm, - mbedtls_mpi_uint *T ); +void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, const mbedtls_mpi_uint *B, + size_t B_len, const mbedtls_mpi_uint *N, + size_t n, mbedtls_mpi_uint mm, + mbedtls_mpi_uint *T ); /** * \brief Perform a known-size multiply accumulate operation @@ -93,9 +91,9 @@ void MPI_CORE(montmul)( mbedtls_mpi_uint *X, * * \return c The carry at the end of the operation. */ -mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len , - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ); +mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len , + const mbedtls_mpi_uint *s, size_t s_len, + mbedtls_mpi_uint b ); /** * \brief Subtract two known-size large unsigned integers, returning the borrow. @@ -114,10 +112,10 @@ mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len , * \return 1 if `l < r`. * 0 if `l >= r`. */ -mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *l, - const mbedtls_mpi_uint *r, - size_t n ); +mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ); /** * \brief Constant-time conditional addition of two known-size large unsigned @@ -142,9 +140,9 @@ mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, * * \return 1 if `d + cond*r >= (2^{ciL})^n`, 0 otherwise. */ -mbedtls_mpi_uint MPI_CORE(add_if)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *r, - size_t n, - unsigned cond ); +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *r, + size_t n, + unsigned cond ); #endif /* MBEDTLS_BIGNUM_CORE_H */ diff --git a/library/bignum_new.c b/library/bignum_new.c index 29c12126650f..0aff9fba4e98 100644 --- a/library/bignum_new.c +++ b/library/bignum_new.c @@ -27,16 +27,16 @@ #include -void MPI_CORE(montmul)( mbedtls_mpi_uint *X, - const mbedtls_mpi_uint *A, - const mbedtls_mpi_uint *B, - size_t B_len, - const mbedtls_mpi_uint *N, - size_t n, - mbedtls_mpi_uint mm, - mbedtls_mpi_uint *T ) +void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t B_len, + const mbedtls_mpi_uint *N, + size_t n, + mbedtls_mpi_uint mm, + mbedtls_mpi_uint *T ) { - memset( T, 0, (2*n+1)*ciL ); + memset( T, 0, (2 * n + 1) * ciL ); for( size_t i = 0; i < n; i++, T++ ) { @@ -45,21 +45,21 @@ void MPI_CORE(montmul)( mbedtls_mpi_uint *X, u0 = A[i]; u1 = ( T[0] + u0 * B[0] ) * mm; - (void) MPI_CORE(mla)( T, n + 2, B, B_len, u0 ); - (void) MPI_CORE(mla)( T, n + 2, N, n, u1 ); + (void) mbedtls_mpi_core_mla( T, n + 2, B, B_len, u0 ); + (void) mbedtls_mpi_core_mla( T, n + 2, N, n, u1 ); } mbedtls_mpi_uint carry, borrow, fixup; carry = T[n]; - borrow = MPI_CORE(sub)( X, T, N, n ); + borrow = mbedtls_mpi_core_sub( X, T, N, n ); fixup = carry < borrow; - (void) MPI_CORE(add_if)( X, N, n, fixup ); + (void) mbedtls_mpi_core_add_if( X, N, n, fixup ); } -mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len, - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ) +mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, + const mbedtls_mpi_uint *s, size_t s_len, + mbedtls_mpi_uint b ) { mbedtls_mpi_uint c = 0; /* carry */ if( d_len < s_len ) @@ -90,10 +90,10 @@ mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len, return( c ); } -mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *l, - const mbedtls_mpi_uint *r, - size_t n ) +mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ) { mbedtls_mpi_uint c = 0, t, z; @@ -106,10 +106,10 @@ mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, return( c ); } -mbedtls_mpi_uint MPI_CORE(add_if)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *r, - size_t n, - unsigned cond ) +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *r, + size_t n, + unsigned cond ) { mbedtls_mpi_uint c = 0, t; for( size_t i = 0; i < n; i++ )