Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/ssz/src/type/uint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const BIGINT_2_POW_192 = BigInt(2) ** BigInt(192);
// const BIGINT_64_MAX = BigInt("0xffffffffffffffff");
const NUMBER_2_POW_32 = 2 ** 32;
const NUMBER_32_MAX = 0xffffffff;
/** Max high 32 bits of a uint64 that keeps the value <= Number.MAX_SAFE_INTEGER */
const NUMBER_SAFE_HIGH_MAX = 0x1fffff;

export interface UintNumberOpts {
/** Represent the value 2^64-1 as the symbolic value `+Infinity`. @see UintNumberType for a justification. */
Expand All @@ -37,6 +39,8 @@ export const uintBigintByteLens: UintBigintByteLen[] = [1, 2, 4, 8, 16, 32];
* As of 2021 performance of 'Number' is extremely faster than 'BigInt'. Some values are spec'ed as Uint64 but
* practically they will never exceed 53 bits, such as any unit time or simple counters. This type is an optimization
* for these cases, as UintNumber64 can represent any value between 0 and 2^53−1 as well as the max value 2^64-1.
* Deserializing any other value throws, since it can't be represented exactly as a Number. Packed arrays
* deserialized to a tree are not checked per element, they keep the original bytes so their root stays exact.
*/
export class UintNumberType extends BasicType<number> {
readonly typeName: string;
Expand Down Expand Up @@ -125,8 +129,12 @@ export class UintNumberType extends BasicType<number> {
case 8: {
const a = dataView.getUint32(start, true);
const b = dataView.getUint32(start + 4, true);
if (b === NUMBER_32_MAX && a === NUMBER_32_MAX && this.clipInfinity) {
return Infinity;
if (b > NUMBER_SAFE_HIGH_MAX) {
if (b === NUMBER_32_MAX && a === NUMBER_32_MAX && this.clipInfinity) {
return Infinity;
}
// Throw to prevent decimal precision errors downstream
throw Error("Invalid serialized number > MAX_SAFE_INTEGER");
}
return b * NUMBER_2_POW_32 + a;
}
Expand Down
23 changes: 22 additions & 1 deletion packages/ssz/test/unit/byType/uint/invalid.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, it} from "vitest";
import {UintBigintType, UintNumberType} from "../../../../src/index.ts";
import {ContainerType, UintBigintType, UintNumberType} from "../../../../src/index.ts";
import {UintNumberByteLen, uintBigintByteLens, uintNumberByteLens} from "../../../../src/type/uint.ts";
import {InvalidValue, runTypeTestInvalid} from "../runTypeTestInvalid.ts";

Expand Down Expand Up @@ -32,15 +32,36 @@ const invalidValuesForNumberAndBigint: InvalidValue[] = [
{id: "Object stringified", json: JSON.stringify({})},
];

const serializedOverMaxSafeInteger = "Invalid serialized number > MAX_SAFE_INTEGER";

runTypeTestInvalid({
type: new UintNumberType(8),
values: [
{id: "Number over Number.MAX_SAFE_INTEGER", json: String(Number.MAX_SAFE_INTEGER * 2)},
{id: "BigInt over Number.MAX_SAFE_INTEGER", json: BigInt(Number.MAX_SAFE_INTEGER * 2)},
{id: "Serialized 2^53", serialized: "0x0000000000002000", error: serializedOverMaxSafeInteger},
{id: "Serialized 2^64-2", serialized: "0xfeffffffffffffff", error: serializedOverMaxSafeInteger},
{id: "Serialized 2^64-1", serialized: "0xffffffffffffffff", error: serializedOverMaxSafeInteger},
...invalidValuesForNumberAndBigint,
],
});

runTypeTestInvalid({
type: new UintNumberType(8, {clipInfinity: true}),
values: [
{id: "Serialized 2^53", serialized: "0x0000000000002000", error: serializedOverMaxSafeInteger},
{id: "Serialized 2^64-2", serialized: "0xfeffffffffffffff", error: serializedOverMaxSafeInteger},
],
});

runTypeTestInvalid({
type: new ContainerType({a: new UintNumberType(8), b: new UintNumberType(8, {clipInfinity: true})}),
values: [
{id: "Field a 2^64-2", serialized: "0xfeffffffffffffff0000000000000000", error: serializedOverMaxSafeInteger},
{id: "Field b 2^64-2", serialized: "0x0000000000000000feffffffffffffff", error: serializedOverMaxSafeInteger},
],
});

runTypeTestInvalid({
type: new UintBigintType(8),
values: invalidValuesForNumberAndBigint,
Expand Down
2 changes: 2 additions & 0 deletions packages/ssz/test/unit/byType/uint/valid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ runTypeTestValid({
{json: 2 ** 32, serialized: "0x0000000001000000", root: null},
{json: 2 ** 52 - 1, serialized: "0xffffffffffff0f00", root: null},
{json: BigInt(2 ** 52 - 1), serialized: "0xffffffffffff0f00", root: null},
{json: Number.MAX_SAFE_INTEGER, serialized: "0xffffffffffff1f00", root: null},
],
});

Expand All @@ -83,6 +84,7 @@ runTypeTestValid({
},
{json: 2 ** 32, serialized: "0x0000000001000000", root: null},
{json: 2 ** 52 - 1, serialized: "0xffffffffffff0f00", root: null},
{json: Number.MAX_SAFE_INTEGER, serialized: "0xffffffffffff1f00", root: null},
],
});

Expand Down
Loading