Commit Graph

77 Commits

Author SHA1 Message Date
Tim Ledbetter
48a3a02238 LibCrypto: Make constructing a BigInteger from string fallible
Previously, constructing a `UnsignedBigInteger::from_base()` could
produce an incorrect result if the input string contained a valid
Base36 digit that was out of range of the given base. The same method
would also crash if the input string contained an invalid Base36 digit.
An error is now returned in both these cases.

Constructing a BigFraction from string is now also fallible, so that we
can handle the case where we are given an input string with invalid
digits.
2024-01-13 19:01:35 -07:00
Ali Mohammad Pur
5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30
Lucas CHOLLET
3f35ffb648 Userland: Prefer _string over _short_string
As `_string` can't fail anymore (since 3434412), there are no real
benefits to use the short variant in most cases.
2023-08-08 07:37:21 +02:00
Timothy Flynn
c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Linus Groh
09d40bfbb2 Everywhere: Use _{short_,}string to create Strings from literals 2023-02-25 20:51:49 +01:00
Timothy Flynn
f1de4f8872 Everywhere: Convert known short-strings to the infallible String factory
For now, this is limited to strings that are 3 bytes or less. We can use
7 bytes on 64-bit platforms, but we do not yet assume 64-bit for Lagom
hosts (e.g. wasm).
2023-01-20 14:24:12 -05:00
Timothy Flynn
3ad1f250e7 LibCrypto: Define *BigInteger::to_base to convert big integers to String 2023-01-15 01:00:20 +00:00
Timothy Flynn
0ddc2e1f50 LibCrypto+Everywhere: Rename *BigInteger::to_base to to_base_deprecated 2023-01-15 01:00:20 +00:00
Moustafa Raafat
ae2abcebbb Everywhere: Use C++ concepts instead of requires clauses 2022-12-09 11:25:30 +00:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Dan Klishch
17c9a3e8d9 AK+LibC+LibCrypto: Move FloatExtractor to AK/FloatingPoint.h 2022-11-03 20:17:09 -06:00
Moustafa Raafat
54b8a2b094 LibCrypto: Add a way to compare UnsignedBigInteger with double
This patch also make SignedBigInteger::compare_to_double make use
of the new function.
2022-11-02 22:04:34 -06:00
Moustafa Raafat
cfd684dc2f LibCrypto: Add SignedBigInteger::is_positive() 2022-10-22 19:14:14 +02:00
Lucas CHOLLET
4ab8ad2ed2 LibCrypto: Fix -0 and 0 non-equality
SignedBigInteger::operator==(const UnsignedBigInteger&) was rejecting
all negative value before testing for equality. It now accepts negative
zero and test for a value equality with the UnsignedBigInteger.
2022-09-15 14:08:21 +01:00
Lucas CHOLLET
d589898f5b LibCrypto: Add SignedBigInteger::negated_value()
Return the negated value of the current number.
2022-09-15 14:08:21 +01:00
Brian Gianforcaro
d0a1775369 Everywhere: Fix a variety of typos
Spelling fixes found by `codespell`.
2022-09-14 04:46:49 +00:00
davidot
791855deab LibCrypto+LibJS: Remove the create_from methods from BigInteger
Instead we just use a specific constructor. With this set of
constructors using curly braces for constructing is highly recommended.
As then it will not do too many implicit conversions which could lead to
unexpected loss of data or calling the much slower double constructor.

Also to ensure we don't feed (Un)SignedBigInteger infinities we throw
RangeError earlier for Durations.
2022-08-26 19:18:26 +01:00
davidot
528891bf69 LibCrypto: Add a constructor to (Un)SignedBigInteger taking a double
For now this will assume that the double given is exactly representable
as an integer, so no NaN, infinity or rounding.
2022-08-26 19:18:26 +01:00
davidot
c87d10365b LibCrypto: Make the constructors of (Un)SignedBigInteger templated
This means it can take any (un)signed word of size at most Word.
This means the constructor can be disambiguated if we were to add a
double constructor :^).

This requires a change in just one test.
2022-08-26 19:18:26 +01:00
davidot
77d71a5ffd LibCrypto: Add a rounding mode to UnsignedBigInteger::to_double
This allows using different options for rounding, like IEEE
roundTiesToEven, which is the mode that JS requires.

Also fix that the last word read from the bigint for the mantissa could
be shifted incorrectly leading to incorrect results.
2022-08-26 19:18:26 +01:00
davidot
8b8cee3172 LibCrypto: Implement a (mostly) proper to_double for UnsignedBigInteger
SignedBigInteger can immediately use this by just negating the double if
the sign bit is set.
For simple cases (below 2^53) we can just convert via an u64, however
above that we need to extract the top 53 bits and use those as the
mantissa.

