69
72
# self.curve is allowed to be None only for INFINITY:
70
73
if self.__curve: assert self.__curve.contains_point( x, y )
71
74
if order: assert self * order == INFINITY
73
def __cmp__( self, other ):
74
"""Return 0 if the points are identical, 1 otherwise."""
76
def __eq__( self, other ):
77
"""Return True if the points are identical, False otherwise."""
75
78
if self.__curve == other.__curve \
76
79
and self.__x == other.__x \
77
80
and self.__y == other.__y:
82
85
def __add__( self, other ):
83
86
"""Add one point to another point."""
87
90
if other == INFINITY: return self
101
104
x3 = ( l * l - self.__x - other.__x ) % p
102
105
y3 = ( l * ( self.__x - x3 ) - self.__y ) % p
104
107
return Point( self.__curve, x3, y3 )
106
109
def __mul__( self, other ):
124
127
negative_self = Point( self.__curve, self.__x, -self.__y, self.__order )
125
128
i = leftmost_bit( e3 ) // 2
127
# print "Multiplying %s by %d (e3 = %d):" % ( self, other, e3 )
130
# print_("Multiplying %s by %d (e3 = %d):" % ( self, other, e3 ))
129
132
result = result.double()
130
133
if ( e3 & i ) != 0 and ( e & i ) == 0: result = result + self
131
134
if ( e3 & i ) == 0 and ( e & i ) != 0: result = result + negative_self
132
# print ". . . i = %d, result = %s" % ( i, result )
135
# print_(". . . i = %d, result = %s" % ( i, result ))
137
140
def __rmul__( self, other ):
138
141
"""Multiply a point by an integer."""
140
143
return self * other
142
145
def __str__( self ):
186
189
p1 = Point( c, x1, y1 )
187
190
p2 = Point( c, x2, y2 )
189
print "%s + %s = %s" % ( p1, p2, p3 ),
192
print_("%s + %s = %s" % ( p1, p2, p3 ), end=' ')
190
193
if p3.x() != x3 or p3.y() != y3:
191
194
raise FailedTest("Failure: should give (%d,%d)." % ( x3, y3 ))
195
198
def test_double( c, x1, y1, x3, y3 ):
196
199
"""We expect that on curve c, 2*(x1,y1) = (x3, y3)."""
197
200
p1 = Point( c, x1, y1 )
199
print "%s doubled = %s" % ( p1, p3 ),
202
print_("%s doubled = %s" % ( p1, p3 ), end=' ')
200
203
if p3.x() != x3 or p3.y() != y3:
201
204
raise FailedTest("Failure: should give (%d,%d)." % ( x3, y3 ))
205
208
def test_double_infinity( c ):
206
209
"""We expect that on curve c, 2*INFINITY = INFINITY."""
209
print "%s doubled = %s" % ( p1, p3 ),
212
print_("%s doubled = %s" % ( p1, p3 ), end=' ')
210
213
if p3.x() != INFINITY.x() or p3.y() != INFINITY.y():
211
214
raise FailedTest("Failure: should give (%d,%d)." % ( INFINITY.x(), INFINITY.y() ))
215
218
def test_multiply( c, x1, y1, m, x3, y3 ):
216
219
"""We expect that on curve c, m*(x1,y1) = (x3,y3)."""
217
220
p1 = Point( c, x1, y1 )
219
print "%s * %d = %s" % ( p1, m, p3 ),
222
print_("%s * %d = %s" % ( p1, m, p3 ), end=' ')
220
223
if p3.x() != x3 or p3.y() != y3:
221
224
raise FailedTest("Failure: should give (%d,%d)." % ( x3, y3 ))
226
229
# A few tests from X9.62 B.3:
241
244
for i in range( 7 + 1 ):
242
245
p = ( i % 7 ) * g
243
print "%s * %d = %s, expected %s . . ." % ( g, i, p, check ),
246
print_("%s * %d = %s, expected %s . . ." % ( g, i, p, check ), end=' ')
247
250
raise FailedTest("Bad.")
248
251
check = check + g
250
253
# NIST Curve P-192:
251
p = 6277101735386680763835789423207666416083908700390324961279L
252
r = 6277101735386680763835789423176059013767194773182842284081L
253
s = 0x3045ae6fc8422f64ed579528d38120eae12196d5L
254
c = 0x3099d2bbbfcb2538542dcd5fb078b6ef5f3d6fe2c745de65L
255
b = 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1L
256
Gx = 0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012L
257
Gy = 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811L
254
p = 6277101735386680763835789423207666416083908700390324961279
255
r = 6277101735386680763835789423176059013767194773182842284081
256
#s = 0x3045ae6fc8422f64ed579528d38120eae12196d5L
257
c = 0x3099d2bbbfcb2538542dcd5fb078b6ef5f3d6fe2c745de65
258
b = 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1
259
Gx = 0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012
260
Gy = 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811
259
262
c192 = CurveFp( p, -3, b )
260
263
p192 = Point( c192, Gx, Gy, r )
262
265
# Checking against some sample computations presented
265
d = 651056770906015076056810763456358567190100156695615665659L
268
d = 651056770906015076056810763456358567190100156695615665659
267
if Q.x() != 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5L:
270
if Q.x() != 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5:
268
271
raise FailedTest("p192 * d came out wrong.")
270
print "p192 * d came out right."
273
print_("p192 * d came out right.")
272
k = 6140507067065001063065065565667405560006161556565665656654L
275
k = 6140507067065001063065065565667405560006161556565665656654
274
if R.x() != 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEADL \
275
or R.y() != 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835L:
277
if R.x() != 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD \
278
or R.y() != 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835:
276
279
raise FailedTest("k * p192 came out wrong.")
278
print "k * p192 came out right."
281
print_("k * p192 came out right.")
280
u1 = 2563697409189434185194736134579731015366492496392189760599L
281
u2 = 6266643813348617967186477710235785849136406323338782220568L
283
u1 = 2563697409189434185194736134579731015366492496392189760599
284
u2 = 6266643813348617967186477710235785849136406323338782220568
282
285
temp = u1 * p192 + u2 * Q
283
if temp.x() != 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEADL \
284
or temp.y() != 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835L:
286
if temp.x() != 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD \
287
or temp.y() != 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835:
285
288
raise FailedTest("u1 * p192 + u2 * Q came out wrong.")
287
print "u1 * p192 + u2 * Q came out right."
290
print_("u1 * p192 + u2 * Q came out right.")
289
292
if __name__ == "__main__":