Skip to content

Commit 038393b

Browse files
committed
chore: introduce endianness BytesBitIterator
Signed-off-by: aeryz <[email protected]>
1 parent 9d94515 commit 038393b

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/aptos-verifier/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use unionlabs::{
2020
validator_verifier::ValidatorVerifier,
2121
},
2222
primitives::{encoding::HexPrefixed, H256, H384, H768},
23-
BytesBitIterator,
23+
BytesBitIteratorBE,
2424
};
2525

2626
pub(crate) const MAX_ACCUMULATOR_PROOF_DEPTH: usize = 63;
@@ -41,9 +41,9 @@ pub trait BlsVerify {
4141
///
4242
/// * `current_validator_verifier`: The validator verifier for the current(trusted) epoch.
4343
/// * `trusted_state`: Currently trusted `LedgerInfo`. Note that if there's any epoch change, it **MUST** start
44-
/// from the current epoch + 1.
44+
/// from the current epoch + 1.
4545
/// * `state_proof`: Proof of state transition. Note that the function expects epoch changes to be in an ascending
46-
/// order respective to the epoch number.
46+
/// order respective to the epoch number.
4747
/// * `bls_verifier`: BLS verifier
4848
pub fn verify_state_proof<V: BlsVerify>(
4949
current_validator_verifier: &ValidatorVerifier,
@@ -88,7 +88,7 @@ pub fn verify_ledger_info<V: BlsVerify>(
8888
bls_verifier: &V,
8989
) -> Result<(), Error> {
9090
// Self::check_num_of_voters(self.len() as u16, multi_signature.get_signers_bitvec())?;
91-
let (pub_keys, _) = BytesBitIterator::new(&ledger_info.signatures.validator_bitmask.inner)
91+
let (pub_keys, _) = BytesBitIteratorBE::new(&ledger_info.signatures.validator_bitmask.inner)
9292
.enumerate()
9393
.filter(|(_, is_true)| *is_true)
9494
.map(|(i, _)| {
@@ -214,7 +214,7 @@ pub fn verify_existence_proof(
214214
.iter()
215215
.rev()
216216
.zip(
217-
BytesBitIterator::new(&element_key)
217+
BytesBitIteratorBE::new(&element_key)
218218
.rev()
219219
.skip(256 - proof.siblings.len()),
220220
)

lib/ethereum-sync-protocol/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use typenum::Unsigned;
1919
use unionlabs::{
2020
ensure,
2121
primitives::{H256, H384, H768},
22-
BytesBitIterator,
22+
BytesBitIteratorLE,
2323
};
2424

2525
use crate::{
@@ -73,7 +73,7 @@ pub fn validate_light_client_update<C: ChainSpec, V: BlsVerify>(
7373
) -> Result<(), Error> {
7474
// verify that the sync committee has sufficient participants
7575
let sync_aggregate = &update.sync_aggregate;
76-
let set_bits = BytesBitIterator::new(&sync_aggregate.sync_committee_bits)
76+
let set_bits = BytesBitIteratorLE::new(&sync_aggregate.sync_committee_bits)
7777
.filter(|included| *included)
7878
.count();
7979
ensure(
@@ -209,7 +209,7 @@ pub fn validate_light_client_update<C: ChainSpec, V: BlsVerify>(
209209

210210
// It's not mandatory for all of the members of the sync committee to participate. So we are extracting the
211211
// public keys of the ones who participated.
212-
let participant_pubkeys = BytesBitIterator::new(&sync_aggregate.sync_committee_bits)
212+
let participant_pubkeys = BytesBitIteratorLE::new(&sync_aggregate.sync_committee_bits)
213213
.zip(sync_committee.pubkeys.iter())
214214
.filter_map(|(included, pubkey)| if included { Some(pubkey) } else { None })
215215
.collect::<Vec<_>>();

lib/unionlabs/src/aptos/ledger_info.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ pub enum LedgerInfoWithSignatures {
1515
}
1616

1717
impl LedgerInfoWithSignatures {
18+
#[must_use]
1819
pub fn ledger_info(&self) -> &LedgerInfo {
1920
let Self::V0(ledger_info) = self;
2021
&ledger_info.ledger_info
2122
}
2223

24+
#[must_use]
2325
pub fn signatures(&self) -> &AggregateSignature {
2426
let Self::V0(ledger_info) = self;
2527
&ledger_info.signatures

lib/unionlabs/src/lib.rs

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate alloc;
1717
use core::{
1818
fmt::{self, Debug, Display},
1919
iter,
20+
marker::PhantomData,
2021
ptr::addr_of,
2122
str::FromStr,
2223
};
@@ -260,17 +261,43 @@ impl<T: core::error::Error> Display for ErrorReporter<T> {
260261
}
261262
}
262263

264+
pub trait BitIndex {
265+
/// Get the 'i'th bit
266+
fn index_of(i: usize) -> usize;
267+
}
268+
269+
pub struct LittleEndianBitIndex;
270+
271+
impl BitIndex for LittleEndianBitIndex {
272+
fn index_of(i: usize) -> usize {
273+
i % 8
274+
}
275+
}
276+
277+
pub struct BigEndianBitIndex;
278+
279+
impl BitIndex for BigEndianBitIndex {
280+
fn index_of(i: usize) -> usize {
281+
7 - i % 8
282+
}
283+
}
284+
285+
pub type BytesBitIteratorLE<'a> = BytesBitIterator<'a, LittleEndianBitIndex>;
286+
pub type BytesBitIteratorBE<'a> = BytesBitIterator<'a, BigEndianBitIndex>;
287+
263288
#[must_use = "constructing an iterator has no effect"]
264-
pub struct BytesBitIterator<'a> {
289+
pub struct BytesBitIterator<'a, E: BitIndex> {
265290
bz: &'a [u8],
266291
pos: core::ops::Range<usize>,
292+
_marker: PhantomData<E>,
267293
}
268294

269-
impl<'a> BytesBitIterator<'a> {
295+
impl<'a, E: BitIndex> BytesBitIterator<'a, E> {
270296
pub fn new(bz: &'a impl AsRef<[u8]>) -> Self {
271297
BytesBitIterator {
272298
bz: bz.as_ref(),
273299
pos: (0..bz.as_ref().len() * 8),
300+
_marker: PhantomData,
274301
}
275302
}
276303

@@ -279,12 +306,12 @@ impl<'a> BytesBitIterator<'a> {
279306
// debug_assert_eq!(self.hash_bytes.len(), Hash::LENGTH); // invariant
280307
// debug_assert_lt!(index, Hash::LENGTH_IN_BITS); // assumed precondition
281308
let pos = index / 8;
282-
let bit = index % 8;
309+
let bit = E::index_of(index);
283310
(self.bz[pos] >> bit) & 1 != 0
284311
}
285312
}
286313

287-
impl core::iter::Iterator for BytesBitIterator<'_> {
314+
impl<E: BitIndex> core::iter::Iterator for BytesBitIterator<'_, E> {
288315
type Item = bool;
289316

290317
fn next(&mut self) -> Option<Self::Item> {
@@ -296,7 +323,7 @@ impl core::iter::Iterator for BytesBitIterator<'_> {
296323
}
297324
}
298325

299-
impl core::iter::DoubleEndedIterator for BytesBitIterator<'_> {
326+
impl<E: BitIndex> core::iter::DoubleEndedIterator for BytesBitIterator<'_, E> {
300327
fn next_back(&mut self) -> Option<Self::Item> {
301328
self.pos.next_back().map(|x| self.get_bit(x))
302329
}

0 commit comments

Comments
 (0)