1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

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.
This commit is contained in:
Linus Groh 2021-12-21 21:27:14 +01:00
parent 0c424c4dab
commit 61410e05eb

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/Forward.h> #include <AK/Forward.h>
#include <LibCrypto/Forward.h>
#include <LibJS/AST.h> #include <LibJS/AST.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Runtime/GlobalObject.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;
}
} }