Skip to content

Commit

Permalink
Cleaned up ot.Label API.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Apr 9, 2020
1 parent 8c513c7 commit eb6a4c6
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 46 deletions.
6 changes: 3 additions & 3 deletions apps/ot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func main() {
log.Fatal(err)
}

m0Data := m0.Data()
m1Data := m1.Data()
m0Data := m0.Bytes()
m1Data := m1.Bytes()

sXfer, err := sender.NewTransfer(m0Data[:], m1Data[:])
sXfer, err := sender.NewTransfer(m0Data, m1Data)
if err != nil {
log.Fatal(err)
}
Expand Down
133 changes: 133 additions & 0 deletions circuit/enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ package circuit

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"io"
"testing"

"github.com/markkurossi/mpc/ot"
Expand Down Expand Up @@ -42,3 +45,133 @@ func TestEnc(t *testing.T) {
t.Fatalf("Encrypt-decrypt failed")
}
}

func BenchmarkEnc(b *testing.B) {
var key [32]byte

cipher, err := aes.NewCipher(key[:])
if err != nil {
b.Fatalf("Failed to create cipher: %s", err)
}

al, err := ot.NewLabel(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}
bl, err := ot.NewLabel(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}
cl, err := ot.NewLabel(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}

for i := 0; i < b.N; i++ {
encrypt(cipher, al, bl, cl, uint32(i))
}
}

func BenchmarkLabelX(b *testing.B) {
var key [32]byte

cipher, err := aes.NewCipher(key[:])
if err != nil {
b.Fatalf("Failed to create cipher: %s", err)
}

al, err := NewLabelX(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}
bl, err := NewLabelX(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}
cl, err := NewLabelX(rand.Reader)
if err != nil {
b.Fatalf("Failed to create label: %s", err)
}

for i := 0; i < b.N; i++ {
encryptX(cipher, al, bl, cl, uint32(i))
}
}

type LabelX struct {
d0 uint64
d1 uint64
}

func NewLabelX(rand io.Reader) (LabelX, error) {
var buf [16]byte
var result LabelX

_, err := rand.Read(buf[:])
if err != nil {
return result, err
}
result.SetData(&buf)

return result, nil
}

func (l *LabelX) SetData(buf *[16]byte) {
l.d0 = binary.BigEndian.Uint64((*buf)[0:8])
l.d1 = binary.BigEndian.Uint64((*buf)[8:16])
}

func (l *LabelX) GetData(buf *[16]byte) {
binary.BigEndian.PutUint64((*buf)[0:8], l.d0)
binary.BigEndian.PutUint64((*buf)[8:16], l.d1)
}

func (l *LabelX) Xor(o LabelX) {
l.d0 ^= o.d0
l.d1 ^= o.d1
}

func (l *LabelX) Mul2() {
l.d0 <<= 1
l.d0 |= (l.d1 >> 63)
l.d1 <<= 1
}

func (l *LabelX) Mul4() {
l.d0 <<= 2
l.d0 |= (l.d1 >> 62)
l.d1 <<= 2
}

func NewTweakX(tweak uint32) LabelX {
return LabelX{
d1: uint64(tweak),
}
}

func encryptX(alg cipher.Block, a, b, c LabelX, t uint32) LabelX {
k := makeKX(a, b, t)

var kData [16]byte
k.GetData(&kData)
alg.Encrypt(kData[:], kData[:])

var pi LabelX
pi.SetData(&kData)

pi.Xor(k)
pi.Xor(c)

return pi
}

