Skip to content

Commit

Permalink
Merge pull request #39 from BP-WG/fix/38
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Aug 28, 2024
2 parents 04a7a2b + e1df0fc commit c3d2b8f
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions derive/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ pub trait Idx: IdxBase {
/// Constructs index from a given child number.
///
/// Child number is always a value in range of `0..`[`HARDENED_INDEX_BOUNDARY`]
fn from_child_number(no: impl Into<u16>) -> Self;
fn from_child_number(child_no: impl Into<u16>) -> Self;

/// Constructs index from a given child number.
///
/// Child number is always a value in range of `0..`[`HARDENED_INDEX_BOUNDARY`]
fn try_from_child_number(index: impl Into<u32>) -> Result<Self, IndexError>;
fn try_from_child_number(child_no: impl Into<u32>) -> Result<Self, IndexError>;

/// Constructs derivation path segment with specific derivation value, which
/// for normal indexes must lie in range `0..`[`HARDENED_INDEX_BOUNDARY`]
/// and for hardened in range of [`HARDENED_INDEX_BOUNDARY`]`..=u32::MAX`
fn try_from_index(value: u32) -> Result<Self, IndexError>;
fn try_from_index(index: u32) -> Result<Self, IndexError>;

fn to_be_bytes(&self) -> [u8; 4] { self.index().to_be_bytes() }

Expand Down Expand Up @@ -313,11 +313,11 @@ impl Idx for NormalIndex {
const MAX: Self = Self(HARDENED_INDEX_BOUNDARY - 1);

#[inline]
fn from_child_number(index: impl Into<u16>) -> Self { Self(index.into() as u32) }
fn from_child_number(child_no: impl Into<u16>) -> Self { Self(child_no.into() as u32) }

#[inline]
fn try_from_child_number(index: impl Into<u32>) -> Result<Self, IndexError> {
let index = index.into();
fn try_from_child_number(child_no: impl Into<u32>) -> Result<Self, IndexError> {
let index = child_no.into();
if index >= HARDENED_INDEX_BOUNDARY {
Err(IndexError {
what: "child number",
Expand All @@ -331,8 +331,8 @@ impl Idx for NormalIndex {
}

#[inline]
fn try_from_index(value: u32) -> Result<Self, IndexError> {
Self::try_from_child_number(value).map_err(|mut err| {
fn try_from_index(index: u32) -> Result<Self, IndexError> {
Self::try_from_child_number(index).map_err(|mut err| {
err.what = "index";
err
})
Expand Down Expand Up @@ -375,7 +375,7 @@ impl FromStr for NormalIndex {
)]
#[display("{0}h", alt = "{0}'")]
pub struct HardenedIndex(
/// The inner index value; always reduced by [`HARDENED_INDEX_BOUNDARY`]
/// The inner child number value; always reduced by [`HARDENED_INDEX_BOUNDARY`]
#[from(u8)]
#[from(u16)]
pub(crate) u32,
Expand Down Expand Up @@ -407,8 +407,7 @@ impl IdxBase for HardenedIndex {
#[inline]
fn child_number(&self) -> u32 { self.0 }

/// Returns hardened index number offset by [`HARDENED_INDEX_BOUNDARY`]
/// (i.e. zero-based).
/// Returns hardened index number offset by [`HARDENED_INDEX_BOUNDARY`].
#[inline]
fn index(&self) -> u32 { self.0 + HARDENED_INDEX_BOUNDARY }

Expand All @@ -417,11 +416,11 @@ impl IdxBase for HardenedIndex {
}

impl Idx for HardenedIndex {
const ZERO: Self = Self(HARDENED_INDEX_BOUNDARY);
const ZERO: Self = Self(0);

const ONE: Self = Self(1);

const MAX: Self = Self(u32::MAX);
const MAX: Self = Self(HARDENED_INDEX_BOUNDARY - 1);

#[inline]
fn from_child_number(child_no: impl Into<u16>) -> Self { Self(child_no.into() as u32) }
Expand All @@ -442,16 +441,17 @@ impl Idx for HardenedIndex {
}

#[inline]
fn try_from_index(child_no: u32) -> Result<Self, IndexError> {
if child_no < HARDENED_INDEX_BOUNDARY {
return Ok(Self(child_no));
fn try_from_index(index: u32) -> Result<Self, IndexError> {
if index < HARDENED_INDEX_BOUNDARY {
Err(IndexError {
what: "index",
invalid: index,
start: HARDENED_INDEX_BOUNDARY,
end: u32::MAX,
})
} else {
Ok(Self(index - HARDENED_INDEX_BOUNDARY))
}
Self::try_from_child_number(child_no - HARDENED_INDEX_BOUNDARY).map_err(|_| IndexError {
what: "index",
invalid: child_no,
start: HARDENED_INDEX_BOUNDARY,
end: u32::MAX,
})
}

#[inline]
Expand Down

0 comments on commit c3d2b8f

Please sign in to comment.