From 47808507488c1d345271449a80edb3836cc88c3e Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Mon, 30 Jan 2023 04:56:07 +0000 Subject: [PATCH] montgomery: fix fieldLen --- src/abstract/montgomery.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/abstract/montgomery.ts b/src/abstract/montgomery.ts index 69a6c8e..e8abc00 100644 --- a/src/abstract/montgomery.ts +++ b/src/abstract/montgomery.ts @@ -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 {