postcard_rpc/
hash.rs

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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! URI and Schema Hashing
//!
//! We use `FNV1a` hashes with a digest size of 64 bits to represent dispatch keys.
//!
//! Unfortunately. using [core::hash::Hash] seems to not produce consistent results,
//! which [was noted] in the docs. To overcome this, we implement a custom method for
//! hashing the postcard [Schema].
//!
//! [was noted]: https://doc.rust-lang.org/stable/std/hash/trait.Hash.html#portability

use postcard_schema::{
    schema::{DataModelType, NamedType, NamedValue, NamedVariant},
    Schema,
};

/// A const compatible Fnv1a64 hasher
pub struct Fnv1a64Hasher {
    state: u64,
}

impl Fnv1a64Hasher {
    // source: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
    const BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const PRIME: u64 = 0x0000_0100_0000_01b3;

    /// Create a new hasher with the default basis as state contents
    pub fn new() -> Self {
        Self { state: Self::BASIS }
    }

    /// Calculate the hash for each of the given data bytes
    pub fn update(&mut self, data: &[u8]) {
        for b in data {
            let ext = u64::from(*b);
            self.state ^= ext;
            self.state = self.state.wrapping_mul(Self::PRIME);
        }
    }

    /// Extract the current state for finalizing the hash
    pub fn digest(self) -> u64 {
        self.state
    }

    /// Same as digest but as bytes
    pub fn digest_bytes(self) -> [u8; 8] {
        self.digest().to_le_bytes()
    }
}

impl Default for Fnv1a64Hasher {
    fn default() -> Self {
        Self::new()
    }
}

pub mod fnv1a64 {
    //! Const and no-std helper methods and types for perfoming hash calculation
    use postcard_schema::schema::DataModelVariant;

    use super::*;

    /// Calculate the Key hash for the given path and type T
    pub const fn hash_ty_path<T: Schema + ?Sized>(path: &str) -> [u8; 8] {
        let schema = T::SCHEMA;
        let state = hash_update_str(Fnv1a64Hasher::BASIS, path);
        hash_named_type(state, schema).to_le_bytes()
    }

    pub(crate) const fn hash_update(mut state: u64, bytes: &[u8]) -> u64 {
        let mut idx = 0;
        while idx < bytes.len() {
            let ext = bytes[idx] as u64;
            state ^= ext;
            state = state.wrapping_mul(Fnv1a64Hasher::PRIME);
            idx += 1;
        }
        state
    }

    pub(crate) const fn hash_update_str(state: u64, s: &str) -> u64 {
        hash_update(state, s.as_bytes())
    }

