Skip to content

Commit

Permalink
Benchmarking stream garbling.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Aug 8, 2023
1 parent 8a38142 commit c684141
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
16 changes: 12 additions & 4 deletions circuit/stream_garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/cipher"
"crypto/rand"
"fmt"
"time"

"github.com/markkurossi/mpc/ot"
"github.com/markkurossi/mpc/p2p"
Expand Down Expand Up @@ -148,30 +149,37 @@ func (stream *Streaming) Set(w Wire, val ot.Wire) (index Wire, tmp bool) {

// Garble garbles the circuit and streams the garbled tables into the
// stream.
func (stream *Streaming) Garble(c *Circuit, in, out []Wire) error {
func (stream *Streaming) Garble(c *Circuit, in, out []Wire) (
time.Duration, time.Duration, error) {
if StreamDebug {
fmt.Printf(" - Streaming.Garble: in=%v, out=%v\n", in, out)
}

start := time.Now()

stream.initCircuit(c, in, out)

// Garble gates.

var data ot.LabelData
var id uint32
var table [4]ot.Label

mid := time.Now()

for i := 0; i < len(c.Gates); i++ {
gate := &c.Gates[i]
err := stream.conn.NeedSpace(512)
if err != nil {
return err
return 0, 0, err
}
err = stream.garbleGate(gate, &id, table[:], &data,
stream.conn.WriteBuf, &stream.conn.WritePos)
if err != nil {
return err
return 0, 0, err
}
}
return nil
return mid.Sub(start), time.Now().Sub(mid), nil
}

// GarbleGate garbles the gate and streams it to the stream.
Expand Down
35 changes: 18 additions & 17 deletions compiler/ssa/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ import (

// Program implements SSA program.
type Program struct {
Params *utils.Params
Inputs circuit.IO
Outputs circuit.IO
InputWires []*circuits.Wire
OutputWires []*circuits.Wire
Constants map[string]ConstantInst
Steps []Step
wires map[string]*wireAlloc
freeWires map[types.Size][][]*circuits.Wire
nextWireID uint32
zeroWire *circuits.Wire
oneWire *circuits.Wire
stats circuit.Stats
numWires int
garbleDuration time.Duration
Params *utils.Params
Inputs circuit.IO
Outputs circuit.IO
InputWires []*circuits.Wire
OutputWires []*circuits.Wire
Constants map[string]ConstantInst
Steps []Step
wires map[string]*wireAlloc
freeWires map[types.Size][][]*circuits.Wire
nextWireID uint32
zeroWire *circuits.Wire
oneWire *circuits.Wire
stats circuit.Stats
numWires int
tInit time.Duration
tGarble time.Duration
}

type wireAlloc struct {
Expand Down Expand Up @@ -118,9 +119,9 @@ func (prog *Program) allocWires(bits types.Size, assign bool) *wireAlloc {

fl, ok := prog.freeWires[bits]
if ok && len(fl) > 0 {
result.Wires = fl[0]
result.Wires = fl[len(fl)-1]
result.Base = result.Wires[0].ID()
prog.freeWires[bits] = fl[1:]
prog.freeWires[bits] = fl[:len(fl)-1]
} else {
result.Wires = circuits.MakeWires(bits)
}
Expand Down
36 changes: 20 additions & 16 deletions compiler/ssa/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,17 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, oti ot.OT,

start := time.Now()
lastReport := start
var circCompileDuration time.Duration

var dInstrInit time.Duration
var dCircCompile time.Duration

istats := make(map[string]circuit.Stats)

var wires [][]*circuits.Wire
var iIDs, oIDs []circuit.Wire

for idx, step := range prog.Steps {
dStart := time.Now()
if idx%10 == 0 && params.Verbose {
now := time.Now()
if now.Sub(lastReport) > time.Second*5 {
Expand Down Expand Up @@ -194,6 +197,7 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, oti ot.OT,
if params.Verbose && circuit.StreamDebug {
fmt.Printf("%05d: %s\n", idx, instr.String())
}
dInstrInit += time.Now().Sub(dStart)

switch instr.Op {

Expand Down Expand Up @@ -469,7 +473,7 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, oti ot.OT,
fmt.Printf("%05d: - %s\n", idx, circ)
}
circ.AssignLevels()
circCompileDuration += time.Now().Sub(startTime)
dCircCompile += time.Now().Sub(startTime)
}
if false {
circ.Dump()
Expand Down Expand Up @@ -502,12 +506,20 @@ func (prog *Program) StreamCircuit(conn *p2p.Conn, oti ot.OT,
ioStats = conn.Stats.Sum()
sample := timing.Sample("Stream", []string{circuit.FileSize(xfer).String()})
sample.Samples = append(sample.Samples, &circuit.Sample{
Label: "Compile",
Abs: circCompileDuration,
Label: "InstrInit",
Abs: dInstrInit,
})
sample.Samples = append(sample.Samples, &circuit.Sample{
Label: "CircComp",
Abs: dCircCompile,
})
sample.Samples = append(sample.Samples, &circuit.Sample{
Label: "StreamInit",
Abs: prog.tInit,
})
sample.Samples = append(sample.Samples, &circuit.Sample{
Label: "Garble",
Abs: prog.garbleDuration,
Abs: prog.tGarble,
})

result := new(big.Int)
Expand Down Expand Up @@ -652,20 +664,12 @@ func (prog *Program) garble(conn *p2p.Conn, streaming *circuit.Streaming,
if err := conn.SendUint32(int(maxID + 1)); err != nil {
return err
}

gStart := time.Now()
err := streaming.Garble(circ, in, out)
tInit, tGarble, err := streaming.Garble(circ, in, out)
if err != nil {
return err
}
dt := time.Now().Sub(gStart)
elapsed := float64(time.Now().UnixNano() - gStart.UnixNano())
elapsed /= 1000000000
if elapsed > 0 && false {
fmt.Printf("%05d: - garbled %10.0f gates/s (%s)\n",
step, float64(circ.NumGates)/elapsed, dt)
}
prog.garbleDuration += dt
prog.tInit += tInit
prog.tGarble += tGarble
prog.stats.Add(circ.Stats)
prog.numWires += circ.NumWires

Expand Down

0 comments on commit c684141

Please sign in to comment.