Skip to content

Commit 96fd995

Browse files
committed
docs(sql): add Doxygen comments to ORE block module (Phase 3 batch 2)
Documented 27 database objects implementing Order-Revealing Encryption for range queries on encrypted data: - 2 types (ore_block_u64_8_256_term, ore_block_u64_8_256_v1) - 8 functions (make, extract, comparison, equality) - 1 comparison function (ore_block_u64_8_256_term_cmp) - 2 casts (to/from JSONB) - 6 operator functions (=, <>, <, <=, >, >=) - 6 operators supporting encrypted range queries - 2 operator class definitions (btree, hash) Note: operators.sql and operator_class.sql marked as DISABLED (not included in build due to performance/architectural decisions) Progress: Phase 3 batch 2 - ORE block cryptographic module complete
1 parent cebefcd commit 96fd995

File tree

6 files changed

+238
-42
lines changed

6 files changed

+238
-42
lines changed

src/ore_block_u64_8_256/casts.sql

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
-- REQUIRE: src/schema.sql
22
-- REQUIRE: src/ore_block_u64_8_256/types.sql
33

4-
-- casts text to ore_block_u64_8_256_term (bytea)
5-
4+
--! @brief Cast text to ORE block term
5+
--! @internal
6+
--!
7+
--! Converts text to bytea and wraps in ore_block_u64_8_256_term type.
8+
--! Used internally for ORE block extraction and manipulation.
9+
--!
10+
--! @param t Text Text value to convert
11+
--! @return eql_v2.ore_block_u64_8_256_term ORE term containing bytea representation
12+
--!
13+
--! @see eql_v2.ore_block_u64_8_256_term
614
CREATE FUNCTION eql_v2.text_to_ore_block_u64_8_256_term(t text)
715
RETURNS eql_v2.ore_block_u64_8_256_term
816
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
917
BEGIN ATOMIC
1018
RETURN t::bytea;
1119
END;
1220

13-
-- cast to cleanup ore_block_u64_8_256 extraction
14-
21+
--! @brief Implicit cast from text to ORE block term
22+
--!
23+
--! Defines an implicit cast allowing automatic conversion of text values
24+
--! to ore_block_u64_8_256_term type for ORE operations.
25+
--!
26+
--! @see eql_v2.text_to_ore_block_u64_8_256_term
1527
CREATE CAST (text AS eql_v2.ore_block_u64_8_256_term)
1628
WITH FUNCTION eql_v2.text_to_ore_block_u64_8_256_term(text) AS IMPLICIT;

src/ore_block_u64_8_256/compare.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
44

55

6+
--! @brief Compare two encrypted values using ORE block index terms
7+
--!
8+
--! Performs a three-way comparison (returns -1/0/1) of encrypted values using
9+
--! their ORE block index terms. Used internally by range operators (<, <=, >, >=)
10+
--! for order-revealing comparisons without decryption.
11+
--!
12+
--! @param a eql_v2_encrypted First encrypted value to compare
13+
--! @param b eql_v2_encrypted Second encrypted value to compare
14+
--! @return Integer -1 if a < b, 0 if a = b, 1 if a > b
15+
--!
16+
--! @note NULL values are sorted before non-NULL values
17+
--! @note Uses ORE cryptographic protocol for secure comparisons
18+
--!
19+
--! @see eql_v2.ore_block_u64_8_256
20+
--! @see eql_v2.has_ore_block_u64_8_256
21+
--! @see eql_v2."<"
22+
--! @see eql_v2.">"
623
CREATE FUNCTION eql_v2.compare_ore_block_u64_8_256(a eql_v2_encrypted, b eql_v2_encrypted)
724
RETURNS integer
825
IMMUTABLE STRICT PARALLEL SAFE

src/ore_block_u64_8_256/functions.sql

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,16 @@
55
-- REQUIRE: src/ore_block_u64_8_256/types.sql
66

77

8-
-- Casts a jsonb array of hex-encoded strings to the `ore_block_u64_8_256` composite type.
9-
-- In other words, this function takes the ORE index format sent through in the
10-
-- EQL payload from Proxy and decodes it as the composite type that we use for
11-
-- ORE operations on the Postgres side.
12-
-- CREATE FUNCTION eql_v2.jsonb_array_to_ore_block_u64_8_256(val jsonb)
13-
-- RETURNS eql_v2.ore_block_u64_8_256 AS $$
14-
-- DECLARE
15-
-- terms_arr eql_v2.ore_block_u64_8_256_term[];
16-
-- BEGIN
17-
-- IF jsonb_typeof(val) = 'null' THEN
18-
-- RETURN NULL;
19-
-- END IF;
20-
21-
-- SELECT array_agg(ROW(decode(value::text, 'hex'))::eql_v2.ore_block_u64_8_256_term)
22-
-- INTO terms_arr
23-
-- FROM jsonb_array_elements_text(val) AS value;
24-
25-
-- PERFORM eql_v2.log('terms', terms_arr::text);
26-
27-
-- RETURN ROW(terms_arr)::eql_v2.ore_block_u64_8_256;
28-
-- END;
29-
-- $$ LANGUAGE plpgsql;
30-
31-
8+
--! @brief Convert JSONB array to ORE block composite type
9+
--! @internal
10+
--!
11+
--! Converts a JSONB array of hex-encoded ORE terms from the CipherStash Proxy
12+
--! payload into the PostgreSQL composite type used for ORE operations.
13+
--!
14+
--! @param val JSONB Array of hex-encoded ORE block terms
15+
--! @return eql_v2.ore_block_u64_8_256 ORE block composite type, or NULL if input is null
16+
--!
17+
--! @see eql_v2.ore_block_u64_8_256(jsonb)
3218
CREATE FUNCTION eql_v2.jsonb_array_to_ore_block_u64_8_256(val jsonb)
3319
RETURNS eql_v2.ore_block_u64_8_256 AS $$
3420
DECLARE
@@ -47,7 +33,17 @@ END;
4733
$$ LANGUAGE plpgsql;
4834

