vitaminc_normalize/scalar.rs
1//! One normalized equality term per scalar.
2//!
3//! [`normalize`] turns a physical value and its optional logical type into
4//! the bytes an equality index derives under, and the frozen domain label
5//! those bytes belong to. The encoders are public, one per kind, so an index
6//! layer that already knows a value's kind can call them directly.
7//!
8//! Every integer width shares one 17-byte encoding and domain, and both
9//! float widths share one 8-byte encoding and domain, so equal numbers
10//! match whichever width wrote them. Text is Unicode 16 NFC. These bytes
11//! and their [`domain`] labels are protocol data: changing either requires
12//! a new domain. Text processing is variable-time; this module makes no
13//! constant-time claim for Unicode normalization.
14//!
15//! Logical types opt in. A date, a timestamp, a decimal and a uuid each
16//! have a domain of their own, separate from the physical value they are
17//! stored as, so a date's day count never matches the integer it equals and
18//! a uuid never matches the same sixteen bytes written as bytes. Every other
19//! logical type, [`Logical::Unknown`] included, has no term and is refused
20//! with [`NormalizeError::NoDomain`].
21
22use unicode_general_category::{get_general_category, GeneralCategory};
23use unicode_normalization::UnicodeNormalization;
24use vitaminc_aead::plaintext::kinds::{timestamp_nanos, MAX_DATE_DAYS, MIN_DATE_DAYS};
25use vitaminc_aead::{Float, Int, Logical, Physical, TimeUnit};
26use vitaminc_protected::{Controlled, OpaqueDebug, Protected};
27
28/// Frozen domain labels for equality terms, one per kind that has one.
29pub mod domain {
30 /// Every integer width, `i8` through `u128`.
31 pub const INTEGER: &str = "vitaminc/prf/value/integer-orderable/v1";
32 /// `f32` and `f64`, as `f64`.
33 pub const FLOAT: &str = "vitaminc/prf/value/float-orderable/v1";
34 /// A date, over its day count.
35 pub const DATE: &str = "vitaminc/prf/value/date-orderable/v1";
36 /// A timestamp, truncated to microseconds.
37 pub const TIMESTAMP: &str = "vitaminc/prf/value/timestamp-micros-orderable/v1";
38 /// A decimal, with scale and signed zero normalized.
39 pub const DECIMAL: &str = "vitaminc/prf/value/decimal-orderable/v1";
40 /// Text, as Unicode 16 NFC.
41 pub const TEXT: &str = "vitaminc/prf/value/text-nfc/unicode-16/v1";
42 /// Bytes.
43 pub const BYTES: &str = "vitaminc/prf/value/bytes/v1";
44 /// A uuid, over its sixteen raw bytes.
45 pub const UUID: &str = "vitaminc/prf/value/uuid/v1";
46 /// Every domain, for a test or an index layer that enumerates them. A
47 /// slice, so a domain added later does not change its type.
48 pub const ALL: &[&str] = &[INTEGER, FLOAT, DATE, TIMESTAMP, DECIMAL, TEXT, BYTES, UUID];
49}
50
51/// The normalized bytes for one equality term and the domain they belong
52/// to.
53///
54/// The fields are public so an index layer can build a term of its own,
55/// and whatever consumes a `Normalized` trusts them: a PRF derives under
56/// the `domain` it is handed. The only way to get the frozen encodings is
57/// through [`normalize`] or the per-kind encoders here.
58///
59/// `Debug` shows the domain and masks the bytes.
60#[derive(OpaqueDebug)]
61pub struct Normalized {
62 /// The frozen domain label, one of the [`domain`] constants.
63 #[non_sensitive]
64 pub domain: &'static str,
65 /// The normalized plaintext, protected.
66 pub bytes: Protected<Vec<u8>>,
67}
68
69/// Why a scalar has no term. Never includes plaintext.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
71#[non_exhaustive]
72pub enum NormalizeError {
73 /// The scalar has no domain under the normalizer: a null, an
74 /// `undefined`, a boolean, or a logical type outside its opt-in table.
75 /// The message does not name an index, because the equality and order
76 /// normalizers both report it; whatever wraps it says which.
77 #[error("no domain for this kind of value")]
78 NoDomain,
79 /// The value is not valid for its logical type.
80 #[error("the value is not valid for its logical type")]
81 Invalid,
82 /// Text contains a code point unassigned in Unicode 16.
83 #[error("text contains a code point unassigned in Unicode 16")]
84 UnassignedCodePoint,
85 /// The value is of another kind than the type the index declares: text
86 /// for an integer column, say. Not reported by the equality
87 /// normalizer, which declares no type.
88 #[error("the value is not of the kind the index declares")]
89 Mismatch,
90 /// The value is of the kind the index declares but does not fit it: an
91 /// integer outside the declared width. Not reported by the equality
92 /// normalizer, which declares no type.
93 #[error("the value does not fit the type the index declares")]
94 OutOfRange,
95}
96
97/// The opt-in table: the domain for a scalar, or `None` for one that has
98/// no term. A logical type decides on its own; a value without one goes by
99/// its kind. Neither looks at the value: [`normalize`] checks that.
100pub fn domain_of(ty: Option<Logical>, v: &Physical<'_>) -> Option<&'static str> {
101 match (ty, v) {
102 (None, Physical::Int(_)) => Some(domain::INTEGER),
103 (None, Physical::Float(_)) => Some(domain::FLOAT),
104 (None, Physical::Text(_)) => Some(domain::TEXT),
105 (None, Physical::Bytes(_)) => Some(domain::BYTES),
106 (None, Physical::Null | Physical::Bool(_)) => None,
107 (Some(Logical::Date), _) => Some(domain::DATE),
108 (Some(Logical::Timestamp(_)), _) => Some(domain::TIMESTAMP),
109 (Some(Logical::Decimal { .. }), _) => Some(domain::DECIMAL),
110 (Some(Logical::Uuid), _) => Some(domain::UUID),
111 // `Logical` is non-exhaustive: a type this build does not know, and
112 // any added later, has no term until it is given a domain here.
113 (Some(_), _) => None,
114 }
115}
116
117/// Encode one scalar for equality, with the domain its term belongs to.
118///
119/// Integers of every width share [`domain::INTEGER`]: a sign byte (`0x00`
120/// negative, `0x01` otherwise) then the value as a big-endian 128-bit
121/// two's-complement word, which orders correctly across `i128::MIN` to
122/// `u128::MAX`. Both float widths share [`domain::FLOAT`]: an `f32` widens
123/// exactly to `f64`, so it matches an `f64` only when the numbers are equal
124/// (`f32` 1.5 matches, `f32` 0.1 does not match `f64` 0.1). Float zero
125/// signs and NaN payloads are folded. A timestamp in any unit is truncated
126/// to microseconds. A decimal's scale is normalized, so `1.50` and `1.5`
127/// are one term, as are `-0.00` and `0`.
128pub fn normalize(ty: Option<Logical>, v: Physical<'_>) -> Result<Normalized, NormalizeError> {
129 // A value that does not fit its logical type is refused as invalid
130 // before the type is looked up, so `undefined` over an int is `Invalid`
131 // rather than "nothing to index".
132 if ty.is_some_and(|ty| !ty.admits(&v)) {
133 return Err(NormalizeError::Invalid);
134 }
135 let domain = domain_of(ty, &v).ok_or(NormalizeError::NoDomain)?;
136 let bytes = match (ty, v) {
137 (None, Physical::Int(i)) => Some(integer(i)),
138 (None, Physical::Float(f)) => Some(float(f)),
139 (None, Physical::Text(s)) => Some(text(s)?),
140 (None, Physical::Bytes(b)) => Some(bytes(b)),
141 (Some(Logical::Date), Physical::Int(days)) => date(days),
142 (Some(Logical::Timestamp(unit)), Physical::Int(count)) => timestamp(unit, count),
143 (Some(Logical::Decimal { scale }), Physical::Int(mantissa)) => decimal(scale, mantissa),
144 (Some(Logical::Uuid), Physical::Bytes(b)) => uuid(b),
145 // `admits` has already refused a logical type over the wrong kind,
146 // and `domain_of` everything without a term. Each encoder checks
147 // its own range as well, for a caller that goes to it directly.
148 _ => None,
149 }
150 .ok_or(NormalizeError::Invalid)?;
151 Ok(Normalized { domain, bytes })
152}
153
154/// An integer of any width: a sign byte, then the big-endian 128-bit word.
155pub fn integer(value: Int) -> Protected<Vec<u8>> {
156 let (negative, word) = value.sign_and_word();
157 fixed::<17>(|b| {
158 b[0] = u8::from(!negative);
159 b[1..].copy_from_slice(&word.to_be_bytes());
160 })
161}
162
163/// A float of either width, as the orderable bytes of its `f64` value with
164/// zero signs and NaN payloads folded.
165pub fn float(value: Float) -> Protected<Vec<u8>> {
166 let value = match value {
167 Float::F32(f) => widen_f32(f),
168 Float::F64(f) => f,
169 };
170 orderable_f64(canonical_f64(value))
171}
172
173/// A date's day count since 1970-01-01, as the orderable bytes of its
174/// count of days from 0001-01-01 (the same bytes chrono's `NaiveDate`
175/// orders by). `None` outside the date range.
176pub fn date(days: Int) -> Option<Protected<Vec<u8>>> {
177 let days = i32::try_from(days.to_i128()?).ok()?;
178 if !(MIN_DATE_DAYS..=MAX_DATE_DAYS).contains(&days) {
179 return None;
180 }
181 let from_ce = days.checked_add(EPOCH_DAYS_FROM_CE)?;
182 Some(fixed(|b| {
183 *b = ((from_ce as u32) ^ (1u32 << 31)).to_be_bytes()
184 }))
185}
186
187/// A count of `unit`s since the Unix epoch, as orderable seconds then
188/// big-endian nanoseconds truncated to microseconds (the same bytes
189/// chrono's `DateTime<Utc>` orders by, truncated). `None` outside the
190/// timestamp range.
191pub fn timestamp(unit: TimeUnit, count: Int) -> Option<Protected<Vec<u8>>> {
192 let nanos = timestamp_nanos(unit, count)?;
193 let seconds = i64::try_from(nanos.div_euclid(NANOS_PER_SECOND)).ok()?;
194 let sub = (nanos.rem_euclid(NANOS_PER_SECOND) as u32) / 1_000 * 1_000;
195 Some(fixed::<12>(|b| {
196 b[..8].copy_from_slice(&((seconds as u64) ^ (1u64 << 63)).to_be_bytes());
197 b[8..].copy_from_slice(&sub.to_be_bytes());
198 }))
199}
200
201/// A decimal's mantissa and scale, as the 14-byte orderable encoding of
202/// its value, ported from the frozen `orderable-bytes` release and checked
203/// against it, which normalizes the scale and a signed zero. `None`
204/// if the mantissa does not fit 96 bits or the scale is above 28, the
205/// range `rust_decimal` represents.
206///
207/// # Encoding (scientific form, base 10)
208///
209/// Each non-zero value is re-expressed in signed scientific form:
210///
211/// ```text
212/// value = ±significand × 10^(leading_exp − digits(significand) + 1)
213/// ```
214///
215/// where `significand` is the mantissa with trailing zeros stripped and
216/// `leading_exp` is the decimal exponent of the leading significant digit,
217/// in `[-28, 28]`. The 14 bytes are:
218///
219/// ```text
220/// byte 0 : [sign:1][biased_exp:7]
221/// bytes 1..=13 : padded_significand (104 bits, big-endian)
222/// ```
223///
224/// - The sign bit is `1` for positive and zero, `0` for negative, so every
225/// negative sorts below zero and zero below every positive.
226/// - The biased exponent is `leading_exp + 64`, in `[36, 92]` for a
227/// non-zero value. Zero uses `0`, which no non-zero value reaches.
228/// - The padded significand is the significand left-justified to 29
229/// digits by appending zeros, so values at one exponent compare
230/// byte-wise however many digits they have. A 96-bit mantissa has at
231/// most 29 digits, so the padded value fits 104 bits.
232///
233/// For a negative, the exponent bits and the significand are inverted, so
234/// a larger magnitude sorts lower. `1`, `1.0` and `1.00` reduce to the
235/// same significand and exponent, and `+0` and `-0` to the same zero, so
236/// equal values are equal bytes, as `rust_decimal` compares them.
237pub fn decimal(scale: u8, mantissa: Int) -> Option<Protected<Vec<u8>>> {
238 crate::decimal::orderable(mantissa.to_i128()?, scale)
239}
240
241/// NFC in the pinned Unicode version, without case or accent folding.
242///
243/// The result is written into one protected buffer sized exactly up front,
244/// so it never reallocates and frees an unwiped partial copy.
245///
246/// The `unicode-normalization` iterators keep pending code points in small
247/// buffers of their own, with room for four code points inside the
248/// iterator. Decomposition holds the combining marks that follow a
249/// character in the same buffer as the decomposed form of the next
250/// character, so the buffer moves to the heap as soon as those together
251/// pass four code points: four marks followed by any character is enough.
252/// Composition moves its buffer to the heap when five marks after one
253/// character do not combine with it. Once a buffer is on the heap it stays
254/// there for the rest of the string, so every later character passes
255/// through it, and it is freed without being wiped. That is out of this
256/// crate's control (see #488).
257pub fn text(text: &str) -> Result<Protected<Vec<u8>>, NormalizeError> {
258 if text
259 .chars()
260 .any(|c| get_general_category(c) == GeneralCategory::Unassigned)
261 {
262 return Err(NormalizeError::UnassignedCodePoint);
263 }
264 // NFC can grow UTF-8 (U+0344 is two bytes, its NFC form four), so measure
265 // the output first rather than sizing from the input.
266 let len = text.nfc().map(char::len_utf8).sum();
267 let mut result = Protected::new(Vec::with_capacity(len));
268 for c in text.nfc() {
269 let mut encoded = Protected::new([0u8; 4]);
270 result
271 .risky_inner_mut()
272 .extend_from_slice(c.encode_utf8(encoded.risky_inner_mut()).as_bytes());
273 }
274 Ok(result)
275}
276
277/// Bytes, as they are.
278pub fn bytes(value: &[u8]) -> Protected<Vec<u8>> {
279 Protected::new(value.to_vec())
280}
281
282/// A uuid's sixteen raw bytes. `None` for any other length.
283pub fn uuid(value: &[u8]) -> Option<Protected<Vec<u8>>> {
284 (value.len() == 16).then(|| Protected::new(value.to_vec()))
285}
286
287/// A fixed-width encoding, written by `write` straight into the protected
288/// vector that is returned.
289///
290/// The vector is allocated once at its final size, so it never
291/// reallocates, and `write` fills it in place. No separate array of the
292/// encoding is made and copied, so there is no extra copy to wipe. The
293/// compiler can still make short-lived temporaries while `write` runs, for
294/// example the array that `to_be_bytes` returns, and Rust cannot promise
295/// to wipe those.
296///
297/// `vitaminc-ore` has its own private copy of this function rather than
298/// sharing this one, so it stays out of this crate's public API.
299pub(crate) fn fixed<const N: usize>(mut write: impl FnMut(&mut [u8; N])) -> Protected<Vec<u8>> {
300 let mut bytes = Protected::new(vec![0u8; N]);
301 bytes.update(|bytes| {
302 let array = <&mut [u8; N]>::try_from(bytes.as_mut_slice())
303 .expect("the vector was made with exactly N bytes");
304 write(array);
305 });
306 bytes
307}
308
309/// Days from 0001-01-01 (day 1, chrono's "days from CE") to 1970-01-01.
310const EPOCH_DAYS_FROM_CE: i32 = 719_163;
311const NANOS_PER_SECOND: i128 = 1_000_000_000;
312
313/// The orderable bytes of an `f64`: negatives flip every bit, positives
314/// the sign bit, so the bytes sort as the numbers do. Expects a value
315/// [`canonical_f64`] has already folded.
316///
317/// The bytes are written straight into the protected vector that is
318/// returned (see [`fixed`]).
319fn orderable_f64(value: f64) -> Protected<Vec<u8>> {
320 let bits = value.to_bits();
321 let sign_extension = ((bits as i64) >> 63) as u64;
322 fixed(|b| *b = (bits ^ (sign_extension | (1u64 << 63))).to_be_bytes())
323}
324
325/// `f64::from(f32)` in integer operations only. A hardware conversion can
326/// take a slow path on subnormal inputs, which would leak timing; this keeps
327/// the float path free of floating-point arithmetic.
328fn widen_f32(value: f32) -> f64 {
329 let bits = value.to_bits();
330 let sign = u64::from(bits >> 31) << 63;
331 let exponent = (bits >> 23) & 0xff;
332 let mantissa = u64::from(bits & 0x7f_ffff);
333 let zero_exponent = 0u64.wrapping_sub((exponent == 0) as u64);
334 let max_exponent = 0u64.wrapping_sub((exponent == 0xff) as u64);
335 let nonzero = 0u64.wrapping_sub((mantissa != 0) as u64);
336 // Normal, infinite and NaN: rebias the exponent (127 to 1023, or all
337 // ones stays all ones) and left-align the mantissa.
338 let rebiased = ((u64::from(exponent) + 896) & !max_exponent) | (0x7ff & max_exponent);
339 let normal = (rebiased << 52) | (mantissa << 29);
340 // Subnormal: the highest set bit becomes the implicit leading one.
341 let top = highest_set_bit(mantissa);
342 let subnormal = ((top + 874) << 52) | ((mantissa << (52 - top)) & ((1 << 52) - 1));
343 f64::from_bits(sign | (normal & !zero_exponent) | (subnormal & zero_exponent & nonzero))
344}
345
346/// Index of the highest set bit of a subnormal f32 mantissa. `| 1` keeps the
347/// result in range for a zero mantissa, which `widen_f32` masks out.
348///
349/// Kept out of `widen_f32` so the `.cargo/mutants.toml` exclusion of that
350/// function's equivalent `|`-to-`^` mutants cannot reach this `|`, whose
351/// operands overlap and whose mutant a test does catch.
352fn highest_set_bit(mantissa: u64) -> u64 {
353 u64::from(63 - (mantissa | 1).leading_zeros())
354}
355
356/// Folds every NaN to one quiet NaN and `-0.0` to `0.0`, in integer
357/// operations only.
358fn canonical_f64(value: f64) -> f64 {
359 let bits = value.to_bits();
360 let magnitude = bits & 0x7fff_ffff_ffff_ffff;
361 let nan = 0u64.wrapping_sub((magnitude > 0x7ff0_0000_0000_0000) as u64);
362 let nonzero = 0u64.wrapping_sub((magnitude != 0) as u64);
363 f64::from_bits((bits & !nan).wrapping_add(0x7ff8_0000_0000_0000 & nan) & nonzero)
364}
365
366#[cfg(test)]
367#[path = "scalar_tests.rs"]
368mod tests;