Skip to content

Commit

Permalink
wire-mix: hardcode msize, use [][20]byte for MixVect type
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Nov 27, 2023
1 parent 95461d6 commit 0027107
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
8 changes: 8 additions & 0 deletions wire/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ func writeElement(w io.Writer, element interface{}) error {
}
return nil

// Mixed message
case [MixMsgSize]byte:
_, err := w.Write(e[:])
if err != nil {
return err
}
return nil

case *[32]byte:
_, err := w.Write(e[:])
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions wire/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +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, 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{})
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{})

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestMessage(t *testing.T) {
{msgMixKE, msgMixKE, pver, MainNet, 1449},
{msgMixCT, msgMixCT, pver, MainNet, 166},
{msgMixSR, msgMixSR, pver, MainNet, 169},
{msgMixDC, msgMixDC, pver, MainNet, 171},
{msgMixDC, msgMixDC, pver, MainNet, 189},
{msgMixCM, msgMixCM, pver, MainNet, 181},
{msgMixRS, msgMixRS, pver, MainNet, 213},
}
Expand Down
30 changes: 7 additions & 23 deletions wire/mixvect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@ import (
"strings"
)

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

// 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 *MixVect) String() string {
m := func(i int) []byte {
off := uint32(i) * v.Msize
return v.Data[off : off+v.Msize]
}
// MixVect is vector of padded or unpadded DC-net messages.
type MixVect [][MixMsgSize]byte

func (v MixVect) String() string {
b := new(strings.Builder)
b.Grow(2 + int(v.N*(2*v.Msize+1)))
b.Grow(2 + len(v)*(2*MixMsgSize+1))
b.WriteString("[")
for i := 0; uint32(i) < v.N; i++ {
for i := range v {
if i != 0 {
b.WriteString(" ")
}
fmt.Fprintf(b, "%x", m(i))
fmt.Fprintf(b, "%x", v[i][:])
}
b.WriteString("]")
return b.String()
Expand Down
39 changes: 19 additions & 20 deletions wire/msgmixdcnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (msg *MsgMixDCNet) writeMessageNoSignature(op string, w io.Writer, pver uin
}

for i := range msg.DCNet {
err := writeMixVect(w, pver, &msg.DCNet[i])
err := writeMixVect(w, pver, msg.DCNet[i])
if err != nil {
return err
}
Expand All @@ -166,18 +166,21 @@ func (msg *MsgMixDCNet) writeMessageNoSignature(op string, w io.Writer, pver uin
return nil
}

func writeMixVect(w io.Writer, pver uint32, vec *MixVect) error {
err := WriteVarInt(w, pver, uint64(vec.N))
func writeMixVect(w io.Writer, pver uint32, vec MixVect) error {
err := WriteVarInt(w, pver, uint64(len(vec)))
if err != nil {
return err
}
err = WriteVarInt(w, pver, uint64(vec.Msize))
err = WriteVarInt(w, pver, MixMsgSize)
if err != nil {
return err
}
err = WriteVarBytes(w, pver, vec.Data)
if err != nil {
return err

for i := range vec {
_, err = w.Write(vec[i][:])
if err != nil {
return err
}
}

return nil
Expand All @@ -196,23 +199,19 @@ func readMixVect(op string, r io.Reader, pver uint32, vec *MixVect) error {
if err != nil {
return err
}
if msize > 32 {
msg := "mixed message length exceeds max"
if msize != MixMsgSize {
msg := fmt.Sprintf("mixed message length must be %d [got: %d]",
MixMsgSize, msize)
return messageError(op, ErrInvalidMsg, msg)
}
data, err := ReadVarBytes(r, pver, MaxMixKPCount*32, "Data")
if err != nil {
return err
}
if int(n*msize) != len(data) {
msg := "vec dimensions do not match data length"
return messageError(op, ErrInvalidMsg, msg)
*vec = make(MixVect, n)
for i := uint64(0); i < n; i++ {
_, err = r.Read((*vec)[i][:])
if err != nil {
return err
}
}

vec.N = uint32(n)
vec.Msize = uint32(msize)
vec.Data = data

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions wire/msgmixdcnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func TestMixDCWire(t *testing.T) {
// dcnet, ranging from 0x85 through 0x94
b := byte(0x85)
for i := 0; i < mcount; i++ {
dcnet[i].N = kpcount
dcnet[i].Msize = 32
dcnet[i] = make(MixVect, kpcount)
for j := 0; j < int(kpcount); j++ {
dcnet[i].Data = append(dcnet[i].Data, repeat(b, 32)...)
copy(dcnet[i][j][:], repeat(b, 32))
b++
}
}
Expand Down

0 comments on commit 0027107

Please sign in to comment.