Skip to content

Commit 0e3f247

Browse files
committed
test(stack): compare bigint ORE samples numerically, not lexicographically
The plaintext comparator fell through to a String() compare for `bigint` samples, so the two bigint ORE domains derived a numerically wrong expected order (String(-42n) < String(-9223372036854775808n) while -42n > -9.2e18n). The ORE ciphertext order was correct; only the expectation was wrong.
1 parent 1b1cb16 commit 0e3f247

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

packages/stack/__tests__/v3-matrix/matrix-live-pg.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,20 @@ const MAX_ORDER_ROWS = Math.max(
172172
)
173173

174174
/** Plaintext ordering used to derive the EXPECTED ORE order per domain.
175-
* Dates compare by instant, numbers numerically, strings by code point (ORE
176-
* text order is byte order, which matches JS `<` for the ASCII samples here). */
175+
* Dates compare by instant, numbers and bigints numerically, strings by code
176+
* point (ORE text order is byte order, which matches JS `<` for the ASCII
177+
* samples here). */
177178
const comparePlaintext = (a: unknown, b: unknown): number => {
178179
if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime()
179180
if (typeof a === 'number' && typeof b === 'number') return a - b
181+
// `bigint` samples (BIGINT_S) must compare numerically, not lexicographically:
182+
// String(-42n) < String(-9223372036854775808n) is TRUE ('-4' < '-9') yet
183+
// -42 > -9.2e18. Without this branch the two bigint ORE domains fall through
184+
// to the string compare below and the expected order is numerically wrong,
185+
// even though the ORE ciphertext order is correct. (`a - b` is a bigint, so
186+
// it can't be returned to Array.sort — use the comparison form.)
187+
if (typeof a === 'bigint' && typeof b === 'bigint')
188+
return a < b ? -1 : a > b ? 1 : 0
180189
const sa = String(a)
181190
const sb = String(b)
182191
return sa < sb ? -1 : sa > sb ? 1 : 0

0 commit comments

Comments
 (0)