Skip to content

Treat single element ste vec as eql_v2_encrypted #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/operators/compare.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ AS $$
RETURN 1;
END IF;

-- Use ORE if both parameters have ore index
a := eql_v2.to_ste_vec_value(a);
b := eql_v2.to_ste_vec_value(b);

IF eql_v2.has_ore_block_u64_8_256(a) AND eql_v2.has_ore_block_u64_8_256(b) THEN
RETURN eql_v2.compare_ore_block_u64_8_256(a, b);
END IF;
Expand All @@ -72,7 +74,6 @@ AS $$
RETURN eql_v2.compare_ore_cllw_var_8(a, b);
END IF;

-- Fallback to hmac if both parameters have hmac index
IF eql_v2.has_hmac_256(a) AND eql_v2.has_hmac_256(b) THEN
RETURN eql_v2.compare_hmac_256(a, b);
END IF;
Expand Down
67 changes: 65 additions & 2 deletions src/ste_vec/functions.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- REQUIRE: src/schema.sql
-- REQUIRE: src/encrypted/types.sql
-- REQUIRE: src/encrypted/casts.sql
-- REQUIRE: src/encrypted/functions.sql


--
Expand All @@ -18,7 +20,7 @@ AS $$
sv := jsonb_build_array(val);
END IF;

SELECT array_agg(elem::eql_v2_encrypted)
SELECT array_agg(eql_v2.to_encrypted(elem))
INTO ary
FROM jsonb_array_elements(sv) AS elem;

Expand All @@ -38,7 +40,68 @@ AS $$
END;
$$ LANGUAGE plpgsql;

--
-- Returns true if val is an SteVec with a single array item.
-- SteVec value items can be treated as regular eql_encrypted
--
CREATE FUNCTION eql_v2.is_ste_vec_value(val jsonb)
RETURNS boolean
IMMUTABLE STRICT PARALLEL SAFE
AS $$
BEGIN
IF val ? 'sv' THEN
RETURN jsonb_array_length(val->'sv') = 1;
END IF;

RETURN false;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION eql_v2.is_ste_vec_value(val eql_v2_encrypted)
RETURNS boolean
IMMUTABLE STRICT PARALLEL SAFE
AS $$
BEGIN
RETURN eql_v2.is_ste_vec_value(val.data);
END;
$$ LANGUAGE plpgsql;

--
-- Returns an SteVec with a single array item as an eql_encrypted
--
CREATE FUNCTION eql_v2.to_ste_vec_value(val jsonb)
RETURNS eql_v2_encrypted
IMMUTABLE STRICT PARALLEL SAFE
AS $$
DECLARE
meta jsonb;
sv jsonb;
BEGIN

IF val IS NULL THEN
RETURN NULL;
END IF;

IF eql_v2.is_ste_vec_value(val) THEN
meta := eql_v2.meta_data(val);
sv := val->'sv';
sv := sv[0];

RETURN eql_v2.to_encrypted(meta || sv);
END IF;

RETURN eql_v2.to_encrypted(val);
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION eql_v2.to_ste_vec_value(val eql_v2_encrypted)
RETURNS eql_v2_encrypted
IMMUTABLE STRICT PARALLEL SAFE
AS $$
BEGIN
RETURN eql_v2.to_ste_vec_value(val.data);
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION eql_v2.selector(val jsonb)
RETURNS text
Expand Down Expand Up @@ -119,7 +182,7 @@ AS $$
$$ LANGUAGE plpgsql;


-- Returns truy if a contains b
-- Returns true if a contains b
-- All values of b must be in a
CREATE FUNCTION eql_v2.ste_vec_contains(a eql_v2_encrypted, b eql_v2_encrypted)
RETURNS boolean
Expand Down
43 changes: 41 additions & 2 deletions src/ste_vec/functions_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ $$ LANGUAGE plpgsql;
DO $$
DECLARE
e eql_v2_encrypted;
sv eql_v2_encrypted[];
BEGIN
e := '{ "a": 1 }'::jsonb::eql_v2_encrypted;
ASSERT eql_v2.is_ste_vec_array(e);


e := '{ "a": 0 }'::jsonb::eql_v2_encrypted;
ASSERT NOT eql_v2.is_ste_vec_array(e);

Expand All @@ -43,6 +41,47 @@ DO $$
$$ LANGUAGE plpgsql;


DO $$
DECLARE
e jsonb;
BEGIN
-- extract the ste_vec array item as an eeql_v2_encrypted
e := eql_v2.to_ste_vec_value('{ "i": "i", "v": 2, "sv": [ { "ocf": "ocf" }] }'::jsonb);

ASSERT e ? 'i';
ASSERT e ? 'v';
ASSERT e ? 'ocf';

-- Returns the original if not an stevec value
e := eql_v2.to_ste_vec_value('{ "i": "i", "v": 2, "b3": "b3" }'::jsonb);

ASSERT e ? 'i';
ASSERT e ? 'v';
ASSERT e ? 'b3';

END;
$$ LANGUAGE plpgsql;



DO $$
DECLARE
e eql_v2_encrypted;
sv eql_v2_encrypted[];
BEGIN
e := '{ "sv": [1] }'::jsonb::eql_v2_encrypted;
ASSERT eql_v2.is_ste_vec_value(e);

e := '{ "sv": [] }'::jsonb::eql_v2_encrypted;
ASSERT NOT eql_v2.is_ste_vec_value(e);

e := '{ }'::jsonb::eql_v2_encrypted;
ASSERT NOT eql_v2.is_ste_vec_value(e);

END;
$$ LANGUAGE plpgsql;


-- ------------------------------------------------------------------------
-- ------------------------------------------------------------------------
--
Expand Down