@@ -787,11 +787,12 @@ int bi_multiply( bi * res, bi lhs, bi rhs )
787
787
bi_increase (& product , curr );
788
788
}
789
789
790
- res -> sign = lhs .sign * rhs .sign ;
790
+ product . sign = lhs .sign * rhs .sign ;
791
791
792
792
bi_destroy (curr );
793
793
bi_destroy (* res );
794
794
res -> data = product .data ;
795
+ res -> sign = product .sign ;
795
796
for ( res -> num_limbs = product .num_limbs ; res -> num_limbs > 0 ; -- res -> num_limbs )
796
797
{
797
798
if ( res -> data [res -> num_limbs - 1 ] != 0 )
@@ -873,14 +874,13 @@ int bi_divide( bi * quo, bi * rem, bi numerator, bi denominator )
873
874
bi_negate (& negative_quotient );
874
875
bi_copy (quo , negative_quotient );
875
876
876
- bi_negate (& negative_remainder );
877
- bi_add (rem , negative_remainder , denominator );
878
-
879
877
one = bi_init (0 );
880
878
bi_one (& one );
881
879
temp = bi_init (0 );
882
880
bi_copy (& temp , * quo );
883
881
bi_subtract (quo , temp , one );
882
+ bi_multiply (& temp , * quo , denominator );
883
+ bi_subtract (rem , numerator , temp );
884
884
bi_destroy (temp );
885
885
bi_destroy (one );
886
886
@@ -1389,23 +1389,9 @@ int bi_xgcd( bi * x, bi * y, bi * g, bi a, bi b )
1389
1389
{
1390
1390
bi_divide (& quotient , & remainder , old_r , r );
1391
1391
1392
- /*
1393
- printf("numerator: "); bi_print_bitstring(old_r); printf("\n");
1394
- printf("divisor: "); bi_print_bitstring(r); printf("\n");
1395
- printf("quotient: "); bi_print_bitstring(quotient); printf("\n");
1396
- printf("remainder: "); bi_print_bitstring(remainder); printf("\n");
1397
- getchar();
1398
- */
1399
-
1400
1392
/**/
1401
1393
bi_copy (& old_r , r );
1402
1394
bi_copy (& r , remainder );
1403
- /*/
1404
- bi_copy(&temp2, old_r);
1405
- bi_copy(&old_r, r);
1406
- bi_multiply(&temp, quotient, r);
1407
- bi_subtract(&r, temp2, temp);
1408
- */
1409
1395
1410
1396
bi_copy (& temp2 , old_s );
1411
1397
bi_copy (& old_s , s );
@@ -1509,16 +1495,19 @@ int bi_naf( bi * respos, bi * resneg, bi source )
1509
1495
}
1510
1496
1511
1497
/**
1512
- * bi_modexp
1513
- * Raise the base to the given power but compute only the residue of
1514
- * that function modulo the modulus. Uses the square-and-multiply
1515
- * routine.
1498
+ * bi_modexp_naf
1499
+ * Perform modular exponentiation except first transforms the power into NAF.
1500
+ * This sparsity transformation leads to fewer multiplications and
1501
+ * the tradeoff is positive when the integers are large (sortof
1502
+ * bigger than 250 bits).
1516
1503
*/
1517
- int bi_modexp ( bi * res , bi base , bi power , bi modulus )
1504
+ int bi_modexp_naf ( bi * res , bi base , bi power , bi modulus )
1518
1505
{
1519
1506
int bitsize ;
1507
+ int negones_bitsize ;
1520
1508
int i ;
1521
1509
bi temp , temp2 ;
1510
+ bi posones , negones ;
1522
1511
bi inverse ;
1523
1512
1524
1513
if ( base .sign == -1 )
@@ -1545,46 +1534,63 @@ int bi_modexp( bi * res, bi base, bi power, bi modulus )
1545
1534
return 1 ;
1546
1535
}
1547
1536
1548
- if ( bi_bitsize (power ) > 20 && bi_bitsize (modulus ) > 250 )
1549
- {
1550
- return bi_modexp_naf (res , base , power , modulus );
1551
- }
1552
-
1553
1537
temp = bi_init (0 );
1538
+ posones = bi_init (power .num_limbs * sizeof (unsigned long int ) * 8 );
1539
+ negones = bi_init (power .num_limbs * sizeof (unsigned long int ) * 8 );
1540
+ bi_naf (& posones , & negones , power );
1541
+ negones_bitsize = bi_bitsize (negones );
1554
1542
1543
+ inverse = bi_init (0 );
1544
+ bi_modinverse (& inverse , base , modulus );
1555
1545
bi_one (res );
1556
1546
1557
1547
bitsize = bi_bitsize (power );
1558
- for ( i = bitsize ; i >= 0 ; -- i )
1548
+ for ( i = bitsize ; i >= negones_bitsize ; -- i )
1559
1549
{
1560
1550
bi_multiply (& temp , * res , * res );
1561
1551
1562
- if ( bi_getbit (power , i ) == 1 )
1552
+ if ( bi_getbit (posones , i ) == 1 )
1553
+ {
1554
+ bi_multiply (& temp , temp , base );
1555
+ }
1556
+
1557
+ bi_modulo (res , temp , modulus );
1558
+ }
1559
+ for ( ; i >= 0 ; -- i )
1560
+ {
1561
+ bi_multiply (& temp , * res , * res );
1562
+
1563
+ if ( bi_getbit (posones , i ) == 1 )
1563
1564
{
1564
- bi_multiply (& temp , * res , base );
1565
+ bi_multiply (& temp , temp , base );
1565
1566
}
1566
1567
1568
+ if ( bi_getbit (negones , i ) == 1 )
1569
+ {
1570
+ bi_multiply (& temp , temp , inverse );
1571
+ }
1567
1572
bi_modulo (res , temp , modulus );
1568
1573
}
1569
1574
1570
1575
bi_destroy (temp );
1576
+ bi_destroy (inverse );
1577
+ bi_destroy (posones );
1578
+ bi_destroy (negones );
1571
1579
1572
1580
return 1 ;
1573
1581
}
1582
+
1574
1583
/**
1575
- * bi_modexp_naf
1576
- * Does the same thing except first transforms the power into NAF.
1577
- * This sparsity transformation leads to fewer multiplications and
1578
- * the tradeoff is positive when the integers are large (sortof
1579
- * bigger than 250 bits).
1584
+ * bi_modexp
1585
+ * Raise the base to the given power but compute only the residue of
1586
+ * that function modulo the modulus. Uses the square-and-multiply
1587
+ * routine.
1580
1588
*/
1581
- int bi_modexp_naf ( bi * res , bi base , bi power , bi modulus )
1589
+ int bi_modexp ( bi * res , bi base , bi power , bi modulus )
1582
1590
{
1583
1591
int bitsize ;
1584
- int negones_bitsize ;
1585
1592
int i ;
1586
1593
bi temp , temp2 ;
1587
- bi posones , negones ;
1588
1594
bi inverse ;
1589
1595
1590
1596
if ( base .sign == -1 )
@@ -1611,48 +1617,30 @@ int bi_modexp_naf( bi * res, bi base, bi power, bi modulus )
1611
1617
return 1 ;
1612
1618
}
1613
1619
1620
+ if ( bi_bitsize (power ) > 20 && bi_bitsize (modulus ) > 250 )
1621
+ {
1622
+ return bi_modexp_naf (res , base , power , modulus );
1623
+ }
1624
+
1614
1625
temp = bi_init (0 );
1615
- posones = bi_init (power .num_limbs * sizeof (unsigned long int ) * 8 );
1616
- negones = bi_init (power .num_limbs * sizeof (unsigned long int ) * 8 );
1617
- bi_naf (& posones , & negones , power );
1618
- negones_bitsize = bi_bitsize (negones );
1619
1626
1620
- inverse = bi_init (0 );
1621
- bi_modinverse (& inverse , base , modulus );
1622
1627
bi_one (res );
1623
1628
1624
1629
bitsize = bi_bitsize (power );
1625
- for ( i = bitsize ; i >= negones_bitsize ; -- i )
1630
+ for ( i = bitsize - 1 ; i >= 0 ; -- i )
1626
1631
{
1627
1632
bi_multiply (& temp , * res , * res );
1633
+ bi_modulo (& temp , temp , modulus );
1628
1634
1629
- if ( bi_getbit (posones , i ) == 1 )
1630
- {
1631
- bi_multiply (& temp , temp , base );
1632
- }
1633
-
1634
- bi_modulo (res , temp , modulus );
1635
- }
1636
- for ( ; i >= 0 ; -- i )
1637
- {
1638
- bi_multiply (& temp , * res , * res );
1639
-
1640
- if ( bi_getbit (posones , i ) == 1 )
1635
+ if ( bi_getbit (power , i ) == 1 )
1641
1636
{
1642
1637
bi_multiply (& temp , temp , base );
1643
1638
}
1644
1639
1645
- if ( bi_getbit (negones , i ) == 1 )
1646
- {
1647
- bi_multiply (& temp , temp , inverse );
1648
- }
1649
1640
bi_modulo (res , temp , modulus );
1650
1641
}
1651
1642
1652
1643
bi_destroy (temp );
1653
- bi_destroy (inverse );
1654
- bi_destroy (posones );
1655
- bi_destroy (negones );
1656
1644
1657
1645
return 1 ;
1658
1646
}
@@ -1689,8 +1677,8 @@ int bi_modinverse( bi * res, bi element, bi modulus )
1689
1677
* Perform a single Miller-Rabin trial of the integer with base as
1690
1678
* test.
1691
1679
* @returns
1692
- * * 0 if the integer is definitely composite, 1 if it might be a
1693
- * prime
1680
+ * * 0 -- definitely composite; or
1681
+ * * 1 -- possibly prime
1694
1682
*/
1695
1683
int bi_miller_rabin_trial ( bi integer , bi base )
1696
1684
{
@@ -1745,6 +1733,7 @@ int bi_miller_rabin_trial( bi integer, bi base )
1745
1733
{
1746
1734
bi_copy (& temp , x );
1747
1735
bi_modexp (& x , temp , two , integer );
1736
+
1748
1737
if ( bi_is_one (x ) )
1749
1738
{
1750
1739
bi_destroy (x );
@@ -1753,8 +1742,9 @@ int bi_miller_rabin_trial( bi integer, bi base )
1753
1742
bi_destroy (one );
1754
1743
bi_destroy (two );
1755
1744
bi_destroy (temp );
1756
- return 0 ;
1745
+ return 1 ;
1757
1746
}
1747
+
1758
1748
if ( bi_compare (x , nm1 ) == 0 )
1759
1749
{
1760
1750
bi_destroy (x );
@@ -1795,7 +1785,7 @@ int bi_miller_rabin_trial( bi integer, bi base )
1795
1785
*/
1796
1786
int bi_is_prime ( bi integer , unsigned long int * random_ints , int certainty )
1797
1787
{
1798
- bi base , mod6 ;
1788
+ bi base , mod24 ;
1799
1789
int i ;
1800
1790
int composite ;
1801
1791
@@ -1818,17 +1808,20 @@ int bi_is_prime( bi integer, unsigned long int * random_ints, int certainty )
1818
1808
}
1819
1809
}
1820
1810
1821
- /* make sure the integer is 1 or -1 modulo 6 */
1822
- base = bi_cast (6 );
1823
- mod6 = bi_init (3 );
1824
- bi_modulo (& mod6 , integer , base );
1811
+ /* make sure the integer raised to 4 is 1 modulo 24 */
1812
+ base = bi_cast (24 );
1813
+ mod24 = bi_init (5 );
1814
+ bi_modulo (& mod24 , integer , base );
1815
+ bi_multiply (& mod24 , mod24 , mod24 );
1816
+ bi_multiply (& mod24 , mod24 , mod24 );
1817
+ bi_modulo (& mod24 , mod24 , base );
1825
1818
bi_destroy (base );
1826
- if ( mod6 .data [0 ] != 1 && mod6 . data [ 0 ] != 5 )
1819
+ if ( mod24 .data [0 ] != 1 )
1827
1820
{
1828
- bi_destroy (mod6 );
1821
+ bi_destroy (mod24 );
1829
1822
return 0 ;
1830
1823
}
1831
- bi_destroy (mod6 );
1824
+ bi_destroy (mod24 );
1832
1825
1833
1826
/* run Miller-Rabin trials */
1834
1827
composite = 1 ;
@@ -1865,7 +1858,6 @@ int bi_getbit( bi integer, int bit_index )
1865
1858
/**
1866
1859
* bi_print
1867
1860
* Send the decimal representation of this integer to stdout.
1868
- * TODO: debug
1869
1861
*/
1870
1862
int bi_print ( bi integer )
1871
1863
{
0 commit comments