16
16
//!
17
17
18
18
use core:: ptr;
19
- use core:: ops:: { FnMut , Deref } ;
19
+ use core:: ops:: FnMut ;
20
20
21
21
use key:: { SecretKey , PublicKey } ;
22
22
use ffi:: { self , CPtr } ;
@@ -39,75 +39,33 @@ use secp256k1_sys::types::{c_int, c_uchar, c_void};
39
39
/// assert_eq!(sec1, sec2);
40
40
/// # }
41
41
// ```
42
- #[ derive( Copy , Clone ) ]
43
- pub struct SharedSecret {
44
- data : [ u8 ; 256 ] ,
45
- len : usize ,
46
- }
47
- impl_raw_debug ! ( SharedSecret ) ;
48
-
49
-
50
- // This implementes `From<N>` for all `[u8; N]` arrays from 128bits(16 byte) to 2048bits allowing known hash lengths.
51
- // Lower than 128 bits isn't resistant to collisions any more.
52
- impl_from_array_len ! ( SharedSecret , 256 , ( 16 20 28 32 48 64 96 128 256 ) ) ;
42
+ #[ derive( Copy , Clone , Debug , PartialEq ) ]
43
+ pub struct SharedSecret ( [ u8 ; 32 ] ) ;
53
44
54
45
impl SharedSecret {
55
46
56
47
/// Creates an empty `SharedSecret`.
57
48
pub ( crate ) fn empty ( ) -> SharedSecret {
58
- SharedSecret {
59
- data : [ 0u8 ; 256 ] ,
60
- len : 0 ,
61
- }
49
+ SharedSecret ( [ 0u8 ; 32 ] )
62
50
}
63
51
64
52
/// Gets a pointer to the underlying data with the specified capacity.
65
53
pub ( crate ) fn get_data_mut_ptr ( & mut self ) -> * mut u8 {
66
- self . data . as_mut_ptr ( )
67
- }
68
-
69
- /// Gets the capacity of the underlying data buffer.
70
- pub fn capacity ( & self ) -> usize {
71
- self . data . len ( )
72
- }
73
-
74
- /// Gets the len of the used data.
75
- pub fn len ( & self ) -> usize {
76
- self . len
54
+ self . 0 . as_mut_ptr ( )
77
55
}
78
56
79
57
/// Returns true if the underlying data buffer is empty.
80
58
pub fn is_empty ( & self ) -> bool {
81
- self . data . is_empty ( )
82
- }
83
-
84
- /// Sets the length of the object.
85
- pub ( crate ) fn set_len ( & mut self , len : usize ) {
86
- debug_assert ! ( len <= self . data. len( ) ) ;
87
- self . len = len;
88
- }
89
- }
90
-
91
- impl PartialEq for SharedSecret {
92
- fn eq ( & self , other : & SharedSecret ) -> bool {
93
- self . as_ref ( ) == other. as_ref ( )
59
+ self . 0 . is_empty ( )
94
60
}
95
61
}
96
62
97
63
impl AsRef < [ u8 ] > for SharedSecret {
98
64
fn as_ref ( & self ) -> & [ u8 ] {
99
- & self . data [ ..self . len ]
100
- }
101
- }
102
-
103
- impl Deref for SharedSecret {
104
- type Target = [ u8 ] ;
105
- fn deref ( & self ) -> & [ u8 ] {
106
- & self . data [ ..self . len ]
65
+ & self . 0
107
66
}
108
67
}
109
68
110
-
111
69
unsafe extern "C" fn c_callback ( output : * mut c_uchar , x : * const c_uchar , y : * const c_uchar , _data : * mut c_void ) -> c_int {
112
70
ptr:: copy_nonoverlapping ( x, output, 32 ) ;
113
71
ptr:: copy_nonoverlapping ( y, output. offset ( 32 ) , 32 ) ;
@@ -132,7 +90,6 @@ impl SharedSecret {
132
90
// The default `secp256k1_ecdh_hash_function_default` should always return 1.
133
91
// and the scalar was verified to be valid(0 > scalar > group_order) via the type system
134
92
debug_assert_eq ! ( res, 1 ) ;
135
- ss. set_len ( 32 ) ; // The default hash function is SHA256, which is 32 bytes long.
136
93
ss
137
94
}
138
95
@@ -183,6 +140,12 @@ impl SharedSecret {
183
140
}
184
141
}
185
142
143
+ impl From < [ u8 ; 32 ] > for SharedSecret {
144
+ fn from ( inner : [ u8 ; 32 ] ) -> SharedSecret {
145
+ SharedSecret ( inner)
146
+ }
147
+ }
148
+
186
149
#[ cfg( test) ]
187
150
#[ allow( unused_imports) ]
188
151
mod tests {
@@ -226,15 +189,15 @@ mod tests {
226
189
fn ecdh_with_hash_callback ( ) {
227
190
let s = Secp256k1 :: signing_only ( ) ;
228
191
let ( sk1, pk1) = s. generate_keypair ( & mut thread_rng ( ) ) ;
229
- let expect_result: [ u8 ; 64 ] = [ 123 ; 64 ] ;
192
+ let expect_result: [ u8 ; 32 ] = [ 123 ; 32 ] ;
230
193
let mut x_out = [ 0u8 ; 32 ] ;
231
194
let mut y_out = [ 0u8 ; 32 ] ;
232
195
let result = SharedSecret :: new_with_hash ( & pk1, & sk1, |x, y| {
233
196
x_out = x;
234
197
y_out = y;
235
198
expect_result. into ( )
236
199
} ) ;
237
- assert_eq ! ( & expect_result[ ..] , & result[ ..] ) ;
200
+ assert_eq ! ( & expect_result[ ..] , & result. 0 [ ..] ) ;
238
201
assert_ne ! ( x_out, [ 0u8 ; 32 ] ) ;
239
202
assert_ne ! ( y_out, [ 0u8 ; 32 ] ) ;
240
203
}
0 commit comments