Add bignum_new.c starting with MPI_CORE(montmul) for Montgomery multiplication
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
diff --git a/library/bignum_core.h b/library/bignum_core.h
index 1e159a7..62d0151 100644
--- a/library/bignum_core.h
+++ b/library/bignum_core.h
@@ -172,8 +172,107 @@
*
* \return c 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 *d, size_t d_len,
const mbedtls_mpi_uint *s, size_t s_len,
mbedtls_mpi_uint b );
+#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal
+
+/** Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
+ *
+ * \param[out] X The destination MPI, as a big 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 Big endian presentation of first operand.
+ * Must have exactly \p n limbs.
+ * \param[in] B Big endian presentation of second operand.
+ * \param[in] B_len The number of limbs in \p B.
+ * \param[in] N Big 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 `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.
+ */
+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 );
+
+/**
+ * \brief Perform a known-size multiply accumulate operation
+ *
+ * Add \p b * \p s to \p d.
+ *
+ * \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.
+ *
+ * \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 );
+
+/**
+ * \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).
+ *
+ * d may be aliased to l or r.
+ *
+ * \param[out] d The result of the subtraction.
+ * \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.
+ *
+ * \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 );
+
+/**
+ * \brief Constant-time conditional addition of two known-size large unsigned
+ * integers, returning the carry.
+ *
+ * Functionally equivalent to
+ *
+ * ```
+ * if( cond )
+ * d += r;
+ * 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.
+ *
+ * \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 );
+
#endif /* MBEDTLS_BIGNUM_CORE_H */