Skip to content

Commit

Permalink
Add u128 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored May 31, 2024
1 parent 981cf5a commit 218d007
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ members = [
"ssz",
"ssz_derive",
]
resolver = "2"
19 changes: 19 additions & 0 deletions ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl_decodable_for_uint!(u8, 8);
impl_decodable_for_uint!(u16, 16);
impl_decodable_for_uint!(u32, 32);
impl_decodable_for_uint!(u64, 64);
impl_decodable_for_uint!(u128, 128);

#[cfg(target_pointer_width = "32")]
impl_decodable_for_uint!(usize, 32);
Expand Down Expand Up @@ -774,4 +775,22 @@ mod tests {
Ok((65535, 0))
);
}

#[test]
fn vec_of_u128_roundtrip() {
let values = vec![
vec![0u128, 55u128, u128::MAX, u128::MAX - 3],
vec![],
vec![u128::MAX],
vec![u32::MAX as u128],
vec![0, 0, 0, 0],
vec![0, 0, 0, 0, 0, 0],
];
for vec in values {
assert_eq!(
Vec::<u128>::from_ssz_bytes(&vec.as_ssz_bytes()).unwrap(),
vec
);
}
}
}
13 changes: 13 additions & 0 deletions ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl_encodable_for_uint!(u8, 8);
impl_encodable_for_uint!(u16, 16);
impl_encodable_for_uint!(u32, 32);
impl_encodable_for_uint!(u64, 64);
impl_encodable_for_uint!(u128, 128);

#[cfg(target_pointer_width = "32")]
impl_encodable_for_uint!(usize, 32);
Expand Down Expand Up @@ -582,6 +583,18 @@ mod tests {
);
}

#[test]
fn ssz_encode_u128() {
assert_eq!(
1_u128.as_ssz_bytes(),
vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
assert_eq!(
(!0_u128).as_ssz_bytes(),
vec![255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
);
}

#[test]
fn ssz_encode_usize() {
assert_eq!(1_usize.as_ssz_bytes(), vec![1, 0, 0, 0, 0, 0, 0, 0]);
Expand Down

0 comments on commit 218d007

Please sign in to comment.