Tidy up, remove MPI_CORE(), apply the naming convention, and use the new mbedtls_mpi_core_add()
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
diff --git a/library/bignum.c b/library/bignum.c
index 58cd2f7..f30df2b 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -867,8 +867,7 @@
int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
- size_t i, j;
- mbedtls_mpi_uint *o, *p, c, tmp;
+ size_t j;
MPI_VALIDATE_RET( X != NULL );
MPI_VALIDATE_RET( A != NULL );
MPI_VALIDATE_RET( B != NULL );
@@ -882,7 +881,7 @@
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
/*
- * X should always be positive as a result of unsigned additions.
+ * X must always be positive as a result of unsigned additions.
*/
X->s = 1;
@@ -892,27 +891,25 @@
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
- o = B->p; p = X->p; c = 0;
+ /* j is the number of non-zero limbs of B. Add those to X. */
- /*
- * tmp is used because it might happen that p == o
- */
- for( i = 0; i < j; i++, o++, p++ )
- {
- tmp= *o;
- *p += c; c = ( *p < c );
- *p += tmp; c += ( *p < tmp );
- }
+ mbedtls_mpi_uint *p = X->p;
+
+ mbedtls_mpi_uint c = mbedtls_mpi_core_add( p, p, B->p, j);
+
+ p += j;
+
+ /* Now propagate any carry */
while( c != 0 )
{
- if( i >= X->n )
+ if( j >= X->n )
{
- MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
- p = X->p + i;
+ MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j + 1 ) );
+ p = X->p + j;
}
- *p += c; c = ( *p < c ); i++; p++;
+ *p += c; c = ( *p < c ); j++; p++;
}
cleanup:
diff --git a/library/bignum_core.c b/library/bignum_core.c
index 3f4e651..6862316 100644
--- a/library/bignum_core.c
+++ b/library/bignum_core.c
@@ -358,19 +358,22 @@
}
}
-mbedtls_mpi_uint MPI_CORE(add)( mbedtls_mpi_uint *d,
- const mbedtls_mpi_uint *l,
- const mbedtls_mpi_uint *r,
- size_t n )
+mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
+ const mbedtls_mpi_uint *A,
+ const mbedtls_mpi_uint *B,
+ size_t limbs )
{
- mbedtls_mpi_uint c = 0, t;
- for( size_t i = 0; i < n; i++ )
+ mbedtls_mpi_uint c = 0;
+
+ for( size_t i = 0; i < limbs; i++ )
{
- t = c;
- t += l[i]; c = ( t < l[i] );
- t += r[i]; c += ( t < r[i] );
- d[i] = t;
+ mbedtls_mpi_uint t = c + A[i];
+ c = ( t < A[i] );
+ t += B[i];
+ c += ( t < B[i] );
+ X[i] = t;
}
+
return( c );
}
diff --git a/library/bignum_core.h b/library/bignum_core.h
index 0d7b89f..8cc985e 100644
--- a/library/bignum_core.h
+++ b/library/bignum_core.h
@@ -277,28 +277,27 @@
void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
size_t count );
-#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal
-
/**
- * \brief Add two known-size large unsigned integers, returning the carry.
+ * \brief Add two fixed-size large unsigned integers, returning the carry.
*
- * Calculate l + r where l and r have the same size.
- * This function operates modulo (2^ciL)^n and returns the carry
+ * Calculates `A + B` where `A` and `B` have the same size.
+ *
+ * This function operates modulo 2^(biL*limbs) and returns the carry
* (1 if there was a wraparound, and 0 otherwise).
*
- * d may be aliased to l or r.
+ * \p X may be aliased to \p A or \p B.
*
- * \param[out] d The result of the addition.
- * \param[in] l The left operand.
- * \param[in] r The right operand.
- * \param n Number of limbs of \p d, \p l and \p r.
+ * \param[out] X The result of the addition.
+ * \param[in] A Little-endian presentation of the left operand.
+ * \param[in] B Little-endian presentation of the right operand.
+ * \param limbs Number of limbs of \p X, \p A and \p B.
*
- * \return 1 if `l + r >= (2^{ciL})^n`, 0 otherwise.
+ * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
*/
-mbedtls_mpi_uint MPI_CORE(add)( mbedtls_mpi_uint *d,
- const mbedtls_mpi_uint *l,
- const mbedtls_mpi_uint *r,
- size_t n );
+mbedtls_mpi_uint mbedtls_mpi_core_add( mbedtls_mpi_uint *X,
+ const mbedtls_mpi_uint *A,
+ const mbedtls_mpi_uint *B,
+ size_t limbs );
/**
* \brief Conditional addition of two fixed-size large unsigned integers,