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
commit 61410e05eb
Notes: sideshowbarker 2024-07-17 22:22:25 +09:00

View file

@ -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;
}
}