    const fn hash_sdm_type(state: u64, sdmty: &'static DataModelType) -> u64 {
        // The actual values we use here don't matter that much (as far as I know),
        // as long as the values for each variant are unique. I am unsure of the
        // implications of doing a TON of single byte calls to `update`, it may be
        // worth doing some buffering, and only calling update every 4/8/16 bytes
        // instead, if performance is a concern.
        //
        // As of initial implementation, I'm mostly concerned with "does it work",
        // as hashing is typically only done on startup.
        //
        // Using all primes that fit into a single byte:
        //
        // all_primes = [
        //     0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11, 0x13,
        //     0x17, 0x1D, 0x1F, 0x25, 0x29, 0x2B, 0x2F, 0x35,
        //     0x3B, 0x3D, 0x43, 0x47, 0x49, 0x4F, 0x53, 0x59,
        //     0x61, 0x65, 0x67, 0x6B, 0x6D, 0x71, 0x7F, 0x83,
        //     0x89, 0x8B, 0x95, 0x97, 0x9D, 0xA3, 0xA7, 0xAD,
        //     0xB3, 0xB5, 0xBF, 0xC1, 0xC5, 0xC7, 0xD3, 0xDF,
        //     0xE3, 0xE5, 0xE9, 0xEF, 0xF1, 0xFB,
        // ];
        // shuffled_primes = [
        //     0x11, 0xC5, 0x3D, 0x95, 0x1D, 0x0D, 0x0B, 0x02,
        //     0x83, 0xD3, 0x13, 0x8B, 0x6B, 0xAD, 0xEF, 0x71,
        //     0xC1, 0x25, 0x65, 0x6D, 0x47, 0xBF, 0xB5, 0x9D,
        //     0xDF, 0x03, 0xA7, 0x05, 0xC7, 0x4F, 0x7F, 0x67,
        //     0xE9, 0xB3, 0xE5, 0x2B, 0x97, 0xFB, 0x61, 0x3B,
        //     0x1F, 0xA3, 0x35, 0x43, 0x89, 0x49, 0xE3, 0x07,
        //     0x53, 0xF1, 0x17, 0x2F, 0x29, 0x59,
        // ];
        match sdmty {
            DataModelType::Bool => hash_update(state, &[0x11]),
            DataModelType::I8 => hash_update(state, &[0xC5]),
            DataModelType::U8 => hash_update(state, &[0x3D]),
            DataModelType::I16 => hash_update(state, &[0x1D]),
            DataModelType::I32 => hash_update(state, &[0x0D]),
            DataModelType::I64 => hash_update(state, &[0x0B]),
            DataModelType::I128 => hash_update(state, &[0x02]),
            DataModelType::U16 => hash_update(state, &[0x83]),
            DataModelType::U32 => hash_update(state, &[0xD3]),
            DataModelType::U64 => hash_update(state, &[0x13]),
            DataModelType::U128 => hash_update(state, &[0x8B]),
            DataModelType::Usize => hash_update(state, &[0x6B]),
            DataModelType::Isize => hash_update(state, &[0xAD]),
            DataModelType::F32 => hash_update(state, &[0xEF]),
            DataModelType::F64 => hash_update(state, &[0x71]),
            DataModelType::Char => hash_update(state, &[0xC1]),
            DataModelType::String => hash_update(state, &[0x25]),
            DataModelType::ByteArray => hash_update(state, &[0x65]),
            DataModelType::Option(nt) => {
                let state = hash_update(state, &[0x6D]);
                hash_named_type(state, nt)
            }
            DataModelType::Unit => hash_update(state, &[0x47]),
            DataModelType::UnitStruct => hash_update(state, &[0xBF]),
            DataModelType::NewtypeStruct(nt) => {
                let state = hash_update(state, &[0x9D]);
                hash_named_type(state, nt)
            }
            DataModelType::Seq(nt) => {
                let state = hash_update(state, &[0x03]);
                hash_named_type(state, nt)
            }
            DataModelType::Tuple(nts) => {
                let mut state = hash_update(state, &[0xA7]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type(state, nts[idx]);
                    idx += 1;
                }
                state
            }
            DataModelType::TupleStruct(nts) => {
                let mut state = hash_update(state, &[0x05]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type(state, nts[idx]);
                    idx += 1;
                }
                state
            }
            DataModelType::Map { key, val } => {
                let state = hash_update(state, &[0x4F]);
                let state = hash_named_type(state, key);
                hash_named_type(state, val)
            }
            DataModelType::Struct(nvs) => {
                let mut state = hash_update(state, &[0x7F]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_value(state, nvs[idx]);
                    idx += 1;
                }
                state
            }
            DataModelType::Enum(nvs) => {
                let mut state = hash_update(state, &[0xE9]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_variant(state, nvs[idx]);
                    idx += 1;
                }
                state
            }
            DataModelType::Schema => hash_update(state, &[0xB3]),
        }
    }

    const fn hash_named_type(state: u64, nt: &NamedType) -> u64 {
        // NOTE: We do *not* hash the name of the type in hashv2. This
        // is to allow "safe" type punning, e.g. treating `Vec<u8>` and
        // `&[u8]` as compatible types, when talking between std and no-std
        // targets
        //
        // let state = hash_update(state, nt.name.as_bytes());
        hash_sdm_type(state, nt.ty)
    }

    const fn hash_named_variant(state: u64, nt: &NamedVariant) -> u64 {
        let state = hash_update(state, nt.name.as_bytes());
        match nt.ty {
            DataModelVariant::UnitVariant => hash_update(state, &[0xB5]),
            DataModelVariant::NewtypeVariant(nt) => {
                let state = hash_update(state, &[0xDF]);
                hash_named_type(state, nt)
            }
            DataModelVariant::TupleVariant(nts) => {
                let mut state = hash_update(state, &[0xC7]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type(state, nts[idx]);
                    idx += 1;
                }
                state
            }
            DataModelVariant::StructVariant(nvs) => {
                let mut state = hash_update(state, &[0x67]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_value(state, nvs[idx]);
                    idx += 1;
                }
                state
            }
        }
    }

    const fn hash_named_value(state: u64, nt: &NamedValue) -> u64 {
        let state = hash_update(state, nt.name.as_bytes());
        hash_named_type(state, nt.ty)
    }
}

#[cfg(feature = "use-std")]
pub mod fnv1a64_owned {
    //! Heapful helpers and versions of hashing for use on `std` targets

