-
Notifications
You must be signed in to change notification settings - Fork 1
/
point.go
238 lines (184 loc) · 5.49 KB
/
point.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
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: BSD-3-Clause
// Package secp256k1 implements the secp256k1 elliptic curve as specified
// in SEC 2, Version 2.0, Section 2.4.1.
package secp256k1
import (
"fmt"
"gitlab.com/yawning/secp256k1-voi/internal/disalloweq"
"gitlab.com/yawning/secp256k1-voi/internal/field"
)
var (
// feGX is the x-coordinate of the generator.
feGX = field.NewElementFromCanonicalHex("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
// feGY is the y-coordinate of the generator.
feGY = field.NewElementFromCanonicalHex("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
)
// Point represets a point on the secp256k1 curve. All arguments and
// receivers are allowed to alias. The zero value is NOT valid, and
// may only be used as a receiver.
//
// Properly initialized Points will always either be on the curve, or
// the point at infinity, and all of the curve arithmetic routines
// handle the point at infinity correctly.
type Point struct {
_ disalloweq.DisallowEqual
// The point internally is represented in projective coordinates
// (X, Y, Z) where x = X/Z y = Y/Z.
x, y, z field.Element
isValid bool
}
// Identity sets `v = id`, and returns `v`.
func (v *Point) Identity() *Point {
v.x.Zero()
v.y.One()
v.z.Zero()
v.isValid = true
return v
}
// Generator sets `v = G`, and returns `v`.
func (v *Point) Generator() *Point {
v.x.Set(feGX)
v.y.Set(feGY)
v.z.One()
v.isValid = true
return v
}
// Add sets `v = p + q`, and returns `v`.
func (v *Point) Add(p, q *Point) *Point {
assertPointsValid(p, q)
v.addComplete(p, q)
v.isValid = p.isValid && q.isValid
return v
}
// Double sets `v = p + p`, and returns `v`. Calling `Add(p, p)` will
// also return correct results, however this method is faster.
func (v *Point) Double(p *Point) *Point {
assertPointsValid(p)
v.doubleComplete(p)
v.isValid = p.isValid
return v
}
// Subtract sets `v = p - q`, and returns `v`.
func (v *Point) Subtract(p, q *Point) *Point {
assertPointsValid(p, q)
return v.Add(p, newRcvr().Negate(q))
}
// Negate sets `v = -p`, and returns `v`.
func (v *Point) Negate(p *Point) *Point {
assertPointsValid(p)
// Affine negation formulas: -(x1,y1)=(x1,-y1).
v.x.Set(&p.x)
v.y.Negate(&p.y)
v.z.Set(&p.z)
v.isValid = p.isValid
return v
}
// ConditionalNegate sets `v = p` iff `ctrl == 0`, `v = -p` otherwise,
// and returns `v`.
func (v *Point) ConditionalNegate(p *Point, ctrl uint64) *Point {
assertPointsValid(p)
v.x.Set(&p.x)
v.y.ConditionalNegate(&p.y, ctrl)
v.z.Set(&p.z)
v.isValid = p.isValid
return v
}
// ConditionalSelect sets `v = a` iff `ctrl == 0`, `v = b` otherwise,
// and returns `v`.
func (v *Point) ConditionalSelect(a, b *Point, ctrl uint64) *Point {
assertPointsValid(a, b)
v.uncheckedConditionalSelect(a, b, ctrl)
v.isValid = a.isValid && b.isValid
return v
}
func (v *Point) uncheckedConditionalSelect(a, b *Point, ctrl uint64) *Point {
v.x.ConditionalSelect(&a.x, &b.x, ctrl)
v.y.ConditionalSelect(&a.y, &b.y, ctrl)
v.z.ConditionalSelect(&a.z, &b.z, ctrl)
return v
}
// Equal returns 1 iff `v == p`, 0 otherwise.
func (v *Point) Equal(p *Point) uint64 {
assertPointsValid(v, p)
// Check X1Z2 == X2Z1 Y1Z2 == Y2Z1
x1z2 := field.NewElement().Multiply(&v.x, &p.z)
x2z1 := field.NewElement().Multiply(&p.x, &v.z)
y1z2 := field.NewElement().Multiply(&v.y, &p.z)
y2z1 := field.NewElement().Multiply(&p.y, &v.z)
return x1z2.Equal(x2z1) & y1z2.Equal(y2z1)
}
// IsIdentity returns 1 iff `v` is the identity point, 0 otherwise.
func (v *Point) IsIdentity() uint64 {
assertPointsValid(v)
return v.z.IsZero()
}
// IsYOdd returns 1 iff `v.y` is odd, 0 otherwise.
func (v *Point) IsYOdd() uint64 {
assertPointsValid(v)
scaled := newRcvr().rescale(v) // XXX/perf: Don't need to rescale X.
return scaled.y.IsOdd()
}
// Set sets `v = p`, and returns `v`.
func (v *Point) Set(p *Point) *Point {
assertPointsValid(p)
v.x.Set(&p.x)
v.y.Set(&p.y)
v.z.Set(&p.z)
v.isValid = p.isValid
return v
}
// NewGeneratorPoint returns a new Point set to the canonical generator.
func NewGeneratorPoint() *Point {
return newRcvr().Generator()
}
// NewIdentityPoint returns a new Point set to the identity element (point at infinity).
func NewIdentityPoint() *Point {
// Note: This doesn't use p.Identity(), because x and z are guaranteed
// to already be 0, and it makes escape analysis upset.
p := newRcvr()
// p.x.Zero()
p.y.One()
// p.z.Zero()
p.isValid = true
return p
}
// NewPointFrom returns a new Point set to an existing Point.
func NewPointFrom(p *Point) *Point {
assertPointsValid(p)
return newRcvr().Set(p)
}
// NewPointFromCoords creates a new Point from the big-endian encoded x
// and y coordinates.
func NewPointFromCoords(xBytes, yBytes *[CoordSize]byte) (*Point, error) {
x, err := field.NewElementFromCanonicalBytes(xBytes)
if err != nil {
return nil, fmt.Errorf("secp256k1: invalid x-coordinate: %w", err)
}
y, err := field.NewElementFromCanonicalBytes(yBytes)
if err != nil {
return nil, fmt.Errorf("secp256k1: invalid y-coordinate: %w", err)
}
if xyOnCurve(x, y) != 1 {
return nil, errPointNotOnCurve
}
p := newRcvr()
p.x.Set(x)
p.y.Set(y)
p.z.One()
p.isValid = true
return p, nil
}
// assertPointsValid ensures that the points have been initialized.
func assertPointsValid(points ...*Point) {
for _, p := range points {
if !p.isValid {
panic("secp256k1: use of uninitialized Point")
}
}
}
func newRcvr() *Point {
// This is explcitly for nicely creating receivers.
return &Point{}
}