-
Notifications
You must be signed in to change notification settings - Fork 120
/
secp256k1.go
274 lines (220 loc) · 6.18 KB
/
secp256k1.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
package curve
import (
"encoding/hex"
"errors"
"fmt"
"github.com/cronokirby/saferith"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
var secp256k1BaseX, secp256k1BaseY secp256k1.FieldVal
func init() {
Gx, _ := hex.DecodeString("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
Gy, _ := hex.DecodeString("483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
secp256k1BaseX.SetByteSlice(Gx)
secp256k1BaseY.SetByteSlice(Gy)
}
type Secp256k1 struct{}
func (Secp256k1) NewPoint() Point {
return new(Secp256k1Point)
}
func (Secp256k1) NewBasePoint() Point {
out := new(Secp256k1Point)
out.value.X.Set(&secp256k1BaseX)
out.value.Y.Set(&secp256k1BaseY)
out.value.Z.SetInt(1)
return out
}
func (Secp256k1) NewScalar() Scalar {
return new(Secp256k1Scalar)
}
func (Secp256k1) ScalarBits() int {
return 256
}
func (Secp256k1) SafeScalarBytes() int {
return 32
}
var secp256k1OrderNat, _ = new(saferith.Nat).SetHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")
var secp256k1Order = saferith.ModulusFromNat(secp256k1OrderNat)
func (Secp256k1) Order() *saferith.Modulus {
return secp256k1Order
}
func (Secp256k1) LiftX(data []byte) (*Secp256k1Point, error) {
out := new(Secp256k1Point)
out.value.Z.SetInt(1)
if out.value.X.SetByteSlice(data) {
return nil, fmt.Errorf("secp256k1Point.UnmarshalBinary: x coordinate out of range")
}
if !secp256k1.DecompressY(&out.value.X, false, &out.value.Y) {
return nil, fmt.Errorf("secp256k1Point.UnmarshalBinary: x coordinate not on curve")
}
return out, nil
}
func (Secp256k1) Name() string {
return "secp256k1"
}
type Secp256k1Scalar struct {
value secp256k1.ModNScalar
}
func secp256k1CastScalar(generic Scalar) *Secp256k1Scalar {
out, ok := generic.(*Secp256k1Scalar)
if !ok {
panic(fmt.Sprintf("failed to convert to secp256k1Scalar: %v", generic))
}
return out
}
func (*Secp256k1Scalar) Curve() Curve {
return Secp256k1{}
}
func (s *Secp256k1Scalar) MarshalBinary() ([]byte, error) {
data := s.value.Bytes()
return data[:], nil
}
func (s *Secp256k1Scalar) UnmarshalBinary(data []byte) error {
if len(data) != 32 {
return fmt.Errorf("invalid length for secp256k1 scalar: %d", len(data))
}
var exactData [32]byte
copy(exactData[:], data)
if s.value.SetBytes(&exactData) != 0 {
return errors.New("invalid bytes for secp256k1 scalar")
}
return nil
}
func (s *Secp256k1Scalar) Add(that Scalar) Scalar {
other := secp256k1CastScalar(that)
s.value.Add(&other.value)
return s
}
func (s *Secp256k1Scalar) Sub(that Scalar) Scalar {
other := secp256k1CastScalar(that)
negated := new(Secp256k1Scalar)
negated.value.Set(&other.value)
negated.value.Negate()
s.value.Add(&negated.value)
return s
}
func (s *Secp256k1Scalar) Mul(that Scalar) Scalar {
other := secp256k1CastScalar(that)
s.value.Mul(&other.value)
return s
}
func (s *Secp256k1Scalar) Invert() Scalar {
s.value.InverseNonConst()
return s
}
func (s *Secp256k1Scalar) Negate() Scalar {
s.value.Negate()
return s
}
func (s *Secp256k1Scalar) IsOverHalfOrder() bool {
return s.value.IsOverHalfOrder()
}
func (s *Secp256k1Scalar) Equal(that Scalar) bool {
other := secp256k1CastScalar(that)
return s.value.Equals(&other.value)
}
func (s *Secp256k1Scalar) IsZero() bool {
return s.value.IsZero()
}
func (s *Secp256k1Scalar) Set(that Scalar) Scalar {
other := secp256k1CastScalar(that)
s.value.Set(&other.value)
return s
}
func (s *Secp256k1Scalar) SetNat(x *saferith.Nat) Scalar {
reduced := new(saferith.Nat).Mod(x, secp256k1Order)
s.value.SetByteSlice(reduced.Bytes())
return s
}
func (s *Secp256k1Scalar) Act(that Point) Point {
other := secp256k1CastPoint(that)
out := new(Secp256k1Point)
secp256k1.ScalarMultNonConst(&s.value, &other.value, &out.value)
return out
}
func (s *Secp256k1Scalar) ActOnBase() Point {
out := new(Secp256k1Point)
secp256k1.ScalarBaseMultNonConst(&s.value, &out.value)
return out
}
type Secp256k1Point struct {
value secp256k1.JacobianPoint
}
func secp256k1CastPoint(generic Point) *Secp256k1Point {
out, ok := generic.(*Secp256k1Point)
if !ok {
panic(fmt.Sprintf("failed to convert to secp256k1Point: %v", generic))
}
return out
}
func (*Secp256k1Point) Curve() Curve {
return Secp256k1{}
}
func (p *Secp256k1Point) XBytes() []byte {
p.value.ToAffine()
return p.value.X.Bytes()[:]
}
func (p *Secp256k1Point) MarshalBinary() ([]byte, error) {
out := make([]byte, 33)
// we clone v to not case a race during a hash.Write
v := p.value
v.ToAffine()
// Doing it this way is compatible with Bitcoin
out[0] = byte(v.Y.IsOddBit()) + 2
data := v.X.Bytes()
copy(out[1:], data[:])
return out, nil
}
func (p *Secp256k1Point) UnmarshalBinary(data []byte) error {
if len(data) != 33 {
return fmt.Errorf("invalid length for secp256k1Point: %d", len(data))
}
p.value.Z.SetInt(1)
if p.value.X.SetByteSlice(data[1:]) {
return fmt.Errorf("secp256k1Point.UnmarshalBinary: x coordinate out of range")
}
if !secp256k1.DecompressY(&p.value.X, data[0] == 3, &p.value.Y) {
return fmt.Errorf("secp256k1Point.UnmarshalBinary: x coordinate not on curve")
}
return nil
}
func (p *Secp256k1Point) Add(that Point) Point {
other := secp256k1CastPoint(that)
out := new(Secp256k1Point)
secp256k1.AddNonConst(&p.value, &other.value, &out.value)
return out
}
func (p *Secp256k1Point) Sub(that Point) Point {
return p.Add(that.Negate())
}
func (p *Secp256k1Point) Set(that Point) Point {
other := secp256k1CastPoint(that)
p.value.Set(&other.value)
return p
}
func (p *Secp256k1Point) Negate() Point {
out := new(Secp256k1Point)
out.value.Set(&p.value)
out.value.Y.Negate(1)
out.value.Y.Normalize()
return out
}
func (p *Secp256k1Point) Equal(that Point) bool {
other := secp256k1CastPoint(that)
p.value.ToAffine()
other.value.ToAffine()
return p.value.X.Equals(&other.value.X) && p.value.Y.Equals(&other.value.Y) && p.value.Z.Equals(&other.value.Z)
}
func (p *Secp256k1Point) IsIdentity() bool {
return p == nil || (p.value.X.IsZero() && p.value.Y.IsZero()) || p.value.Z.IsZero()
}
func (p *Secp256k1Point) HasEvenY() bool {
p.value.ToAffine()
return !p.value.Y.IsOdd()
}
func (p *Secp256k1Point) XScalar() Scalar {
out := new(Secp256k1Scalar)
p.value.ToAffine()
out.value.SetBytes(p.value.X.Bytes())
return out
}