Skip to content

Array Blind Indexes ​

[BlindIndex] makes a single encrypted string queryable. [ArrayBlindIndex] does the same for a string collection - a string[], List<string>, HashSet<string>, and similar. Tayra computes one HMAC hash per element and stores the result in a companion collection of the same shape and length, so you can answer questions like "find every subject whose email list contains alice@example.com" without decrypting a single row.

Version compatibility

[ArrayBlindIndex] is a Tayra.Core feature with no framework-specific integration code, so it works across every supported store and integration package - Tayra.Marten8 / Tayra.Marten9, Tayra.Wolverine5 / Tayra.Wolverine6, EF Core (Npgsql), and MongoDB. The companion collection is plain data that every supported serializer round-trips natively. The one version-specific point is a Marten duplicated column for the companion, which needs Tayra.Marten9 (see below).

When to Use ​

Reach for [ArrayBlindIndex] when:

  • A property holds a collection of personal strings (email addresses, phone numbers, aliases, prior surnames) and you need equality lookups against the collection.
  • You want native LINQ Contains / Any to translate into an indexed database operation rather than a full table scan.

It is the collection sibling of [BlindIndex]. The HMAC machinery, key separation, transforms, scope, and truncation all behave exactly as they do for the scalar attribute - applied per element.

Quick Start ​

Apply [ArrayBlindIndex] next to [PersonalData] and add a companion collection property:

cs
public class User
{
    [DataSubjectId]
    public Guid Id { get; set; }

    [PersonalData]
    [ArrayBlindIndex(
        IndexName = "EmailsIndex",
        IndexPropertyName = nameof(EmailsIndex),
        Transforms = ["lowercase", "trim"])]
    public string[] Emails { get; set; } = [];

    /// <summary>
    /// Companion collection - one HMAC hash per element of Emails.
    /// Same kind (array) and same element nullability as the source.
    /// </summary>
    public string[] EmailsIndex { get; set; } = [];
}
anchor

When EncryptAsync runs (or the EF Core / Marten interceptor fires on save), Tayra enumerates Emails, applies the transforms to each element, HMACs it, and writes the hashes into EmailsIndex element-by-element. The source order, length, and duplicates are preserved.

Attribute Reference ​

