@@ -180,6 +180,36 @@ mod tests {
180
180
assert_eq ! ( sec1, sec2) ;
181
181
assert ! ( sec_odd != sec2) ;
182
182
}
183
+
184
+ #[ test]
185
+ fn ecdh_with_hash ( ) {
186
+ let s = Secp256k1 :: signing_only ( ) ;
187
+ let ( sk1, pk1) = s. generate_keypair ( & mut thread_rng ( ) ) ;
188
+ let ( sk2, pk2) = s. generate_keypair ( & mut thread_rng ( ) ) ;
189
+
190
+ let sec1 = SharedSecret :: new_with_hash ( & pk1, & sk2, |x, _| x. into ( ) ) ;
191
+ let sec2 = SharedSecret :: new_with_hash ( & pk2, & sk1, |x, _| x. into ( ) ) ;
192
+ let sec_odd = SharedSecret :: new_with_hash ( & pk1, & sk1, |x, _| x. into ( ) ) ;
193
+ assert_eq ! ( sec1, sec2) ;
194
+ assert_ne ! ( sec_odd, sec2) ;
195
+ }
196
+
197
+ #[ test]
198
+ fn ecdh_with_hash_callback ( ) {
199
+ let s = Secp256k1 :: signing_only ( ) ;
200
+ let ( sk1, pk1) = s. generate_keypair ( & mut thread_rng ( ) ) ;
201
+ let expect_result: [ u8 ; 64 ] = [ 123 ; 64 ] ;
202
+ let mut x_out = [ 0u8 ; 32 ] ;
203
+ let mut y_out = [ 0u8 ; 32 ] ;
204
+ let result = SharedSecret :: new_with_hash ( & pk1, & sk1, | x, y | {
205
+ x_out = x;
206
+ y_out = y;
207
+ expect_result. into ( )
208
+ } ) ;
209
+ assert_eq ! ( & expect_result[ ..] , & result[ ..] ) ;
210
+ assert_ne ! ( x_out, [ 0u8 ; 32 ] ) ;
211
+ assert_ne ! ( y_out, [ 0u8 ; 32 ] ) ;
212
+ }
183
213
}
184
214
185
215
#[ cfg( all( test, feature = "unstable" ) ) ]
0 commit comments