From 37d5147cacbe30b3db6fb3591e5eb499f804d2cb Mon Sep 17 00:00:00 2001
From: Daniel Cousens <github@dcousens.com>
Date: Sat, 24 May 2014 13:40:20 +1000
Subject: [PATCH] ecdsa: add invalid tests for verifyRaw

---
 test/ecdsa.js            | 14 +++++++++++-
 test/fixtures/ecdsa.json | 49 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/test/ecdsa.js b/test/ecdsa.js
index 8e53881..cbe1fd3 100644
--- a/test/ecdsa.js
+++ b/test/ecdsa.js
@@ -62,7 +62,7 @@ describe('ecdsa', function() {
   })
 
   describe('verifyRaw', function() {
-    it('matches the test vectors', function() {
+    it('verifies valid signatures', function() {
       fixtures.valid.forEach(function(f) {
         var D = BigInteger.fromHex(f.D)
         var Q = ecparams.getG().multiply(D)
@@ -74,6 +74,18 @@ describe('ecdsa', function() {
         assert(ecdsa.verifyRaw(ecparams, e, r, s, Q))
       })
     })
+
+    fixtures.invalid.verifyRaw.forEach(function(f) {
+      it('fails to verify with ' + f.description, function() {
+        var D = BigInteger.fromHex(f.D)
+        var e = BigInteger.fromHex(f.e)
+        var r = new BigInteger(f.signature.r)
+        var s = new BigInteger(f.signature.s)
+        var Q = ecparams.getG().multiply(D)
+
+        assert.equal(ecdsa.verifyRaw(ecparams, e, r, s, Q), false)
+      })
+    })
   })
 
   describe('serializeSig', function() {
diff --git a/test/fixtures/ecdsa.json b/test/fixtures/ecdsa.json
index d8b7f60..f3ce488 100644
--- a/test/fixtures/ecdsa.json
+++ b/test/fixtures/ecdsa.json
@@ -1,7 +1,7 @@
 {
   "valid": [
     {
-      "D": "0000000000000000000000000000000000000000000000000000000000000001",
+      "D": "01",
       "k": "ec633bd56a5774a0940cb97e27a9e4e51dc94af737596a0c5cbb3d30332d92a5",
       "message": "Everything should be made as simple as possible, but not simpler.",
       "compact": "1f33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262",
@@ -132,6 +132,53 @@
         "description": "Sequence too long",
         "hex": "30080304ffffffff0304ffffffffffffff"
       }
+    ],
+    "verifyRaw": [
+      {
+        "description": "The wrong signature",
+        "D": "01",
+        "e": "06ef2b193b83b3d701f765f1db34672ab84897e1252343cc2197829af3a30456",
+        "signature": {
+          "r": "38341707918488238920692284707283974715538935465589664377561695343399725051885",
+          "s": "3180566392414476763164587487324397066658063772201694230600609996154610926757"
+        }
+      },
+      {
+        "description": "Invalid r value (== 0)",
+        "D": "01",
+        "e": "01",
+        "signature": {
+          "r": "00",
+          "s": "02"
+        }
+      },
+      {
+        "description": "Invalid r value (>= n)",
+        "D": "01",
+        "e": "01",
+        "signature": {
+          "r": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
+          "s": "02"
+        }
+      },
+      {
+        "description": "Invalid s value (== 0)",
+        "D": "01",
+        "e": "01",
+        "signature": {
+          "r": "02",
+          "s": "00"
+        }
+      },
+      {
+        "description": "Invalid s value (>= n)",
+        "D": "01",
+        "e": "01",
+        "signature": {
+          "r": "02",
+          "s": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"
+        }
+      }
     ]
   }
 }