Skip to content

Commit

Permalink
wire-mix: review items
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Jan 12, 2024
1 parent 2860b9b commit 7b213b7
Show file tree
Hide file tree
Showing 17 changed files with 1,517 additions and 367 deletions.
8 changes: 8 additions & 0 deletions wire/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ func writeElement(w io.Writer, element interface{}) error {
}
return nil

// Mix identity
case *[33]byte:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil

// Mix signature
case *[64]byte:
_, err := w.Write(e[:])
Expand Down
24 changes: 24 additions & 0 deletions wire/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,27 @@ func TestRandomUint64Errors(t *testing.T) {
t.Errorf("Nonce is not 0 [%v]", nonce)
}
}

// repeat returns the byte slice containing count elements of the byte b.
func repeat(b byte, count int) []byte {
s := make([]byte, count)
for i := range s {
s[i] = b
}
return s
}

// rhash returns a chainhash.Hash with all bytes set to b.
func rhash(b byte) chainhash.Hash {
var h chainhash.Hash
for i := range h {
h[i] = b
}
return h
}

// varBytesLen returns the size required to encode l bytes as a varint
// followed by the bytes themselves.
func varBytesLen(l uint32) uint32 {
return uint32(VarIntSerializeSize(uint64(l))) + l
}
6 changes: 3 additions & 3 deletions wire/message_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2023 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestMessage(t *testing.T) {
msgMixSR := NewMsgMixSlotReserve([33]byte{}, [32]byte{}, 1, 1, [][][]byte{{{}}}, []chainhash.Hash{})
msgMixDC := NewMsgMixDCNet([33]byte{}, [32]byte{}, 1, 1, []MixVect{make(MixVect, 1)}, []chainhash.Hash{})
msgMixCM := NewMsgMixConfirm([33]byte{}, [32]byte{}, 1, 1, NewMsgTx(), []chainhash.Hash{})
msgMixRS := NewMsgMixSecrets([33]byte{}, [32]byte{}, 1, 1, [32]byte{}, [][]byte{}, [][]byte{})
msgMixRS := NewMsgMixSecrets([33]byte{}, [32]byte{}, 1, 1, [32]byte{}, [][]byte{}, MixVect{})

tests := []struct {
in Message // Value to encode
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestMessage(t *testing.T) {
{msgMixSR, msgMixSR, pver, MainNet, 165},
{msgMixDC, msgMixDC, pver, MainNet, 185},
{msgMixCM, msgMixCM, pver, MainNet, 177},
{msgMixRS, msgMixRS, pver, MainNet, 209},
{msgMixRS, msgMixRS, pver, MainNet, 196},
}

t.Logf("Running %d tests", len(tests))
Expand Down
47 changes: 27 additions & 20 deletions wire/msgmixciphertexts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 The Decred developers
// Copyright (c) 2023-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -46,14 +46,14 @@ func (msg *MsgMixCiphertexts) BtcDecode(r io.Reader, pver uint32) error {
return err
}

// Count is of both Ciphertexts and seen KeyExchanges.
// Count is of both Ciphertexts and seen SeenKeyExchanges.
count, err := ReadVarInt(r, pver)
if err != nil {
return err
}
if count > MaxPrevMixMsgs {
if count > MaxMixPeers {
msg := fmt.Sprintf("too many previous referenced messages [count %v, max %v]",
count, MaxPrevMixMsgs)
count, MaxMixPeers)
return messageError(op, ErrTooManyPrevMixMsgs, msg)
}

Expand Down Expand Up @@ -117,14 +117,14 @@ func (msg *MsgMixCiphertexts) Hash() chainhash.Hash {
// panic. This method is designed to work only with hashers returned by
// blake256.New.
func (msg *MsgMixCiphertexts) WriteHash(h hash.Hash) {
h.Reset()
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, MixVersion)
sum := h.Sum(msg.hash[:0])
if len(sum) != len(msg.hash) {
if h.Size() != chainhash.HashSize {
s := fmt.Sprintf("hasher type %T has invalid Size() for chainhash.Hash", h)
panic(s)
}
h.Reset()
writeElement(h, &msg.Signature)
msg.writeMessageNoSignature("", h, MixVersion)
h.Sum(msg.hash[:0])
}

// writeMessageNoSignature serializes all elements of the message except for
Expand All @@ -136,23 +136,25 @@ func (msg *MsgMixCiphertexts) WriteHash(h hash.Hash) {
func (msg *MsgMixCiphertexts) writeMessageNoSignature(op string, w io.Writer, pver uint32) error {
_, hashing := w.(hash.Hash)

err := writeElements(w, &msg.Identity, &msg.SessionID, msg.Expiry,
msg.Run)
if err != nil {
return err
}

count := len(msg.Ciphertexts)
if !hashing && count != len(msg.SeenKeyExchanges) {
msg := "differing counts of ciphertexts and seen key exchange messages"
msg := fmt.Sprintf("differing counts of ciphertexts (%d) "+
"and seen key exchange messages (%d)", count,
len(msg.SeenKeyExchanges))
return messageError(op, ErrInvalidMsg, msg)
}
if !hashing && count > MaxPrevMixMsgs {
if !hashing && count > MaxMixPeers {
msg := fmt.Sprintf("too many previous referenced messages [count %v, max %v]",
count, MaxPrevMixMsgs)
count, MaxMixPeers)
return messageError(op, ErrTooManyPrevMixMsgs, msg)
}

err := writeElements(w, &msg.Identity, &msg.SessionID, msg.Expiry,
msg.Run)
if err != nil {
return err
}

err = WriteVarInt(w, pver, uint64(count))
if err != nil {
return err
Expand All @@ -173,10 +175,10 @@ func (msg *MsgMixCiphertexts) writeMessageNoSignature(op string, w io.Writer, pv
return nil
}

// WriteSigned writes a tag identifying the message data, followed by all
// WriteSignedData writes a tag identifying the message data, followed by all
// message fields excluding the signature. This is the data committed to when
// the message is signed.
func (msg *MsgMixCiphertexts) WriteSigned(h hash.Hash) {
func (msg *MsgMixCiphertexts) WriteSignedData(h hash.Hash) {
WriteVarString(h, MixVersion, CmdMixCiphertexts+"-sig")
msg.writeMessageNoSignature("", h, MixVersion)
}
Expand All @@ -190,6 +192,11 @@ func (msg *MsgMixCiphertexts) Command() string {
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgMixCiphertexts) MaxPayloadLength(pver uint32) uint32 {
if pver < MixVersion {
return 0
}

// See tests for this calculation.
return 552588
}

Expand Down
Loading

0 comments on commit 7b213b7

Please sign in to comment.