Commit 51765d10 authored by Anders Jensen Løvig's avatar Anders Jensen Løvig
Browse files

Copy all table getters

parent 401d7c05
...@@ -69,24 +69,25 @@ func NewParty(in chan []byte, out chan []byte, obliv *oblivious.Party) (p *Party ...@@ -69,24 +69,25 @@ func NewParty(in chan []byte, out chan []byte, obliv *oblivious.Party) (p *Party
func (alice *Party) RunAlice(x int) (err error) { func (alice *Party) RunAlice(x int) (err error) {
// 1. Garble // 1. Garble
F, _ := garbled.NewTable(NumWires-NumInputs*2, 4, K*2) // F, _ := garbled.NewTable(NumWires-NumInputs*2, 4, K*2)
F.SetData(<-alice.receive) alice.circuit.F.SetData(<-alice.receive)
fmt.Println("Alice: Received F from Bob") fmt.Println("Alice: Received F from Bob")
// 2. Encode Bob's input // 2. Encode Bob's input
fmt.Println("Alice: Received Y from Bob")
Y := <-alice.receive Y := <-alice.receive
fmt.Println("Alice: Received Y from Bob")
// 3. Encode Alice's input // 3. Encode Alice's input
X, err := alice.obliv.Receive(x) X, err := alice.obliv.Receive(x)
fmt.Println("Alice: Oblivious transfer encoding from Bob")
if err != nil { if err != nil {
return return
} }
// 4. Evaluation // 4. Evaluation
evalInput := append(X, Y...) evalInput := append(X, Y...)
fmt.Println(len(evalInput)) // fmt.Println(len(evalInput))
_, err = garbled.Evaluate(F, evalInput) _, err = alice.circuit.Evaluate(evalInput)
if err != nil { if err != nil {
return return
} }
...@@ -96,15 +97,15 @@ func (alice *Party) RunAlice(x int) (err error) { ...@@ -96,15 +97,15 @@ func (alice *Party) RunAlice(x int) (err error) {
func (bob *Party) RunBob(y int) (err error) { func (bob *Party) RunBob(y int) (err error) {
// 1. Garble // 1. Garble
bob.Circuit, err = garbled.NewGarbBloodCircuit(NumInputs, NumWires, K) err = bob.circuit.GarbleBloodCircuit()
if err != nil { if err != nil {
return return
} }
fmt.Println("Bob: Sending F to Alice") fmt.Println("Bob: Sending F to Alice")
bob.send <- bob.Circuit.F.GetData() bob.send <- bob.circuit.F.GetData()
// 2. Encode Bob's input // 2. Encode Bob's input
Y := garbled.Encode(bob.Circuit.E[1], util.Int2Bools(y, 3)) Y := garbled.Encode(bob.circuit.E[1], util.Int2Bools(y, 3))
fmt.Println("Bob: Sending Y to Alice") fmt.Println("Bob: Sending Y to Alice")
bob.send <- Y bob.send <- Y
...@@ -112,7 +113,7 @@ func (bob *Party) RunBob(y int) (err error) { ...@@ -112,7 +113,7 @@ func (bob *Party) RunBob(y int) (err error) {
// Create data for each possible encoding // Create data for each possible encoding
data := make([][]byte, len(blood.Table)) data := make([][]byte, len(blood.Table))
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
data[i] = garbled.Encode(bob.Circuit.E[0], util.Int2Bools(i, 3)) data[i] = garbled.Encode(bob.circuit.E[0], util.Int2Bools(i, 3))
} }
fmt.Println("Bob: Oblivious transfer encoding to Alice") fmt.Println("Bob: Oblivious transfer encoding to Alice")
err = bob.obliv.Send(data) err = bob.obliv.Send(data)
...@@ -159,8 +160,10 @@ func (bob *Party) RunBob(y int) (err error) { ...@@ -159,8 +160,10 @@ func (bob *Party) RunBob(y int) (err error) {
// type y. // type y.
// outputMode: 0: Alice learns, 1: Bob learns else both learns // outputMode: 0: Alice learns, 1: Bob learns else both learns
func RunProtocol(x, y int, outputMode int) (z bool, err error) { func RunProtocol(x, y int, outputMode int) (z bool, err error) {
z = false p, err := NewProtocol()
p := NewProtocol() if err != nil {
return
}
// Concurrently run Bob // Concurrently run Bob
go func() { go func() {
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
cryptoUtil "crycomp/internal/crypto/util" cryptoUtil "crycomp/internal/crypto/util"
"crycomp/internal/util" "crycomp/internal/util"
"crypto/sha256" "crypto/sha256"
"encoding/hex"
"fmt"
) )
type Circuit struct { type Circuit struct {
...@@ -27,6 +29,7 @@ func NewCircuit(numInputs, numWires, k int) (c *Circuit, err error) { ...@@ -27,6 +29,7 @@ func NewCircuit(numInputs, numWires, k int) (c *Circuit, err error) {
numWires: numWires, numWires: numWires,
k: k, k: k,
G: G,
F: F, F: F,
} }
return return
...@@ -53,11 +56,11 @@ func (c *Circuit) GarbleBloodCircuit() (err error) { ...@@ -53,11 +56,11 @@ func (c *Circuit) GarbleBloodCircuit() (err error) {
// Create e // Create e
c.E = make([]*Table, 2) c.E = make([]*Table, 2)
c.E[0], _ = NewTable(c.numInputs, 2, c.k) c.E[0], _ = NewTable(c.numInputs/2, 2, c.k)
c.E[0].SetData(kTable.getRows(0, c.numInputs)) c.E[0].SetData(kTable.getRows(0, c.numInputs/2))
c.E[1], _ = NewTable(c.numInputs, 2, c.k) c.E[1], _ = NewTable(c.numInputs/2, 2, c.k)
c.E[1].SetData(kTable.getRows(c.numInputs, c.numInputs)) c.E[1].SetData(kTable.getRows(c.numInputs/2, c.numInputs/2))
// Create d // Create d
c.D, _ = NewTable(1, 2, c.k) c.D, _ = NewTable(1, 2, c.k)
...@@ -96,10 +99,13 @@ func (c *Circuit) garbleGate(K *Table, Li, Ri, out int, gateFun func(a, b int) i ...@@ -96,10 +99,13 @@ func (c *Circuit) garbleGate(K *Table, Li, Ri, out int, gateFun func(a, b int) i
copy(right[:K.valueLen], K.getValue(out, gateFun(a, b))) copy(right[:K.valueLen], K.getValue(out, gateFun(a, b)))
dst := util.XOR(left, right) // XOR with left as destination.
fmt.Printf("%s %s %s\n", hex.EncodeToString(left), hex.EncodeToString(right), hex.EncodeToString(dst))
// The index to write this value to. This depends in the row permutation. // The index to write this value to. This depends in the row permutation.
rowI := perm[a*2+b] * K.valueLen rowI := perm[a*2+b] * K.valueLen
util.XOR(left, left, right) // XOR with left as destination.
copy(cRow[rowI:rowI+K.valueLen], left) copy(cRow[rowI:rowI+K.valueLen], dst)
} }
} }
return cRow return cRow
...@@ -113,17 +119,33 @@ func G(A, B []byte, i int) []byte { ...@@ -113,17 +119,33 @@ func G(A, B []byte, i int) []byte {
return hash.Sum(nil) return hash.Sum(nil)
} }
func Evaluate(F *Table, x []byte) ([]byte, error) { func (c *Circuit) Evaluate(x []byte) ([]byte, error) {
K, err := NewTable(1, len(x)/F.valueLen, F.valueLen*8) K, err := NewTable(1, c.numWires, c.k)
if err != nil { if err != nil {
return nil, err return nil, err
} }
K.SetData(x) copy(K.data[:len(x)], x)
Li := []int{0, 1, 2, 6, 8}
Ri := []int{3, 4, 5, 7, 9}
Oi := []int{6, 7, 8, 9, 10}
// zeroes := make([]byte, K.valueLen)
fmt.Println(c.F.valueLen)
// For each circuit gate // For each circuit gate
for i := 0; i < len(F.data); i++ { for i := 0; i < c.F.rows; i++ {
// For each C // For each C
for j := 0; j < 4; j++ { for j := 0; j < 4; j++ {
left := c.G(K.getValue(0, Li[i]), K.getValue(0, Ri[i]), Oi[i])
right := c.F.getValue(i, j)
dst := util.XOR(left, right)
fmt.Printf("%s %s %s\n", hex.EncodeToString(left), hex.EncodeToString(right), hex.EncodeToString(dst))
// fmt.Printf(len(left))
// dst := make([]byte, 2*F.valueLen) // dst := make([]byte, 2*F.valueLen)
// util.XOR(dst, K.getValue()) // util.XOR(dst, K.getValue())
} }
......
...@@ -40,9 +40,11 @@ func (t *Table) index(r, c int) int { ...@@ -40,9 +40,11 @@ func (t *Table) index(r, c int) int {
} }
// getValue returns a byte slice containing the value at row i and column j. // getValue returns a byte slice containing the value at row i and column j.
func (t *Table) getValue(r, c int) []byte { func (t *Table) getValue(r, c int) (data []byte) {
index := t.index(r, c) index := t.index(r, c)
return t.data[index : index+t.valueLen] data = make([]byte, t.valueLen)
copy(data, t.data[index:index+t.valueLen])
return
} }
func (t *Table) setRow(r int, data []byte) { func (t *Table) setRow(r int, data []byte) {
...@@ -53,9 +55,11 @@ func (t *Table) setRow(r int, data []byte) { ...@@ -53,9 +55,11 @@ func (t *Table) setRow(r int, data []byte) {
copy(t.data[index:index+t.cols*t.valueLen], data) copy(t.data[index:index+t.cols*t.valueLen], data)
} }
func (t *Table) getRow(r int) []byte { func (t *Table) getRow(r int) (data []byte) {
index := t.index(r, 0) index := t.index(r, 0)
return t.data[index : index+t.cols*t.valueLen] data = make([]byte, t.cols*t.valueLen)
copy(data, t.data[index:index+t.cols*t.valueLen])
return
} }
func (t *Table) getRows(r, count int) []byte { func (t *Table) getRows(r, count int) []byte {
...@@ -70,8 +74,10 @@ func (t *Table) SetData(data []byte) { ...@@ -70,8 +74,10 @@ func (t *Table) SetData(data []byte) {
t.data = data t.data = data
} }
func (t *Table) GetData() []byte { func (t *Table) GetData() (data []byte) {
return t.data data = make([]byte, len(t.data))
copy(data, t.data)
return
} }
func (t *Table) randomizeTable() error { func (t *Table) randomizeTable() error {
......
...@@ -24,11 +24,13 @@ func Int2Bools(x, n int) []bool { ...@@ -24,11 +24,13 @@ func Int2Bools(x, n int) []bool {
} }
// XOR computes the byte-wise XOR of two byte slices. Slices must hve equal length. // XOR computes the byte-wise XOR of two byte slices. Slices must hve equal length.
func XOR(dst, a, b []byte) { func XOR(a, b []byte) (dst []byte) {
if len(a) != len(b) { if len(a) != len(b) {
panic("length must be equal") panic("length must be equal")
} }
dst = make([]byte, len(a))
for i := range a { for i := range a {
dst[i] = a[i] ^ b[i] dst[i] = a[i] ^ b[i]
} }
return
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment