Skip to content

Commit

Permalink
OT labels by value.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Mar 27, 2020
1 parent d007a30 commit 17bcbe3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 80 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,22 @@ Circuit: #gates=6769820 (XOR=4836732 XNOR=58368 AND=1874719 OR=0 INV=1)
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

Labels by value:

```
Circuit: #gates=6717340 (XOR=4787324 XNOR=108545 AND=1821471 OR=0 INV=0)
┏━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ Op ┃ Time ┃ % ┃ Xfer ┃
┣━━━━━━━━╋━━━━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
┃ Wait ┃ 6.117743762s ┃ 77.25% ┃ ┃
┃ Recv ┃ 1.196140342s ┃ 15.10% ┃ 172MB ┃
┃ Inputs ┃ 236.647371ms ┃ 2.99% ┃ 41kB ┃
┃ Eval ┃ 368.944904ms ┃ 4.66% ┃ ┃
┃ Result ┃ 347.483µs ┃ 0.00% ┃ 1kB ┃
┃ Total ┃ 7.919823862s ┃ ┃ ┃
┗━━━━━━━━┻━━━━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛
```

# Develoment ideas

## Mathematical operations
Expand Down
13 changes: 6 additions & 7 deletions circuit/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (
"github.com/markkurossi/mpc/ot"
)

func (c *Circuit) Eval(key []byte, wires []*ot.Label,
func (c *Circuit) Eval(key []byte, wires []ot.Label,
garbled [][][]byte) 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 []byte) ([]byte, error) {
return decrypt(alg, a, b, t, data)
}

