-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixZp.go
381 lines (347 loc) · 8.96 KB
/
matrixZp.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package tkn
import (
"encoding/binary"
"fmt"
"io"
pairing "github.com/cloudflare/circl/ecc/bls12381"
"golang.org/x/crypto/blake2b"
)
// matrixZp represents a matrix of mod grouporder elements. They are stored in row-major order.
// The name is a gesture toward the paper.
type matrixZp struct {
rows int
cols int
entries []pairing.Scalar
}
func (m *matrixZp) marshalBinary() ([]byte, error) {
ret := make([]byte, 4+pairing.ScalarSize*m.rows*m.cols)
binary.LittleEndian.PutUint16(ret[0:], uint16(m.rows))
binary.LittleEndian.PutUint16(ret[2:], uint16(m.cols))
for i := 0; i < m.rows*m.cols; i++ {
pt, err := m.entries[i].MarshalBinary()
if err != nil {
return nil, err
}
if len(pt) != pairing.ScalarSize {
panic("matrixZp: incorrect assumption of size")
}
copy(ret[pairing.ScalarSize*i+4:], pt)
}
return ret, nil
}
func (m *matrixZp) unmarshalBinary(data []byte) error {
if len(data) < 4 {
return fmt.Errorf("matrixZp deserialization failure: input too short")
}
m.rows = int(binary.LittleEndian.Uint16(data[0:]))
m.cols = int(binary.LittleEndian.Uint16(data[2:]))
data = data[4:]
if len(data) != pairing.ScalarSize*m.rows*m.cols {
return fmt.Errorf("matrixZp deserialization failure: invalid entries length: expected %d, actual %d",
pairing.ScalarSize*m.cols*m.rows,
len(data))
}
m.entries = make([]pairing.Scalar, m.rows*m.cols)
var err error
for i := 0; i < m.rows*m.cols; i++ {
err = m.entries[i].UnmarshalBinary(data[pairing.ScalarSize*i : pairing.ScalarSize*(i+1)])
if err != nil {
return fmt.Errorf("matrixZp deserialization failure: error from bytes %v: %w",
data[pairing.ScalarSize*i:pairing.ScalarSize*(i+1)],
err)
}
}
return nil
}
// sampleDlin samples from the distribution Dk.
// See section 3.2 of the paper for details.
func sampleDlin(rand io.Reader) (*matrixZp, error) {
var ret matrixZp
ret.rows = 3
ret.cols = 2
ret.entries = make([]pairing.Scalar, 6)
err := ret.entries[0].Random(rand)
if err != nil {
return nil, err
}
ret.entries[1].SetUint64(0)
ret.entries[2].SetUint64(0)
err = ret.entries[3].Random(rand)
if err != nil {
return nil, err
}
ret.entries[4].SetOne()
ret.entries[5].SetOne()
return &ret, nil
}
func randomMatrixZp(rand io.Reader, r int, c int) (*matrixZp, error) {
ret := newMatrixZp(r, c)
for i := 0; i < r*c; i++ {
err := ret.entries[i].Random(rand)
if err != nil {
return nil, err
}
}
return ret, nil
}
// We adopt the interface that math.Big uses
// Receivers get set to the results of operations, and return themselves.
// All aliases are allowed.
// Now recall that G1, G2, GT are acted on by Zp.
// So we don't have all products, just left and right with Zp.
// Zp doesn't need this distinction.
// Errors are signalled by returning nil, which propagates.
// initialize sets up m to be r x c
func (m *matrixZp) resize(r int, c int) {
if m.rows != r || m.cols != c {
m.rows = r
m.cols = c
m.entries = make([]pairing.Scalar, m.rows*m.cols)
}
}
// clear makes m an all 0
func (m *matrixZp) clear() {
for i := 0; i < len(m.entries); i++ {
m.entries[i] = pairing.Scalar{}
}
}
func newMatrixZp(r int, c int) *matrixZp {
ret := new(matrixZp)
ret.resize(r, c)
ret.clear()
return ret
}
// eye returns the k by k identity matrix.
func eye(k int) *matrixZp {
ret := newMatrixZp(k, k)
for i := 0; i < k; i++ {
ret.entries[i*k+i].SetUint64(1)
}
return ret
}
// conformal returns true iff m and a have the same dimensions.
func (m *matrixZp) conformal(a *matrixZp) bool {
return a.rows == m.rows && a.cols == m.cols
}
// Equal returns true iff m == a.
func (m *matrixZp) Equal(a *matrixZp) bool {
if !m.conformal(a) {
return false
}
for i := 0; i < m.rows*m.cols; i++ {
if m.entries[i].IsEqual(&a.entries[i]) == 0 {
return false
}
}
return true
}
// set sets m to a.
func (m *matrixZp) set(a *matrixZp) {
m.resize(a.rows, a.cols)
for i := 0; i < m.rows*m.cols; i++ {
m.entries[i].Set(&a.entries[i])
}
}
// add sets m to a+b.
func (m *matrixZp) add(a *matrixZp, b *matrixZp) {
if !a.conformal(b) {
panic(errBadMatrixSize)
}
m.resize(a.rows, a.cols)
for i := 0; i < m.rows*m.cols; i++ {
m.entries[i].Add(&a.entries[i], &b.entries[i])
}
}
// sub sets m to a-b.
func (m *matrixZp) sub(a *matrixZp, b *matrixZp) {
if !a.conformal(b) {
panic(errBadMatrixSize)
}
m.resize(a.rows, a.cols)
for i := 0; i < m.rows*m.cols; i++ {
m.entries[i].Sub(&a.entries[i], &b.entries[i])
}
}
// mul sets m to a*b.
func (m *matrixZp) mul(a *matrixZp, b *matrixZp) {
if a.cols != b.rows {
panic(errBadMatrixSize)
}
if m == a {
c := newMatrixZp(a.rows, a.cols)
c.set(a)
a = c
}
if m == b {
c := newMatrixZp(b.rows, b.cols)
c.set(b)
b = c
}
m.resize(a.rows, b.cols)
m.clear()
t := &pairing.Scalar{}
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
for k := 0; k < a.cols; k++ {
t.Mul(&a.entries[i*a.cols+k], &b.entries[k*b.cols+j])
m.entries[i*m.cols+j].Add(&m.entries[i*m.cols+j], t)
}
}
}
}
// transpose sets m to the transpose of a.
func (m *matrixZp) transpose(a *matrixZp) {
if m == a {
c := newMatrixZp(a.rows, a.cols)
c.set(a)
a = c
}
m.resize(a.cols, a.rows)
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
m.entries[i*m.cols+j].Set(&a.entries[j*a.cols+i])
}
}
}
// swaprows swaps two rows.
func (m *matrixZp) swapRows(i int, j int) {
t := &pairing.Scalar{}
for k := 0; k < m.cols; k++ {
t.Set(&m.entries[i*m.cols+k])
m.entries[i*m.cols+k].Set(&m.entries[j*m.cols+k])
m.entries[j*m.cols+k].Set(t)
}
}
// scalerow scales a row.
func (m *matrixZp) scaleRow(alpha *pairing.Scalar, i int) {
for k := 0; k < m.cols; k++ {
m.entries[i*m.cols+k].Mul(&m.entries[i*m.cols+k], alpha)
}
}
// addscaledrow takes alpha * row i and adds it to row j.
func (m *matrixZp) addScaledRow(alpha *pairing.Scalar, i int, j int) {
tmp := &pairing.Scalar{}
for k := 0; k < m.cols; k++ {
tmp.Mul(alpha, &m.entries[i*m.cols+k])
m.entries[j*m.cols+k].Add(tmp, &m.entries[j*m.cols+k])
}
}
// inverse sets m to the inverse of a. If a is not invertible,
// the result is undefined and an error is returned.
// Aliasing safe
func (m *matrixZp) inverse(a *matrixZp) error {
if a.rows != a.cols {
panic(errBadMatrixSize)
}
// Any way we slice it we need additional storage.
y := newMatrixZp(a.rows, 2*a.cols)
for i := 0; i < a.rows; i++ {
for j := 0; j < a.cols; j++ {
y.entries[i*y.cols+j].Set(&a.entries[i*a.cols+j])
}
y.entries[i*y.cols+y.rows+i].SetUint64(1)
}
tmp := &pairing.Scalar{}
// Gaussian elimination with pivoting begins here.
for i := 0; i < y.rows; i++ {
pivoted := false
pivot:
for j := i; j < y.rows; j++ {
if y.entries[i*y.cols+j].IsZero() == 0 {
y.swapRows(i, j)
pivoted = true
break pivot
}
}
if !pivoted {
return errMatrixNonInvertible
}
tmp.Inv(&y.entries[i*y.cols+i])
y.scaleRow(tmp, i)
for j := i + 1; j < y.rows; j++ {
tmp.Set(&y.entries[j*y.cols+i])
tmp.Neg()
y.addScaledRow(tmp, i, j)
}
}
// At this point the matrix is in reduced row echelon form.
// The next step is to substitute back.
for i := y.rows - 1; i >= 0; i-- {
for j := i - 1; j >= 0; j-- {
tmp.Set(&y.entries[j*y.cols+i])
tmp.Neg()
y.addScaledRow(tmp, i, j)
}
}
m.resize(a.rows, a.cols)
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
m.entries[i*m.cols+j].Set(&y.entries[i*y.cols+m.cols+j])
}
}
return nil
}
// prf computes a prf with output in pairs of 3x2 matrices
func prf(key []byte, input []byte) (*matrixZp, *matrixZp, error) {
xof, err := blake2b.NewXOF(blake2b.OutputLengthUnknown, key)
if err != nil {
return nil, nil, err
}
if _, err = xof.Write(input); err != nil {
return nil, nil, err
}
m1 := newMatrixZp(3, 2)
m2 := newMatrixZp(3, 2)
for i := 0; i < m1.rows; i++ {
for j := 0; j < m1.cols; j++ {
local := xof.Clone()
if _, err = local.Write([]byte(fmt.Sprintf("m1 matrix entry (%d, %d)", i, j))); err != nil {
return nil, nil, err
}
err = m1.entries[i*m1.cols+j].Random(local)
if err != nil {
return nil, nil, err
}
local = xof.Clone()
if _, err = local.Write([]byte(fmt.Sprintf("m2 matrix entry (%d, %d)", i, j))); err != nil {
return nil, nil, err
}
err = m2.entries[i*m2.cols+j].Random(local)
if err != nil {
return nil, nil, err
}
}
}
return m1, m2, nil
}
// scalarmul sets m to a matrix a*B
func (m *matrixZp) scalarmul(a *pairing.Scalar, b *matrixZp) {
m.resize(b.rows, b.cols)
for i := 0; i < b.rows*b.cols; i++ {
m.entries[i].Mul(a, &b.entries[i])
}
}
// colsel sets m to a matrix with the selected columns.
func (m *matrixZp) colsel(a *matrixZp, cols []int) {
if m == a {
c := newMatrixZp(a.rows, a.cols)
c.set(a)
a = c
}
m.resize(a.rows, len(cols))
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
m.entries[i*m.cols+j].Set(&a.entries[i*a.cols+cols[j]])
}
}
}
func (m *matrixZp) String() string {
var s string
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
s += fmt.Sprintf("%v ", m.entries[i*m.cols+j].String())
}
s += "\n"
}
return s
}