-
Notifications
You must be signed in to change notification settings - Fork 1
/
point_projective.go
302 lines (249 loc) · 7.24 KB
/
point_projective.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
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: BSD-3-Clause
package secp256k1
import "gitlab.com/yawning/secp256k1-voi/internal/field"
// These are the internal routines specific to working with points
// represented in projective coordinates.
//
// As an explicit simplicity/performance tradeoff, this representation
// was chosen so that it is possible to use the complete addition
// formulas.
//
// See:
// - https://eprint.iacr.org/2015/1060.pdf
// - https://hyperelliptic.org/EFD/g1p/auto-shortw-projective.html
// feB3 is the constant `b * 3`, used in the point addition algorithm.
var feB3 = field.NewElementFromUint64(7 * 3)
// addComplete sets `v = p + q`, and returns `v`.
func (v *Point) addComplete(p, q *Point) *Point {
// Algorithm 7 from "Complete addition formulas for prime
// order elliptic curves" by Renes, Costello, and Batina.
//
// The formula is complete in that it is valid for all p and q,
// without exceptions or extra assumptions about the inputs.
//
// The operation costs are `12M + 2m3b + 19a`, though our current
// field implmentation lacks a multiply tailored for small
// multiples (`m3b`).
//
// If you are looking to use this formula for something else
// note that it is specialized for `a = 0`.
var (
t0 = field.NewElement()
t1 = field.NewElement()
t2 = field.NewElement()
t3 = field.NewElement()
t4 = field.NewElement()
x1 = &p.x
y1 = &p.y
z1 = &p.z
x2 = &q.x
y2 = &q.y
z2 = &q.z
// To make this alias-safe, allocate these.
x3 = field.NewElement()
y3 = field.NewElement()
z3 = field.NewElement()
)
// t0 := X1 * X2 ; t1 := Y1 * Y2 ; t2 := Z1 * Z2 ;
t0.Multiply(x1, x2)
t1.Multiply(y1, y2)
t2.Multiply(z1, z2)
// t3 := X1 + Y1 ; t4 := X2 + Y2 ; t3 := t3 * t4 ;
t3.Add(x1, y1)
t4.Add(x2, y2)
t3.Multiply(t3, t4)
// t4 := t0 + t1 ; t3 := t3 - t4 ; t4 := Y1 + Z1 ;
t4.Add(t0, t1)
t3.Subtract(t3, t4)
t4.Add(y1, z1)
// X3 := Y2 + Z2 ; t4 := t4 * X3 ; X3 := t1 + t2 ;
x3.Add(y2, z2)
t4.Multiply(t4, x3)
x3.Add(t1, t2)
// t4 := t4 - X3 ; X3 := X1 + Z1 ; Y3 := X2 + Z2 ;
t4.Subtract(t4, x3)
x3.Add(x1, z1)
y3.Add(x2, z2)
// X3 := X3 * Y3 ; Y3 := t0 + t2 ; Y3 := X3 - Y3 ;
x3.Multiply(x3, y3)
y3.Add(t0, t2)
y3.Subtract(x3, y3)
// X3 := t0 + t0 ; t0 := X3 + t0 ; t2 := b3 * t2 ;
x3.Add(t0, t0)
t0.Add(x3, t0)
t2.Multiply(feB3, t2)
// Z3 := t1 + t2 ; t1 := t1 - t2 ; Y3 := b3 * Y3 ;
z3.Add(t1, t2)
t1.Subtract(t1, t2)
y3.Multiply(feB3, y3)
// X3 := t4 * Y3 ; t2 := t3 * t1 ; X3 := t2 - X3 ;
x3.Multiply(t4, y3)
t2.Multiply(t3, t1)
x3.Subtract(t2, x3)
// Y3 := Y3 * t0 ; t1 := t1 * Z3 ; Y3 := t1 + Y3 ;
y3.Multiply(y3, t0)
t1.Multiply(t1, z3)
y3.Add(t1, y3)
// t0 := t0 * t3 ; Z3 := Z3 * t4 ; Z3 := Z3 + t0 ;
t0.Multiply(t0, t3)
z3.Multiply(z3, t4)
z3.Add(z3, t0)
// return X3 , Y3 , Z3 ;
v.x.Set(x3)
v.y.Set(y3)
v.z.Set(z3)
return v
}
// addMixed sets `v = p + (x2, y2, 1)`, and returns `v`.
func (v *Point) addMixed(p *Point, x2, y2 *field.Element) *Point {
// Algorithm 8 from "Complete addition formulas for prime
// order elliptic curves" by Renes, Costello, and Batina.
//
// The formula is mixed in that it assumes the z-coordinate
// of the addend (`Z2`) is `1`, meaning that it CAN NOT
// handle the addend being the point at infinity.
//
// The operation costs are `11M + 2m3b + 13a`, though our current
// field implmentation lacks a multiply tailored for small
// multiples (`m3b`). This saves `1M + 6a` over `addComplete`.
//
// If you are looking to use this formula for something else
// note that it is specialized for `a = 0`.
var (
t0 = field.NewElement()
t1 = field.NewElement()
t2 = field.NewElement()
t3 = field.NewElement()
t4 = field.NewElement()
x1 = &p.x
y1 = &p.y
z1 = &p.z
// To make this alias-safe, allocate these.
x3 = field.NewElement()
y3 = field.NewElement()
z3 = field.NewElement()
)
// t0 := X1 * X2 ; t1 := Y1 * Y2 ; t3 := X2 + Y2 ;
t0.Multiply(x1, x2)
t1.Multiply(y1, y2)
t3.Add(x2, y2)
// t4 := X1 + Y1 ; t3 := t3 * t4 ; t4 := t0 + t1 ;
t4.Add(x1, y1)
t3.Multiply(t3, t4)
t4.Add(t0, t1)
// t3 := t3 - t4 ; t4 := Y2 * Z1 ; t4 := t4 + Y1 ;
t3.Subtract(t3, t4)
t4.Multiply(y2, z1)
t4.Add(t4, y1)
// Y3 := X2 * Z1 ; Y3 := Y3 + X1 ; X3 := t0 + t0 ;
y3.Multiply(x2, z1)
y3.Add(y3, x1)
x3.Add(t0, t0)
// t0 := X3 + t0 ; t2 := b3 * Z1 ; Z3 := t1 + t2 ;
t0.Add(x3, t0)
t2.Multiply(feB3, z1)
z3.Add(t1, t2)
// t1 := t1 - t2 ; Y3 := b3 * Y3 ; X3 := t4 * Y3 ;
t1.Subtract(t1, t2)
y3.Multiply(feB3, y3)
x3.Multiply(t4, y3)
// t2 := t3 * t1 ; X3 := t2 - X3 ; Y3 := Y3 * t0 ;
t2.Multiply(t3, t1)
x3.Subtract(t2, x3)
y3.Multiply(y3, t0)
// t1 := t1 * Z3 ; Y3 := t1 + Y3 ; t0 := t0 * t3 ;
t1.Multiply(t1, z3)
y3.Add(t1, y3)
t0.Multiply(t0, t3)
// Z3 := Z3 * t4 ; Z3 := Z3 + t0
z3.Multiply(z3, t4)
z3.Add(z3, t0)
// return X3 , Y3 , Z3 ;
v.x.Set(x3)
v.y.Set(y3)
v.z.Set(z3)
return v
}
// doubleComplete sets `v = p + p`, and returns `v`.
func (v *Point) doubleComplete(p *Point) *Point { //nolint:unparam
// Algorithm 9 from "Complete addition formulas for prime
// order elliptic curves" by Renes, Costello, and Batina.
//
// The formula is complete in that it is valid for all p,
// without exceptions or extra assumptions about the inputs.
//
// The operation costs are `6M + 2S + 1m3b + 9a`, though our
// current field implmentation lacks a multiply tailored for
// small multiples (`m3b`).
//
// If you are looking to use this formula for something else
// note that it is specialized for `a = 0`.
var (
t0 = field.NewElement()
t1 = field.NewElement()
t2 = field.NewElement()
x = &p.x
y = &p.y
z = &p.z
// To make this alias-safe, allocate these.
x3 = field.NewElement()
y3 = field.NewElement()
z3 = field.NewElement()
)
// t0 := Y ^2; Z3 := t0 + t0 ; Z3 := Z3 + Z3 ;
t0.Square(y)
z3.Add(t0, t0)
z3.Add(z3, z3)
// Z3 := Z3 + Z3 ; t1 := Y * Z ; t2 := Z ^2;
z3.Add(z3, z3)
t1.Multiply(y, z)
t2.Square(z)
// t2 := b3 * t2 ; X3 := t2 * Z3 ; Y3 := t0 + t2 ;
t2.Multiply(feB3, t2)
x3.Multiply(t2, z3)
y3.Add(t0, t2)
// Z3 := t1 * Z3 ; t1 := t2 + t2 ; t2 := t1 + t2 ;
z3.Multiply(t1, z3)
t1.Add(t2, t2)
t2.Add(t1, t2)
// t0 := t0 - t2 ; Y3 := t0 * Y3 ; Y3 := X3 + Y3 ;
t0.Subtract(t0, t2)
y3.Multiply(t0, y3)
y3.Add(x3, y3)
// t1 := X * Y ; X3 := t0 * t1 ; X3 := X3 + X3 ;
t1.Multiply(x, y)
x3.Multiply(t0, t1)
x3.Add(x3, x3)
// return X3 , Y3 , Z3 ;
v.x.Set(x3)
v.y.Set(y3)
v.z.Set(z3)
return v
}
// rescale scales the point such that Z = 1.
//
// Note: This is quite expensive, and should only done when serializing points.
func (v *Point) rescale(p *Point) *Point {
assertPointsValid(p)
// A = 1/Z1
// X3 = A*X1
// Y3 = A*Y1
// Z3 = 1
//
// As per "From A to Z: Projective coordinates leakage in the wild"
// leaking the Z-coordinate is bad. The modular inversion algorithm
// used in this library is based on Fermat's Little Theorem
// (ie: Z^-1 = Z^(p -2) mod p). Bernstein-Yang also would be safe.
//
// See: https://eprint.iacr.org/2020/432.pdf
scaled := newRcvr()
a := field.NewElement().Invert(&p.z)
scaled.x.Multiply(a, &p.x)
scaled.y.Multiply(a, &p.y)
scaled.z.One()
scaled.isValid = p.isValid
// Iff p is the point at infinity, set v to (0, 1, 0).
return v.ConditionalSelect(scaled, NewIdentityPoint(), p.IsIdentity())
}