Skip to content

Commit 847e1c9

Browse files
committed
primitives: Inline public functions
Audit the `primitives` module and all submodules; add inline to all public methods that are small enough (excl. error impls).
1 parent d998236 commit 847e1c9

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

src/primitives/checksum.rs

+13
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ impl<Ck: Checksum> Default for Engine<Ck> {
9393

9494
impl<Ck: Checksum> Engine<Ck> {
9595
/// Constructs a new checksum engine with no data input.
96+
#[inline]
9697
pub fn new() -> Self { Engine { residue: Ck::MidstateRepr::ONE } }
9798

9899
/// Feeds `hrp` into the checksum engine.
100+
#[inline]
99101
pub fn input_hrp(&mut self, hrp: &Hrp) {
100102
for fe in HrpFe32Iter::new(hrp) {
101103
self.input_fe(fe)
@@ -105,6 +107,7 @@ impl<Ck: Checksum> Engine<Ck> {
105107
/// Adds a single gf32 element to the checksum engine.
106108
///
107109
/// This is where the actual checksum computation magic happens.
110+
#[inline]
108111
pub fn input_fe(&mut self, e: Fe32) {
109112
let xn = self.residue.mul_by_x_then_add(Ck::CHECKSUM_LENGTH, e.into());
110113
for i in 0..5 {
@@ -120,13 +123,15 @@ impl<Ck: Checksum> Engine<Ck> {
120123
/// string, then computing the actual residue, and then replacing the
121124
/// target with the actual. This method lets us compute the actual residue
122125
/// without doing any string concatenations.
126+
#[inline]
123127
pub fn input_target_residue(&mut self) {
124128
for i in 0..Ck::CHECKSUM_LENGTH {
125129
self.input_fe(Fe32(Ck::TARGET_RESIDUE.unpack(Ck::CHECKSUM_LENGTH - i - 1)));
126130
}
127131
}
128132

129133
/// Returns for the current checksum residue.
134+
#[inline]
130135
pub fn residue(&self) -> &Ck::MidstateRepr { &self.residue }
131136
}
132137

@@ -161,12 +166,15 @@ pub struct PackedNull;
161166

162167
impl ops::BitXor<PackedNull> for PackedNull {
163168
type Output = PackedNull;
169+
#[inline]
164170
fn bitxor(self, _: PackedNull) -> PackedNull { PackedNull }
165171
}
166172

167173
impl PackedFe32 for PackedNull {
168174
const ONE: Self = PackedNull;
175+
#[inline]
169176
fn unpack(&self, _: usize) -> u8 { 0 }
177+
#[inline]
170178
fn mul_by_x_then_add(&mut self, _: usize, _: u8) -> u8 { 0 }
171179
}
172180

@@ -175,11 +183,13 @@ macro_rules! impl_packed_fe32 {
175183
impl PackedFe32 for $ty {
176184
const ONE: Self = 1;
177185

186+
#[inline]
178187
fn unpack(&self, n: usize) -> u8 {
179188
debug_assert!(n < Self::WIDTH);
180189
(*self >> (n * 5)) as u8 & 0x1f
181190
}
182191

192+
#[inline]
183193
fn mul_by_x_then_add(&mut self, degree: usize, add: u8) -> u8 {
184194
debug_assert!(degree > 0);
185195
debug_assert!(degree <= Self::WIDTH);
@@ -208,6 +218,7 @@ pub struct HrpFe32Iter<'hrp> {
208218
impl<'hrp> HrpFe32Iter<'hrp> {
209219
/// Creates an iterator that yields the field elements of `hrp` as they are input into the
210220
/// checksum algorithm.
221+
#[inline]
211222
pub fn new(hrp: &'hrp Hrp) -> Self {
212223
let high_iter = hrp.lowercase_byte_iter();
213224
let low_iter = hrp.lowercase_byte_iter();
@@ -218,6 +229,7 @@ impl<'hrp> HrpFe32Iter<'hrp> {
218229

219230
impl<'hrp> Iterator for HrpFe32Iter<'hrp> {
220231
type Item = Fe32;
232+
#[inline]
221233
fn next(&mut self) -> Option<Fe32> {
222234
if let Some(ref mut high_iter) = &mut self.high_iter {
223235
match high_iter.next() {
@@ -237,6 +249,7 @@ impl<'hrp> Iterator for HrpFe32Iter<'hrp> {
237249
None
238250
}
239251

252+
#[inline]
240253
fn size_hint(&self) -> (usize, Option<usize>) {
241254
let high = match &self.high_iter {
242255
Some(high_iter) => {

src/primitives/decode.rs

+24
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<'s> UncheckedHrpstring<'s> {
122122
/// Parses an bech32 encode string and constructs a [`UncheckedHrpstring`] object.
123123
///
124124
/// Checks for valid ASCII values, does not validate the checksum.
125+
#[inline]
125126
pub fn new(s: &'s str) -> Result<Self, UncheckedHrpstringError> {
126127
let sep_pos = check_characters(s)?;
127128
let (hrp, data) = s.split_at(sep_pos);
@@ -135,9 +136,11 @@ impl<'s> UncheckedHrpstring<'s> {
135136
}
136137

137138
/// Returns the human-readable part.
139+
#[inline]
138140
pub fn hrp(&self) -> Hrp { self.hrp }
139141

140142
/// Validates that data has a valid checksum for the `Ck` algorithm and returns a [`CheckedHrpstring`].
143+
#[inline]
141144
pub fn validate_and_remove_checksum<Ck: Checksum>(
142145
self,
143146
) -> Result<CheckedHrpstring<'s>, ChecksumError> {
@@ -151,12 +154,14 @@ impl<'s> UncheckedHrpstring<'s> {
151154
/// This is useful if you do not know which checksum algorithm was used and wish to validate
152155
/// against multiple algorithms consecutively. If this function returns `true` then call
153156
/// `remove_checksum` to get a [`CheckedHrpstring`].
157+
#[inline]
154158
pub fn has_valid_checksum<Ck: Checksum>(&self) -> bool {
155159
self.validate_checksum::<Ck>().is_ok()
156160
}
157161

158162
/// Validates that data has a valid checksum for the `Ck` algorithm (this may mean an empty
159163
/// checksum if `NoChecksum` is used).
164+
#[inline]
160165
pub fn validate_checksum<Ck: Checksum>(&self) -> Result<(), ChecksumError> {
161166
use ChecksumError::*;
162167

@@ -193,6 +198,7 @@ impl<'s> UncheckedHrpstring<'s> {
193198
/// # Panics
194199
///
195200
/// May panic if data is not valid.
201+
#[inline]
196202
pub fn remove_checksum<Ck: Checksum>(self) -> CheckedHrpstring<'s> {
197203
let data_len = self.data.len() - Ck::CHECKSUM_LENGTH;
198204

@@ -237,24 +243,28 @@ impl<'s> CheckedHrpstring<'s> {
237243
/// If you are validating the checksum multiple times consider using [`UncheckedHrpstring`].
238244
///
239245
/// This is equivalent to `UncheckedHrpstring::new().validate_and_remove_checksum::<CK>()`.
246+
#[inline]
240247
pub fn new<Ck: Checksum>(s: &'s str) -> Result<Self, CheckedHrpstringError> {
241248
let unchecked = UncheckedHrpstring::new(s)?;
242249
let checked = unchecked.validate_and_remove_checksum::<Ck>()?;
243250
Ok(checked)
244251
}
245252

246253
/// Returns the human-readable part.
254+
#[inline]
247255
pub fn hrp(&self) -> Hrp { self.hrp }
248256

249257
/// Returns an iterator that yields the data part of the parsed bech32 encoded string.
250258
///
251259
/// Converts the ASCII bytes representing field elements to the respective field elements, then
252260
/// converts the stream of field elements to a stream of bytes.
261+
#[inline]
253262
pub fn byte_iter(&self) -> ByteIter {
254263
ByteIter { iter: AsciiToFe32Iter { iter: self.data.iter().copied() }.fes_to_bytes() }
255264
}
256265

257266
/// Converts this type to a [`SegwitHrpstring`] after validating the witness and HRP.
267+
#[inline]
258268
pub fn validate_segwit(mut self) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError> {
259269
if self.data.is_empty() {
260270
return Err(SegwitHrpstringError::MissingWitnessVersion);
@@ -362,6 +372,7 @@ impl<'s> SegwitHrpstring<'s> {
362372
///
363373
/// NOTE: We do not enforce any restrictions on the HRP, use [`SegwitHrpstring::has_valid_hrp`]
364374
/// to get strict BIP conformance (also [`Hrp::is_valid_on_mainnet`] and friends).
375+
#[inline]
365376
pub fn new(s: &'s str) -> Result<Self, SegwitHrpstringError> {
366377
let unchecked = UncheckedHrpstring::new(s)?;
367378

@@ -391,6 +402,7 @@ impl<'s> SegwitHrpstring<'s> {
391402
///
392403
/// [BIP-173]: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
393404
/// [BIP-350]: https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
405+
#[inline]
394406
pub fn new_bech32(s: &'s str) -> Result<Self, SegwitHrpstringError> {
395407
let unchecked = UncheckedHrpstring::new(s)?;
396408

@@ -409,12 +421,15 @@ impl<'s> SegwitHrpstring<'s> {
409421
/// BIP-173 requires that the HRP is "bc" or "tb" but software in the Bitcoin ecosystem uses
410422
/// other HRPs, specifically "bcrt" for regtest addresses. We provide this function in order to
411423
/// be BIP-173 compliant but their are no restrictions on the HRP of [`SegwitHrpstring`].
424+
#[inline]
412425
pub fn has_valid_hrp(&self) -> bool { self.hrp().is_valid_segwit() }
413426

414427
/// Returns the human-readable part.
428+
#[inline]
415429
pub fn hrp(&self) -> Hrp { self.hrp }
416430

417431
/// Returns the witness version.
432+
#[inline]
418433
pub fn witness_version(&self) -> Fe32 { self.witness_version }
419434

420435
/// Returns an iterator that yields the data part, excluding the witness version, of the parsed
@@ -424,6 +439,7 @@ impl<'s> SegwitHrpstring<'s> {
424439
/// converts the stream of field elements to a stream of bytes.
425440
///
426441
/// Use `self.witness_version()` to get the witness version.
442+
#[inline]
427443
pub fn byte_iter(&self) -> ByteIter {
428444
ByteIter { iter: AsciiToFe32Iter { iter: self.data.iter().copied() }.fes_to_bytes() }
429445
}
@@ -472,11 +488,14 @@ pub struct ByteIter<'s> {
472488

473489
impl<'s> Iterator for ByteIter<'s> {
474490
type Item = u8;
491+
#[inline]
475492
fn next(&mut self) -> Option<u8> { self.iter.next() }
493+
#[inline]
476494
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
477495
}
478496

479497
impl<'s> ExactSizeIterator for ByteIter<'s> {
498+
#[inline]
480499
fn len(&self) -> usize { self.iter.len() }
481500
}
482501

@@ -487,7 +506,9 @@ pub struct Fe32Iter<'s> {
487506

488507
impl<'s> Iterator for Fe32Iter<'s> {
489508
type Item = Fe32;
509+
#[inline]
490510
fn next(&mut self) -> Option<Fe32> { self.iter.next() }
511+
#[inline]
491512
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
492513
}
493514

@@ -507,7 +528,9 @@ where
507528
I: Iterator<Item = u8>,
508529
{
509530
type Item = Fe32;
531+
#[inline]
510532
fn next(&mut self) -> Option<Fe32> { self.iter.next().map(Fe32::from_char_unchecked) }
533+
#[inline]
511534
fn size_hint(&self) -> (usize, Option<usize>) {
512535
// Each ASCII character is an fe32 so iterators are the same size.
513536
self.iter.size_hint()
@@ -518,6 +541,7 @@ impl<I> ExactSizeIterator for AsciiToFe32Iter<I>
518541
where
519542
I: Iterator<Item = u8> + ExactSizeIterator,
520543
{
544+
#[inline]
521545
fn len(&self) -> usize { self.iter.len() }
522546
}
523547

src/primitives/encode.rs

+13
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,22 @@ where
9090
Ck: Checksum,
9191
{
9292
/// Constructs a new bech32 encoder.
93+
#[inline]
9394
pub fn new(data: I, hrp: &'hrp Hrp) -> Self {
9495
Self { data, hrp, witness_version: None, marker: PhantomData::<Ck> }
9596
}
9697

9798
/// Adds `witness_version` to the encoder (as first byte of encoded data).
9899
///
99100
/// Note, caller to guarantee that witness version is within valid range (0-16).
101+
#[inline]
100102
pub fn with_witness_version(mut self, witness_version: Fe32) -> Self {
101103
self.witness_version = Some(witness_version);
102104
self
103105
}
104106

105107
/// Returns an iterator that yields the bech32 encoded address as field ASCII characters.
108+
#[inline]
106109
pub fn chars(self) -> CharIter<'hrp, I, Ck> {
107110
let witver_iter = WitnessVersionIter::new(self.witness_version, self.data);
108111
CharIter::new(self.hrp, witver_iter)
@@ -111,6 +114,7 @@ where
111114
/// Returns an iterator that yields the field elements that go into the checksum, as well as the checksum at the end.
112115
///
113116
/// Each field element yielded has been input into the checksum algorithm (including the HRP as it is fed into the algorithm).
117+
#[inline]
114118
pub fn fes(self) -> Fe32Iter<'hrp, I, Ck> {
115119
let witver_iter = WitnessVersionIter::new(self.witness_version, self.data);
116120
Fe32Iter::new(self.hrp, witver_iter)
@@ -133,6 +137,7 @@ where
133137
I: Iterator<Item = Fe32>,
134138
{
135139
/// Creates a [`WitnessVersionIter`].
140+
#[inline]
136141
pub fn new(witness_version: Option<Fe32>, iter: I) -> Self { Self { witness_version, iter } }
137142
}
138143

@@ -142,8 +147,10 @@ where
142147
{
143148
type Item = Fe32;
144149

150+
#[inline]
145151
fn next(&mut self) -> Option<Fe32> { self.witness_version.take().or_else(|| self.iter.next()) }
146152

153+
#[inline]
147154
fn size_hint(&self) -> (usize, Option<usize>) {
148155
let (min, max) = self.iter.size_hint();
149156
match self.witness_version {
@@ -174,6 +181,7 @@ where
174181
Ck: Checksum,
175182
{
176183
/// Adapts the `Fe32Iter` iterator to yield characters representing the bech32 encoding.
184+
#[inline]
177185
pub fn new(hrp: &'hrp Hrp, data: WitnessVersionIter<I>) -> Self {
178186
let checksummed = Checksummed::new_hrp(hrp, data);
179187
Self { hrp_iter: Some(hrp.lowercase_char_iter()), checksummed }
@@ -187,6 +195,7 @@ where
187195
{
188196
type Item = char;
189197

198+
#[inline]
190199
fn next(&mut self) -> Option<char> {
191200
if let Some(ref mut hrp_iter) = self.hrp_iter {
192201
match hrp_iter.next() {
@@ -201,6 +210,7 @@ where
201210
self.checksummed.next().map(|fe| fe.to_char())
202211
}
203212

213+
#[inline]
204214
fn size_hint(&self) -> (usize, Option<usize>) {
205215
match &self.hrp_iter {
206216
// We have yielded the hrp and separator already.
@@ -245,6 +255,7 @@ where
245255
Ck: Checksum,
246256
{
247257
/// Creates a [`Fe32Iter`] which yields all the field elements which go into the checksum algorithm.
258+
#[inline]
248259
pub fn new(hrp: &'hrp Hrp, data: WitnessVersionIter<I>) -> Self {
249260
let hrp_iter = HrpFe32Iter::new(hrp);
250261
let checksummed = Checksummed::new_hrp(hrp, data);
@@ -258,6 +269,7 @@ where
258269
Ck: Checksum,
259270
{
260271
type Item = Fe32;
272+
#[inline]
261273
fn next(&mut self) -> Option<Fe32> {
262274
if let Some(ref mut hrp_iter) = &mut self.hrp_iter {
263275
match hrp_iter.next() {
@@ -268,6 +280,7 @@ where
268280
self.checksummed.next()
269281
}
270282

283+
#[inline]
271284
fn size_hint(&self) -> (usize, Option<usize>) {
272285
let hrp = match &self.hrp_iter {
273286
Some(hrp_iter) => hrp_iter.size_hint(),

0 commit comments

Comments
 (0)