[ArrayBlindIndex] has the same properties as [BlindIndex]. There are no array-specific knobs (no dedupe option, no length cap - those are the caller's responsibility). One default differs: setting IndexName does not move the companion, which stays {PropertyName}Index (see Index name and companion).

PropertyTypeDefaultDescription
IndexNamestring?IndexPropertyName if set, else {PropertyName}IndexLogical query name passed to ComputeBlindIndexAsync.
IndexPropertyNamestring?{PropertyName}IndexName of the companion collection property that holds the hashes. Does not depend on IndexName.
BitLengthint0 (full 256-bit)Truncates each element hash to the given bit length. See Security Considerations.
Scopestring"default"HMAC key scope. Elements in the same scope share an HMAC key.
SharedAcrossTenantsboolfalseKeeps one HMAC key across tenants when keys are partitioned by tenant.
Transformsstring[][]Normalisation transforms applied to each element before hashing. See Transforms.

Fluent Configuration ​

If you prefer keeping attributes off your model, configure the same index fluently. ArrayBlindIndex sits alongside the scalar BlindIndex and CompoundBlindIndex builders and exposes the full attribute surface: transforms, StoredIn (companion), WithIndexName, WithScope, and WithBitLength.

cs
services.AddTayra(opts =>
{
    opts.LicenseKey = licenseKey;
    opts.Entity<User>(e =>
    {
        e.DataSubjectId(u => u.Id);
        e.PersonalData(u => u.Emails);
        e.ArrayBlindIndex(u => u.Emails)
            .WithLowercase()
            .WithTrim()
            .StoredIn(u => u.EmailsIndex)   // optional; defaults to {Property}Index
            .WithScope("emails")
            .WithBitLength(0);
    });
});
anchor

The fluent and attribute paths produce identical companions for the same plaintext, transforms, and scope. The shape rules below are enforced either way - the fluent path validates the source and companion collection types when the metadata is built (throwing InvalidOperationException on a bad shape), while the attribute path is additionally checked at compile time by TAYRA010.

Source and Companion Shape Rules ​

The companion property must be the same collection kind and same element nullability as the source. The analyzer TAYRA010 enforces this at compile time; a runtime guard backs it up for reflection-defined types.

Declared source typeRequired companion type
string[]string[]
string?[]string?[]
List<string>List<string>
List<string?>List<string?>
IList<string>IList<string>
IList<string?>IList<string?>
IReadOnlyList<string>IReadOnlyList<string>
IReadOnlyList<string?>IReadOnlyList<string?>
HashSet<string>HashSet<string>
HashSet<string?>HashSet<string?>
IEnumerable<string>Rejected - needs a materialized shape
Non-string elements (int[], Guid[], ...)Rejected - v1 supports string elements only

Notes:

  • Source nullability drives the contract. If the source declares non-null elements (string[], List<string>), a null element at write time is a caller bug and the indexer throws. Declare the nullable variant (string?[], List<string?>) if your data legitimately contains null slots.
  • HashSet<string> dedupes by set semantics. Because the source set already holds distinct values, the companion holds distinct hashes. Order is not preserved (consistent with HashSet).

Null, Empty, and Duplicate Behavior ​

Source stateCompanion resultNotes
Whole collection is nullCompanion left untouched (no-op)Tayra never overwrites with an empty collection here.
Empty collectionEmpty companion of the same kindstring[0] to string[0], List<string>(0) to List<string>(0).
null element, source admits nulls (string?[])null companion slotIndex alignment preserved.
null element, source declares non-null (string[])Indexer throwsCaller bug surfaced loudly with the entity type, property, and index.
"" (empty string) elementHMAC(transforms(""))Hashed normally - same as scalar [BlindIndex].
Duplicate elements (["a", "a"])Duplicate hashes ([h("a"), h("a")])Preserved, unless the source is HashSet<string>, which dedupes first.

Querying ​

Querying uses each LINQ provider's native collection operators against the companion - there are no Tayra-specific LINQ extensions or expression rewriters.

Single value ​

IBlindIndexer.BuildContainsPredicateAsync builds the predicate from the source member, so the query site names neither the index nor the companion:

cs
// "users whose email list contains this address", without naming the index or the companion
var match = await blindIndexer.BuildContainsPredicateAsync((User u) => u.Emails, "alice@example.com");

var users = await session.Query<User>().Where(match).ToListAsync();
anchor

It returns u => u.EmailsIndex.Contains(hash), a plain expression that composes with the rest of your query. For tenant-partitioned keys, use BuildContainsPredicateForTenantAsync. To write the query yourself, compute the hash and compare the companion:

cs
// "users whose email list contains this address"
var hash = await tayra.ComputeBlindIndexAsync(
    "alice@example.com", "EmailsIndex", typeof(User));

var users = await session.Query<User>()
    .Where(u => u.EmailsIndex.Contains(hash))
    .ToListAsync();
anchor

IBlindIndexer.ComputeHashAsync((User u) => u.Emails, "alice@example.com") computes the same hash from the member instead of the index name.

Set of values ​

ComputeBlindIndexesAsync hashes a batch of search terms in one call, reusing the cached HMAC key:

cs
// "users matching any of these addresses"
var hashes = await tayra.ComputeBlindIndexesAsync(
    ["alice@example.com", "bob@example.com"], "EmailsIndex", typeof(User));

var users = await session.Query<User>()
    .Where(u => u.EmailsIndex.Any(h => hashes.Contains(h)))
    .ToListAsync();
anchor

The two methods are ITayra.ComputeBlindIndexAsync(string value, string indexName, Type entityType, CancellationToken ct = default), which returns one hash as a Task<string>, and the ITayra extension TayraBlindIndexExtensions.ComputeBlindIndexesAsync(IEnumerable<string> values, string indexName, Type entityType, CancellationToken ct = default), which returns a Task<IReadOnlyList<string>>.

Both take the Type entityType so Tayra can resolve the index definition and its scope. See Querying for the full pattern reference.

Per-Backend Storage and Query ​

Tayra stays storage-agnostic - it computes the companion collection and lets your store persist it. The guidance below covers the shape and index recommendation for each backend.

Marten (JSONB) ​

Marten serialises the companion as a JSON array inside the document, and translates Contains on it to containment over the whole document: d.data @> '{"EmailsIndex": ["<hash>"]}'. Only a GIN index over the whole document serves that, so register one:

cs
services.AddMarten(opts =>
{
    opts.Connection(connectionString);

    // A GIN index over the whole document: Marten turns Contains on the companion
    // into containment over the document (data @> ...), which only this index serves.
    opts.Schema.For<User>().GinIndexJsonData();
});

// Wraps the Marten serializer so [PersonalData] is encrypted at rest
services.UseTayra();
anchor

A GIN index on the companion path alone is not used

opts.Schema.For<User>().Index(x => x.EmailsIndex, idx => idx.Method = IndexMethod.gin) indexes the expression (data ->> 'EmailsIndex')::jsonb, which the whole-document containment above never matches. PostgreSQL scans the table. Earlier versions of these docs recommended that index; replace it with GinIndexJsonData(), or with the duplicated column below.

A whole-document GIN index covers every value in the document, ciphertext included. For large documents, a duplicated text[] column holding only the companion keeps the index small.

Duplicated fields: companion only, never the source ​

The companion holds HMAC hashes, not personal data, so it is safe to expose as a Marten duplicated field - the same pattern TAYRA008 steers you toward for scalar [BlindIndex] companions. The [PersonalData] source array must never be duplicated: Marten reads a duplicated column off the .NET object outside Tayra's serializer, so duplicating the source would write plaintext (TAYRA008 flags it).

cs
opts.Schema.For<User>()
    // OK: the companion is hashes, not PII
    .Duplicate(x => x.EmailsIndex, pgType: "text[]", configure: idx => idx.Method = IndexMethod.gin);
    // NOT x.Emails - that is [PersonalData]; TAYRA008 will flag it
anchor

Marten's LINQ translates Contains on a duplicated array column to ? = ANY(col), which a GIN index does not serve. Query the column with the containment operator through MatchesSql instead, which the GIN index does serve:

cs
var hash = await blindIndexer.ComputeHashAsync((User u) => u.Emails, "alice@example.com");

// Containment on the duplicated column, which its GIN index serves
var users = await session.Query<User>()
    .Where(u => u.MatchesSql("d.emails_index @> ?", new object[] { new[] { hash } }))
    .ToListAsync();
anchor

The column name is Marten's snake_case form of the member (EmailsIndex becomes emails_index).

Tayra.Marten9 only

Marten 8 fills a duplicated column from the document before Tayra's serializer computes the companion, so with Tayra.Marten8 the column is written empty and this query matches nothing. On Marten 8, use the whole-document GIN index above.

EF Core + Npgsql (text[]) ​

Map the companion as a PostgreSQL text[] column and add a GIN index. Contains translates to the @> containment operator:

cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>(b =>
    {
        b.Property(u => u.EmailsIndex)
            .HasColumnType("text[]");

        b.HasIndex(u => u.EmailsIndex)
            .HasMethod("gin");
    });
}
anchor
cs
var hash = await tayra.ComputeBlindIndexAsync(
    "alice@example.com", "EmailsIndex", typeof(User));

