Skip to content

Commit

Permalink
Merge pull request #2 from markkurossi/topic/streaming
Browse files Browse the repository at this point in the history
Topic/streaming
  • Loading branch information
markkurossi authored Apr 13, 2020
2 parents 5b26e9d + 8e451d4 commit 33aebe4
Show file tree
Hide file tree
Showing 45 changed files with 2,705 additions and 517 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: Get dependencies
run: |
Expand Down
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,16 @@ form assembly:
- [ ] if-blocks
- [ ] For-loop unrolling
- [ ] Function call and return
- [ ] sort blocks in topological order
- [ ] peephole optimization over block boundaries
- [ ] peephole optimization
- [X] sort blocks in topological order
- [X] peephole optimization over block boundaries
- [ ] variable liveness analysis for templates
- [ ] Signed / unsigned arithmetics
- [ ] unary expressions
- [ ] logical not
- [ ] BitShift
- Circuit & garbling:
- [ ] Incremental (streaming) evaluation
- [X] Incremental (streaming) garbling and evaluation
- [ ] Row reduction
- [ ] Half AND
- [ ] Oblivious transfer extensions
Expand Down Expand Up @@ -343,6 +345,7 @@ Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
Labels by value in protocol, garbler, and evaluator:

```
Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
Expand All @@ -358,6 +361,7 @@ Labels by value in protocol, garbler, and evaluator:
Gate wires by value in garbler:

```
Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
Expand Down Expand Up @@ -387,6 +391,7 @@ Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
```

Pruning dead gates:

```
Circuit: #gates=5972956 (XOR=4315452 XNOR=53761 AND=1603743 OR=0 INV=0)
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
Expand All @@ -401,17 +406,34 @@ Circuit: #gates=5972956 (XOR=4315452 XNOR=53761 AND=1603743 OR=0 INV=0)
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

Optimized garbling:

