Skip to content

Commit 6197e29

Browse files
authored
Merge pull request #1246 from cipherstash/feat/golang-value-kinds-v2
feat(stack-encrypt,golang): give every Go integer width, dates, timestamps, decimals and 128-bit integers their own kinds
2 parents c9b6d77 + 4b039db commit 6197e29

43 files changed

Lines changed: 4218 additions & 423 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.cargo/mutants.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ exclude_re = [
8888
# A passthrough has no kind: `KindOf::passthrough` returns `Ok(None)`,
8989
# which is `Ok(Default::default())`. `a_frame_names_its_kind` asserts it.
9090
'stack-encrypt/src/dynamic/value_frame\.rs:\d+:\d+: replace <impl Sink for KindOf>::passthrough -> Result<Self::Ok, DecodeError> with Ok\(Default::default\(\)\)$',
91+
# `model`'s `admits` guard on a date, decimal or undefined value is checked
92+
# again by vitaminc on every path that reaches it: the frame writer behind
93+
# `Modeled` and `ModelOnly` refuses an inadmissible logical value
94+
# (`FrameWriteError::Invalid`, mapped to the same `Expected`), and
95+
# `ValueFrame::check` only walks frames `Frame::parse` has validated. So
96+
# `true` there refuses the same values with the same error. The guard stays
97+
# so the model says what it holds without leaning on the writer behind it.
98+
'stack-encrypt/src/dynamic/value_frame\.rs:\d+:\d+: replace match guard logical\.admits\(&value\) with true in model$',
9199
]
92100

93101
# Headroom over the measured baseline before a slow-but-correct mutant is

‎biome.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"!languages/typescript/packages/protect-ffi/src/eql-v3-types",
1313
"!packages/stack-encrypt/tests/fixtures/record_lowering.json",
1414
"!packages/stack-encrypt/tests/fixtures/query_operators.json",
15+
"!packages/stack-encrypt/tests/fixtures/value_kinds.json",
1516
"!packages/eql/tests/encryption/fixtures/ste_vec_selectors.json",
1617
"!packages/eql/tests/encryption/fixtures/v4_rows.json",
1718
"!packages/eql/crates/eql-bindings/bindings",

