-
Notifications
You must be signed in to change notification settings - Fork 17
/
math.go
182 lines (157 loc) · 4.15 KB
/
math.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// (C) 1996-1998 Luigi Rizzo ([email protected])
// 2009-2010 Jack Lloyd ([email protected])
// 2011 Billy Brumley ([email protected])
// 2016-2017 Vivint, Inc. ([email protected])
//
// Portions derived from code by Phil Karn ([email protected]),
// Robert Morelos-Zaragoza ([email protected]) and Hari
// Thirumoorthy ([email protected]), Aug 1995
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package infectious
import (
"bytes"
"errors"
)
type pivotSearcher struct {
k int
ipiv []bool
}
func newPivotSearcher(k int) *pivotSearcher {
return &pivotSearcher{
k: k,
ipiv: make([]bool, k),
}
}
func (p *pivotSearcher) search(col int, matrix []byte) (int, int, error) {
if p.ipiv[col] == false && matrix[col*p.k+col] != 0 {
p.ipiv[col] = true
return col, col, nil
}
for row := 0; row < p.k; row++ {
if p.ipiv[row] {
continue
}
for i := 0; i < p.k; i++ {
if p.ipiv[i] == false && matrix[row*p.k+i] != 0 {
p.ipiv[i] = true
return row, i, nil
}
}
}
return 0, 0, errors.New("pivot not found")
}
func swap(a, b *byte) {
tmp := *a
*a = *b
*b = tmp
}
// TODO(jeff): matrix is a K*K array, row major.
func invertMatrix(matrix []byte, k int) error {
pivot_searcher := newPivotSearcher(k)
indxc := make([]int, k)
indxr := make([]int, k)
id_row := make([]byte, k)
for col := 0; col < k; col++ {
icol, irow, err := pivot_searcher.search(col, matrix)
if err != nil {
return err
}
if irow != icol {
for i := 0; i < k; i++ {
swap(&matrix[irow*k+i], &matrix[icol*k+i])
}
}
indxr[col] = irow
indxc[col] = icol
pivot_row := matrix[icol*k:][:k]
c := pivot_row[icol]
if c == 0 {
return errors.New("singular matrix")
}
if c != 1 {
c = gf_inverse[c]
pivot_row[icol] = 1
mul_c := gf_mul_table[c][:]
for i := 0; i < k; i++ {
pivot_row[i] = mul_c[pivot_row[i]]
}
}
id_row[icol] = 1
if !bytes.Equal(pivot_row, id_row) {
p := matrix
for i := 0; i < k; i++ {
if i != icol {
c = p[icol]
p[icol] = 0
addmul(p[:k], pivot_row, c)
}
p = p[k:]
}
}
id_row[icol] = 0
}
for i := 0; i < k; i++ {
if indxr[i] != indxc[i] {
for row := 0; row < k; row++ {
swap(&matrix[row*k+indxr[i]], &matrix[row*k+indxc[i]])
}
}
}
return nil
}
func createInvertedVdm(vdm []byte, k int) {
if k == 1 {
vdm[0] = 1
return
}
b := make([]byte, k)
c := make([]byte, k)
c[k-1] = 0
for i := 1; i < k; i++ {
mul_p_i := gf_mul_table[gf_exp[i]][:]
for j := k - 1 - (i - 1); j < k-1; j++ {
c[j] ^= mul_p_i[c[j+1]]
}
c[k-1] ^= gf_exp[i]
}
for row := 0; row < k; row++ {
index := 0
if row != 0 {
index = int(gf_exp[row])
}
mul_p_row := gf_mul_table[index][:]
t := byte(1)
b[k-1] = 1
for i := k - 2; i >= 0; i-- {
b[i] = c[i+1] ^ mul_p_row[b[i+1]]
t = b[i] ^ mul_p_row[t]
}
mul_t_inv := gf_mul_table[gf_inverse[t]][:]
for col := 0; col < k; col++ {
vdm[col*k+row] = mul_t_inv[b[col]]
}
}
}