diff --git a/mixing/dcnet.go b/mixing/dcnet.go index 3091d711d..f8541c871 100644 --- a/mixing/dcnet.go +++ b/mixing/dcnet.go @@ -15,9 +15,8 @@ import ( // SRMixPads creates a vector of exponential DC-net pads from a vector of // shared secrets with each participating peer in the DC-net. func SRMixPads(kp [][]byte, my uint32) []*big.Int { - h := blake256.New() + h := blake256.NewHasher256() scratch := make([]byte, 8) - digest := make([]byte, blake256.Size) pads := make([]*big.Int, len(kp)) partialPad := new(big.Int) for j := uint32(0); j < uint32(len(kp)); j++ { @@ -30,8 +29,8 @@ func SRMixPads(kp [][]byte, my uint32) []*big.Int { h.Reset() h.Write(kp[i]) h.Write(scratch) - digest = h.Sum(digest[:0]) - partialPad.SetBytes(digest) + digest := h.Sum256() + partialPad.SetBytes(digest[:]) if my > i { pads[j].Add(pads[j], partialPad) } else { diff --git a/mixing/mixclient/client.go b/mixing/mixclient/client.go index 16d4644a2..a76722814 100644 --- a/mixing/mixclient/client.go +++ b/mixing/mixclient/client.go @@ -417,7 +417,7 @@ func NewClient(w Wallet) *Client { height: height, warming: make(chan struct{}), workQueue: make(chan *queueWork, runtime.NumCPU()), - blake256Hasher: blake256.New(), + blake256Hasher: blake256.NewHasher256(), epoch: w.Mixpool().Epoch(), stopping: make(chan struct{}), } diff --git a/mixing/sid.go b/mixing/sid.go index 7394bf60c..1dd384f2d 100644 --- a/mixing/sid.go +++ b/mixing/sid.go @@ -17,7 +17,7 @@ import ( // deriveSessionID creates the mix session identifier from an initial sorted // slice of PR message hashes. func deriveSessionID(seenPRs []chainhash.Hash, epoch uint64) [32]byte { - h := blake256.New() + h := blake256.NewHasher256() buf := make([]byte, 8) h.Write([]byte("decred-mix-session")) @@ -29,7 +29,7 @@ func deriveSessionID(seenPRs []chainhash.Hash, epoch uint64) [32]byte { h.Write(seenPRs[i][:]) } - return *(*[32]byte)(h.Sum(nil)) + return h.Sum256() } // SortPRsForSession performs an in-place sort of prs, moving each pair diff --git a/mixing/signatures.go b/mixing/signatures.go index 190a06c28..4351b52b1 100644 --- a/mixing/signatures.go +++ b/mixing/signatures.go @@ -42,9 +42,9 @@ func SignMessage(m Signed, priv *secp256k1.PrivateKey) error { // VerifySignedMessage verifies that a signed message carries a valid // signature for the represented identity. func VerifySignedMessage(m Signed) bool { - h := blake256.New() + h := blake256.NewHasher256() m.WriteSignedData(h) - sigHash := h.Sum(nil) + sigHash := h.Sum256() h.Reset() @@ -56,7 +56,7 @@ func VerifySignedMessage(m Signed) bool { run = 0 } - return verify(h, m.Pub(), m.Sig(), sigHash, command, sid, run) + return verify(h, m.Pub(), m.Sig(), sigHash[:], command, sid, run) } // VerifySignature verifies a message signature from its signature hash and @@ -65,16 +65,16 @@ func VerifySignedMessage(m Signed) bool { // the same public key, and demonstrating this can be used to prove malicious // behavior by sending different versions of messages through the network. func VerifySignature(pub, sig, sigHash []byte, command string, sid []byte, run uint32) bool { - h := blake256.New() + h := blake256.NewHasher256() return verify(h, pub, sig, sigHash, command, sid, run) } var zeroSID [32]byte func sign(priv *secp256k1.PrivateKey, m Signed) ([]byte, error) { - h := blake256.New() + h := blake256.NewHasher256() m.WriteSignedData(h) - sigHash := h.Sum(nil) + sigHash := h.Sum256() h.Reset() @@ -92,17 +92,18 @@ func sign(priv *secp256k1.PrivateKey, m Signed) ([]byte, error) { 64 + // sigHash 4, // commas ) - fmt.Fprintf(buf, tag+",%s,%x,%d,%x", m.Command(), sid, run, sigHash) + fmt.Fprintf(buf, tag+",%s,%x,%d,%x", m.Command(), sid, run, sigHash[:]) h.Write(buf.Bytes()) - sig, err := schnorr.Sign(priv, h.Sum(nil)) + hash := h.Sum256() + sig, err := schnorr.Sign(priv, hash[:]) if err != nil { return nil, err } return sig.Serialize(), nil } -func verify(h hash.Hash, pk []byte, sig []byte, sigHash []byte, command string, sid []byte, run uint32) bool { +func verify(h *blake256.Hasher256, pk []byte, sig []byte, sigHash []byte, command string, sid []byte, run uint32) bool { if len(pk) != secp256k1.PubKeyBytesLenCompressed { return false } @@ -126,5 +127,6 @@ func verify(h hash.Hash, pk []byte, sig []byte, sigHash []byte, command string, ) fmt.Fprintf(buf, tag+",%s,%x,%d,%x", command, sid, run, sigHash) h.Write(buf.Bytes()) - return sigParsed.Verify(h.Sum(nil), pkParsed) + hash := h.Sum256() + return sigParsed.Verify(hash[:], pkParsed) } diff --git a/mixing/utxoproof/utxoproof.go b/mixing/utxoproof/utxoproof.go index e0a33ec38..ce4d88b23 100644 --- a/mixing/utxoproof/utxoproof.go +++ b/mixing/utxoproof/utxoproof.go @@ -5,8 +5,6 @@ package utxoproof import ( - "encoding/binary" - "github.com/decred/dcrd/crypto/blake256" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr" @@ -23,7 +21,7 @@ const ( secp256k1P2PKH = "P2PKH(EC-Schnorr-DCRv0)" ) -var sep = []byte{','} +const sep = "," // The signature hash is created from the serialization of: // tag , scheme , expiry pubkey @@ -43,17 +41,14 @@ type Secp256k1KeyPair struct { func (k *Secp256k1KeyPair) SignUtxoProof(expires uint32) ([]byte, error) { const scheme = secp256k1P2PKH - h := blake256.New() - h.Write([]byte(tag)) - h.Write(sep) - h.Write([]byte(scheme)) - h.Write(sep) - expiresBytes := binary.BigEndian.AppendUint32(make([]byte, 0, 4), expires) - h.Write(expiresBytes) - h.Write(k.Pub) - hash := h.Sum(nil) + h := blake256.NewHasher256() + const preamble = tag + sep + scheme + sep + h.WriteBytes([]byte(preamble)) + h.WriteUint32BE(expires) + h.WriteBytes(k.Pub) + hash := h.Sum256() - sig, err := schnorr.Sign(k.Priv, hash) + sig, err := schnorr.Sign(k.Priv, hash[:]) if err != nil { return nil, err } @@ -76,15 +71,12 @@ func ValidateSecp256k1P2PKH(pubkey, proof []byte, expires uint32) bool { return false } - h := blake256.New() - h.Write([]byte(tag)) - h.Write(sep) - h.Write([]byte(scheme)) - h.Write(sep) - expiresBytes := binary.BigEndian.AppendUint32(make([]byte, 0, 4), expires) - h.Write(expiresBytes) + h := blake256.NewHasher256() + const preamble = tag + sep + scheme + sep + h.WriteBytes([]byte(preamble)) + h.WriteUint32BE(expires) h.Write(pubkey) - hash := h.Sum(nil) + hash := h.Sum256() - return proofParsed.Verify(hash, pubkeyParsed) + return proofParsed.Verify(hash[:], pubkeyParsed) }