table.go 2.34 KB
Newer Older
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package garbled

import (
	"crypto/rand"
	"errors"
)

// Table contains the random or encoded strings of bit-length k.
type Table struct {
	// data contains the raw byte data this table holds.
	data []byte
	// rows is the number of rows in data (not length).
	// Normally this is the numbe of wires the table was created for.
	rows int
	// cols is the number of strings in each row. E.g the two input
	// keys (K_0^i, K_1^i)
	cols int
	// valueLen contains the string length (of each value). This is always k/8.
	valueLen int
}

// NewTable returns a new garbled table with n rows and each row containing numValues columns.
// Each value contains k/8 bytes. k must be a multiple of 8.
func NewTable(n, numValues, k int) (t *Table, err error) {
	if k%8 != 0 {
		return nil, errors.New("k must be multiple of 8")
	}
	t = &Table{
		data:     make([]byte, n*numValues*k/8),
		rows:     n,
		cols:     numValues,
		valueLen: k / 8,
	}
	return
}

// index returns the data index for row i and column j.
func (t *Table) index(r, c int) int {
	return r*t.cols*t.valueLen + c*t.valueLen
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
42
43
44
45
46
47
48
49
func (t *Table) setValue(r, c int, data []byte) {
	if len(data) != t.valueLen {
		panic("data have invalid length")
	}
	index := t.index(r, c)
	copy(t.data[index:index+t.valueLen], data)
}

Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
50
// getValue returns a byte slice containing the value at row i and column j.
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
51
func (t *Table) getValue(r, c int) (data []byte) {
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
52
	index := t.index(r, c)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
53
54
55
	data = make([]byte, t.valueLen)
	copy(data, t.data[index:index+t.valueLen])
	return
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
56
57
58
59
60
61
62
63
64
65
}

func (t *Table) setRow(r int, data []byte) {
	if len(data) != t.cols*t.valueLen {
		panic("data have invalid length")
	}
	index := t.index(r, 0)
	copy(t.data[index:index+t.cols*t.valueLen], data)
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
66
func (t *Table) getRow(r int) (data []byte) {
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
67
	index := t.index(r, 0)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
68
69
70
	data = make([]byte, t.cols*t.valueLen)
	copy(data, t.data[index:index+t.cols*t.valueLen])
	return
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
71
72
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
73
func (t *Table) getRows(r, count int) (data []byte) {
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
74
	index := t.index(r, 0)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
75
76
77
	data = make([]byte, count*t.cols*t.valueLen)
	copy(data, t.data[index:index+count*t.cols*t.valueLen])
	return
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
78
79
80
81
82
83
84
85
86
}

func (t *Table) SetData(data []byte) {
	if len(data) != len(t.data) {
		panic("data have invalid length")
	}
	t.data = data
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
87
88
89
90
func (t *Table) GetData() (data []byte) {
	data = make([]byte, len(t.data))
	copy(data, t.data)
	return
Anders Jensen Løvig's avatar
Stuff  
Anders Jensen Løvig committed
91
92
93
94
95
96
}

func (t *Table) randomizeTable() error {
	_, err := rand.Read(t.data)
	return err
}