Skip to content

Context & encryption

fairlearn_fhe.context.CKKSContext dataclass

make_evaluator_context()

Return a copy of this context with the secret key removed.

Use this when handing the context to an evaluator (e.g. the auditor in a Vendor→Auditor split): the evaluator can perform arithmetic and rotations but cannot decrypt. The keyholder retains the original secret-key context for the final decrypt.

For TenSEAL this serialises the inner context without the secret key and reloads it into a fully independent copy — the keyholder's original context is never mutated. For OpenFHE this clones the :class:OpenFHEContext with keys.secretKey set to None.

fairlearn_fhe.context.build_context(*, backend=None, poly_modulus_degree=1 << 14, scale_bits=40, coeff_mod_bit_sizes=None, batch_size=None, noise_flooding=None, insecure_allow_low_security=False)

Build a CKKS context for the chosen backend.

The defaults match a depth-6 fairness-metric circuit at 128-bit security. For backend="openfhe" the coeff_mod_bit_sizes argument is ignored (OpenFHE chooses the chain itself); pass batch_size to control the number of plaintext slots.

noise_flooding enables decrypt-time noise flooding on backends that support it. Pass either True or one of the recognized strings ("openfhe-NOISE_FLOODING_DECRYPT", "noise-flooding"). Currently honoured by the OpenFHE backend only; TenSEAL ignores the flag because TenSEAL/SEAL has no NOISE_FLOODING_DECRYPT execution mode at the time of this release.

Caller-supplied coeff_mod_bit_sizes are validated against the HE-standard 128-bit security table; combinations that fall below are rejected unless insecure_allow_low_security=True is set.

fairlearn_fhe.context.default_context()

Return the lazily-initialised process-wide default context.

The same object is returned on every call until :func:reset_default_context is invoked. Tests that want a clean context per case should pass an explicit ctx instead of relying on the default.

fairlearn_fhe.encrypted.EncryptedVector dataclass

fairlearn_fhe.encrypted.encrypt(ctx, values)

fairlearn_fhe.encrypted.decrypt(ev)

fairlearn_fhe._groups.EncryptedMaskSet dataclass

One-hot group masks under encryption + plaintext counts.

masks[label] is an :class:EncryptedVector; counts[label] is the plaintext sum of the mask (auditor-public metadata).

For TPR/FPR-style metrics the auditor also needs per-group positive / negative counts. They can be attached via :meth:attach_label_counts once at encryption time when the plaintext y_true is known to the encrypting party.

attach_label_counts(y_true, sample_weight=None, *, plaintext_masks=None)

Stamp per-group positive/negative counts using plaintext y_true.

plaintext_masks is the preferred input: pass the original {0,1} masks held by the encrypting party. When omitted this method falls back to decrypting every encrypted mask, which defeats Mode-B privacy for the sensitive features. A :class:MaskDecryptionWarning is raised in that case.

Returns self for chaining. Prefer constructing the :class:EncryptedMaskSet via :func:encrypt_sensitive_features with y_true= so counts are stamped from plaintext at encryption time and this method is never needed.

fairlearn_fhe._groups.encrypt_sensitive_features(ctx, sensitive_features, *, y_true=None, sample_weight=None)

Encode a plaintext sensitive-features array as an encrypted mask set. The vendor calls this before sending masks to the auditor.

If y_true is supplied, per-group positive / negative counts are stamped on the result without re-decrypting (used by the confusion-matrix circuits).