Skip to content

Commit

Permalink
Tidy up a bit, removing the MPI_CORE() macro
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Cosgrove <[email protected]>
  • Loading branch information
tom-cosgrove-arm committed Jul 11, 2022
1 parent ee48106 commit 122cce3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
38 changes: 18 additions & 20 deletions library/bignum_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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 */
48 changes: 24 additions & 24 deletions library/bignum_new.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@

#include <string.h>

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++ )
{
Expand All @@ -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 )
Expand Down Expand Up @@ -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;

Expand All @@ -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++ )
Expand Down

0 comments on commit 122cce3

Please sign in to comment.