Skip to content

Commit

Permalink
wire: Add MsgMixFactoredPoly
Browse files Browse the repository at this point in the history
This message contains the result of the factored slot reservation polynomial.
Clients that are capable of solving this (with optionally-installed
csppsolver) will publish the result for clients that don't.
  • Loading branch information
jrick committed May 6, 2024
1 parent 5334565 commit 7e348d3
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 31 deletions.
66 changes: 35 additions & 31 deletions wire/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,38 @@ const MaxMessagePayload = (1024 * 1024 * 32) // 32MB

// Commands used in message headers which describe the type of message.
const (
CmdVersion = "version"
CmdVerAck = "verack"
CmdGetAddr = "getaddr"
CmdAddr = "addr"
CmdGetBlocks = "getblocks"
CmdInv = "inv"
CmdGetData = "getdata"
CmdNotFound = "notfound"
CmdBlock = "block"
CmdTx = "tx"
CmdGetHeaders = "getheaders"
CmdHeaders = "headers"
CmdPing = "ping"
CmdPong = "pong"
CmdMemPool = "mempool"
CmdMiningState = "miningstate"
CmdGetMiningState = "getminings"
CmdReject = "reject"
CmdSendHeaders = "sendheaders"
CmdFeeFilter = "feefilter"
CmdGetCFilterV2 = "getcfilterv2"
CmdCFilterV2 = "cfilterv2"
CmdGetInitState = "getinitstate"
CmdInitState = "initstate"
CmdMixPairReq = "mixpairreq"
CmdMixKeyExchange = "mixkeyxchg"
CmdMixCiphertexts = "mixcphrtxt"
CmdMixSlotReserve = "mixslotres"
CmdMixDCNet = "mixdcnet"
CmdMixConfirm = "mixconfirm"
CmdMixSecrets = "mixsecrets"
CmdVersion = "version"
CmdVerAck = "verack"
CmdGetAddr = "getaddr"
CmdAddr = "addr"
CmdGetBlocks = "getblocks"
CmdInv = "inv"
CmdGetData = "getdata"
CmdNotFound = "notfound"
CmdBlock = "block"
CmdTx = "tx"
CmdGetHeaders = "getheaders"
CmdHeaders = "headers"
CmdPing = "ping"
CmdPong = "pong"
CmdMemPool = "mempool"
CmdMiningState = "miningstate"
CmdGetMiningState = "getminings"
CmdReject = "reject"
CmdSendHeaders = "sendheaders"
CmdFeeFilter = "feefilter"
CmdGetCFilterV2 = "getcfilterv2"
CmdCFilterV2 = "cfilterv2"
CmdGetInitState = "getinitstate"
CmdInitState = "initstate"
CmdMixPairReq = "mixpairreq"
CmdMixKeyExchange = "mixkeyxchg"
CmdMixCiphertexts = "mixcphrtxt"
CmdMixSlotReserve = "mixslotres"
CmdMixFactoredPoly = "mixfactpoly"
CmdMixDCNet = "mixdcnet"
CmdMixConfirm = "mixconfirm"
CmdMixSecrets = "mixsecrets"
)

