@@ -478,6 +478,53 @@ int test_wc_export_int(void)
478478 return EXPECT_RESULT ();
479479} /* End test_wc_export_int */
480480
481+ /*
482+ * Testing mp_invmod with an odd modulus of about 8192 bits
483+ */
484+ int test_mp_invmod_large_odd_modulus (void )
485+ {
486+ EXPECT_DECLS ;
487+ #if defined(USE_INTEGER_HEAP_MATH ) && defined(WOLFSSL_PUBLIC_MP ) && \
488+ !defined(WC_NO_RNG ) && (!defined(HAVE_FIPS ) || FIPS_VERSION3_GE (7 ,0 ,0 ))
489+ mp_int a ;
490+ mp_int k ;
491+ mp_int m ;
492+ mp_int r ;
493+ mp_int t ;
494+ WC_RNG rng ;
495+ int digits = (4096 + DIGIT_BIT - 1 ) / DIGIT_BIT ;
496+
497+ XMEMSET (& rng , 0 , sizeof (WC_RNG ));
498+
499+ ExpectIntEQ (mp_init_multi (& a , & k , & m , & r , & t , NULL ), MP_OKAY );
500+ ExpectIntEQ (wc_InitRng (& rng ), 0 );
501+
502+ /* m = 2*a*k + 1 is odd and coprime to a, so the inverse exists. */
503+ ExpectIntEQ (mp_rand (& a , digits , & rng ), 0 );
504+ ExpectIntEQ (mp_set_bit (& a , 4095 ), MP_OKAY );
505+ ExpectIntEQ (mp_rand (& k , digits , & rng ), 0 );
506+ ExpectIntEQ (mp_set_bit (& k , 4095 ), MP_OKAY );
507+ ExpectIntEQ (mp_mul (& a , & k , & m ), MP_OKAY );
508+ ExpectIntEQ (mp_mul_2d (& m , 1 , & m ), MP_OKAY );
509+ ExpectIntEQ (mp_add_d (& m , 1 , & m ), MP_OKAY );
510+ ExpectIntGE (mp_count_bits (& m ), 8192 );
511+
512+ ExpectIntEQ (mp_invmod (& a , & m , & r ), MP_OKAY );
513+ ExpectIntEQ (mp_isneg (& r ), MP_NO );
514+ ExpectIntEQ (mp_cmp (& r , & m ), MP_LT );
515+ ExpectIntEQ (mp_mulmod (& a , & r , & m , & t ), MP_OKAY );
516+ ExpectIntEQ (mp_cmp_d (& t , 1 ), MP_EQ );
517+
518+ mp_clear (& t );
519+ mp_clear (& r );
520+ mp_clear (& m );
521+ mp_clear (& k );
522+ mp_clear (& a );
523+ DoExpectIntEQ (wc_FreeRng (& rng ), 0 );
524+ #endif
525+ return EXPECT_RESULT ();
526+ } /* End test_mp_invmod_large_odd_modulus */
527+
481528/*
482529 * MC/DC coverage for the sp_int.c multi-precision engine (the sp-math
483530 * module, iso26262/mcdc-per-module). These call the sp_* primitives
0 commit comments