|
1 | 1 | /*
|
2 | 2 | * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
|
3 |
| - * Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de |
| 3 | + * Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de |
4 | 4 | *
|
5 | 5 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
|
6 | 6 | * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
|
@@ -252,17 +252,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
|
252 | 252 | final long m = pinvArrayL[pIndex];
|
253 | 253 | final long q = ((x*m)>>>32);
|
254 | 254 | xModP = (int) (x - q * p);
|
255 |
| - if (DEBUG) Ensure.ensureSmaller(xModP, p); |
256 | 255 | if (xModP<0) xModP += p;
|
257 | 256 | if (DEBUG) {
|
258 | 257 | // 0 <= xModP < p
|
259 | 258 | Ensure.ensureSmallerEquals(0, xModP);
|
260 | 259 | Ensure.ensureSmaller(xModP, p);
|
261 | 260 |
|
262 |
| - int xModP2 = x % p; |
263 |
| - if (xModP2<0) xModP2 += p; |
264 |
| - if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2); |
265 |
| - Ensure.ensureEquals(xModP2, xModP); |
| 261 | + int correctMod = correctMod(x, p); |
| 262 | + if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod); |
| 263 | + Ensure.ensureEquals(correctMod, xModP); |
266 | 264 | }
|
267 | 265 | }
|
268 | 266 | if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
|
@@ -291,17 +289,15 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
|
291 | 289 | final long m = pinvArrayL[pIndex];
|
292 | 290 | final long q = ((x*m)>>>32);
|
293 | 291 | xModP = (int) (x - q * p);
|
294 |
| - if (DEBUG) Ensure.ensureGreaterEquals(xModP, 0); |
295 | 292 | if (xModP>=p) xModP -= p;
|
296 | 293 | if (DEBUG) {
|
297 | 294 | // 0 <= xModP < p
|
298 | 295 | Ensure.ensureSmallerEquals(0, xModP);
|
299 | 296 | Ensure.ensureSmaller(xModP, p);
|
300 | 297 |
|
301 |
| - int xModP2 = x % p; |
302 |
| - if (xModP2<0) xModP2 += p; |
303 |
| - if (xModP != xModP2) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but xModP2=" + xModP2); |
304 |
| - Ensure.ensureEquals(xModP2, xModP); |
| 298 | + int correctMod = correctMod(x, p); |
| 299 | + if (xModP != correctMod) LOG.debug("x=" + x + ", p=" + p + ": xModP=" + xModP + ", but correctMod=" + correctMod); |
| 300 | + Ensure.ensureEquals(correctMod, xModP); |
305 | 301 | }
|
306 | 302 | }
|
307 | 303 | if (xModP==x1Array[pIndex] || xModP==x2Array[pIndex]) {
|
@@ -397,6 +393,12 @@ private AQPair test(BigInteger A, BigInteger QRest0, int x) {
|
397 | 393 | return new Partial2Large(A, smallFactors, factor1.longValue(), factor2.longValue());
|
398 | 394 | }
|
399 | 395 |
|
| 396 | + private static final int correctMod(int x, int p) { |
| 397 | + int mod = x % p; |
| 398 | + // x < 0 then mod < 0, fix that |
| 399 | + return mod < 0 ? mod + p : mod; |
| 400 | + } |
| 401 | + |
400 | 402 | /**
|
401 | 403 | * Add factors that all Q(x) for the same a-parameter have in common.
|
402 | 404 | * These are the q-values whose product gives the a-parameter and 2 if d==2.
|
|
0 commit comments