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
func (alice *Party) RunAlice(x int) (err error) {
// 1. Garble
F, _ := garbled.NewTable(NumWires-NumInputs*2, 4, K*2)
F.SetData(<-alice.receive)
// F, _ := garbled.NewTable(NumWires-NumInputs*2, 4, K*2)
alice.circuit.F.SetData(<-alice.receive)
fmt.Println("Alice: Received F from Bob")
// 2. Encode Bob's input
fmt.Println("Alice: Received Y from Bob")
Y := <-alice.receive
fmt.Println("Alice: Received Y from Bob")
// 3. Encode Alice's input
X, err := alice.obliv.Receive(x)
fmt.Println("Alice: Oblivious transfer encoding from Bob")
if err != nil {
return
}
// 4. Evaluation
evalInput := append(X, Y...)
fmt.Println(len(evalInput))
_, err = garbled.Evaluate(F, evalInput)
// fmt.Println(len(evalInput))
_, err = alice.circuit.Evaluate(evalInput)
if err != nil {
return
}
......@@ -96,15 +97,15 @@ func (alice *Party) RunAlice(x int) (err error) {
func (bob *Party) RunBob(y int) (err error) {
// 1. Garble
bob.Circuit, err = garbled.NewGarbBloodCircuit(NumInputs, NumWires, K)
err = bob.circuit.GarbleBloodCircuit()
if err != nil {
return
}
fmt.Println("Bob: Sending F to Alice")
bob.send <- bob.Circuit.F.GetData()
bob.send <- bob.circuit.F.GetData()
// 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")
bob.send <- Y
......@@ -112,7 +113,7 @@ func (bob *Party) RunBob(y int) (err error) {
// Create data for each possible encoding
data := make([][]byte, len(blood.Table))
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")
err = bob.obliv.Send(data)
......@@ -159,8 +160,10 @@ func (bob *Party) RunBob(y int) (err error) {
// type y.
// outputMode: 0: Alice learns, 1: Bob learns else both learns
func RunProtocol(x, y int, outputMode int) (z bool, err error) {
z = false
p := NewProtocol()
p, err := NewProtocol()
if err != nil {
return
}
// Concurrently run Bob
go func() {
......
......@@ -5,6 +5,8 @@ import (
cryptoUtil "crycomp/internal/crypto/util"
"crycomp/internal/util"
"crypto/sha256"
"encoding/hex"
"fmt"
)
type Circuit struct {
......@@ -27,6 +29,7 @@ func NewCircuit(numInputs, numWires, k int) (c *Circuit, err error) {
numWires: numWires,
k: k,
G: G,
F: F,
}
return
......@@ -53,11 +56,11 @@ func (c *Circuit) GarbleBloodCircuit() (err error) {
// Create e
c.E = make([]*Table, 2)
c.E[0], _ = NewTable(c.numInputs, 2, c.k)
c.E[0].SetData(kTable.getRows(0, c.numInputs))
c.E[0], _ = NewTable(c.numInputs/2, 2, c.k)
c.E[0].SetData(kTable.getRows(0, c.numInputs/2))
c.E[1], _ = NewTable(c.numInputs, 2, c.k)
c.E[1].SetData(kTable.getRows(c.numInputs, c.numInputs))
c.E[1], _ = NewTable(c.numInputs/2, 2, c.k)
c.E[1].SetData(kTable.getRows(c.numInputs/2, c.numInputs/2))
// Create d
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
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.
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
......@@ -113,17 +119,33 @@ func G(A, B []byte, i int) []byte {
return hash.Sum(nil)
}
func Evaluate(F *Table, x []byte) ([]byte, error) {
K, err := NewTable(1, len(x)/F.valueLen, F.valueLen*8)
func (c *Circuit) Evaluate(x []byte) ([]byte, error) {
K, err := NewTable(1, c.numWires, c.k)
if err != nil {
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 i := 0; i < len(F.data); i++ {
for i := 0; i < c.F.rows; i++ {
// For each C
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)
// util.XOR(dst, K.getValue())
}
......
......@@ -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.
func (t *Table) getValue(r, c int) []byte {
func (t *Table) getValue(r, c int) (data []byte) {
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) {
......@@ -53,9 +55,11 @@ func (t *Table) setRow(r int, data []byte) {
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)
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 {
......@@ -70,8 +74,10 @@ func (t *Table) SetData(data []byte) {
t.data = data
}
func (t *Table) GetData() []byte {
return t.data
func (t *Table) GetData() (data []byte) {
data = make([]byte, len(t.data))
copy(data, t.data)
return
}
func (t *Table) randomizeTable() error {
......
......@@ -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.
func XOR(dst, a, b []byte) {
func XOR(a, b []byte) (dst []byte) {
if len(a) != len(b) {
panic("length must be equal")
}
dst = make([]byte, len(a))
for i := range a {
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