Skip to content

Commit 64397f9

Browse files
committed
feat: compare_ore_cllw_u64_8
1 parent 293d450 commit 64397f9

File tree

6 files changed

+167
-39
lines changed

6 files changed

+167
-39
lines changed

src/ore_cllw_u64_8/compare.sql

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- REQUIRE: src/schema.sql
2+
-- REQUIRE: src/ore_cllw_u64_8/types.sql
3+
-- REQUIRE: src/ore_cllw_u64_8/functions.sql
4+
5+
6+
CREATE FUNCTION eql_v2.compare_ore_cllw_u64_8(a eql_v2_encrypted, b eql_v2_encrypted)
7+
RETURNS integer
8+
IMMUTABLE STRICT PARALLEL SAFE
9+
AS $$
10+
DECLARE
11+
a_term eql_v2.ore_cllw_u64_8;
12+
b_term eql_v2.ore_cllw_u64_8;
13+
BEGIN
14+
15+
-- PERFORM eql_v2.log('eql_v2.compare_ore_cllw_u64_8');
16+
-- PERFORM eql_v2.log('a', a::text);
17+
-- PERFORM eql_v2.log('b', b::text);
18+
19+
IF a IS NULL AND b IS NULL THEN
20+
RETURN 0;
21+
END IF;
22+
23+
IF a IS NULL THEN
24+
RETURN -1;
25+
END IF;
26+
27+
IF b IS NULL THEN
28+
RETURN 1;
29+
END IF;
30+
31+
IF eql_v2.has_ore_cllw_u64_8(a) THEN
32+
a_term := eql_v2.ore_cllw_u64_8(a);
33+
END IF;
34+
35+
IF eql_v2.has_ore_cllw_u64_8(a) THEN
36+
b_term := eql_v2.ore_cllw_u64_8(b);
37+
END IF;
38+
39+
IF a_term IS NULL AND b_term IS NULL THEN
40+
RETURN 0;
41+
END IF;
42+
43+
IF a_term IS NULL THEN
44+
RETURN -1;
45+
END IF;
46+
47+
IF b_term IS NULL THEN
48+
RETURN 1;
49+
END IF;
50+
51+
RETURN eql_v2.compare_ore_cllw_term_bytes(a_term.bytes, b_term.bytes);
52+
END;
53+
$$ LANGUAGE plpgsql;
54+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
\set ON_ERROR_STOP on
2+
3+
DO $$
4+
DECLARE
5+
a eql_v2_encrypted;
6+
b eql_v2_encrypted;
7+
c eql_v2_encrypted;
8+
BEGIN
9+
10+
-- {"number": {N}}
11+
-- $.number: 3dba004f4d7823446e7cb71f6681b344
12+
a := eql_v2.jsonb_path_query(create_encrypted_ste_vec_json(1), '3dba004f4d7823446e7cb71f6681b344');
13+
b := eql_v2.jsonb_path_query(create_encrypted_ste_vec_json(5), '3dba004f4d7823446e7cb71f6681b344');
14+
c := eql_v2.jsonb_path_query(create_encrypted_ste_vec_json(10), '3dba004f4d7823446e7cb71f6681b344');
15+
16+
ASSERT eql_v2.compare_ore_cllw_u64_8(a, a) = 0;
17+
ASSERT eql_v2.compare_ore_cllw_u64_8(a, b) = -1;
18+
ASSERT eql_v2.compare_ore_cllw_u64_8(a, c) = -1;
19+
20+
ASSERT eql_v2.compare_ore_cllw_u64_8(b, b) = 0;
21+
ASSERT eql_v2.compare_ore_cllw_u64_8(b, a) = 1;
22+
ASSERT eql_v2.compare_ore_cllw_u64_8(b, c) = -1;
23+
24+
ASSERT eql_v2.compare_ore_cllw_u64_8(c, c) = 0;
25+
ASSERT eql_v2.compare_ore_cllw_u64_8(c, b) = 1;
26+
ASSERT eql_v2.compare_ore_cllw_u64_8(c, a) = 1;
27+
END;
28+
$$ LANGUAGE plpgsql;
29+

src/ore_cllw_u64_8/functions.sql

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@ $$ LANGUAGE plpgsql;
6868
-- Used by both fixed and variable ore cllw variants
6969
--
7070

