UNPKG

6.37 kBJavaScriptView Raw
1var assert = require('assert')
2var BigInteger = require('bigi')
3
4var THREE = BigInteger.valueOf(3)
5
6function Point (curve, x, y, z) {
7 assert.notStrictEqual(z, undefined, 'Missing Z coordinate')
8
9 this.curve = curve
10 this.x = x
11 this.y = y
12 this.z = z
13 this._zInv = null
14
15 this.compressed = true
16}
17
18Object.defineProperty(Point.prototype, 'zInv', {
19 get: function () {
20 if (this._zInv === null) {
21 this._zInv = this.z.modInverse(this.curve.p)
22 }
23
24 return this._zInv
25 }
26})
27
28Object.defineProperty(Point.prototype, 'affineX', {
29 get: function () {
30 return this.x.multiply(this.zInv).mod(this.curve.p)
31 }
32})
33
34Object.defineProperty(Point.prototype, 'affineY', {
35 get: function () {
36 return this.y.multiply(this.zInv).mod(this.curve.p)
37 }
38})
39
40Point.fromAffine = function (curve, x, y) {
41 return new Point(curve, x, y, BigInteger.ONE)
42}
43
44Point.prototype.equals = function (other) {
45 if (other === this) return true
46 if (this.curve.isInfinity(this)) return this.curve.isInfinity(other)
47 if (this.curve.isInfinity(other)) return this.curve.isInfinity(this)
48
49 // u = Y2 * Z1 - Y1 * Z2
50 var u = other.y.multiply(this.z).subtract(this.y.multiply(other.z)).mod(this.curve.p)
51
52 if (u.signum() !== 0) return false
53
54 // v = X2 * Z1 - X1 * Z2
55 var v = other.x.multiply(this.z).subtract(this.x.multiply(other.z)).mod(this.curve.p)
56
57 return v.signum() === 0
58}
59
60Point.prototype.negate = function () {
61 var y = this.curve.p.subtract(this.y)
62
63 return new Point(this.curve, this.x, y, this.z)
64}
65
66Point.prototype.add = function (b) {
67 if (this.curve.isInfinity(this)) return b
68 if (this.curve.isInfinity(b)) return this
69
70 var x1 = this.x
71 var y1 = this.y
72 var x2 = b.x
73 var y2 = b.y
74
75 // u = Y2 * Z1 - Y1 * Z2
76 var u = y2.multiply(this.z).subtract(y1.multiply(b.z)).mod(this.curve.p)
77 // v = X2 * Z1 - X1 * Z2
78 var v = x2.multiply(this.z).subtract(x1.multiply(b.z)).mod(this.curve.p)
79
80 if (v.signum() === 0) {
81 if (u.signum() === 0) {
82 return this.twice() // this == b, so double
83 }
84
85 return this.curve.infinity // this = -b, so infinity
86 }
87
88 var v2 = v.square()
89 var v3 = v2.multiply(v)
90 var x1v2 = x1.multiply(v2)
91 var zu2 = u.square().multiply(this.z)
92
93 // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
94 var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.p)
95 // y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3
96 var y3 = x1v2.multiply(THREE).multiply(u).subtract(y1.multiply(v3)).subtract(zu2.multiply(u)).multiply(b.z).add(u.multiply(v3)).mod(this.curve.p)
97 // z3 = v^3 * z1 * z2
98 var z3 = v3.multiply(this.z).multiply(b.z).mod(this.curve.p)
99
100 return new Point(this.curve, x3, y3, z3)
101}
102
103Point.prototype.twice = function () {
104 if (this.curve.isInfinity(this)) return this
105 if (this.y.signum() === 0) return this.curve.infinity
106
107 var x1 = this.x
108 var y1 = this.y
109
110 var y1z1 = y1.multiply(this.z).mod(this.curve.p)
111 var y1sqz1 = y1z1.multiply(y1).mod(this.curve.p)
112 var a = this.curve.a
113
114 // w = 3 * x1^2 + a * z1^2
115 var w = x1.square().multiply(THREE)
116
117 if (a.signum() !== 0) {
118 w = w.add(this.z.square().multiply(a))
119 }
120
121 w = w.mod(this.curve.p)
122 // x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
123 var x3 = w.square().subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p)
124 // y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
125 var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(3)).mod(this.curve.p)
126 // z3 = 8 * (y1 * z1)^3
127 var z3 = y1z1.pow(3).shiftLeft(3).mod(this.curve.p)
128
129 return new Point(this.curve, x3, y3, z3)
130}
131
132// Simple NAF (Non-Adjacent Form) multiplication algorithm
133// TODO: modularize the multiplication algorithm
134Point.prototype.multiply = function (k) {
135 if (this.curve.isInfinity(this)) return this
136 if (k.signum() === 0) return this.curve.infinity
137
138 var e = k
139 var h = e.multiply(THREE)
140
141 var neg = this.negate()
142 var R = this
143
144 for (var i = h.bitLength() - 2; i > 0; --i) {
145 var hBit = h.testBit(i)
146 var eBit = e.testBit(i)
147
148 R = R.twice()
149
150 if (hBit !== eBit) {
151 R = R.add(hBit ? this : neg)
152 }
153 }
154
155 return R
156}
157
158// Compute this*j + x*k (simultaneous multiplication)
159Point.prototype.multiplyTwo = function (j, x, k) {
160 var i = Math.max(j.bitLength(), k.bitLength()) - 1
161 var R = this.curve.infinity
162 var both = this.add(x)
163
164 while (i >= 0) {
165 var jBit = j.testBit(i)
166 var kBit = k.testBit(i)
167
168 R = R.twice()
169
170 if (jBit) {
171 if (kBit) {
172 R = R.add(both)
173 } else {
174 R = R.add(this)
175 }
176 } else if (kBit) {
177 R = R.add(x)
178 }
179 --i
180 }
181
182 return R
183}
184
185Point.prototype.getEncoded = function (compressed) {
186 if (compressed == null) compressed = this.compressed
187 if (this.curve.isInfinity(this)) return new Buffer('00', 'hex') // Infinity point encoded is simply '00'
188
189 var x = this.affineX
190 var y = this.affineY
191
192 var buffer
193
194 // Determine size of q in bytes
195 var byteLength = Math.floor((this.curve.p.bitLength() + 7) / 8)
196
197 // 0x02/0x03 | X
198 if (compressed) {
199 buffer = new Buffer(1 + byteLength)
200 buffer.writeUInt8(y.isEven() ? 0x02 : 0x03, 0)
201
202 // 0x04 | X | Y
203 } else {
204 buffer = new Buffer(1 + byteLength + byteLength)
205 buffer.writeUInt8(0x04, 0)
206
207 y.toBuffer(byteLength).copy(buffer, 1 + byteLength)
208 }
209
210 x.toBuffer(byteLength).copy(buffer, 1)
211
212 return buffer
213}
214
215Point.decodeFrom = function (curve, buffer) {
216 var type = buffer.readUInt8(0)
217 var compressed = (type !== 4)
218
219 var byteLength = Math.floor((curve.p.bitLength() + 7) / 8)
220 var x = BigInteger.fromBuffer(buffer.slice(1, 1 + byteLength))
221
222 var Q
223 if (compressed) {
224 assert.equal(buffer.length, byteLength + 1, 'Invalid sequence length')
225 assert(type === 0x02 || type === 0x03, 'Invalid sequence tag')
226
227 var isOdd = (type === 0x03)
228 Q = curve.pointFromX(isOdd, x)
229 } else {
230 assert.equal(buffer.length, 1 + byteLength + byteLength, 'Invalid sequence length')
231
232 var y = BigInteger.fromBuffer(buffer.slice(1 + byteLength))
233 Q = Point.fromAffine(curve, x, y)
234 }
235
236 Q.compressed = compressed
237 return Q
238}
239
240Point.prototype.toString = function () {
241 if (this.curve.isInfinity(this)) return '(INFINITY)'
242
243 return '(' + this.affineX.toString() + ',' + this.affineY.toString() + ')'
244}
245
246module.exports = Point