-
Notifications
You must be signed in to change notification settings - Fork 5
feat(bip0032): add SLIP-0010 support with secp256k1/nist256p1/ed25519 #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # SLIP-0010 | ||
|
|
||
| This crate provides optional [SLIP-0010](https://github.com/satoshilabs/slips/blob/master/slip-0010.md) support behind the `slip10` feature. | ||
| It shares the same [`ExtendedPrivateKey`]/[`ExtendedPublicKey`] structure as [BIP-0032](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki), | ||
| but the derivation rules and supported curves differ. | ||
|
|
||
| ## Supported curves and features | ||
|
|
||
| | Curve | Feature | Backends | Hardened | Non-hardened (private) | Non-hardened (public) | Serialization | | ||
| | --------- | ---------------------------------------------------- | ----------------------------- | -------- | ---------------------- | --------------------- | ------------- | | ||
| | secp256k1 | `slip10` + (`k256` \| `secp256k1` \| `libsecp256k1`) | k256, secp256k1, libsecp256k1 | yes | yes | yes | no | | ||
| | nist256p1 | `slip10` + `p256` | p256 | yes | yes | yes | no | | ||
| | ed25519 | `slip10` + `ed25519-dalek` | ed25519-dalek | yes | no | no | no | | ||
|
|
||
| Note: SLIP-0010 does not define a standardized extended key serialization. | ||
| Only the BIP32 secp256k1 encoding (xpub/xprv) is supported for serialization. | ||
|
|
||
| ## Usage | ||
|
|
||
| Seed material is typically derived from a [BIP-0039](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic | ||
| (for example, via [bip0039](https://crates.io/crates/bip0039)). | ||
|
|
||
| ```rust | ||
| use bip0039::{Count, English, Mnemonic}; | ||
|
|
||
| let mnemonic = <Mnemonic<English>>::generate(Count::Words12); | ||
| let seed = mnemonic.to_seed(""); | ||
| ``` | ||
|
|
||
| The examples below assume the `seed` from above. | ||
|
|
||
| ### secp256k1 (non-hardened supported) | ||
|
|
||
| ```rust | ||
| use bip0032::{DerivationPath, ExtendedPrivateKey, curve::secp256k1::*}; | ||
| use bip0032::slip10::{Slip10MasterKey, Slip10NonHardenedDerivation}; | ||
|
|
||
| # let seed = [0u8; 64]; | ||
| let master = ExtendedPrivateKey::<Secp256k1Curve<K256Backend>>::new_slip10(&seed).unwrap(); | ||
| let path: DerivationPath = "m/0H/1".parse().unwrap(); | ||
| let child = Slip10NonHardenedDerivation::derive_slip10_path(&master, &path).unwrap(); | ||
| let public = child.public_key().to_bytes(); | ||
| ``` | ||
|
|
||
| ### nist256p1 (non-hardened supported) | ||
|
|
||
| ```rust | ||
| use bip0032::{DerivationPath, ExtendedPrivateKey, curve::nist256p1::*}; | ||
| use bip0032::slip10::{Slip10MasterKey, Slip10NonHardenedDerivation}; | ||
|
|
||
| # let seed = [0u8; 64]; | ||
| let master = ExtendedPrivateKey::<Nist256p1Curve<P256Backend>>::new_slip10(&seed).unwrap(); | ||
| let path: DerivationPath = "m/0H/1".parse().unwrap(); | ||
| let child = Slip10NonHardenedDerivation::derive_slip10_path(&master, &path).unwrap(); | ||
| let public = child.public_key().to_bytes(); | ||
| ``` | ||
|
|
||
| ### ed25519 (hardened only) | ||
|
|
||
| ```rust | ||
| use bip0032::{ExtendedPrivateKey, HardenedDerivationPath, curve::ed25519::*}; | ||
| use bip0032::slip10::{Slip10HardenedOnlyDerivation, Slip10MasterKey}; | ||
|
|
||
| # let seed = [0u8; 64]; | ||
| let master = ExtendedPrivateKey::<Ed25519Curve<Ed25519DalekBackend>>::new_slip10(&seed).unwrap(); | ||
| let path: HardenedDerivationPath = "m/0H/1H".parse().unwrap(); | ||
| let child = Slip10HardenedOnlyDerivation::derive_slip10_path(&master, &path).unwrap(); | ||
| let public = child.public_key().to_bytes(); | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - `Slip10NonHardenedDerivation` is implemented for both extended private and | ||
| extended public keys; hardened derivation is only available for private keys. | ||
| - Fingerprints are computed using Hash160 over the serialized public key bytes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| use ed25519_dalek::{SigningKey, VerifyingKey}; | ||
|
|
||
| use crate::curve::{CurveError, CurvePrivateKey, CurvePublicKey, ed25519::Ed25519Backend}; | ||
|
|
||
| /// Ed25519 backend powered by the [`ed25519-dalek`](https://github.com/dalek-cryptography/curve25519-dalek/tree/main/ed25519-dalek) crate. | ||
| pub struct Ed25519DalekBackend; | ||
|
|
||
| impl CurvePublicKey for VerifyingKey { | ||
| type Error = CurveError; | ||
| type Bytes = [u8; 33]; | ||
|
|
||
| fn from_bytes(bytes: &Self::Bytes) -> Result<Self, Self::Error> { | ||
| if bytes[0] != 0 { | ||
| return Err(CurveError::from("invalid ed25519 public key prefix")); | ||
| } | ||
|
|
||
| let mut raw = [0u8; 32]; | ||
| raw.copy_from_slice(&bytes[1..]); | ||
| VerifyingKey::from_bytes(&raw).map_err(CurveError::new) | ||
| } | ||
|
|
||
| fn to_bytes(&self) -> Self::Bytes { | ||
| let raw = self.to_bytes(); | ||
| let mut out = [0u8; 33]; | ||
| out[1..].copy_from_slice(&raw); | ||
| out | ||
| } | ||
| } | ||
|
|
||
| impl CurvePrivateKey for SigningKey { | ||
| type Error = CurveError; | ||
| type PublicKey = VerifyingKey; | ||
| type Bytes = [u8; 32]; | ||
|
|
||
| fn from_bytes(bytes: &Self::Bytes) -> Result<Self, Self::Error> { | ||
| Ok(SigningKey::from_bytes(bytes)) | ||
| } | ||
|
|
||
| fn to_bytes(&self) -> Self::Bytes { | ||
| self.to_bytes() | ||
| } | ||
|
|
||
| fn to_public(&self) -> Self::PublicKey { | ||
| self.verifying_key() | ||
| } | ||
| } | ||
|
|
||
| impl Ed25519Backend for Ed25519DalekBackend { | ||
| type PublicKey = VerifyingKey; | ||
| type PrivateKey = SigningKey; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //! Backend implementations for Ed25519. | ||
|
|
||
| use crate::curve::{CurvePrivateKey, CurvePublicKey}; | ||
|
|
||
| /// Ed25519 backend interface. | ||
| pub trait Ed25519Backend { | ||
| /// Backend-specific public key type. | ||
| type PublicKey: CurvePublicKey<Bytes = [u8; 33]>; | ||
| /// Backend-specific private key type. | ||
| type PrivateKey: CurvePrivateKey<Bytes = [u8; 32], PublicKey = Self::PublicKey>; | ||
| } | ||
|
|
||
| #[cfg(feature = "ed25519-dalek")] | ||
| mod ed25519_dalek; | ||
|
|
||
| #[cfg(feature = "ed25519-dalek")] | ||
| pub use self::ed25519_dalek::Ed25519DalekBackend; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //! Ed25519 curve implementation. | ||
|
|
||
| use core::marker::PhantomData; | ||
|
|
||
| use super::*; | ||
|
|
||
| mod backends; | ||
| pub use self::backends::*; | ||
|
|
||
| /// An Ed25519 curve parameterization for a specific backend. | ||
| pub struct Ed25519Curve<B>(PhantomData<B>); | ||
|
|
||
| impl<B: Ed25519Backend> Curve for Ed25519Curve<B> { | ||
| const HMAC_KEY: &'static [u8] = b"ed25519 seed"; | ||
|
|
||
| type PublicKey = <B as Ed25519Backend>::PublicKey; | ||
| type PrivateKey = <B as Ed25519Backend>::PrivateKey; | ||
| } | ||
|
|
||
| impl<B: Ed25519Backend> Slip10Curve for Ed25519Curve<B> {} | ||
|
|
||
| impl<B: Ed25519Backend> Slip10HardenedOnlyCurve for Ed25519Curve<B> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //! Curve-related error helpers. | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| use alloc::string::String; | ||
| use core::{error, fmt}; | ||
|
|
||
| use crate::error::{ErrorSource, IntoErrorSource}; | ||
|
|
||
| /// Common curve error type. | ||
| pub struct CurveError(ErrorSource); | ||
|
|
||
| impl CurveError { | ||
| /// Creates a curve error from a source error. | ||
| #[cfg(feature = "std")] | ||
| pub fn new<E>(error: E) -> Self | ||
| where | ||
| E: IntoErrorSource, | ||
| { | ||
| Self(error.into_error_source()) | ||
| } | ||
|
|
||
| /// Creates a curve error from a source error. | ||
| #[cfg(not(feature = "std"))] | ||
| pub fn new<E>(error: E) -> Self | ||
| where | ||
| E: fmt::Display + fmt::Debug + Send + Sync + 'static, | ||
| { | ||
| let error = anyhow::Error::msg(error); | ||
| Self(error.into_error_source()) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Debug for CurveError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&self.0, f) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for CurveError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(&self.0, f) | ||
| } | ||
| } | ||
|
|
||
| impl From<ErrorSource> for CurveError { | ||
| fn from(error: ErrorSource) -> Self { | ||
| Self(error) | ||
| } | ||
| } | ||
|
|
||
| impl From<String> for CurveError { | ||
| fn from(message: String) -> Self { | ||
| Self(ErrorSource::from(message)) | ||
| } | ||
| } | ||
|
|
||
| impl From<&'static str> for CurveError { | ||
| fn from(message: &'static str) -> Self { | ||
| Self(ErrorSource::from(message)) | ||
| } | ||
| } | ||
|
|
||
| impl error::Error for CurveError { | ||
| fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
| Some(self.0.as_error()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SLIP-0010 defines Ed25519 public keys as 32 raw bytes; introducing a 33-byte encoding with a
0x00prefix changes the public key serialization and therefore the parent fingerprint (hash160 over the serialized key) used throughout SLIP-0010 metadata. This will make derived fingerprints and public key bytes incompatible with standard SLIP-0010 vectors/implementations when users compare or interoperate across libraries.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-1-for-ed25519, it is known that the calculation of the parent key fingerprint uses a 0x00 prefix plus a 32-byte ed25519 public key as input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add
ed25519_pubkey_from_slip10_bytes/ed25519_pubkey_to_slip10_bytesand update docs about it