Skip to content

Commit 373b05d

Browse files
committed
Merge pull request bitcoinjs#150 from dcousens/bigitests
Adds BI.toPaddedBuffer and toBuffer
2 parents 596b31b + df6ea8a commit 373b05d

File tree

8 files changed

+129
-26
lines changed

8 files changed

+129
-26
lines changed

src/base58.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ function decode(string) {
5555
i++
5656
}
5757

58-
// FIXME: If BigInteger supported buffers, this could be a copy
59-
var buffer = new Buffer(num.toByteArrayUnsigned())
60-
var padding = new Buffer(i)
61-
padding.fill(0)
58+
var buffer = num.toBuffer()
59+
var leadz = new Buffer(i)
60+
leadz.fill(0)
6261

63-
return Buffer.concat([padding, buffer])
62+
return Buffer.concat([leadz, buffer])
6463
}
6564

6665
module.exports = {

src/bigi.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
var assert = require('assert')
22
var BigInteger = require('bigi')
33

4+
// Import operations
5+
BigInteger.fromHex = function(hex) {
6+
var buffer = new Buffer(hex, 'hex')
7+
assert.equal(buffer.length, Buffer.byteLength(hex) / 2)
8+
9+
return BigInteger.fromBuffer(buffer)
10+
}
11+
412
BigInteger.fromBuffer = function(buffer) {
13+
assert(Array.isArray(buffer) || Buffer.isBuffer(buffer)) // FIXME: Transitionary
14+
515
// FIXME: Transitionary
616
if (Buffer.isBuffer(buffer)) {
717
buffer = Array.prototype.slice.call(buffer)
@@ -10,4 +20,19 @@ BigInteger.fromBuffer = function(buffer) {
1020
return BigInteger.fromByteArrayUnsigned(buffer)
1121
}
1222

23+
// Export operations
24+
BigInteger.prototype.toBuffer = function(s) {
25+
if (s != undefined) assert(Number.isFinite(s))
26+
27+
var buffer = new Buffer(this.toByteArrayUnsigned())
28+
var padded = new Buffer(s - buffer.length)
29+
padded.fill(0)
30+
31+
return Buffer.concat([padded, buffer], s)
32+
}
33+
34+
BigInteger.prototype.toHex = function(s) {
35+
return this.toBuffer(s).toString('hex')
36+
}
37+
1338
module.exports = BigInteger

src/eckey.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ ECKey.prototype.sign = function(hash) {
6565

6666
// Export functions
6767
ECKey.prototype.toBuffer = function() {
68-
var buffer = new Buffer(this.D.toByteArrayUnsigned())
69-
70-
// pad out to atleast 32 bytes
71-
var padded = new Buffer(32 - buffer.length)
72-
padded.fill(0)
73-
74-
return Buffer.concat([padded, buffer])
68+
return this.D.toBuffer(32)
7569
}
7670
ECKey.prototype.toHex = function() {
7771
return this.toBuffer().toString('hex')

src/message.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,18 @@ function magicHash(message) {
2525
// TODO: parameterize compression instead of using ECKey.compressed
2626
function sign(key, message) {
2727
var hash = magicHash(message)
28-
var sig = key.sign(hash)
29-
var obj = ecdsa.parseSig(sig)
30-
var i = ecdsa.calcPubKeyRecoveryParam(key.pub.Q, obj.r, obj.s, hash)
28+
var sig = ecdsa.parseSig(key.sign(hash))
29+
var i = ecdsa.calcPubKeyRecoveryParam(key.pub.Q, sig.r, sig.s, hash)
3130

3231
i += 27
3332
if (key.pub.compressed) {
3433
i += 4
3534
}
3635

37-
var rBa = obj.r.toByteArrayUnsigned()
38-
var sBa = obj.s.toByteArrayUnsigned()
36+
var rB = sig.r.toBuffer(32)
37+
var sB = sig.s.toBuffer(32)
3938

40-
// Pad to 32 bytes per value
41-
while (rBa.length < 32) rBa.unshift(0);
42-
while (sBa.length < 32) sBa.unshift(0);
43-
44-
sig = [i].concat(rBa, sBa)
45-
46-
return sig
39+
return Buffer.concat([new Buffer([i]), rB, sB], 65)
4740
}
4841

4942
// FIXME: stricter API?

test/bigi.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var assert = require('assert')
2+
var BigInteger = require('../').BigInteger
3+
4+
var fixtures = require('./fixtures/bigi')
5+
6+
describe('BigInteger', function() {
7+
describe('fromBuffer/fromHex', function() {
8+
it('should match the test vectors', function() {
9+
fixtures.valid.forEach(function(f) {
10+
assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec)
11+
assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec)
12+
})
13+
})
14+
15+
fixtures.invalid.forEach(function(f) {
16+
it('throws on ' + f.description, function() {
17+
assert.throws(function() {
18+
BigInteger.fromHex(f.string)
19+
})
20+
})
21+
})
22+
})
23+
24+
describe('toBuffer/toHex', function() {
25+
it('should match the test vectors', function() {
26+
fixtures.valid.forEach(function(f) {
27+
var bi = new BigInteger(f.dec)
28+
29+
assert.equal(bi.toHex(), f.hex)
30+
assert.equal(bi.toHex(32), f.hexPadded)
31+
})
32+
})
33+
34+
it('throws on non-finite padding value', function() {
35+
var bi = new BigInteger('1')
36+
37+
assert.throws(function() { bi.toHex({}) })
38+
assert.throws(function() { bi.toHex([]) })
39+
assert.throws(function() { bi.toHex('') })
40+
assert.throws(function() { bi.toHex(0 / 0) })
41+
assert.throws(function() { bi.toHex(1 / 0) })
42+
})
43+
})
44+
})
45+
46+
module.exports = BigInteger

test/ecdsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('ecdsa', function() {
3939
var sig_a = s1.sign([0])
4040

4141
assert.ok(sig_a, 'Sign null')
42-
assert.ok(s1.pub.verify(BigInteger.ZERO, sig_a))
42+
assert.ok(s1.pub.verify([0], sig_a))
4343

4444
var message = new Buffer(1024) // More or less random :P
4545
var hash = crypto.sha256(message)

test/fixtures/base58.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ module.exports = {
7171
"string": " \t\n\u000b\f\r skip \r\f\u000b\n\t a"
7272
}
7373
]
74-
}
74+
}

test/fixtures/bigi.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
"valid": [
3+
{
4+
"dec": "1",
5+
"hex": "01",
6+
"hexPadded": "0000000000000000000000000000000000000000000000000000000000000001"
7+
},
8+
{
9+
"dec": "158798437896437949616241483468158498679",
10+
"hex": "77777777777777777777777777777777",
11+
"hexPadded": "0000000000000000000000000000000077777777777777777777777777777777"
12+
},
13+
{
14+
"dec": "115792089237316195423570985008687907852837564279074904382605163141518161494336",
15+
"hex": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
16+
"hexPadded": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140"
17+
},
18+
{
19+
"dec": "48968302285117906840285529799176770990048954789747953886390402978935544927851",
20+
"hex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b",
21+
"hexPadded": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b"
22+
}
23+
],
24+
"invalid": [
25+
{
26+
"description": "non-hex string",
27+
"string": "invalid"
28+
},
29+
{
30+
"description": "non-hex alphabet",
31+
"string": "c2F0b3NoaQo="
32+
},
33+
{
34+
"description": "internal whitespace",
35+
"string": "11111 11111"
36+
},
37+
{
38+
"description": "leading whitespace",
39+
"string": " 1111111111"
40+
},
41+
{
42+
"description": "trailing whitespace",
43+
"string": "1111111111 "
44+
}
45+
]
46+
}

0 commit comments

Comments
 (0)