File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,11 @@ var ecdsa = {
6969
7070 var s = k . modInverse ( n ) . multiply ( e . add ( d . multiply ( r ) ) ) . mod ( n )
7171
72+ if ( s . compareTo ( n . divide ( BigInteger . valueOf ( 2 ) ) ) > 0 ) {
73+ // Make 's' value 'low', as per https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures
74+ s = n . subtract ( s ) ;
75+ }
76+
7277 return ecdsa . serializeSig ( r , s )
7378 } ,
7479
Original file line number Diff line number Diff line change 11var assert = require ( 'assert' )
22var crypto = require ( '../' ) . crypto
33var ecdsa = require ( '..' ) . ecdsa
4+ var sec = require ( '../src/jsbn/sec.js' )
5+ var BigInteger = require ( '../src/jsbn/jsbn.js' )
6+ var ecparams = sec ( "secp256k1" )
47var rng = require ( 'secure-random' )
58
69var BigInteger = require ( '..' ) . BigInteger
@@ -55,5 +58,18 @@ describe('ecdsa', function() {
5558
5659 assert . ok ( ecdsa . verify ( hash2 , sig_c , s2 ) , 'Verify constant signature' )
5760 } )
61+
62+ it ( 'should sign with low S value' , function ( ) {
63+ var priv = new ECKey ( 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458' )
64+ var message = 'Vires in numeris'
65+ var signature = priv . sign ( message )
66+ var parsed = ecdsa . parseSig ( signature )
67+
68+ // Check that the 's' value is 'low', to prevent possible transaction malleability as per
69+ // https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures
70+ assert . ok ( parsed . s . compareTo ( ecparams . getN ( ) . divide ( BigInteger . valueOf ( 2 ) ) ) <= 0 )
71+
72+ assert . ok ( priv . verify ( message , signature ) )
73+ } )
5874 } )
5975} )
You can’t perform that action at this time.
0 commit comments