Browse Source

Make constants publicly available

develop
Silvestr Predko 2 years ago
parent
commit
fe64dfd024
3 changed files with 33 additions and 28 deletions
  1. 15
    13
      common-api/src/crypto/ed25519.rs
  2. 13
    13
      common-api/src/crypto/key_exchange.rs
  3. 5
    2
      common-api/src/crypto/mod.rs

+ 15
- 13
common-api/src/crypto/ed25519.rs View File

@@ -3,10 +3,7 @@
3 3
 //! Used Dalek cryptography, and implemented [`Borsh`](https://borsh.io/) serialization for them
4 4
 
5 5
 use borsh::{BorshDeserialize, BorshSerialize};
6
-use ed25519_dalek::{
7
-    ExpandedSecretKey, PublicKey, SecretKey, Signature, Verifier, PUBLIC_KEY_LENGTH,
8
-    SECRET_KEY_LENGTH, SIGNATURE_LENGTH,
9
-};
6
+use ed25519_dalek::{ExpandedSecretKey, PublicKey, SecretKey, Signature, Verifier};
10 7
 use itertools::Itertools;
11 8
 use serde::{Deserialize, Serialize};
12 9
 use std::{
@@ -17,6 +14,11 @@ use std::{
17 14
 
18 15
 use super::{split_encoded_str, Error, Key, Result, ED25519};
19 16
 
17
+pub use ed25519_dalek::{
18
+    PUBLIC_KEY_LENGTH as ED25519_PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH as ED25519_SECRET_KEY_LENGTH,
19
+    SIGNATURE_LENGTH as ED25519_SIGNATURE_LENGTH,
20
+};
21
+
20 22
 #[derive(Copy, Clone, Default, Eq, PartialEq)]
21 23
 pub struct Ed25519PublicKey(PublicKey);
22 24
 
@@ -28,16 +30,16 @@ impl Ed25519PublicKey {
28 30
     }
29 31
 
30 32
     #[inline]
31
-    pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] {
33
+    pub fn as_bytes(&self) -> &[u8; ED25519_PUBLIC_KEY_LENGTH] {
32 34
         self.0.as_bytes()
33 35
     }
34 36
 }
35 37
 
36
-impl Key<PUBLIC_KEY_LENGTH> for Ed25519PublicKey {
38
+impl Key<ED25519_PUBLIC_KEY_LENGTH> for Ed25519PublicKey {
37 39
     const KEY_TYPE: &'static str = ED25519;
38 40
 
39 41
     #[inline]
40
-    fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
42
+    fn to_bytes(&self) -> [u8; ED25519_PUBLIC_KEY_LENGTH] {
41 43
         self.0.to_bytes()
42 44
     }
43 45
 
@@ -115,22 +117,22 @@ impl Ed25519SecretKey {
115 117
                 Error::from_string::<Ed25519SecretKey>(bs58_encoded.to_owned(), err.to_string())
116 118
             })?
117 119
             .into_iter()
118
-            .take(SECRET_KEY_LENGTH)
120
+            .take(ED25519_SECRET_KEY_LENGTH)
119 121
             .collect_vec();
120 122
         Self::try_from_bytes(&expanded_key_bytes)
121 123
     }
122 124
 
123 125
     #[inline]
124
-    pub fn as_bytes(&self) -> &[u8; PUBLIC_KEY_LENGTH] {
126
+    pub fn as_bytes(&self) -> &[u8; ED25519_PUBLIC_KEY_LENGTH] {
125 127
         self.0.as_bytes()
126 128
     }
127 129
 }
128 130
 
129
-impl Key<SECRET_KEY_LENGTH> for Ed25519SecretKey {
131
+impl Key<ED25519_SECRET_KEY_LENGTH> for Ed25519SecretKey {
130 132
     const KEY_TYPE: &'static str = ED25519;
131 133
 
132 134
     #[inline]
133
-    fn to_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
135
+    fn to_bytes(&self) -> [u8; ED25519_SECRET_KEY_LENGTH] {
134 136
         self.0.to_bytes()
135 137
     }
136 138
 
@@ -157,11 +159,11 @@ impl BorshSerialize for Ed25519SecretKey {
157 159
 #[derive(Copy, Clone, Eq, PartialEq)]
158 160
 pub struct Ed25519Signature(Signature);
159 161
 
160
-impl Key<SIGNATURE_LENGTH> for Ed25519Signature {
162
+impl Key<ED25519_SIGNATURE_LENGTH> for Ed25519Signature {
161 163
     const KEY_TYPE: &'static str = ED25519;
162 164
 
163 165
     #[inline]
164
-    fn to_bytes(&self) -> [u8; SIGNATURE_LENGTH] {
166
+    fn to_bytes(&self) -> [u8; ED25519_SIGNATURE_LENGTH] {
165 167
         self.0.to_bytes()
166 168
     }
167 169
 

+ 13
- 13
common-api/src/crypto/key_exchange.rs View File

@@ -11,8 +11,8 @@ use x25519_dalek::{PublicKey, StaticSecret};
11 11
 
12 12
 use super::{Error, Key, Result, X25519};
13 13
 
14
-const PUBLIC_LENGTH: usize = 32_usize;
15
-const SECRET_LENGTH: usize = 32_usize;
14
+pub const PUBLIC_KEY_LENGTH: usize = 32_usize;
15
+pub const SECRET_KEY_LENGTH: usize = 32_usize;
16 16
 
17 17
 pub struct Secret(StaticSecret);
18 18
 
@@ -53,52 +53,52 @@ impl Display for Public {
53 53
     }
54 54
 }
55 55
 
56
-impl Key<SECRET_LENGTH> for Secret {
56
+impl Key<SECRET_KEY_LENGTH> for Secret {
57 57
     const KEY_TYPE: &'static str = X25519;
58 58
 
59 59
     #[inline]
60
-    fn to_bytes(&self) -> [u8; SECRET_LENGTH] {
60
+    fn to_bytes(&self) -> [u8; SECRET_KEY_LENGTH] {
61 61
         self.0.to_bytes()
62 62
     }
63 63
 
64 64
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
65
-        if buf.len() != SECRET_LENGTH {
65
+        if buf.len() != SECRET_KEY_LENGTH {
66 66
             return Err(Error::from_bytes::<Public>(
67 67
                 buf,
68 68
                 format!(
69
-                    "input buffer size \"{}\" not equal to secret key size \"{SECRET_LENGTH}\"",
69
+                    "input buffer size \"{}\" not equal to secret key size \"{SECRET_KEY_LENGTH}\"",
70 70
                     buf.len()
71 71
                 ),
72 72
             ));
73 73
         }
74 74
 
75
-        let mut temp_buf = [0_u8; SECRET_LENGTH];
75
+        let mut temp_buf = [0_u8; SECRET_KEY_LENGTH];
76 76
         temp_buf.copy_from_slice(buf);
77 77
 
78 78
         Ok(Self(StaticSecret::from(temp_buf)))
79 79
     }
80 80
 }
81 81
 
82
-impl Key<PUBLIC_LENGTH> for Public {
82
+impl Key<PUBLIC_KEY_LENGTH> for Public {
83 83
     const KEY_TYPE: &'static str = X25519;
84 84
 
85 85
     #[inline]
86
-    fn to_bytes(&self) -> [u8; PUBLIC_LENGTH] {
86
+    fn to_bytes(&self) -> [u8; PUBLIC_KEY_LENGTH] {
87 87
         self.0.to_bytes()
88 88
     }
89 89
 
90 90
     fn try_from_bytes(buf: &[u8]) -> Result<Self> {
91
-        if buf.len() != PUBLIC_LENGTH {
91
+        if buf.len() != PUBLIC_KEY_LENGTH {
92 92
             return Err(Error::from_bytes::<Public>(
93 93
                 buf,
94 94
                 format!(
95
-                    "input buffer size \"{}\" not equal to public key size \"{PUBLIC_LENGTH}\"",
95
+                    "input buffer size \"{}\" not equal to public key size \"{PUBLIC_KEY_LENGTH}\"",
96 96
                     buf.len()
97 97
                 ),
98 98
             ));
99 99
         }
100 100
 
101
-        let mut temp_buf = [0_u8; PUBLIC_LENGTH];
101
+        let mut temp_buf = [0_u8; PUBLIC_KEY_LENGTH];
102 102
         temp_buf.copy_from_slice(buf);
103 103
 
104 104
         Ok(Self(PublicKey::from(temp_buf)))
@@ -120,7 +120,7 @@ impl Secret {
120 120
     /// ## Returns
121 121
     /// Byte array with a shared secret key
122 122
     ///
123
-    pub fn exchange(&self, other_public: &Public) -> [u8; SECRET_LENGTH] {
123
+    pub fn exchange(&self, other_public: &Public) -> [u8; SECRET_KEY_LENGTH] {
124 124
         self.0.diffie_hellman(&other_public.0).to_bytes()
125 125
     }
126 126
 }

+ 5
- 2
common-api/src/crypto/mod.rs View File

@@ -38,8 +38,11 @@ pub mod ed25519;
38 38
 pub mod key_exchange;
39 39
 pub mod prelude {
40 40
     pub use super::{
41
-        ed25519::{Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, Keypair},
42
-        key_exchange::{Public, Secret},
41
+        ed25519::{
42
+            Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature, Keypair,
43
+            ED25519_PUBLIC_KEY_LENGTH, ED25519_SECRET_KEY_LENGTH, ED25519_SIGNATURE_LENGTH,
44
+        },
45
+        key_exchange::{Public, Secret, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH},
43 46
         Error, Key,
44 47
     };
45 48
 }

Loading…
Cancel
Save