Skip to content

Commit 8f6ff46

Browse files
dynstwebmaster128
authored andcommitted
replace bn.js in Uint64 class
1 parent 7b37cb9 commit 8f6ff46

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

packages/math/src/integers.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-disable no-bitwise */
22

3-
// eslint-disable-next-line @typescript-eslint/naming-convention
4-
import BN from "bn.js";
5-
6-
const uint64MaxValue = new BN("18446744073709551615", 10, "be");
3+
const uint64MaxValue = 18446744073709551615n;
74

85
/** Internal interface to ensure all integer types can be used equally */
96
interface Integer {
@@ -201,21 +198,25 @@ export class Uint64 implements Integer, WithByteConverters {
201198
throw new Error("Invalid input length. Expected 8 bytes.");
202199
}
203200

204-
for (let i = 0; i < bytes.length; ++i) {
205-
if (!Number.isInteger(bytes[i]) || bytes[i] > 255 || bytes[i] < 0) {
206-
throw new Error("Invalid value in byte. Found: " + bytes[i]);
201+
const beBytes = endianess === "be" ? Array.from(bytes) : Array.from(bytes).reverse();
202+
let value = 0n;
203+
204+
for (const byte of beBytes) {
205+
value *= 256n;
206+
if (!Number.isInteger(byte) || byte > 255 || byte < 0) {
207+
throw new Error("Invalid value in byte. Found: " + byte);
207208
}
209+
value += BigInt(byte);
208210
}
209211

210-
const beBytes = endianess === "be" ? Array.from(bytes) : Array.from(bytes).reverse();
211-
return new Uint64(new BN(beBytes));
212+
return new Uint64(value);
212213
}
213214

214215
public static fromString(str: string): Uint64 {
215216
if (!str.match(/^[0-9]+$/)) {
216217
throw new Error("Invalid string format");
217218
}
218-
return new Uint64(new BN(str, 10, "be"));
219+
return new Uint64(BigInt(str));
219220
}
220221

221222
public static fromNumber(input: number): Uint64 {
@@ -227,45 +228,55 @@ export class Uint64 implements Integer, WithByteConverters {
227228
throw new Error("Input is not an integer");
228229
}
229230

230-
let bigint: BN;
231-
try {
232-
bigint = new BN(input);
233-
} catch {
231+
if (!Number.isSafeInteger(input)) {
234232
throw new Error("Input is not a safe integer");
235233
}
234+
235+
const bigint = BigInt(input);
236236
return new Uint64(bigint);
237237
}
238238

239-
private readonly data: BN;
239+
private readonly data: bigint;
240240

241-
private constructor(data: BN) {
242-
if (data.isNeg()) {
241+
private constructor(data: bigint) {
242+
if (data < 0n) {
243243
throw new Error("Input is negative");
244244
}
245-
if (data.gt(uint64MaxValue)) {
245+
if (data > uint64MaxValue) {
246246
throw new Error("Input exceeds uint64 range");
247247
}
248248
this.data = data;
249249
}
250250

251251
public toBytesBigEndian(): Uint8Array {
252-
return Uint8Array.from(this.data.toArray("be", 8));
252+
return this.toBytesLittleEndian().reverse();
253253
}
254254

255255
public toBytesLittleEndian(): Uint8Array {
256-
return Uint8Array.from(this.data.toArray("le", 8));
256+
const bytes = new Uint8Array(8);
257+
let value = this.data;
258+
for (let i = 0; i < bytes.length; i++) {
259+
bytes[i] = Number(value % 256n);
260+
value /= 256n;
261+
}
262+
263+
return bytes;
257264
}
258265

259266
public toString(): string {
260267
return this.data.toString(10);
261268
}
262269

263270
public toBigInt(): bigint {
264-
return BigInt(this.toString());
271+
return this.data;
265272
}
266273

267274
public toNumber(): number {
268-
return this.data.toNumber();
275+
const num = Number(this.data);
276+
if (!Number.isSafeInteger(num)) {
277+
throw new Error("Not safe integer");
278+
}
279+
return num;
269280
}
270281
}
271282

0 commit comments

Comments
 (0)