var users = await db.Users
    .Where(u => u.EmailsIndex.Contains(hash))
    .ToListAsync();
anchor

The generated SQL uses the containment operator against the GIN index:

sql
WHERE x."EmailsIndex" @> ARRAY[@hash]::text[]

The set query also works:

cs
var hashes = await tayra.ComputeBlindIndexesAsync(
    ["alice@example.com", "bob@example.com"], "EmailsIndex", typeof(User));

var users = await db.Users
    .Where(u => u.EmailsIndex.Any(h => hashes.Contains(h)))
    .ToListAsync();
anchor

EF Core + SQL Server / SQLite (caller-driven) ​

SQL Server and SQLite have no native array column type. Tayra does not ship migration helpers for them in v1, but the companion still works if you provide the storage shape yourself. Two common patterns:

  1. JSON column with a value converter. Map the companion string[] to a JSON nvarchar(max) / text column via a ValueConverter, and query with the provider's JSON functions (for example OPENJSON on SQL Server). Containment may need an explicit JSON-path predicate.
  2. Sidecar table. Project the companion into a (EntityId, Field, Hash) table with a composite index on (Field, Hash), write the join yourself, and treat it as a dev-only pattern.

These are guidance only - they are not shipped or test-covered in v1.

MongoDB (multikey) ​

MongoDB stores the companion as a native BSON array. Native LINQ Contains translates to a multikey-indexable match. Create a multikey index on the companion field:

cs
var keys = Builders<User>.IndexKeys.Ascending(u => u.EmailsIndex);
await collection.Indexes.CreateOneAsync(new CreateIndexModel<User>(keys));
anchor
cs
var hash = await tayra.ComputeBlindIndexAsync(
    "alice@example.com", "EmailsIndex", typeof(User));

var users = await collection
    .Find(u => u.EmailsIndex.Contains(hash))
    .ToListAsync();
anchor

Automatic recompute after key rotation is not yet available for MongoDB. If you rotate the HMAC key, re-hash and re-save affected documents yourself until the Mongo recompute service ships.

Recompute After Key Rotation ​

When you rotate an HMAC key, the existing companion hashes become stale. The recompute services in Tayra.EFCore and Tayra.Marten handle array companions automatically: for each entity they read the source collection and re-hash every element under the new key, preserving null slots and collection shape. No array-specific call is required - run the same RecomputeAsync<T> you use for scalar indexes. See Recompute.

Security Considerations ​

Array blind indexes inherit every trade-off of scalar blind indexes (see Security) and add one of their own: element multiplicity leakage.

  • Frequency leakage is multiplied across array length. As with scalar indexes, identical plaintext produces identical hashes, so an attacker reading the companion can perform frequency analysis. With arrays this is worse: a common value that appears in many elements across many records produces a high-frequency hash that stands out more sharply than a single-column scalar would.
  • Use HashSet<string> to drop duplicate-frequency leakage. If the same value can legitimately appear multiple times within one record's collection, a List<string> / string[] companion leaks that per-record multiplicity (the hash repeats). Declaring the source as HashSet<string> collapses duplicates to a single hash per record via set semantics, removing the within-record frequency signal.
  • Keep HashSet<string> companions at 128 bits or the full hash. A short BitLength introduces collisions, and set semantics then absorb those collisions silently. The two effects compound: truncation false positives plus set absorption widen the query-hit fan-out more than they would for an ordered List<string> / string[] companion at the same BitLength. At 128 bits or more, collisions are too rare to matter.

For low-cardinality collection values the same advice as scalar indexes applies - avoid blind indexes where the value space is small enough to enumerate.

See Also ​