-
Notifications
You must be signed in to change notification settings - Fork 2
feat: ssz, eth2api add bitfield accessors, synthetic graffiti #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+222
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -253,6 +253,15 @@ impl<T: TreeHash, const SIZE: usize> TreeHash for SszVector<T, SIZE> { | |
|
|
||
| const BIT_MASK: [u8; 8] = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]; | ||
|
|
||
| /// Error returned by bitfield combinators that require operands of equal | ||
| /// length. | ||
| #[derive(Debug, thiserror::Error, PartialEq, Eq)] | ||
| pub enum BitfieldError { | ||
| /// The two bitlists have different bit lengths and cannot be combined. | ||
| #[error("bitlists are different lengths")] | ||
| DifferentLength, | ||
| } | ||
|
|
||
| /// SSZ variable-length bitfield with maximum capacity. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct BitList<const MAX: usize> { | ||
|
|
@@ -344,6 +353,66 @@ impl<const MAX: usize> BitList<MAX> { | |
| len: capacity, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the bit at index `i`, or `false` if `i` is out of range. | ||
| pub fn bit_at(&self, i: usize) -> bool { | ||
| if i >= self.len { | ||
| return false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this better return Result / Option for this function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| self.bytes[i / 8] & BIT_MASK[i % 8] != 0 | ||
| } | ||
|
|
||
| /// Sets the bit at index `i` to `value`; out-of-range indices are ignored. | ||
| pub fn set_bit_at(&mut self, i: usize, value: bool) { | ||
| if i >= self.len { | ||
| return; | ||
|
mskrzypkows marked this conversation as resolved.
|
||
| } | ||
| if value { | ||
| self.bytes[i / 8] |= BIT_MASK[i % 8]; | ||
| } else { | ||
| self.bytes[i / 8] &= !BIT_MASK[i % 8]; | ||
| } | ||
| } | ||
|
|
||
| /// Returns the indices of all set bits in ascending order. | ||
| pub fn bit_indices(&self) -> Vec<usize> { | ||
| (0..self.len).filter(|&i| self.bit_at(i)).collect() | ||
| } | ||
|
|
||
| /// Returns `true` if every bit set in `other` is also set in `self`. | ||
| /// | ||
| /// Errors with [`BitfieldError::DifferentLength`] if the two bitlists do | ||
| /// not have the same bit length. | ||
| pub fn contains(&self, other: &Self) -> Result<bool, BitfieldError> { | ||
| if self.len != other.len { | ||
| return Err(BitfieldError::DifferentLength); | ||
| } | ||
| Ok(other | ||
| .bytes | ||
| .iter() | ||
| .zip(&self.bytes) | ||
| .all(|(o, s)| o & s == *o)) | ||
| } | ||
|
|
||
| /// Returns the bitwise OR (union) of `self` and `other`. | ||
| /// | ||
| /// Errors with [`BitfieldError::DifferentLength`] if the two bitlists do | ||
| /// not have the same bit length. | ||
| pub fn or(&self, other: &Self) -> Result<Self, BitfieldError> { | ||
| if self.len != other.len { | ||
| return Err(BitfieldError::DifferentLength); | ||
| } | ||
| let bytes = self | ||
| .bytes | ||
| .iter() | ||
| .zip(&other.bytes) | ||
| .map(|(a, b)| a | b) | ||
| .collect(); | ||
| Ok(Self { | ||
| bytes, | ||
| len: self.len, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<const MAX: usize> Serialize for BitList<MAX> { | ||
|
|
@@ -433,6 +502,31 @@ impl<const SIZE: usize> BitVector<SIZE> { | |
| } | ||
| v | ||
| } | ||
|
|
||
| /// Returns the bit at index `i`, or `false` if `i` is out of range. | ||
| pub fn bit_at(&self, i: usize) -> bool { | ||
| if i >= SIZE { | ||
| return false; | ||
| } | ||
| self.bytes[i / 8] & BIT_MASK[i % 8] != 0 | ||
| } | ||
|
|
||
| /// Sets the bit at index `i` to `value`; out-of-range indices are ignored. | ||
| pub fn set_bit_at(&mut self, i: usize, value: bool) { | ||
| if i >= SIZE { | ||
| return; | ||
| } | ||
| if value { | ||
| self.bytes[i / 8] |= BIT_MASK[i % 8]; | ||
| } else { | ||
| self.bytes[i / 8] &= !BIT_MASK[i % 8]; | ||
| } | ||
| } | ||
|
|
||
| /// Returns the indices of all set bits in ascending order. | ||
| pub fn bit_indices(&self) -> Vec<usize> { | ||
| (0..SIZE).filter(|&i| self.bit_at(i)).collect() | ||
| } | ||
| } | ||
|
|
||
| impl<const SIZE: usize> Serialize for BitVector<SIZE> { | ||
|
|
@@ -560,4 +654,77 @@ mod tests { | |
| let vec: SszVector<u8, 3> = vec![1, 2, 3].into(); | ||
| assert_eq!(vec.as_ref(), &[1, 2, 3]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitlist_bit_at_and_indices() { | ||
| let bl = BitList::<2048>::with_bits(3, &[0, 2]); | ||
| assert!(bl.bit_at(0)); | ||
| assert!(!bl.bit_at(1)); | ||
| assert!(bl.bit_at(2)); | ||
| // Out-of-range index reads as unset. | ||
| assert!(!bl.bit_at(3)); | ||
| assert!(!bl.bit_at(9001)); | ||
| assert_eq!(bl.bit_indices(), vec![0, 2]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitlist_bit_at_matches_ssz_round_trip() { | ||
| // SSZ byte 0x0D = sentinel at bit 3 ⇒ 3 data bits with bits 0 and 2 set, | ||
| // matching the bytes returned by `aggregation_bits()`. | ||
| let bl = BitList::<2048>::from_ssz_bytes(vec![0x0D]); | ||
| assert_eq!(bl.len(), 3); | ||
| assert_eq!(bl.bit_indices(), vec![0, 2]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitlist_set_bit_at() { | ||
| let mut bl = BitList::<2048>::with_bits(8, &[0]); | ||
| bl.set_bit_at(3, true); | ||
| assert_eq!(bl.bit_indices(), vec![0, 3]); | ||
| bl.set_bit_at(0, false); | ||
| assert_eq!(bl.bit_indices(), vec![3]); | ||
| // Out-of-range set is a no-op. | ||
| bl.set_bit_at(8, true); | ||
| assert_eq!(bl.bit_indices(), vec![3]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitlist_contains() { | ||
| let superset = BitList::<2048>::with_bits(4, &[0, 1, 2]); | ||
| let subset = BitList::<2048>::with_bits(4, &[0, 2]); | ||
| assert_eq!(superset.contains(&subset), Ok(true)); | ||
| assert_eq!(subset.contains(&superset), Ok(false)); | ||
|
|
||
| let other_len = BitList::<2048>::with_bits(8, &[0]); | ||
| assert_eq!( | ||
| superset.contains(&other_len), | ||
| Err(BitfieldError::DifferentLength) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitlist_or() { | ||
| let a = BitList::<2048>::with_bits(4, &[0]); | ||
| let b = BitList::<2048>::with_bits(4, &[1, 3]); | ||
| assert_eq!(a.or(&b).unwrap().bit_indices(), vec![0, 1, 3]); | ||
|
|
||
| let other_len = BitList::<2048>::with_bits(8, &[0]); | ||
| assert_eq!(a.or(&other_len), Err(BitfieldError::DifferentLength)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bitvector_bit_ops() { | ||
| let mut bv = BitVector::<64>::with_bits(&[0, 2]); | ||
| assert!(bv.bit_at(0)); | ||
| assert!(!bv.bit_at(1)); | ||
| assert_eq!(bv.bit_indices(), vec![0, 2]); | ||
|
|
||
| bv.set_bit_at(1, true); | ||
| assert_eq!(bv.bit_indices(), vec![0, 1, 2]); | ||
|
|
||
| // Out-of-range access is a no-op / reads as unset. | ||
| bv.set_bit_at(64, true); | ||
| assert!(!bv.bit_at(64)); | ||
| assert_eq!(bv.bit_indices(), vec![0, 1, 2]); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.