Skip to content

Commit 508680c

Browse files
committed
fix: BYTES_PER_KEY value
Maximum number of bytes which are allowed to be encrypted under a single data key The _real_ maximum is 2 ** 63 - 1, However Javascript can only perform safe operations on values up to Number.MAX_SAFE_INTEGER === 9007199254740991 === 2 ** 53 - 1. e.g Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 => true Number.MAX_SAFE_INTEGER + 1 > Number.MAX_SAFE_INTEGER + 2 => false Number.MAX_SAFE_INTEGER + 1 < Number.MAX_SAFE_INTEGER + 2 => false This means that after 2 ** 53 - 1 the process of accumulating a byte count will never yield an accurate comparison and so, never halt. The choice here to use 2 ** 53 - 1 instead of Number.MAX_SAFE_INTEGER is deliberate. This is because in the future Number.MAX_SAFE_INTEGER could be raised to 2 ** 66 or some value larger 2 ** 63.
1 parent 698180f commit 508680c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

modules/serialize/src/identifiers.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,23 @@ Object.freeze(SequenceIdentifier)
6262
export enum Maximum {
6363
// Maximum number of messages which are allowed to be encrypted under a single cached data key
6464
MESSAGES_PER_KEY = 2 ** 32, // eslint-disable-line no-unused-vars
65-
// Maximum number of bytes which are allowed to be encrypted under a single cached data key
66-
BYTES_PER_KEY = 2 ** 63 - 1, // eslint-disable-line no-unused-vars
65+
/* Maximum number of bytes which are allowed to be encrypted under a single data key
66+
* The _real_ maximum is 2 ** 63 - 1,
67+
* However Javascript can only perform safe operations on values
68+
* up to Number.MAX_SAFE_INTEGER === 9007199254740991 === 2 ** 53 - 1.
69+
* e.g
70+
* Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 => true
71+
* Number.MAX_SAFE_INTEGER + 1 > Number.MAX_SAFE_INTEGER + 2 => false
72+
* Number.MAX_SAFE_INTEGER + 1 < Number.MAX_SAFE_INTEGER + 2 => false
73+
*
74+
* This means that after 2 ** 53 - 1 the process of accumulating a byte count
75+
* will never yield an accurate comparison and so, never halt.
76+
*
77+
* The choice here to use 2 ** 53 - 1 instead of Number.MAX_SAFE_INTEGER is deliberate.
78+
* This is because in the future Number.MAX_SAFE_INTEGER could be raised to 2 ** 66
79+
* or some value larger 2 ** 63.
80+
*/
81+
BYTES_PER_KEY = 2 ** 53 - 1, // eslint-disable-line no-unused-vars
6782
// Maximum number of frames allowed in one message as defined in specification
6883
FRAME_COUNT = 2 ** 32 - 1, // eslint-disable-line no-unused-vars
6984
// Maximum bytes allowed in a single frame as defined in specification

0 commit comments

Comments
 (0)