From 21fe8bdeacf6e1fa79baf8c4ecce849323cf7b28 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Wed, 7 Dec 2022 18:06:05 +0000 Subject: [PATCH 1/6] bignum_mod_raw: Added modular negation. This patch adds the `mpi_mod_raw_neg()` method. Co-authored-by: Hanno Becker Co-authored-by: Minos Galanakis Signed-off-by: Minos Galanakis --- library/bignum_mod_raw.c | 12 ++++++++++++ library/bignum_mod_raw.h | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c index 22e56b7e63..138d5a08bc 100644 --- a/library/bignum_mod_raw.c +++ b/library/bignum_mod_raw.c @@ -182,6 +182,18 @@ int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, mbedtls_free( T ); return( 0 ); } + +void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_mod_modulus *m ) +{ + mbedtls_mpi_core_sub( X, m->p, A, m->limbs ); + + /* If A=0 initially, then X=N now. Detect this by + * subtracting N and catching the carry. */ + mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub( X, X, m->p, m->limbs ); + (void) mbedtls_mpi_core_add_if( X, m->p, m->limbs, (unsigned) borrow ); +} /* END MERGE SLOT 7 */ /* BEGIN MERGE SLOT 8 */ diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h index d7b6dd115e..b512ae0d30 100644 --- a/library/bignum_mod_raw.h +++ b/library/bignum_mod_raw.h @@ -233,6 +233,24 @@ int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X, */ int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, const mbedtls_mpi_mod_modulus *m ); + +/** \brief Perform fixed width modular negation. + * + * The size of the operation is determined by \p N. \p A must have + * the same number of limbs as \p N. + * + * \p X may be aliased to \p A. + * + * \param[out] X The result of the modular negation. + * This must be initialized. Must have enough limbs to + * store the full value of the result. + * \param[in] A Little-endian presentation of the input operand. This + * must be smaller or equal to \p N. + * \param[in] m The address of the modulus related to \p A. + */ +void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_mod_modulus *m); /* END MERGE SLOT 7 */ /* BEGIN MERGE SLOT 8 */ From 6118a3e5aa66c062f096283925d815648c531881 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Wed, 7 Dec 2022 18:09:48 +0000 Subject: [PATCH 2/6] test_suite_bignumg_mod_raw: Added test for mpi_mod_raw_neg Signed-off-by: Minos Galanakis --- .../suites/test_suite_bignum_mod_raw.function | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index c7decf0071..8759bc9de5 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -526,6 +526,61 @@ exit: mbedtls_free( X ); } /* END_CASE */ + +/* BEGIN_CASE */ +void mpi_mod_raw_neg( char * input_N, char * input_A, char * input_X ) +{ + mbedtls_mpi_uint *N = NULL; + mbedtls_mpi_uint *A = NULL; + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *R = NULL; + mbedtls_mpi_uint *Z = NULL; + size_t n_limbs, a_limbs, x_limbs, bytes; + + mbedtls_mpi_mod_modulus m; + mbedtls_mpi_mod_modulus_init( &m ); + + /* Read inputs */ + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &N, &n_limbs, input_N ) ); + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &A, &a_limbs, input_A ) ); + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &X, &x_limbs, input_X ) ); + + TEST_EQUAL( a_limbs, n_limbs ); + TEST_EQUAL( x_limbs, n_limbs ); + bytes = n_limbs * sizeof( mbedtls_mpi_uint ); + + ASSERT_ALLOC( R, n_limbs ); + ASSERT_ALLOC( Z, n_limbs ); + + TEST_EQUAL( 0, mbedtls_mpi_mod_modulus_setup( &m, N, n_limbs, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) ); + + /* Neg( A == 0 ) => Zero result */ + mbedtls_mpi_mod_raw_neg( R, Z, &m ); + ASSERT_COMPARE( R, bytes, Z, bytes ); + + /* Neg( A == N ) => Zero result */ + mbedtls_mpi_mod_raw_neg( R, N, &m ); + ASSERT_COMPARE( R, bytes, Z, bytes ); + + /* Neg( A ) => Correct result */ + mbedtls_mpi_mod_raw_neg( R, A, &m ); + ASSERT_COMPARE( R, bytes, X, bytes ); + + /* Neg( A ): alias A to R => Correct result */ + memcpy( R, A, bytes ); + mbedtls_mpi_mod_raw_neg( R, R, &m ); + ASSERT_COMPARE( R, bytes, X, bytes ); +exit: + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_free( N ); + mbedtls_free( A ); + mbedtls_free( X ); + mbedtls_free( R ); + mbedtls_free( Z ); +} +/* END_CASE */ + /* END MERGE SLOT 7 */ /* BEGIN MERGE SLOT 8 */ From 78665eba8f59c6d36b47b9d73178a58a353867e8 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Wed, 7 Dec 2022 18:10:46 +0000 Subject: [PATCH 3/6] bignum_mod_raw.py: Added BignumModRawModNegate. This patch adds autogenerated inputs for the `mpi_mod_raw_neg()` test in the bignum_mod_raw suite. Signed-off-by: Minos Galanakis --- scripts/mbedtls_dev/bignum_mod_raw.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 0bbad5dd90..34d26f9bb0 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -109,7 +109,18 @@ class BignumModRawConvertFromMont(bignum_common.ModOperationCommon, result = (self.int_a * self.r_inv) % self.int_n return [self.format_result(result)] +class BignumModRawModNegate(bignum_common.ModOperationCommon, + BignumModRawTarget): + """ Test cases for mpi_mod_raw_neg(). """ + test_function = "mpi_mod_raw_neg" + test_name = "Modular negation: " + symbol = "(-A)" + input_style = "arch_split" + arity = 1 + def result(self) -> List[str]: + result = (self.int_n - self.int_a) % self.int_n + return [self.format_result(result)] # END MERGE SLOT 7 # BEGIN MERGE SLOT 8 From 5e8443e6efe2a80dc4d4b165f5fc3d2ec317e406 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Thu, 8 Dec 2022 11:40:51 +0000 Subject: [PATCH 4/6] mbedtls_mpi_mod_raw_neg: Updated documentation. Signed-off-by: Minos Galanakis --- library/bignum_mod_raw.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library/bignum_mod_raw.h b/library/bignum_mod_raw.h index b512ae0d30..b21092ff70 100644 --- a/library/bignum_mod_raw.h +++ b/library/bignum_mod_raw.h @@ -236,17 +236,16 @@ int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X, /** \brief Perform fixed width modular negation. * - * The size of the operation is determined by \p N. \p A must have - * the same number of limbs as \p N. + * The size of the operation is determined by \p m. \p A must have + * the same number of limbs as \p m. * * \p X may be aliased to \p A. * * \param[out] X The result of the modular negation. - * This must be initialized. Must have enough limbs to - * store the full value of the result. + * This must be initialized. * \param[in] A Little-endian presentation of the input operand. This - * must be smaller or equal to \p N. - * \param[in] m The address of the modulus related to \p A. + * must be less than or equal to \p m. + * \param[in] m The modulus to use. */ void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X, const mbedtls_mpi_uint *A, From 9a60b2373bdfb7d9930ef8a8d25f955bfd6cd03d Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Thu, 8 Dec 2022 11:45:00 +0000 Subject: [PATCH 5/6] bignum_mod_raw testsuite: Refactored `mpi_mod_raw_neg()`. Signed-off-by: Minos Galanakis --- tests/suites/test_suite_bignum_mod_raw.function | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 8759bc9de5..ac8a02fc3e 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -545,8 +545,8 @@ void mpi_mod_raw_neg( char * input_N, char * input_A, char * input_X ) TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &A, &a_limbs, input_A ) ); TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &X, &x_limbs, input_X ) ); - TEST_EQUAL( a_limbs, n_limbs ); - TEST_EQUAL( x_limbs, n_limbs ); + TEST_EQUAL( a_limbs, n_limbs ); + TEST_EQUAL( x_limbs, n_limbs ); bytes = n_limbs * sizeof( mbedtls_mpi_uint ); ASSERT_ALLOC( R, n_limbs ); @@ -568,9 +568,8 @@ void mpi_mod_raw_neg( char * input_N, char * input_A, char * input_X ) ASSERT_COMPARE( R, bytes, X, bytes ); /* Neg( A ): alias A to R => Correct result */ - memcpy( R, A, bytes ); - mbedtls_mpi_mod_raw_neg( R, R, &m ); - ASSERT_COMPARE( R, bytes, X, bytes ); + mbedtls_mpi_mod_raw_neg( A, A, &m ); + ASSERT_COMPARE( A, bytes, X, bytes ); exit: mbedtls_mpi_mod_modulus_free( &m ); mbedtls_free( N ); From f3abea66413ae31b3fc612f50a6cd060784ae299 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Thu, 8 Dec 2022 11:48:26 +0000 Subject: [PATCH 6/6] bignum_mod_raw.py: Changed the symbol for modular negation to "-". Signed-off-by: Minos Galanakis --- scripts/mbedtls_dev/bignum_mod_raw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/mbedtls_dev/bignum_mod_raw.py b/scripts/mbedtls_dev/bignum_mod_raw.py index 34d26f9bb0..c3cb70bed2 100644 --- a/scripts/mbedtls_dev/bignum_mod_raw.py +++ b/scripts/mbedtls_dev/bignum_mod_raw.py @@ -114,7 +114,7 @@ class BignumModRawModNegate(bignum_common.ModOperationCommon, """ Test cases for mpi_mod_raw_neg(). """ test_function = "mpi_mod_raw_neg" test_name = "Modular negation: " - symbol = "(-A)" + symbol = "-" input_style = "arch_split" arity = 1