integration: clean up single-key stealth derivation

This commit is contained in:
Daniel Cousens 2015-08-18 15:39:43 +10:00
parent 5d2abb523e
commit 2ccf00f4be

View file

@ -9,35 +9,42 @@ var crypto = require('crypto')
describe('bitcoinjs-lib (crypto)', function () { describe('bitcoinjs-lib (crypto)', function () {
it('can generate a single-key stealth address', function () { it('can generate a single-key stealth address', function () {
var receiver = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss')
// XXX: ephemeral, must be random (and secret to sender) to preserve privacy
var sender = bitcoin.ECPair.fromWIF('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct')
var G = bitcoin.ECPair.curve.G var G = bitcoin.ECPair.curve.G
var d = receiver.d // secret (receiver only) var n = bitcoin.ECPair.curve.n
var Q = receiver.Q // shared
var e = sender.d // secret (sender only)
var P = sender.Q // shared
// derived shared secret
var eQ = Q.multiply(e) // sender
var dP = P.multiply(d) // receiver
assert.deepEqual(eQ.getEncoded(), dP.getEncoded())
function stealthSend (Q, nonce) {
var noncePair = new bitcoin.ECPair(bigi.fromBuffer(nonce))
var e = noncePair.d
var eQ = Q.multiply(e)
var c = bigi.fromBuffer(bitcoin.crypto.sha256(eQ.getEncoded())) var c = bigi.fromBuffer(bitcoin.crypto.sha256(eQ.getEncoded()))
var cG = G.multiply(c) var cG = G.multiply(c)
var Qprime = Q.add(cG)
// derived public key return {
var QprimeS = Q.add(cG) address: new bitcoin.ECPair(null, Qprime).getAddress(),
var QprimeR = G.multiply(d.add(c)) nonceQ: noncePair.Q
assert.deepEqual(QprimeR.getEncoded(), QprimeS.getEncoded()) }
}
// derived shared-secret address function stealthReceive (d, P) {
var address = new bitcoin.ECPair(null, QprimeS).getAddress() var dP = P.multiply(d)
var c = bigi.fromBuffer(bitcoin.crypto.sha256(dP.getEncoded()))
var derived = new bitcoin.ECPair(d.add(c).mod(n))
assert.strictEqual(address, '1EwCNJNZM5q58YPPTnjR1H5BvYRNeyZi47') return {
keyPair: derived
}
}
// receiver private key
var receiver = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss')
var nonce = crypto.randomBytes(32)
var stealthS = stealthSend(receiver.Q, nonce)
var stealthR = stealthReceive(receiver.d, stealthS.nonceQ)
// and check that we derived both sides correctly
assert.equal(stealthS.address, stealthR.keyPair.getAddress())
}) })
// TODO // TODO