    use postcard_schema::schema::owned::{
        OwnedDataModelType, OwnedDataModelVariant, OwnedNamedType, OwnedNamedValue,
        OwnedNamedVariant,
    };

    use super::fnv1a64::*;
    use super::*;

    /// Calculate the Key hash for the given path and OwnedNamedType
    pub fn hash_ty_path_owned(path: &str, nt: &OwnedNamedType) -> [u8; 8] {
        let state = hash_update_str(Fnv1a64Hasher::BASIS, path);
        hash_named_type_owned(state, nt).to_le_bytes()
    }

    fn hash_sdm_type_owned(state: u64, sdmty: &OwnedDataModelType) -> u64 {
        // The actual values we use here don't matter that much (as far as I know),
        // as long as the values for each variant are unique. I am unsure of the
        // implications of doing a TON of single byte calls to `update`, it may be
        // worth doing some buffering, and only calling update every 4/8/16 bytes
        // instead, if performance is a concern.
        //
        // As of initial implementation, I'm mostly concerned with "does it work",
        // as hashing is typically only done on startup.
        //
        // Using all primes that fit into a single byte:
        //
        // all_primes = [
        //     0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11, 0x13,
        //     0x17, 0x1D, 0x1F, 0x25, 0x29, 0x2B, 0x2F, 0x35,
        //     0x3B, 0x3D, 0x43, 0x47, 0x49, 0x4F, 0x53, 0x59,
        //     0x61, 0x65, 0x67, 0x6B, 0x6D, 0x71, 0x7F, 0x83,
        //     0x89, 0x8B, 0x95, 0x97, 0x9D, 0xA3, 0xA7, 0xAD,
        //     0xB3, 0xB5, 0xBF, 0xC1, 0xC5, 0xC7, 0xD3, 0xDF,
        //     0xE3, 0xE5, 0xE9, 0xEF, 0xF1, 0xFB,
        // ];
        // shuffled_primes = [
        //     0x11, 0xC5, 0x3D, 0x95, 0x1D, 0x0D, 0x0B, 0x02,
        //     0x83, 0xD3, 0x13, 0x8B, 0x6B, 0xAD, 0xEF, 0x71,
        //     0xC1, 0x25, 0x65, 0x6D, 0x47, 0xBF, 0xB5, 0x9D,
        //     0xDF, 0x03, 0xA7, 0x05, 0xC7, 0x4F, 0x7F, 0x67,
        //     0xE9, 0xB3, 0xE5, 0x2B, 0x97, 0xFB, 0x61, 0x3B,
        //     0x1F, 0xA3, 0x35, 0x43, 0x89, 0x49, 0xE3, 0x07,
        //     0x53, 0xF1, 0x17, 0x2F, 0x29, 0x59,
        // ];
        match sdmty {
            OwnedDataModelType::Bool => hash_update(state, &[0x11]),
            OwnedDataModelType::I8 => hash_update(state, &[0xC5]),
            OwnedDataModelType::U8 => hash_update(state, &[0x3D]),
            OwnedDataModelType::I16 => hash_update(state, &[0x1D]),
            OwnedDataModelType::I32 => hash_update(state, &[0x0D]),
            OwnedDataModelType::I64 => hash_update(state, &[0x0B]),
            OwnedDataModelType::I128 => hash_update(state, &[0x02]),
            OwnedDataModelType::U16 => hash_update(state, &[0x83]),
            OwnedDataModelType::U32 => hash_update(state, &[0xD3]),
            OwnedDataModelType::U64 => hash_update(state, &[0x13]),
            OwnedDataModelType::U128 => hash_update(state, &[0x8B]),
            OwnedDataModelType::Usize => hash_update(state, &[0x6B]),
            OwnedDataModelType::Isize => hash_update(state, &[0xAD]),
            OwnedDataModelType::F32 => hash_update(state, &[0xEF]),
            OwnedDataModelType::F64 => hash_update(state, &[0x71]),
            OwnedDataModelType::Char => hash_update(state, &[0xC1]),
            OwnedDataModelType::String => hash_update(state, &[0x25]),
            OwnedDataModelType::ByteArray => hash_update(state, &[0x65]),
            OwnedDataModelType::Option(nt) => {
                let state = hash_update(state, &[0x6D]);
                hash_named_type_owned(state, nt)
            }
            OwnedDataModelType::Unit => hash_update(state, &[0x47]),
            OwnedDataModelType::UnitStruct => hash_update(state, &[0xBF]),
            OwnedDataModelType::NewtypeStruct(nt) => {
                let state = hash_update(state, &[0x9D]);
                hash_named_type_owned(state, nt)
            }
            OwnedDataModelType::Seq(nt) => {
                let state = hash_update(state, &[0x03]);
                hash_named_type_owned(state, nt)
            }
            OwnedDataModelType::Tuple(nts) => {
                let mut state = hash_update(state, &[0xA7]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type_owned(state, &nts[idx]);
                    idx += 1;
                }
                state
            }
            OwnedDataModelType::TupleStruct(nts) => {
                let mut state = hash_update(state, &[0x05]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type_owned(state, &nts[idx]);
                    idx += 1;
                }
                state
            }
            OwnedDataModelType::Map { key, val } => {
                let state = hash_update(state, &[0x4F]);
                let state = hash_named_type_owned(state, key);
                hash_named_type_owned(state, val)
            }
            OwnedDataModelType::Struct(nvs) => {
                let mut state = hash_update(state, &[0x7F]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_value_owned(state, &nvs[idx]);
                    idx += 1;
                }
                state
            }
            OwnedDataModelType::Enum(nvs) => {
                let mut state = hash_update(state, &[0xE9]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_variant_owned(state, &nvs[idx]);
                    idx += 1;
                }
                state
            }
            OwnedDataModelType::Schema => hash_update(state, &[0xB3]),
        }
    }

