wallet_gen/ed25519/
keys.rs

1/*
2 * ed25519/keys.rs
3 *
4 * Copyright 2018 Standard Mining
5 *
6 * Available to be used and modified under the terms of the MIT License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
11 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
12 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
13 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 */
15
16use super::prelude::*;
17use ed25519::crypto::{derive_pubkey, bn_to_vec32, sc_reduce32};
18use openssl::bn::BigNumContextRef;
19use std::ops::Deref;
20
21/// An ed25519 private key.
22#[derive(Debug, Clone)]
23#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24pub struct PrivateKey([u8; 32]);
25
26impl PrivateKey {
27    /// Generate a private key from the given seed. The byte data will be converted
28    /// to a valid private ed25519 key.
29    pub fn from_bytes(mut bytes: [u8; 32], ctx: &mut BigNumContextRef) -> Result<Self> {
30        let bn = sc_reduce32(&mut bytes, ctx)?;
31        let vec = bn_to_vec32(&bn);
32
33        {
34            let dest = &mut bytes[..];
35            dest.copy_from_slice(vec.as_slice());
36        }
37
38        Ok(PrivateKey(bytes))
39    }
40
41    /// Converts this private key into an array of its bytes
42    #[inline]
43    pub fn into_bytes(self) -> [u8; 32] { self.0 }
44
45    /// Gets a copy of this private key's bytes
46    #[inline]
47    pub fn to_bytes(&self) -> [u8; 32] { self.0 }
48
49    /// Gets a reference to the internally stored private key bytes
50    #[inline]
51    pub fn as_bytes(&self) -> &[u8; 32] { &self.0 }
52}
53
54impl Deref for PrivateKey {
55    type Target = [u8; 32];
56
57    #[inline]
58    fn deref(&self) -> &Self::Target { &self.0 }
59}
60
61impl AsRef<[u8]> for PrivateKey {
62    #[inline]
63    fn as_ref(&self) -> &[u8] { &self.0[..] }
64}
65
66/// An ed25519 public key.
67#[derive(Debug, Clone)]
68#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
69pub struct PublicKey([u8; 32]);
70
71impl PublicKey {
72    /// Calculates the ed25519 public key from the given private key.
73    pub fn from_private(priv_key: &PrivateKey, ctx: &mut BigNumContextRef) -> Result<Self> {
74        let mut bytes = priv_key.to_bytes();
75        derive_pubkey(&mut bytes, ctx)?;
76        Ok(PublicKey(bytes))
77    }
78
79    /// Converts this public key into an array of its bytes
80    #[inline]
81    pub fn into_bytes(self) -> [u8; 32] { self.0 }
82
83    /// Gets a copy of this public key's bytes
84    #[inline]
85    pub fn to_bytes(&self) -> [u8; 32] { self.0 }
86
87    /// Gets a reference to the internally stored public key bytes
88    #[inline]
89    pub fn as_bytes(&self) -> &[u8; 32] { &self.0 }
90}
91
92impl Deref for PublicKey {
93    type Target = [u8; 32];
94
95    #[inline]
96    fn deref(&self) -> &Self::Target { &self.0 }
97}
98
99impl AsRef<[u8]> for PublicKey {
100    #[inline]
101    fn as_ref(&self) -> &[u8] { &self.0[..] }
102}
103
104/// A ed25519 keypair, containing both a private and public part.
105#[derive(Debug, Clone)]
106#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
107pub struct Keypair {
108    /// The public key.
109    pub public: PublicKey,
110
111    /// The private key.
112    pub private: PrivateKey,
113}