```
Circuit: #gates=5972956 (XOR=4315452 XNOR=53761 AND=1603743 OR=0 INV=0)
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
┃ Wait ┃ 700.031233ms ┃ 38.57% ┃ ┃
┃ Recv ┃ 706.339086ms ┃ 38.92% ┃ 126MB ┃
┃ Inputs ┃ 233.615365ms ┃ 12.87% ┃ 41kB ┃
┃ Eval ┃ 174.84741ms ┃ 9.63% ┃ ┃
┃ Result ┃ 215.733µs ┃ 0.01% ┃ 1kB ┃
┃ Total ┃ 1.815048827s ┃ ┃ ┃
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

## RSA signature computation

| Input Size | MODP Size | Total gates | Non-XOR gates |
|-----------:|----------:|------------:|--------------:|
| 2 | 4 | 708 | 201 |
| 4 | 8 | 5596 | 1571 |
| 8 | 16 | 44796 | 12423 |
| 16 | 32 | 374844 | 102255 |
| 32 | 64 | 2986556 | 801887 |
| 64 | 128 | 23171068 | 6137023 |
| 128 | 256 | 177580028 | 46495359 |
| Input | MODP | Gates | Non-XOR | Stream gates | Stream non-XOR |
|------:|-----:|----------:|---------:|-------------:|---------------:|
| 2 | 4 | 708 | 201 | 730 | 205 |
| 4 | 8 | 5596 | 1571 | 5640 | 1579 |
| 8 | 16 | 44796 | 12423 | 44884 | 12439 |
| 16 | 32 | 374844 | 102255 | 375052 | 102287 |
| 32 | 64 | 2986556 | 801887 | 2986972 | 801951 |
| 64 | 128 | 23171068 | 6137023 | 23171900 | 6137151 |
| 128 | 256 | 177580028 | 46495359 | 177581692 | 46495615 |
| 256 | 512 | | | 1356768508 | 351848191 |

## Multiplication

Expand Down
6 changes: 4 additions & 2 deletions apps/garbled/GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

programs := $(wildcard examples/*.mpcl)
circs := $(patsubst %.mpcl,%.circ,$(programs))
mpclcs := $(patsubst %.mpcl,%.mpclc,$(programs))
bristols := $(patsubst %.mpcl,%.bristol,$(programs))
ssas := $(patsubst %.mpcl,%.ssa,$(programs))
dots := $(patsubst %,%.dot,$(circs) $(ssas))
dots := $(patsubst %,%.dot,$(circs) $(mpclcs) $(bristols) $(ssas))
svgs := $(patsubst %.dot,%.svg,$(dots))

all:
@echo $(svgs)

clean:
@rm -f $(circs) $(ssas) $(dots) $(svgs)
@rm -f $(circs) $(mpclcs) $(bristols) $(ssas) $(dots) $(svgs)
27 changes: 15 additions & 12 deletions apps/garbled/examples/bug.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import (
"crypto/rsa"
)

type Size = uint32
type Size = uint4

type Garbler struct {
msg Size
privShare Size
pubN Size
pubE Size
}
func main(a, b Size) (uint, uint) {
sum := count(a, b)
sum2 := count(b, a)

func main(g Garbler, privShare Size) (uint, uint) {
return sum, sum2
}

priv := g.privShare + privShare
func count(a, b uint) uint {
sumType := make(uint, size(b))

cipher := rsa.Encrypt(g.msg, g.pubE, g.pubN)
plain := rsa.Decrypt(cipher, priv, g.pubN)
var sum sumType

return cipher, plain
for i := 0; i < size(a); i++ {
if a>>i&1 == 1 {
sum += b
}
}
return sum
}
2 changes: 1 addition & 1 deletion apps/garbled/examples/credit.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package main

type Size = uint42
type Size = uint32

type Applicant struct {
male bool
Expand Down
6 changes: 4 additions & 2 deletions apps/garbled/examples/max3.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

package main

type Size = int4

type G struct {
a, b int
a, b Size
}

func main(g G, e int) int {
func main(g G, e Size) int {
if g.a > g.b {
if g.a > e {
return g.a
Expand Down
2 changes: 1 addition & 1 deletion apps/garbled/examples/mult.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

package main

func main(a, b uint64) uint {
func main(a, b uint2048) uint {
return a * b
}
3 changes: 3 additions & 0 deletions apps/garbled/examples/rsa.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
// msg: 0x6d7472
// cipher: 0x61f9ef88

// $ ./garbled -e -v -i 9 examples/rsa.mpcl
// $ ./garbled -v -i 0x6d7472,0x321af130,0xd60b2b09,0x10001 examples/rsa.mpcl

type Size = uint32

type Garbler struct {
Expand Down
4 changes: 2 additions & 2 deletions apps/garbled/examples/rsasign.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
// public: n, e

// msg: 0x6d7472
// cipher: 0x61f9ef88
// signature: 0x55a83b79

type Size = uint32
type Size = uint1024

type Garbler struct {
msg Size
Expand Down
31 changes: 22 additions & 9 deletions apps/garbled/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {

func main() {
evaluator := flag.Bool("e", false, "evaluator / garbler mode")
stream := flag.Bool("stream", false, "streaming mode")
compile := flag.Bool("circ", false, "compile MPCL to circuit")
circFormat := flag.String("format", "mpclc",
"circuit format: mpclc, bristol")
Expand All @@ -76,11 +77,6 @@ func main() {
var circ *circuit.Circuit
var err error

if len(flag.Args()) == 0 {
fmt.Printf("No input files\n")
os.Exit(1)
}

if len(*cpuprofile) > 0 {
f, err := os.Create(*cpuprofile)
if err != nil {
Expand All @@ -105,6 +101,24 @@ func main() {
params.NoCircCompile = true
}

if *stream {
if *evaluator {
err = streamEvaluatorMode(params, inputFlag, len(*cpuprofile) > 0)
} else {
err = streamGarblerMode(params, inputFlag, flag.Args())
}
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
return
}

if len(flag.Args()) == 0 {
fmt.Printf("No input files\n")
os.Exit(1)
}

for _, arg := range flag.Args() {
if strings.HasSuffix(arg, ".circ") ||
strings.HasSuffix(arg, ".bristol") ||
Expand Down Expand Up @@ -145,7 +159,6 @@ func main() {
}
}
}

circ, _, err = compiler.NewCompiler(params).CompileFile(arg)
if err != nil {
fmt.Printf("%s\n", err)
Expand All @@ -161,7 +174,7 @@ func main() {
fmt.Printf("Circuit: %v\n", circ)
}

if *ssa || *compile {
if *ssa || *compile || *stream {
return
}

Expand Down Expand Up @@ -203,7 +216,7 @@ func main() {
}

if len(circ.Inputs) != 2 {
fmt.Printf("Invalid circuit for 2-party computation: %d parties\n",
fmt.Printf("invalid circuit for 2-party computation: %d parties\n",
len(circ.Inputs))
return
}
Expand All @@ -220,7 +233,7 @@ func main() {
fmt.Printf(" %sIn1: %s\n", i1t, circ.Inputs[0])
fmt.Printf(" %sIn2: %s\n", i2t, circ.Inputs[1])
fmt.Printf(" - Out: %s\n", circ.Outputs)
fmt.Printf(" - In: %s\n", inputFlag)
fmt.Printf(" - In: %s\n", inputFlag)

if *evaluator {
input, err = circ.Inputs[1].Parse(inputFlag)
Expand Down
67 changes: 67 additions & 0 deletions apps/garbled/streaming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Copyright (c) 2020 Markku Rossi
//
// All rights reserved.
//

package main

import (
"fmt"
"io"
"net"
"strings"

"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/compiler"
"github.com/markkurossi/mpc/compiler/utils"
"github.com/markkurossi/mpc/p2p"
)

func streamEvaluatorMode(params *utils.Params, input input, once bool) error {
ln, err := net.Listen("tcp", port)
if err != nil {
return err
}
fmt.Printf("Listening for connections at %s\n", port)

for {
nc, err := ln.Accept()
if err != nil {
return err
}
fmt.Printf("New connection from %s\n", nc.RemoteAddr())

conn := p2p.NewConn(nc)
result, err := circuit.StreamEvaluator(conn, input, verbose)
conn.Close()

if err != nil && err != io.EOF {
return err
}

printResult(result)
if once {
return nil
}
}
}

func streamGarblerMode(params *utils.Params, input input, args []string) error {
if len(args) != 1 || !strings.HasSuffix(args[0], ".mpcl") {
return fmt.Errorf("streaming mode takes single MPCL file")
}
nc, err := net.Dial("tcp", port)
if err != nil {
return err
}
conn := p2p.NewConn(nc)
defer conn.Close()

result, err := compiler.NewCompiler(params).StreamFile(conn, args[0], input)
if err != nil {
return err
}
printResult(result)
return nil
}
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
2 changes: 1 addition & 1 deletion circuit/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (c *Circuit) String() string {
}
stats += fmt.Sprintf("%s=%d", k, v)
}
return fmt.Sprintf("#gates=%d (%s)", c.NumGates, stats)
return fmt.Sprintf("#gates=%d (%s) #w=%d", c.NumGates, stats, c.NumWires)
}

func (c *Circuit) Cost() int {
Expand Down
Loading

0 comments on commit 33aebe4

Please sign in to comment.