diff --git a/packages/ssz/src/type/uint.ts b/packages/ssz/src/type/uint.ts index a3d5cfc4..6ec0c484 100644 --- a/packages/ssz/src/type/uint.ts +++ b/packages/ssz/src/type/uint.ts @@ -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. */ @@ -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 { readonly typeName: string; @@ -125,8 +129,12 @@ export class UintNumberType extends BasicType { 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; } diff --git a/packages/ssz/test/unit/byType/uint/invalid.test.ts b/packages/ssz/test/unit/byType/uint/invalid.test.ts index ab7bebf4..0b54db73 100644 --- a/packages/ssz/test/unit/byType/uint/invalid.test.ts +++ b/packages/ssz/test/unit/byType/uint/invalid.test.ts @@ -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"; @@ -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, diff --git a/packages/ssz/test/unit/byType/uint/valid.test.ts b/packages/ssz/test/unit/byType/uint/valid.test.ts index 8b75f3ea..eca7a7fb 100644 --- a/packages/ssz/test/unit/byType/uint/valid.test.ts +++ b/packages/ssz/test/unit/byType/uint/valid.test.ts @@ -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}, ], }); @@ -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}, ], });