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)
3218CREATE FUNCTION eql_v2 .jsonb_array_to_ore_block_u64_8_256(val jsonb)
3319RETURNS eql_v2 .ore_block_u64_8_256 AS $$
3420DECLARE
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
5147CREATE FUNCTION eql_v2 .ore_block_u64_8_256(val jsonb)
5248 RETURNS eql_v2 .ore_block_u64_8_256
5349 IMMUTABLE STRICT PARALLEL SAFE
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)
7073CREATE 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
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
8392CREATE FUNCTION eql_v2 .has_ore_block_u64_8_256(val jsonb)
8493 RETURNS boolean
8594 IMMUTABLE STRICT PARALLEL SAFE
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)
93111CREATE 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
104136CREATE 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
106138AS $$
@@ -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
193230CREATE 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 [])
194231RETURNS 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[])
231279CREATE 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 )
232280RETURNS integer AS $$
233281 BEGIN
0 commit comments