mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-01-08 13:03:39 +00:00
Merge pull request #6606 from gabor-mezei-arm/6222_bignum_low_level_subtraction
Bignum: Add low level subtraction
This commit is contained in:
commit
505a228b7b
@ -108,6 +108,16 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
|
||||
|
||||
/* BEGIN MERGE SLOT 2 */
|
||||
|
||||
void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_uint *B,
|
||||
const mbedtls_mpi_mod_modulus *N )
|
||||
{
|
||||
mbedtls_mpi_uint c = mbedtls_mpi_core_sub( X, A, B, N->limbs );
|
||||
|
||||
(void) mbedtls_mpi_core_add_if( X, N->p, N->limbs, (unsigned) c );
|
||||
}
|
||||
|
||||
/* END MERGE SLOT 2 */
|
||||
|
||||
/* BEGIN MERGE SLOT 3 */
|
||||
|
@ -144,6 +144,28 @@ int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
|
||||
|
||||
/* BEGIN MERGE SLOT 2 */
|
||||
|
||||
/** \brief Subtract two MPIs, returning the residue modulo the specified
|
||||
* modulus.
|
||||
*
|
||||
* The size of the operation is determined by \p N. \p A and \p B must have
|
||||
* the same number of limbs as \p N.
|
||||
*
|
||||
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
||||
* either otherwise.
|
||||
*
|
||||
* \param[out] X The address of the result MPI.
|
||||
* This must be initialized. Must have enough limbs to
|
||||
* store the full value of the result.
|
||||
* \param[in] A The address of the first MPI. This must be initialized.
|
||||
* \param[in] B The address of the second MPI. This must be initialized.
|
||||
* \param[in] N The address of the modulus. Used to perform a modulo
|
||||
* operation on the result of the subtraction.
|
||||
*/
|
||||
void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_uint *B,
|
||||
const mbedtls_mpi_mod_modulus *N );
|
||||
|
||||
/* END MERGE SLOT 2 */
|
||||
|
||||
/* BEGIN MERGE SLOT 3 */
|
||||
|
@ -30,6 +30,25 @@ class BignumModRawTarget(test_data_generation.BaseTarget):
|
||||
|
||||
# BEGIN MERGE SLOT 2
|
||||
|
||||
class BignumModRawSub(bignum_common.ModOperationCommon,
|
||||
BignumModRawTarget):
|
||||
"""Test cases for bignum mpi_mod_raw_sub()."""
|
||||
symbol = "-"
|
||||
test_function = "mpi_mod_raw_sub"
|
||||
test_name = "mbedtls_mpi_mod_raw_sub"
|
||||
input_style = "fixed"
|
||||
arity = 2
|
||||
|
||||
def arguments(self) -> List[str]:
|
||||
return [bignum_common.quote_str(n) for n in [self.arg_a,
|
||||
self.arg_b,
|
||||
self.arg_n]
|
||||
] + self.result()
|
||||
|
||||
def result(self) -> List[str]:
|
||||
result = (self.int_a - self.int_b) % self.int_n
|
||||
return [self.format_result(result)]
|
||||
|
||||
# END MERGE SLOT 2
|
||||
|
||||
# BEGIN MERGE SLOT 3
|
||||
|
@ -70,6 +70,7 @@ if(GEN_FILES)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_common.py
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_core.py
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod_raw.py
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py
|
||||
)
|
||||
|
@ -94,6 +94,7 @@ $(GENERATED_BIGNUM_DATA_FILES): generated_bignum_test_data
|
||||
generated_bignum_test_data: scripts/generate_bignum_tests.py
|
||||
generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_common.py
|
||||
generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_core.py
|
||||
generated_bignum_test_data: ../scripts/mbedtls_dev/bignum_mod_raw.py
|
||||
generated_bignum_test_data: ../scripts/mbedtls_dev/test_case.py
|
||||
generated_bignum_test_data: ../scripts/mbedtls_dev/test_data_generation.py
|
||||
generated_bignum_test_data:
|
||||
|
@ -275,6 +275,79 @@ exit:
|
||||
|
||||
/* BEGIN MERGE SLOT 2 */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_mod_raw_sub( char * input_A,
|
||||
char * input_B,
|
||||
char * input_N,
|
||||
char * result )
|
||||
{
|
||||
mbedtls_mpi_uint *A = NULL;
|
||||
mbedtls_mpi_uint *B = NULL;
|
||||
mbedtls_mpi_uint *N = NULL;
|
||||
mbedtls_mpi_uint *X = NULL;
|
||||
mbedtls_mpi_uint *res = NULL;
|
||||
size_t limbs_A;
|
||||
size_t limbs_B;
|
||||
size_t limbs_N;
|
||||
size_t limbs_res;
|
||||
|
||||
mbedtls_mpi_mod_modulus m;
|
||||
mbedtls_mpi_mod_modulus_init( &m );
|
||||
|
||||
TEST_EQUAL( mbedtls_test_read_mpi_core( &A, &limbs_A, input_A ), 0 );
|
||||
TEST_EQUAL( mbedtls_test_read_mpi_core( &B, &limbs_B, input_B ), 0 );
|
||||
TEST_EQUAL( mbedtls_test_read_mpi_core( &N, &limbs_N, input_N ), 0 );
|
||||
TEST_EQUAL( mbedtls_test_read_mpi_core( &res, &limbs_res, result ), 0 );
|
||||
|
||||
size_t limbs = limbs_N;
|
||||
size_t bytes = limbs * sizeof( mbedtls_mpi_uint );
|
||||
|
||||
TEST_EQUAL( limbs_A, limbs );
|
||||
TEST_EQUAL( limbs_B, limbs );
|
||||
TEST_EQUAL( limbs_res, limbs );
|
||||
|
||||
ASSERT_ALLOC( X, limbs );
|
||||
|
||||
TEST_EQUAL( mbedtls_mpi_mod_modulus_setup(
|
||||
&m, N, limbs,
|
||||
MBEDTLS_MPI_MOD_EXT_REP_BE,
|
||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY ), 0 );
|
||||
|
||||
mbedtls_mpi_mod_raw_sub( X, A, B, &m );
|
||||
ASSERT_COMPARE( X, bytes, res, bytes );
|
||||
|
||||
/* alias X to A */
|
||||
memcpy( X, A, bytes );
|
||||
mbedtls_mpi_mod_raw_sub( X, X, B, &m );
|
||||
ASSERT_COMPARE( X, bytes, res, bytes );
|
||||
|
||||
/* alias X to B */
|
||||
memcpy( X, B, bytes );
|
||||
mbedtls_mpi_mod_raw_sub( X, A, X, &m );
|
||||
ASSERT_COMPARE( X, bytes, res, bytes );
|
||||
|
||||
/* A == B: alias A and B */
|
||||
if( memcmp( A, B, bytes ) == 0 )
|
||||
{
|
||||
mbedtls_mpi_mod_raw_sub( X, A, A, &m );
|
||||
ASSERT_COMPARE( X, bytes, res, bytes );
|
||||
|
||||
/* X, A, B all aliased together */
|
||||
memcpy( X, A, bytes );
|
||||
mbedtls_mpi_mod_raw_sub( X, X, X, &m );
|
||||
ASSERT_COMPARE( X, bytes, res, bytes );
|
||||
}
|
||||
exit:
|
||||
mbedtls_free( A );
|
||||
mbedtls_free( B );
|
||||
mbedtls_free( X );
|
||||
mbedtls_free( res );
|
||||
|
||||
mbedtls_mpi_mod_modulus_free( &m );
|
||||
mbedtls_free( N );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* END MERGE SLOT 2 */
|
||||
|
||||
/* BEGIN MERGE SLOT 3 */
|
||||
|
Loading…
Reference in New Issue
Block a user