Skip to content

Commit

Permalink
Optimized p2p.Conn.SendLabel() not to allocate memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Sep 3, 2021
1 parent 2d8300a commit c669bac
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 26 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,22 @@ Max permanent wires: 43892147, cached circuits: 26
#gates=936772905, #non-XOR=292263475
```

Optimizing p2p.Conn.SendLabel() not to allocate memory:

```
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━┩
│ Init │ 73.349µs │ 0.00% │ 276B │
│ OT Init │ 231.818904ms │ 0.13% │ 264B │
│ Peer Inputs │ 67.641816ms │ 0.04% │ 10kB │
│ Eval │ 2m57.583645976s │ 99.83% │ 25GB │
│ Total │ 2m57.883180045s │ │ 25GB │
└─────────────┴─────────────────┴────────┴──────┘
Max permanent wires: 43892147, cached circuits: 26
#gates=936772905, #non-XOR=292263475
```


## RSA signature computation

Expand Down
7 changes: 4 additions & 3 deletions circuit/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// garbler.go
// evaluator.go
//
// Copyright (c) 2019 Markku Rossi
// Copyright (c) 2019-2021 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -141,8 +141,9 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (
if err := conn.SendUint32(OpResult); err != nil {
return nil, err
}
var labelData ot.LabelData
for _, l := range labels {
if err := conn.SendLabel(l); err != nil {
if err := conn.SendLabel(l, &labelData); err != nil {
return nil, err
}
}
Expand Down
10 changes: 6 additions & 4 deletions circuit/garbler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (
if err := conn.SendUint32(len(garbled.Gates)); err != nil {
return nil, err
}
var labelData ot.LabelData
for _, data := range garbled.Gates {
if err := conn.SendUint32(len(data)); err != nil {
return nil, err
}
for _, d := range data {
if err := conn.SendLabel(d); err != nil {
if err := conn.SendLabel(d, &labelData); err != nil {
return nil, err
}
}
Expand All @@ -108,7 +109,7 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (
if verbose && false {
fmt.Printf("N1[%d]:\t%s\n", idx, i)
}
if err := conn.SendLabel(i); err != nil {
if err := conn.SendLabel(i, &labelData); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -169,8 +170,9 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs *big.Int, verbose bool) (

wire := garbled.Wires[bit]

m0Data := wire.L0.Bytes()
m1Data := wire.L1.Bytes()
var m0Buf, m1Buf ot.LabelData
m0Data := wire.L0.Bytes(&m0Buf)
m1Data := wire.L1.Bytes(&m1Buf)

xfer, err := sender.NewTransfer(m0Data, m1Data)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion circuit/stream_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,9 @@ loop:
if err := conn.SendUint32(OpResult); err != nil {
return nil, nil, err
}
var labelData ot.LabelData
for _, l := range labels {
if err := conn.SendLabel(l); err != nil {
if err := conn.SendLabel(l, &labelData); err != nil {
return nil, nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion circuit/stream_garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (stream *Streaming) GarbleGate(g *Gate, id uint32,
}

for i := 0; i < count; i++ {
if err := stream.conn.SendLabel(table[i]); err != nil {
if err := stream.conn.SendLabel(table[i], data); err != nil {
return err
}
}
Expand Down
12 changes: 7 additions & 5 deletions compiler/ssa/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, params *utils.Params,
}

// Send our inputs.
var labelData ot.LabelData
for idx, i := range n1 {
if params.Verbose && false {
fmt.Printf("N1[%d]:\t%s\n", idx, i)
}
if err := conn.SendLabel(i); err != nil {
if err := conn.SendLabel(i, &labelData); err != nil {
return nil, nil, err
}
}
Expand Down Expand Up @@ -131,8 +132,9 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, params *utils.Params,
}
wire := streaming.GetInput(circuit.Wire(bit))

m0Data := wire.L0.Bytes()
m1Data := wire.L1.Bytes()
var m0Buf, m1Buf ot.LabelData
m0Data := wire.L0.Bytes(&m0Buf)
m1Data := wire.L1.Bytes(&m1Buf)

xfer, err := sender.NewTransfer(m0Data, m1Data)
if err != nil {
Expand Down Expand Up @@ -535,6 +537,8 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, params *utils.Params,
}
}

result := new(big.Int)

op, err := conn.ReceiveUint32()
if err != nil {
return nil, nil, err
Expand All @@ -543,8 +547,6 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, params *utils.Params,
return nil, nil, fmt.Errorf("unexpected operation: %d", op)
}

result := new(big.Int)

for i := 0; i < prog.Outputs.Size(); i++ {
label, err := conn.ReceiveLabel()
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions ot/rsa.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// rsa.go
//
// Copyright (c) 2019 Markku Rossi
// Copyright (c) 2019-2021 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -186,8 +186,8 @@ func (l *Label) Xor(o Label) {

// 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.
Expand All @@ -197,9 +197,8 @@ func (l *Label) SetData(data *LabelData) {
}

// Bytes returns the label data as bytes.
func (l Label) Bytes() []byte {
var buf LabelData
l.GetData(&buf)
func (l Label) Bytes(buf *LabelData) []byte {
l.GetData(buf)
return buf[:]
}

Expand Down
10 changes: 6 additions & 4 deletions p2p/network.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2020 Markku Rossi
// Copyright (c) 2020-2021 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -456,8 +456,9 @@ func (peer *Peer) otrRespond(x1, x2 []ot.Label) error {
if err != nil {
return err
}
m0 := x1[bit].Bytes()
m1 := x2[bit].Bytes()
var m0Buf, m1Buf ot.LabelData
m0 := x1[bit].Bytes(&m0Buf)
m1 := x2[bit].Bytes(&m1Buf)

xfer, err := peer.otSender.NewTransfer(m0, m1)
if err != nil {
Expand Down Expand Up @@ -565,8 +566,9 @@ func (peer *Peer) exchangeSendArr(arr []ot.Label) (err error) {
if err := peer.conn.SendUint32(len(arr)); err != nil {
return err
}
var labelData ot.LabelData
for _, label := range arr {
if err := peer.conn.SendLabel(label); err != nil {
if err := peer.conn.SendLabel(label, &labelData); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (c *Conn) SendData(val []byte) error {
}

// SendLabel sends an OT label.
func (c *Conn) SendLabel(val ot.Label) error {
n, err := c.io.Write(val.Bytes())
func (c *Conn) SendLabel(val ot.Label, data *ot.LabelData) error {
n, err := c.io.Write(val.Bytes(data))
if err != nil {
return err
}
Expand Down

0 comments on commit c669bac

Please sign in to comment.