From 72594633a1bdc73339dff5b46a5955848367557c Mon Sep 17 00:00:00 2001 From: Tom Cosgrove Date: Wed, 24 Aug 2022 11:51:58 +0100 Subject: [PATCH] Apply the function parameter naming convention Signed-off-by: Tom Cosgrove --- library/bignum_core.c | 54 +++++++++--------- library/bignum_core.h | 126 ++++++++++++++++++++++-------------------- 2 files changed, 92 insertions(+), 88 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index ac9285361..4e5012b79 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -296,35 +296,35 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, const mbedtls_mpi_uint *A, const mbedtls_mpi_uint *B, - size_t B_len, + size_t B_limbs, const mbedtls_mpi_uint *N, - size_t n, + size_t AN_limbs, mbedtls_mpi_uint mm, 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; /* T = (T + u0*B + u1*N) / 2^biL */ u0 = A[i]; 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, n + 2, N, n, u1 ); + (void) mbedtls_mpi_core_mla( T, AN_limbs + 2, B, B_limbs, u0 ); + (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 */ - mbedtls_mpi_uint carry = T[n]; - mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, n ); + mbedtls_mpi_uint carry = T[AN_limbs]; + mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, T, N, AN_limbs ); /* * 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 - * 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. * * 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). * * 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 ); } -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 mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t limbs ) { 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 t = l[i] - c; - c = ( t < r[i] ) + z; - d[i] = t - r[i]; + mbedtls_mpi_uint z = ( A[i] < c ); + mbedtls_mpi_uint t = A[i] - c; + c = ( t < B[i] ) + z; + X[i] = t - B[i]; } return( c ); } -mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *r, - size_t n, +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t limbs, unsigned cond ) { 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 += d[i]; c = ( t < d[i] ); + t += A[i]; c = ( t < A[i] ); t += add; c += ( t < add ); - d[i] = t; + A[i] = t; } return( c ); } diff --git a/library/bignum_core.h b/library/bignum_core.h index 85e25a82c..7fd6fed91 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -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) * - * \param[out] X The destination MPI, as a little-endian array of - * length \p n. - * On successful completion, X contains the result of - * the multiplication A * B * R^-1 mod N where - * R = (2^ciL)^n. - * \param[in] A Little-endian presentation of first operand. - * Must have exactly \p n limbs. - * \param[in] B Little-endian presentation of second operand. - * \param[in] B_len The number of limbs in \p B. - * \param[in] N Little-endian presentation of the modulus. - * This must be odd and have exactly \p n limbs. - * \param[in] n The number of limbs in \p X, \p A, \p N. - * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. - * This can be calculated by `mbedtls_mpi_montg_init()`. - * \param[in,out] T Temporary storage of size at least 2*n+1 limbs. - * Its initial content is unused and - * its final content is indeterminate. + * \param[out] X The destination MPI, as a little-endian array of + * length \p AN_limbs. + * On successful completion, X contains the result of + * the multiplication A * B * R^-1 mod N where + * R = (2^ciL)^AN_limbs. + * \param[in] A Little-endian presentation of first operand. + * Must have exactly \p AN_limbs limbs. + * \param[in] B Little-endian presentation of second operand. + * \param[in] B_limbs The number of limbs in \p B. + * \param[in] N Little-endian presentation of the modulus. + * This must be odd and have exactly \p AN_limbs limbs. + * \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. + * This can be calculated by `mbedtls_mpi_montg_init()`. + * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. + * Its initial content is unused and + * its final content is indeterminate. */ 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, + const mbedtls_mpi_uint *B, size_t B_limbs, + const mbedtls_mpi_uint *N, size_t AN_limbs, 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 ); /** - * \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 - * representing the bignum to accumulate onto. - * \param d_len The number of limbs of \p d. This must be - * at least \p s_len. - * \param[in] s The pointer to the (little-endian) array - * representing the bignum to multiply with. - * This may be the same as \p d. Otherwise, - * it must be disjoint from \p d. - * \param s_len The number of limbs of \p s. - * \param b A scalar to multiply with. + * \param[in,out] A The pointer to the (little-endian) array + * representing the bignum to accumulate onto. + * \param A_limbs The number of limbs of \p A. This must be + * at least \p B_limbs. + * \param[in] B The pointer to the (little-endian) array + * representing the bignum to multiply with. + * This may be the same as \p A. Otherwise, + * it must be disjoint from \p A. + * \param B_limbs The number of limbs of \p B. + * \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, - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ); +mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *A, size_t A_limbs, + const mbedtls_mpi_uint *B, size_t B_limbs, + mbedtls_mpi_uint c ); /** * \brief Subtract two known-size large unsigned integers, returning the borrow. * - * Calculate l - r where l and r have the same size. - * This function operates modulo (2^ciL)^n and returns the carry - * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise). + * Calculate A - B where A and B have the same size. + * This function operates modulo (2^ciL)^limbs and returns the carry + * (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[in] l Little-endian presentation of left operand. - * \param[in] r Little-endian presentation of right operand. - * \param n Number of limbs of \p d, \p l and \p r. + * \param[out] X The result of the subtraction. + * \param[in] A Little-endian presentation of left operand. + * \param[in] B Little-endian presentation of right operand. + * \param limbs Number of limbs of \p X, \p A and \p B. * - * \return 1 if `l < r`. - * 0 if `l >= r`. + * \return 1 if `A < B`. + * 0 if `A >= B`. */ -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 mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t limbs ); /** * \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 ) - * d += r; + * A += B; * return carry; * ``` * - * \param[in,out] d The pointer to the (little-endian) array - * representing the bignum to accumulate onto. - * \param[in] r The pointer to the (little-endian) array - * representing the bignum to conditionally add - * to \p d. This must be disjoint from \p d. - * \param n Number of limbs of \p d and \p r. - * \param cond Condition bit dictating whether addition should - * happen or not. This must be \c 0 or \c 1. + * \param[in,out] A The pointer to the (little-endian) array + * representing the bignum to accumulate onto. + * \param[in] B The pointer to the (little-endian) array + * representing the bignum to conditionally add + * to \p A. This must be disjoint from \p A. + * \param limbs Number of limbs of \p A and \p B. + * \param cond Condition bit dictating whether addition should + * 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, - const mbedtls_mpi_uint *r, - size_t n, +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t limbs, unsigned cond ); #endif /* MBEDTLS_BIGNUM_CORE_H */