1616//!
1717
1818use core:: ptr;
19- use core:: ops:: { FnMut , Deref } ;
19+ use core:: ops:: FnMut ;
2020
2121use key:: { SecretKey , PublicKey } ;
2222use ffi:: { self , CPtr } ;
@@ -39,75 +39,33 @@ use secp256k1_sys::types::{c_int, c_uchar, c_void};
3939/// assert_eq!(sec1, sec2);
4040/// # }
4141// ```
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 ] ) ;
5344
5445impl SharedSecret {
5546
5647 /// Creates an empty `SharedSecret`.
5748 pub ( crate ) fn empty ( ) -> SharedSecret {
58- SharedSecret {
59- data : [ 0u8 ; 256 ] ,
60- len : 0 ,
61- }
49+ SharedSecret ( [ 0u8 ; 32 ] )
6250 }
6351
6452 /// Gets a pointer to the underlying data with the specified capacity.
6553 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 ( )
7755 }
7856
7957 /// Returns true if the underlying data buffer is empty.
8058 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 ( )
9460 }
9561}
9662
9763impl AsRef < [ u8 ] > for SharedSecret {
9864 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
10766 }
10867}
10968
110-
11169unsafe extern "C" fn c_callback ( output : * mut c_uchar , x : * const c_uchar , y : * const c_uchar , _data : * mut c_void ) -> c_int {
11270 ptr:: copy_nonoverlapping ( x, output, 32 ) ;
11371 ptr:: copy_nonoverlapping ( y, output. offset ( 32 ) , 32 ) ;
@@ -132,7 +90,6 @@ impl SharedSecret {
13290 // The default `secp256k1_ecdh_hash_function_default` should always return 1.
13391 // and the scalar was verified to be valid(0 > scalar > group_order) via the type system
13492 debug_assert_eq ! ( res, 1 ) ;
135- ss. set_len ( 32 ) ; // The default hash function is SHA256, which is 32 bytes long.
13693 ss
13794 }
13895
@@ -183,6 +140,12 @@ impl SharedSecret {
183140 }
184141}
185142
143+ impl From < [ u8 ; 32 ] > for SharedSecret {
144+ fn from ( inner : [ u8 ; 32 ] ) -> SharedSecret {
145+ SharedSecret ( inner)
146+ }
147+ }
148+
186149#[ cfg( test) ]
187150#[ allow( unused_imports) ]
188151mod tests {
@@ -226,15 +189,15 @@ mod tests {
226189 fn ecdh_with_hash_callback ( ) {
227190 let s = Secp256k1 :: signing_only ( ) ;
228191 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 ] ;
230193 let mut x_out = [ 0u8 ; 32 ] ;
231194 let mut y_out = [ 0u8 ; 32 ] ;
232195 let result = SharedSecret :: new_with_hash ( & pk1, & sk1, |x, y| {
233196 x_out = x;
234197 y_out = y;
235198 expect_result. into ( )
236199 } ) ;
237- assert_eq ! ( & expect_result[ ..] , & result[ ..] ) ;
200+ assert_eq ! ( & expect_result[ ..] , & result. 0 [ ..] ) ;
238201 assert_ne ! ( x_out, [ 0u8 ; 32 ] ) ;
239202 assert_ne ! ( y_out, [ 0u8 ; 32 ] ) ;
240203 }
0 commit comments