@@ -113,6 +113,110 @@ module.exports = function (t, secp256k1) {
113113 t . end ( )
114114 } )
115115
116+ t . test ( 'privateKeyNegate' , function ( t ) {
117+ t . test ( 'private key should be a Buffer' , function ( t ) {
118+ t . throws ( function ( ) {
119+ secp256k1 . privateKeyNegate ( null )
120+ } , new RegExp ( '^TypeError: ' + messages . EC_PRIVATE_KEY_TYPE_INVALID + '$' ) )
121+ t . end ( )
122+ } )
123+
124+ t . test ( 'private key length is invalid' , function ( t ) {
125+ t . throws ( function ( ) {
126+ var privateKey = util . getPrivateKey ( ) . slice ( 1 )
127+ secp256k1 . privateKeyNegate ( privateKey )
128+ } , new RegExp ( '^RangeError: ' + messages . EC_PRIVATE_KEY_LENGTH_INVALID + '$' ) )
129+ t . end ( )
130+ } )
131+
132+ t . test ( 'private key is 0' , function ( t ) {
133+ var privateKey = util . BN_ZERO . toArrayLike ( Buffer , 'be' , 32 )
134+
135+ var expected = Buffer . alloc ( 32 )
136+ var result = secp256k1 . privateKeyNegate ( privateKey )
137+ t . same ( result , expected )
138+
139+ t . end ( )
140+ } )
141+
142+ t . test ( 'private key equal to N' , function ( t ) {
143+ var privateKey = util . ec . curve . n . toArrayLike ( Buffer , 'be' , 32 )
144+
145+ var expected = Buffer . alloc ( 32 )
146+ var result = secp256k1 . privateKeyNegate ( privateKey )
147+ t . same ( result , expected )
148+
149+ t . end ( )
150+ } )
151+
152+ t . test ( 'private key overflow' , function ( t ) {
153+ var privateKey = util . ec . curve . n . addn ( 10 ) . toArrayLike ( Buffer , 'be' , 32 )
154+
155+ var expected = util . ec . curve . n . subn ( 10 ) . toArrayLike ( Buffer , 'be' , 32 )
156+ var result = secp256k1 . privateKeyNegate ( privateKey )
157+ t . same ( result , expected )
158+
159+ t . end ( )
160+ } )
161+
162+ util . repeat ( t , 'random tests' , util . env . repeat , function ( t ) {
163+ var privateKey = util . getPrivateKey ( )
164+
165+ var expected = util . ec . curve . n . sub ( new BN ( privateKey ) )
166+ var result = secp256k1 . privateKeyNegate ( privateKey )
167+ t . same ( result . toString ( 'hex' ) , expected . toString ( 16 , 64 ) )
168+
169+ t . end ( )
170+ } )
171+
172+ t . end ( )
173+ } )
174+
175+ t . test ( 'privateKeyModInverse' , function ( t ) {
176+ t . test ( 'private key should be a Buffer' , function ( t ) {
177+ t . throws ( function ( ) {
178+ secp256k1 . privateKeyModInverse ( null )
179+ } , new RegExp ( '^TypeError: ' + messages . EC_PRIVATE_KEY_TYPE_INVALID + '$' ) )
180+ t . end ( )
181+ } )
182+
183+ t . test ( 'private key length is invalid' , function ( t ) {
184+ t . throws ( function ( ) {
185+ var privateKey = util . getPrivateKey ( ) . slice ( 1 )
186+ secp256k1 . privateKeyModInverse ( privateKey )
187+ } , new RegExp ( '^RangeError: ' + messages . EC_PRIVATE_KEY_LENGTH_INVALID + '$' ) )
188+ t . end ( )
189+ } )
190+
191+ t . test ( 'private key is 0' , function ( t ) {
192+ t . throws ( function ( ) {
193+ var privateKey = util . BN_ZERO . toArrayLike ( Buffer , 'be' , 32 )
194+ secp256k1 . privateKeyModInverse ( privateKey )
195+ } , new RegExp ( '^Error: ' + messages . EC_PRIVATE_KEY_RANGE_INVALID + '$' ) )
196+ t . end ( )
197+ } )
198+
199+ t . test ( 'private key equal to N' , function ( t ) {
200+ t . throws ( function ( ) {
201+ var privateKey = util . ec . curve . n . toArrayLike ( Buffer , 'be' , 32 )
202+ secp256k1 . privateKeyModInverse ( privateKey )
203+ } , new RegExp ( '^Error: ' + messages . EC_PRIVATE_KEY_RANGE_INVALID + '$' ) )
204+ t . end ( )
205+ } )
206+
207+ util . repeat ( t , 'random tests' , util . env . repeat , function ( t ) {
208+ var privateKey = util . getPrivateKey ( )
209+
210+ var expected = new BN ( privateKey ) . invm ( util . ec . curve . n )
211+ var result = secp256k1 . privateKeyModInverse ( privateKey )
212+ t . same ( result . toString ( 'hex' ) , expected . toString ( 16 , 64 ) )
213+
214+ t . end ( )
215+ } )
216+
217+ t . end ( )
218+ } )
219+
116220 t . test ( 'privateKeyTweakAdd' , function ( t ) {
117221 t . test ( 'private key should be a Buffer' , function ( t ) {
118222 t . throws ( function ( ) {
0 commit comments