1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Canonical, order-preserving byte encodings for plaintext types.
//!
//! Each supported type implements [`OrderableBytes`], which maps a
//! value to bytes whose byte-wise lexicographic order agrees with the
//! type's natural total order, and whose byte equality agrees with the
//! type's value equality. The resulting bytes are scheme-agnostic —
//! they're intended for any comparison-as-bytes scheme that wants to
//! preserve plaintext order on ciphertexts (e.g. `ore-rs` BlockORE, an
//! OPE construction, an ordered hash).
//!
//! Every type also implements exactly one of two subtraits, which say
//! how its bytes may be handled:
//!
//! - [`FixedOrderableBytes`]: every value encodes to the same length,
//! [`ENCODED_LEN`](FixedOrderableBytes::ENCODED_LEN), available as an
//! owned array. Numbers, `bool`, `char`, byte arrays `[u8; N]`,
//! `Decimal` and the `chrono` types.
//! - [`VariableOrderableBytes`]: the length varies with the value, and
//! the bytes are borrowed from it. Strings and byte strings (see
//! [`variable`]).
//!
//! A reference `&T` implements the same traits as `T`.
//!
//! | Need | Bound | Call |
//! |---|---|---|
//! | Pad, store or return fixed-length bytes | `T: FixedOrderableBytes` | [`to_fixed_orderable_bytes`](FixedOrderableBytes::to_fixed_orderable_bytes) (owned) |
//! | Bytes of either kind, used immediately | `T: OrderableBytes` | [`to_orderable_bytes`](OrderableBytes::to_orderable_bytes) (may borrow) |
//! | Strings or byte strings specifically | `T: VariableOrderableBytes` | [`to_orderable_bytes`](OrderableBytes::to_orderable_bytes) (borrowed) |
//!
//! Padding to a block size is only safe for [`FixedOrderableBytes`];
//! bound on [`OrderableBytes`] only when you handle both kinds correctly.
//!
//! The traits are sealed: only this crate implements them, so every
//! encoding is pinned by its golden vectors, and the rules above hold
//! for every type that implements them.
//!
//! Encoders are gated behind per-type feature flags so callers only pay
//! for the dependencies they actually use.
/// Implements [`OrderableBytes`] and [`FixedOrderableBytes`] for a type
/// whose encoding is always `$len` bytes, from one body that builds the
/// array. Writing both impls from one length keeps
/// [`FixedOrderableBytes::ENCODED_LEN`], [`FixedOrderableBytes::Array`]
/// and [`OrderableBytes::Bytes`] in agreement.
extern crate quickcheck;
// Runs the README's Rust examples as doctests, so the README cannot drift
// from the API. The examples use no optional feature, so they run under
// a plain `cargo test` as well as with `--all-features`.
;
/// Maps a value to its canonical, order-preserving byte encoding.
///
/// Implementors guarantee, for any `a` and `b` of the implementing type:
///
/// - **Equality:** byte equality of the outputs agrees with the type's
/// value equality (`a.to_orderable_bytes() == b.to_orderable_bytes()`
/// iff `a == b`).
/// - **Order:** byte-wise lexicographic comparison of the outputs agrees
/// with the type's natural total order
/// (`a.to_orderable_bytes() <= b.to_orderable_bytes()` iff `a <= b`).
///
/// Every implementor also implements exactly one of
/// [`FixedOrderableBytes`] or [`VariableOrderableBytes`]. Code that
/// needs a fixed length, such as code that pads to a block size, must
/// bound on [`FixedOrderableBytes`], not on this trait.
///
/// The trait is sealed, so types outside this crate can't implement it:
///
/// ```compile_fail,E0277
/// use orderable_bytes::OrderableBytes;
///
/// struct Mine(u32);
///
/// impl OrderableBytes for Mine {
/// type Bytes<'a> = [u8; 4];
/// fn to_orderable_bytes(&self) -> [u8; 4] {
/// self.0.to_be_bytes()
/// }
/// }
/// ```
///
/// In 0.1 this trait was called `ToOrderableBytes` and was fixed-length
/// only. It was renamed so that 0.1 code bounded on it fails to compile
/// rather than silently accepting variable-length types: migrate such
/// bounds to [`FixedOrderableBytes`].
/// An [`OrderableBytes`] encoding whose length is the same for every
/// value of the type.
///
/// [`to_fixed_orderable_bytes`](Self::to_fixed_orderable_bytes) returns
/// the same bytes as
/// [`to_orderable_bytes`](OrderableBytes::to_orderable_bytes), as an
/// owned [`Array`](Self::Array) of exactly
/// [`ENCODED_LEN`](Self::ENCODED_LEN) bytes that doesn't borrow from the
/// value. Generic code can store or return it:
///
/// ```
/// use orderable_bytes::FixedOrderableBytes;
///
/// fn term<T: FixedOrderableBytes>(value: T) -> T::Array {
/// value.to_fixed_orderable_bytes()
/// }
///
/// assert_eq!(term(7u32), [0, 0, 0, 7]);
/// ```
///
/// Fixed-length encodings may be zero-extended to a larger block, since
/// every value is extended the same way. Bounding on this trait is what
/// keeps variable-length types out of such code:
///
/// ```
/// use orderable_bytes::FixedOrderableBytes;
///
/// fn pad_to_16<T: FixedOrderableBytes>(value: &T) -> [u8; 16] {
/// let mut block = [0u8; 16];
/// block[..T::ENCODED_LEN].copy_from_slice(value.to_fixed_orderable_bytes().as_ref());
/// block
/// }
///
/// assert!(pad_to_16(&1u32) < pad_to_16(&2u32));
/// ```
///
/// ```compile_fail,E0277
/// # use orderable_bytes::FixedOrderableBytes;
/// # fn pad_to_16<T: FixedOrderableBytes>(value: &T) -> [u8; 16] {
/// # let mut block = [0u8; 16];
/// # block[..T::ENCODED_LEN].copy_from_slice(value.to_fixed_orderable_bytes().as_ref());
/// # block
/// # }
/// // Strings are variable-length, so padding them is rejected.
/// pad_to_16(&String::from("a"));
/// ```
/// An [`OrderableBytes`] encoding whose length varies with the value.
///
/// A value sorts before any longer value it is a prefix of. The bytes do
/// not mark where they end, so they must never be padded or
/// concatenated: see [`variable`] for why, and for the implementations.
/// A reference encodes exactly as the value it points to.