#length #fixed-length #required #bit-fields #ssz #serialization #bit-vector

eth2_ssz_types

Provides types with unique properties required for SSZ serialization and Merklization

3 unstable releases

0.2.1 Nov 23, 2021
0.2.0 Aug 24, 2021
0.1.0 Jul 29, 2021

#1608 in Rust patterns

Download history 723/week @ 2024-07-21 705/week @ 2024-07-28 495/week @ 2024-08-04 3213/week @ 2024-08-11 413/week @ 2024-08-18 465/week @ 2024-08-25 1191/week @ 2024-09-01 3097/week @ 2024-09-08 903/week @ 2024-09-15 495/week @ 2024-09-22 481/week @ 2024-09-29 242/week @ 2024-10-06 415/week @ 2024-10-13 336/week @ 2024-10-20 387/week @ 2024-10-27 827/week @ 2024-11-03

1,998 downloads per month
Used in trin-types

Apache-2.0

85KB
2K SLoC

Provides types with unique properties required for SSZ serialization and Merklization:

  • FixedVector: A heap-allocated list with a size that is fixed at compile time.
  • VariableList: A heap-allocated list that cannot grow past a type-level maximum length.
  • BitList: A heap-allocated bitfield that with a type-level maximum length.
  • BitVector: A heap-allocated bitfield that with a type-level fixed_ length.

These structs are required as SSZ serialization and Merklization rely upon type-level lengths for padding and verification.

Adheres to the Ethereum 2.0 SSZ specification at v0.12.1.

Example

use ssz_types::*;

pub struct Example {
    bit_vector: BitVector<typenum::U8>,
    bit_list: BitList<typenum::U8>,
    variable_list: VariableList<u64, typenum::U8>,
    fixed_vector: FixedVector<u64, typenum::U8>,
}

let mut example = Example {
    bit_vector: Bitfield::new(),
    bit_list: Bitfield::with_capacity(4).unwrap(),
    variable_list: <_>::from(vec![0, 1]),
    fixed_vector: <_>::from(vec![2, 3]),
};

assert_eq!(example.bit_vector.len(), 8);
assert_eq!(example.bit_list.len(), 4);
assert_eq!(&example.variable_list[..], &[0, 1]);
assert_eq!(&example.fixed_vector[..], &[2, 3, 0, 0, 0, 0, 0, 0]);

Dependencies

~6.5–9.5MB
~267K SLoC