    fn hash_named_type_owned(state: u64, nt: &OwnedNamedType) -> u64 {
        // NOTE: We do *not* hash the name of the type in hashv2. This
        // is to allow "safe" type punning, e.g. treating `Vec<u8>` and
        // `&[u8]` as compatible types, when talking between std and no-std
        // targets
        //
        // let state = hash_update(state, nt.name.as_bytes());
        hash_sdm_type_owned(state, &nt.ty)
    }

    fn hash_named_variant_owned(state: u64, nt: &OwnedNamedVariant) -> u64 {
        let state = hash_update(state, nt.name.as_bytes());
        match &nt.ty {
            OwnedDataModelVariant::UnitVariant => hash_update(state, &[0xB5]),
            OwnedDataModelVariant::NewtypeVariant(nt) => {
                let state = hash_update(state, &[0xDF]);
                hash_named_type_owned(state, nt)
            }
            OwnedDataModelVariant::TupleVariant(nts) => {
                let mut state = hash_update(state, &[0xC7]);
                let mut idx = 0;
                while idx < nts.len() {
                    state = hash_named_type_owned(state, &nts[idx]);
                    idx += 1;
                }
                state
            }
            OwnedDataModelVariant::StructVariant(nvs) => {
                let mut state = hash_update(state, &[0x67]);
                let mut idx = 0;
                while idx < nvs.len() {
                    state = hash_named_value_owned(state, &nvs[idx]);
                    idx += 1;
                }
                state
            }
        }
    }

    fn hash_named_value_owned(state: u64, nt: &OwnedNamedValue) -> u64 {
        let state = hash_update(state, nt.name.as_bytes());
        hash_named_type_owned(state, &nt.ty)
    }
}

#[cfg(test)]
mod test {
    use super::fnv1a64::hash_ty_path;
    use super::*;

    #[test]
    fn type_punning_good() {
        let hash_1 = hash_ty_path::<Vec<u8>>("test_path");
        let hash_2 = hash_ty_path::<&[u8]>("test_path");
        let hash_3 = hash_ty_path::<Vec<u16>>("test_path");
        let hash_4 = hash_ty_path::<&[u16]>("test_path");
        let hash_5 = hash_ty_path::<Vec<u8>>("test_patt");
        let hash_6 = hash_ty_path::<&[u8]>("test_patt");
        assert_eq!(hash_1, hash_2);
        assert_eq!(hash_3, hash_4);
        assert_ne!(hash_1, hash_3);
        assert_ne!(hash_2, hash_4);
        assert_ne!(hash_1, hash_5);
        assert_ne!(hash_2, hash_6);
    }

    // TODO: It is questionable if I like this outcome
    #[test]
    fn type_punning_questionable() {
        #[derive(Schema)]
        #[allow(unused)]
        struct Wrapper1(u8);

        #[derive(Schema)]
        #[allow(unused)]
        struct Wrapper2(u8);

        let hash_1 = hash_ty_path::<Wrapper1>("test_path");
        let hash_2 = hash_ty_path::<Wrapper2>("test_path");
        assert_eq!(hash_1, hash_2);
    }
}