Apply the function parameter naming convention

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2022-08-24 11:51:58 +01:00
parent f0ffb1585a
commit 72594633a1
2 changed files with 92 additions and 88 deletions

View File

@ -296,35 +296,35 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *A, const mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *B, const mbedtls_mpi_uint *B,
size_t B_len, size_t B_limbs,
const mbedtls_mpi_uint *N, const mbedtls_mpi_uint *N,
size_t n, size_t AN_limbs,
mbedtls_mpi_uint mm, mbedtls_mpi_uint mm,
mbedtls_mpi_uint *T ) mbedtls_mpi_uint *T )
{ {
memset( T, 0, ( 2 * n + 1 ) * ciL ); memset( T, 0, ( 2 * AN_limbs + 1 ) * ciL );
for( size_t i = 0; i < n; i++, T++ ) for( size_t i = 0; i < AN_limbs; i++, T++ )
{ {
mbedtls_mpi_uint u0, u1; mbedtls_mpi_uint u0, u1;
/* T = (T + u0*B + u1*N) / 2^biL */ /* T = (T + u0*B + u1*N) / 2^biL */
u0 = A[i]; u0 = A[i];
u1 = ( T[0] + u0 * B[0] ) * mm; u1 = ( T[0] + u0 * B[0] ) * mm;
(void) mbedtls_mpi_core_mla( T, n + 2, B, B_len, u0 ); (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 );
(void) mbedtls_mpi_core_mla( T, n + 2, N, n, u1 ); (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, N, AN_limbs, u1 );
} }
/* It's possible that the result in T is > N, and so we might need to subtract N */ /* It's possible that the result in T is > N, and so we might need to subtract N */
mbedtls_mpi_uint carry = T[n]; mbedtls_mpi_uint carry = T[AN_limbs];
mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, n ); mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs );
/* /*
* Both carry and borrow can only be 0 or 1. * Both carry and borrow can only be 0 or 1.
* *
* If carry = 1, the result in T must be > N by definition, and the subtraction * If carry = 1, the result in T must be > N by definition, and the subtraction
* using only n limbs will create borrow, but that will have the correct * using only AN_limbs limbs will create borrow, but that will have the correct
* final result. * final result.
* *
* i.e. (carry, borrow) of (1, 1) => return X * i.e. (carry, borrow) of (1, 1) => return X
@ -340,9 +340,9 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
* see (carry, borrow) = (1, 0). * see (carry, borrow) = (1, 0).
* *
* So the correct return value is already in X if (carry ^ borrow) = 0, * So the correct return value is already in X if (carry ^ borrow) = 0,
* but is in (the lower n limbs of) T if (carry ^ borrow) = 1. * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
*/ */
mbedtls_ct_mpi_uint_cond_assign( n, X, T, (unsigned char) ( carry ^ borrow ) ); mbedtls_ct_mpi_uint_cond_assign( AN_limbs, X, T, (unsigned char) ( carry ^ borrow ) );
} }
/* /*
@ -393,37 +393,37 @@ mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
return( c ); return( c );
} }
mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *l, const mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *r, const mbedtls_mpi_uint *B,
size_t n ) size_t limbs )
{ {
mbedtls_mpi_uint c = 0; mbedtls_mpi_uint c = 0;
for( size_t i = 0; i < n; i++ ) for( size_t i = 0; i < limbs; i++ )
{ {
mbedtls_mpi_uint z = ( l[i] < c ); mbedtls_mpi_uint z = ( A[i] < c );
mbedtls_mpi_uint t = l[i] - c; mbedtls_mpi_uint t = A[i] - c;
c = ( t < r[i] ) + z; c = ( t < B[i] ) + z;
d[i] = t - r[i]; X[i] = t - B[i];
} }
return( c ); return( c );
} }
mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *r, const mbedtls_mpi_uint *B,
size_t n, size_t limbs,
unsigned cond ) unsigned cond )
{ {
mbedtls_mpi_uint c = 0, t; mbedtls_mpi_uint c = 0, t;
for( size_t i = 0; i < n; i++ ) for( size_t i = 0; i < limbs; i++ )
{ {
mbedtls_mpi_uint add = cond * r[i]; mbedtls_mpi_uint add = cond * B[i];
t = c; t = c;
t += d[i]; c = ( t < d[i] ); t += A[i]; c = ( t < A[i] );
t += add; c += ( t < add ); t += add; c += ( t < add );
d[i] = t; A[i] = t;
} }
return( c ); return( c );
} }

View File

@ -158,28 +158,28 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
/** /**
* \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
* *
* \param[out] X The destination MPI, as a little-endian array of * \param[out] X The destination MPI, as a little-endian array of
* length \p n. * length \p AN_limbs.
* On successful completion, X contains the result of * On successful completion, X contains the result of
* the multiplication A * B * R^-1 mod N where * the multiplication A * B * R^-1 mod N where
* R = (2^ciL)^n. * R = (2^ciL)^AN_limbs.
* \param[in] A Little-endian presentation of first operand. * \param[in] A Little-endian presentation of first operand.
* Must have exactly \p n limbs. * Must have exactly \p AN_limbs limbs.
* \param[in] B Little-endian presentation of second operand. * \param[in] B Little-endian presentation of second operand.
* \param[in] B_len The number of limbs in \p B. * \param[in] B_limbs The number of limbs in \p B.
* \param[in] N Little-endian presentation of the modulus. * \param[in] N Little-endian presentation of the modulus.
* This must be odd and have exactly \p n limbs. * This must be odd and have exactly \p AN_limbs limbs.
* \param[in] n The number of limbs in \p X, \p A, \p N. * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N.
* \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
* This can be calculated by `mbedtls_mpi_montg_init()`. * This can be calculated by `mbedtls_mpi_montg_init()`.
* \param[in,out] T Temporary storage of size at least 2*n+1 limbs. * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
* Its initial content is unused and * Its initial content is unused and
* its final content is indeterminate. * its final content is indeterminate.
*/ */
void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *A, const mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *B, size_t B_len, const mbedtls_mpi_uint *B, size_t B_limbs,
const mbedtls_mpi_uint *N, size_t n, const mbedtls_mpi_uint *N, size_t AN_limbs,
mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
/** /**
@ -194,46 +194,46 @@ void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N ); mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N );
/** /**
* \brief Perform a known-size multiply accumulate operation: d += b * s * \brief Perform a known-size multiply accumulate operation: A += c * B
* *
* \param[in,out] d The pointer to the (little-endian) array * \param[in,out] A The pointer to the (little-endian) array
* representing the bignum to accumulate onto. * representing the bignum to accumulate onto.
* \param d_len The number of limbs of \p d. This must be * \param A_limbs The number of limbs of \p A. This must be
* at least \p s_len. * at least \p B_limbs.
* \param[in] s The pointer to the (little-endian) array * \param[in] B The pointer to the (little-endian) array
* representing the bignum to multiply with. * representing the bignum to multiply with.
* This may be the same as \p d. Otherwise, * This may be the same as \p A. Otherwise,
* it must be disjoint from \p d. * it must be disjoint from \p A.
* \param s_len The number of limbs of \p s. * \param B_limbs The number of limbs of \p B.
* \param b A scalar to multiply with. * \param c A scalar to multiply with.
* *
* \return c The carry at the end of the operation. * \return The carry at the end of the operation.
*/ */
mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *A, size_t A_limbs,
const mbedtls_mpi_uint *s, size_t s_len, const mbedtls_mpi_uint *B, size_t B_limbs,
mbedtls_mpi_uint b ); mbedtls_mpi_uint c );
/** /**
* \brief Subtract two known-size large unsigned integers, returning the borrow. * \brief Subtract two known-size large unsigned integers, returning the borrow.
* *
* Calculate l - r where l and r have the same size. * Calculate A - B where A and B have the same size.
* This function operates modulo (2^ciL)^n and returns the carry * This function operates modulo (2^ciL)^limbs and returns the carry
* (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise). * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
* *
* d may be aliased to l or r. * X may be aliased to A or B.
* *
* \param[out] d The result of the subtraction. * \param[out] X The result of the subtraction.
* \param[in] l Little-endian presentation of left operand. * \param[in] A Little-endian presentation of left operand.
* \param[in] r Little-endian presentation of right operand. * \param[in] B Little-endian presentation of right operand.
* \param n Number of limbs of \p d, \p l and \p r. * \param limbs Number of limbs of \p X, \p A and \p B.
* *
* \return 1 if `l < r`. * \return 1 if `A < B`.
* 0 if `l >= r`. * 0 if `A >= B`.
*/ */
mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
const mbedtls_mpi_uint *l, const mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *r, const mbedtls_mpi_uint *B,
size_t n ); size_t limbs );
/** /**
* \brief Constant-time conditional addition of two known-size large unsigned * \brief Constant-time conditional addition of two known-size large unsigned
@ -243,24 +243,28 @@ mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d,
* *
* ``` * ```
* if( cond ) * if( cond )
* d += r; * A += B;
* return carry; * return carry;
* ``` * ```
* *
* \param[in,out] d The pointer to the (little-endian) array * \param[in,out] A The pointer to the (little-endian) array
* representing the bignum to accumulate onto. * representing the bignum to accumulate onto.
* \param[in] r The pointer to the (little-endian) array * \param[in] B The pointer to the (little-endian) array
* representing the bignum to conditionally add * representing the bignum to conditionally add
* to \p d. This must be disjoint from \p d. * to \p A. This must be disjoint from \p A.
* \param n Number of limbs of \p d and \p r. * \param limbs Number of limbs of \p A and \p B.
* \param cond Condition bit dictating whether addition should * \param cond Condition bit dictating whether addition should
* happen or not. This must be \c 0 or \c 1. * happen or not. This must be \c 0 or \c 1.
* *
* \return 1 if `d + cond*r >= (2^{ciL})^n`, 0 otherwise. * \warning If \p assign is neither 0 nor 1, the result of this function
* is unspecified, and the resulting value in \p A might be
* neither its original value nor \p A + \p B.
*
* \return 1 if `A + cond * B >= (2^{ciL})^limbs`, 0 otherwise.
*/ */
mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A,
const mbedtls_mpi_uint *r, const mbedtls_mpi_uint *B,
size_t n, size_t limbs,
unsigned cond ); unsigned cond );
#endif /* MBEDTLS_BIGNUM_CORE_H */ #endif /* MBEDTLS_BIGNUM_CORE_H */