/
galois_field.go
247 lines (199 loc) · 5.27 KB
/
galois_field.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
package galoisfield
import (
"github.com/awnumar/memguard"
)
// GaloisField448 is a field with a finite number of elements. The number depends
// on the word bits: 512/word_bits
// This should not be confunsed with the Field Element
type GaloisField448 struct {
Limb *memguard.LockedBuffer
}
// NewGaloisField448 will return a newly created, empty field element
// TODO: use no escape
func NewGaloisField448(nlimbs int) *GaloisField448 {
var gf GaloisField448
// TODO: extract to constant. It should be 320 for ed25519
// equivalent to: gf.Limb = memguard.NewBuffer(512 / WordBits)
// TODO: check this
gf.Limb = memguard.NewBuffer(nlimbs * 8)
gf.Limb.Freeze() // Make it inmmutable
return &gf
}
// NewGaloisField448FromBytes will return a newly created field element with the
// provided byte array
// TODO: use no escape
func NewGaloisField448FromBytes(src []byte) *GaloisField448 {
var gf GaloisField448
if len(src) > N32Limbs*8 || len(src) < N64Limbs*8 {
panic("Wrong Len for buffer initialization")
}
gf.Limb = memguard.NewBufferFromBytes(src)
gf.Limb.Freeze() // Make it inmmutable
return &gf
}
// limbs will return a uint32 slice for the limb used.
func (gf *GaloisField448) limbs32() []uint32 {
return gf.Limb.Uint32()
}
// limbs will return a uint64 slice for the limb used.
func (gf *GaloisField448) limbs64() []uint64 {
return gf.Limb.Uint64()
}
// Destroy securely wipes and frees the underlying memory of the gf.Limb
func (gf *GaloisField448) Destroy() {
gf.Limb.Destroy()
}
// Copy copies one galoisfield to another.
func (gf *GaloisField448) Copy() *GaloisField448 {
n := NewGaloisField448(N32Limbs)
*n = *gf
return n
}
// AddRaw32 adds one galoisfield to another. For a 32 arch
// TODO: how should be error?
func AddRaw32(x *GaloisField448, y *GaloisField448) *GaloisField448 {
if x.Limb.Size() != 128 || y.Limb.Size() != 128 {
return nil
}
gf := NewGaloisField448(N32Limbs)
gf.Limb.Melt()
defer gf.Limb.Freeze()
n := gf.limbs32()
t := x.limbs32()
z := y.limbs32()
n[0] = t[0] + z[0]
n[1] = t[1] + z[1]
n[2] = t[2] + z[2]
n[3] = t[3] + z[3]
n[4] = t[4] + z[4]
n[5] = t[5] + z[5]
n[6] = t[6] + z[6]
n[7] = t[7] + z[7]
n[8] = t[8] + z[8]
n[9] = t[9] + z[9]
n[10] = t[10] + z[10]
n[11] = t[11] + z[11]
n[12] = t[12] + z[12]
n[13] = t[13] + z[13]
n[14] = t[14] + z[14]
n[15] = t[15] + z[15]
return gf
}
// AddRaw64 adds one galoisfield to another. For a 32 arch
func AddRaw64(x *GaloisField448, y *GaloisField448) *GaloisField448 {
if x.Limb.Size() != 64 || y.Limb.Size() != 64 {
return nil
}
gf := NewGaloisField448(N64Limbs)
gf.Limb.Melt()
defer gf.Limb.Freeze()
n := gf.limbs64()
t := x.limbs64()
z := y.limbs64()
n[0] = t[0] + z[0]
n[1] = t[1] + z[1]
n[2] = t[2] + z[2]
n[3] = t[3] + z[3]
n[4] = t[4] + z[4]
n[5] = t[5] + z[5]
n[6] = t[6] + z[6]
n[7] = t[7] + z[7]
return gf
}
// SubRaw32 subtracts one galoisfield to another. For a 32 arch
// TODO: how should be error?
func SubRaw32(x *GaloisField448, y *GaloisField448) *GaloisField448 {
if x.Limb.Size() != 128 || y.Limb.Size() != 128 {
return nil
}
gf := NewGaloisField448(N32Limbs)
gf.Limb.Melt()
defer gf.Limb.Freeze()
n := gf.limbs32()
t := x.limbs32()
z := y.limbs32()
n[0] = t[0] - z[0]
n[1] = t[1] - z[1]
n[2] = t[2] - z[2]
n[3] = t[3] - z[3]
n[4] = t[4] - z[4]
n[5] = t[5] - z[5]
n[6] = t[6] - z[6]
n[7] = t[7] - z[7]
n[8] = t[8] - z[8]
n[9] = t[9] - z[9]
n[10] = t[10] - z[10]
n[11] = t[11] - z[11]
n[12] = t[12] - z[12]
n[13] = t[13] - z[13]
n[14] = t[14] - z[14]
n[15] = t[15] - z[15]
return gf
}
// SubRaw64 subtracts one galoisfield to another. For a 32 arch
// TODO: how should be error?
func SubRaw64(x *GaloisField448, y *GaloisField448) *GaloisField448 {
if x.Limb.Size() != 64 || y.Limb.Size() != 64 {
return nil
}
gf := NewGaloisField448(N64Limbs)
gf.Limb.Melt()
defer gf.Limb.Freeze()
n := gf.limbs64()
t := x.limbs64()
z := y.limbs64()
n[0] = t[0] - z[0]
n[1] = t[1] - z[1]
n[2] = t[2] - z[2]
n[3] = t[3] - z[3]
n[4] = t[4] - z[4]
n[5] = t[5] - z[5]
n[6] = t[6] - z[6]
n[7] = t[7] - z[7]
return gf
}
// Bias32 is
func Bias32(x *GaloisField448, amt int) *GaloisField448 {
if x.Limb.Size() != 128 {
return nil
}
x.Limb.Melt()
defer x.Limb.Freeze()
n := x.limbs32()
co1 := uint32(((1 << 28) - 1) * amt)
co2 := co1 - uint32(amt)
n[0] += co1
n[1] += co1
n[2] += co1
n[3] += co1
n[4] += co1
n[5] += co1
n[6] += co1
n[7] += co1
n[8] += co2
n[9] += co1
n[10] += co1
n[11] += co1
n[12] += co1
n[13] += co1
n[14] += co1
n[15] += co1
gf := x.Copy()
//x.Destroy()
return gf
}
//static INLINE_UNUSED void gf_bias (gf inout, int amount);
//static INLINE_UNUSED void gf_weak_reduce (gf inout);
//
//void gf_strong_reduce (gf inout);
//void gf_add (gf out, const gf a, const gf b);
//void gf_sub (gf out, const gf a, const gf b);
//void gf_mul (gf_s *__restrict__ out, const gf a, const gf b);
//void gf_mulw_unsigned (gf_s *__restrict__ out, const gf a, uint32_t b);
//void gf_sqr (gf_s *__restrict__ out, const gf a);
//mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0. Return true if successful */
//mask_t gf_eq (const gf x, const gf y);
//mask_t gf_lobit (const gf x);
//
//void gf_serialize (uint8_t *serial, const gf x);
//mask_t gf_deserialize (gf x, const uint8_t serial[SER_BYTES],uint8_t hi_nmask);