@@ -1516,6 +1516,70 @@ static int test_mp_log_n(void)
15161516 return EXIT_FAILURE ;
15171517}
15181518
1519+ static int test_mp_log (void )
1520+ {
1521+ mp_int a , base , bn , t ;
1522+ int lb , lb2 , i , j ;
1523+
1524+ DOR (mp_init_multi (& a , & base , & bn , & t , NULL ));
1525+
1526+ /*
1527+ The small values got tested above for mp_log_n already, leaving the big stuff
1528+ with bases larger than INT_MAX.
1529+ */
1530+
1531+ /* Edgecases a^b and -1+a^b (floor(log_2(256^129)) = 1032) */
1532+ for (i = 2 ; i < 256 ; i ++ ) {
1533+ mp_set_i32 (& a ,i );
1534+ for (j = 2 ; j < ((i /2 )+ 1 ); j ++ ) {
1535+ DO (mp_expt_n (& a , j , & bn ));
1536+ mp_set_i32 (& base ,j );
1537+ /* i^j a perfect power */
1538+ DO (mp_log (& bn , & a , & lb ));
1539+ DO (mp_expt_n (& a , lb , & t ));
1540+ if (mp_cmp (& t , & bn ) != MP_EQ ) {
1541+ fprintf (stderr ,"FAILURE mp_log for perf. power at i = %d, j = %d\n" , i , j );
1542+ goto LBL_ERR ;
1543+ }
1544+ /* -1 + i^j */
1545+ DO (mp_decr (& bn ));
1546+ DO (mp_log (& bn , & a , & lb2 ));
1547+ if (lb != (lb2 + 1 )) {
1548+ fprintf (stderr ,"FAILURE mp_log for -1 + i^j at i = %d, j = %d\n" , i , j );
1549+ goto LBL_ERR ;
1550+ }
1551+ }
1552+ }
1553+
1554+ /* Random a, base */
1555+ for (i = 1 ; i < 256 ; i ++ ) {
1556+ DO (mp_rand (& a , i ));
1557+ for (j = 1 ; j < ((i /2 )+ 1 ); j ++ ) {
1558+ DO (mp_rand (& base , j ));
1559+ DO (mp_log (& a , & base , & lb ));
1560+ DO (mp_expt_n (& base , lb , & bn ));
1561+ /* "bn" must be smaller than or equal to "a" at this point. */
1562+ if (mp_cmp (& bn , & a ) == MP_GT ) {
1563+ fprintf (stderr ,"FAILURE mp_log random in GT check" );
1564+ goto LBL_ERR ;
1565+ }
1566+ DO (mp_mul (& bn , & base , & bn ));
1567+ /* "bn" must be bigger than "a" at this point. */
1568+ if (mp_cmp (& bn , & a ) != MP_GT ) {
1569+ fprintf (stderr ,"FAILURE mp_log random in NOT GT check" );
1570+ goto LBL_ERR ;
1571+ }
1572+ }
1573+ }
1574+
1575+ mp_clear_multi (& a , & base , & bn , & t , NULL );
1576+ return EXIT_SUCCESS ;
1577+ LBL_ERR :
1578+ mp_clear_multi (& a , & base , & bn , & t , NULL );
1579+ return EXIT_FAILURE ;
1580+ }
1581+
1582+
15191583static int test_mp_incr (void )
15201584{
15211585 mp_int a , b ;
@@ -2373,6 +2437,7 @@ static int unit_tests(int argc, char **argv)
23732437 T1 (mp_get_u64 , MP_GET_I64 ),
23742438 T1 (mp_get_ul , MP_GET_L ),
23752439 T1 (mp_log_n , MP_LOG_N ),
2440+ T1 (mp_log , MP_LOG ),
23762441 T1 (mp_incr , MP_ADD_D ),
23772442 T1 (mp_invmod , MP_INVMOD ),
23782443 T1 (mp_is_square , MP_IS_SQUARE ),
0 commit comments