4935

50-
-- extracts ore index from jsonb
36+
--! @brief Extract ORE block index term from JSONB payload
37+
--!
38+
--! Extracts the ORE block array from the 'ob' field of an encrypted
39+
--! data payload. Used internally for range query comparisons.
40+
--!
41+
--! @param val JSONB Encrypted data payload containing index terms
42+
--! @return eql_v2.ore_block_u64_8_256 ORE block index term
43+
--! @throws Exception if 'ob' field is missing when ore index is expected
44+
--!
45+
--! @see eql_v2.has_ore_block_u64_8_256
46+
--! @see eql_v2.compare_ore_block_u64_8_256
5147
CREATE FUNCTION eql_v2.ore_block_u64_8_256(val jsonb)
5248
RETURNS eql_v2.ore_block_u64_8_256
5349
IMMUTABLE STRICT PARALLEL SAFE
@@ -65,8 +61,15 @@ AS $$
6561
$$ LANGUAGE plpgsql;
6662

6763

68-
-- extracts ore index from an encrypted column
69-
64+
--! @brief Extract ORE block index term from encrypted column value
65+
--!
66+
--! Extracts the ORE block from an encrypted column value by accessing
67+
--! its underlying JSONB data field.
68+
--!
69+
--! @param val eql_v2_encrypted Encrypted column value
70+
--! @return eql_v2.ore_block_u64_8_256 ORE block index term
71+
--!
72+
--! @see eql_v2.ore_block_u64_8_256(jsonb)
7073
CREATE FUNCTION eql_v2.ore_block_u64_8_256(val eql_v2_encrypted)
7174
RETURNS eql_v2.ore_block_u64_8_256
7275
IMMUTABLE STRICT PARALLEL SAFE
@@ -77,9 +80,15 @@ AS $$
7780
$$ LANGUAGE plpgsql;
7881

7982

80-
--
81-
-- Checks if val contains an ore_block_u64_8_256 search term
82-
--
83+
--! @brief Check if JSONB payload contains ORE block index term
84+
--!
85+
--! Tests whether the encrypted data payload includes an 'ob' field,
86+
--! indicating an ORE block is available for range queries.
87+
--!
88+
--! @param val JSONB Encrypted data payload
89+
--! @return Boolean True if 'ob' field is present and non-null
90+
--!
91+
--! @see eql_v2.ore_block_u64_8_256
8392
CREATE FUNCTION eql_v2.has_ore_block_u64_8_256(val jsonb)
8493
RETURNS boolean
8594
IMMUTABLE STRICT PARALLEL SAFE
@@ -90,6 +99,15 @@ AS $$
9099
$$ LANGUAGE plpgsql;
91100

92101

102+
--! @brief Check if encrypted column value contains ORE block index term
103+
--!
104+
--! Tests whether an encrypted column value includes an ORE block
105+
--! by checking its underlying JSONB data field.
106+
--!
107+
--! @param val eql_v2_encrypted Encrypted column value
108+
--! @return Boolean True if ORE block is present
109+
--!
110+
--! @see eql_v2.has_ore_block_u64_8_256(jsonb)
93111
CREATE FUNCTION eql_v2.has_ore_block_u64_8_256(val eql_v2_encrypted)
94112
RETURNS boolean
95113
IMMUTABLE STRICT PARALLEL SAFE
@@ -101,6 +119,20 @@ $$ LANGUAGE plpgsql;
101119

102120

103121

122+
--! @brief Compare two ORE block terms using cryptographic comparison
123+
--! @internal
124+
--!
125+
--! Performs a three-way comparison (returns -1/0/1) of individual ORE block terms
126+
--! using the ORE cryptographic protocol. Compares PRP and PRF blocks to determine
127+
--! ordering without decryption.
128+
--!
129+
--! @param a eql_v2.ore_block_u64_8_256_term First ORE term to compare
130+
--! @param b eql_v2.ore_block_u64_8_256_term Second ORE term to compare
131+
--! @return Integer -1 if a < b, 0 if a = b, 1 if a > b
132+
--! @throws Exception if ciphertexts are different lengths
133+
--!
134+
--! @note Uses AES-ECB encryption for bit comparisons per ORE protocol
135+
--! @see eql_v2.compare_ore_block_u64_8_256_terms
104136
CREATE FUNCTION eql_v2.compare_ore_block_u64_8_256_term(a eql_v2.ore_block_u64_8_256_term, b eql_v2.ore_block_u64_8_256_term)
105137
RETURNS integer
106138
AS $$
@@ -182,14 +214,19 @@ AS $$
182214
$$ LANGUAGE plpgsql;
183215

