Skip to content
Merged
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
29 changes: 27 additions & 2 deletions .changeset/eql-v4-match-short-words.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@
'@cipherstash/eql': minor
---

**EQL v4's match types index each two-letter word whole, so a two-letter surname such as "Ng" is searchable** (#1309). `eql-bindings`' `EqlMatch`, the match configuration of `TextMatch`, `TextSearch` and `TextSearchOre`, turns on Stack Encrypt's `MatchOptions::short_words`: beside the 3-grams of the whole text, every word of exactly two characters (words split on space, comma, semicolon, colon and exclamation mark) is one more token, the word itself, hashed under a domain of its own and folded into the same 2048-bit filter by the same 6 positions. "Ng" now stores into `TextMatch` (it was refused, cutting no 3-gram), a `TextSearch` holding "Ng" stores a filter rather than the empty one, and `@@` with the query "ng" finds the rows holding the word "Ng" ("Ng", "Mike Ng", "NG, Anna"), not "Nguyen". A query's two-letter words must be whole words of the row, so "Mike Ng" does not find "Mike Nguyen". Text that still cuts no token (one letter, or none) is refused by `TextMatch` and stored with the empty filter by the search types, as before.
**EQL v4's match types now index each two-letter word as a whole word, so a two-letter surname such as "Ng" is searchable** (#1309). `eql-bindings`' `EqlMatch` is the match configuration of `TextMatch`, `TextSearch`, and `TextSearchOre`. `EqlMatch` now turns on Stack Encrypt's `MatchOptions::short_words`.

A stored value's filter contains the 3-grams of the whole text. A 3-gram is a run of three characters, such as "mik" in "Mike". The filter now also contains each word of exactly two characters, as one more token. Words are split on space, comma, semicolon, colon, and exclamation mark.

The word token is hashed under a domain of its own. Like a 3-gram, it sets 6 positions in the same 2048-bit filter.

- "Ng" can now be stored in `TextMatch`. Before, `TextMatch` refused it, because "Ng" has no 3-gram.
- A `TextSearch` value of "Ng" now stores a filter, not the empty filter.
- `@@` with the query "ng" finds the rows that contain the word "Ng", such as "Ng", "Mike Ng", and "NG, Anna". The query does not find "Nguyen".

A query asks for its two-letter words only when the whole query is one two-letter word. A query of three characters or more asks for its 3-grams alone, as it would without word tokens. So "Mike Ng" finds "Mike Ng" and "Mike Nguyen". A name typed in part, such as "ke Ng" or "san fr", still finds its row.

Every query path follows this rule, so a query's filter and its verify agree. The paths are:

- a field reference's `matches`;
- the query twins' `EncryptTarget`;
- the by-name query dispatch that a binding runs;
- `verify`.

A query twin's filter has a new field type, `v4::terms::QueryBloomFilter`. A query twin is the struct that contains a query's terms. The new type is the `bf` of `TextMatchQuery`, `TextSearchMatchQuery`, `TextSearchOreMatchQuery`, `TextSearchQuery`, and `TextSearchOreQuery`. `QueryBloomFilter` has the JSON and JSON Schema of `BloomFilter`. Only fields of the `v4` module change, and that module is not released yet.

Text with no token at all, such as one letter or nothing, is handled as before. `TextMatch` refuses it. The search types store it with the empty filter.

**The v4 match format now includes short-word tokens.** A filter of text holding a two-letter word gains that word's bits; text with none derives the filter it did. EQL v4 is unreleased, so this is the format it ships with: a v4 value written by an earlier build that holds a two-letter word lacks the word's bits, and a query naming that word does not find it until the value is re-encrypted.

**What it reveals.** One more token per two-letter word, under the same PRF, in the same filter, by the same containment rule, so no new class of leakage. The token is specific to the word: someone who clusters tokens across rows can tell the rows holding the word "Ng" from those holding "Nguyen", where before only the boundary trigram `" ng"` they share was visible. No bigram of a longer word is indexed.
**What it reveals.** Each stored value gains one more token for each two-letter word it contains. The token uses the same pseudorandom function (PRF), the same filter, and the same containment rule. No two-character piece of a longer word is indexed.

The word token is unique to the word. Someone who groups tokens across rows can tell the rows that contain "Ng" from the rows that contain "Nguyen", and count them. Without the word token, they see only the 3-gram `" ng"` that both names share.

A query of one two-letter word shows which word it asks for. A longer query sends no word token.

No SQL function changes (one doc comment in the bundle is reworded), and the TypeScript types do not change.
20 changes: 20 additions & 0 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.
token derives under the cipher's scopes alone. The children open under
the scopes and the binding. A cipher with a binding still refuses
`ChildCount`, since a count opens nothing.

- **`Matches` on `TextMatch`, `TextSearch`, and `TextSearchOre` finds a
name typed in part again** (#1398). Before this fix, "Mike Ng" did not
find "Mike Nguyen". A stored value contains its 3-grams, and each
two-letter word as a whole word. A 3-gram is a run of three characters,
such as "mik" in "Mike". A query also asked for each of its two-letter
words as a whole word. "Nguyen" contains "ng" only inside a longer word,
so the query missed it.

Now a query asks for its two-letter words only when the whole query is
one two-letter word. A query of three characters or more asks for its
3-grams alone.
- "Mike Ng" finds "Mike Ng" and "Mike Nguyen".
- "ke Ng" finds "Mike Ng".
- "Ng" still asks for the whole word. The query finds "Ng" and "Mike
Ng", and not "Nguyen".

`Verify` keeps the same rows that the filter selects. Stored values do
not change, so nothing needs re-encrypting.

- `vcffi` and `vcvalue` move to vitaminc `f1fc9ae74`, the commit vitaminc
0.10.0 was released from. The host now checks the same date and
timestamp range as the guests.
Expand Down
2 changes: 1 addition & 1 deletion languages/golang/cmd/stashgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ The `Eq` types answer `Eq`; the `Ord`, `OrdOpe` and `OrdOre` types answer `Lt`,
The ordered date, timestamp and numeric types answer the four comparisons, as do the other ordered EQL types. The generated EQL fields have no `Between` method yet: EQL v4 has range query domains, but `stashgen` does not construct their operands. A JSON document's declared ordered paths have their own `Between` method, which returns two leaf query values; see [Encrypted JSON documents](#encrypted-json-documents).

Any built-in Go integer field takes an integer type (ADR-0002) and seals at the type's width: an `int16` field always fits an `Integer`, and an `int64` field is range checked value by value, `Encrypt` refusing a value that does not fit with `encrypt.ErrEncoding`. `encrypt.Int128` and `encrypt.Uint128` are not converted. A float's width must match: a `float64` field is not a `Real`.
A `TextMatch` field indexes the 3-grams of its text and each two-letter word whole, so a two-letter surname such as "Ng" is stored and found by the query "ng"; a query's two-letter words must be whole words of the row.
A `TextMatch` field indexes the 3-grams of its text, and each two-letter word as a whole word. A 3-gram is a run of three characters, such as "mik" in "Mike". So a two-letter surname such as "Ng" is stored, and the query "ng" finds it. A query asks for its two-letter words only when the whole query is one two-letter word. So the query "Mike Ng" asks for its 3-grams alone, and finds "Mike Nguyen" too.
A `TextMatch` field refuses text that cuts no token, such as an empty string or one letter; its query refuses such text too.
A `TextSearch` field stores such text with an empty match filter, so its `Eq`, `Lt`, `Lte`, `Gt` and `Gte` queries find it and no `Matches` query does; those queries take such text too, and its `Matches` query refuses it.
Put the column on the left of `@@` (`WHERE name @@ $1::eql_v4.query_text_match`): the other operand order finds nothing until #1171 is fixed.
Expand Down
24 changes: 17 additions & 7 deletions languages/golang/encrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,23 @@ with `ErrEncoding`, naming the field's kind and not the value.
`encrypt.Int128` and `encrypt.Uint128` are not converted, and floats take
exactly their own width.

A `TextMatch` value is indexed by its 3-grams and by each two-letter word
whole, so a two-letter surname such as "Ng" is stored, and `Matches` with
"ng" finds the rows holding the word "Ng" ("Ng", "Mike Ng"), not "Nguyen".
A query's two-letter words must be whole words of the row, so "Mike Ng" does
not find "Mike Nguyen". The word token reveals one more token per two-letter
word, in the same filter: someone who clusters tokens across rows can tell
"Ng" from "Nguyen", where before only the trigram they share was visible.
A `TextMatch` index contains the 3-grams of a value, and each two-letter
word as a whole word. A 3-gram is a run of three characters, such as "mik" in
"Mike". The whole-word token lets you store a two-letter surname such as
"Ng". `Matches` with "ng" finds the rows that contain the word "Ng", such as
"Ng" and "Mike Ng", and not "Nguyen".

A query asks for its two-letter words only when the whole query is one
two-letter word. A query of three characters or more asks for its 3-grams
alone. So "Mike Ng" finds "Mike Ng" and "Mike Nguyen". A name typed in part,
such as "ke Ng", finds its row.

The word token adds one token for each two-letter word in a stored value.
The token is unique to the word. Someone who groups tokens across rows can
tell the rows that contain "Ng" from the rows that contain "Nguyen", and
count them. Without the word token, they see only the 3-gram that both
names share.

Text that cuts no token, such as an empty string or one letter, is refused. A
`TextSearch` value of such text is stored with an empty match filter: `Eq`,
`Lt`, `Lte`, `Gt` and `Gte` find it through its other terms, and no
Expand Down
9 changes: 9 additions & 0 deletions languages/golang/encrypt/eql_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ func TestV4QueriesMatchTheRustFixture(t *testing.T) {
if ok, err := ng.Verify(ctx, []string{"Ng", "Mike Ng", "Nguyen"}); err != nil || !ok[0] || !ok[1] || ok[2] {
t.Errorf("Verify(Ng, Mike Ng, Nguyen) = %v, %v, want true, true, false", ok, err)
}
// A query of three characters or more keeps no two-letter word. So a
// name typed in part finds the longer name too.
mikeNg, err := testusers.ProfileFields.Name.Matches(ctx, cipher, "Mike Ng")
if err != nil {
t.Fatalf("Name.Matches(%q): %v", "Mike Ng", err)
}
if ok, err := mikeNg.Verify(ctx, []string{"Mike Ng", "Mike Nguyen", "Ng"}); err != nil || !ok[0] || !ok[1] || ok[2] {
t.Errorf("Verify(Mike Ng, Mike Nguyen, Ng) = %v, %v, want true, true, false", ok, err)
}
}

// rowsFixture is v4_rows.json: the profiles above as the generated code seals
Expand Down
29 changes: 29 additions & 0 deletions packages/eql/crates/eql-bindings/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- **A match query asks for its two-letter words only when the whole query
is one two-letter word** (#1398, `stack-encrypt` feature). Before this
fix, "Mike Ng" did not find "Mike Nguyen". `EqlMatch` stores the 3-grams
of a value, and each two-letter word as a whole-word token. A 3-gram is
a run of three characters, such as "mik" in "Mike". A query that
contained a two-letter word also asked for that word whole. "Nguyen"
contains "ng" only inside a longer word, so the query missed it.

Now a query of three characters or more asks for its 3-grams alone.
"Mike Ng" finds "Mike Ng" and "Mike Nguyen". "ke Ng" and "san fr" find
the names they are part of. A query of one two-letter word asks for that
word: "Ng" finds "Ng" and "Mike Ng", and not "Nguyen". Stored values do
not change.
- The `bf` of every v4 query twin is now the new
`v4::terms::QueryBloomFilter`. A query twin is the struct that contains
a query's terms. The twins are `TextMatchQuery`,
`TextSearchMatchQuery`, `TextSearchOreMatchQuery`, `TextSearchQuery`,
and `TextSearchOreQuery`. In the last two, `bf` is an
`Option<QueryBloomFilter>`.
- `QueryBloomFilter` has the JSON and JSON Schema of `BloomFilter`.
`From<QueryBloomFilter> for BloomFilter` unwraps it.
- The `EncryptTarget` of `QueryBloomFilter` derives the query's filter.
So a query twin follows the rule however it is built: directly, by a
field reference's `matches`, or by the by-name query dispatch.
- `TwinIndex` forwards Stack Encrypt's `Index::query_operation` to the
indexes whose terms its twin contains, through
`Indexes::query_operations`. So `matches` on `TextMatch`, `TextSearch`,
and `TextSearchOre` sends the query's filter. Every other operator
sends the term its index stores.
- **A v4 order term is Stack Encrypt's own, typed by its plaintext**
(`stack-encrypt` feature). An `ob` field is
`stack_encrypt::sem::OreBlockTerm<T>` and an `op` field
Expand Down
31 changes: 23 additions & 8 deletions packages/eql/crates/eql-bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,29 @@ not `=` either. Text with a code point Unicode 16 does not assign has no
order term and is refused, stored or queried. A family's `Ord` and `OrdOpe`
types store the same term under one column. The query value carries one
bound: `BETWEEN` needs EQL v4's range query domains and is not produced yet.
`TextMatch` builds its filter under a configuration pinned in this crate
(`EqlMatch`: 3-grams of the lower-cased text, plus each two-letter word whole
as one more token, `k = 6`, a 2048-bit filter) for stored values and queries
alike. The word token makes a two-letter surname such as "Ng" storable and
searchable: `@@` with "ng" finds "Ng" and "Mike Ng", not "Nguyen". A query's
two-letter words must be whole words of the row (words split on space,
comma, semicolon, colon and exclamation mark), so "Mike Ng" does not find
"Mike Nguyen", and a query is not a plain substring test. The configuration is part of the stored

`TextMatch` builds its filter under a configuration pinned in this crate,
`EqlMatch`. Stored values and queries both use it.

`EqlMatch` takes the 3-grams of the lower-cased text. A 3-gram is a run of
three characters, such as "mik" in "Mike". `EqlMatch` also takes each
two-letter word as one more token, whole. Each token sets `k = 6`
positions in a 2048-bit filter. Words are split on space, comma, semicolon,
colon, and exclamation mark.

The word token lets you store and search a two-letter surname such as "Ng".
`@@` with "ng" finds "Ng" and "Mike Ng", and not "Nguyen". A query asks for
its two-letter words only when the whole query is one two-letter word. A
query of three characters or more asks for its 3-grams alone. So "Mike Ng"
finds "Mike Ng" and "Mike Nguyen", and "san fr" finds "San Francisco".

A query's filter is a `QueryBloomFilter`, with the same JSON as
`BloomFilter`. A query is not a plain substring test. The word token adds
one token for each two-letter word in a stored value. The token is unique
to the word, so someone can tell the rows that contain "Ng" from the rows
that contain "Nguyen".

The configuration is part of the stored
format, so changing it means a new domain or payload version. A filter can
match a row that does not contain the query, so re-check after decrypting
where that matters. Write the stored column on the left, `col @@ query`: the
Expand Down
Loading
Loading