Skip to content

Commit ccd4431

Browse files
committed
feat(stack-encrypt)!: let an inverted Between range through, as SQL does
A `Between` whose low bound is above its high bound was refused with `TermError::InvertedRange` before either term was derived. SQL's own `BETWEEN` refuses nothing: `x BETWEEN 20 AND 10` is a valid query that matches no row, and an encrypted range now behaves the same way. The empty result leaks nothing and corrupts nothing, so a refusal bought little. It also cost more than it bought. Judging a range correctly for text and floats means reproducing, before encryption, the order the index compares in: text by its fold, floats by their canonical encoding. The check did that by encrypting both bounds under a fixed public OPE key, which tied a query-time refusal to one scheme's ciphertext. That order belongs to the ORE crates, not to this one. So `TermError::InvertedRange` (code `stack_encrypt::inverted_range`) goes, with the typed path's `Ordered::inverted`, the data path's `bound_order` and `integer_order`, and the comparison scheme behind them. The variant is under [Unreleased] and never shipped. The data path still refuses order text with no fold (a code point Unicode 16 does not assign) before a term or a keyset, for one value and now for each bound of a pair. It asks `vitaminc_ore::text` directly, which is the encoding `vitaminc_ore::order` uses for the `Text` target, so it refuses exactly what derivation would. Claude-Session: https://claude.ai/code/session_01BZhkvKLpNtKuzj3HCfMXoL
1 parent f915cf5 commit ccd4431

11 files changed

Lines changed: 123 additions & 266 deletions

File tree

‎languages/golang/encrypt/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,10 @@ field takes a `uint32`.
255255
Text orders by its folded form (ADR-0002): case and accents are folded and
256256
`ß` becomes `ss`, under Unicode 16. So `Café`, `cafe` and `CAFÉ` are one
257257
value and tie, `alice` orders before `Zoe`, and `é` orders with `e`, not
258-
after `z`. `Between("b", "A")` is refused as an inverted range, since `A`
259-
folds to `a`. Text holding a code point Unicode 16 does not assign has no
258+
after `z`. `Between` is inclusive at both ends and, as SQL's `BETWEEN`,
259+
does not refuse a range whose low bound is above its high bound: such a
260+
range matches nothing. So `Between("b", "A")` matches nothing, since `A`
261+
folds to `a`, below `b`. Text holding a code point Unicode 16 does not assign has no
260262
fold, and its query or write is refused with `encrypt.ErrTerm`.
261263
262264
An order index on bytes or a float stores its term but answers no ordering

