22
33Checks that ` eql_v3_internal.compare_ore_block_256_terms ` , the comparator
44behind every ordered ORE operator and the ORE operator class, takes the same
5- time wherever its operands first differ.
5+ time wherever its operands first differ, and whether they differ at all .
66
77## Why it matters
88
9- The comparator's inputs are ciphertexts, so the database operator learns
10- nothing from its timing: they can run the comparison themselves. The party
11- who gains is someone who can issue queries and time them without reading the
9+ The database operator gains nothing from the comparator's timing. They hold
10+ the stored ciphertexts and can run the comparator on any two of them
11+ directly, which tells them everything its timing could. The party who gains
12+ is someone who can issue queries and time them without reading the
1213stored ciphertexts, such as an application user. Query results already tell
1314them the order of their value relative to stored ones. If the comparison time
1415depends on where the two values first differ, timing also tells them how long
@@ -17,9 +18,12 @@ comparison when the first of six terms differs, 47 µs when only the sixth
1718does.
1819
1920PL/pgSQL is not a constant-time environment. What it can give is a constant
20- * operation count* : the same statements and function calls on every path, so
21- nothing at the scale of a statement (fractions of a microsecond) depends on
22- the data. C-level effects inside those statements (` memcmp ` stopping early
21+ * operation count* : for operands of the same shape, the same statements and
22+ function calls whatever their contents, so nothing at the scale of a
23+ statement (fractions of a microsecond) depends on where or whether they
24+ differ. The shape is the term count, the term width and the position of any
25+ NULL element. It is public structure, and the comparator may branch on it:
26+ a single term and a six-term value take different paths. C-level effects inside those statements (` memcmp ` stopping early
2327within 16 bytes, the offset a ` substr ` reads at) remain, orders of magnitude
2428smaller. The Rust comparator in ore.rs is constant-time at the instruction
2529level; this is the PL/pgSQL counterpart.
@@ -51,6 +55,8 @@ plus a correctness set:
5155
5256- ** single term** : pairs differing in the first block against pairs differing
5357 only in the last (block 7);
58+ - ** equal term** : a single term against itself, against pairs differing in
59+ the first block;
5460- ** six terms** (text-shaped values): pairs differing in the first term
5561 against pairs differing only in the sixth;
5662- ** prefix** : five terms against six, where the five equal the first five of
@@ -81,21 +87,38 @@ difference.
8187## Results
8288
8389PostgreSQL 17.11, aarch64 (Docker on an Apple M1 Max). Welch t for two runs
84- each; the current comparator at 200,000 single-term samples of 16 comparisons
85- and 50,000 multi-term samples of 8 per class:
90+ each; the current comparator at 200,000 samples of 16 comparisons for the
91+ single-term kind and 50,000 samples of 8 for each multi-term kind, each split
92+ at random between the kind's two classes:
8693
87- | comparator | single term | six terms | prefix | NULL element |
88- | ---| ---:| ---:| ---:| ---:|
89- | original (term by term) | +4.9, +13.9 | −1552, −2575 | +517, +459 ¹ | +520, +682 ¹ |
90- | first constant-count version ² | +1.8, +1.1 | +4.5, +2.7 | −62, −69 ³ | −60, −70 ³ |
91- | current | +2.24, +0.15 | +1.03, −0.27 | −0.68, +0.18 | −0.57, +0.14 |
94+ | comparator | single term | equal term | six terms | prefix | NULL element |
95+ | ---| ---:| ---:| ---:| ---:| ---:|
96+ | original (term by term) | +4.9, +13.9 | | −1552, −2575 | +517, +459 ¹ | +520, +682 ¹ |
97+ | first constant-count version ² | +1.8, +1.1 | | +4.5, +2.7 | −62, −69 ³ | −60, −70 ³ |
98+ | current | +2.24, +0.15 | | +1.03, −0.27 | −0.68, +0.18 | −0.57, +0.14 |
99+ | current, second machine ⁴ | +1.60, +0.71 | −2.49, −1.99 | −2.58, +1.33 | −4.96, −1.19, −2.56, −7.42, −5.14 | −3.82, −9.63, −2.23, −3.88, −2.88 |
92100
93101¹ 5,000 samples per run. ² It scanned every block of every term, and skipped
94102its hash when the term counts or a NULL settled the order. ³ 10,000 samples
95- per run.
103+ per run. ⁴ PostgreSQL 17.5 run natively on macOS, Apple silicon, at the same
104+ sample counts, with three more runs of the prefix and NULL kinds.
105+
106+ On the second machine the prefix and NULL-element kinds come out negative on
107+ every run, and three runs pass |t| = 5. Class 0, where the structure settles
108+ the order, is faster by 0.1 to 0.2 µs per sample of eight comparisons: about
109+ 20 ns a comparison, or 0.2% of one. That is below the cost of a single
110+ statement, so it is a C-level effect of the kind described above, not a
111+ skipped statement; a skipped hash showed up as t = −60 to −70 at a tenth of
112+ the samples. The equal-term class shows the same sign at about 6 ns a
113+ comparison.
96114
97115Cost: the current comparator takes about 6% more than the original per
98- single-term comparison, and about 18 µs per six-term comparison whatever the
116+ single-term comparison when the terms differ, and about 36% more when they are
117+ equal (3.6 µs against 2.7 µs on the second machine, calling
118+ ` compare_ore_block_256_term ` directly), because equal terms now run the hash
119+ too. Duplicates are common in index builds, ` ORDER BY ` ties and equality
120+ probes on ORE columns, so expect ordered workloads with many ties to see the
121+ larger figure. It takes about 18 µs per six-term comparison whatever the
99122operands. The original took 11 µs when the first term differs and 47 µs when
100123only the sixth does; the first constant-count version took 31 µs.
101124
0 commit comments