Skip to content

Commit

Permalink
Optimizing memory allocations and wire access with arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Mar 30, 2020
1 parent 1b40da1 commit 86f6187
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 60 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,21 @@ Gate wires by value in garbler:
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

Garbler keeping wires in an array instead of map:

```
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
┃ Wait ┃ 1.710655914s ┃ 53.63% ┃ ┃
┃ Recv ┃ 1.088403425s ┃ 34.12% ┃ 143MB ┃
┃ Inputs ┃ 243.393526ms ┃ 7.63% ┃ 41kB ┃
┃ Eval ┃ 146.879726ms ┃ 4.61% ┃ ┃
┃ Result ┃ 222.8µs ┃ 0.01% ┃ 1kB ┃
┃ Total ┃ 3.189555391s ┃ ┃ ┃
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

# Develoment ideas

## Mathematical operations
Expand Down
4 changes: 2 additions & 2 deletions circuit/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
return nil, err
}

var values []ot.Label
values := make([]ot.Label, count)
for j := 0; j < count; j++ {
v, err := conn.ReceiveLabel()
if err != nil {
return nil, err
}
values = append(values, v)
values[j] = v
}
garbled[i] = values
}
Expand Down
57 changes: 18 additions & 39 deletions circuit/garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func makeLabels(r ot.Label) (ot.Wire, error) {

type Garbled struct {
R ot.Label
Wires ot.Inputs
Wires []ot.Wire
Gates [][]ot.Label
}

Expand Down Expand Up @@ -173,9 +173,19 @@ func (c *Circuit) Garble(key []byte) (*Garbled, error) {
return encrypt(alg, a, b, c, t)
}

// Assign labels to wires.
wires := make(ot.Inputs)
// Wire labels.
wires := make([]ot.Wire, c.NumWires)

// Assing all input wires.
for i := 0; i < c.N1.Size()+c.N2.Size(); i++ {
w, err := makeLabels(r)
if err != nil {
return nil, err
}
wires[i] = w
}

// Garble gates.
for i := 0; i < len(c.Gates); i++ {
gate := &c.Gates[i]
data, err := gate.Garble(wires, enc, r, uint32(i))
Expand All @@ -185,64 +195,33 @@ func (c *Circuit) Garble(key []byte) (*Garbled, error) {
garbled[i] = data
}

// Assign unset wires. Wire can be unset if one of the inputs is
// not used in the computation
for i := 0; i < c.NumWires; i++ {
_, ok := wires[i]
if !ok {
w, err := makeLabels(r)
if err != nil {
return nil, err
}
wires[i] = w
}
}

return &Garbled{
R: r,
Wires: wires,
Gates: garbled,
}, nil
}

func (g *Gate) Garble(wires ot.Inputs, enc Enc, r ot.Label, id uint32) (
func (g *Gate) Garble(wires []ot.Wire, enc Enc, r ot.Label, id uint32) (
[]ot.Label, error) {

var a, b, c ot.Wire
var err error
var ok bool

// Inputs.
switch g.Op {
case XOR, XNOR, AND, OR:
b, ok = wires[g.Input1.ID()]
if !ok {
b, err = makeLabels(r)
if err != nil {
return nil, err
}
wires[g.Input1.ID()] = b
}
b = wires[g.Input1.ID()]
fallthrough

case INV:
a, ok = wires[g.Input0.ID()]
if !ok {
a, err = makeLabels(r)
if err != nil {
return nil, err
}
wires[g.Input0.ID()] = a
}
a = wires[g.Input0.ID()]

default:
return nil, fmt.Errorf("invalid gate type %s", g.Op)
}

// Output
c, ok = wires[g.Output.ID()]
if ok {
return nil, fmt.Errorf("gate output already set %d", g.Output)
}
// Output.
switch g.Op {
case XOR:
l0 := a.L0
Expand Down
5 changes: 1 addition & 4 deletions circuit/garbler.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
}
allowedOTs[bit] = false

wire, ok := garbled.Wires[bit]
if !ok {
return nil, fmt.Errorf("unknown wire %d", bit)
}
wire := garbled.Wires[bit]

m0Data := wire.L0.Data()
m1Data := wire.L1.Data()
Expand Down
15 changes: 0 additions & 15 deletions ot/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,6 @@ type Wire struct {
L1 Label
}

type Inputs map[int]Wire

func (i Inputs) String() string {
var result string

for k, v := range i {
str := fmt.Sprintf("%d={%x,%x}", k, v.L0, v.L1)
if len(result) > 0 {
result += ", "
}
result += str
}
return result
}

type Sender struct {
key *rsa.PrivateKey
}
Expand Down

0 comments on commit 86f6187

Please sign in to comment.