From c6b2a07326c739d7b7a9b81102579b567cdbeccb Mon Sep 17 00:00:00 2001 From: Michiel Visser Date: Fri, 10 Nov 2023 15:50:39 +0100 Subject: [PATCH] LibCrypto: Add static_assert to check that A = -3 mod p This is required for some optimization made in the file. While this should always be the case for the SECP256r1 curve, it is good to check it anyway. --- Userland/Libraries/LibCrypto/Curves/SECP256r1.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Userland/Libraries/LibCrypto/Curves/SECP256r1.cpp b/Userland/Libraries/LibCrypto/Curves/SECP256r1.cpp index 4c7e0f88be..96b3d4693f 100644 --- a/Userland/Libraries/LibCrypto/Curves/SECP256r1.cpp +++ b/Userland/Libraries/LibCrypto/Curves/SECP256r1.cpp @@ -52,6 +52,9 @@ static constexpr u256 A { { 0xfffffffffffffffcull, 0x00000000ffffffffull, 0x0000 static constexpr u256 B { { 0x3bce3c3e27d2604bull, 0x651d06b0cc53b0f6ull, 0xb3ebbd55769886bcull, 0x5ac635d8aa3a93e7ull } }; static constexpr u256 ORDER { { 0xf3b9cac2fc632551ull, 0xbce6faada7179e84ull, 0xffffffffffffffffull, 0xffffffff00000000ull } }; +// Verify that A = -3 mod p, which is required for some optimizations +static_assert(A == PRIME - 3); + // Precomputed helper values for reduction and Montgomery multiplication static constexpr u256 REDUCE_PRIME = u256 { 0 } - PRIME; static constexpr u256 REDUCE_ORDER = u256 { 0 } - ORDER;