Repository navigation
refactor: abstract EDDSA backend types behind newtypes, switch to dash-pkc
#1056
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0002c5d
test(dash): use an asymmetric vector for the bincode layout check
kwvg 242c8a7
test(dash): pin the persisted byte order of the platform node id
kwvg ea4dd7d
build(deps): pin `dash-{types,pkc}` to revision at workspace
kwvg 16ed2b3
refactor!: isolate the backend's EDDSA public key type with a newtype
kwvg 823cbf9
refactor!: isolate the backend's EDDSA secret key type with a newtype
kwvg e014ee0
refactor!: holster Ed22519 public key hash definition as `EddsaPkHash`
kwvg efb8a85
refactor(dash): reap `platform_node_id`, retain as `EddsaPkHash` alias
kwvg 673b9c9
feat(crypto)!: switch EDDSA backend to `dash-pkc`
kwvg 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
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,188 @@ | ||
| // | ||
| // This file is a part of rust-dashcore. | ||
| // SPDX-License-Identifier: CC0-1.0 | ||
| // See the accompanying file LICENSE or https://creativecommons.org/publicdomain/zero/1.0 | ||
| // | ||
|
|
||
| //! Ed25519 keys for Platform node identity. | ||
|
|
||
| #[cfg(feature = "eddsa")] | ||
| use dash_pkc::eddsa::{ | ||
| EddsaPkBytes as PkcPkBytes, EddsaPublicKey as PkcPublicKey, EddsaSecretKey as PkcSecretKey, | ||
| }; | ||
| #[cfg(feature = "eddsa")] | ||
| use dash_types::Hashable; | ||
| use dash_types::{make_bytes, make_sbytes}; | ||
| #[cfg(feature = "eddsa")] | ||
| use thiserror::Error as ThisError; | ||
|
|
||
| /// Raw Ed25519 public key length. | ||
| pub const EDDSA_PK_LEN: usize = 32; | ||
|
|
||
| /// Ed25519 public key hash length. | ||
| pub const EDDSA_PK_HASH_LEN: usize = 20; | ||
|
|
||
| /// Raw Ed25519 secret key (seed) length. | ||
| pub const EDDSA_SK_LEN: usize = 32; | ||
|
|
||
| /// Errors produced by Ed25519 operations. | ||
| #[cfg(feature = "eddsa")] | ||
| #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, ThisError)] | ||
| pub enum EddsaError { | ||
| /// Public key bytes are not a usable curve point. | ||
| #[error("Invalid Ed25519 public key: {0}")] | ||
| InvalidPublicKey(String), | ||
| } | ||
|
|
||
| make_bytes! { | ||
| /// Ed25519 public key hash (20 bytes). | ||
| EddsaPkHash, EDDSA_PK_HASH_LEN, rev | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl EddsaPkHash { | ||
| /// Wraps the canonical (i.e. Tenderdash) order bytes. | ||
| pub fn from_canonical_bytes(mut bytes: [u8; EDDSA_PK_HASH_LEN]) -> Self { | ||
| bytes.reverse(); | ||
| Self::from_bytes(bytes) | ||
| } | ||
|
|
||
| /// Copies out the canonical (i.e. Tenderdash) order bytes. | ||
| pub fn to_canonical_bytes(self) -> [u8; EDDSA_PK_HASH_LEN] { | ||
| let mut bytes = self.to_bytes(); | ||
| bytes.reverse(); | ||
| bytes | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| impl bincode::Encode for EddsaPkHash { | ||
| fn encode<E: bincode::enc::Encoder>( | ||
| &self, | ||
| encoder: &mut E, | ||
| ) -> Result<(), bincode::error::EncodeError> { | ||
| self.to_canonical_bytes().encode(encoder) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| impl<C> bincode::Decode<C> for EddsaPkHash { | ||
| fn decode<D: bincode::de::Decoder<Context = C>>( | ||
| decoder: &mut D, | ||
| ) -> Result<Self, bincode::error::DecodeError> { | ||
| Ok(Self::from_canonical_bytes(<[u8; EDDSA_PK_HASH_LEN]>::decode(decoder)?)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "bincode")] | ||
| impl<'de, C> bincode::BorrowDecode<'de, C> for EddsaPkHash { | ||
| fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = C>>( | ||
| decoder: &mut D, | ||
| ) -> Result<Self, bincode::error::DecodeError> { | ||
| Ok(Self::from_canonical_bytes(<[u8; EDDSA_PK_HASH_LEN]>::borrow_decode(decoder)?)) | ||
| } | ||
| } | ||
|
|
||
| make_bytes! { | ||
| /// Ed25519 public key (32 bytes, unvalidated). | ||
| EddsaPkBytes, EDDSA_PK_LEN | ||
| } | ||
|
|
||
| #[cfg(feature = "eddsa")] | ||
| impl EddsaPkBytes { | ||
| /// Checks these bytes are a usable curve point. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns `InvalidPublicKey` when the bytes are not on the curve. | ||
| pub fn validate(&self) -> Result<(), EddsaError> { | ||
| PkcPublicKey::from_bytes(self.as_bytes()) | ||
| .map(|_| ()) | ||
| .map_err(|e| EddsaError::InvalidPublicKey(e.to_string())) | ||
| } | ||
|
|
||
| /// The CometBFT hash of the public key. | ||
| pub fn hash(&self) -> EddsaPkHash { | ||
| EddsaPkHash::from_bytes( | ||
| *Hashable::hash(&PkcPkBytes::from_bytes(*self.as_bytes())).as_bytes(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| make_sbytes! { | ||
| /// Ed25519 secret key seed (32 bytes). | ||
| EddsaSkBytes, EDDSA_SK_LEN | ||
| } | ||
|
|
||
| #[cfg(feature = "eddsa")] | ||
| impl EddsaSkBytes { | ||
| /// Derives the corresponding public key. | ||
| pub fn public_key(&self) -> EddsaPkBytes { | ||
| EddsaPkBytes::from_bytes(PkcSecretKey::from_bytes(self.as_bytes()).public_key().to_bytes()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// Persisted masternode-list snapshots were written while the platform | ||
| /// node id was typed as `PubkeyHash`; the bincode layout must stay | ||
| /// identical so old snapshots keep decoding. | ||
| #[cfg(feature = "bincode")] | ||
| #[test] | ||
| fn bincode_layout_matches_pubkey_hash() { | ||
| use dashcore_hashes::Hash as _; | ||
|
|
||
| let bytes = [ | ||
| 0x8b, 0xaa, 0xdf, 0xf0, 0x0d, 0x8b, 0xaa, 0xdf, 0xf0, 0x0d, 0x8b, 0xaa, 0xdf, 0xf0, | ||
| 0x0d, 0x8b, 0xaa, 0xdf, 0xf0, 0x0d, | ||
| ]; | ||
| let config = bincode::config::standard(); | ||
| let hash_bytes = bincode::encode_to_vec(EddsaPkHash::from_canonical_bytes(bytes), config) | ||
| .expect("encode hash"); | ||
| let pubkey_hash_bytes = | ||
| bincode::encode_to_vec(crate::key::PubkeyHash::from_byte_array(bytes), config) | ||
| .expect("encode pubkey hash"); | ||
| assert_eq!(hash_bytes, pubkey_hash_bytes); | ||
| assert_eq!(hash_bytes, bytes, "the canonical order is the persisted order"); | ||
|
|
||
| let (decoded, _): (EddsaPkHash, _) = | ||
| bincode::decode_from_slice(&hash_bytes, config).expect("decode hash"); | ||
| assert_eq!(decoded.to_canonical_bytes(), bytes); | ||
| } | ||
|
|
||
| #[test] | ||
| fn hex_display_and_parse_round_trip() { | ||
| let hex = "4cd2ca50b36e0a2bb1b6b29da140448b47eeb7a1"; | ||
| let hash: EddsaPkHash = hex.parse().expect("parse hash hex"); | ||
| assert_eq!(hash.to_string(), hex); | ||
| assert_eq!(format!("{:?}", hash), format!("EddsaPkHash({})", hex)); | ||
| assert!("abcd".parse::<EddsaPkHash>().is_err(), "wrong-length hex must fail"); | ||
| assert!("zz".repeat(20).parse::<EddsaPkHash>().is_err(), "non-hex input must fail"); | ||
| } | ||
|
|
||
| #[cfg(feature = "serde")] | ||
| #[test] | ||
| fn serde_json_round_trip_as_hex_string() { | ||
| let hash = EddsaPkHash::from_bytes([0x11; 20]); | ||
| let json = serde_json::to_string(&hash).expect("serialize hash"); | ||
| assert_eq!(json, format!("\"{}\"", "11".repeat(20))); | ||
| let back: EddsaPkHash = serde_json::from_str(&json).expect("deserialize hash"); | ||
| assert_eq!(back, hash); | ||
| } | ||
|
|
||
| #[cfg(feature = "eddsa")] | ||
| #[test] | ||
| fn hash_is_truncated_sha256() { | ||
| use dashcore_hashes::{sha256, Hash}; | ||
|
|
||
| let public_key = [7u8; 32]; | ||
| let digest = sha256::Hash::hash(&public_key); | ||
| // `EddsaPkHash` holds the wire order, which is the byte-reversal of | ||
| // the canonical form the digest is read in. | ||
| let mut canonical = *EddsaPkBytes::from_bytes(public_key).hash().as_bytes(); | ||
| canonical.reverse(); | ||
|
|
||
| assert_eq!(canonical[..], digest.to_byte_array()[..20]); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.