Skip to content

Commit 1c62cfe

Browse files
Merge pull request #5 from starkbank/feature/postman-compatibility
Feature/postman compatibility
2 parents c21952c + a169bc6 commit 1c62cfe

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

ellipticcurve/ecdsa.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ const randomInteger = Integer.between;
99
const modulo = Integer.modulo;
1010

1111

12-
exports.sign = function (message, privateKey, hashfunc=sha256) {
12+
exports.sign = function (message, privateKey, hashfunc = null, randNum = null) {
13+
if (hashfunc == null) {
14+
hashfunc = sha256;
15+
}
1316
let hashMessage = hashfunc(message);
1417
let numberMessage = BinaryAscii.numberFromHex(hashMessage);
1518
let curve = privateKey.curve;
16-
let randNum = randomInteger(BigInt(1), curve.N.minus(1));
19+
if (randNum == null) {
20+
randNum = randomInteger(BigInt(1), curve.N.minus(1));
21+
}
1722
let randSignPoint = EcdsaMath.multiply(curve.G, randNum, curve.N, curve.A, curve.P);
1823
let r = modulo(randSignPoint.x, curve.N);
1924
let s = modulo((numberMessage.add(r.multiply(privateKey.secret)).multiply(EcdsaMath.inv(randNum, curve.N))), curve.N);

test/test.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var assert = require("assert");
1+
const assert = require("assert");
2+
const BigInt = require("big-integer");
23

3-
var Ecdsa = require("../ellipticcurve/ecdsa");
4-
var PrivateKey = require("../ellipticcurve/privateKey").PrivateKey;
5-
var PublicKey = require("../ellipticcurve/publicKey").PublicKey;
6-
var Signature = require("../ellipticcurve/signature").Signature;
7-
var File = require("../ellipticcurve/utils/file");
4+
const Ecdsa = require("../ellipticcurve/ecdsa");
5+
const PrivateKey = require("../ellipticcurve/privateKey").PrivateKey;
6+
const PublicKey = require("../ellipticcurve/publicKey").PublicKey;
7+
const Signature = require("../ellipticcurve/signature").Signature;
8+
const File = require("../ellipticcurve/utils/file");
89

910

1011
describe("ECDSA test", function() {
@@ -152,4 +153,18 @@ describe("Signature test", function() {
152153
assert.equal(String(signature1.s), String(signature2.s));
153154
});
154155
});
156+
describe("#testExternalRandNum()", function () {
157+
it("should confirm authenticity and same signature", function () {
158+
let privateKey = new PrivateKey();
159+
let publicKey = privateKey.publicKey();
160+
let message = "This is a message";
161+
let signature_1 = Ecdsa.sign(message, privateKey, null, BigInt(123));
162+
let signature_2 = Ecdsa.sign(message, privateKey, null, BigInt(123));
163+
164+
assert.equal(Ecdsa.verify(message, signature_1, publicKey), true);
165+
assert.equal(Ecdsa.verify(message, signature_2, publicKey), true);
166+
assert.equal(signature_1.r.value, signature_2.r.value);
167+
assert.equal(signature_1.s.value, signature_2.s.value);
168+
});
169+
});
155170
});

0 commit comments

Comments
 (0)