71-
CREATE FUNCTION eql_v2.compare_ore_cllw(a bytea, b bytea)
71+
CREATE FUNCTION eql_v2.compare_ore_cllw_term_bytes(a bytea, b bytea)
7272
RETURNS int AS $$
7373
DECLARE
7474
len_a INT;
75+
len_b INT;
7576
x BYTEA;
7677
y BYTEA;
7778
i INT;
78-
differing RECORD;
79+
differing boolean;
7980
BEGIN
81+
82+
-- Check if the lengths of the two bytea arguments are the same
8083
len_a := LENGTH(a);
84+
len_b := LENGTH(b);
85+
86+
IF len_a != len_b THEN
87+
RAISE EXCEPTION 'ore_cllw index terms are not the same length';
88+
END IF;
8189

8290
-- Iterate over each byte and compare them
8391
FOR i IN 1..len_a LOOP
@@ -86,13 +94,13 @@ BEGIN
8694

8795
-- Check if there's a difference
8896
IF x != y THEN
89-
differing := (x, y);
97+
differing := true;
9098
EXIT;
9199
END IF;
92100
END LOOP;
93101

94102
-- If a difference is found, compare the bytes as in Rust logic
95-
IF differing IS NOT NULL THEN
103+
IF differing THEN
96104
IF (get_byte(y, 0) + 1) % 256 = get_byte(x, 0) THEN
97105
RETURN 1;
98106
ELSE
@@ -105,27 +113,3 @@ END;
105113
$$ LANGUAGE plpgsql;
106114

107115

108-
109-
110-
CREATE FUNCTION eql_v2.compare_ore_cllw_u64_8(a eql_v2.ore_cllw_u64_8, b eql_v2.ore_cllw_u64_8)
111-
RETURNS int AS $$
112-
DECLARE
113-
len_a INT;
114-
len_b INT;
115-
BEGIN
116-
IF a IS NULL OR b IS NULL THEN
117-
RETURN NULL;
118-
END IF;
119-
120-
-- Check if the lengths of the two bytea arguments are the same
121-
len_a := LENGTH(a.bytes);
122-
len_b := LENGTH(b.bytes);
123-
124-
IF len_a != len_b THEN
125-
RAISE EXCEPTION 'ore_cllw_u64_8 index terms are not the same length';
126-
END IF;
127-
128-
RETURN eql_v2.compare_ore_cllw(a.bytes, b.bytes);
129-
END;
130-
$$ LANGUAGE plpgsql;
131-

tasks/test.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,28 @@ fail_if_postgres_not_running
3838
mise run build --force
3939
mise run reset --force --postgres ${POSTGRES_VERSION}
4040

41+
echo
42+
echo '###############################################'
43+
echo '# Installing release/cipherstash-encrypt.sql'
44+
echo '###############################################'
45+
echo
4146

4247
# Install
43-
# cat release/cipherstash-encrypt.sql | docker exec -i ${container_name} psql ${connection_url} -f-
44-
if cat release/cipherstash-encrypt.sql | docker exec -i ${container_name} psql ${connection_url} -f- | grep -q "ERROR"; then
45-
echo
46-
echo '******************************************************'
47-
echo '* ❌ ERROR installing release/cipherstash-encrypt.sql'
48-
echo '******************************************************'
49-
echo
48+
cat release/cipherstash-encrypt.sql | docker exec -i ${container_name} psql ${connection_url} -f-
49+
# if cat release/cipherstash-encrypt.sql | docker exec -i ${container_name} psql ${connection_url} -f- | grep -q "ERROR"; then
50+
# echo
51+
# echo '******************************************************'
52+
# echo '* ❌ ERROR installing release/cipherstash-encrypt.sql'
53+
# echo '******************************************************'
54+
# echo
5055

51-
exit 1
52-
fi
56+
# exit 1
57+
# fi
5358

5459

5560
cat tests/test_helpers.sql | docker exec -i ${container_name} psql ${connection_url} -f-
5661
cat tests/ore.sql | docker exec -i ${container_name} psql ${connection_url} -f-
62+
cat tests/ste_vec.sql | docker exec -i ${container_name} psql ${connection_url} -f-
5763

5864
if [ $usage_test = "false" ]; then
5965
find src -type f -path "*_test.sql" | while read -r sql_file; do

0 commit comments

Comments
 (0)