Skip to content

Commit

Permalink
Made label parts public so garbler can inline label operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jan 30, 2022
1 parent e886613 commit f924ed0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
30 changes: 15 additions & 15 deletions ot/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,39 @@ func BenchmarkLabelXor(b *testing.B) {

func TestLabel(t *testing.T) {
label := &Label{
d0: 0xffffffffffffffff,
d1: 0xffffffffffffffff,
D0: 0xffffffffffffffff,
D1: 0xffffffffffffffff,
}

label.SetS(true)
if label.d0 != 0xffffffffffffffff {
if label.D0 != 0xffffffffffffffff {
t.Fatal("Failed to set S-bit")
}

label.SetS(false)
if label.d0 != 0x7fffffffffffffff {
t.Fatalf("Failed to clear S-bit: %x", label.d0)
if label.D0 != 0x7fffffffffffffff {
t.Fatalf("Failed to clear S-bit: %x", label.D0)
}

label = &Label{
d1: 0xffffffffffffffff,
D1: 0xffffffffffffffff,
}
label.Mul2()
if label.d0 != 0x1 {
t.Fatalf("Mul2 d0 failed")
if label.D0 != 0x1 {
t.Fatalf("Mul2 D0 failed")
}
if label.d1 != 0xfffffffffffffffe {
t.Fatalf("Mul2 d1 failed: %x", label.d1)
if label.D1 != 0xfffffffffffffffe {
t.Fatalf("Mul2 D1 failed: %x", label.D1)
}

label = &Label{
d1: 0xffffffffffffffff,
D1: 0xffffffffffffffff,
}
label.Mul4()
if label.d0 != 0x3 {
t.Fatalf("Mul4 d0 failed")
if label.D0 != 0x3 {
t.Fatalf("Mul4 D0 failed")
}
if label.d1 != 0xfffffffffffffffc {
t.Fatalf("Mul4 d1 failed")
if label.D1 != 0xfffffffffffffffc {
t.Fatalf("Mul4 D1 failed")
}
}
46 changes: 23 additions & 23 deletions ot/rsa.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// rsa.go
//
// Copyright (c) 2019-2021 Markku Rossi
// Copyright (c) 2019-2022 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -32,20 +32,20 @@ func RandomData(size int) ([]byte, error) {

// Label implements a 128 bit wire label.
type Label struct {
d0 uint64
d1 uint64
D0 uint64
D1 uint64
}

// LabelData contains lable data as byte array.
type LabelData [16]byte

func (l Label) String() string {
return fmt.Sprintf("%016x%016x", l.d0, l.d1)
return fmt.Sprintf("%016x%016x", l.D0, l.D1)
}

// Equal test if the labels are equal.
func (l Label) Equal(o Label) bool {
return l.d0 == o.d0 && l.d1 == o.d1
return l.D0 == o.D0 && l.D1 == o.D1
}

// NewLabel creates a new random label.
Expand All @@ -63,54 +63,54 @@ func NewLabel(rand io.Reader) (Label, error) {
// NewTweak creates a new label from the tweak value.
func NewTweak(tweak uint32) Label {
return Label{
d1: uint64(tweak),
D1: uint64(tweak),
}
}

// S tests the label's S bit.
func (l Label) S() bool {
return (l.d0 & 0x8000000000000000) != 0
return (l.D0 & 0x8000000000000000) != 0
}

// SetS sets the label's S bit.
func (l *Label) SetS(set bool) {
if set {
l.d0 |= 0x8000000000000000
l.D0 |= 0x8000000000000000
} else {
l.d0 &= 0x7fffffffffffffff
l.D0 &= 0x7fffffffffffffff
}
}

// Mul2 multiplies the label by 2.
func (l *Label) Mul2() {
l.d0 <<= 1
l.d0 |= (l.d1 >> 63)
l.d1 <<= 1
l.D0 <<= 1
l.D0 |= (l.D1 >> 63)
l.D1 <<= 1
}

// Mul4 multiplies the label by 4.
func (l *Label) Mul4() {
l.d0 <<= 2
l.d0 |= (l.d1 >> 62)
l.d1 <<= 2
l.D0 <<= 2
l.D0 |= (l.D1 >> 62)
l.D1 <<= 2
}

// Xor xors the label with the argument label.
func (l *Label) Xor(o Label) {
l.d0 ^= o.d0
l.d1 ^= o.d1
l.D0 ^= o.D0
l.D1 ^= o.D1
}

// GetData gets the labels as label data.
func (l Label) GetData(buf *LabelData) {
binary.BigEndian.PutUint64(buf[0:8], l.d0)
binary.BigEndian.PutUint64(buf[8:16], l.d1)
binary.BigEndian.PutUint64(buf[0:8], l.D0)
binary.BigEndian.PutUint64(buf[8:16], l.D1)
}

// SetData sets the labels from label data.
func (l *Label) SetData(data *LabelData) {
l.d0 = binary.BigEndian.Uint64((*data)[0:8])
l.d1 = binary.BigEndian.Uint64((*data)[8:16])
l.D0 = binary.BigEndian.Uint64((*data)[0:8])
l.D1 = binary.BigEndian.Uint64((*data)[8:16])
}

// Bytes returns the label data as bytes.
Expand All @@ -121,8 +121,8 @@ func (l Label) Bytes(buf *LabelData) []byte {

// SetBytes sets the label data from bytes.
func (l *Label) SetBytes(data []byte) {
l.d0 = binary.BigEndian.Uint64(data[0:8])
l.d1 = binary.BigEndian.Uint64(data[8:16])
l.D0 = binary.BigEndian.Uint64(data[0:8])
l.D1 = binary.BigEndian.Uint64(data[8:16])
}

// Wire implements a wire with 0 and 1 labels.
Expand Down

0 comments on commit f924ed0

Please sign in to comment.