Skip to content

Commit aad8207

Browse files
committed
permit bigger modular power arguments, long instead of int
1 parent 2aca2ad commit aad8207

4 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/main/java/de/tilman_neumann/jml/modular/LegendreSymbol.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ public int EulerFormula(BigInteger a, int p) {
5656
}
5757

5858
/**
59-
* Computes the Legendre symbol L(a|p) via Eulers formula for a, p int.
60-
* p must be an odd prime.
59+
* Computes the Legendre symbol L(a|p) via Eulers formula for <code>a</code> long, <code>p</code> an odd prime int.
6160
*
6261
* Eulers formula with int p is quite fast, but the Jacobi symbol may be faster.
6362
*
6463
* @param a
6564
* @param p
6665
* @return Legendre symbol L(a|p)
6766
*/
68-
public int EulerFormula(int a, int p) {
67+
public int EulerFormula(long a, int p) {
6968
int modPow = mpe.modPow(a, (p-1)>>1, p);
7069
return (modPow>1) ? modPow-p : modPow;
7170
}

src/main/java/de/tilman_neumann/jml/modular/ModularPower.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public int modPow(BigInteger a, long b, int c) {
5555
return modPowCore(a.mod(BigInteger.valueOf(c)).longValue(), b, c);
5656
}
5757

58+
/**
59+
* Computes a^b (mod c) for <code>a, b</code> long, <code>c</code> int. Very fast.
60+
* @param a
61+
* @param b
62+
* @param c
63+
* @return a^b (mod c)
64+
*/
65+
public int modPow(long a, long b, int c) {
66+
return modPowCore(a % c, b, c);
67+
}
68+
5869
/**
5970
* Computes a^b (mod c) for <code>a</code> int, <code>b</code> long, <code>c</code> int. Very fast.
6071
* @param a

src/main/java/de/tilman_neumann/jml/modular/ModularSqrt.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ private int bruteForce(BigInteger n, int p) {
191191
* Works for any odd p with t^2 == n (mod p) having solution t>0.
192192
* Implementation for arguments having int solutions.
193193
*
194-
* @param n
194+
* @param n a positive BigInteger having Jacobi(n|p) = 1
195195
* @param power p^exponent
196196
* @param last_power p^(exponent-1)
197197
* @param t solution of t^2 == n (mod p)
198198
*
199199
* @return sqrt of n (mod p^exponent)
200200
*/
201-
public int modularSqrtModPower(BigInteger n, int power, int last_power, int t) {
201+
public int modularSqrtModPower(BigInteger n, int power, int last_power, long t) {
202202
// Barthel's e = (power - 2*last_power + 1)/2
203203
int f = (power - (last_power<<1) + 1)>>1;
204204
// square root == n (mod p^exponent)

src/main/java/de/tilman_neumann/jml/modular/ModularSqrt31.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int case5Mod8(int n, int p) { // not private because used in tests
174174
* @param p
175175
* @return (the smaller) sqrt of n (mod p)
176176
*/
177-
int bruteForce(int n, int p) { // not private because used in tests
177+
private int bruteForce(int n, int p) { // not private because used in tests
178178
//boolean foundT = false;
179179
int nModP = n%p;
180180
int t;
@@ -204,14 +204,14 @@ int bruteForce(int n, int p) { // not private because used in tests
204204
* Works for any odd p with t^2 == n (mod p) having solution t>0.
205205
* Implementation for arguments having int solutions.
206206
*
207-
* @param n a positive integer having Jacobi(n|p) = 1
207+
* @param n a positive long having Jacobi(n|p) = 1
208208
* @param power p^exponent
209209
* @param last_power p^(exponent-1)
210210
* @param t solution of t^2 == n (mod p)
211211
*
212212
* @return sqrt of n (mod p^exponent)
213213
*/
214-
public int modularSqrtModPower(int n, int power, int last_power, int t) {
214+
public int modularSqrtModPower(long n, int power, int last_power, long t) {
215215
// Barthel's e = (power - 2*last_power + 1)/2
216216
int f = (power - (last_power<<1) + 1)>>1;
217217
// square root == n (mod p^exponent)

0 commit comments

Comments
 (0)