‎docs/adr/0002-one-value-model-three-encodings-and-eql-v4-by-producer.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bundle (see Consequences). Chosen.
168168
| `0x0E`, `0x0F` | `Int16`, `UInt16` | 2 bytes, little-endian; two's complement for `Int16` |
169169
| `0x10`, `0x11` | `Int128`, `UInt128` | 16 bytes, little-endian; two's complement for `Int128` |
170170
| `0x12` | `Date` | `i32`, little-endian: the day number in the proleptic Gregorian calendar with **0001-01-01 as day 1** (chrono's `num_days_from_ce`). 0001-01-01 is `01 00 00 00`, not zero |
171-
| `0x13` | `Timestamp` | `i64` Unix seconds, then `u32` nanoseconds, both little-endian, UTC. Nanoseconds are below 1,000,000,000, except at a leap second (seconds modulo 60 is 59), where they may go up to 1,999,999,999 (chrono's representation). Anything else is refused |
171+
| `0x13` | `Timestamp` | `i64` Unix seconds, then `u32` nanoseconds, both little-endian, UTC. Nanoseconds are below 1,000,000,000. *Amended 2026-10-18 (#1144):* vitaminc 0.6 refuses a leap second: nanoseconds of 1,000,000,000 or more (chrono's leap-second representation, which the pre-ADR-0001 `Value` codec accepted when the seconds modulo 60 were 59) are refused, because a leap second has no count in any unit. The instant must also lie within chrono's `NaiveDateTime` range (vitaminc's `MIN_TIMESTAMP_NANOS` to `MAX_TIMESTAMP_NANOS`, years -262143 to 262142). Anything else is refused |
172172
| `0x14` | `Decimal` | 16 bytes: a little-endian `u32` of flags (sign in bit 31, scale in bits 16–23 and at most 28, every other bit zero), then the low, middle and high little-endian `u32` words of the 96-bit mantissa. This is rust_decimal's `serialize()` and keeps the scale. There is no NaN or infinity |
173173

174174
- `transport` stays a module of `vitaminc-aead-value`.
@@ -353,6 +353,13 @@ without loss, and refuses it otherwise.
353353
`uint8` and `uint16` get their own kinds when a target names them; no
354354
target does yet, so today they reach `Int16`, `Int32` or `Int64` through
355355
the fits-losslessly rule.
356+
*Amended 2026-10-18 (#1144):* a field with no EQL target (`encrypt`,
357+
`index=`) has no target to pick a kind from, so Go picks it from the Go
358+
type: each integer type is its own kind (`int8` is `Int8`, `int16`
359+
`Int16`, `uint8` `UInt8`, `uint16` `UInt16`, `int` and `int64` `Int64`,
360+
`uint` and `uint64` `UInt64`), and its order terms are that width's. A
361+
field stored as an EQL target still takes the target's kind, by the rule
362+
above.
356363
`Int128` and `Uint128` are SDK value types. `time.Time` means `Timestamp`,
357364
and `encrypt.Date{Year, Month, Day}` is a date. A field whose target is a
358365
date family also accepts `time.Time` and takes its calendar day **in the

‎languages/golang/CHANGELOG.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ listed under **Breaking**. Releases are tagged `languages/golang/vX.Y.Z`.
1212

1313
### Breaking
1414

15+
- **An `int8`, `int16`, `uint8` or `uint16` sealed field seals at its own
16+
width; run stashgen again.** It used to seal as a 32-bit integer. Values
17+
stored before do not decrypt under the regenerated declaration: the
18+
engine checks the stored leaf's kind against the declared one and
19+
refuses an int32 leaf for an `int16` field. Equality terms are the same
20+
at every width, order terms are not. To keep reading stored values,
21+
declare the field `int32` or `uint32` (or re-encrypt them).
1522
- **Run stashgen again.** A field's query methods are now one per operator
1623
the engine answers on it (`Eq`, `Lt`, `Lte`, `Gt`, `Gte`, `Between`,
1724
`Matches`), in place of one per index (`Equality`, `Match`, `Ore`, `Ope`,
@@ -31,6 +38,24 @@ listed under **Breaking**. Releases are tagged `languages/golang/vX.Y.Z`.
3138

3239
### Added
3340

41+
- **Every integer width, 128-bit integers, dates, timestamps and decimals
42+
as sealed fields** (#1144). Each integer type seals at its own width
43+
(`int8` as an int8, `uint16` as a uint16), so `record.Kind` and
44+
`gensupport` gain `Int8`, `Int16`, `Int128`, `Uint8`, `Uint16`,
45+
`Uint128`, `Decimal`, `Date` and `Timestamp`. A `time.Time` field seals
46+
as a timestamp and opens in UTC. `encrypt.Date`, `Int128`, `Uint128` and
47+
`Decimal` are vitaminc's `vcvalue` types, with `encrypt.DateOf`,
48+
`ParseInt128`, `ParseUint128` and `ParseDecimal` (which refuses NaN and
49+
the infinities with `ErrNonFiniteDecimal`). Equality applies to all of
50+
them, ORE and OPE to every integer width. stashgen refuses an order index
51+
on a decimal, date or timestamp, naming #1236. The value-kinds fixture
52+
test opens a record the Rust lowering sealed and derives the same terms.
53+
- **Any built-in Go integer field takes an integer EQL type** (ADR-0002):
54+
an `int16` or a `uint16` into `Integer`, an `int` into `Bigint`. The
55+
field is sealed at the type's width and opens back into the field's
56+
type. A type wider than the EQL type (an `int64` into `Integer`) is range
57+
checked value by value, and `Encrypt` refuses a value that does not fit
58+
with `ErrEncoding`. `encrypt.Int128` and `Uint128` are not converted.
3459
- **`encrypt.MatchQuery` and `encrypt.EQLMatchQuery`**: what a `Matches`
3560
method returns. Each carries the term or EQL query value to bind,
3661
`Verify` (check decrypted rows, since a match can return false positives)

‎languages/golang/cmd/stashgen/README.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ The engine produces these EQL types today, each from one Go field type:
159159
| `encrypt_into` | Go field |
160160
|---|---|
161161
| `TextEq`, `Text`, `TextMatch` | `string` |
162-
| `Integer`, `IntegerEq` | `int32` |
163-
| `Bigint`, `BigintEq` | `int64` |
162+
| `Integer`, `IntegerEq` | `int32`, or any Go integer type |
163+
| `Bigint`, `BigintEq` | `int64`, or any Go integer type |
164164
| `Boolean` | `bool` |
165165
| `Real`, `RealEq` | `float32` |
166166
| `Double`, `DoubleEq` | `float64` |
167167

168168
The `Eq` types answer `Eq`; `Text`, `Integer`, `Bigint`, `Boolean`, `Real` and `Double` are stored and read only, with no query method.
169169

170-
The width must match: an `int` or `int64` field is not an `Integer`, and a `float64` field is not a `Real`.
170+
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`.
171171
A `TextMatch` field refuses text shorter than three characters, such as an empty string or a two-letter code, because it has no 3-grams to index; its query refuses such text too.
172172
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.
173173
`encrypt_into` with any other type is refused with the type's name and the reason it is not produced yet.
@@ -240,10 +240,13 @@ No warning, error or log line holds a plaintext value.
240240
## What crosses the binding
241241

242242
Generated code sends the engine every sealed field with its value, under the declaration lowered to data: each field's label (`<context>/<name>`), its outputs and its wire type (`int64`, `string`, `bytes`, ...), which `stashgen` chose from the field's Go type.
243-
A passthrough field stays on the host: the engine does nothing to it a program could observe, and the FFI codec cannot carry every Go type a program stores beside a ciphertext.
243+
A passthrough field stays on the host: the engine does nothing to it a program could observe, and the FFI codec cannot carry every Go type a program stores beside a ciphertext (a `driver.Valuer`, a `sql.NullTime`).
244244
An `opaque` struct crosses as one JSON document and is one column, decoded back into the exact Go types the struct declares; a field may be any type `encoding/json` carries both ways (a scalar or a type defined over one, `[]byte`, slices, maps with string or integer keys, pointers, structs whose fields are all exported, `time.Time`).
245245
A float in an opaque struct must be finite: `encoding/json` refuses NaN and the infinities, so `Encrypt` fails for the batch with `encrypt.ErrEncoding`. A sealed float field outside an opaque struct carries them.
246246
A sealed field outside an opaque struct is one scalar, or a type defined over one; a struct, slice or map field seals only inside an opaque struct.
247+
The scalars are `string`, `[]byte`, `bool`, every integer type (each sealed at its own width, `int` and `uint` as 64-bit), `encrypt.Int128` and `encrypt.Uint128`, `float32`, `float64`, `encrypt.Decimal`, `encrypt.Date` and `time.Time` (a timestamp). The wire type `stashgen` writes for each is the field's own: `int8` is `gensupport.Int8`, `time.Time` is `gensupport.Timestamp`, and so on.
248+
An order index (`ore`, `ope`) on a decimal, date or timestamp is refused: those order terms wait for new cllw-ore and ore-rs releases (#1236).
249+
The zero `encrypt.Date` is not a date: a date field must be set, and `Encrypt` refuses a zero one with `encrypt.ErrEncoding`, naming the field. A sealed field cannot be a pointer (`*encrypt.Date` is refused as a composite), so an optional date has no sealed form yet: keep it in an opaque struct, or store a sentinel date.
247250
A nil `[]byte` in a sealed field, or a nil slice of a type defined over `[]byte`, comes back from `Decrypt` as an empty, non-nil slice; inside an opaque struct, nil comes back as nil.
248251
A protobuf message with a `oneof` cannot be generated from a policy: protoc-gen-go puts its members in wrapper types, not in the message struct, and `Generate` refuses it.
249252

‎languages/golang/encrypt/README.md‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,42 @@ and `Equality`, `Match`, `Ore` or `Ope` (the term types). A field with
205205
An `opaque` struct is one `Sealed` field. Every stored type implements
206206
`driver.Valuer` and `sql.Scanner`.
207207
208+
A sealed field outside an opaque struct is one value of one of these types,
209+
or a type defined over one (`type Email string`):
210+
211+
| Go field | Seals as | Indexes it can store | Query methods |
212+
|---|---|---|---|
213+
| `string` | text | `equality`, `match`, `ore`, `ope` | `Eq`, `Matches` |
214+
| `[]byte` | bytes | `equality`, `ore`, `ope` | `Eq` |
215+
| `bool` | a boolean | none | none |
216+
| `int8`, `int16`, `int32`, `int64`, `int` | an integer of the field's own width (`int` is 64-bit) | `equality`, `ore`, `ope` | `Eq`, `Lt`, `Lte`, `Gt`, `Gte`, `Between` |
217+
| `uint8` (`byte`), `uint16`, `uint32`, `uint64`, `uint` | the same, unsigned | `equality`, `ore`, `ope` | the same |
218+
| `encrypt.Int128`, `encrypt.Uint128` | a 128-bit integer | `equality`, `ore`, `ope` | the same |
219+
| `float32`, `float64` | a float of its width | `equality`, `ore`, `ope` | `Eq` |
220+
| `encrypt.Decimal` | a decimal: a 96-bit coefficient and a scale up to 28 | `equality` | `Eq` |
221+
| `encrypt.Date` | a calendar date | `equality` | `Eq` |
222+
| `time.Time` | a timestamp, to the nanosecond, in UTC | `equality` | `Eq` |
223+
224+
Each query method needs its index declared. An `ore` or `ope` index on text,
225+
bytes or a float stores its term, but the engine answers no ordering through
226+
it yet, so such a field has no `Lt` (see [Queries](#queries)).
227+
228+
`encrypt.Date`, `Int128`, `Uint128` and `Decimal` are vitaminc's `vcvalue`
229+
types; build them with `encrypt.DateOf`, `ParseInt128`, `ParseUint128` and
230+
`ParseDecimal` (which refuses NaN and the infinities). A `time.Time` opens in
231+
UTC: compare it with `Equal`, not `==`. A decimal opens at the scale it was
232+
sealed with, but a zero has no sign: `-0.00` opens as `0.00`. The zero
233+
`encrypt.Date` is not a date, and `Encrypt` or a query refuses it with
234+
`ErrEncoding`, naming the field; so is a decimal or a `time.Time` outside
235+
vitaminc's range. A sealed field cannot be a pointer, so an optional date has
236+
no sealed form yet: keep it in an opaque struct, or use a sentinel date. Each integer type seals at its own
237+
width, and each width is passed to the order scheme as itself, so an `int8`
238+
field's terms are an `int8`'s. Equality follows vitaminc's normalisation: an
239+
integer equals itself at every width, decimals compare at any scale
240+
(`1.50 = 1.5`), and a timestamp compares to the microsecond. An order index on
241+
a decimal, date or timestamp is refused for now: those order terms wait for
242+
new cllw-ore and ore-rs releases (#1236).
243+
208244
A `match` index stores text with no token (an empty string, separator-only
209245
text, or text shorter than the n-gram length of 3 characters) as the empty
210246
filter: a valid value that no query matches, so an optional or short value can
@@ -243,7 +279,7 @@ rules.
243279
| The field declares | Methods | Each returns |
244280
|---|---|---|
245281
| `index=equality` | `Eq` | `encrypt.EqualityTerm` |
246-
| `index=ore` or `index=ope`, on an integer | `Lt`, `Lte`, `Gt`, `Gte` | `encrypt.OreTerm`, or `encrypt.OpeTerm` when the field declares no `ore` |
282+
| `index=ore` or `index=ope`, on an integer of any width | `Lt`, `Lte`, `Gt`, `Gte` | `encrypt.OreTerm`, or `encrypt.OpeTerm` when the field declares no `ore` |
247283
| the same | `Between(ctx, cipher, lo, hi)` | `encrypt.Range[encrypt.OreTerm]` or `encrypt.Range[encrypt.OpeTerm]`, the two bounds' terms |
248284
| `index=match`, on a string | `Matches` | `encrypt.MatchQuery[T]` |
249285
| `encrypt_into=TextEq` | `Eq` | `eql.TextEqQuery` |
@@ -398,16 +434,25 @@ The EQL types the engine produces, and the Go field types each takes:
398434
| `TextEq` | `string` | `=`, `<>` |
399435
| `TextMatch` | `string` | `@@` (substring match, 3-grams, case-insensitive) |
400436
| `Text` | `string` | none: stored and read only |
401-
| `Integer` | `int32` | none |
402-
| `IntegerEq` | `int32` | `=`, `<>` |
403-
| `Bigint` | `int64` | none |
404-
| `BigintEq` | `int64` | `=`, `<>` |
437+
| `Integer` | `int32`, or any Go integer type (see below) | none |
438+
| `IntegerEq` | the same | `=`, `<>` |
439+
| `Bigint` | `int64`, or any Go integer type | none |
440+
| `BigintEq` | the same | `=`, `<>` |
405441
| `Boolean` | `bool` | none |
406442
| `Real` | `float32` | none |
407443
| `RealEq` | `float32` | `=`, `<>` |
408444
| `Double` | `float64` | none |
409445
| `DoubleEq` | `float64` | `=`, `<>` |
410446

447+
Any built-in Go integer field takes an integer EQL type (ADR-0002): the field
448+
is sealed at the type's width, and opens back into the field's type. A type
449+
every value of which fits (an `int16` or a `uint16` field for `Integer`)
450+
always seals; a wider one (an `int64` or a `uint32` field for `Integer`) is
451+
range checked value by value, and `Encrypt` refuses a value that does not fit
452+
with `ErrEncoding`, naming the field's kind and not the value.
453+
`encrypt.Int128` and `encrypt.Uint128` are not converted, and floats take
454+
exactly their own width.
455+
411456
A `TextMatch` value must be at least three characters long: shorter text has
412457
no 3-grams to index and is refused. The number equality types compare as
413458
PostgreSQL compares the plain types: an integer equals itself whatever its

‎languages/golang/encrypt/doc.go‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
// encrypt_into names the EQL type the field is stored as. The Go field type
2424
// must match it: TextEq, TextMatch and Text take a string, Integer and
2525
// IntegerEq an int32, Bigint and BigintEq an int64, Boolean a bool, Real and
26-
// RealEq a float32, and Double and DoubleEq a float64. The *Eq types answer
27-
// Eq; Integer, Bigint, Boolean, Real, Double and Text are stored and read
28-
// only.
26+
// RealEq a float32, and Double and DoubleEq a float64. An integer EQL type
27+
// also takes any other built-in Go integer type, sealed at the type's width:
28+
// one every value of which fits it (an int16 into Integer) always seals, and
29+
// a wider one (an int64 into Integer) is range checked, a value that does not
30+
// fit refused with [ErrEncoding]. The *Eq types answer Eq; Integer, Bigint,
31+
// Boolean, Real, Double and Text are stored and read only.
2932
//
3033
// 3. Run the generator. It writes user_stash.go beside the struct:
3134
//
@@ -106,11 +109,20 @@
106109
// plan the Rust chain and the derive run. A program never builds or names a
107110
// plan, and no call takes a context or an index choice of its own.
108111
//
112+
// A sealed field outside an opaque struct is one value: a string, []byte,
113+
// bool, an integer of any width (int8 to int64, uint8 to uint64, int, uint,
114+
// or [Int128] and [Uint128]), a float32 or float64, a [Decimal], a [Date] or a
115+
// time.Time (a timestamp), or a type defined over one of the built-in types.
116+
// Each integer seals at its own width. Equality applies to every one but a
117+
// bool; ORE and OPE to the integers, text, bytes and floats, and not yet to a
118+
// decimal, date or timestamp, whose order terms wait for new cllw-ore and
119+
// ore-rs releases (#1236).
120+
//
109121
// Passthrough fields never cross the binding: the engine does nothing to a
110122
// passthrough value that a program could observe, and the FFI codec cannot
111-
// carry every Go type a program stores beside a ciphertext (a time.Time, a
112-
// driver.Valuer). An opaque struct crosses as one JSON document and is one
113-
// column.
123+
// carry every Go type a program stores beside a ciphertext (a
124+
// driver.Valuer, a sql.NullTime). An opaque struct crosses as one JSON
125+
// document and is one column.
114126
//
115127
// # Terms
116128
//

‎languages/golang/encrypt/gensupport/codec.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ func (c *Codec[P, E]) split(vals Values) (record.Source, map[string]any, error)
203203
return nil, nil, fmt.Errorf("the generated Source gave no field %q", f.name)
204204
}
205205
if f.crosses() {
206-
row[f.name] = v
206+
converted, err := asKind(f.kind, v)
207+
if err != nil {
208+
return nil, nil, fmt.Errorf("field %q: %w", f.name, err)
209+
}
210+
row[f.name] = converted
207211
} else {
208212
keep[f.name] = v
209213
}

0 commit comments

Comments
 (0)