Skip to content

Truly independent web browser

LibJS+Utilities: Make C++ BigInts from 64-bit integers and words

Shannon Booth1268d46babb3
diff --git a/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Libraries/LibJS/Runtime/ArrayBuffer.cpp
index 12554d2b56f..a793f4e06b3 100644
--- a/Libraries/LibJS/Runtime/ArrayBuffer.cpp
+++ b/Libraries/LibJS/Runtime/ArrayBuffer.cpp
@@ -6,8 +6,6 @@
#include <AK/Array.h>
#include <AK/Atomic.h>
-#include <LibCrypto/BigInt/SignedBigInteger.h>
-#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/ArrayBufferABIConversions.h>
#include <LibJS/Runtime/FunctionObject.h>
@@ -298,9 +296,9 @@ Value ArrayBuffer::get_value_of_element_type(ElementType element_type, size_t by
case ElementType::Int32:
return Value(value_of_raw_bytes<i32>(raw_value));
case ElementType::BigUint64:
- return BigInt::create(vm(), Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { value_of_raw_bytes<u64>(raw_value) } });
+ return BigInt::create_from_u64(vm(), value_of_raw_bytes<u64>(raw_value));
case ElementType::BigInt64:
- return BigInt::create(vm(), Crypto::SignedBigInteger { value_of_raw_bytes<i64>(raw_value) });
+ return BigInt::create_from_i64(vm(), value_of_raw_bytes<i64>(raw_value));
case ElementType::Float16:
return Value(static_cast<double>(value_of_raw_bytes<f16>(raw_value)));
case ElementType::Float32:
diff --git a/Libraries/LibJS/Runtime/BigInt.cpp b/Libraries/LibJS/Runtime/BigInt.cpp
index 1b144c91a25..095458e9e46 100644
--- a/Libraries/LibJS/Runtime/BigInt.cpp
+++ b/Libraries/LibJS/Runtime/BigInt.cpp
@@ -5,7 +5,6 @@
*/
#include <AK/Vector.h>
-#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibJS/EmbeddingABIConversions.h>
#include <LibJS/Runtime/BigInt.h>
@@ -13,23 +12,25 @@ namespace JS {
using namespace EmbeddingABI;
-// Magnitudes cross the ABI as 32-bit words, least significant first, which is the order of the words of
-// Crypto::UnsignedBigInteger.
-GC::Ref<BigInt> BigInt::create(VM& vm, Crypto::SignedBigInteger big_integer)
+GC::Ref<BigInt> BigInt::create(VM& vm, bool is_negative, ReadonlySpan<u32> magnitude_words)
{
- auto magnitude_words = big_integer.unsigned_value().words();
- auto* bigint = js_bigint_create_from_magnitude(vm_to_abi(vm), big_integer.is_negative(), magnitude_words.data(), magnitude_words.size());
+ auto* bigint = js_bigint_create_from_magnitude(vm_to_abi(vm), is_negative, magnitude_words.data(), magnitude_words.size());
VERIFY(bigint);
return cell_ref_from_abi<BigInt>(bigint);
}
-Crypto::SignedBigInteger BigInt::big_integer() const
+GC::Ref<BigInt> BigInt::create_from_i64(VM& vm, i64 value)
{
- auto* bigint = bigint_to_abi(*this);
- Vector<u32> magnitude_words;
- magnitude_words.resize(js_bigint_magnitude_word_count(bigint));
- js_bigint_copy_magnitude_words(bigint, magnitude_words.data(), magnitude_words.size());
- return Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { magnitude_words }, js_bigint_is_negative(bigint) };
+ // NB: The magnitude of i64's minimum is 2^63, which fits in a u64.
+ auto magnitude = value < 0 ? static_cast<u64>(0) - static_cast<u64>(value) : static_cast<u64>(value);
+ u32 const words[] = { static_cast<u32>(magnitude), static_cast<u32>(magnitude >> 32) };
+ return create(vm, value < 0, words);
+}
+
+GC::Ref<BigInt> BigInt::create_from_u64(VM& vm, u64 value)
+{
+ u32 const words[] = { static_cast<u32>(value), static_cast<u32>(value >> 32) };
+ return create(vm, false, words);
}
GC::Ptr<BigInt> BigInt::create_from_string(VM& vm, Utf16View const& string)
@@ -37,6 +38,20 @@ GC::Ptr<BigInt> BigInt::create_from_string(VM& vm, Utf16View const& string)
return cell_from_abi<BigInt>(js_bigint_create_from_string(vm_to_abi(vm), utf16_view_to_abi(string)));
}
+bool BigInt::is_negative() const
+{
+ return js_bigint_is_negative(bigint_to_abi(*this));
+}
+
+Vector<u32> BigInt::magnitude_words() const
+{
+ auto* bigint = bigint_to_abi(*this);
+ Vector<u32> magnitude_words;
+ magnitude_words.resize(js_bigint_magnitude_word_count(bigint));
+ js_bigint_copy_magnitude_words(bigint, magnitude_words.data(), magnitude_words.size());
+ return magnitude_words;
+}
+
String BigInt::to_string() const
{
return owned_utf16_string_from_abi(js_bigint_to_string(bigint_to_abi(*this), 10)).to_utf8();
diff --git a/Libraries/LibJS/Runtime/BigInt.h b/Libraries/LibJS/Runtime/BigInt.h
index 8b2751b326e..63b8f68665e 100644
--- a/Libraries/LibJS/Runtime/BigInt.h
+++ b/Libraries/LibJS/Runtime/BigInt.h
@@ -10,7 +10,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Utf16String.h>
-#include <LibCrypto/BigInt/SignedBigInteger.h>
+#include <AK/Vector.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Ptr.h>
#include <LibJS/Export.h>
@@ -22,13 +22,17 @@ namespace JS {
class JS_API BigInt final : public EngineCell {
public:
- [[nodiscard]] static GC::Ref<BigInt> create(VM&, Crypto::SignedBigInteger);
+ // The magnitude is in 32-bit words, least significant first.
+ [[nodiscard]] static GC::Ref<BigInt> create(VM&, bool is_negative, ReadonlySpan<u32> magnitude_words);
+ [[nodiscard]] static GC::Ref<BigInt> create_from_i64(VM&, i64);
+ [[nodiscard]] static GC::Ref<BigInt> create_from_u64(VM&, u64);
// https://tc39.es/ecma262/#sec-stringtobigint
[[nodiscard]] static GC::Ptr<BigInt> create_from_string(VM&, Utf16View const&);
- // The integer lives in the Rust runtime's cell, so this returns a copy rather than a reference to it.
- Crypto::SignedBigInteger big_integer() const;
+ bool is_negative() const;
+ // The magnitude in 32-bit words, least significant first, without leading zero words.
+ Vector<u32> magnitude_words() const;
// The decimal digits, after a "-" if it is negative, which is ToString(bigint).
String to_string() const;
diff --git a/Libraries/LibJS/Rust/src/embedding/bigint.rs b/Libraries/LibJS/Rust/src/embedding/bigint.rs
index fda0b30090a..3a79d6679b9 100644
--- a/Libraries/LibJS/Rust/src/embedding/bigint.rs
+++ b/Libraries/LibJS/Rust/src/embedding/bigint.rs
@@ -4,9 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-//! BigInt values. A magnitude crosses as 32-bit words, least significant first, which is the word order of
-//! Crypto::UnsignedBigInteger's words() and of its constructor from them. Every function runs on the thread that owns
-//! the VM.
+//! BigInt values. A magnitude crosses as 32-bit words, least significant first. Every function runs on the thread
+//! that owns the VM.
use num_bigint::Sign;
diff --git a/Libraries/LibWasm/Tests/Executor/test-v128-global-bigint.js b/Libraries/LibWasm/Tests/Executor/test-v128-global-bigint.js
new file mode 100644
index 00000000000..b502e55b0d5
--- /dev/null
+++ b/Libraries/LibWasm/Tests/Executor/test-v128-global-bigint.js
@@ -0,0 +1,22 @@
+test("a v128 global is the unsigned 128-bit integer with the low half first", () => {
+ // (global (export "one") v128 (v128.const i64x2 1 0))
+ // (global (export "high_half") v128 (v128.const i64x2 0 1))
+ // (global (export "all_bits") v128 (v128.const i64x2 -1 -1))
+ // prettier-ignore
+ const binary = new Uint8Array([
+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x06, 0x40, 0x03, 0x7b,
+ 0x00, 0xfd, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x7b, 0x00, 0xfd, 0x0c,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x0b, 0x7b, 0x00, 0xfd, 0x0c, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0x0b, 0x07, 0x1e, 0x03, 0x03, 0x6f, 0x6e, 0x65, 0x03, 0x00, 0x09,
+ 0x68, 0x69, 0x67, 0x68, 0x5f, 0x68, 0x61, 0x6c, 0x66, 0x03, 0x01, 0x08,
+ 0x61, 0x6c, 0x6c, 0x5f, 0x62, 0x69, 0x74, 0x73, 0x03, 0x02
+ ]);
+
+ const module = parseWebAssemblyModule(binary);
+ expect(module.getExport("one")).toBe(1n);
+ expect(module.getExport("high_half")).toBe(1n << 64n);
+ expect(module.getExport("all_bits")).toBe((1n << 128n) - 1n);
+});
diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp
index 738c5268414..8e9dfa842ca 100644
--- a/Tests/LibWasm/test-wasm.cpp
+++ b/Tests/LibWasm/test-wasm.cpp
@@ -481,11 +481,11 @@ static JS::ThrowCompletionOr<JS::Value> to_js_value(JS::VM& vm, Wasm::Runtime::V
case ValueKind::I32:
return JS::Value(static_cast<double>(value.to_i32()));
case ValueKind::I64:
- return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { value.to_i64() }));
+ return JS::Value(JS::BigInt::create_from_i64(vm, value.to_i64()));
case ValueKind::F32:
return JS::Value(static_cast<double>(value.f32_bits()));
case ValueKind::F64:
- return JS::Value(JS::BigInt::create(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { value.f64_bits() } }));
+ return JS::Value(JS::BigInt::create_from_u64(vm, value.f64_bits()));
case ValueKind::V128: {
auto buffer = TRY(JS::ArrayBuffer::create(*vm.current_realm(), 16));
u64 halves[2] = { value.low, value.high };
@@ -525,14 +525,15 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::get_export)
case ValueKind::I32:
return JS::Value(static_cast<double>(global.to_i32()));
case ValueKind::I64:
- return JS::BigInt::create(vm, Crypto::SignedBigInteger { global.to_i64() });
+ return JS::BigInt::create_from_i64(vm, global.to_i64());
case ValueKind::F32:
return JS::Value(static_cast<double>(global.to_f32()));
case ValueKind::F64:
return JS::Value(global.to_f64());
case ValueKind::V128: {
- u64 halves[2] = { global.low, global.high };
- return JS::BigInt::create(vm, Crypto::SignedBigInteger::import_data({ reinterpret_cast<u8 const*>(halves), sizeof(halves) }));
+ // NB: The vector is the unsigned 128-bit integer with the low half first.
+ u32 const words[] = { static_cast<u32>(global.low), static_cast<u32>(global.low >> 32), static_cast<u32>(global.high), static_cast<u32>(global.high >> 32) };
+ return JS::BigInt::create(vm, false, words);
}
case ValueKind::Reference:
return reference_to_js_value(global, type);
diff --git a/Utilities/wasm.cpp b/Utilities/wasm.cpp
index eb1c5f3f799..f08cfb9dfe0 100644
--- a/Utilities/wasm.cpp
+++ b/Utilities/wasm.cpp
@@ -21,7 +21,6 @@
#include <LibCore/ElapsedTimer.h>
#include <LibCore/EventLoop.h>
#include <LibCore/MappedFile.h>
-#include <LibCrypto/BigInt/SignedBigInteger.h>
#include <LibFileSystem/FileSystem.h>
#include <LibGC/Function.h>
#include <LibGC/Root.h>
@@ -422,8 +421,9 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
js_arguments.append(JS::Value(argument.to_f64()));
break;
case ValueKind::V128: {
- u64 halves[2] = { argument.low, argument.high };
- js_arguments.append(JS::BigInt::create(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger::import_data({ reinterpret_cast<u8 const*>(halves), sizeof(halves) }) }));
+ // NB: The vector is the unsigned 128-bit integer with the low half first.
+ u32 const words[] = { static_cast<u32>(argument.low), static_cast<u32>(argument.low >> 32), static_cast<u32>(argument.high), static_cast<u32>(argument.high >> 32) };
+ js_arguments.append(JS::BigInt::create(vm, false, words));
break;
}
case ValueKind::Reference:
@@ -508,9 +508,18 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
case ValueKind::F64:
return WasmValue::from_f64(TRY(value.to_double(vm)));
case ValueKind::V128: {
+ // NB: The vector is the BigInt modulo 2^128, as BigInt.asUintN(128, value) gives it.
auto bigint = TRY(value.to_bigint(vm));
+ auto words = bigint->magnitude_words();
u64 halves[2] = {};
- (void)bigint->big_integer().unsigned_value().export_data({ reinterpret_cast<u8*>(halves), sizeof(halves) });
+ for (size_t i = 0; i < min(words.size(), 4uz); ++i)
+ halves[i / 2] |= static_cast<u64>(words[i]) << (32 * (i % 2));
+ if (bigint->is_negative()) {
+ halves[0] = ~halves[0];
+ halves[1] = ~halves[1];
+ if (++halves[0] == 0)
+ ++halves[1];
+ }
return WasmValue::from_v128(halves[0], halves[1]);
}
case ValueKind::Reference: