pub fn child(&self, index: ChildIndex) -> Result<ExtSecretKey, ExtSecretKeyError> {
        // Unwrap is fine due to `ChainCode` type having fixed length of 32.
        #[allow(clippy::unwrap_used)]
        let mut mac = HmacSha512::new_from_slice(&self.chain_code).unwrap();
        match index {
            ChildIndex::Hardened(_) => {
                mac.update(&[0u8]);
                mac.update(&self.secret_key_bytes());
            }
            ChildIndex::Normal(_) => mac.update(&self.public_image_bytes()?),
        }
        mac.update(&index.to_bits().to_be_bytes());
        let mac_bytes = mac.finalize().into_bytes();
        let mut secret_key_bytes = [0; SecretKeyBytes::LEN];
        secret_key_bytes.copy_from_slice(&mac_bytes[..32]);
        if let Some(dlog_prover) = DlogProverInput::from_bytes(&secret_key_bytes) {
            // parse256(IL) + kpar (mod n).
            // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
            let child_secret_key: DlogProverInput = dlog_prover.w.add(&self.private_input.w).into();
            if child_secret_key.is_zero() {
                // ki == 0 case of:
                // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one
                // > should proceed with the next value for i
                // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
                self.child(index.next()?)
            } else {
                let mut chain_code = [0; ChainCode::LEN];
                chain_code.copy_from_slice(&mac_bytes[32..]);
                ExtSecretKey::new(
                    child_secret_key.to_bytes(),
                    chain_code,
                    self.derivation_path.extend(index),
                )
            }
        } else {
            // not in range [0, modulus), thus repeat with next index value (BIP-32)
            // This is the 'parse256(IL) ≥ n' case of:
            // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one
            // > should proceed with the next value for i
            // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
            self.child(index.next()?)
        }
    }
/// Derive a child extended secret key using the provided index
Derive a child extended secret key using the provided index
Derive a child extended secret key using the provided index
pub fn child(&self, index: ChildIndex) -> Result<ExtSecretKey, ExtSecretKeyError> {
        // Unwrap is fine due to `ChainCode` type having fixed length of 32.
        #[allow(clippy::unwrap_used)]
        let mut mac = HmacSha512::new_from_slice(&self.chain_code).unwrap();
        match index {
            ChildIndex::Hardened(_) => {
                mac.update(&[0u8]);
                mac.update(&self.secret_key_bytes());
            }
            ChildIndex::Normal(_) => mac.update(&self.public_image_bytes()?),
        }
        mac.update(&index.to_bits().to_be_bytes());
        let mac_bytes = mac.finalize().into_bytes();
        let mut secret_key_bytes = [0; SecretKeyBytes::LEN];
        secret_key_bytes.copy_from_slice(&mac_bytes[..32]);
        if let Some(dlog_prover) = DlogProverInput::from_bytes(&secret_key_bytes) {
            // parse256(IL) + kpar (mod n).
            // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
            let child_secret_key: DlogProverInput = dlog_prover.w.add(&self.private_input.w).into();
            if child_secret_key.is_zero() {
                // ki == 0 case of:
                // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one
                // > should proceed with the next value for i
                // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
                self.child(index.next()?)
            } else {
                let mut chain_code = [0; ChainCode::LEN];
                chain_code.copy_from_slice(&mac_bytes[32..]);
                ExtSecretKey::new(
                    child_secret_key.to_bytes(),
                    chain_code,
                    self.derivation_path.extend(index),
                )
            }
        } else {
            // not in range [0, modulus), thus repeat with next index value (BIP-32)
            // This is the 'parse256(IL) ≥ n' case of:
            // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one
            // > should proceed with the next value for i
            // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
            self.child(index.next()?)
        }
    }
pub fn child ( & self , index : ChildIndex ) -> Result < ExtSecretKey , ExtSecretKeyError > { // Unwrap is fine due to `ChainCode` type having fixed length of 32. # [ allow ( clippy unwrap_used ) ] let mut mac = HmacSha512 :: new_from_slice ( & self . chain_code ) . unwrap ( ) ; match index { ChildIndex :: Hardened ( _ ) => { mac . update ( & [ 0u8 ] ) ; mac . update ( & self . secret_key_bytes ( ) ) ; } ChildIndex :: Normal ( _ ) => mac . update ( & self . public_image_bytes ( ) ? ) , } mac . update ( & index . to_bits ( ) . to_be_bytes ( ) ) ; let mac_bytes = mac . finalize ( ) . into_bytes ( ) ; let mut secret_key_bytes = [ 0 ; SecretKeyBytes :: LEN ] ; secret_key_bytes . copy_from_slice ( & mac_bytes [ .. 32 ] ) ; if let Some ( dlog_prover ) = DlogProverInput :: from_bytes ( & secret_key_bytes ) { // parse256(IL) + kpar (mod n). // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions let child_secret_key : DlogProverInput = dlog_prover . w . add ( & self . private_input . w ) . into ( ) ; if child_secret_key . is_zero ( ) { // ki == 0 case of: // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one // > should proceed with the next value for i // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions self . child ( index . next ( ) ? ) } else { let mut chain_code = [ 0 ; ChainCode :: LEN ] ; chain_code . copy_from_slice ( & mac_bytes [ 32 .. ] ) ; ExtSecretKey :: new ( child_secret_key . to_bytes ( ) , chain_code , self . derivation_path . extend ( index ) , ) } } else { // not in range [0, modulus), thus repeat with next index value (BIP-32) // This is the 'parse256(IL) ≥ n' case of: // > In case parse256(IL) ≥ n or ki = 0, the resulting key is invalid, and one // > should proceed with the next value for i // via https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions self . child ( index . next ( ) ? ) } }
Derive a child extended secret key using the provided index
Derive a child extended secret key using the provided index
&self
index
ChildIndex
&self
index
ChildIndex