Skip to main content

midnight_transient_crypto/
fab.rs

1// This file is part of midnight-ledger.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! Defines the primitives of the field-aligned binary representation, where
15//! values are represented as sequences of binary strings, that are tied to an
16//! alignment which can be used to interpret them either as binary data, or a
17//! sequence of field elements for proving.
18
19use std::iter::once;
20
21use crate::curve::{EmbeddedFr, EmbeddedGroupAffine};
22use crate::curve::{FR_BYTES, FR_BYTES_STORED, Fr};
23use crate::hash::transient_commit;
24use crate::merkle_tree::{MerklePath, MerkleTreeDigest};
25use crate::repr::{FieldRepr, bytes_from_field_repr};
26use base_crypto::fab::{
27    Aligned, AlignedValue, Alignment, AlignmentAtom, AlignmentSegment, DynAligned,
28    FIELD_BYTE_LIMIT, InvalidBuiltinDecode, Value, ValueAtom, ValueSlice, int_size,
29};
30use base_crypto::repr::{BinaryHashRepr, MemWrite};
31use rand::Rng;
32use rand::distributions::Standard;
33use rand::prelude::Distribution;
34
35/// An extension for the value in the field-aligned binary encoding.
36pub(crate) trait ValueExt {
37    fn repr_traverse<
38        T,
39        F: Fn(T, &AlignmentAtom, &ValueAtom) -> T,
40        L: Fn(&Alignment) -> usize,
41        P: Fn(T, usize) -> T,
42    >(
43        atom_slice: &mut &[ValueAtom],
44        align: &Alignment,
45        f: &F,
46        len: &L,
47        pad: &P,
48        acc: T,
49    ) -> T;
50    fn field_repr_unchecked<W: MemWrite<Fr>>(&self, align: &Alignment, writer: &mut W);
51    fn binary_repr_unchecked<W: MemWrite<u8>>(&self, align: &Alignment, writer: &mut W);
52}
53
54impl From<MerkleTreeDigest> for ValueAtom {
55    fn from(val: MerkleTreeDigest) -> ValueAtom {
56        Fr::from(val).into()
57    }
58}
59
60impl TryFrom<&ValueAtom> for MerkleTreeDigest {
61    type Error = InvalidBuiltinDecode;
62
63    fn try_from(value: &ValueAtom) -> Result<MerkleTreeDigest, InvalidBuiltinDecode> {
64        Ok(Fr::try_from(value)?.into())
65    }
66}
67
68impl<T: Into<Value>> From<MerklePath<T>> for Value {
69    fn from(path: MerklePath<T>) -> Value {
70        let mut parts = Vec::new();
71        parts.push(path.leaf.into());
72        for entry in path.path.iter() {
73            parts.push(entry.sibling.into());
74            parts.push(entry.goes_left.into());
75        }
76        Value::concat(parts.iter())
77    }
78}
79
80impl From<MerkleTreeDigest> for Value {
81    fn from(val: MerkleTreeDigest) -> Value {
82        Value(vec![val.into()])
83    }
84}
85
86impl TryFrom<&ValueSlice> for MerkleTreeDigest {
87    type Error = InvalidBuiltinDecode;
88
89    fn try_from(value: &ValueSlice) -> Result<MerkleTreeDigest, InvalidBuiltinDecode> {
90        if value.0.len() == 1 {
91            Ok(MerkleTreeDigest::try_from(&value.0[0])?)
92        } else {
93            Err(InvalidBuiltinDecode(stringify!($ty)))
94        }
95    }
96}
97
98impl Aligned for MerkleTreeDigest {
99    fn alignment() -> Alignment {
100        Alignment::singleton(AlignmentAtom::Field)
101    }
102}
103
104impl<T: DynAligned> DynAligned for MerklePath<T> {
105    fn dyn_alignment(&self) -> Alignment {
106        let leaf_align = self.leaf.dyn_alignment();
107        let entry_align = Alignment::concat([&MerkleTreeDigest::alignment(), &bool::alignment()]);
108        Alignment::concat(
109            once(&leaf_align).chain(std::iter::repeat_n(&entry_align, self.path.len())),
110        )
111    }
112}
113
114impl From<EmbeddedGroupAffine> for Value {
115    fn from(value: EmbeddedGroupAffine) -> Value {
116        Value(vec![
117            value.x().unwrap_or(0.into()).into(),
118            value.y().unwrap_or(0.into()).into(),
119        ])
120    }
121}
122
123impl From<EmbeddedFr> for Value {
124    fn from(val: EmbeddedFr) -> Value {
125        Value(vec![val.into()])
126    }
127}
128
129impl TryFrom<&ValueSlice> for EmbeddedGroupAffine {
130    type Error = InvalidBuiltinDecode;
131
132    fn try_from(value: &ValueSlice) -> Result<EmbeddedGroupAffine, InvalidBuiltinDecode> {
133        if value.0.len() == 2 {
134            let x: Fr = (&value.0[0]).try_into()?;
135            let y: Fr = (&value.0[1]).try_into()?;
136            let is_identity = EmbeddedGroupAffine::HAS_INFINITY && x == 0.into() && y == 0.into();
137            if is_identity {
138                Ok(EmbeddedGroupAffine::identity())
139            } else {
140                Ok(EmbeddedGroupAffine::new(x, y)
141                    .ok_or(InvalidBuiltinDecode("EmbeddedGroupAffine"))?)
142            }
143        } else {
144            Err(InvalidBuiltinDecode("EmbeddedGroupAffine"))
145        }
146    }
147}
148
149impl TryFrom<&ValueSlice> for EmbeddedFr {
150    type Error = InvalidBuiltinDecode;
151
152    fn try_from(value: &ValueSlice) -> Result<EmbeddedFr, InvalidBuiltinDecode> {
153        if value.0.len() == 1 {
154            Ok(<EmbeddedFr>::try_from(&value.0[0])?)
155        } else {
156            Err(InvalidBuiltinDecode(stringify!(EmbeddedFr)))
157        }
158    }
159}
160
161impl From<EmbeddedFr> for ValueAtom {
162    fn from(val: EmbeddedFr) -> ValueAtom {
163        ValueAtom(val.as_le_bytes()).normalize()
164    }
165}
166
167impl TryFrom<&ValueAtom> for EmbeddedFr {
168    type Error = InvalidBuiltinDecode;
169
170    fn try_from(value: &ValueAtom) -> Result<EmbeddedFr, InvalidBuiltinDecode> {
171        EmbeddedFr::from_le_bytes_wide(&value.0).ok_or(InvalidBuiltinDecode("EmbeddedFr"))
172    }
173}
174
175macro_rules! forward_primitive_value {
176    ($($ty:ty),*) => {
177        $(
178            impl From<$ty> for Value {
179                fn from(val: $ty) -> Value {
180                    Value(vec![val.into()])
181                }
182            }
183
184            impl TryFrom<&ValueSlice> for $ty {
185                type Error = InvalidBuiltinDecode;
186
187                fn try_from(value: &ValueSlice) -> Result<$ty, InvalidBuiltinDecode> {
188                    if value.0.len() == 1 {
189                        Ok(<$ty>::try_from(&value.0[0])?)
190                    } else {
191                        Err(InvalidBuiltinDecode(stringify!($ty)))
192                    }
193                }
194            }
195        )*
196    }
197}
198
199forward_primitive_value!(Fr);
200
201impl From<Fr> for ValueAtom {
202    fn from(val: Fr) -> ValueAtom {
203        ValueAtom(val.as_le_bytes()).normalize()
204    }
205}
206
207impl TryFrom<&ValueAtom> for Fr {
208    type Error = InvalidBuiltinDecode;
209
210    fn try_from(value: &ValueAtom) -> Result<Fr, InvalidBuiltinDecode> {
211        if value.0.len() <= FR_BYTES {
212            Fr::from_le_bytes(&value.0).ok_or(InvalidBuiltinDecode("Fr"))
213        } else {
214            Err(InvalidBuiltinDecode("Fr"))
215        }
216    }
217}
218
219impl ValueExt for Value {
220    fn repr_traverse<
221        T,
222        F: Fn(T, &AlignmentAtom, &ValueAtom) -> T,
223        L: Fn(&Alignment) -> usize,
224        P: Fn(T, usize) -> T,
225    >(
226        atom_slice: &mut &[ValueAtom],
227        align: &Alignment,
228        f: &F,
229        len: &L,
230        pad: &P,
231        mut acc: T,
232    ) -> T {
233        for segment in align.0.iter() {
234            match segment {
235                AlignmentSegment::Atom(atom) => {
236                    acc = f(acc, atom, &atom_slice[0]);
237                    *atom_slice = &atom_slice[1..];
238                }
239                AlignmentSegment::Option(options) => {
240                    let discriminant = u16::try_from(&atom_slice[0])
241                        .expect("unchecked discriminant should decode");
242                    let choice = &options[discriminant as usize];
243                    acc = f(acc, &AlignmentAtom::Bytes { length: 2 }, &atom_slice[0]);
244                    *atom_slice = &atom_slice[1..];
245                    acc = Value::repr_traverse(atom_slice, choice, f, len, pad, acc);
246                    let padding = options.iter().map(len).max().unwrap_or(0) - len(choice);
247                    acc = pad(acc, padding);
248                }
249            }
250        }
251        acc
252    }
253
254    fn field_repr_unchecked<W: MemWrite<Fr>>(&self, align: &Alignment, writer: &mut W) {
255        let mut unconsumed_value = &self.0[..];
256        Value::repr_traverse(
257            &mut unconsumed_value,
258            align,
259            &|mut w: &mut W, a, v| {
260                v.field_repr_unchecked(a, &mut w);
261                w
262            },
263            &Alignment::field_len,
264            &|w, n| {
265                w.write(&vec![Fr::from(0); n]);
266                w
267            },
268            writer,
269        );
270        debug_assert!(unconsumed_value.is_empty());
271    }
272
273    fn binary_repr_unchecked<W: MemWrite<u8>>(&self, align: &Alignment, writer: &mut W) {
274        let mut unconsumed_value = &self.0[..];
275        Value::repr_traverse(
276            &mut unconsumed_value,
277            align,
278            &|mut w: &mut W, a, v| {
279                v.binary_repr_unchecked(a, &mut w);
280                w
281            },
282            &Alignment::bin_len,
283            &|w, n| {
284                w.write(&vec![0u8; n]);
285                w
286            },
287            writer,
288        );
289        debug_assert!(unconsumed_value.is_empty());
290    }
291}
292
293/// An extension or the alignment in the field-aligned binary encoding.
294pub trait AlignmentExt {
295    /// Parses a given field representation as this alignment, and returns the
296    /// corresponding aligned value.
297    fn parse_field_repr(&self, repr: &[Fr]) -> Option<AlignedValue>;
298
299    /// The maximum size of an `AlignedValue` within this alignment.
300    fn max_aligned_size(&self) -> usize;
301
302    /// Returns a field length.
303    fn field_len(&self) -> usize;
304
305    /// Returns a binary length.
306    fn bin_len(&self) -> usize;
307}
308
309/// Parses a given field representation to construct a vector `ValueAtom`s for use in `parse_field_repr`.
310fn parse_field_repr_inner(
311    segments: &[AlignmentSegment],
312    repr: &mut &[Fr],
313    val: &mut Vec<ValueAtom>,
314) -> Option<()> {
315    for segment in segments.iter() {
316        match segment {
317            AlignmentSegment::Atom(atom) => val.push(atom.parse_field_repr(repr)?),
318            AlignmentSegment::Option(options) => {
319                let variant = u16::try_from(*repr.first()?).ok()?;
320                *repr = &repr[1..];
321                val.push(variant.into());
322                let choice = options.get(variant as usize)?;
323                parse_field_repr_inner(&choice.0, repr, val)?;
324                let padding = options.iter().map(Alignment::field_len).max().unwrap_or(0)
325                    - choice.field_len();
326                if repr.len() < padding || repr[..padding].iter().any(|f| *f != Fr::from(0)) {
327                    return None;
328                }
329                *repr = &repr[padding..];
330            }
331        }
332    }
333    Some(())
334}
335
336impl AlignmentExt for Alignment {
337    fn parse_field_repr(&self, mut repr: &[Fr]) -> Option<AlignedValue> {
338        let mut value = Vec::new();
339        parse_field_repr_inner(&self.0, &mut repr, &mut value)?;
340        Some(AlignedValue {
341            value: Value(value),
342            alignment: self.clone(),
343        })
344    }
345
346    fn max_aligned_size(&self) -> usize {
347        1 + int_size(self.0.len())
348            + self
349                .0
350                .iter()
351                .map(AlignmentSegment::max_aligned_size)
352                .sum::<usize>()
353    }
354
355    fn field_len(&self) -> usize {
356        self.0.iter().map(AlignmentSegment::field_len).sum()
357    }
358
359    fn bin_len(&self) -> usize {
360        self.0.iter().map(AlignmentSegment::bin_len).sum()
361    }
362}
363
364impl FieldRepr for Alignment {
365    fn field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
366        (self.0.len() as u32).field_repr(writer);
367        for segment in self.0.iter() {
368            segment.field_repr(writer);
369        }
370    }
371
372    fn field_size(&self) -> usize {
373        1 + self.0.iter().map(FieldRepr::field_size).sum::<usize>()
374    }
375}
376
377impl FieldRepr for AlignedValue {
378    fn field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
379        self.alignment.field_repr(writer);
380        self.value.field_repr_unchecked(&self.alignment, writer);
381    }
382
383    fn field_size(&self) -> usize {
384        self.alignment.field_size() + self.alignment.field_len()
385    }
386}
387
388/// An extension for the `AlignedValue`.
389pub trait AlignedValueExt {
390    /// Iterate over the field elements in this value, not encoding the alignment itself.
391    fn value_only_field_repr<W: MemWrite<Fr>>(&self, writer: &mut W);
392
393    /// Returns the number of elements output by [`Self::value_only_field_repr`].
394    fn value_only_field_size(&self) -> usize;
395}
396
397impl AlignedValueExt for AlignedValue {
398    fn value_only_field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
399        self.value.field_repr_unchecked(&self.alignment, writer)
400    }
401
402    fn value_only_field_size(&self) -> usize {
403        self.alignment.field_len()
404    }
405}
406
407/// Wrapper around [`AlignedValue`] whose [`FieldRepr`] implementation uses
408/// [`AlignedValue::value_only_field_repr`].
409pub struct ValueReprAlignedValue(pub AlignedValue);
410
411impl From<ValueReprAlignedValue> for Value {
412    fn from(value: ValueReprAlignedValue) -> Value {
413        value.0.value
414    }
415}
416
417impl FieldRepr for ValueReprAlignedValue {
418    fn field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
419        self.0.value_only_field_repr(writer);
420    }
421
422    fn field_size(&self) -> usize {
423        self.0.value_only_field_size()
424    }
425}
426
427impl BinaryHashRepr for ValueReprAlignedValue {
428    fn binary_repr<W: MemWrite<u8>>(&self, writer: &mut W) {
429        self.0
430            .value
431            .binary_repr_unchecked(&self.0.alignment, writer);
432    }
433    fn binary_len(&self) -> usize {
434        self.0.alignment.bin_len()
435    }
436}
437
438impl DynAligned for ValueReprAlignedValue {
439    fn dyn_alignment(&self) -> Alignment {
440        self.0.dyn_alignment()
441    }
442}
443
444/// An extension for the `ValueAtom`.
445pub(crate) trait ValueAtomExt {
446    /// The field representation of this atom, against a matching alignment
447    /// atom. Returns `false` if the value does not fit.
448    #[allow(dead_code)]
449    fn field_repr<W: MemWrite<Fr>>(&self, ty: &AlignmentAtom, writer: &mut W) -> bool;
450
451    /// Returns the field representation of a primitive value wrt. a primitive
452    /// type.
453    ///
454    /// # Safety
455    ///
456    /// This is safe to call iff `ty.`[`fits`](AlignmentAtom::fits)`(self)` has
457    /// returns `true`.
458    fn field_repr_unchecked<W: MemWrite<Fr>>(&self, ty: &AlignmentAtom, writer: &mut W);
459    fn binary_repr_unchecked<W: MemWrite<u8>>(&self, ty: &AlignmentAtom, writer: &mut W);
460}
461
462// Interprets a value atom as a field element, reducing modulo the field prime
463// if required.
464//
465// The input *must* have validated as having AlignementAtom::Field, and
466// therefore is guaranteed to be at most FIELD_BYTE_LIMIT bytes long.
467fn value_atom_as_field(atom: &ValueAtom) -> Fr {
468    let mut bytes = [0u8; FIELD_BYTE_LIMIT];
469    assert!(atom.0.len() <= FIELD_BYTE_LIMIT);
470    bytes[..atom.0.len()].copy_from_slice(&atom.0);
471    // A white lie -- we aren't inputting uniform bytes, but we are of the
472    // correct length, and this function does the right modulus reduction.
473    Fr::from_uniform_bytes(&bytes)
474}
475
476impl ValueAtomExt for ValueAtom {
477    fn field_repr<W: MemWrite<Fr>>(&self, ty: &AlignmentAtom, writer: &mut W) -> bool {
478        if !ty.fits(self) {
479            false
480        } else {
481            self.field_repr_unchecked(ty, writer);
482            true
483        }
484    }
485
486    fn field_repr_unchecked<W: MemWrite<Fr>>(&self, ty: &AlignmentAtom, writer: &mut W) {
487        match ty {
488            AlignmentAtom::Compress => {
489                // Special case for the empty string to make defaults work well.
490                if self.0.is_empty() {
491                    writer.write(&[0.into()]);
492                } else {
493                    writer.write(&[transient_commit(&self.0[..], (self.0.len() as u64).into())])
494                }
495            }
496            AlignmentAtom::Bytes { length } => {
497                let prepend_zeros = (*length as usize).div_ceil(FR_BYTES_STORED)
498                    - self.0.len().div_ceil(FR_BYTES_STORED);
499                let raw = self
500                    .0
501                    .chunks(FR_BYTES_STORED)
502                    .map(|bytes| {
503                        Fr::from_le_bytes(bytes).expect("Bytes must fit into FR_BYTES_STORED chunk")
504                    })
505                    .rev();
506                writer.write(&vec![Fr::from(0); prepend_zeros]);
507                writer.write(&raw.collect::<Vec<_>>());
508            }
509            AlignmentAtom::Field => writer.write(&[value_atom_as_field(self)]),
510        }
511    }
512
513    fn binary_repr_unchecked<W: MemWrite<u8>>(&self, ty: &AlignmentAtom, writer: &mut W) {
514        match ty {
515            AlignmentAtom::Compress => {
516                transient_commit(&self.0[..], (self.0.len() as u64).into()).binary_repr(writer);
517            }
518            AlignmentAtom::Bytes { length } => {
519                writer.write(&self.0);
520                let missing_bytes = (*length as usize) - self.0.len();
521                let zeroes = vec![0u8; missing_bytes];
522                writer.write(&zeroes);
523            }
524            AlignmentAtom::Field => {
525                value_atom_as_field(self).binary_repr(writer);
526            }
527        }
528    }
529}
530
531/// An extension for the `AlignmentAtom`.
532pub(crate) trait AlignmentAtomExt {
533    fn parse_field_repr(&self, repr: &mut &[Fr]) -> Option<ValueAtom>;
534    #[allow(dead_code)]
535    fn sample_value_atom<R: Rng + ?Sized>(&self, rng: &mut R) -> ValueAtom;
536    fn max_aligned_size(&self) -> usize;
537    fn field_len(&self) -> usize;
538    fn bin_len(&self) -> usize;
539}
540
541impl AlignmentAtomExt for AlignmentAtom {
542    fn parse_field_repr(&self, repr: &mut &[Fr]) -> Option<ValueAtom> {
543        match self {
544            // Impossible to parse compress from a field!
545            AlignmentAtom::Compress => None,
546            AlignmentAtom::Field => {
547                let res = repr.first()?;
548                *repr = &repr[1..];
549                Some(ValueAtom(res.as_le_bytes()).normalize())
550            }
551            AlignmentAtom::Bytes { length } => {
552                bytes_from_field_repr(repr, *length as usize).map(ValueAtom)
553            }
554        }
555    }
556
557    fn sample_value_atom<R: Rng + ?Sized>(&self, rng: &mut R) -> ValueAtom
558    where
559        Standard: Distribution<Fr>,
560    {
561        match self {
562            Self::Compress | Self::Field => {
563                let val = rng.r#gen::<Fr>().as_le_bytes();
564                ValueAtom(val).normalize()
565            }
566            Self::Bytes { length } => {
567                let mut bytes: Vec<u8> = vec![0; *length as usize];
568                rng.fill_bytes(&mut bytes);
569                ValueAtom(bytes).normalize()
570            }
571        }
572    }
573
574    fn max_aligned_size(&self) -> usize {
575        match self {
576            AlignmentAtom::Compress | AlignmentAtom::Field => 2 + FR_BYTES,
577            AlignmentAtom::Bytes { length } => 2 + int_size(*length as usize) + *length as usize,
578        }
579    }
580
581    fn field_len(&self) -> usize {
582        match self {
583            AlignmentAtom::Compress | AlignmentAtom::Field => 1,
584            AlignmentAtom::Bytes { length } => length.div_ceil(FR_BYTES_STORED as u32) as usize,
585        }
586    }
587
588    fn bin_len(&self) -> usize {
589        match self {
590            AlignmentAtom::Compress | AlignmentAtom::Field => FR_BYTES,
591            AlignmentAtom::Bytes { length } => *length as usize,
592        }
593    }
594}
595
596impl FieldRepr for AlignmentAtom {
597    fn field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
598        match self {
599            AlignmentAtom::Bytes { length } => writer.write(&[(*length).into()]),
600            AlignmentAtom::Compress => writer.write(&[(-1).into()]),
601            AlignmentAtom::Field => writer.write(&[(-2).into()]),
602        }
603    }
604
605    fn field_size(&self) -> usize {
606        1
607    }
608}
609
610/// An extension for the `AlignmentSegmentExt`.
611pub(crate) trait AlignmentSegmentExt {
612    fn max_aligned_size(&self) -> usize;
613    fn field_len(&self) -> usize;
614    fn bin_len(&self) -> usize;
615}
616
617impl AlignmentSegmentExt for AlignmentSegment {
618    fn max_aligned_size(&self) -> usize {
619        match self {
620            AlignmentSegment::Atom(atom) => atom.max_aligned_size(),
621            AlignmentSegment::Option(options) => options
622                .iter()
623                .map(Alignment::max_aligned_size)
624                .max()
625                .unwrap_or(0),
626        }
627    }
628
629    fn field_len(&self) -> usize {
630        match self {
631            AlignmentSegment::Atom(atom) => atom.field_len(),
632            AlignmentSegment::Option(options) => {
633                1 + options.iter().map(Alignment::field_len).max().unwrap_or(0)
634            }
635        }
636    }
637
638    fn bin_len(&self) -> usize {
639        match self {
640            AlignmentSegment::Atom(atom) => atom.bin_len(),
641            AlignmentSegment::Option(options) => {
642                2 + options.iter().map(Alignment::bin_len).max().unwrap_or(0)
643            }
644        }
645    }
646}
647
648impl FieldRepr for AlignmentSegment {
649    fn field_repr<W: MemWrite<Fr>>(&self, writer: &mut W) {
650        match self {
651            AlignmentSegment::Atom(atom) => atom.field_repr(writer),
652            AlignmentSegment::Option(options) => {
653                writer.write(&[(-3).into(), (options.len() as u32).into()]);
654                for option in options {
655                    option.field_repr(writer);
656                }
657            }
658        }
659    }
660
661    fn field_size(&self) -> usize {
662        match self {
663            AlignmentSegment::Atom(atom) => atom.field_size(),
664            AlignmentSegment::Option(options) => {
665                2 + options.iter().map(FieldRepr::field_size).sum::<usize>()
666            }
667        }
668    }
669}
670
671#[cfg(all(test, feature = "proptest"))]
672mod tests {
673    use super::*;
674    use base_crypto::fab::AlignedValue;
675    use proptest::prelude::*;
676
677    proptest! {
678        #[test]
679        fn test_fab_repr_consistency(value in AlignedValue::arbitrary()) {
680            assert!(value.alignment.fits(&value.value));
681            assert_eq!(value.field_vec().len(), value.field_size());
682            let value_repr = ValueReprAlignedValue(value);
683            assert_eq!(value_repr.binary_vec().len(), value_repr.binary_len());
684        }
685    }
686}