for i := 0; i < len(c.Gates); i++ {
gate := &c.Gates[i]

var a *ot.Label
var b *ot.Label
var a ot.Label
var b ot.Label

switch gate.Op {
case XOR, XNOR, AND, OR:
Expand All @@ -46,9 +46,8 @@ func (c *Circuit) Eval(key []byte, wires []*ot.Label,

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

default:
row := garbled[i]
Expand Down
4 changes: 2 additions & 2 deletions circuit/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
garbled[i] = values
}

wires := make([]*ot.Label, circ.NumWires)
wires := make([]ot.Label, circ.NumWires)

// Receive peer inputs.
for i := 0; i < circ.N1.Size(); i++ {
Expand Down Expand Up @@ -144,7 +144,7 @@ func Evaluator(conn *p2p.Conn, circ *Circuit, inputs []*big.Int, verbose bool) (
}
timing.Sample("Eval", nil)

var labels []*ot.Label
var labels []ot.Label

for i := 0; i < circ.N3.Size(); i++ {
r := wires[Wire(circ.NumWires-circ.N3.Size()+i)]
Expand Down
58 changes: 29 additions & 29 deletions circuit/garble.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var (
verbose = false
)

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

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

type TableEntry struct {
Index int
Expand All @@ -45,15 +45,15 @@ func (a ByIndex) Less(i, j int) bool {
return a[i].Index < a[j].Index
}

func entry(enc Enc, a, b, c *ot.Label, tweak uint32) TableEntry {
func entry(enc Enc, a, b, c ot.Label, tweak uint32) TableEntry {
return TableEntry{
Index: idx(a, b),
Data: enc(a, b, c, tweak),
}
}

func idx(l0, l1 *ot.Label) int {
if l1 == nil {
func idx(l0, l1 ot.Label) int {
if l1.Undefined() {
if l0.S() {
return 1
} else {
Expand All @@ -73,7 +73,7 @@ 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) []byte {
k := makeK(a, b, t)

crypted := make([]byte, alg.BlockSize())
Expand All @@ -86,7 +86,7 @@ func encrypt(alg cipher.Block, a, b, c *ot.Label, t uint32) []byte {
return pi.Bytes()
}

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

k := makeK(a, b, t)
Expand All @@ -101,26 +101,24 @@ func decrypt(alg cipher.Block, a, b *ot.Label, t uint32, encrypted []byte) (
return c.Bytes(), nil
}

func makeK(a, b *ot.Label, t uint32) *ot.Label {
k := a.Copy()
k.Mul2()
func makeK(a, b ot.Label, t uint32) ot.Label {
a.Mul2()

if b != nil {
tmp := b.Copy()
tmp.Mul4()
k.Xor(tmp)
if !b.Undefined() {
b.Mul4()
a.Xor(b)
}
k.Xor(ot.NewTweak(t))
a.Xor(ot.NewTweak(t))

return k
return a
}

func makeLabels(r *ot.Label) (ot.Wire, error) {
func makeLabels(r ot.Label) (ot.Wire, error) {
l0, err := ot.NewLabel(rand.Reader)
if err != nil {
return ot.Wire{}, err
}
l1 := l0.Copy()
l1 := l0
l1.Xor(r)

return ot.Wire{
Expand All @@ -130,7 +128,7 @@ func makeLabels(r *ot.Label) (ot.Wire, error) {
}

type Garbled struct {
R *ot.Label
R ot.Label
Wires ot.Inputs
Gates [][][]byte
}
Expand All @@ -143,11 +141,13 @@ func (g *Garbled) Lambda(wire Wire) uint {
}

func (g *Garbled) SetLambda(wire Wire, val uint) {
w := g.Wires[int(wire)]
if val == 0 {
g.Wires[int(wire)].L0.SetS(false)
w.L0.SetS(false)
} else {
g.Wires[int(wire)].L0.SetS(true)
w.L0.SetS(true)
}
g.Wires[int(wire)] = w
}

func (c *Circuit) Garble(key []byte) (*Garbled, error) {
Expand All @@ -165,7 +165,7 @@ func (c *Circuit) Garble(key []byte) (*Garbled, error) {
return nil, err
}

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

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

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

var in []ot.Wire
Expand All @@ -227,21 +227,21 @@ func (g *Gate) Garble(wires ot.Inputs, enc Enc, r *ot.Label, id uint32) (
}
switch g.Op {
case XOR:
l0 := in[0].L0.Copy()
l0 := in[0].L0
l0.Xor(in[1].L0)

l1 := l0.Copy()
l1 := l0
l1.Xor(r)
w = ot.Wire{
L0: l0,
L1: l1,
}

case XNOR:
l0 := in[0].L0.Copy()
l0 := in[0].L0
l0.Xor(in[1].L0)

l1 := l0.Copy()
l1 := l0
l1.Xor(r)
w = ot.Wire{
L0: l1,
Expand Down Expand Up @@ -300,8 +300,8 @@ func (g *Gate) Garble(wires ot.Inputs, enc Enc, r *ot.Label, id uint32) (
// 1 0
a := in[0]
c := out[0]
table = append(table, entry(enc, a.L0, nil, c.L1, id))
table = append(table, entry(enc, a.L1, nil, c.L0, id))
table = append(table, entry(enc, a.L0, ot.Label{}, c.L1, id))
table = append(table, entry(enc, a.L1, ot.Label{}, c.L0, id))

default:
return nil, fmt.Errorf("Invalid operand %s", g.Op)
Expand Down
34 changes: 17 additions & 17 deletions circuit/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,34 +185,34 @@ func Player(nw *p2p.Network, circ *Circuit, inputs []*big.Int, verbose bool) (
if err != nil {
return nil, err
}
X1LongAg[peerID][g] = *rand1
X1LongAg[peerID][g] = rand1
Gs[peerID].Ag[g].Xor(rand1)

rand2, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
X1LongBg[peerID][g] = *rand2
X1LongBg[peerID][g] = rand2
Gs[peerID].Bg[g].Xor(rand2)

rand3, err := ot.NewLabel(rand.Reader)
if err != nil {
return nil, err
}
X1LongCg[peerID][g] = *rand3
X1LongCg[peerID][g] = rand3
Gs[peerID].Cg[g].Xor(rand3)

Gs[peerID].Dg[g].Xor(rand1)
Gs[peerID].Dg[g].Xor(rand2)
Gs[peerID].Dg[g].Xor(rand3)

X2LongAg[peerID][g] = *garbled.R.Copy()
X2LongAg[peerID][g] = garbled.R
X2LongAg[peerID][g].Xor(rand1)

X2LongBg[peerID][g] = *garbled.R.Copy()
X2LongBg[peerID][g] = garbled.R
X2LongBg[peerID][g].Xor(rand2)

X2LongCg[peerID][g] = *garbled.R.Copy()
X2LongCg[peerID][g] = garbled.R
X2LongCg[peerID][g].Xor(rand3)
}
}
Expand Down Expand Up @@ -258,13 +258,13 @@ func Player(nw *p2p.Network, circ *Circuit, inputs []*big.Int, verbose bool) (
case INV:

default:
Gs[result.peerID].Ag[g].Xor(&result.Ra[g])
Gs[result.peerID].Bg[g].Xor(&result.Rb[g])
Gs[result.peerID].Cg[g].Xor(&result.Rc[g])
Gs[result.peerID].Ag[g].Xor(result.Ra[g])
Gs[result.peerID].Bg[g].Xor(result.Rb[g])
Gs[result.peerID].Cg[g].Xor(result.Rc[g])

Gs[result.peerID].Dg[g].Xor(&result.Ra[g])
Gs[result.peerID].Dg[g].Xor(&result.Rb[g])
Gs[result.peerID].Dg[g].Xor(&result.Rc[g])
Gs[result.peerID].Dg[g].Xor(result.Ra[g])
Gs[result.peerID].Dg[g].Xor(result.Rb[g])
Gs[result.peerID].Dg[g].Xor(result.Rc[g])
}
}
}
Expand Down Expand Up @@ -316,10 +316,10 @@ func Player(nw *p2p.Network, circ *Circuit, inputs []*big.Int, verbose bool) (
case INV:

default:
Gs[result.peerID].Ag[g].Xor(&result.Ra[g])
Gs[result.peerID].Bg[g].Xor(&result.Rb[g])
Gs[result.peerID].Cg[g].Xor(&result.Rc[g])
Gs[result.peerID].Dg[g].Xor(&result.Rd[g])
Gs[result.peerID].Ag[g].Xor(result.Ra[g])
Gs[result.peerID].Bg[g].Xor(result.Rb[g])
Gs[result.peerID].Cg[g].Xor(result.Rc[g])
Gs[result.peerID].Dg[g].Xor(result.Rd[g])
}
}
for w := 0; w < circ.N3.Size(); w++ {
Expand Down Expand Up @@ -411,7 +411,7 @@ func arrayOfLabels(count int) []ot.Label {
if err != nil {
panic(err)
}
result[i] = *l
result[i] = l
}
return result
}
Loading

0 comments on commit 17bcbe3

Please sign in to comment.