forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These messages implement the stages of a cspp mix. Messages will be broadcast to all full nodes and all peers participating in mixing through inventory messages.
- Loading branch information
Showing
22 changed files
with
2,596 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2023 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 ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// MixVec is a N-element vector of Msize []byte messages. | ||
type MixVec 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{ | ||
N: n, | ||
Msize: msize, | ||
Data: make([]byte, n*msize), | ||
} | ||
} | ||
|
||
// IsDim returns whether the Vec has dimensions n-by-msize. | ||
func (v *MixVec) IsDim(n, msize uint32) bool { | ||
return v.N == n && v.Msize == msize && len(v.Data) == int(n*msize) | ||
} | ||
|
||
// Equals returns whether the two vectors have equal dimensions and data. | ||
func (v *MixVec) Equals(other *MixVec) bool { | ||
return other.IsDim(v.N, v.Msize) && bytes.Equal(other.Data, v.Data) | ||
} | ||
|
||
// M returns the i'th message of the vector. | ||
func (v *MixVec) M(i int) []byte { | ||
off := uint32(i) * v.Msize | ||
return v.Data[off : off+v.Msize] | ||
} | ||
|
||
func (v *MixVec) String() string { | ||
b := new(strings.Builder) | ||
b.Grow(2 + int(v.N*(2*v.Msize+1))) | ||
b.WriteString("[") | ||
for i := 0; uint32(i) < v.N; i++ { | ||
if i != 0 { | ||
b.WriteString(" ") | ||
} | ||
fmt.Fprintf(b, "%x", v.M(i)) | ||
} | ||
b.WriteString("]") | ||
return b.String() | ||
} | ||
|
||
// Xor writes the xor of each vector element of src1 and src2 into v. | ||
// Source and destination vectors are allowed to be equal. | ||
// Panics if vectors do not share identical dimensions. | ||
func (v *MixVec) Xor(src1, src2 *MixVec) { | ||
switch { | ||
case v.N != src1.N, v.Msize != src1.Msize, len(v.Data) != len(src1.Data): | ||
fallthrough | ||
case v.N != src2.N, v.Msize != src2.Msize, len(v.Data) != len(src2.Data): | ||
panic("dcnet: vectors do not share identical dimensions") | ||
} | ||
for i := range v.Data { | ||
v.Data[i] = src1.Data[i] ^ src2.Data[i] | ||
} | ||
} | ||
|
||
// XorVectors calculates the xor of all vectors. | ||
// Panics if vectors do not share identical dimensions. | ||
func XorVectors(vs ...MixVec) MixVec { | ||
n := uint32(len(vs)) | ||
msize := vs[0].Msize | ||
res := NewMixVec(n, msize) | ||
for i := range vs { | ||
res.Xor(res, &vs[i]) | ||
} | ||
return *res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.