1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Tonelli-Shanks algorithm implementation for computing square roots modulo a prime.
//!
//! This library provides functions to compute square roots in the finite field Z/pZ
//! where p is an odd prime number.
/// Computes modular exponentiation: x^n mod p
///
/// Uses the square-and-multiply algorithm for efficient computation.
///
/// # Arguments
/// * `x` - The base
/// * `n` - The exponent
/// * `p` - The modulus (must be positive)
///
/// # Returns
/// The result of x^n mod p
///
/// # Examples
/// ```
/// use tonelli_rs::pow_mod;
///
/// assert_eq!(pow_mod(2, 10, 1000), 24);
/// assert_eq!(pow_mod(3, 5, 7), 5);
/// ```
/// Computes the Legendre symbol (a/p)
///
/// The Legendre symbol indicates whether a is a quadratic residue modulo p:
/// * 1 if a is a quadratic residue modulo p
/// * -1 if a is a quadratic non-residue modulo p
/// * 0 if a ≡ 0 (mod p)
///
/// # Arguments
/// * `a` - The number to check
/// * `p` - The prime modulus
///
/// # Returns
/// The Legendre symbol as an i32
///
/// # Examples
/// ```
/// use tonelli_rs::legendre_symbol;
///
/// assert_eq!(legendre_symbol(2, 7), 1); // 2 is a quadratic residue mod 7
/// assert_eq!(legendre_symbol(3, 7), -1); // 3 is a quadratic non-residue mod 7
/// ```
/// Finds the first quadratic non-residue modulo p
///
/// This function searches for the smallest positive integer z such that
/// z is a quadratic non-residue modulo p.
///
/// # Arguments
/// * `p` - The prime modulus
///
/// # Returns
/// The smallest quadratic non-residue modulo p
///
/// # Examples
/// ```
/// use tonelli_rs::find_quadratic_non_residue;
///
/// assert_eq!(find_quadratic_non_residue(7), 3);
/// ```
/// Computes a square root of n modulo p using the Tonelli-Shanks algorithm
///
/// This function finds r such that r² ≡ n (mod p) if n is a quadratic residue.
///
/// # Arguments
/// * `n` - The number to find the square root of
/// * `p` - The prime modulus
///
/// # Returns
/// * `Some(r)` if n is a quadratic residue modulo p, where r² ≡ n (mod p)
/// * `None` if n is not a quadratic residue modulo p
///
/// # Examples
/// ```
/// use tonelli_rs::tonelli_shanks;
///
/// // 2 is a quadratic residue modulo 7: 4² ≡ 2 (mod 7)
/// assert_eq!(tonelli_shanks(2, 7), Some(4));
///
/// // 3 is not a quadratic residue modulo 7
/// assert_eq!(tonelli_shanks(3, 7), None);
/// ```
/// Computes both square roots of n modulo p
///
/// If n is a quadratic residue modulo p, this function returns both square roots.
///
/// # Arguments
/// * `n` - The number to find the square roots of
/// * `p` - The prime modulus
///
/// # Returns
/// * `Some((r1, r2))` if n is a quadratic residue, where r1 and r2 are the two square roots
/// * `None` if n is not a quadratic residue
///
/// # Examples
/// ```
/// use tonelli_rs::square_roots;
///
/// let roots = square_roots(2, 7);
/// assert_eq!(roots, Some((3, 4))); // 3² ≡ 4² ≡ 2 (mod 7)
/// ```