‎languages/golang/encrypt/guest/tests/native_ops.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,13 +1559,6 @@ fn query_term_validation_refuses_what_the_query_op_refuses() {
15591559
"between",
15601560
STATUS_ENCODING,
15611561
),
1562-
(
1563-
"inverted range",
1564-
FfiValue::Array(vec![FfiValue::UInt32(65), FfiValue::UInt32(18)]),
1565-
"age",
1566-
"between",
1567-
STATUS_TERM,
1568-
),
15691562
("no token", s("al"), "name", "matches", STATUS_TERM),
15701563
] {
15711564
let input = encode(input);

‎languages/golang/encrypt/order_test.go‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"cmp"
66
"context"
7-
"errors"
87
"math/rand"
98
"reflect"
109
"slices"
@@ -212,8 +211,8 @@ func TestTermOrderIsPlaintextOrder(t *testing.T) {
212211
}
213212

214213
// The generated ordering methods on a text field derive the term its
215-
// Encrypt stores for the folded value, and Between refuses a range that
216-
// is inverted once folded.
214+
// Encrypt stores for the folded value, and Between orders its bounds by
215+
// their folds.
217216
func TestTextOrderingMethodsFoldTheValue(t *testing.T) {
218217
c := deterministicClient(t)
219218
cipher := c.DefaultKeyset()
@@ -239,10 +238,20 @@ func TestTextOrderingMethodsFoldTheValue(t *testing.T) {
239238
if r.Low.Compare(r.High) >= 0 {
240239
t.Errorf("a should order below Zoe")
241240
}
242-
if _, err := f.S.Between(ctx, cipher, "b", "A"); !errors.Is(err, encrypt.ErrTerm) {
243-
t.Errorf("Between(b, A) = %v, want ErrTerm: A folds to a, below b", err)
241+
// A range inverted once folded is not refused, as SQL's BETWEEN is
242+
// not: it matches nothing.
243+
inverted, err := f.S.Between(ctx, cipher, "b", "A")
244+
if err != nil {
245+
t.Fatalf("Between(b, A): %v", err)
246+
}
247+
if inverted.Low.Compare(inverted.High) <= 0 {
248+
t.Errorf("Between(b, A): A folds to a, below b")
249+
}
250+
tie, err := f.S.Between(ctx, cipher, "A", "a")
251+
if err != nil {
252+
t.Fatal(err)
244253
}
245-
if _, err := f.S.Between(ctx, cipher, "A", "a"); err != nil {
246-
t.Errorf("Between(A, a) is one folded value: %v", err)
254+
if tie.Low.Compare(tie.High) != 0 {
255+
t.Errorf("Between(A, a) is one folded value")
247256
}
248257
}

‎languages/golang/encrypt/query_test.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ func TestAQueryTheFieldDoesNotAnswerIsRefused(t *testing.T) {
316316
t.Fatalf("%s on %s: %v, want field %q and reason %q", tc.operator, tc.field, err, tc.field, tc.reason)
317317
}
318318
}
319-
if _, err := cipher.QueryTerm(ctx, queryPlan, "age", "between", []any{uint32(65), uint32(18)}); !errors.Is(err, encrypt.ErrTerm) {
320-
t.Fatalf("an inverted range: %v, want ErrTerm", err)
319+
// An inverted range is not refused, as SQL's BETWEEN is not.
320+
if _, err := cipher.QueryTerm(ctx, queryPlan, "age", "between", []any{uint32(65), uint32(18)}); err != nil {
321+
t.Fatalf("an inverted range: %v", err)
321322
}
322323
if _, err := cipher.QueryTerm(ctx, queryPlan, "name", "matches", "al"); !errors.Is(err, encrypt.ErrTerm) {
323324
t.Fatalf("text that cuts no token: %v, want ErrTerm", err)
@@ -498,9 +499,9 @@ func TestGeneratedQueriesAreTheRustFixtures(t *testing.T) {
498499
t.Errorf("%s.Value() = %T %v", label, got, err)
499500
}
500501
}
501-
// An inverted range is the typed path's refusal, from the engine.
502-
if _, err := fields.Age.Between(ctx, cipher, 65, 18); !errors.Is(err, encrypt.ErrTerm) {
503-
t.Errorf("an inverted range: %v, want ErrTerm", err)
502+
// An inverted range is not refused, as SQL's BETWEEN is not.
503+
if _, err := fields.Age.Between(ctx, cipher, 65, 18); err != nil {
504+
t.Errorf("an inverted range: %v", err)
504505
}
505506
}
506507

‎packages/stack-encrypt/CHANGELOG.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9090
for the value (`OreTerm`, `OpeTerm`); a range's is one `sem::RangeQuery`
9191
carrying both bounds' terms, read with `lo()` and `hi()`, so no statement
9292
can pair the low bound of one query with the high bound of another. A
93-
range whose low bound is above its high bound is refused when the query
94-
is awaited, before either term is derived (`TermError::InvertedRange`).
93+
range whose low bound is above its high bound is not refused: as SQL's
94+
`BETWEEN`, it matches nothing.
9595
What a query takes is the operator's `op::Arity` (`op::One` or
9696
`op::Pair`), and `op::Input` relates the value given to the plaintext the
9797
plan is checked against (ADR-0009, decision 7).
@@ -180,9 +180,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
180180
point Unicode 16 does not assign has no order term
181181
(`TermError::Normalize`), on write and on query. The ordering operators
182182
are answered on text, on the typed path (`op::Ordered` for `String`) and
183-
on the data-plan path, so a binding generates them too. `Between` judges
184-
an inverted range in the order the stored terms compare, by comparing the
185-
bounds' encryptions: `("b", "A")` is inverted, `("A", "a")` is one value.
183+
on the data-plan path, so a binding generates them too.
186184

187185
- **A match value with no tokens is stored as the empty filter, so
188186
`KeysetCipher::match_terms` is no longer a query.** In 0.2.0 the same call

‎packages/stack-encrypt/docs/adr/0009-operators-select-the-index-and-a-query-term-is-not-the-stored-term.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,11 @@ the ranked loop is worth packaging; it is deferred, not refused.
572572

573573
`cipher.query(op::Between, &(lo, hi)).on(&plan)` produces one term,
574574
`sem::RangeQuery`, carrying the index's term for each bound (the ORE left
575-
ciphertexts, once block ORE lands), inclusive at both ends, and the client
576-
refuses `lo > hi` (`TermError::InvertedRange`) before either term is
577-
derived; `(v, v)` is one value. One call derives both bounds, under one
575+
ciphertexts, once block ORE lands), inclusive at both ends; `(v, v)` is one
576+
value. The client does not refuse `lo > hi`: as SQL's `BETWEEN`, such a
577+
range matches nothing. Judging it would mean reproducing, before
578+
encryption, the order the index compares in (text by its fold, floats by
579+
their canonical encoding), which belongs to the ORE crates. One call derives both bounds, under one
578580
context, so a caller cannot pair a bound from one column with a bound from
579581
another. The ordering operators are answered over the integer types and
580582
text (`op::Ordered`), by the ORE index by default and by OPE through

‎packages/stack-encrypt/src/codes.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ mod tests {
8080
TermError::Ore(_) => TermError::Ore(cllw_ore::Error),
8181
TermError::InvalidOptions(_) => TermError::InvalidOptions("k out of range"),
8282
TermError::EmptyTermText => TermError::EmptyTermText,
83-
TermError::InvertedRange => TermError::InvertedRange,
8483
TermError::NoEqualityTerm => TermError::NoEqualityTerm,
8584
TermError::NoOrderTerm => TermError::NoOrderTerm,
8685
TermError::Normalize(_) => {
@@ -391,7 +390,6 @@ mod tests {
391390
json!({ "len": 3 }),
392391
),
393392
(Box::new(TermError::EmptyTermText), json!({})),
394-
(Box::new(TermError::InvertedRange), json!({})),
395393
(Box::new(TermError::NoEqualityTerm), json!({})),
396394
(Box::new(TermError::NoOrderTerm), json!({})),
397395
(

‎packages/stack-encrypt/src/dynamic/query.rs‎

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
//! [`Probabilistic::verify`]: crate::op::Probabilistic::verify
6565
//! [`Probabilistic::target_limit`]: crate::op::Probabilistic::target_limit
6666
67-
use std::cmp::Ordering;
6867
use std::fmt;
6968

7069
use stack_kms::DataKeySource;
@@ -77,7 +76,6 @@ use crate::op::{Limit, Stats};
7776
use crate::sem::{match_limit, MatchOptions, QueryTokens, TermError};
7877
use crate::target::IndexSpec;
7978
use crate::{KeysetCipher, Pending};
80-
use vitaminc_ore::OrderTarget;
8179

8280
/// An operator, named at run time: the runtime form of the
8381
/// [`op`](crate::op) module's operator types, as [`IndexSpec`] is of the
@@ -565,28 +563,24 @@ fn not_a_range(field: &str) -> Error {
565563
}
566564

567565
/// The refusals the typed path makes when a query is awaited, before any
568-
/// term exists ([`Supports::check`](crate::op::Supports::check)): a range
569-
/// whose low bound is above its high bound, and match text the index
570-
/// cannot normalize or that cuts no token. The match index's own
571-
/// derivation refuses the last two again; asking here lets a binding
572-
/// refuse at its boundary.
566+
/// term exists ([`Supports::check`](crate::op::Supports::check)): order
567+
/// text the fold cannot take, and match text the index cannot normalize or
568+
/// that cuts no token. Each index's own derivation refuses these again;
569+
/// asking here lets a binding refuse at its boundary. A range whose low
570+
/// bound is above its high bound is not refused: as SQL's `BETWEEN`, it
571+
/// matches nothing.
573572
fn check_input(index: &IndexSpec, input: &Input) -> Result<(), Error> {
574573
let refused = |error: TermError| Error::Cipher(error.into());
575574
match (index, input) {
576-
(_, Input::Pair(lo, hi)) => {
577-
if bound_order(lo, hi).map_err(refused)? == Some(Ordering::Greater) {
578-
return Err(refused(TermError::InvertedRange));
579-
}
580-
}
581575
// Order text the fold cannot take (a code point unassigned in
582576
// Unicode 16) is refused here, before a term or a keyset, as
583577
// derivation would refuse it after one.
584-
(IndexSpec::Ore | IndexSpec::Ope, Input::One(FfiValue::String(text))) => {
585-
if let Some(text) = utf8(text) {
586-
crate::sem::order_cmp_as(OrderTarget::Text, text, text)
587-
.map(drop)
588-
.map_err(refused)?;
589-
}
578+
(IndexSpec::Ore | IndexSpec::Ope, Input::One(value)) => {
579+
order_text(value).map_err(refused)?;
580+
}
581+
(IndexSpec::Ore | IndexSpec::Ope, Input::Pair(lo, hi)) => {
582+
order_text(lo).map_err(refused)?;
583+
order_text(hi).map_err(refused)?;
590584
}
591585
(IndexSpec::Match(options), Input::One(value)) => {
592586
query_tokens(value, options).map(drop)?;
@@ -602,28 +596,18 @@ fn check_input(index: &IndexSpec, input: &Input) -> Result<(), Error> {
602596
Ok(())
603597
}
604598

605-
/// The order of a range's two bounds, as the server compares their terms:
606-
/// integers of one kind by value, text in cllw-ore's order of its encoding
607-
/// (the system ADR-0002's fold), so `("b", "A")` is inverted and
608-
/// `("A", "a")` is one value. `None` for anything else.
609-
fn bound_order(lo: &FfiValue, hi: &FfiValue) -> Result<Option<Ordering>, TermError> {
610-
if let (FfiValue::String(lo), FfiValue::String(hi)) = (lo, hi) {
611-
let (Some(lo), Some(hi)) = (utf8(lo), utf8(hi)) else {
612-
return Ok(None);
613-
};
614-
return crate::sem::order_cmp_as(OrderTarget::Text, lo, hi).map(Some);
615-
}
616-
Ok(integer_order(lo, hi))
617-
}
618-
619-
/// The order of two integers of one kind, or `None` for anything else.
620-
fn integer_order(lo: &FfiValue, hi: &FfiValue) -> Option<Ordering> {
621-
match (lo, hi) {
622-
(FfiValue::Int32(lo), FfiValue::Int32(hi)) => Some(lo.cmp(hi)),
623-
(FfiValue::Int64(lo), FfiValue::Int64(hi)) => Some(lo.cmp(hi)),
624-
(FfiValue::UInt32(lo), FfiValue::UInt32(hi)) => Some(lo.cmp(hi)),
625-
(FfiValue::UInt64(lo), FfiValue::UInt64(hi)) => Some(lo.cmp(hi)),
626-
_ => None,
599+
/// Whether `value`, if it is order text, has a fold: `vitaminc-ore`'s
600+
/// `Text` encoding, the one an order index encrypts, refuses a code point
601+
/// Unicode 16 does not assign. Anything but text has nothing to refuse.
602+
fn order_text(value: &FfiValue) -> Result<(), TermError> {
603+
match value {
604+
FfiValue::String(text) => match utf8(text) {
605+
Some(text) => vitaminc_ore::text(text)
606+
.map(drop)
607+
.map_err(TermError::Normalize),
608+
None => Ok(()),
609+
},
610+
_ => Ok(()),
627611
}
628612
}
629613

@@ -720,10 +704,11 @@ pub fn check_query(
720704
/// field does not answer ([`Reason::OperatorNotAnswered`]), an input that
721705
/// cannot be read as the field's kind ([`Reason::FieldType`]) and a
722706
/// `between` input that is not two values ([`Reason::NotARange`]).
723-
/// [`Error::Cipher`] for the query refusals the typed path makes: a range
724-
/// whose low bound is above its high ([`TermError::InvertedRange`]), and
725-
/// match text that cuts no token ([`TermError::EmptyTermText`]) or that
726-
/// the index cannot normalize ([`TermError::Normalize`]).
707+
/// [`Error::Cipher`] for the query refusals the typed path makes: match
708+
/// text that cuts no token ([`TermError::EmptyTermText`]), and text the
709+
/// index cannot normalize or fold ([`TermError::Normalize`]). A range
710+
/// whose low bound is above its high bound is not refused: as SQL's
711+
/// `BETWEEN`, it matches nothing.
727712
/// [`Error::Target`] for what the resolver refuses.
728713
pub fn query_term<'a, K>(
729714
cipher: &'a KeysetCipher<'_, K>,
@@ -1396,13 +1381,8 @@ mod tests {
13961381
async fn the_typed_paths_query_refusals_are_made_before_any_term() {
13971382
let cipher = cipher().await;
13981383
let keyset = cipher.default_keyset();
1399-
let pair = |lo, hi| FfiValue::Array(vec![FfiValue::UInt32(lo), FfiValue::UInt32(hi)]);
14001384
let texts = |lo, hi| FfiValue::Array(vec![text(lo), text(hi)]);
14011385
let cases = [
1402-
("age", OpSpec::Between, pair(65, 18), "inverted"),
1403-
// Text bounds compare by their folded order keys: `A` folds
1404-
// to `a`, which is below `b`.
1405-
("nickname", OpSpec::Between, texts("b", "A"), "inverted"),
14061386
("nickname", OpSpec::Lt, text("a\u{0378}"), "normalize"),
14071387
(
14081388
"nickname",
@@ -1421,27 +1401,13 @@ mod tests {
14211401
] {
14221402
let term = term_error(&error);
14231403
let expected = match what {
1424-
"inverted" => matches!(term, Some(TermError::InvertedRange)),
14251404
"empty" => matches!(term, Some(TermError::EmptyTermText)),
14261405
_ => matches!(term, Some(TermError::Normalize(_))),
14271406
};
14281407
assert!(expected, "{what}: {error:?}");
14291408
}
14301409
}
1431-
// A folded tie is one value, so the range is not inverted, and the
1432-
// terms of folded equivalents are one term.
1433-
let tie = texts("A", "a");
1434-
assert!(check_query(
1435-
&users(),
1436-
"nickname",
1437-
OpSpec::Between,
1438-
tie.clone(),
1439-
&NoTargets
1440-
)
1441-
.is_ok());
1442-
assert!(query(&keyset, "nickname", OpSpec::Between, tie)
1443-
.await
1444-
.is_ok());
1410+
// The terms of folded equivalents are one term.
14451411
let cafe = query(&keyset, "nickname", OpSpec::Lt, text("cafe"))
14461412
.await
14471413
.unwrap();
@@ -1829,27 +1795,25 @@ mod tests {
18291795
}
18301796
}
18311797

1832-
/// A range is refused the wrong way round for every integer kind a
1833-
/// field can declare, and two values of different kinds have no order
1834-
/// here: the input was read as one kind first.
1835-
#[test]
1836-
fn a_range_is_ordered_within_each_integer_kind() {
1837-
let cases = [
1838-
(FfiValue::Int32(-1), FfiValue::Int32(1)),
1839-
(FfiValue::Int64(-1), FfiValue::Int64(1)),
1840-
(FfiValue::UInt32(1), FfiValue::UInt32(2)),
1841-
(FfiValue::UInt64(1), FfiValue::UInt64(2)),
1842-
];
1843-
for (lo, hi) in cases {
1844-
assert_eq!(integer_order(&lo, &hi), Some(Ordering::Less), "{lo:?}");
1845-
assert_eq!(integer_order(&hi, &lo), Some(Ordering::Greater), "{lo:?}");
1846-
assert_eq!(integer_order(&lo, &lo), Some(Ordering::Equal), "{lo:?}");
1798+
/// A range whose low bound is above its high bound is not refused, as
1799+
/// SQL's `BETWEEN` is not: it is checked, and its terms derived, like
1800+
/// any other range, and matches nothing.
1801+
#[tokio::test]
1802+
async fn an_inverted_range_is_a_query_like_any_other() {
1803+
let cipher = cipher().await;
1804+
let keyset = cipher.default_keyset();
1805+
let pair = |lo, hi| FfiValue::Array(vec![FfiValue::UInt32(lo), FfiValue::UInt32(hi)]);
1806+
let texts = |lo, hi| FfiValue::Array(vec![text(lo), text(hi)]);
1807+
for (name, input) in [("age", pair(65, 18)), ("nickname", texts("b", "A"))] {
1808+
assert!(
1809+
check_query(&users(), name, OpSpec::Between, input.clone(), &NoTargets).is_ok(),
1810+
"{name}: checked"
1811+
);
1812+
assert!(
1813+
query(&keyset, name, OpSpec::Between, input).await.is_ok(),
1814+
"{name}: derived"
1815+
);
18471816
}
1848-
assert_eq!(
1849-
integer_order(&FfiValue::UInt32(1), &FfiValue::UInt64(2)),
1850-
None
1851-
);
1852-
assert_eq!(integer_order(&text("a"), &text("b")), None);
18531817
}
18541818

18551819
#[test]

0 commit comments

Comments
 (0)