@@ -1584,6 +1584,192 @@ def test_pickling():
1584
1584
obj2 = pickle .loads (s )
1585
1585
assert obj == obj2
1586
1586
1587
+ def test_fmpz_mod ():
1588
+ from flint import fmpz_mod_ctx , fmpz , fmpz_mod
1589
+
1590
+ p_sml = 163
1591
+ p_med = 2 ** 127 - 1
1592
+ p_big = 2 ** 255 - 19
1593
+
1594
+ F_sml = fmpz_mod_ctx (p_sml )
1595
+ F_med = fmpz_mod_ctx (p_med )
1596
+ F_big = fmpz_mod_ctx (p_big )
1597
+
1598
+ # Context tests
1599
+ assert raises (lambda : fmpz_mod_ctx ("AAA" ), TypeError )
1600
+ assert raises (lambda : fmpz_mod_ctx (- 1 ), ValueError )
1601
+ assert F_sml .modulus () == p_sml
1602
+ assert F_med .modulus () == p_med
1603
+ assert F_big .modulus () == p_big
1604
+
1605
+ F_big_copy = fmpz_mod_ctx (p_big )
1606
+ assert F_big_copy == F_big
1607
+ assert F_big != F_sml
1608
+ assert hash (F_big_copy ) == hash (F_big )
1609
+ assert hash (F_big ) != hash (F_sml )
1610
+ assert F_big_copy != F_sml
1611
+ assert F_big_copy != "A"
1612
+
1613
+ assert repr (F_sml ) == "fmpz_mod_ctx(163)"
1614
+ assert str (F_sml ) == "Context for fmpz_mod with modulus: 163"
1615
+
1616
+ # Type tests
1617
+ assert raises (lambda : fmpz_mod (1 , "AAA" ), TypeError )
1618
+
1619
+ # Test for small, medium and large char.
1620
+ for F_test in [F_sml , F_med , F_big ]:
1621
+ test_mod = int (F_test .modulus ())
1622
+ test_x = (- 123 ) % test_mod # canonical value
1623
+ test_y = ((- 456 ) % test_mod )** 2 # non-canoncial value
1624
+
1625
+ F_test_copy = fmpz_mod_ctx (test_mod )
1626
+ F_other = fmpz_mod_ctx (11 )
1627
+
1628
+ assert raises (lambda : F_test (test_x ) > 0 , TypeError )
1629
+ assert raises (lambda : F_test (test_x ) >= 0 , TypeError )
1630
+ assert raises (lambda : F_test (test_x ) < 0 , TypeError )
1631
+ assert raises (lambda : F_test (test_x ) <= 0 , TypeError )
1632
+
1633
+ assert (test_x == F_test (test_x )) is True , f"{ test_x } , { F_test (test_x )} "
1634
+ assert (124 != F_test (test_x )) is True
1635
+ assert (F_test (test_x ) == test_x ) is True
1636
+ assert (F_test (test_x ) == test_x + test_mod ) is True
1637
+ assert (F_test (test_x ) == 1 ) is False
1638
+ assert (F_test (test_x ) != 1 ) is True
1639
+ assert (F_test (test_x ) == F_test (test_x )) is True
1640
+ assert (F_test (test_x ) == F_test (test_x + test_mod )) is True
1641
+ assert (F_test (test_x ) == F_test (1 )) is False
1642
+ assert (F_test (test_x ) != F_test (1 )) is True
1643
+
1644
+ assert (hash (F_test (test_x )) == hash (test_x )) is True
1645
+ assert (hash (F_test (F_test (test_x ))) == hash (test_x )) is True
1646
+ assert (hash (F_test (test_x )) == hash (1 )) is False
1647
+ assert (hash (F_test (test_x )) != hash (1 )) is True
1648
+ assert (hash (F_test (test_x )) == hash (F_test (test_x ))) is True
1649
+ assert (hash (F_test (test_x )) == hash (F_test (test_x + test_mod ))) is True
1650
+ assert (hash (F_test (test_x )) == hash (F_test (1 ))) is False
1651
+ assert (hash (F_test (test_x )) != hash (F_test (1 ))) is True
1652
+
1653
+ # Is one, zero
1654
+ assert (F_test (0 ) == 0 ) is True
1655
+ assert F_test (0 ).is_zero () is True
1656
+ assert not F_test (0 )
1657
+ assert not F_test (test_mod )
1658
+ assert F_test (1 ).is_one () is True
1659
+ assert F_test (test_mod + 1 ).is_one () is True
1660
+ assert F_test (1 ).is_one () is True
1661
+ assert F_test (2 ).is_one () is False
1662
+
1663
+ # int, str, repr
1664
+ assert str (F_test (11 )) == "11"
1665
+ assert str (F_test (- 1 )) == str (test_mod - 1 )
1666
+ assert repr (F_test (11 )) == f"fmpz_mod(11, { test_mod } )"
1667
+ assert repr (F_test (- 1 )) == f"fmpz_mod({ test_mod - 1 } , { test_mod } )"
1668
+
1669
+ assert + F_test (5 ) == F_test (5 )
1670
+
1671
+ # Arithmetic tests
1672
+
1673
+ # Negation
1674
+ assert - F_test (test_x ) == F_test (- test_x ) == (- test_x % test_mod )
1675
+ assert - F_test (1 ) == F_test (- 1 ) == F_test (test_mod - 1 )
1676
+
1677
+ # Addition
1678
+ assert F_test (test_x ) + F_test (test_y ) == F_test (test_x + test_y )
1679
+ assert F_test (test_x ) + F_test_copy (test_y ) == F_test (test_x + test_y )
1680
+ assert F_test (test_x ) + F_test (test_y ) == F_test_copy (test_x + test_y )
1681
+ assert raises (lambda : F_test (test_x ) + "AAA" , TypeError )
1682
+
1683
+ assert F_test (test_x ) + F_test (test_y ) == F_test (test_y ) + F_test (test_x )
1684
+ assert F_test (test_x ) + test_y == F_test (test_x + test_y )
1685
+ assert test_y + F_test (test_x ) == F_test (test_x + test_y )
1686
+ assert F_test (test_x ) + fmpz (test_y ) == F_test (test_y ) + F_test (test_x )
1687
+ assert raises (lambda : F_test (test_x ) + F_other (test_y ), ValueError )
1688
+
1689
+ # Subtraction
1690
+
1691
+ assert F_test (test_x ) - F_test (test_y ) == F_test (test_x - test_y )
1692
+ assert F_test (test_x ) - test_y == F_test (test_x - test_y )
1693
+ assert F_test (test_x ) - test_y == F_test (test_x ) - F_test (test_y )
1694
+ assert F_test (test_y ) - test_x == F_test (test_y ) - F_test (test_x )
1695
+ assert test_x - F_test (test_y ) == F_test (test_x ) - F_test (test_y )
1696
+ assert test_y - F_test (test_x ) == F_test (test_y ) - F_test (test_x )
1697
+ assert F_test (test_x ) - fmpz (test_y ) == F_test (test_x ) - F_test (test_y )
1698
+ assert raises (lambda : F_test (test_x ) - F_other (test_y ), ValueError )
1699
+ assert raises (lambda : F_test (test_x ) - "AAA" , TypeError )
1700
+
1701
+ # Multiplication
1702
+
1703
+ assert F_test (test_x ) * F_test (test_y ) == (test_x * test_y ) % test_mod
1704
+ assert F_test (test_x ) * test_y == (test_x * test_y ) % test_mod
1705
+ assert test_y * F_test (test_x ) == (test_x * test_y ) % test_mod
1706
+
1707
+ assert F_test (1 ) * F_test (test_x ) == F_test (1 * test_x )
1708
+ assert F_test (2 ) * F_test (test_x ) == F_test (2 * test_x )
1709
+ assert F_test (3 ) * F_test (test_x ) == F_test (3 * test_x )
1710
+ assert 1 * F_test (test_x ) == F_test (1 * test_x )
1711
+ assert 2 * F_test (test_x ) == F_test (2 * test_x )
1712
+ assert 3 * F_test (test_x ) == F_test (3 * test_x )
1713
+ assert F_test (test_x ) * 1 == F_test (1 * test_x )
1714
+ assert F_test (test_x ) * 2 == F_test (2 * test_x )
1715
+ assert F_test (test_x ) * 3 == F_test (3 * test_x )
1716
+ assert fmpz (1 ) * F_test (test_x ) == F_test (1 * test_x )
1717
+ assert fmpz (2 ) * F_test (test_x ) == F_test (2 * test_x )
1718
+ assert fmpz (3 ) * F_test (test_x ) == F_test (3 * test_x )
1719
+ assert raises (lambda : F_test (test_x ) * "AAA" , TypeError )
1720
+ assert raises (lambda : F_test (test_x ) * F_other (test_x ), ValueError )
1721
+
1722
+ # Exponentiation
1723
+
1724
+ assert F_test (0 )** 0 == pow (0 , 0 , test_mod )
1725
+ assert F_test (0 )** 1 == pow (0 , 1 , test_mod )
1726
+ assert F_test (0 )** 2 == pow (0 , 2 , test_mod )
1727
+ assert raises (lambda : F_test (0 )** (- 1 ), ZeroDivisionError )
1728
+ assert raises (lambda : F_test (0 )** ("AA" ), NotImplementedError )
1729
+
1730
+ assert F_test (test_x )** fmpz (0 ) == pow (test_x , 0 , test_mod )
1731
+ assert F_test (test_x )** fmpz (1 ) == pow (test_x , 1 , test_mod )
1732
+ assert F_test (test_x )** fmpz (2 ) == pow (test_x , 2 , test_mod )
1733
+ assert F_test (test_x )** fmpz (3 ) == pow (test_x , 3 , test_mod )
1734
+
1735
+ assert F_test (test_x )** 0 == pow (test_x , 0 , test_mod )
1736
+ assert F_test (test_x )** 1 == pow (test_x , 1 , test_mod )
1737
+ assert F_test (test_x )** 2 == pow (test_x , 2 , test_mod )
1738
+ assert F_test (test_x )** 3 == pow (test_x , 3 , test_mod )
1739
+ assert F_test (test_x )** 100 == pow (test_x , 100 , test_mod )
1740
+
1741
+ assert F_test (test_x )** (- 1 ) == pow (test_x , - 1 , test_mod )
1742
+ assert F_test (test_x )** (- 2 ) == pow (test_x , - 2 , test_mod )
1743
+ assert F_test (test_x )** (- 3 ) == pow (test_x , - 3 , test_mod )
1744
+ assert F_test (test_x )** (- 4 ) == pow (test_x , - 4 , test_mod )
1745
+
1746
+ # Inversion
1747
+
1748
+ assert raises (lambda : ~ F_test (0 ), ZeroDivisionError )
1749
+ assert ~ F_test (test_x ) == pow (test_x , - 1 , test_mod )
1750
+ assert ~ F_test (1 ) == pow (1 , - 1 , test_mod )
1751
+ assert ~ F_test (2 ) == pow (2 , - 1 , test_mod ), f"Broken!! { ~ F_test (2 )} , { pow (2 , - 1 , test_mod )} "
1752
+
1753
+ assert F_test (1 ).inverse (check = False ) == pow (1 , - 1 , test_mod )
1754
+ assert F_test (2 ).inverse (check = False ) == pow (2 , - 1 , test_mod )
1755
+ assert F_test (test_x ).inverse (check = False ) == pow (test_x , - 1 , test_mod )
1756
+
1757
+ # Division
1758
+ assert raises (lambda : F_test (1 ) / F_test (0 ), ZeroDivisionError )
1759
+ assert F_test (test_x ) / F_test (test_y ) == (test_x * pow (test_y , - 1 , test_mod )) % test_mod
1760
+ assert F_test (test_x ) / fmpz (test_y ) == (test_x * pow (test_y , - 1 , test_mod )) % test_mod
1761
+ assert F_test (test_x ) / test_y == (test_x * pow (test_y , - 1 , test_mod )) % test_mod
1762
+ assert raises (lambda : F_test (test_x ) / "AAA" , TypeError )
1763
+ assert raises (lambda : "AAA" / F_test (test_x ), TypeError )
1764
+ assert raises (lambda : F_other (test_x ) / F_test (test_x ), ValueError )
1765
+ assert raises (lambda : F_test (test_x ) // F_test (test_x ), TypeError )
1766
+ assert 1 / F_test (2 ) == pow (2 , - 1 , test_mod )
1767
+ assert 1 / F_test (test_x ) == pow (test_x , - 1 , test_mod )
1768
+ assert 1 / F_test (test_y ) == pow (test_y , - 1 , test_mod )
1769
+
1770
+ assert fmpz (test_y ) / F_test (test_x ) == (test_y * pow (test_x , - 1 , test_mod )) % test_mod
1771
+ assert test_y / F_test (test_x ) == (test_y * pow (test_x , - 1 , test_mod )) % test_mod
1772
+
1587
1773
1588
1774
all_tests = [
1589
1775
test_pyflint ,
@@ -1603,4 +1789,5 @@ def test_pickling():
1603
1789
test_nmod_poly ,
1604
1790
test_nmod_mat ,
1605
1791
test_arb ,
1792
+ test_fmpz_mod ,
1606
1793
]
0 commit comments