multi: replace usage of fastsha256 with crypto/sha256

This commit removes all instances of the fastsha256 library and
replaces it with the sha256 library in the standard library. This
change should see a number of performance improvements as the standard
library has highly optimized assembly instructions with use vectorized
instructions as the platform supports.
This commit is contained in:
Olaoluwa Osuntokun committed 2017-03-15 18:56:41 -07:00
1 parent d723aada30
commit f217093c00
16 files changed
+53 -55

No files matched your search

+8 -9
View File
@@ -3,11 +3,11 @@ package lnwallet
import (
"bytes"
"container/list"
"crypto/sha256"
"fmt"
"sync"
"sync/atomic"
"github.com/btcsuite/fastsha256"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
@@ -1500,7 +1500,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(rawSig []byte) error {
return err
}
revocationKey := DeriveRevocationPubkey(theirCommitKey, revocation[:])
revocationHash := fastsha256.Sum256(revocation[:])
revocationHash := sha256.Sum256(revocation[:])
// With the revocation information calculated, construct the new
// commitment view which includes all the entries we know of in their
@@ -1605,7 +1605,7 @@ func (lc *LightningChannel) RevokeCurrentCommitment() (*lnwire.RevokeAndAck, err
}
revocationMsg.NextRevocationKey = DeriveRevocationPubkey(theirCommitKey,
revocationEdge[:])
revocationMsg.NextRevocationHash = fastsha256.Sum256(revocationEdge[:])
revocationMsg.NextRevocationHash = sha256.Sum256(revocationEdge[:])
walletLog.Tracef("ChannelPoint(%v): revoking height=%v, now at height=%v, window_edge=%v",
lc.channelState.ChanID, lc.localCommitChain.tail().height,
@@ -1686,7 +1686,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ([]*P
// Additionally, we need to ensure we were given the proper preimage
// to the revocation hash used within any current HTLCs.
if !bytes.Equal(lc.channelState.TheirCurrentRevocationHash[:], zeroHash[:]) {
revokeHash := fastsha256.Sum256(pendingRevocation[:])
revokeHash := sha256.Sum256(pendingRevocation[:])
// TODO(roasbeef): rename to drop the "Their"
if !bytes.Equal(lc.channelState.TheirCurrentRevocationHash[:], revokeHash[:]) {
return nil, fmt.Errorf("revocation hash mismatch")
@@ -1798,7 +1798,7 @@ func (lc *LightningChannel) ExtendRevocationWindow() (*lnwire.RevokeAndAck, erro
theirCommitKey := lc.channelState.TheirCommitKey
revMsg.NextRevocationKey = DeriveRevocationPubkey(theirCommitKey,
revocation[:])
revMsg.NextRevocationHash = fastsha256.Sum256(revocation[:])
revMsg.NextRevocationHash = sha256.Sum256(revocation[:])
lc.revocationWindowEdge++
@@ -1888,8 +1888,7 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte) (uint64, error) {
lc.Lock()
defer lc.Unlock()
paymentHash := fastsha256.Sum256(preimage[:])
paymentHash := sha256.Sum256(preimage[:])
targetHTLCs, ok := lc.rHashMap[paymentHash]
if !ok {
return 0, fmt.Errorf("invalid payment hash")
@@ -1923,10 +1922,10 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, logIndex uint64
lc.Lock()
defer lc.Unlock()
paymentHash := fastsha256.Sum256(preimage[:])
paymentHash := sha256.Sum256(preimage[:])
htlc := lc.localUpdateLog.lookup(logIndex)
if htlc == nil {
return fmt.Errorf("non existent log entry")
return fmt.Errorf("non existant log entry")
}
if !bytes.Equal(htlc.RHash[:], paymentHash[:]) {
+8 -7
View File
@@ -2,6 +2,7 @@ package lnwallet
import (
"bytes"
"crypto/sha256"
"io/ioutil"
"os"
"testing"
@@ -357,7 +358,7 @@ func TestSimpleAddSettleWorkflow(t *testing.T) {
}
paymentPreimage := bytes.Repeat([]byte{1}, 32)
paymentHash := fastsha256.Sum256(paymentPreimage)
paymentHash := sha256.Sum256(paymentPreimage)
htlc := &lnwire.UpdateAddHTLC{
PaymentHash: paymentHash,
Amount: btcutil.SatoshiPerBitcoin,
@@ -622,7 +623,7 @@ func TestCheckCommitTxSize(t *testing.T) {
createHTLC := func(i int) (*lnwire.UpdateAddHTLC, [32]byte) {
preimage := bytes.Repeat([]byte{byte(i)}, 32)
paymentHash := fastsha256.Sum256(preimage)
paymentHash := sha256.Sum256(preimage)
var returnPreimage [32]byte
copy(returnPreimage[:], preimage)
@@ -744,7 +745,7 @@ func TestCooperativeChannelClosure(t *testing.T) {
func TestCheckHTLCNumberConstraint(t *testing.T) {
createHTLC := func(i int) *lnwire.UpdateAddHTLC {
preimage := bytes.Repeat([]byte{byte(i)}, 32)
paymentHash := fastsha256.Sum256(preimage)
paymentHash := sha256.Sum256(preimage)
return &lnwire.UpdateAddHTLC{
PaymentHash: paymentHash,
Amount: btcutil.Amount(1e7),
@@ -1025,7 +1026,7 @@ func TestCheckDustLimit(t *testing.T) {
createHTLC := func(data, amount btcutil.Amount) (*lnwire.UpdateAddHTLC,
[32]byte) {
preimage := bytes.Repeat([]byte{byte(data)}, 32)
paymentHash := fastsha256.Sum256(preimage)
paymentHash := sha256.Sum256(preimage)
var returnPreimage [32]byte
copy(returnPreimage[:], preimage)
@@ -1243,7 +1244,7 @@ func TestStateUpdatePersistence(t *testing.T) {
var bobPreimage [32]byte
copy(bobPreimage[:], bytes.Repeat([]byte{0xbb}, 32))
for i := 0; i < 3; i++ {
rHash := fastsha256.Sum256(alicePreimage[:])
rHash := sha256.Sum256(alicePreimage[:])
h := &lnwire.UpdateAddHTLC{
PaymentHash: rHash,
Amount: btcutil.Amount(1000),
@@ -1257,7 +1258,7 @@ func TestStateUpdatePersistence(t *testing.T) {
t.Fatalf("unable to recv alice's htlc: %v", err)
}
}
rHash := fastsha256.Sum256(bobPreimage[:])
rHash := sha256.Sum256(bobPreimage[:])
bobh := &lnwire.UpdateAddHTLC{
PaymentHash: rHash,
Amount: btcutil.Amount(1000),
@@ -1467,7 +1468,7 @@ func TestCancelHTLC(t *testing.T) {
var preImage [32]byte
copy(preImage[:], bytes.Repeat([]byte{0xaa}, 32))
htlc := &lnwire.UpdateAddHTLC{
PaymentHash: fastsha256.Sum256(preImage[:]),
PaymentHash: sha256.Sum256(preImage[:]),
Amount: htlcAmt,
Expiry: 10,
}
+1 -2
View File
@@ -9,7 +9,6 @@ import (
"golang.org/x/crypto/hkdf"
"github.com/btcsuite/fastsha256"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/txscript"
@@ -56,7 +55,7 @@ func witnessScriptHash(witnessScript []byte) ([]byte, error) {
bldr := txscript.NewScriptBuilder()
bldr.AddOp(txscript.OP_0)
scriptHash := fastsha256.Sum256(witnessScript)
scriptHash := sha256.Sum256(witnessScript)
bldr.AddData(scriptHash[:])
return bldr.Script()
}
+5 -5
View File
@@ -2,11 +2,11 @@ package lnwallet
import (
"bytes"
"crypto/sha256"
"fmt"
"testing"
"time"
"github.com/btcsuite/fastsha256"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/txscript"
"github.com/roasbeef/btcd/wire"
@@ -245,10 +245,10 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
// Generate a payment and revocation preimage to be used below.
revokePreimage := testHdSeed[:]
revokeHash := fastsha256.Sum256(revokePreimage)
revokeHash := sha256.Sum256(revokePreimage)
paymentPreimage := revokeHash
paymentPreimage[0] ^= 1
paymentHash := fastsha256.Sum256(paymentPreimage[:])
paymentHash := sha256.Sum256(paymentPreimage[:])
// We'll also need some tests keys for alice and bob, and metadata of
// the HTLC output.
@@ -417,10 +417,10 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
// Generate a payment and revocation preimage to be used below.
revokePreimage := testHdSeed[:]
revokeHash := fastsha256.Sum256(revokePreimage)
revokeHash := sha256.Sum256(revokePreimage)
paymentPreimage := revokeHash
paymentPreimage[0] ^= 1
paymentHash := fastsha256.Sum256(paymentPreimage[:])
paymentHash := sha256.Sum256(paymentPreimage[:])
// We'll also need some tests keys for alice and bob, and metadata of
// the HTLC output.
+2 -2
View File
@@ -1,12 +1,12 @@
package lnwallet
import (
"crypto/sha256"
"fmt"
"net"
"sync"
"sync/atomic"
"github.com/btcsuite/fastsha256"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
@@ -1287,7 +1287,7 @@ func deriveStateHintObfuscator(producer shachain.Producer) ([StateHintSize]byte,
return obfuscator, err
}
grandChild := fastsha256.Sum256(firstChild[:])
grandChild := sha256.Sum256(firstChild[:])
copy(obfuscator[:], grandChild[:])
return obfuscator, nil