diff options
author | Linus Groh <mail@linusgroh.de> | 2021-12-21 21:27:14 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-22 11:27:31 +0100 |
commit | 61410e05ebcdb2b4eed97ed8d4f5e0864836e8a4 (patch) | |
tree | 1d608537e427bef3da8cbb54594dceef9eb4c269 /Userland | |
parent | 0c424c4dabfaaa7a1ccd52cae58375ebb6f2fb2e (diff) | |
download | serenity-61410e05ebcdb2b4eed97ed8d4f5e0864836e8a4.zip |
LibJS: Add modulo(x, y) overload for Crypto::{Unsigned,Signed}BigInteger
Just like with integral and floating numbers, doing it manually is
error-prone: when we get a negative remainder, y has to be added to the
result to match the spec's modulo.
Solve this by just adding another (templated) overload to also permit
using the function for LibCrypto BigInts.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/AbstractOperations.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index e9e6a7595d..de155d7e78 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Forward.h> +#include <LibCrypto/Forward.h> #include <LibJS/AST.h> #include <LibJS/Forward.h> #include <LibJS/Runtime/GlobalObject.h> @@ -91,4 +92,13 @@ auto modulo(T x, U y) requires(IsArithmetic<T>, IsArithmetic<U>) } } +auto modulo(Crypto::BigInteger auto const& x, Crypto::BigInteger auto const& y) +{ + VERIFY(y != "0"_bigint); + auto result = x.divided_by(y).remainder; + if (result.is_negative()) + result = result.plus(y); + return result; +} + } |