diff --git a/modules/math.js b/modules/math.js index 0294f8e..b3bbfcb 100644 --- a/modules/math.js +++ b/modules/math.js @@ -163,8 +163,6 @@ function modsqrt(n, modulus) { throw new Error("modulus must be prime to compute square root"); } - // TODO: add special case for modulus = 3 (mod 4) - n %= modulus; if (n < 0n) { n += modulus; @@ -176,6 +174,8 @@ function modsqrt(n, modulus) { throw new Error("radicand is not a quadratic residue of the modulus"); } else if (modulus === 2n) { return n % 2n; + } else if (modulus % 4n === 3n) { + return modpow(n, (modulus+1n)/4n, modulus); } return tonelliShanks(n, modulus);