Skip to content

Commit

Permalink
Explicit ot.Labels in protocol, garbler, and evaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Mar 29, 2020
1 parent 97deb55 commit ae939f5
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 78 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@ Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

Labels by value in protocol, garbler, and evaluator:

```
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
┃ Wait ┃ 5.076677475s ┃ 78.67% ┃ ┃
┃ Recv ┃ 940.758975ms ┃ 14.58% ┃ 143MB ┃
┃ Inputs ┃ 229.741398ms ┃ 3.56% ┃ 41kB ┃
┃ Eval ┃ 205.513944ms ┃ 3.18% ┃ ┃
┃ Result ┃ 185.197µs ┃ 0.00% ┃ 1kB ┃
┃ Total ┃ 6.452876989s ┃ ┃ ┃
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

# Develoment ideas

## Mathematical operations
Expand Down
8 changes: 6 additions & 2 deletions apps/garbled/examples/bug.mpcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package main

func main(a, b uint1024) uint {
return a * b
import (
"math"
)

func main(b, m, e uint32) uint {
return math.Exp(b, e, m)
}
9 changes: 6 additions & 3 deletions apps/ot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func main() {
log.Fatal(err)
}

sXfer, err := sender.NewTransfer(m0.Bytes(), m1.Bytes())
m0Data := m0.Data()
m1Data := m1.Data()

sXfer, err := sender.NewTransfer(m0Data[:], m1Data[:])
if err != nil {
log.Fatal(err)
}
Expand All @@ -59,9 +62,9 @@ func main() {

var ret int
if bit == 0 {
ret = bytes.Compare(m0.Bytes(), m)
ret = bytes.Compare(m0Data[:], m)
} else {
ret = bytes.Compare(m1.Bytes(), m)
ret = bytes.Compare(m1Data[:], m)
}
if ret != 0 {
fmt.Printf("Verify failed!\n")
Expand Down
8 changes: 1 addition & 7 deletions circuit/enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
package circuit

import (
"bytes"
"crypto/aes"
"crypto/rand"
"encoding/hex"
"fmt"
"testing"

"github.com/markkurossi/mpc/ot"
Expand Down Expand Up @@ -41,10 +38,7 @@ func TestEnc(t *testing.T) {
t.Fatalf("Decrypt failed: %s", err)
}

if bytes.Compare(c.Bytes(), plain) != 0 {
if !c.Equal(plain) {
t.Fatalf("Encrypt-decrypt failed")
} else {
fmt.Printf("c:\n%s", hex.Dump(c.Bytes()))
fmt.Printf("plain:\n%s", hex.Dump(plain))
}
}
11 changes: 5 additions & 6 deletions circuit/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
)

func (c *Circuit) Eval(key []byte, wires []ot.Label,
garbled [][][]byte) error {
garbled [][]ot.Label) error {

alg, err := aes.NewCipher(key)
if err != nil {
return err
}
dec := func(a, b ot.Label, t uint32, data []byte) ([]byte, error) {
dec := func(a, b ot.Label, t uint32, data ot.Label) (ot.Label, error) {
return decrypt(alg, a, b, t, data)
}

Expand All @@ -42,12 +42,12 @@ func (c *Circuit) Eval(key []byte, wires []ot.Label,
return fmt.Errorf("invalid operation %s", gate.Op)
}

var output []byte
var output ot.Label

switch gate.Op {
case XOR, XNOR:
a.Xor(b)
output = a.Bytes()
output = a

default:
row := garbled[i]
Expand All @@ -61,8 +61,7 @@ func (c *Circuit) Eval(key []byte, wires []ot.Label,
return err
}
}

wires[gate.Output] = ot.LabelFromData(output)
wires[gate.Output] = output
}

return nil
Expand Down
20 changes: 11 additions & 9 deletions circuit/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (

timing := NewTiming()

garbled := make([][][]byte, circ.NumGates)
garbled := make([][]ot.Label, circ.NumGates)

// Receive program info.
if verbose {
Expand Down Expand Up @@ -56,14 +56,14 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
return nil, err
}

var values [][]byte
var values []ot.Label
for j := 0; j < count; j++ {
v, err := conn.ReceiveData()
v, err := conn.ReceiveLabel()
if err != nil {
return nil, err
}
if debug {
fmt.Printf("G%d.%d\t%x\n", i, j, v)
fmt.Printf("G%d.%d\t%s\n", i, j, v)
}
values = append(values, v)
}
Expand All @@ -74,14 +74,14 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (

// Receive peer inputs.
for i := 0; i < circ.N1.Size(); i++ {
n, err := conn.ReceiveData()
label, err := conn.ReceiveLabel()
if err != nil {
return nil, err
}
if debug {
fmt.Printf("N1[%d]:\t%x\n", i, n)
fmt.Printf("N1[%d]:\t%s\n", i, label)
}
wires[Wire(i)] = ot.LabelFromData(n)
wires[Wire(i)] = label
}

// Init oblivious transfer.
Expand Down Expand Up @@ -126,7 +126,9 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
if debug {
fmt.Printf("N2[%d]:\t%x\n", w, n)
}
wires[Wire(circ.N1.Size()+w)] = ot.LabelFromData(n)
var data ot.LabelData
copy(data[:], n)
wires[Wire(circ.N1.Size()+w)] = ot.LabelFromData(data)
w++
}
}
Expand Down Expand Up @@ -156,7 +158,7 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
return nil, err
}
for _, l := range labels {
if err := conn.SendData(l.Bytes()); err != nil {
if err := conn.SendLabel(l); err != nil {
return nil, err
}
}
Expand Down
38 changes: 20 additions & 18 deletions circuit/garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var (
verbose = false
)

type Enc func(a, b, c ot.Label, t uint32) []byte
type Enc func(a, b, c ot.Label, t uint32) ot.Label

type Dec func(a, b ot.Label, t uint32, data []byte) ([]byte, error)
type Dec func(a, b ot.Label, t uint32, data ot.Label) (ot.Label, error)

type TableEntry struct {
Index int
Data []byte
Data ot.Label
}

type ByIndex []TableEntry
Expand Down Expand Up @@ -73,32 +73,34 @@ func idx(l0, l1 ot.Label) int {
return ret
}

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

crypted := make([]byte, alg.BlockSize())
alg.Encrypt(crypted, k.Bytes())
var crypted ot.LabelData
alg.Encrypt(crypted[:], kData[:])

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

return pi.Bytes()
return pi
}

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

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

crypted := make([]byte, alg.BlockSize())
alg.Encrypt(crypted, k.Bytes())
var crypted ot.LabelData
alg.Encrypt(crypted[:], kData[:])

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

return c.Bytes(), nil
return c, nil
}

func makeK(a, b ot.Label, t uint32) ot.Label {
Expand Down Expand Up @@ -130,7 +132,7 @@ func makeLabels(r ot.Label) (ot.Wire, error) {
type Garbled struct {
R ot.Label
Wires ot.Inputs
Gates [][][]byte
Gates [][]ot.Label
}

func (g *Garbled) Lambda(wire Wire) uint {
Expand Down Expand Up @@ -158,14 +160,14 @@ func (c *Circuit) Garble(key []byte) (*Garbled, error) {
}
r.SetS(true)

garbled := make([][][]byte, c.NumGates)
garbled := make([][]ot.Label, c.NumGates)

alg, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

enc := func(a, b, c ot.Label, t uint32) []byte {
enc := func(a, b, c ot.Label, t uint32) ot.Label {
return encrypt(alg, a, b, c, t)
}

Expand Down Expand Up @@ -202,7 +204,7 @@ func (c *Circuit) Garble(key []byte) (*Garbled, error) {
}

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

var in []ot.Wire
var out []ot.Wire
Expand Down Expand Up @@ -309,7 +311,7 @@ func (g *Gate) Garble(wires ot.Inputs, enc Enc, r ot.Label, id uint32) (

sort.Sort(ByIndex(table))

var result [][]byte
var result []ot.Label
for _, entry := range table {
result = append(result, entry.Data)
}
Expand Down
26 changes: 14 additions & 12 deletions circuit/garbler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package circuit

import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
Expand Down Expand Up @@ -78,14 +77,14 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
return nil, err
}
for _, d := range data {
if err := conn.SendData(d); err != nil {
if err := conn.SendLabel(d); err != nil {
return nil, err
}
}
}

// Select our inputs.
var n1 [][]byte
var n1 []ot.Label
var w int
for idx, io := range circ.N1 {
var input *big.Int
Expand All @@ -96,12 +95,12 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
wire := garbled.Wires[w]
w++

var n []byte
var n ot.Label

if input != nil && input.Bit(i) == 1 {
n = wire.L1.Bytes()
n = wire.L1
} else {
n = wire.L0.Bytes()
n = wire.L0
}
n1 = append(n1, n)
}
Expand All @@ -110,9 +109,9 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
// Send our inputs.
for idx, i := range n1 {
if verbose && false {
fmt.Printf("N1[%d]:\t%x\n", idx, i)
fmt.Printf("N1[%d]:\t%s\n", idx, i)
}
if err := conn.SendData(i); err != nil {
if err := conn.SendLabel(i); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -177,7 +176,10 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
return nil, fmt.Errorf("unknown wire %d", bit)
}

xfer, err = sender.NewTransfer(wire.L0.Bytes(), wire.L1.Bytes())
m0Data := wire.L0.Data()
m1Data := wire.L1.Data()

xfer, err = sender.NewTransfer(m0Data[:], m1Data[:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -212,16 +214,16 @@ func Garbler(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (

case OP_RESULT:
for i := 0; i < circ.N3.Size(); i++ {
label, err := conn.ReceiveData()
label, err := conn.ReceiveLabel()
if err != nil {
return nil, err
}
wire := garbled.Wires[circ.NumWires-circ.N3.Size()+i]

var bit uint
if bytes.Compare(label, wire.L0.Bytes()) == 0 {
if label.Equal(wire.L0) {
bit = 0
} else if bytes.Compare(label, wire.L1.Bytes()) == 0 {
} else if label.Equal(wire.L1) {
bit = 1
} else {
return nil, fmt.Errorf("Unknown label %x for result %d",
Expand Down
Loading

0 comments on commit ae939f5

Please sign in to comment.