@@ -1828,6 +1828,51 @@ static int test_mp_root_n(void)
18281828 return EXIT_FAILURE ;
18291829}
18301830
1831+ /* Less error-prone than -1 + 2^n with mp_2expt */
1832+ static mp_err s_fill_with_ones (mp_int * a , int size )
1833+ {
1834+ int i ;
1835+ mp_err err = MP_OKAY ;
1836+
1837+ mp_zero (a );
1838+
1839+ if ((err = mp_grow (a , size )) != MP_OKAY ) goto LTM_ERR ;
1840+ for (i = 0 ; i < size ; i ++ ) {
1841+ a -> dp [i ] = (mp_digit )MP_MASK ;
1842+ a -> used ++ ;
1843+ }
1844+
1845+ LTM_ERR :
1846+ return err ;
1847+ }
1848+
1849+ static int test_s_mp_sqr_comba (void )
1850+ {
1851+ mp_int a , r1 , r2 ;
1852+ int i , j ;
1853+
1854+ DOR (mp_init_multi (& a , & r1 , & r2 , NULL ));
1855+
1856+ for (i = 1 ; i <= MP_MAX_COMBA ; i ++ ) {
1857+ DO (s_fill_with_ones (& a , i ));
1858+ DO (s_mp_sqr_comba (& a , & r1 ));
1859+ DO (s_mp_sqr (& a , & r2 ));
1860+ EXPECT (mp_cmp (& r1 , & r2 ) == MP_EQ );
1861+ for (j = 0 ; j < 20 ; j ++ ) {
1862+ DO (mp_rand (& a , i ));
1863+ DO (s_mp_sqr_comba (& a , & r1 ));
1864+ DO (s_mp_sqr (& a , & r2 ));
1865+ EXPECT (mp_cmp (& r1 , & r2 ) == MP_EQ );
1866+ }
1867+ }
1868+
1869+ mp_clear_multi (& a , & r1 , & r2 , NULL );
1870+ return EXIT_SUCCESS ;
1871+ LBL_ERR :
1872+ mp_clear_multi (& a , & r1 , & r2 , NULL );
1873+ return EXIT_FAILURE ;
1874+ }
1875+
18311876static int test_s_mp_mul_balance (void )
18321877{
18331878 mp_int a , b , c ;
@@ -2328,6 +2373,8 @@ static int unit_tests(int argc, char **argv)
23282373 T1 (mp_xor , MP_XOR ),
23292374 T3 (s_mp_div_recursive , ONLY_PUBLIC_API , S_MP_DIV_RECURSIVE , S_MP_DIV_SCHOOL ),
23302375 T3 (s_mp_div_small , ONLY_PUBLIC_API , S_MP_DIV_SMALL , S_MP_DIV_SCHOOL ),
2376+ /* s_mp_mul_comba not (yet) testable because s_mp_mul branches to s_mp_mul_comba automatically */
2377+ T2 (s_mp_sqr_comba , ONLY_PUBLIC_API , S_MP_SQR_COMBA ),
23312378 T2 (s_mp_mul_balance , ONLY_PUBLIC_API , S_MP_MUL_BALANCE ),
23322379 T2 (s_mp_mul_karatsuba , ONLY_PUBLIC_API , S_MP_MUL_KARATSUBA ),
23332380 T2 (s_mp_sqr_karatsuba , ONLY_PUBLIC_API , S_MP_SQR_KARATSUBA ),
0 commit comments