Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to Rust's notion of
- Migrated from yanked `core2` library to `corez`
- `orchard::pczt::Bundle::extract` now takes its `self` argument by
reference instead of by value.
- `orchard::zip32::Error` has added variant `MaxDerivationDepth`

## [0.12.0] - 2025-12-05

Expand Down
22 changes: 21 additions & 1 deletion src/zip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum Error {
InvalidSpendingKey,
/// A child index in a derivation path exceeded 2^31
InvalidChildIndex(u32),
/// Derivation depth would exceed 255
MaxDerivationDepth,
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -203,6 +205,8 @@ impl ExtendedSpendingKey {
///
/// Discards index if it results in an invalid sk
fn derive_child(&self, index: ChildIndex) -> Result<Self, Error> {
let depth = self.depth.checked_add(1).ok_or(Error::MaxDerivationDepth)?;

let child_i = self.inner.derive_child(index);

let sk = SpendingKey::from_bytes(*child_i.parts().0);
Expand All @@ -213,7 +217,7 @@ impl ExtendedSpendingKey {
let fvk: FullViewingKey = self.into();

Ok(Self {
depth: self.depth + 1,
depth,
parent_fvk_tag: FvkFingerprint::from(&fvk).tag(),
child_index: KeyIndex::child(index),
inner: child_i,
Expand Down Expand Up @@ -246,6 +250,22 @@ mod tests {
assert!(xsk_5.is_ok());
}

#[test]
fn derive_child_depth_overflow() {
let seed = [0; 32];
let mut xsk = ExtendedSpendingKey::master(&seed).unwrap();

let i_5 = ChildIndex::hardened(5);
for _ in 0..255 {
xsk = xsk.derive_child(i_5).unwrap();
}
assert_eq!(xsk.depth, 255);
assert!(matches!(
xsk.derive_child(i_5),
Err(Error::MaxDerivationDepth)
));
}

#[test]
fn path() {
let seed = [0; 32];
Expand Down
Loading