-
Notifications
You must be signed in to change notification settings - Fork 8
/
table.go
217 lines (192 loc) · 4.73 KB
/
table.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
package edwards25519
type NielsPoint struct {
YPlusX, YMinusX, XY2D FieldElement
}
// Precomputed scalar multiplication table
type ScalarMultTable [32][8]NielsPoint
// Set p to zero, the neutral element. Return p.
func (p *NielsPoint) SetZero() *NielsPoint {
p.YMinusX.SetOne()
p.YPlusX.SetOne()
p.XY2D.SetZero()
return p
}
// Set p to q. Returns p.
func (p *NielsPoint) Set(q *NielsPoint) *NielsPoint {
p.YPlusX.Set(&q.YPlusX)
p.YMinusX.Set(&q.YMinusX)
p.XY2D.Set(&q.XY2D)
return p
}
// Set p to q if b == 1. Assumes b is 0 or 1. Returns p.
func (p *NielsPoint) ConditionalSet(q *NielsPoint, b int32) *NielsPoint {
p.YPlusX.ConditionalSet(&q.YPlusX, b)
p.YMinusX.ConditionalSet(&q.YMinusX, b)
p.XY2D.ConditionalSet(&q.XY2D, b)
return p
}
// Set p to -q. Returns p.
func (p *NielsPoint) Neg(q *NielsPoint) *NielsPoint {
p.YMinusX.Set(&q.YPlusX)
p.YPlusX.Set(&q.YMinusX)
p.XY2D.Neg(&q.XY2D)
return p
}
// Sets p to q+r. Returns p.
func (p *CompletedPoint) AddExtendedNiels(q *ExtendedPoint, r *NielsPoint) *CompletedPoint {
var t0 FieldElement
p.X.add(&q.Y, &q.X)
p.Y.sub(&q.Y, &q.X)
p.Z.Mul(&p.X, &r.YPlusX)
p.Y.Mul(&p.Y, &r.YMinusX)
p.T.Mul(&r.XY2D, &q.T)
t0.add(&q.Z, &q.Z)
p.X.sub(&p.Z, &p.Y)
p.Y.add(&p.Z, &p.Y)
p.Z.add(&t0, &p.T)
p.T.sub(&t0, &p.T)
return p
}
// Set p to q-r. Returns p.
func (p *CompletedPoint) SubExtendedNiels(q *ExtendedPoint, r *NielsPoint) *CompletedPoint {
var t0 FieldElement
p.X.add(&q.Y, &q.X)
p.Y.sub(&q.Y, &q.X)
p.Z.Mul(&p.X, &r.YMinusX)
p.Y.Mul(&p.Y, &r.YPlusX)
p.T.Mul(&r.XY2D, &q.T)
t0.add(&q.Z, &q.Z)
p.X.sub(&p.Z, &p.Y)
p.Y.add(&p.Z, &p.Y)
p.Z.sub(&t0, &p.T)
p.T.add(&t0, &p.T)
return p
}
// Sets p to q. Returns p.
func (p *NielsPoint) SetExtended(q *ExtendedPoint) *NielsPoint {
var x, y, zInv FieldElement
zInv.Inverse(&q.Z)
x.Mul(&q.X, &zInv)
y.Mul(&q.Y, &zInv)
p.YPlusX.Add(&y, &x)
p.YMinusX.Sub(&y, &x)
p.XY2D.Mul(&x, &y)
p.XY2D.Add(&p.XY2D, &p.XY2D)
p.XY2D.Mul(&p.XY2D, &feD)
return p
}
// Fill the table t with data for the point p.
func (t *ScalarMultTable) Compute(p *ExtendedPoint) {
var c, cp ExtendedPoint
var c_pp ProjectivePoint
var c_cp CompletedPoint
cp.Set(p)
for i := 0; i < 32; i++ {
c.SetZero()
for v := 0; v < 8; v++ {
c.Add(&c, &cp)
t[i][v].SetExtended(&c)
}
c_cp.DoubleExtended(&c)
c_pp.SetCompleted(&c_cp)
c_cp.DoubleProjective(&c_pp)
c_pp.SetCompleted(&c_cp)
c_cp.DoubleProjective(&c_pp)
c_pp.SetCompleted(&c_cp)
c_cp.DoubleProjective(&c_pp)
c_pp.SetCompleted(&c_cp)
c_cp.DoubleProjective(&c_pp)
cp.SetCompleted(&c_cp)
}
}
// Compute 4-bit signed window for the scalar s
func computeScalarWindow4(s *[32]byte, w *[64]int8) {
for i := 0; i < 32; i++ {
w[2*i] = int8(s[i] & 15)
w[2*i+1] = int8((s[i] >> 4) & 15)
}
carry := int8(0)
for i := 0; i < 63; i++ {
w[i] += carry
carry = (w[i] + 8) >> 4
w[i] -= carry << 4
}
w[63] += carry
}
// Set p to s * q, where t was computed for q using t.Compute(q).
func (t *ScalarMultTable) ScalarMult(p *ExtendedPoint, s *[32]byte) {
var w [64]int8
computeScalarWindow4(s, &w)
p.SetZero()
var np NielsPoint
var cp CompletedPoint
var pp ProjectivePoint
for i := int32(0); i < 32; i++ {
t.selectPoint(&np, i, int32(w[2*i+1]))
cp.AddExtendedNiels(p, &np)
p.SetCompleted(&cp)
}
cp.DoubleExtended(p)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
p.SetCompleted(&cp)
for i := int32(0); i < 32; i++ {
t.selectPoint(&np, i, int32(w[2*i]))
cp.AddExtendedNiels(p, &np)
p.SetCompleted(&cp)
}
}
func (t *ScalarMultTable) VarTimeScalarMult(p *ExtendedPoint, s *[32]byte) {
var w [64]int8
computeScalarWindow4(s, &w)
p.SetZero()
var np NielsPoint
var cp CompletedPoint
var pp ProjectivePoint
for i := int32(0); i < 32; i++ {
if t.varTimeSelectPoint(&np, i, int32(w[2*i+1])) {
cp.AddExtendedNiels(p, &np)
p.SetCompleted(&cp)
}
}
cp.DoubleExtended(p)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
pp.SetCompleted(&cp)
cp.DoubleProjective(&pp)
p.SetCompleted(&cp)
for i := int32(0); i < 32; i++ {
if t.varTimeSelectPoint(&np, i, int32(w[2*i])) {
cp.AddExtendedNiels(p, &np)
p.SetCompleted(&cp)
}
}
}
func (t *ScalarMultTable) selectPoint(p *NielsPoint, pos int32, b int32) {
bNegative := negative(b)
bAbs := b - (((-bNegative) & b) << 1)
p.SetZero()
for i := int32(0); i < 8; i++ {
p.ConditionalSet(&t[pos][i], equal30(bAbs, i+1))
}
var negP NielsPoint
negP.Neg(p)
p.ConditionalSet(&negP, bNegative)
}
func (t *ScalarMultTable) varTimeSelectPoint(p *NielsPoint, pos int32, b int32) bool {
if b == 0 {
return false
}
if b < 0 {
p.Neg(&t[pos][-b-1])
} else {
p.Set(&t[pos][b-1])
}
return true
}