mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +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:
parent
0c424c4dab
commit
61410e05eb
1 changed files with 10 additions and 0 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue