add function for legendre symbol
This commit is contained in:
parent
e55b3c89b0
commit
3f7519bed4
|
|
@ -101,10 +101,19 @@ function isprime(n) {
|
||||||
return true;
|
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) {
|
function quadraticNonResidue(p) {
|
||||||
// TODO: consider randomizing this
|
// TODO: consider randomizing this
|
||||||
for (let a = 2n; a < p; a++) {
|
for (let a = 2n; a < p; a++) {
|
||||||
if (modpow(a, (p-1n)/2n, p) === p - 1n) {
|
if (legendreSymbol(a, p) === -1n) {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -158,7 +167,7 @@ function modsqrt(n, modulus) {
|
||||||
|
|
||||||
if (n % modulus === 0n) {
|
if (n % modulus === 0n) {
|
||||||
return 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");
|
throw new Error("radicand is not a quadratic residue of the modulus");
|
||||||
} else if (modulus === 2n) {
|
} else if (modulus === 2n) {
|
||||||
return n % 2n;
|
return n % 2n;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue