128-bit Blind Indexes
By default a blind index stores the full 256-bit HMAC: 64 hex characters for every index, and for every element of an [ArrayBlindIndex]. An equality lookup does not need that much. With 128-bit blind indexes, Tayra stores the first 128 bits (32 characters) instead, which halves the space blind indexes take in every row and in every database index over them.
128 bits is as reliable as the full hash. The chance that two different values share an index is about 2-128, so even across billions of rows a lookup never returns a record for a different value. It is a size choice, not a privacy one: see Security.
Turning it on
It is opt-in, for the whole application:
services.AddTayra(opts => opts.BlindIndex.Use128BitIndexes = true);Default in the next major version
128-bit blind indexes are planned to become the default in Tayra 3.0. Because lookups also match 256-bit indexes, data written before the upgrade keeps matching without a migration, exactly as it does when you turn the option on today.
It applies to every [BlindIndex] and [ArrayBlindIndex], whether configured with attributes or fluently. Two kinds of index are left as they are:
- An index that sets its own
BitLengthkeeps it. - Compound blind indexes keep 256 bits. Their lookups do not match both forms, so moving one needs a recompute.
Existing data needs no migration
Turning it on changes what new and rewritten records store. Records written before keep their 256-bit indexes until they are rewritten. Lookups still find them:
- Writes store 128 bits. Any save recomputes the index in the new form, and a projection rebuild converts every document it writes.
- Lookups match either form.
BuildPredicateAsyncbuildsx => x.BsnIndex == hash128 || x.BsnIndex == hash256, andBuildContainsPredicateAsyncanOrElseof twoContains. Both come from one HMAC key fetch. The predicate is a plain expression, so Marten, EF Core and MongoDB translate it as usual, and PostgreSQL serves it from the same index that serves a single comparison.
Queries do not change:
// Unchanged: the predicate matches records in either form.
var patients = await session.Query<Patient>()
.Where(await blindIndexer.BuildPredicateAsync((Patient x) => x.ClientBsn, "123456782"))
.ToListAsync();Rolling deploys
While instances without the option still run, they look only for 256-bit indexes, so they miss records that instances with it have already written with 128 bits. Only those new records are affected, only on lookups served by the instances not yet switched, and only until every instance runs with the option on. Nothing is lost: the records are intact and found again once the rollout completes.
To avoid the window, switch every instance at once:
- Blue/green deployment. The old instances keep serving, and writing 256-bit indexes the new ones also find, until traffic moves to the new instances in one step.
- Turn it on through configuration. Deploy the new version with
Use128BitIndexesoff, bound from configuration such asappsettings.json. Then turn it on and restart all instances together, so the window is only as long as the restart. - Stop, then start. Stop every instance and start them on the new version, trading the window for a short outage.
Converting existing records
Records convert to 128 bits on their own as they are saved. To convert the rest, so that you can turn off the 256-bit match, pick one of three ways:
| Way | What it rewrites | Keys and crypto | Suits |
|---|---|---|---|
| Let it happen | nothing extra: saves and projection rebuilds convert what they write | none | most applications |
| SQL in place | only the blind index values | none | large stores that want to convert now |
| Recompute | every record, including every encrypted value | decrypts and re-encrypts every record | stores without direct SQL access |
Convert only once Use128BitIndexes is on for every instance. An instance still writing and looking for 256-bit indexes does not find records you have converted.
SQL in place
The 128-bit index is exactly the first 32 characters of the 256-bit one, so a stored index converts by cutting it to its first half. No key is needed, no encrypted value is read or written, and the result is identical to what Tayra writes for the same value. The statements touch only 64-character lowercase hex values, so they are safe to run again, and each run converts one batch: repeat it until it reports 0 rows. Small batches keep each transaction, and the write-ahead log it produces, small.
On Marten, for a [BlindIndex] companion:
UPDATE mt_doc_patient
SET data = jsonb_set(data, '{ClientBsnIndex}', to_jsonb(left(data ->> 'ClientBsnIndex', 32)))
WHERE id IN (
SELECT id FROM mt_doc_patient
WHERE data ->> 'ClientBsnIndex' ~ '^[0-9a-f]{64}$'
LIMIT 10000);For an [ArrayBlindIndex] companion, element by element, keeping the order:
UPDATE mt_doc_patient
SET data = jsonb_set(data, '{NameSearchTermsIndex}', (
SELECT coalesce(jsonb_agg(CASE WHEN h ~ '^[0-9a-f]{64}$' THEN left(h, 32) ELSE h END ORDER BY i), '[]'::jsonb)
FROM jsonb_array_elements_text(data -> 'NameSearchTermsIndex') WITH ORDINALITY AS e(h, i)))
WHERE id IN (
SELECT id FROM mt_doc_patient
WHERE EXISTS (
SELECT 1 FROM jsonb_array_elements_text(data -> 'NameSearchTermsIndex') AS e(h)
WHERE h ~ '^[0-9a-f]{64}$')
LIMIT 10000);On EF Core with PostgreSQL, for a [BlindIndex] column:
UPDATE "Patients" SET "ClientBsnIndex" = left("ClientBsnIndex", 32)
WHERE "Id" IN (
SELECT "Id" FROM "Patients"
WHERE "ClientBsnIndex" ~ '^[0-9a-f]{64}$'
LIMIT 10000);For an [ArrayBlindIndex] stored as a text[] column:
UPDATE "Patients" SET "NameSearchTermsIndex" = ARRAY(
SELECT CASE WHEN h ~ '^[0-9a-f]{64}$' THEN left(h, 32) ELSE h END
FROM unnest("NameSearchTermsIndex") WITH ORDINALITY AS e(h, i)
ORDER BY i)
WHERE "Id" IN (
SELECT "Id" FROM "Patients"
WHERE EXISTS (SELECT 1 FROM unnest("NameSearchTermsIndex") AS e(h) WHERE h ~ '^[0-9a-f]{64}$')
LIMIT 10000);Use your own table, column and JSON names, and run the statements in every database when documents are spread across shards or tenant databases. They cover companions at the top level of a document or table; other databases convert the same way, by keeping the first 32 characters.
Recompute
The recompute services load every record, compute its blind indexes again, and save it. With Use128BitIndexes on, that writes every index with 128 bits. It needs no SQL, but it decrypts and re-encrypts every record, so it costs as much as rewriting the whole store: plan it like a projection rebuild.
Turning off the 256-bit match
AlsoMatch256BitIndexes is on by default. Once no record holds a 256-bit index, turn it off to drop the second comparison from every lookup:
services.AddTayra(opts =>
{
opts.BlindIndex.Use128BitIndexes = true;
opts.BlindIndex.AlsoMatch256BitIndexes = false;
});Turn it off only when no record holds a 256-bit index, or those records stop matching. On Marten, count the records that still do (a 256-bit index is 64 characters):
SELECT count(*)
FROM mt_doc_patient
WHERE length(data ->> 'ClientBsnIndex') = 64;For an [ArrayBlindIndex], check the elements:
SELECT count(*)
FROM mt_doc_patient
WHERE EXISTS (
SELECT 1 FROM jsonb_array_elements_text(data -> 'NameSearchTermsIndex') AS h
WHERE length(h) = 64);Use the JSON names your serializer writes. On EF Core and MongoDB, count rows or documents whose companion is 64 characters long in the same way. Records nothing ever rewrites keep their 256-bit indexes: convert them first. The option covers every index at once, so leave it on while any of them still has records in the 256-bit form.
Limits
- Only lookups built by
IBlindIndexermatch both forms. A hash computed directly, withComputeHashAsync,ComputeBlindIndexAsyncorComputeBlindIndexesAsync, is the 128-bit form only. Code that compares such a hash itself (a join against archived rows, a hand-writtenAny(hashes.Contains)) misses records with 256-bit indexes until they are converted. - Uniqueness is not enforced across forms. A unique database index on a companion sees the two forms of one value as different, so it cannot catch a duplicate while both forms exist.
- Turning it off again is not matched on the fly: records written with 128 bits stop matching. Run a recompute after turning it off.
