@@ -170,6 +170,53 @@ int test_mp_rand(void)
170170 return EXPECT_RESULT ();
171171} /* End test_mp_rand */
172172
173+ /* An even modulus with a zero low digit must not use single-digit 2k
174+ * reduction: its setup value is 2^DIGIT_BIT and does not fit in a digit. */
175+ int test_mp_exptmod_even_2k (void )
176+ {
177+ EXPECT_DECLS ;
178+ #if defined(USE_INTEGER_HEAP_MATH ) && defined(WOLFSSL_PUBLIC_MP ) && \
179+ defined(BN_MP_REDUCE_IS_2K_C ) && defined(BN_S_MP_EXPTMOD_C )
180+ mp_int g , x , p , y ;
181+
182+ XMEMSET (& g , 0 , sizeof (g ));
183+ XMEMSET (& x , 0 , sizeof (x ));
184+ XMEMSET (& p , 0 , sizeof (p ));
185+ XMEMSET (& y , 0 , sizeof (y ));
186+ ExpectIntEQ (mp_init_multi (& g , & x , & p , & y , NULL , NULL ), MP_OKAY );
187+
188+ /* p = 2^DIGIT_BIT, then p = 3 * 2^DIGIT_BIT. */
189+ ExpectIntEQ (mp_set (& p , 1 ), MP_OKAY );
190+ ExpectIntEQ (mp_mul_2d (& p , DIGIT_BIT , & p ), MP_OKAY );
191+ ExpectIntEQ (mp_reduce_is_2k (& p ), MP_NO );
192+ /* (p - 1)^2 mod p = 1. */
193+ ExpectIntEQ (mp_sub_d (& p , 1 , & g ), MP_OKAY );
194+ ExpectIntEQ (mp_set (& x , 2 ), MP_OKAY );
195+ ExpectIntEQ (mp_exptmod (& g , & x , & p , & y ), MP_OKAY );
196+ ExpectIntEQ (mp_cmp_d (& y , 1 ), MP_EQ );
197+
198+ ExpectIntEQ (mp_set (& p , 3 ), MP_OKAY );
199+ ExpectIntEQ (mp_mul_2d (& p , DIGIT_BIT , & p ), MP_OKAY );
200+ ExpectIntEQ (mp_reduce_is_2k (& p ), MP_NO );
201+ ExpectIntEQ (mp_sub_d (& p , 1 , & g ), MP_OKAY );
202+ ExpectIntEQ (mp_exptmod (& g , & x , & p , & y ), MP_OKAY );
203+ ExpectIntEQ (mp_cmp_d (& y , 1 ), MP_EQ );
204+
205+ /* The adjacent modulus has a representable single-digit setup value. */
206+ ExpectIntEQ (mp_add_d (& p , 1 , & p ), MP_OKAY );
207+ ExpectIntEQ (mp_reduce_is_2k (& p ), MP_YES );
208+ ExpectIntEQ (mp_sub_d (& p , 1 , & g ), MP_OKAY );
209+ ExpectIntEQ (mp_exptmod (& g , & x , & p , & y ), MP_OKAY );
210+ ExpectIntEQ (mp_cmp_d (& y , 1 ), MP_EQ );
211+
212+ mp_clear (& g );
213+ mp_clear (& x );
214+ mp_clear (& p );
215+ mp_clear (& y );
216+ #endif
217+ return EXPECT_RESULT ();
218+ }
219+
173220/*
174221 * Testing wc_export_int
175222 */
0 commit comments