1616//! 
1717
1818use  core:: ptr; 
19- use  core:: ops:: Deref ; 
2019
2120use  key:: { SecretKey ,  PublicKey } ; 
2221use  ffi:: { self ,  CPtr } ; 
@@ -39,90 +38,18 @@ use secp256k1_sys::types::{c_int, c_uchar, c_void};
3938/// assert_eq!(sec1, sec2); 
4039/// # } 
4140// ``` 
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 ) ) ; 
53- 
54- impl  SharedSecret  { 
55- 
56-     /// Creates an empty `SharedSecret`. 
57- pub ( crate )  fn  empty ( )  ->  SharedSecret  { 
58-         SharedSecret  { 
59-             data :  [ 0u8 ;  256 ] , 
60-             len :  0 , 
61-         } 
62-     } 
63- 
64-     /// Gets a pointer to the underlying data with the specified capacity. 
65- 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 
77-     } 
78- 
79-     /// Returns true if the underlying data buffer is empty. 
80- 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 ( ) 
94-     } 
95- } 
96- 
97- impl  AsRef < [ u8 ] >  for  SharedSecret  { 
98-     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 ] 
107-     } 
108- } 
109- 
110- 
111- 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-     ptr:: copy_nonoverlapping ( x,  output,  32 ) ; 
113-     ptr:: copy_nonoverlapping ( y,  output. offset ( 32 ) ,  32 ) ; 
114-     1 
115- } 
41+ #[ derive( Copy ,  Clone ,  Debug ,  PartialEq ) ]  
42+ pub  struct  SharedSecret ( [ u8 ;  32 ] ) ; 
11643
11744impl  SharedSecret  { 
11845    /// Creates a new shared secret from a pubkey and secret key. 
11946#[ inline]  
12047    pub  fn  new ( point :  & PublicKey ,  scalar :  & SecretKey )  -> SharedSecret  { 
121-         let  mut  ss  = SharedSecret :: empty ( ) ; 
48+         let  mut  buf  = [ 0u8 ;   32 ] ; 
12249        let  res = unsafe  { 
12350             ffi:: secp256k1_ecdh ( 
12451                ffi:: secp256k1_context_no_precomp, 
125-                 ss . get_data_mut_ptr ( ) , 
52+                 buf . as_mut_ptr ( ) , 
12653                point. as_c_ptr ( ) , 
12754                scalar. as_c_ptr ( ) , 
12855                ffi:: secp256k1_ecdh_hash_function_default, 
@@ -132,11 +59,27 @@ impl SharedSecret {
13259        // The default `secp256k1_ecdh_hash_function_default` should always return 1. 
13360        // and the scalar was verified to be valid(0 > scalar > group_order) via the type system 
13461        debug_assert_eq ! ( res,  1 ) ; 
135-         ss. set_len ( 32 ) ;  // The default hash function is SHA256, which is 32 bytes long. 
136-         ss
62+         SharedSecret ( buf) 
63+     } 
64+ 
65+     /// Returns true if the underlying data buffer is empty. 
66+ pub  fn  is_empty ( & self )  -> bool  { 
67+         self . 0 . is_empty ( ) 
13768    } 
13869} 
13970
71+ impl  AsRef < [ u8 ] >  for  SharedSecret  { 
72+     fn  as_ref ( & self )  -> & [ u8 ]  { 
73+         & self . 0 
74+     } 
75+ } 
76+ 
77+ unsafe  extern  "C"  fn  c_callback ( output :  * mut  c_uchar ,  x :  * const  c_uchar ,  y :  * const  c_uchar ,  _data :  * mut  c_void )  -> c_int  { 
78+     ptr:: copy_nonoverlapping ( x,  output,  32 ) ; 
79+     ptr:: copy_nonoverlapping ( y,  output. offset ( 32 ) ,  32 ) ; 
80+     1 
81+ } 
82+ 
14083/// Creates a shared point from public key and secret key. 
14184/// 
14285/// Can be used like `SharedSecret` but caller is responsible for then hashing the returned buffer. 
@@ -183,6 +126,12 @@ pub fn shared_secret_point(point: &PublicKey, scalar: &SecretKey) -> [u8; 64] {
183126    xy
184127} 
185128
129+ impl  From < [ u8 ;  32 ] >  for  SharedSecret  { 
130+     fn  from ( inner :  [ u8 ;  32 ] )  -> SharedSecret  { 
131+         SharedSecret ( inner) 
132+     } 
133+ } 
134+ 
186135#[ cfg( test) ]  
187136#[ allow( unused_imports) ]  
188137mod  tests { 
0 commit comments