Skip to content

Commit 1063087

Browse files
committed
tests: add tests for ecdsa.verify
1 parent 98bc168 commit 1063087

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/ecdsa.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ function sign(curve, hash, d) {
7676
return new ECSignature(r, s)
7777
}
7878

79-
function verify(curve, hash, signature, Q) {
80-
// 1.4.2 H = Hash(M), already done by the user
81-
// 1.4.3 e = H
82-
var e = BigInteger.fromBuffer(hash)
83-
84-
return verifyRaw(curve, e, signature, Q)
85-
}
86-
8779
function verifyRaw(curve, e, signature, Q) {
8880
var n = curve.n
8981
var G = curve.G
@@ -114,6 +106,14 @@ function verifyRaw(curve, e, signature, Q) {
114106
return v.equals(r)
115107
}
116108

109+
function verify(curve, hash, signature, Q) {
110+
// 1.4.2 H = Hash(M), already done by the user
111+
// 1.4.3 e = H
112+
var e = BigInteger.fromBuffer(hash)
113+
114+
return verifyRaw(curve, e, signature, Q)
115+
}
116+
117117
/**
118118
* Recover a public key from a signature.
119119
*

test/ecdsa.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,35 @@ describe('ecdsa', function() {
115115
})
116116
})
117117

118-
describe('verifyRaw', function() {
118+
describe('verify/verifyRaw', function() {
119119
fixtures.valid.forEach(function(f) {
120120
it('verifies a valid signature for \"' + f.message + '\"', function() {
121121
var d = BigInteger.fromHex(f.d)
122-
var e = BigInteger.fromBuffer(crypto.sha256(f.message))
122+
var H = crypto.sha256(f.message)
123+
var e = BigInteger.fromBuffer(H)
123124
var signature = new ECSignature(
124125
new BigInteger(f.signature.r),
125126
new BigInteger(f.signature.s)
126127
)
127128
var Q = curve.G.multiply(d)
128129

130+
assert(ecdsa.verify(curve, H, signature, Q))
129131
assert(ecdsa.verifyRaw(curve, e, signature, Q))
130132
})
131133
})
132134

133135
fixtures.invalid.verifyRaw.forEach(function(f) {
134136
it('fails to verify with ' + f.description, function() {
137+
var H = crypto.sha256(f.message)
138+
var e = BigInteger.fromBuffer(H)
135139
var d = BigInteger.fromHex(f.d)
136-
var e = BigInteger.fromHex(f.e)
137140
var signature = new ECSignature(
138141
new BigInteger(f.signature.r),
139142
new BigInteger(f.signature.s)
140143
)
141144
var Q = curve.G.multiply(d)
142145

146+
assert.equal(ecdsa.verify(curve, H, signature, Q), false)
143147
assert.equal(ecdsa.verifyRaw(curve, e, signature, Q), false)
144148
})
145149
})

test/fixtures/ecdsa.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
{
149149
"description": "The wrong signature",
150150
"d": "01",
151-
"e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456",
151+
"message": "foo",
152152
"signature": {
153153
"r": "38341707918488238920692284707283974715538935465589664377561695343399725051885",
154154
"s": "3180566392414476763164587487324397066658063772201694230600609996154610926757"
@@ -157,7 +157,7 @@
157157
{
158158
"description": "Invalid r value (< 0)",
159159
"d": "01",
160-
"e": "01",
160+
"message": "foo",
161161
"signature": {
162162
"r": "-01",
163163
"s": "02"
@@ -166,7 +166,7 @@
166166
{
167167
"description": "Invalid r value (== 0)",
168168
"d": "01",
169-
"e": "01",
169+
"message": "foo",
170170
"signature": {
171171
"r": "00",
172172
"s": "02"
@@ -175,7 +175,7 @@
175175
{
176176
"description": "Invalid r value (>= n)",
177177
"d": "01",
178-
"e": "01",
178+
"message": "foo",
179179
"signature": {
180180
"r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
181181
"s": "02"
@@ -184,7 +184,7 @@
184184
{
185185
"description": "Invalid s value (< 0)",
186186
"d": "01",
187-
"e": "01",
187+
"message": "foo",
188188
"signature": {
189189
"r": "02",
190190
"s": "-01"
@@ -193,7 +193,7 @@
193193
{
194194
"description": "Invalid s value (== 0)",
195195
"d": "01",
196-
"e": "01",
196+
"message": "foo",
197197
"signature": {
198198
"r": "02",
199199
"s": "00"
@@ -202,7 +202,7 @@
202202
{
203203
"description": "Invalid s value (>= n)",
204204
"d": "01",
205-
"e": "01",
205+
"message": "foo",
206206
"signature": {
207207
"r": "02",
208208
"s": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"
@@ -211,12 +211,12 @@
211211
{
212212
"description": "Invalid r, s values (r = s = -n)",
213213
"d": "01",
214-
"e": "01",
214+
"message": "foo",
215215
"signature": {
216216
"r": "-115792089237316195423570985008687907852837564279074904382605163141518161494337",
217217
"s": "-115792089237316195423570985008687907852837564279074904382605163141518161494337"
218218
}
219219
}
220220
]
221221
}
222-
}
222+
}

0 commit comments

Comments
 (0)