montgomery: fix fieldLen

This commit is contained in:
Paul Miller 2023-01-30 04:56:07 +00:00
parent 3374a70f47
commit 4780850748
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B

@ -150,7 +150,8 @@ export function montgomery(curveDef: CurveType): CurveFn {
// This is very ugly way, but it works because fieldLen-1 is outside of bounds for X448, so this becomes NOOP
// fieldLen - scalaryBytes = 1 for X448 and = 0 for X25519
const u = ensureBytes(uEnc, montgomeryBytes);
u[fieldLen - 1] &= 127; // 0b0111_1111
// u[fieldLen-1] crashes QuickJS (TypeError: out-of-bound numeric index)
if (fieldLen === montgomeryBytes) u[fieldLen - 1] &= 127; // 0b0111_1111
return bytesToNumberLE(u);
}
function decodeScalar(n: Hex): bigint {