func makeKX(a, b LabelX, t uint32) LabelX {
a.Mul2()

b.Mul4()
a.Xor(b)

a.Xor(NewTweakX(t))

return a
}
4 changes: 1 addition & 3 deletions circuit/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (
if err != nil {
return nil, err
}
var data ot.LabelData
copy(data[:], n)
wires[Wire(circ.Inputs[0].Size+w)] = ot.LabelFromData(data)
wires[Wire(circ.Inputs[0].Size+w)].SetBytes(n)
w++
}
xfer := conn.Stats.Sub(ioStats)
Expand Down
23 changes: 15 additions & 8 deletions circuit/garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,35 @@ func idx(l0, l1 ot.Label) int {

func encrypt(alg cipher.Block, a, b, c ot.Label, t uint32) ot.Label {
k := makeK(a, b, t)
kData := k.Data()

var kData ot.LabelData
k.GetData(&kData)

alg.Encrypt(kData[:], kData[:])

pi := ot.LabelFromData(kData)
var pi ot.Label
pi.SetData(&kData)

pi.Xor(k)
pi.Xor(c)

return pi
}

func decrypt(alg cipher.Block, a, b ot.Label, t uint32, encrypted ot.Label) (
func decrypt(alg cipher.Block, a, b ot.Label, t uint32, c ot.Label) (
ot.Label, error) {

k := makeK(a, b, t)
kData := k.Data()

var crypted ot.LabelData
alg.Encrypt(crypted[:], kData[:])
var kData ot.LabelData
k.GetData(&kData)

alg.Encrypt(kData[:], kData[:])

var crypted ot.Label
crypted.SetData(&kData)

c := encrypted
c.Xor(ot.LabelFromData(crypted))
c.Xor(crypted)
c.Xor(k)

return c, nil
Expand Down
6 changes: 3 additions & 3 deletions circuit/garbler.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (

wire := garbled.Wires[bit]

m0Data := wire.L0.Data()
m1Data := wire.L1.Data()
m0Data := wire.L0.Bytes()
m1Data := wire.L1.Bytes()

xfer, err = sender.NewTransfer(m0Data[:], m1Data[:])
xfer, err = sender.NewTransfer(m0Data, m1Data)
if err != nil {
return nil, err
}
Expand Down
32 changes: 15 additions & 17 deletions ot/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func prf() Label {
}

var label Label
label.InitFromData(prfBuffer[prfBlock*PRFBlockSize:])
label.SetBytes(prfBuffer[prfBlock*PRFBlockSize:])
prfBlock++

return label
Expand Down Expand Up @@ -116,7 +116,7 @@ func NewLabel(rand io.Reader) (Label, error) {
if _, err := rand.Read(buf[:]); err != nil {
return label, err
}
label.SetBytes(buf)
label.SetData(&buf)
return label, nil

case LabelPRF:
Expand All @@ -131,12 +131,6 @@ func NewLabel(rand io.Reader) (Label, error) {
}
}

func LabelFromData(data LabelData) Label {
label := Label{}
label.SetBytes(data)
return label
}

func NewTweak(tweak uint32) Label {
return Label{
d1: uint64(tweak),
Expand Down Expand Up @@ -172,19 +166,23 @@ func (l *Label) Xor(o Label) {
l.d1 ^= o.d1
}

func (l Label) Data() LabelData {
var result LabelData
binary.BigEndian.PutUint64(result[0:8], l.d0)
binary.BigEndian.PutUint64(result[8:16], l.d1)
return result
func (l Label) GetData(buf *LabelData) {
binary.BigEndian.PutUint64((*buf)[0:8], l.d0)
binary.BigEndian.PutUint64((*buf)[8:16], l.d1)
}

func (l *Label) SetBytes(data LabelData) {
l.d0 = binary.BigEndian.Uint64(data[0:8])
l.d1 = binary.BigEndian.Uint64(data[8:16])
func (l *Label) SetData(data *LabelData) {
l.d0 = binary.BigEndian.Uint64((*data)[0:8])
l.d1 = binary.BigEndian.Uint64((*data)[8:16])
}

func (l Label) Bytes() []byte {
var buf LabelData
l.GetData(&buf)
return buf[:]
}

func (l *Label) InitFromData(data []byte) {
func (l *Label) SetBytes(data []byte) {
l.d0 = binary.BigEndian.Uint64(data[0:8])
l.d1 = binary.BigEndian.Uint64(data[8:16])
}
Expand Down
6 changes: 3 additions & 3 deletions ot/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func benchmark(b *testing.B, keySize int) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
l0Data := l0.Data()
l1Data := l1.Data()
sXfer, err := sender.NewTransfer(l0Data[:], l1Data[:])
l0Data := l0.Bytes()
l1Data := l1.Bytes()
sXfer, err := sender.NewTransfer(l0Data, l1Data)
if err != nil {
b.Fatal(err)
}
Expand Down
10 changes: 4 additions & 6 deletions p2p/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ func (peer *Peer) OTRQuery(count int, choices *big.Int) ([]ot.Label, error) {
if err != nil {
return nil, err
}
var data ot.LabelData
copy(data[:], n)
result[i] = ot.LabelFromData(data)
result[i].SetBytes(n)
}

return result, nil
Expand Down Expand Up @@ -447,10 +445,10 @@ func (peer *Peer) OTRRespond(x1, x2 []ot.Label) error {
if err != nil {
return err
}
m0 := x1[bit].Data()
m1 := x2[bit].Data()
m0 := x1[bit].Bytes()
m1 := x2[bit].Bytes()

xfer, err := peer.otSender.NewTransfer(m0[:], m1[:])
xfer, err := peer.otSender.NewTransfer(m0, m1)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ func (c *Conn) SendData(val []byte) error {
}

func (c *Conn) SendLabel(val ot.Label) error {
data := val.Data()
n, err := c.io.Write(data[:])
n, err := c.io.Write(val.Bytes())
if err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +134,9 @@ func (c *Conn) ReceiveLabel() (ot.Label, error) {
}
c.Stats.Recvd += uint64(n)

return ot.LabelFromData(buf), nil
var result ot.Label
result.SetData(&buf)
return result, nil
}

func (c *Conn) Receive(receiver *ot.Receiver, wire, bit uint) ([]byte, error) {
Expand Down

0 comments on commit eb6a4c6

Please sign in to comment.