Skip to content

Commit

Permalink
remove xor from wire mixvec type; mixing type will implement this
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Aug 7, 2023
1 parent 31520c3 commit 052a8d9
Showing 1 changed file with 6 additions and 44 deletions.
50 changes: 6 additions & 44 deletions wire/mixvec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,59 +26,21 @@ func NewMixVec(n, msize uint32) *MixVec {
}
}

// 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 {
m := func(i int) []byte {
off := uint32(i) * v.Msize
return v.Data[off : off+v.Msize]
}

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))
fmt.Fprintf(b, "%x", 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
}

0 comments on commit 052a8d9

Please sign in to comment.