184216

185-
-- Compare the "head" of each array and recurse if necessary
186-
-- This function assumes an empty string is "less than" everything else
187-
-- so if a is empty we return -1, if be is empty and a isn't, we return 1.
188-
-- If both are empty we return 0. This cases probably isn't necessary as equality
189-
-- doesn't always make sense but it's here for completeness.
190-
-- If both are non-empty, we compare the first element. If they are equal
191-
-- we need to consider the next block so we recurse, otherwise we return the comparison result.
192-
217+
--! @brief Compare arrays of ORE block terms recursively
218+
--! @internal
219+
--!
220+
--! Recursively compares arrays of ORE block terms element-by-element.
221+
--! Empty arrays are considered less than non-empty arrays. If the first elements
222+
--! are equal, recursively compares remaining elements.
223+
--!
224+
--! @param a eql_v2.ore_block_u64_8_256_term[] First array of ORE terms
225+
--! @param b eql_v2.ore_block_u64_8_256_term[] Second array of ORE terms
226+
--! @return Integer -1 if a < b, 0 if a = b, 1 if a > b, NULL if either array is NULL
227+
--!
228+
--! @note Empty arrays sort before non-empty arrays
229+
--! @see eql_v2.compare_ore_block_u64_8_256_term
193230
CREATE FUNCTION eql_v2.compare_ore_block_u64_8_256_terms(a eql_v2.ore_block_u64_8_256_term[], b eql_v2.ore_block_u64_8_256_term[])
194231
RETURNS integer AS $$
195232
DECLARE
@@ -228,6 +265,17 @@ RETURNS integer AS $$
228265
$$ LANGUAGE plpgsql;
229266

230267

268+
--! @brief Compare ORE block composite types
269+
--! @internal
270+
--!
271+
--! Wrapper function that extracts term arrays from ORE block composite types
272+
--! and delegates to the array comparison function.
273+
--!
274+
--! @param a eql_v2.ore_block_u64_8_256 First ORE block
275+
--! @param b eql_v2.ore_block_u64_8_256 Second ORE block
276+
--! @return Integer -1 if a < b, 0 if a = b, 1 if a > b
277+
--!
278+
--! @see eql_v2.compare_ore_block_u64_8_256_terms(eql_v2.ore_block_u64_8_256_term[], eql_v2.ore_block_u64_8_256_term[])
231279
CREATE FUNCTION eql_v2.compare_ore_block_u64_8_256_terms(a eql_v2.ore_block_u64_8_256, b eql_v2.ore_block_u64_8_256)
232280
RETURNS integer AS $$
233281
BEGIN

src/ore_block_u64_8_256/operator_class.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,33 @@
55
-- !REQUIRE: src/ore_block_u64_8_256/types.sql
66

77

8+
--! @brief B-tree operator family for ORE block types
9+
--!
10+
--! Defines the operator family for creating B-tree indexes on ORE block types.
11+
--!
12+
--! @note FILE IS DISABLED - Not included in build
13+
--! @see eql_v2.ore_block_u64_8_256_operator_class
814
CREATE OPERATOR FAMILY eql_v2.ore_block_u64_8_256_operator_family USING btree;
915

16+
--! @brief B-tree operator class for ORE block encrypted values
17+
--!
18+
--! Defines the operator class required for creating B-tree indexes on columns
19+
--! using the ore_block_u64_8_256 type. Enables range queries and ORDER BY on
20+
--! ORE-encrypted data without decryption.
21+
--!
22+
--! Supports operators: <, <=, =, >=, >
23+
--! Uses comparison function: compare_ore_block_u64_8_256_terms
24+
--!
25+
--! @note FILE IS DISABLED - Not included in build
26+
--!
27+
--! @example
28+
--! -- Would be used like (if enabled):
29+
--! CREATE INDEX ON events USING btree (
30+
--! (encrypted_timestamp::jsonb->'ob')::eql_v2.ore_block_u64_8_256
31+
--! );
32+
--!
33+
--! @see CREATE OPERATOR CLASS in PostgreSQL documentation
34+
--! @see eql_v2.compare_ore_block_u64_8_256_terms
1035
CREATE OPERATOR CLASS eql_v2.ore_block_u64_8_256_operator_class DEFAULT FOR TYPE eql_v2.ore_block_u64_8_256 USING btree FAMILY eql_v2.ore_block_u64_8_256_operator_family AS
1136
OPERATOR 1 <,
1237
OPERATOR 2 <=,

0 commit comments

Comments
 (0)