-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecdsa.go
243 lines (200 loc) · 5.39 KB
/
ecdsa.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
package ecdsa
import (
"log"
"math/big"
)
// Curve struct
// y² = x^3 + ax + b
type Curve struct {
A *big.Int
B *big.Int
P *big.Int
N *big.Int
H *big.Int
G Point // Generator Point
}
// Point struct
type Point struct {
X *big.Int
Y *big.Int
}
// IsInfinity checks if a point is the point at infinity
func (p *Point) IsInfinity() bool {
return p.X == nil && p.Y == nil
}
// ScalarMult computes the scalar multiplication of P by n defined by nP = P + P + ... + P where P + P is the point addition of P and P
func (c *Curve) ScalarMult(n *big.Int, P Point) (*Point, bool) {
/* Using Montgomery ladder algorithm */
var R0 Point
R1 := P
for i := c.N.BitLen() - 1; i >= 0; i-- {
if n.Bit(i) == 0 {
R1, _ = c.AddPoints(R0, R1)
R0, _ = c.AddPoints(R0, R0)
} else {
R0, _ = c.AddPoints(R0, R1)
R1, _ = c.AddPoints(R1, R1)
}
}
return &R0, c.IsOnCurve(R0)
}
// AddPoints computes the addition of two points on the curve
func (c *Curve) AddPoints(P Point, Q Point) (Point, bool) {
R := new(Point)
/* Checkout https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_operations */
if P.IsInfinity() && Q.IsInfinity() {
/* Point at infinity */
// 0 + 0 = 0
R.X = nil
R.Y = nil
} else if P.IsInfinity() {
/* Point at infinity */
// 0 + Q = Q
R.X = Q.X
R.Y = Q.Y
} else if Q.IsInfinity() {
/* Point at infinity */
// P + 0 = P
R.X = P.X
R.Y = P.Y
} else if P.X.Cmp(big.NewInt(0)) == 0 && c.AddMod(P.Y, Q.Y).Cmp(big.NewInt(0)) == 0 {
/* Point negation */
// Check if P.X = 0 and P.Y + Q.Y = 0 ⇒ P.Y = -Q.Y
R.X = nil
R.Y = nil
} else if P.X.Cmp(Q.X) != 0 {
/* Point addition */
// Check if P.X != P.Y
R, _ = c.PointAddition(P, Q)
} else if P.X.Cmp(Q.X) == 0 && P.Y.Cmp(Q.Y) == 0 && P.Y.Cmp(big.NewInt(0)) != 0 {
/* Point doubling */
R, _ = c.PointDoubling(P, Q)
} else {
log.Fatal("Not supported")
}
return *R, c.IsOnCurve(*R)
}
// PointAddition computes the point addition of P + Q
func (c *Curve) PointAddition(P, Q Point) (*Point, bool) {
R := new(Point)
/* Point addition */
// Check if P.X != P.Y
lambda := c.SubMod(Q.Y, P.Y) // Q.Y - P.Y
q := c.SubMod(Q.X, P.X) // Q.X - P.X
q = c.InvMod(q)
lambda = c.MultMod(lambda, q) // λ = (Q.Y - P.Y) / (Q.X - P.X)
/* Compute x */
// x = λ² - P.X - Q.X
R.X = c.MultMod(lambda, lambda)
R.X = c.SubMod(R.X, P.X)
R.X = c.SubMod(R.X, Q.X)
/* Compute y */
// y = λ(P.X - R.X) - P.Y
R.Y = c.SubMod(P.X, R.X)
R.Y = c.MultMod(lambda, R.Y)
R.Y = c.SubMod(R.Y, P.Y)
return R, c.IsOnCurve(*R)
}
// PointDoubling computes the point doubling of P + Q
func (c *Curve) PointDoubling(P, Q Point) (*Point, bool) {
/* Point Doubling */
/* The operation is the same as the Point Addition, except lambda is different */
// Check if P.X == P.Y and P.Y = Q.Y and P.Y != 0
// ⇒ P and Q are coincident
R := new(Point)
/* Compute λ */
// λ = (3*(P.X)² + a) / (2*P.Y)
// a = curve.A
lambda := c.MultMod(P.X, P.X)
lambda = c.MultMod(lambda, big.NewInt(3))
lambda = c.AddMod(lambda, c.A)
lambda = c.MultMod(lambda, c.InvMod(c.MultMod(P.Y, big.NewInt(2))))
/* Point addition from here */
/* Compute x */
// x = λ² - P.X - Q.X
R.X = c.MultMod(lambda, lambda)
R.X = c.SubMod(R.X, P.X)
R.X = c.SubMod(R.X, Q.X)
/* Compute y */
// y = λ(P.X - R.X) - P.Y
R.Y = c.SubMod(P.X, R.X)
R.Y = c.MultMod(lambda, R.Y)
R.Y = c.SubMod(R.Y, P.Y)
return R, c.IsOnCurve(*R)
}
// GetY returns y given a x
func (c *Curve) GetY(x *big.Int) (*big.Int, bool) {
// Construct y equation (y^2 = x^3 + ax + b)
y := new(big.Int) // Initialize y
y.Add(y, x) // Add x
y.Exp(y, big.NewInt(3), nil) // x^3
ax := c.A // Initialize ax = curve.A
ax.Mul(ax, x) // Multiply by x
y.Add(y, ax) // Add ax to y equation
y.Add(y, c.B) // Add b to y equation
// Initialize pMod
pMod := new(big.Int)
pMod.Set(c.P)
// Compute p mod 4
pMod.Mod(pMod, big.NewInt(4))
// Initialize residue
// Checkout https://en.wikipedia.org/wiki/Quadratic_residue#Pairs_of_residues_and_nonresidues
res := new(big.Int)
res.Set(c.P)
// If p ≡ 3 (mod 4) then
if pMod.Cmp(big.NewInt(3)) == 0 {
res.Add(res, big.NewInt(1))
} else { // if p ≡ 1 (mod 4)
res.Add(res, big.NewInt(-1))
}
res.Div(res, big.NewInt(4))
y.Exp(y, res, c.P)
return y, c.IsOnCurve(Point{X: x, Y: y})
}
// AddMod computes (x + y) mod curve.P
func (c *Curve) AddMod(x, y *big.Int) *big.Int {
z := new(big.Int)
z.Add(x, y)
z.Mod(z, c.P)
return z
}
// SubMod computes (x - y) mod curve.P
func (c *Curve) SubMod(x, y *big.Int) *big.Int {
z := new(big.Int)
z.Sub(x, y)
z.Mod(z, c.P)
return z
}
// MultMod computes (x * y) mod curve.P
func (c *Curve) MultMod(x, y *big.Int) *big.Int {
z := new(big.Int)
z.Mul(x, y)
z.Mod(z, c.P)
return z
}
// DivMod computes (x / y) mod curve.P
func (c *Curve) DivMod(x, y *big.Int) *big.Int {
z := new(big.Int)
z.Div(x, y)
z.Mod(z, c.P)
return z
}
// InvMod computes (1/X) mod curve.P
func (c *Curve) InvMod(x *big.Int) *big.Int {
z := new(big.Int)
z.ModInverse(x, c.P)
return z
}
// IsOnCurve checks if P is on elliptic curve
func (c *Curve) IsOnCurve(P Point) bool {
if P.IsInfinity() {
return false
}
y2 := c.MultMod(P.Y, P.Y) // y²
eq := c.MultMod(P.X, P.X) // x^2
eq = c.MultMod(eq, P.X) // x^3
eq = c.AddMod(eq, c.MultMod(c.A, P.X)) // x^3 + ax
eq = c.AddMod(eq, c.B) // x^3 + ax + b
// y² = x^3 + ax + b
return y2.Cmp(eq) == 0
}