siphash: return objects instead of buffers.

This commit is contained in:
Christopher Jeffrey 2017-06-16 14:28:59 -07:00
parent 5bde338a53
commit 015fb8b0b6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 20 additions and 12 deletions

View File

@ -83,7 +83,7 @@ function siphash24(data, key, shift) {
v0.ixor(v2); v0.ixor(v2);
v0.ixor(v3); v0.ixor(v3);
return v0.toRaw(); return v0;
} }
function sipround(v0, v1, v2, v3) { function sipround(v0, v1, v2, v3) {
@ -215,8 +215,8 @@ U64.prototype.irotl = function irotl(bits) {
U64.prototype.toRaw = function toRaw() { U64.prototype.toRaw = function toRaw() {
var data = Buffer.allocUnsafe(8); var data = Buffer.allocUnsafe(8);
data.writeUInt32LE(this.hi >>> 0, 4, true);
data.writeUInt32LE(this.lo >>> 0, 0, true); data.writeUInt32LE(this.lo >>> 0, 0, true);
data.writeUInt32LE(this.hi >>> 0, 4, true);
return data; return data;
}; };
@ -226,8 +226,8 @@ U64.fromRaw = function fromRaw(data, off) {
if (!off) if (!off)
off = 0; off = 0;
hi = data.readUInt32LE(off + 4, true);
lo = data.readUInt32LE(off, true); lo = data.readUInt32LE(off, true);
hi = data.readUInt32LE(off + 4, true);
return new U64(hi, lo); return new U64(hi, lo);
}; };
@ -236,7 +236,7 @@ U64.fromRaw = function fromRaw(data, off) {
* Expose * Expose
*/ */
exports = siphash256; exports = siphash;
exports.siphash = siphash; exports.siphash = siphash;
exports.siphash256 = siphash256; exports.siphash256 = siphash256;
exports.U64 = U64; exports.U64 = U64;

View File

@ -18,7 +18,7 @@ var StaticWriter = require('../utils/staticwriter');
var encoding = require('../utils/encoding'); var encoding = require('../utils/encoding');
var consensus = require('../protocol/consensus'); var consensus = require('../protocol/consensus');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var siphash = require('../crypto/siphash'); var siphash256 = require('../crypto/siphash').siphash256;
var AbstractBlock = require('../primitives/abstractblock'); var AbstractBlock = require('../primitives/abstractblock');
var TX = require('../primitives/tx'); var TX = require('../primitives/tx');
var Headers = require('../primitives/headers'); var Headers = require('../primitives/headers');
@ -387,10 +387,10 @@ CompactBlock.prototype.sid = function sid(hash) {
if (typeof hash === 'string') if (typeof hash === 'string')
hash = Buffer.from(hash, 'hex'); hash = Buffer.from(hash, 'hex');
hash = siphash(hash, this.sipKey); hash = siphash256(hash, this.sipKey);
lo = hash.readUInt32LE(0, true); lo = hash.lo >>> 0;
hi = hash.readUInt16LE(4, true); hi = hash.hi & 0xffff;
return hi * 0x100000000 + lo; return hi * 0x100000000 + lo;
}; };

View File

@ -28,7 +28,7 @@
"hmac-drbg": "^1.0.0" "hmac-drbg": "^1.0.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"bcoin-native": "0.0.17", "bcoin-native": "0.0.18",
"leveldown": "1.7.0-0", "leveldown": "1.7.0-0",
"secp256k1": "3.2.5", "secp256k1": "3.2.5",
"socket.io": "2.0.1", "socket.io": "2.0.1",

View File

@ -2,14 +2,22 @@
var assert = require('assert'); var assert = require('assert');
var siphash = require('../lib/crypto/siphash'); var siphash = require('../lib/crypto/siphash');
var siphash256 = siphash.siphash256;
var U64 = siphash.U64; var U64 = siphash.U64;
function toRaw(num) {
var data = Buffer.allocUnsafe(8);
data.writeUInt32LE(num.lo >>> 0, 0, true);
data.writeUInt32LE(num.hi >>> 0, 4, true);
return data;
};
describe('SipHash', function() { describe('SipHash', function() {
it('should perform siphash with no data', function() { it('should perform siphash with no data', function() {
var k0 = U64(0x07060504, 0x03020100).toRaw(); var k0 = U64(0x07060504, 0x03020100).toRaw();
var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw(); var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw();
var key = Buffer.concat([k0, k1]); var key = Buffer.concat([k0, k1]);
assert.equal(siphash(Buffer.alloc(0), key).toString('hex'), '310e0edd47db6f72'); assert.equal(toRaw(siphash256(Buffer.alloc(0), key)).toString('hex'), '310e0edd47db6f72');
}); });
it('should perform siphash with data', function() { it('should perform siphash with data', function() {
@ -17,7 +25,7 @@ describe('SipHash', function() {
var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw(); var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw();
var data = U64(0x07060504, 0x03020100).toRaw(); var data = U64(0x07060504, 0x03020100).toRaw();
var key = Buffer.concat([k0, k1]); var key = Buffer.concat([k0, k1]);
assert.equal(siphash(data, key).toString('hex'), '6224939a79f5f593'); assert.equal(toRaw(siphash256(data, key)).toString('hex'), '6224939a79f5f593');
}); });
it('should perform siphash with uint256', function() { it('should perform siphash with uint256', function() {
@ -25,6 +33,6 @@ describe('SipHash', function() {
var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw(); var k1 = U64(0x0f0e0d0c, 0x0b0a0908).toRaw();
var hash = Buffer.from('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex'); var hash = Buffer.from('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 'hex');
var key = Buffer.concat([k0, k1]); var key = Buffer.concat([k0, k1]);
assert.equal(siphash(hash, key).toString('hex'), 'ce7cf2722f512771'); assert.equal(toRaw(siphash256(hash, key)).toString('hex'), 'ce7cf2722f512771');
}); });
}); });