Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions mixing/dcnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mixing/mixclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
}
Expand Down
4 changes: 2 additions & 2 deletions mixing/sid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down
22 changes: 12 additions & 10 deletions mixing/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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
}
Expand All @@ -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)
}
36 changes: 14 additions & 22 deletions mixing/utxoproof/utxoproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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)
}