Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
31d6667
feat(vitaminc-aead): mark the decrypt targets that one scalar builds
coderdan Nov 28, 2026
197074e
feat(stack-encrypt): encrypt and decrypt one value known only at run …
coderdan Nov 28, 2026
6ebb1a1
feat(stack-encrypt)!: decrypt a ciphertext only into a scalar target
coderdan Nov 28, 2026
da9784a
refactor(recipher-js): encrypt through the engine's single-value call
coderdan Nov 28, 2026
6f37abb
test(conformance): add the single-value profile
coderdan Nov 28, 2026
d2ce814
docs(stack-encrypt): describe the single-value calls for a binding au…
coderdan Nov 28, 2026
0ad383b
fix(stack-encrypt): refuse undefined as a single value
coderdan Nov 28, 2026
0dc55d8
docs(stack-encrypt): say that string and json share one stored form
coderdan Nov 28, 2026
bf9b705
docs(adr): record that a single ciphertext's target is now a scalar
coderdan Nov 28, 2026
37b629b
docs(conformance): say the single-value profile runs in Rust only
coderdan Nov 28, 2026
5b4794b
refactor(recipher-js): merge single-value calls with Pending::all
coderdan Nov 28, 2026
4747dd5
refactor(stack-encrypt): drop the batch forms of the single-value calls
coderdan Nov 28, 2026
d81ba19
feat(stack-encrypt): give a single-value refusal its own error variant
coderdan Nov 28, 2026
3b0d643
feat(vitaminc-aead)!: derive ScalarTarget from a target's sink
coderdan Nov 28, 2026
5c2cbc4
docs(stack-encrypt): rewrite the single-value prose in plain language
coderdan Nov 28, 2026
fe7640c
fix(stack-encrypt): report a refused decrypt target in the crate's terms
coderdan Nov 28, 2026
2e6b601
fix(stack-encrypt): say which integers one ciphertext decrypts into
coderdan Nov 28, 2026
25e344a
fix(stack-encrypt): correct which decrypt targets the docs say exist
coderdan Nov 28, 2026
1344f90
Merge next into feat/dynamic-single-value
coderdan Nov 28, 2026
dacd89c
feat(stack-encrypt)!: name the kind of a refused single value
coderdan Nov 29, 2026
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
30 changes: 30 additions & 0 deletions conformance/stack-encrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ these rules.
| `record-values-v1.cases.json` | cases | `tests/value_conformance.rs` | `value_conformance_test.go` |
| `record-query-refusals-v1.cases.json` | cases | `tests/query_refusal_conformance.rs` | `query_refusal_conformance_test.go` |
| `record-raw-frames-v1.cases.json` | cases | `tests/raw_frame_conformance.rs` | `raw_frame_conformance_test.go` |
| `single-value-v1.cases.json` | cases | `tests/single_value_conformance.rs` | — (pending, issue #2010) |
| `single-value-v1.expected.rust-native.json` | expected | `tests/single_value_conformance.rs` (writes it) | — (pending, issue #2010) |
| `artifact-exchange-v1.cases.json` | cases | `tests/exchange_conformance.rs`, run by `scripts/stack-exchange-conformance.mjs` | `exchange_conformance_test.go`, run by `scripts/stack-exchange-conformance.mjs` |
| `eql-json-v1.cases.json` | cases | `tests/json_operators.rs`; EQL's `tests/encryption/tests/json_tree_data_plan.rs` | `json_conformance_test.go` |
| `eql-json-v1.enrollment.json` | enrollment | `scripts/stack-eql-conformance.mjs` runs the tests it names | `scripts/stack-eql-conformance.mjs` runs the tests it names |
Expand Down Expand Up @@ -90,6 +92,9 @@ fixtures described below. It ends with the fresh artifact exchange in both
directions (`test:conformance:exchange`, which also needs the deterministic
EQL guest). Set no fixture-update variables when checking conformance.

The task runs the single-value cases in native Rust only, because the Go
runner of that profile is pending.

The Rust runner is `packages/stack-encrypt/tests/conformance.rs`; the Go runner
is `languages/golang/encrypt/conformance_test.go`. Go calls the record interface
used by generated code. Testing public SDK declarations is a separate layer.
Expand Down Expand Up @@ -177,6 +182,31 @@ in the Rust crate and `TestValueConformance`, `TestQueryRefusalConformance`
and `TestRawFrameConformance` in the Go module; `test:conformance` runs all
six.

## Single values

The [single-value contract](single-value-contract.md) defines
`single-value-v1` in `v1/single-value-v1.cases.json`, and the terms used here.
The profile encrypts one value as one ciphertext through the run-time
interface of the engine, and decrypts it again. Each case gives a value, the
kind declared for it, and a context. The cases cover:

- every scalar kind
- the exact conversions that a declared kind makes
- JSON documents
- the one stored form of a string and a document
- batches of single-value calls merged into one key request
- the refusals of an array, an object, a passthrough, a null, `undefined`,
and a value of another kind

`v1/single-value-v1.expected.rust-native.json` freezes three things for each
case: the frame of the input, the plaintext that the leaf is sealed with, and
a ciphertext that every runner must open.

The native runner is `single_value_conformance` in the Rust crate.
`test:conformance` runs it. The runner also compares the run-time interface
with the typed interface. The Go runner is pending. The Go runner will be
written with the single-value calls of the Go SDK (issue #2010).

## `record-reads-v1` contract

The [read contract](read-contract.md) defines cases in `v1/record-reads-v1.cases.json` for
Expand Down
2 changes: 1 addition & 1 deletion conformance/stack-encrypt/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Paths are relative to the repository root. New profile cases run from `v1/`.
| Requirement | Existing evidence | Remaining work |
|---|---|---|
| Scalar value widths and lossless transport | Initial scalar cases, [value-kind requirements](fixture-contract.md#value-kind-requirements) and the [value boundary profile](values-contract.md) (`v1/record-values-v1.cases.json`: every kind's edges, float specials, bytes, decimal and timestamp precision, sealed/SQL nulls per kind, exact write conversions between widths, cross-kind write refusals, `undefined` on a typed field, Go host conversions); every positive case written fresh by each runner and opened by the other ([exchange profile](exchange-contract.md)) | Python/TS mappings; dates before year 1 and the timestamp range ends |
| Owned opaque ciphertext and storage envelope | `packages/stack-encrypt/tests/scalar.rs`, `frozen_bytes.rs`; initial cross-opening cases; `owned_records.rs`, which tests the owned record derives: passthrough fields are moved, sealed fields are wiped, nested and renamed fields work, a record can store its context, and a column makes one batch. Whole-value blobs: `packages/stack-encrypt/tests/opaque_blob.rs` (round trips, the bytes leaf shared with the structural writer, a pinned storage blob and a pinned sealed ciphertext, refusals by class after authentication, source wipe) and `languages/golang/encrypt/gensupport/gensupport_internal_test.go::TestOpaqueReadsARustWrittenBlob`; the storage profile itself is pinned upstream by Vitamin C's golden corpus; fresh sealed blobs exchanged in both directions by the [exchange profile](exchange-contract.md). JSON documents as one ciphertext: `packages/stack-encrypt/tests/json_plaintext.rs` (round trips, the text leaf shared with a sealed string, refusals after authentication, a generated-document property) and, as the EQL type `Json`, `packages/eql/tests/encryption/tests/v4_types.rs` with the Go-sealed rows `v4_json_rows.json` and the frozen `stored_v4_json_rows_v1.json`, opened in Rust and Go | Encoded targets; supported version matrix |
| Owned opaque ciphertext and storage envelope | `packages/stack-encrypt/tests/scalar.rs`, `frozen_bytes.rs`; initial cross-opening cases; `owned_records.rs`, which tests the owned record derives: passthrough fields are moved, sealed fields are wiped, nested and renamed fields work, a record can store its context, and a column makes one batch. Whole-value blobs: `packages/stack-encrypt/tests/opaque_blob.rs` (round trips, the bytes leaf shared with the structural writer, a pinned storage blob and a pinned sealed ciphertext, refusals by class after authentication, source wipe) and `languages/golang/encrypt/gensupport/gensupport_internal_test.go::TestOpaqueReadsARustWrittenBlob`; the storage profile itself is pinned upstream by Vitamin C's golden corpus; fresh sealed blobs exchanged in both directions by the [exchange profile](exchange-contract.md). One value of a kind known only at run time, as one ciphertext: the [single-value profile](single-value-contract.md), run by native Rust (`v1/single-value-v1.cases.json`: every scalar kind, exact conversions, JSON documents, batches, and the refusals, with the plaintext of each leaf and a frozen ciphertext). `packages/stack-encrypt/tests/single_value.rs` compares the run-time interface with the typed interface. JSON documents as one ciphertext: `packages/stack-encrypt/tests/json_plaintext.rs` (round trips, the text leaf shared with a sealed string, refusals after authentication, a generated-document property) and, as the EQL type `Json`, `packages/eql/tests/encryption/tests/v4_types.rs` with the Go-sealed rows `v4_json_rows.json` and the frozen `stored_v4_json_rows_v1.json`, opened in Rust and Go | Encoded targets; supported version matrix; the Go runner of the single-value profile (issue #2010), and Python and TypeScript runners |
| Record declaration and lowering | [Declaration grammar](declaration-grammar.md); `v1/record-declarations-v1.cases.json` shared by native Rust and Go; native normalization observations; existing record lowering suites; `packages/stack-encrypt/tests/owned_equivalence.rs`, which seals one mixed, searchable record three ways: through a hand-written owned target, the owned derive, and the data plan. All three use one provider that records its requests. The test compares the descriptors and the term bytes, and checks that each way decrypts the records of the others | EQL-resolver profile; SDK-to-declaration mappings; runtime source/read shapes |
| Context binding and composition | `packages/stack-encrypt/tests/target.rs`, `descriptor.rs`, `record_lowering.rs`; `languages/golang/encrypt/context_field_test.go`; initial wrong-context cases | Shared composition vectors; expected-context and required-input matrix |
| Query terms and diagnostics | [Query-operator requirements](fixture-contract.md#query-operator-requirements), existing Rust/Go runners, initial equality/query-error cases and the [query-refusal profile](query-refusals-contract.md) (`v1/record-query-refusals-v1.cases.json`: every scalar operator against every field capability that does not answer it, the listed range shapes, other-kind values and nulls, by class, with the typed query's refusal stage) | JSON operator and EQL target refusals |
Expand Down
241 changes: 241 additions & 0 deletions conformance/stack-encrypt/single-value-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# Single-value profile

`single-value-v1` is the conformance profile for one value encrypted as one
ciphertext. The profile describes the cases in
`v1/single-value-v1.cases.json`. The cases run through the run-time interface
of the engine, `stack_encrypt::dynamic::value`. The profile is an experimental
test contract.

The run-time interface takes a value as a frame, with a declared kind. The
typed Rust interface must decrypt the result. The run-time interface must
decrypt a ciphertext that the typed Rust interface made. So every language
can decrypt a value that one language encrypted.

## Terms

- A frame is one value written as bytes, in the format that the engine reads.
- A kind is the name of what a value is, such as `string` or `uint64`. The
declared kind is the kind that the caller says the value has.
- A leaf is the encrypted form of one scalar. A ciphertext of one value
contains one leaf.
- To seal a value is to encrypt and authenticate it. To open a ciphertext is
to decrypt it and check it.
- A passthrough is a value that a record stores without encrypting it.
- A refusal is an error that the engine returns for a value it does not
accept.
- The typed interface is the Rust API for a value of a Rust type:
`keyset.encrypt` and `keyset.decrypt`.

## Runners

The Rust runner is `packages/stack-encrypt/tests/single_value_conformance.rs`.
The Go runner is pending. The Go runner will be written with the single-value
calls of the Go SDK (issue #2010). The Python and TypeScript runners are
pending too. A missing runner is not a passing result.

## Cases

The file has three parts: a public `seed` for the deterministic key provider,
a list of `cases`, and a list of `batches`. Each case has these keys:

| Key | Meaning |
|---|---|
| `id` | The name of the case |
| `kind` | The name of the declared kind, or empty for no declared kind |
| `context` | The parts of the context, or an empty list for the empty context |
| `input` | The value, in the `{type, text}` form that the [value profile](values-contract.md#cases) defines |
| `stored` | The value in the ciphertext, where it differs from the input |
| `opens_as` | Kinds that the ciphertext must open as, other than the declared kind. Each entry has the `value` that the ciphertext opens to. |
| `refused_as` | Kinds that the ciphertext must not open as |
| `expected_error` | For a refused case, the `code`, the `reason`, and the `found` kind of the refusal |

A context of two parts is a pair. For example, `["conformance", "value"]` is
the Rust context `("conformance", "value")`. For an empty list, a runner calls
the form with no context. For any other list, a runner calls the form with a
context.

This profile adds two input types to those of the value profile:

| `type` | `text` |
|---|---|
| `uuid` | The UUID in lowercase hex, in groups of 8, 4, 4, 4, and 12 digits |
| `shape` | The name of a value that `{type, text}` cannot spell. The next table lists the names. |

| Shape | The value |
|---|---|
| `array` | The array of the two strings `a` and `b` |
| `empty_array` | The array with no elements |
| `object` | The object with one member, `a`, the `uint32` 1 |
| `passthrough` | A passthrough of the string `clear` |
| `typed_null` | A typed null of `int64` |

Some host languages cannot build every shape. A runner in such a language
sends the frame that the artifact file records for the case.

## Artifacts

The Rust runner writes `v1/single-value-v1.expected.rust-native.json`. The
file has one entry for each case:

- `frame`: the frame of the input, in hex. Every case has one.
- `leaf`: the plaintext that the leaf of the ciphertext is sealed with, in
hex. That plaintext is the type tag of the leaf and its payload. Only an
accepted case has one.
- `ciphertext`: a ciphertext of the value, in hex. Only an accepted case has
one.

The typed Rust interface made each `ciphertext` where a Rust type spells the
stored value. No Rust type spells a lone `uint8`, so the run-time interface
made the ciphertext of that case.

A runner never compares whole ciphertexts. Each sealed leaf has a random
AES-GCM nonce, so two ciphertexts of one value differ. A runner compares the
parts of a ciphertext that the nonce does not change:

- the keyset
- the IV and tag of the data key
- the length of the ciphertext
- the plaintext of the leaf

The deterministic provider derives a data key from the seed, the context, and
a counter. So a runner makes a new provider for each ciphertext it compares.

Regenerate the artifacts deliberately, with `STACK_ENCRYPT_UPDATE_FIXTURES=1`
and the native runner. Review the diff of the cases and the diff of the
artifacts together.

An update keeps the frozen ciphertext of a case whose frame and leaf have not
changed. The update checks that ciphertext as any run does. So a ciphertext
stays frozen from one update to the next, and the diff shows only the cases
that changed.

## Required behavior

### An accepted case

A case with no `expected_error` is an accepted case. For each one:

1. The frame of the input equals the frozen `frame`.
2. The run-time encrypt accepts the value with the declared kind and the
context. The plaintext of the leaf that it seals equals the frozen `leaf`.
3. The new ciphertext and the frozen `ciphertext` agree in every part that
the nonce does not change.
4. Each of the two ciphertexts opens under the same context, with no declared
kind. The result is `stored`, or the input where the case has no `stored`.
5. Each of the two ciphertexts opens under the same context, with the
declared kind, to the same value. With the kind `json`, the result is the
stored JSON text as a bytes value.
6. Each ciphertext opens as each kind in `opens_as`, to the `value` of that
entry.
7. Each ciphertext is refused when it is opened as a kind in `refused_as`.
The code of the refusal is `stack_encrypt::opened_type`. The `expected`
field of the refusal is the name of the kind. The refusal never says what
the value opened as.
8. Each ciphertext is refused under the context `["conformance", "wrong"]`.
The deterministic provider refuses the key request as forbidden, as
ZeroKMS does. Other providers can report a wrong context differently.

Native Rust also checks two things. The typed interface seals the same parts
for the stored value. The typed decrypt opens both ciphertexts.

### Conversions

A case with `stored` is a conversion. The engine seals a number of another
width or kind as the value of the declared kind, when the conversion is exact.
The conversion rules are the ones that a typed field of a record uses. The
engine seals a document with the kind `json` as its compact JSON text.

### Strings and JSON documents

`json` is a declared kind and not a stored kind. A `string` and a document
have one stored form: text. So a check of the stored kind cannot distinguish
them. Three groups of cases pin the consequences:

- `json-given-string`: a sealed document opens as `string`, to the JSON text
that was sealed.
- `string-json-number-text` and `string-json-spaced-text`: a sealed string
whose text is a JSON document opens as `json`. Decrypt as `json` checks that
the text parses. Decrypt then returns the document as the engine writes it,
as a bytes value. So `{ "b" : [ 1 , null ], "a" : true }` comes back as
`{"a":true,"b":[1,null]}`.
- `string-not-json-text`: a sealed string whose text is not a JSON document is
refused as `json`.

### A refused case

A case with `expected_error` is a refused case. The run-time encrypt refuses
the value before any key request, with the diagnostic `code`, `reason`, and
`found` of the case. The code of every such refusal is
`stack_encrypt::dynamic_value`. A single value has its own refusal, with no
record, plan, or field in it.

`found` is the kind of the value to encrypt, as the payload field `found`
names it. Every refusal of a single value names that kind. The name is a
kind name such as `array`, `string`, or `int64`, or one of `null`,
`undefined`, `passthrough`, and `unknown`. The kind is the kind of the
input. A refusal never names the kind of a decrypted value.

The cases cover these values:

- An array, an object, a passthrough, a plain null, and a typed null. Each is
tested with no declared kind and with one. The reason is `not_a_scalar`.
`found` is `array`, `object`, `passthrough`, or `null`. A typed null is a
`null`.
- JavaScript's `undefined`, with a declared kind and with none. The reason is
`not_a_scalar`. `found` is `undefined`.
- A value of another kind that does not convert exactly. The reason is
`field_type`. `found` is the kind of the input.
- Text that is not a JSON document, with the kind `json`. The reason is
`json_syntax`, `json_duplicate_key`, or `json_integer_range`. `found` is
`string` or `bytes`.

A runner checks all three of `code`, `reason`, and `found`. `reason` says
which rule the value breaks, and `found` says what the value is.

The engine refuses `undefined` with any declared kind. An `undefined` value is
most often a misspelt property name. No typed call could read it back.

A host language with no `undefined` has two options for those cases. The
runner can send the frame that the artifact file records. Or the runner can
check that the cases are well formed, and skip them.

## Batches

The run-time interface has no batch call. To encrypt many values, a binding
makes one single-value call for each value. The binding then merges the
pending calls into one key request. In Rust, `Pending::all` merges the calls.
A binding decrypts many ciphertexts the same way.

Each call has its own declared kind, so one batch can contain values of
different kinds. Each entry of `batches` has three keys:

- `inputs`: a list of values
- `kinds`: the declared kind for each input, or empty for none
- `context`: one context for all the inputs

A runner seals and opens each entry as a binding does:

- An accepted batch seals in one key request and opens in one key request.
The values come back in the order of the inputs. The expected values are
`stored` where the batch has one, and the inputs otherwise.
- An empty batch makes no key request.
- In a batch with `expected_error`, the single-value call refuses one of the
values with that `code`, `reason`, and `found`. The batch then makes no
key request.

The counts of key requests are native observations: the Rust runner counts
them on its key source.

## Scope and remaining work

The profile covers every scalar kind, the exact conversions, JSON documents,
and the refusals of the run-time encrypt. The profile does not cover these:

- A ciphertext of a null. Only the tree interface seals one.
- A scalar of a logical type that the engine does not define. The
`{type, text}` form cannot spell one. With no declared kind, the engine
seals such a scalar as it arrived.
`packages/stack-encrypt/tests/single_value.rs` tests that natively.
- Decrypt limited to one keyset.
`packages/stack-encrypt/tests/single_value.rs` tests that natively.
- New ciphertexts exchanged between languages. That needs a second runner.
Loading
Loading