add function for legendre symbol

This commit is contained in:
filifa 2025-12-11 23:49:34 -05:00
parent e55b3c89b0
commit 3f7519bed4
1 changed files with 11 additions and 2 deletions

View File

@ -101,10 +101,19 @@ function isprime(n) {
return true;
}
function legendreSymbol(a, p) {
let r = modpow(a, (p - 1n)/2n, p);
if (r === p - 1n) {
r = -1n;
}
return r;
}
function quadraticNonResidue(p) {
// TODO: consider randomizing this
for (let a = 2n; a < p; a++) {
if (modpow(a, (p-1n)/2n, p) === p - 1n) {
if (legendreSymbol(a, p) === -1n) {
return a;
}
}
@ -158,7 +167,7 @@ function modsqrt(n, modulus) {
if (n % modulus === 0n) {
return 0n;
} else if (modpow(n, (modulus-1n)/2n, modulus) !== 1n) {
} else if (legendreSymbol(n, modulus) !== 1n) {
throw new Error("radicand is not a quadratic residue of the modulus");
} else if (modulus === 2n) {
return n % 2n;