Skip to content

Commit

Permalink
wire-mix: replace Hash() with WriteHash(hash.Hash)
Browse files Browse the repository at this point in the history
requiring the caller to pass in the hasher allows the hasher to be reused
across many calls, saving some allocations.
  • Loading branch information
jrick committed Nov 27, 2023
1 parent 63be32e commit 95461d6
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 71 deletions.
15 changes: 0 additions & 15 deletions wire/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"crypto/rand"
"encoding/binary"
"fmt"
"hash"
"io"
"math"
"time"

"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/crypto/blake256"
)

const (
Expand Down Expand Up @@ -844,16 +842,3 @@ func isStrictAscii(s string) bool {

return true
}

// hashEncoder specifies a writer for message and signature hashes which never
// errors.
type hashEncoder interface {
hashEncode(h hash.Hash, pver uint32)
}

// hashMessage returns the hash of the serialized message.
func hashMessage(msg hashEncoder, pver uint32) chainhash.Hash {
h := blake256.New()
msg.hashEncode(h, pver)
return *(*chainhash.Hash)(h.Sum(nil))
}
11 changes: 3 additions & 8 deletions wire/msgmixciphertexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func (msg *MsgMixCiphertexts) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixCiphertexts) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixCiphertexts) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -168,11 +168,6 @@ func (msg *MsgMixCiphertexts) MaxPayloadLength(pver uint32) uint32 {
return 552592
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixCiphertexts) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixCiphertexts) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixconfirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func (msg *MsgMixConfirm) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixConfirm) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixConfirm) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -161,11 +161,6 @@ func (msg *MsgMixConfirm) MaxPayloadLength(pver uint32) uint32 {
return 16543 + MaxBlockPayloadV3
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixConfirm) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixConfirm) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixdcnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func (msg *MsgMixDCNet) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixDCNet) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixDCNet) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -236,11 +236,6 @@ func (msg *MsgMixDCNet) MaxPayloadLength(pver uint32) uint32 {
return 16800915
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixDCNet) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixDCNet) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixkeyexchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func (msg *MsgMixKeyExchange) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixKeyExchange) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixKeyExchange) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -158,11 +158,6 @@ func (msg *MsgMixKeyExchange) MaxPayloadLength(pver uint32) uint32 {
return 17811
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixKeyExchange) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixKeyExchange) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixpairreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ func (msg *MsgMixPairReq) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixPairReq) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to a hasher.
func (msg *MsgMixPairReq) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -328,11 +328,6 @@ func (msg *MsgMixPairReq) MaxPayloadLength(pver uint32) uint32 {
return MaxBlockPayload
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixPairReq) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixPairReq) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixsecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ func (msg *MsgMixSecrets) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixSecrets) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixSecrets) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -171,11 +171,6 @@ func (msg *MsgMixSecrets) MaxPayloadLength(pver uint32) uint32 {
return 67773
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixSecrets) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixSecrets) GetIdentity() []byte {
return msg.Identity[:]
Expand Down
11 changes: 3 additions & 8 deletions wire/msgmixslotreserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ func (msg *MsgMixSlotReserve) BtcEncode(w io.Writer, pver uint32) error {
return nil
}

// hashEncode serializes msg to the hasher.
func (msg *MsgMixSlotReserve) hashEncode(h hash.Hash, pver uint32) {
// WriteHash serializes the message to the hasher.
func (msg *MsgMixSlotReserve) WriteHash(h hash.Hash) {
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, pver)
msg.writeMessageNoSignature("", h, MixVersion)
}

// writeMessageNoSignature serializes all elements of the message except for
Expand Down Expand Up @@ -247,11 +247,6 @@ func (msg *MsgMixSlotReserve) MaxPayloadLength(pver uint32) uint32 {
return 17318038
}

// Hash returns the hash of the serialized message.
func (msg *MsgMixSlotReserve) Hash() chainhash.Hash {
return hashMessage(msg, MixVersion)
}

// GetIdentity returns the message sender's public key identity.
func (msg *MsgMixSlotReserve) GetIdentity() []byte {
return msg.Identity[:]
Expand Down

0 comments on commit 95461d6

Please sign in to comment.