Generate operands in Mongomery representation for the test function

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
Gabor Mezei 2022-12-16 15:25:02 +01:00
parent 2840884c35
commit 77b877d5a7
No known key found for this signature in database
GPG key ID: F072ACA227ACD71D
2 changed files with 4 additions and 16 deletions

View file

@ -41,14 +41,14 @@ class BignumModMul(bignum_common.ModOperationCommon,
arity = 2 arity = 2
def arguments(self) -> List[str]: def arguments(self) -> List[str]:
return [bignum_common.quote_str(n) for n in [self.arg_a, return [self.format_result(self.to_montgomery(self.int_a)),
self.arg_b, self.format_result(self.to_montgomery(self.int_b)),
self.arg_n] bignum_common.quote_str(self.arg_n)
] + self.result() ] + self.result()
def result(self) -> List[str]: def result(self) -> List[str]:
result = (self.int_a * self.int_b) % self.int_n result = (self.int_a * self.int_b) % self.int_n
return [self.format_result(result)] return [self.format_result(self.to_montgomery(result))]
# END MERGE SLOT 2 # END MERGE SLOT 2

View file

@ -149,37 +149,28 @@ void mpi_mod_mul( char * input_A,
mbedtls_mpi_mod_residue rX; mbedtls_mpi_mod_residue rX;
TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_residue_setup( &rX, &m, X, limbs ), 0 );
/* Convert to Montgomery representation */
TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rA.p, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_to_mont_rep( rB.p, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rB, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* alias X to A */ /* alias X to A */
memcpy( rX.p, rA.p, bytes ); memcpy( rX.p, rA.p, bytes );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rB, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* alias X to B */ /* alias X to B */
memcpy( rX.p, rB.p, bytes ); memcpy( rX.p, rB.p, bytes );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rX, &m ), 0);
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* A == B: alias A and B */ /* A == B: alias A and B */
if( memcmp( rA.p, rB.p, bytes ) == 0 ) if( memcmp( rA.p, rB.p, bytes ) == 0 )
{ {
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rA, &rA, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* X, A, B all aliased together */ /* X, A, B all aliased together */
memcpy( rX.p, rA.p, bytes ); memcpy( rX.p, rA.p, bytes );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rX, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
} }
@ -187,19 +178,16 @@ void mpi_mod_mul( char * input_A,
else else
{ {
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rA, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* B * A: alias X to A */ /* B * A: alias X to A */
memcpy( rX.p, rA.p, bytes ); memcpy( rX.p, rA.p, bytes );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rB, &rX, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
/* B + A: alias X to B */ /* B + A: alias X to B */
memcpy( rX.p, rB.p, bytes ); memcpy( rX.p, rB.p, bytes );
TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 ); TEST_EQUAL( mbedtls_mpi_mod_mul( &rX, &rX, &rA, &m ), 0 );
TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( rX.p, &m ), 0 );
ASSERT_COMPARE( rX.p, bytes, R, bytes ); ASSERT_COMPARE( rX.p, bytes, R, bytes );
} }