Debian Codegraph @

msp/matrix.go

raw symbols SCIP
1 // Matrix operations for elements in GF(2^128).
2 package msp
3
4 type Row []FieldElem
5
6 // NewRow returns a row of length s with all zero entries.
7 func NewRow(s int) Row {
8 out := Row(make([]FieldElem, s))
9
10 for i := 0; i < s; i++ {
11 out[i] = NewFieldElem()
12 }
13
14 return out
15 }
16
17 // AddM adds two vectors.
18 func (e Row) AddM(f Row) {
19 le, lf := e.Size(), f.Size()
20 if le != lf {
21 panic("Can't add rows that are different sizes!")
22 }
23
24 for i, f_i := range f {
25 e[i].AddM(f_i)
26 }
27
28 return
29 }
30
31 // MulM multiplies the row by a scalar.
32 func (e Row) MulM(f FieldElem) {
33 for i, _ := range e {
34 e[i] = e[i].Mul(f)
35 }
36 }
37
38 func (e Row) Mul(f FieldElem) Row {
39 out := NewRow(e.Size())
40
41 for i := 0; i < e.Size(); i++ {
42 out[i] = e[i].Mul(f)
43 }
44
45 return out
46 }
47
48 // DotProduct computes the dot product of two vectors.
49 func (e Row) DotProduct(f Row) FieldElem {
50 if e.Size() != f.Size() {
51 panic("Can't get dot product of rows of different length!")
52 }
53
54 out := NewFieldElem()
55
56 for i := 0; i < e.Size(); i++ {
57 out.AddM(e[i].Mul(f[i]))
58 }
59
60 return out
61 }
62
63 func (e Row) Size() int {
64 return len(e)
65 }
66
67 type Matrix []Row
68
69 // Mul right-multiplies a matrix by a row.
70 func (e Matrix) Mul(f Row) Row {
71 out, in := e.Size()
72 if in != f.Size() {
73 panic("Can't multiply by row that is wrong size!")
74 }
75
76 res := NewRow(out)
77
78 for i := 0; i < out; i++ {
79 res[i] = e[i].DotProduct(f)
80 }
81
82 return res
83 }
84
85 // Recovery returns the row vector that takes this matrix to the target vector [1 0 0 ... 0].
86 func (e Matrix) Recovery() (Row, bool) {
87 a, b := e.Size()
88
89 // aug is the target vector.
90 aug := NewRow(a)
91 aug[0] = One.Dup()
92
93 // Duplicate e away so we don't mutate it; transpose it at the same time.
94 f := make([]Row, b)
95 for i, _ := range f {
96 f[i] = NewRow(a)
97 }
98
99 for i := 0; i < a; i++ {
100 for j := 0; j < b; j++ {
101 f[j][i] = e[i][j].Dup()
102 }
103 }
104
105 for row, _ := range f {
106 if row >= b { // The matrix is tall and thin--we've finished before exhausting all the rows.
107 break
108 }
109
110 // Find a row with a non-zero entry in the (row)th position
111 candId := -1
112 for j, f_j := range f[row:] {
113 if !f_j[row].IsZero() {
114 candId = j + row
115 break
116 }
117 }
118
119 if candId == -1 { // If we can't find one, fail and return our partial work.
120 return aug, false
121 }
122
123 // Move it to the top
124 f[row], f[candId] = f[candId], f[row]
125 aug[row], aug[candId] = aug[candId], aug[row]
126
127 // Make the pivot 1.
128 fInv := f[row][row].Invert()
129
130 f[row].MulM(fInv)
131 aug[row] = aug[row].Mul(fInv)
132
133 // Cancel out the (row)th position for every row above and below it.
134 for i, _ := range f {
135 if i != row && !f[i][row].IsZero() {
136 c := f[i][row].Dup()
137
138 f[i].AddM(f[row].Mul(c))
139 aug[i].AddM(aug[row].Mul(c))
140 }
141 }
142 }
143
144 return aug, true
145 }
146
147 func (e Matrix) Size() (int, int) {
148 return len(e), e[0].Size()
149 }