const (
Expand Down Expand Up @@ -207,6 +208,9 @@ func makeEmptyMessage(command string) (Message, error) {
case CmdMixSlotReserve:
msg = &MsgMixSlotReserve{}

case CmdMixFactoredPoly:
msg = &MsgMixFactoredPoly{}

case CmdMixDCNet:
msg = &MsgMixDCNet{}

Expand Down
2 changes: 2 additions & 0 deletions wire/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestMessage(t *testing.T) {
msgMixKE := NewMsgMixKeyExchange([33]byte{}, [32]byte{}, 1, 1, [33]byte{}, [1218]byte{}, [32]byte{}, []chainhash.Hash{})
msgMixCT := NewMsgMixCiphertexts([33]byte{}, [32]byte{}, 1, [][1047]byte{}, []chainhash.Hash{})
msgMixSR := NewMsgMixSlotReserve([33]byte{}, [32]byte{}, 1, [][][]byte{{{}}}, []chainhash.Hash{})
msgMixFP := NewMsgMixFactoredPoly([33]byte{}, [32]byte{}, 1, [][]byte{}, []uint8{})
msgMixDC := NewMsgMixDCNet([33]byte{}, [32]byte{}, 1, []MixVect{make(MixVect, 1)}, []chainhash.Hash{})
msgMixCM := NewMsgMixConfirm([33]byte{}, [32]byte{}, 1, NewMsgTx(), []chainhash.Hash{})
msgMixRS := NewMsgMixSecrets([33]byte{}, [32]byte{}, 1, [32]byte{}, [][]byte{}, MixVect{})
Expand Down Expand Up @@ -126,6 +127,7 @@ func TestMessage(t *testing.T) {
{msgMixKE, msgMixKE, pver, MainNet, 1449},
{msgMixCT, msgMixCT, pver, MainNet, 158},
{msgMixSR, msgMixSR, pver, MainNet, 161},
{msgMixFP, msgMixFP, pver, MainNet, 158},
{msgMixDC, msgMixDC, pver, MainNet, 181},
{msgMixCM, msgMixCM, pver, MainNet, 173},
{msgMixRS, msgMixRS, pver, MainNet, 192},
Expand Down
247 changes: 247 additions & 0 deletions wire/msgmixfactoredpoly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Copyright (c) 2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package wire

import (
"fmt"
"hash"
"io"

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

// MsgMixFactoredPoly encodes the solution of the factored slot reservation
// polynomial.
type MsgMixFactoredPoly struct {
Signature [64]byte
Identity [33]byte
SessionID [32]byte
Run uint32
Roots [][]byte
Exponents []uint8

// hash records the hash of the message. It is a member of the
// message for convenience and performance, but is never automatically
// set during creation or deserialization.
hash chainhash.Hash
}

// BtcDecode decodes r using the Decred protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgMixFactoredPoly) BtcDecode(r io.Reader, pver uint32) error {
const op = "MsgMixFactoredPoly.BtcDecode"
if pver < MixVersion {
msg := fmt.Sprintf("%s message invalid for protocol version %d",
msg.Command(), pver)
return messageError(op, ErrMsgInvalidForPVer, msg)
}

err := readElements(r, &msg.Signature, &msg.Identity, &msg.SessionID,
&msg.Run)
if err != nil {
return err
}

count, err := ReadVarInt(r, pver)
if err != nil {
return err
}
if count > MaxMixMcount {
msg := fmt.Sprintf("too many roots in message [count %v, max %v]",
count, MaxMixMcount)
return messageError(op, ErrInvalidMsg, msg)
}

roots := make([][]byte, count)
for i := range roots {
root, err := ReadVarBytes(r, pver, MaxMixFieldValLen, "MixFactoredPoly.Roots")
if err != nil {
return err
}
roots[i] = root
}
msg.Roots = roots

exps := make([]uint8, count)
for i := range exps {
var exp uint8
err := readElement(r, &exp)
if err != nil {
return err
}
exps[i] = exp
}
msg.Exponents = exps

return nil
}

// BtcEncode encodes the receiver to w using the Decred protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgMixFactoredPoly) BtcEncode(w io.Writer, pver uint32) error {
const op = "MsgMixFactoredPoly.BtcEncode"
if pver < MixVersion {
msg := fmt.Sprintf("%s message invalid for protocol version %d",
msg.Command(), pver)
return messageError(op, ErrMsgInvalidForPVer, msg)
}

err := writeElement(w, &msg.Signature)
if err != nil {
return err
}

err = msg.writeMessageNoSignature(op, w, pver)
if err != nil {
return err
}

return nil
}

// Hash returns the message hash calculated by WriteHash.
//
// Hash returns an invalid or zero hash if WriteHash has not been called yet.
//
// This method is not safe while concurrently calling WriteHash.
func (msg *MsgMixFactoredPoly) Hash() chainhash.Hash {
return msg.hash
}

// WriteHash serializes the message to a hasher and records the sum in the
// message's Hash field.
//
// The hasher's Size() must equal chainhash.HashSize, or this method will
// panic. This method is designed to work only with hashers returned by
// blake256.New.
func (msg *MsgMixFactoredPoly) 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) {
s := fmt.Sprintf("hasher type %T has invalid Size() for chainhash.Hash", h)
panic(s)
}
}

// 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.
//
// If w implements hash.Hash, no errors will be returned for invalid message
// construction.
func (msg *MsgMixFactoredPoly) writeMessageNoSignature(op string, w io.Writer, pver uint32) error {
_, hashing := w.(hash.Hash)

count := len(msg.Roots)
if !hashing && count != len(msg.Exponents) {
msg := fmt.Sprintf("root count (%d) does not equal number of exponents (%d)",
count, len(msg.Exponents))
return messageError(op, ErrInvalidMsg, msg)
}
if !hashing && count > MaxMixMcount {
msg := fmt.Sprintf("too many solutions to factored polynomial [count %v, max %v]",
count, MaxMixMcount)
return messageError(op, ErrInvalidMsg, msg)
}
for _, root := range msg.Roots {
if !hashing && len(root) > MaxMixFieldValLen {
msg := "root exceeds bytes necessary to represent number in field"
return messageError(op, ErrInvalidMsg, msg)
}
}

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

err = WriteVarInt(w, pver, uint64(count))
if err != nil {
return err
}
for _, root := range msg.Roots {
err := WriteVarBytes(w, pver, root)
if err != nil {
return err
}
}

for _, exp := range msg.Exponents {
err := writeElement(w, exp)
if err != nil {
return err
}
}

return nil
}

// 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 *MsgMixFactoredPoly) WriteSignedData(h hash.Hash) {
WriteVarString(h, MixVersion, CmdMixPairReq+"-sig")
msg.writeMessageNoSignature("", h, MixVersion)
}

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

// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgMixFactoredPoly) MaxPayloadLength(pver uint32) uint32 {
if pver < MixVersion {
return 0
}

// See tests for this calculation.
return 33928
}

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

// Sig returns the message signature.
func (msg *MsgMixFactoredPoly) Sig() []byte {
return msg.Signature[:]
}

// PrevMsgs always returns nil. Factored polynomial messages do not reference
// previous messages.
func (msg *MsgMixFactoredPoly) PrevMsgs() []chainhash.Hash {
return nil
}

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

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

// NewMsgMixFactoredPoly returns a new mixpairreq message that conforms to the
// Message interface using the passed parameters and defaults for the
// remaining fields.
func NewMsgMixFactoredPoly(identity [33]byte, sid [32]byte, run uint32,
roots [][]byte, exponents []uint8) *MsgMixFactoredPoly {

return &MsgMixFactoredPoly{
Identity: identity,
SessionID: sid,
Run: run,
Roots: roots,
Exponents: exponents,
}
}
Loading

0 comments on commit 7e348d3

Please sign in to comment.