@@ -29,18 +29,19 @@ def test_basic_example(self):
2929 self .assertEqual (pow (2 , result , 29 ), 22 )
3030 found_solution = True
3131 break
32-
33- self .assertTrue (found_solution ,
34- "Algorithm should find a solution within 5 attempts" )
32+
33+ self .assertTrue (
34+ found_solution , "Algorithm should find a solution within 5 attempts"
35+ )
3536
3637 def test_simple_cases (self ):
3738 """Test simple discrete log cases with known answers."""
3839 test_cases = [
39- (2 , 8 , 17 ), # 2^3 ≡ 8 (mod 17)
40- (5 , 3 , 7 ), # 5^5 ≡ 3 (mod 7)
41- (3 , 9 , 11 ), # 3^2 ≡ 9 (mod 11)
40+ (2 , 8 , 17 ), # 2^3 ≡ 8 (mod 17)
41+ (5 , 3 , 7 ), # 5^5 ≡ 3 (mod 7)
42+ (3 , 9 , 11 ), # 3^2 ≡ 9 (mod 11)
4243 ]
43-
44+
4445 for g , h , p in test_cases :
4546 # Try multiple times due to probabilistic nature
4647 found_solution = False
@@ -56,8 +57,7 @@ def test_no_solution_case(self):
5657 """Test case where no solution exists."""
5758 # 3^x ≡ 7 (mod 11) has no solution (verified by brute force)
5859 # The algorithm should return None or fail to find a solution
59- result = pollards_rho_discrete_log (3 , 7 , 11 )
60- if result is not None :
60+ if (result := pollards_rho_discrete_log (3 , 7 , 11 )) is not None :
6161 # If it returns a result, it must be wrong since no solution exists
6262 self .assertNotEqual (pow (3 , result , 11 ), 7 )
6363
@@ -67,7 +67,7 @@ def test_edge_cases(self):
6767 result = pollards_rho_discrete_log (1 , 1 , 7 )
6868 if result is not None :
6969 self .assertEqual (pow (1 , result , 7 ), 1 )
70-
70+
7171 # h = 1: g^x ≡ 1 (mod p) - looking for the multiplicative order
7272 result = pollards_rho_discrete_log (3 , 1 , 7 )
7373 if result is not None :
@@ -76,27 +76,27 @@ def test_edge_cases(self):
7676 def test_small_primes (self ):
7777 """Test with small prime moduli."""
7878 test_cases = [
79- (2 , 4 , 5 ), # 2^2 ≡ 4 (mod 5)
80- (2 , 3 , 5 ), # 2^? ≡ 3 (mod 5)
81- (2 , 1 , 3 ), # 2^2 ≡ 1 (mod 3)
82- (3 , 2 , 5 ), # 3^3 ≡ 2 (mod 5)
79+ (2 , 4 , 5 ), # 2^2 ≡ 4 (mod 5)
80+ (2 , 3 , 5 ), # 2^? ≡ 3 (mod 5)
81+ (2 , 1 , 3 ), # 2^2 ≡ 1 (mod 3)
82+ (3 , 2 , 5 ), # 3^3 ≡ 2 (mod 5)
8383 ]
84-
84+
8585 for g , h , p in test_cases :
8686 result = pollards_rho_discrete_log (g , h , p )
8787 if result is not None :
8888 # Verify the result is mathematically correct
8989 self .assertEqual (pow (g , result , p ), h )
90-
90+
9191 def test_larger_examples (self ):
9292 """Test with larger numbers to ensure algorithm scales."""
9393 # Test cases with larger primes
9494 test_cases = [
95- (2 , 15 , 31 ), # Find x where 2^x ≡ 15 (mod 31)
96- (3 , 10 , 37 ), # Find x where 3^x ≡ 10 (mod 37)
97- (5 , 17 , 41 ), # Find x where 5^x ≡ 17 (mod 41)
95+ (2 , 15 , 31 ), # Find x where 2^x ≡ 15 (mod 31)
96+ (3 , 10 , 37 ), # Find x where 3^x ≡ 10 (mod 37)
97+ (5 , 17 , 41 ), # Find x where 5^x ≡ 17 (mod 41)
9898 ]
99-
99+
100100 for g , h , p in test_cases :
101101 result = pollards_rho_discrete_log (g , h , p )
102102 if result is not None :
@@ -108,17 +108,18 @@ def test_multiple_runs_consistency(self):
108108 # and ensure any returned result is mathematically correct
109109 g , h , p = 2 , 22 , 29
110110 results = []
111-
111+
112112 for _ in range (10 ): # Run 10 times
113113 result = pollards_rho_discrete_log (g , h , p )
114114 if result is not None :
115115 results .append (result )
116116 self .assertEqual (pow (g , result , p ), h )
117-
117+
118118 # Should find at least one solution in 10 attempts
119- self .assertGreater (len (results ), 0 ,
120- "Algorithm should find solution in multiple attempts" )
119+ self .assertGreater (
120+ len (results ), 0 , "Algorithm should find solution in multiple attempts"
121+ )
121122
122123
123124if __name__ == "__main__" :
124- unittest .main (verbosity = 2 )
125+ unittest .main (verbosity = 2 )
0 commit comments