This function currently does not behave exactly as the JS spec specifies
however it is much less naive than the previous implementation.
2022-08-24 23:27:17 +01:00
davidot
2290fbc2a0 LibCrypto: Make a VERIFY a static_assert since it only uses constants 2022-08-24 23:27:17 +01:00
davidot
b5c00830c2 LibCrypto: Add a way to compare a SignedBigInteger with a double
This supports any double value (except for NaNs) instead of having to
cast the double to some smaller type which doesn't work for very large
values.
2022-08-24 23:27:17 +01:00
Timothy Flynn
440d185666 LibCrypto: Expose UnsignedBigInteger's is_zero() in SignedBigInteger
Note we don't need to check the sign because negative zero became
disallowed in b0d6399f60.
2022-07-18 08:51:07 +01:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
sin-ack
7456904a39 Meta+Userland: Simplify some formatters
These are mostly minor mistakes I've encountered while working on the
removal of StringView(char const*). The usage of builder.put_string over
Format<FormatString>::format is preferrable as it will avoid the
indirection altogether when there's no formatting to be done. Similarly,
there is no need to do format(builder, "{}", number) when
builder.put_u64(number) works equally well.

Additionally a few Strings where only constant strings were used are
replaced with StringViews.
2022-07-12 23:11:35 +02:00
Lucas CHOLLET
dab814ea11 LibCrypto: Add the [[nodiscard]] qualifier in both BigInteger classes 2022-07-09 15:55:32 +00:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Lenny Maiorani
f1c452059c Libraries: Use default constructors/destructors in LibCrypto
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-10 18:04:26 -08:00
Timothy Flynn
b0d6399f60 LibCrypto: Do not allow signed big integers to be negative zero
If a big integer were to become negative zero, set the sign to instead
be positive. This prevents odd scenarios where users of signed big ints
would falsely think the result of some big int arithmetic is negative.
2022-02-06 15:49:54 +00:00
Timothy Flynn
0013227377 LibCrypto: Change UnsignedBigInteger parser to use a StringView
SignedBigInteger already accepts a StringView; let's avoid the heap
allocation in UnsignedBigInteger.
2022-01-31 17:50:54 +00:00
Nico Weber
e09f74903e LibCrypo: Simplify mixed-sign bitwise_or
No behavior change.
2022-01-18 20:04:06 +03:30
Nico Weber
2392f65345 LibCrypto: Remove some now-unused (and incorrect) methods
Removes the UnsignedBigInteger overloads of
SignedBigInteger::binary_{and,or,xor}(). They're now unused, and they
also didn't work when *this was negative.
2022-01-18 20:04:06 +03:30
Nico Weber
d9b6eb29bc LibCrypto+LibJS: Better bitwise binary_xor binop
We went through some trouble to make & and | work right. Reimplement ^
in terms of & and | to make ^ work right as well.

This is less fast than a direct implementation, but let's get things
working first.
2022-01-18 20:04:06 +03:30
Nico Weber
013799a4dd LibCrypto+LibJS: Better bigint bitwise_or binop
Similar to the bitwise_and change, but we have to be careful to
sign-extend two's complement numbers only up to the highest set bit
in the positive number.
2022-01-18 20:04:06 +03:30
Nico Weber
1f98639396 LibCrypto+LibJS: Better bigint bitwise_and binop
Bitwise and is defined in terms of two's complement, so some converting
needs to happen for SignedBigInteger's sign/magnitude representation to
work out.

UnsignedBigInteger::bitwise_not() is repurposed to convert all
high-order zero bits to ones up to a limit, for the two's complement
conversion to work.

Fixes test262/test/language/expressions/bitwise-and/bigint.js.
2022-01-18 20:04:06 +03:30
Nico Weber
945d962322 LibJS+LibCrypto: Fix SignedBitInteger::bitwise_not and use it in LibJS
Bitwise operators are defined on two's complement, but SignedBitInteger
uses sign-magnitude. Correctly convert between the two.

Let LibJS delegate to SignedBitInteger for bitwise_not, like it does
for all other bitwise_ operations on bigints.

No behavior change (LibJS is now the only client of
SignedBitInteger::bitwise_not()).
2022-01-18 20:04:06 +03:30
Nico Weber
ec37eadb39 LibCrypto: Add Formatter<SignedBigInteger>
Useful for seeing SignedBigInteger values in test failure messages.
2022-01-18 20:04:06 +03:30
mjz19910
10ec98dd38 Everywhere: Fix spelling mistakes 2022-01-07 15:44:42 +01:00
mjz19910
3102d8e160 Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
Nick Johnson
08e4a1a4dc AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
2021-12-21 22:13:51 +01:00
Andreas Kling
216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Linus Groh
58c6a156bf LibCrypto: Fix subtracting two negative SignedBigIntegers
Currently, we get the following results

    -1 - -2 = -1
    -2 - -1 =  1

Correct would be:

    -1 - -2 =  1
    -2 - -1 = -1

This was already attempted to be fixed in 7ed8970, but that change was
incorrect. This directly translates to LibJS BigInts having the same
incorrect behavior - it even was tested.
2021-11-16 10:06:53 +00:00
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Andreas Kling
6ad427993a Everywhere: Behaviour => Behavior 2021-09-07 13:53:14 +02:00
Idan Horowitz
bcdad57670 LibCrypto: Add naive implementation of {Un,}SignedBigInteger::to_double 2021-09-06 22:15:39 +01:00
Idan Horowitz
ff6ca0f02d LibCrypto: Add operator<= and operator>= to SignedBigInteger 2021-07-19 09:11:20 +01:00
Idan Horowitz
75d1ffea00 LibCrypto: Add the >= operator to UnsignedBigInteger 2021-07-12 19:05:17 +01:00