always return smallest sqrt
This commit is contained in:
parent
23848c3d0a
commit
b02774e725
|
|
@ -184,17 +184,24 @@ function modsqrt(n, modulus) {
|
||||||
n += modulus;
|
n += modulus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let r = null;
|
||||||
if (n % modulus === 0n) {
|
if (n % modulus === 0n) {
|
||||||
return 0n;
|
r = 0n;
|
||||||
} else if (modulus === 2n) {
|
} else if (modulus === 2n) {
|
||||||
return n % 2n;
|
r = n % 2n;
|
||||||
} else if (legendreSymbol(n, modulus) !== 1n) {
|
} else if (legendreSymbol(n, modulus) !== 1n) {
|
||||||
throw new Error("radicand is not a quadratic residue of the modulus");
|
throw new Error("radicand is not a quadratic residue of the modulus");
|
||||||
} else if (modulus % 4n === 3n) {
|
} else if (modulus % 4n === 3n) {
|
||||||
return modpow(n, (modulus+1n)/4n, modulus);
|
r = modpow(n, (modulus+1n)/4n, modulus);
|
||||||
|
} else {
|
||||||
|
r = tonelliShanks(n, modulus);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tonelliShanks(n, modulus);
|
if (modulus - r <= r) {
|
||||||
|
r = modulus - r;
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ord(n, modulus) {
|
function ord(n, modulus) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue