mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-30 22:43:08 +03:00
Merge pull request #7793 from minosgalanakis/ecp/6025_fast_reduction_dispatch
[Bignum] Fast reduction dispatch
This commit is contained in:
@ -114,8 +114,6 @@ void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
|
||||
(void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_TEST_HOOKS)
|
||||
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_mod_modulus *N)
|
||||
@ -125,7 +123,6 @@ void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X,
|
||||
(void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_TEST_HOOKS */
|
||||
|
||||
void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_uint *A,
|
||||
@ -133,8 +130,31 @@ void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_mod_modulus *N,
|
||||
mbedtls_mpi_uint *T)
|
||||
{
|
||||
mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
|
||||
N->rep.mont.mm, T);
|
||||
/* Standard (A * B) multiplication stored into pre-allocated T
|
||||
* buffer of fixed limb size of (2N + 1).
|
||||
*
|
||||
* The space may not not fully filled by when
|
||||
* MBEDTLS_MPI_MOD_REP_OPT_RED is used. */
|
||||
const size_t T_limbs = BITS_TO_LIMBS(N->bits) * 2;
|
||||
switch (N->int_rep) {
|
||||
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
||||
mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
|
||||
N->rep.mont.mm, T);
|
||||
break;
|
||||
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
||||
mbedtls_mpi_core_mul(T, A, N->limbs, B, N->limbs);
|
||||
|
||||
/* Optimised Reduction */
|
||||
(*N->rep.ored.modp)(T, T_limbs);
|
||||
|
||||
/* Convert back to canonical representation */
|
||||
mbedtls_mpi_mod_raw_fix_quasi_reduction(T, N);
|
||||
memcpy(X, T, N->limbs * sizeof(mbedtls_mpi_uint));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
|
||||
|
@ -4922,7 +4922,7 @@ static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry)
|
||||
static int ecp_mod_p192(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * ((192 + biL - 1) / biL);
|
||||
size_t expected_width = BITS_TO_LIMBS(192) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p192_raw(N->p, expected_width);
|
||||
|
||||
@ -4936,7 +4936,7 @@ int mbedtls_ecp_mod_p192_raw(mbedtls_mpi_uint *Np, size_t Nn)
|
||||
mbedtls_mpi_uint c = 0, last_carry[WIDTH] = { 0 };
|
||||
mbedtls_mpi_uint *p, *end;
|
||||
|
||||
if (Nn != 2*((192 + biL - 1)/biL)) {
|
||||
if (Nn != BITS_TO_LIMBS(192) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5082,7 +5082,7 @@ static inline int8_t extract_carry(int64_t cur)
|
||||
static int ecp_mod_p224(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * 224 / biL;
|
||||
size_t expected_width = BITS_TO_LIMBS(224) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p224_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
@ -5092,7 +5092,7 @@ cleanup:
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
if (X_limbs != 2 * 224 / biL) {
|
||||
if (X_limbs != BITS_TO_LIMBS(224) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5135,7 +5135,7 @@ int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p256(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * 256 / biL;
|
||||
size_t expected_width = BITS_TO_LIMBS(256) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p256_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
@ -5145,7 +5145,7 @@ cleanup:
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
if (X_limbs != 2 * 256 / biL) {
|
||||
if (X_limbs != BITS_TO_LIMBS(256) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5215,7 +5215,7 @@ int mbedtls_ecp_mod_p256_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p384(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * ((384 + biL - 1) / biL);
|
||||
size_t expected_width = BITS_TO_LIMBS(384) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p384_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
@ -5225,7 +5225,7 @@ cleanup:
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
if (X_limbs != 2*((384 + biL - 1)/biL)) {
|
||||
if (X_limbs != BITS_TO_LIMBS(384) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5337,7 +5337,7 @@ int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p521(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * P521_WIDTH;
|
||||
size_t expected_width = BITS_TO_LIMBS(521) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p521_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
@ -5349,7 +5349,7 @@ int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
{
|
||||
mbedtls_mpi_uint carry = 0;
|
||||
|
||||
if (X_limbs != 2 * P521_WIDTH || X[2 * P521_WIDTH - 1] != 0) {
|
||||
if (X_limbs != BITS_TO_LIMBS(521) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5423,7 +5423,7 @@ int mbedtls_ecp_mod_p521_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p255(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * P255_WIDTH;
|
||||
size_t expected_width = BITS_TO_LIMBS(255) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p255_raw(N->p, expected_width);
|
||||
cleanup:
|
||||
@ -5434,7 +5434,7 @@ MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_Limbs)
|
||||
{
|
||||
|
||||
if (X_Limbs != 2 * P255_WIDTH) {
|
||||
if (X_Limbs != BITS_TO_LIMBS(255) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5492,7 +5492,7 @@ int mbedtls_ecp_mod_p255_raw(mbedtls_mpi_uint *X, size_t X_Limbs)
|
||||
static int ecp_mod_p448(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * ((448 + biL - 1) / biL);
|
||||
size_t expected_width = BITS_TO_LIMBS(448) * 2;
|
||||
|
||||
/* This is required as some tests and use cases do not pass in a Bignum of
|
||||
* the correct size, and expect the growth to be done automatically, which
|
||||
@ -5522,7 +5522,7 @@ int mbedtls_ecp_mod_p448_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
size_t round;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (X_limbs <= P448_WIDTH) {
|
||||
if (X_limbs != BITS_TO_LIMBS(448) * 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -5734,7 +5734,7 @@ cleanup:
|
||||
static int ecp_mod_p192k1(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * ((192 + biL - 1) / biL);
|
||||
size_t expected_width = BITS_TO_LIMBS(192) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p192k1_raw(N->p, expected_width);
|
||||
|
||||
@ -5750,7 +5750,7 @@ int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
||||
if (X_limbs != 2 * ((192 + biL - 1) / biL)) {
|
||||
if (X_limbs != BITS_TO_LIMBS(192) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5768,7 +5768,7 @@ int mbedtls_ecp_mod_p192k1_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p224k1(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * 224 / biL;
|
||||
size_t expected_width = BITS_TO_LIMBS(224) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p224k1_raw(N->p, expected_width);
|
||||
|
||||
@ -5784,7 +5784,7 @@ int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
||||
if (X_limbs != 2 * 224 / biL) {
|
||||
if (X_limbs != BITS_TO_LIMBS(224) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5802,7 +5802,7 @@ int mbedtls_ecp_mod_p224k1_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
static int ecp_mod_p256k1(mbedtls_mpi *N)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t expected_width = 2 * ((256 + biL - 1) / biL);
|
||||
size_t expected_width = BITS_TO_LIMBS(256) * 2;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
|
||||
ret = mbedtls_ecp_mod_p256k1_raw(N->p, expected_width);
|
||||
|
||||
@ -5818,7 +5818,7 @@ int mbedtls_ecp_mod_p256k1_raw(mbedtls_mpi_uint *X, size_t X_limbs)
|
||||
0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
||||
if (X_limbs != 2 * ((256 + biL - 1) / biL)) {
|
||||
if (X_limbs != BITS_TO_LIMBS(256) * 2) {
|
||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
@ -5970,7 +5970,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP192K1:
|
||||
if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
modp = &mbedtls_ecp_mod_p192_raw;
|
||||
modp = &mbedtls_ecp_mod_p192k1_raw;
|
||||
p = (mbedtls_mpi_uint *) secp192k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp192k1_p));
|
||||
} else {
|
||||
@ -5983,7 +5983,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP224K1:
|
||||
if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
modp = &mbedtls_ecp_mod_p224_raw;
|
||||
modp = &mbedtls_ecp_mod_p224k1_raw;
|
||||
p = (mbedtls_mpi_uint *) secp224k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp224k1_p));
|
||||
} else {
|
||||
@ -5996,7 +5996,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
|
||||
case MBEDTLS_ECP_DP_SECP256K1:
|
||||
if (ctype == (mbedtls_ecp_modulus_type) MBEDTLS_ECP_MOD_COORDINATE) {
|
||||
modp = &mbedtls_ecp_mod_p256_raw;
|
||||
modp = &mbedtls_ecp_mod_p256k1_raw;
|
||||
p = (mbedtls_mpi_uint *) secp256k1_p;
|
||||
p_limbs = CHARS_TO_LIMBS(sizeof(secp256k1_p));
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user