Skip to content

Commit

Permalink
wire-mix: rename types, cmds, and move files around to match
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Nov 22, 2023
1 parent 5b33984 commit 19c4358
Show file tree
Hide file tree
Showing 20 changed files with 413 additions and 405 deletions.
20 changes: 10 additions & 10 deletions wire/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ const (
// exceeds the maximum allowed.
ErrTooManyTSpends

// ErrTooManyManyMixPRs is returned when the number of mix pair
// ErrTooManyManyMixPairReqs is returned when the number of mix pair
// request message hashes exceeds the maximum allowed.
ErrTooManyManyMixPRs
ErrTooManyManyMixPairReqs

// ErrMixPRScriptClassTooLong is returned when a mixing script class
// type string is longer than allowed by the protocol.
ErrMixPRScriptClassTooLong
// ErrMixPairReqScriptClassTooLong is returned when a mixing script
// class type string is longer than allowed by the protocol.
ErrMixPairReqScriptClassTooLong

// ErrTooManyMixPRUTXOs is returned when a MixPR message contains
// more UTXOs than allowed by the protocol.
ErrTooManyMixPRUTXOs
// ErrTooManyMixPairReqUTXOs is returned when a MixPairReq message
// contains more UTXOs than allowed by the protocol.
ErrTooManyMixPairReqUTXOs

// ErrTooManyPrevMixMsgs is returned when too many previous messages of
// a mix run are referenced by a message.
Expand Down Expand Up @@ -184,8 +184,8 @@ var errorCodeStrings = map[ErrorCode]string{
ErrTooManyInitStateTypes: "ErrTooManyInitStateTypes",
ErrInitStateTypeTooLong: "ErrInitStateTypeTooLong",
ErrTooManyTSpends: "ErrTooManyTSpends",
ErrMixPRScriptClassTooLong: "ErrMixPRScriptClassTooLong",
ErrTooManyMixPRUTXOs: "ErrTooManyMixPRUTXOs",
ErrMixPairReqScriptClassTooLong: "ErrMixPairReqScriptClassTooLong",
ErrTooManyMixPairReqUTXOs: "ErrTooManyMixPairReqUTXOs",
ErrTooManyPrevMixMsgs: "ErrTooManyPrevMixMsgs",
}

Expand Down
4 changes: 2 additions & 2 deletions wire/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestMessageErrorCodeStringer(t *testing.T) {
{ErrTooManyInitStateTypes, "ErrTooManyInitStateTypes"},
{ErrInitStateTypeTooLong, "ErrInitStateTypeTooLong"},
{ErrTooManyTSpends, "ErrTooManyTSpends"},
{ErrMixPRScriptClassTooLong, "ErrMixPRScriptClassTooLong"},
{ErrTooManyMixPRUTXOs, "ErrTooManyMixPRUTXOs"},
{ErrMixPairReqScriptClassTooLong, "ErrMixPairReqScriptClassTooLong"},
{ErrTooManyMixPairReqUTXOs, "ErrTooManyMixPairReqUTXOs"},
{ErrTooManyPrevMixMsgs, "ErrTooManyPrevMixMsgs"},

{0xffff, "Unknown ErrorCode (65535)"},
Expand Down
42 changes: 21 additions & 21 deletions wire/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ const (
CmdCFilterV2 = "cfilterv2"
CmdGetInitState = "getinitstate"
CmdInitState = "initstate"
CmdMixPR = "mixpr"
CmdMixKE = "mixke"
CmdMixCT = "mixct"
CmdMixSR = "mixsr"
CmdMixDC = "mixdc"
CmdMixCM = "mixcm"
CmdMixRS = "mixrs"
CmdMixPairReq = "mixpairreq"
CmdMixKeyExchange = "mixkeyxchg"
CmdMixCiphertexts = "mixcphrtxt"
CmdMixSlotReserve = "mixslotres"
CmdMixDCNet = "mixdcnet"
CmdMixConfirm = "mixconfirm"
CmdMixSecrets = "mixsecrets"
)

// Message is an interface that describes a Decred message. A type that
Expand Down Expand Up @@ -175,26 +175,26 @@ func makeEmptyMessage(command string) (Message, error) {
case CmdInitState:
msg = &MsgInitState{}

case CmdMixPR:
msg = &MsgMixPR{}
case CmdMixPairReq:
msg = &MsgMixPairReq{}

case CmdMixKE:
msg = &MsgMixKE{}
case CmdMixKeyExchange:
msg = &MsgMixKeyExchange{}

case CmdMixCT:
msg = &MsgMixCT{}
case CmdMixCiphertexts:
msg = &MsgMixCiphertexts{}

case CmdMixSR:
msg = &MsgMixSR{}
case CmdMixSlotReserve:
msg = &MsgMixSlotReserve{}

case CmdMixDC:
msg = &MsgMixDC{}
case CmdMixDCNet:
msg = &MsgMixDCNet{}

case CmdMixCM:
msg = &MsgMixCM{}
case CmdMixConfirm:
msg = &MsgMixConfirm{}

case CmdMixRS:
msg = &MsgMixRS{}
case CmdMixSecrets:
msg = &MsgMixSecrets{}

default:
str := fmt.Sprintf("unhandled command [%s]", command)
Expand Down
14 changes: 7 additions & 7 deletions wire/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ func TestMessage(t *testing.T) {
msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
msgGetInitState := NewMsgGetInitState()
msgInitState := NewMsgInitState()
msgMixPR, err := NewMsgMixPR([33]byte{}, 1, 1, "", 1, 1, 1, 1, []MixPRUTXO{}, NewTxOut(0, []byte{}))
msgMixPR, err := NewMsgMixPairReq([33]byte{}, 1, 1, "", 1, 1, 1, 1, []MixPairReqUTXO{}, NewTxOut(0, []byte{}))
if err != nil {
t.Errorf("NewMsgMixPR: %v", err)
}
msgMixKE := NewMsgMixKE([33]byte{}, [32]byte{}, 1, 1, [33]byte{}, [1218]byte{}, [32]byte{}, []chainhash.Hash{})
msgMixCT := NewMsgMixCT([33]byte{}, [32]byte{}, 1, 1, [][1047]byte{}, []chainhash.Hash{})
msgMixSR := NewMsgMixSR([33]byte{}, [32]byte{}, 1, 1, [][][]byte{{{}}}, []chainhash.Hash{})
msgMixDC := NewMsgMixDC([33]byte{}, [32]byte{}, 1, 1, []MixVec{*NewMixVec(1, 1)}, []chainhash.Hash{})
msgMixCM := NewMsgMixCM([33]byte{}, [32]byte{}, 1, 1, NewMsgTx(), []chainhash.Hash{})
msgMixRS := NewMsgMixRS([33]byte{}, [32]byte{}, 1, 1, [32]byte{}, [][]byte{}, [][]byte{})
msgMixKE := NewMsgMixKeyExchange([33]byte{}, [32]byte{}, 1, 1, [33]byte{}, [1218]byte{}, [32]byte{}, []chainhash.Hash{})
msgMixCT := NewMsgMixCiphertexts([33]byte{}, [32]byte{}, 1, 1, [][1047]byte{}, []chainhash.Hash{})
msgMixSR := NewMsgMixSlotReserve([33]byte{}, [32]byte{}, 1, 1, [][][]byte{{{}}}, []chainhash.Hash{})
msgMixDC := NewMsgMixDCNet([33]byte{}, [32]byte{}, 1, 1, []MixVect{*NewMixVect(1, 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{})

tests := []struct {
in Message // Value to encode
Expand Down
12 changes: 6 additions & 6 deletions wire/mixvec.go → wire/mixvect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
"strings"
)

// MixVec is a N-element vector of Msize []byte messages.
type MixVec struct {
// MixVect is a N-element vector of Msize []byte messages.
type MixVect struct {
N uint32
Msize uint32
Data []byte
}

// NewMixVec returns a zero vector for holding n messages of msize length.
func NewMixVec(n, msize uint32) *MixVec {
return &MixVec{
// NewMixVect returns a zero vector for holding n messages of msize length.
func NewMixVect(n, msize uint32) *MixVect {
return &MixVect{
N: n,
Msize: msize,
Data: make([]byte, n*msize),
}
}

func (v *MixVec) String() string {
func (v *MixVect) String() string {
m := func(i int) []byte {
off := uint32(i) * v.Msize
return v.Data[off : off+v.Msize]
Expand Down
6 changes: 3 additions & 3 deletions wire/msginitstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (msg *MsgInitState) AddMixPRHash(hash *chainhash.Hash) error {
if len(msg.MixPRHashes)+1 > MaxISMixPRsPerMsg {
msg := fmt.Sprintf("too many mixpr hashes for message [max %v]",
MaxISMixPRsPerMsg)
return messageError(op, ErrTooManyManyMixPRs, msg)
return messageError(op, ErrTooManyManyMixPairReqs, msg)
}

msg.MixPRHashes = append(msg.MixPRHashes, *hash)
Expand Down Expand Up @@ -175,7 +175,7 @@ func (msg *MsgInitState) BtcDecode(r io.Reader, pver uint32) error {
if count > MaxISMixPRsPerMsg {
msg := fmt.Sprintf("too many mixpr hashes for message "+
"[count %v, max %v]", count, MaxISMixPRsPerMsg)
return messageError(op, ErrTooManyManyMixPRs, msg)
return messageError(op, ErrTooManyManyMixPairReqs, msg)
}

msg.MixPRHashes = make([]chainhash.Hash, count)
Expand Down Expand Up @@ -268,7 +268,7 @@ func (msg *MsgInitState) BtcEncode(w io.Writer, pver uint32) error {
if count > MaxISMixPRsPerMsg {
msg := fmt.Sprintf("too many mixpr hashes for message "+
"[count %v, max %v]", count, MaxISMixPRsPerMsg)
return messageError(op, ErrTooManyManyMixPRs, msg)
return messageError(op, ErrTooManyManyMixPairReqs, msg)
}

err = WriteVarInt(w, pver, uint64(count))
Expand Down
99 changes: 50 additions & 49 deletions wire/msgmixct.go → wire/msgmixciphertexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import (
"github.com/decred/dcrd/chaincfg/chainhash"
)

// MsgMixCT is used by mixing peers to share SNTRUP4591761 ciphertexts with
// other peers who have published their public keys. It implements the Message
// interface.
type MsgMixCT struct {
Signature [64]byte
Identity [33]byte
SessionID [32]byte
Expiry int64
Run uint32
Ciphertexts [][1047]byte
SeenKEs []chainhash.Hash
// MsgMixCiphertexts is used by mixing peers to share SNTRUP4591761
// ciphertexts with other peers who have published their public keys. It
// implements the Message interface.
type MsgMixCiphertexts struct {
Signature [64]byte
Identity [33]byte
SessionID [32]byte
Expiry int64
Run uint32
Ciphertexts [][1047]byte
SeenKeyExchanges []chainhash.Hash
}

// BtcDecode decodes r using the Decred protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgMixCT) BtcDecode(r io.Reader, pver uint32) error {
const op = "MsgMixCT.BtcDecode"
func (msg *MsgMixCiphertexts) BtcDecode(r io.Reader, pver uint32) error {
const op = "MsgMixCiphertexts.BtcDecode"
if pver < MixVersion {
msg := fmt.Sprintf("%s message invalid for protocol version %d",
msg.Command(), pver)
Expand All @@ -40,7 +40,7 @@ func (msg *MsgMixCT) BtcDecode(r io.Reader, pver uint32) error {
return err
}

// Count is of both Ciphertexts and SeenKEs.
// Count is of both Ciphertexts and seen KeyExchanges.
count, err := ReadVarInt(r, pver)
if err != nil {
return err
Expand All @@ -67,15 +67,15 @@ func (msg *MsgMixCT) BtcDecode(r io.Reader, pver uint32) error {
return err
}
}
msg.SeenKEs = seen
msg.SeenKeyExchanges = seen

return nil
}

// BtcEncode encodes the receiver to w using the Decred protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgMixCT) BtcEncode(w io.Writer, pver uint32) error {
const op = "MsgMixCT.BtcEncode"
func (msg *MsgMixCiphertexts) BtcEncode(w io.Writer, pver uint32) error {
const op = "MsgMixCiphertexts.BtcEncode"
if pver < MixVersion {
msg := fmt.Sprintf("%s message invalid for protocol version %d",
msg.Command(), pver)
Expand All @@ -98,16 +98,16 @@ func (msg *MsgMixCT) BtcEncode(w io.Writer, pver uint32) error {
// writeMessageNoSignature serializes all elements of the message except for
// the signature. This allows code reuse between message serialization, and
// signing and verifying these message contents.
func (msg *MsgMixCT) writeMessageNoSignature(op string, w io.Writer, pver uint32) error {
func (msg *MsgMixCiphertexts) writeMessageNoSignature(op string, w io.Writer, pver uint32) error {
err := writeElements(w, &msg.Identity, &msg.SessionID, msg.Expiry,
msg.Run)
if err != nil {
return err
}

count := len(msg.Ciphertexts)
if count != len(msg.SeenKEs) {
msg := "differing counts of ciphertexts and seen KE messages"
if count != len(msg.SeenKeyExchanges) {
msg := "differing counts of ciphertexts and seen key exchange messages"
return messageError(op, ErrInvalidMsg, msg)
}
if count > MaxPrevMixMsgs {
Expand All @@ -126,8 +126,8 @@ func (msg *MsgMixCT) writeMessageNoSignature(op string, w io.Writer, pver uint32
return err
}
}
for i := range msg.SeenKEs {
err = writeElement(w, &msg.SeenKEs[i])
for i := range msg.SeenKeyExchanges {
err = writeElement(w, &msg.SeenKeyExchanges[i])
if err != nil {
return err
}
Expand All @@ -139,10 +139,10 @@ func (msg *MsgMixCT) writeMessageNoSignature(op string, w io.Writer, pver uint32
// WriteSigned 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 *MsgMixCT) WriteSigned(w io.Writer) error {
const op = "MsgMixCT.WriteSigned"
func (msg *MsgMixCiphertexts) WriteSigned(w io.Writer) error {
const op = "MsgMixCiphertexts.WriteSigned"

err := WriteVarString(w, MixVersion, CmdMixCT+"-sig")
err := WriteVarString(w, MixVersion, CmdMixCiphertexts+"-sig")
if err != nil {
return err
}
Expand All @@ -157,62 +157,63 @@ func (msg *MsgMixCT) WriteSigned(w io.Writer) error {

// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgMixCT) Command() string {
return CmdMixCT
func (msg *MsgMixCiphertexts) Command() string {
return CmdMixCiphertexts
}

// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgMixCT) MaxPayloadLength(pver uint32) uint32 {
func (msg *MsgMixCiphertexts) MaxPayloadLength(pver uint32) uint32 {
return 552592
}

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

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

// GetSignature returns the message signature.
func (msg *MsgMixCT) GetSignature() []byte {
func (msg *MsgMixCiphertexts) GetSignature() []byte {
return msg.Signature[:]
}

// Expires returns the block height at which the message expires.
func (msg *MsgMixCT) Expires() int64 {
func (msg *MsgMixCiphertexts) Expires() int64 {
return msg.Expiry
}

// PrevMsgs returns the previous KE messages seen by the peer.
func (msg *MsgMixCT) PrevMsgs() []chainhash.Hash {
return msg.SeenKEs
// PrevMsgs returns the previous key exchange messages seen by the peer.
func (msg *MsgMixCiphertexts) PrevMsgs() []chainhash.Hash {
return msg.SeenKeyExchanges
}

// Sid returns the session ID.
func (msg *MsgMixCT) Sid() []byte {
func (msg *MsgMixCiphertexts) Sid() []byte {
return msg.SessionID[:]
}

// GetRun returns the run number.
func (msg *MsgMixCT) GetRun() uint32 {
func (msg *MsgMixCiphertexts) GetRun() uint32 {
return msg.Run
}

// NewMsgMixCT returns a new mixct message that conforms to the Message
// interface using the passed parameters and defaults for the remaining fields.
func NewMsgMixCT(identity [33]byte, sid [32]byte, expires int64, run uint32,
ciphertexts [][1047]byte, seenKEs []chainhash.Hash) *MsgMixCT {

return &MsgMixCT{
Identity: identity,
SessionID: sid,
Expiry: expires,
Run: run,
Ciphertexts: ciphertexts,
SeenKEs: seenKEs,
// NewMsgMixCiphertexts returns a new mixcphrtxt message that conforms to the
// Message interface using the passed parameters and defaults for the
// remaining fields.
func NewMsgMixCiphertexts(identity [33]byte, sid [32]byte, expires int64, run uint32,
ciphertexts [][1047]byte, seenKeyExchanges []chainhash.Hash) *MsgMixCiphertexts {

return &MsgMixCiphertexts{
Identity: identity,
SessionID: sid,
Expiry: expires,
Run: run,
Ciphertexts: ciphertexts,
SeenKeyExchanges: seenKeyExchanges,
}
}
Loading

0 comments on commit 19c4358

Please sign in to comment.