@@ -132,6 +132,44 @@ test(SUITE, 'should set private key and compute secret for secp256k1', () => {
132132 assert . strictEqual ( secret1 . toString ( 'hex' ) , secret2 . toString ( 'hex' ) ) ;
133133} ) ;
134134
135+ test ( SUITE , 'should compute secret with sliced public key buffer' , ( ) => {
136+ const alice = crypto . createECDH ( 'secp256k1' ) ;
137+ alice . generateKeys ( ) ;
138+
139+ const bob = crypto . createECDH ( 'secp256k1' ) ;
140+ bob . generateKeys ( ) ;
141+
142+ const bobPub = bob . getPublicKey ( ) as Buffer ;
143+ assert . isTrue ( Buffer . isBuffer ( bobPub ) , 'public key should be a Buffer' ) ;
144+
145+ // Force non-zero byteOffset by slicing from a larger packet.
146+ const packet = Buffer . concat ( [
147+ Buffer . from ( [ 0xaa , 0xbb ] ) ,
148+ bobPub ,
149+ Buffer . from ( [ 0xcc ] ) ,
150+ ] ) ;
151+ const bobPubSlice = packet . slice ( 2 , 2 + bobPub . length ) ;
152+ assert . strictEqual (
153+ bobPubSlice . length ,
154+ bobPub . length ,
155+ 'slice length should match key length' ,
156+ ) ;
157+ assert . isAbove (
158+ bobPubSlice . byteOffset ,
159+ 0 ,
160+ 'slice should have non-zero byteOffset' ,
161+ ) ;
162+
163+ const secretFromOriginal = alice . computeSecret ( bobPub ) ;
164+ const secretFromSlice = alice . computeSecret ( bobPubSlice ) ;
165+
166+ assert . strictEqual (
167+ secretFromSlice . toString ( 'hex' ) ,
168+ secretFromOriginal . toString ( 'hex' ) ,
169+ 'sliced public key should derive the same shared secret' ,
170+ ) ;
171+ } ) ;
172+
135173test ( SUITE , 'getCurves - should return array of supported curves' , ( ) => {
136174 const curves = getCurves ( ) ;
137175 assert . isArray ( curves ) ;
0 commit comments