The following code i have written for scalar multiplication over elliptic curve for prime field 37 but it gives me error for some of the scalar. the method is LSB first.enter code here
import time
import sys
from sage.all import *
elliptic curve domain parameters, prime192v1
domain parameters
p=37 A=7 B=25 G=1 n=21
print"\n p=",p print"\n A=",A print"\n B=",B F=GF(p)
E = EllipticCurve( F, [A,B] ); print "\n Elliptic curve equation=", E
def point_add(P, Q):
x1 = P[0]
y1 = P[1]
x2 = Q[0]
y2 = Q[1]
# check P == O
if P == [0, 0]:
return Q
# check Q == O
if Q == [0, 0]:
return P
# check Q == -P
if x1 == x2 and y1 + y2 == 0:
return [0, 0]
# check P == Q
if P == Q:
return point_double(P)
slope = F((y2 - y1)/(x2 - x1))
x3 = F(slope**2 - x1 - x2)
y3 = F(slope * (x1 - x3) - y1)
return[x3 , y3]
def point_double(P):
#if P == [0, 0]:
#return [0, 0]
x1 = P[0]
y1 = P[1]
slope = F((3 * x1**2 + A)/(2 * y1))
x3 = F(slope**2 - 2 * x1)
y3 = F(slope * (x1 - x3) - y1)
return [x3, y3]
def scalar_mult(K,P): print('hi') b_k =ZZ(K).bits();print(b_k); #K.digits(2); l = len(b_k);l sum1 = 0*P;sum1 print(b_k[0]) if b_k[0] == 1: sum1 = P;sum1 else: sum1 = 0 Q = P;print(Q) for i in range(1,l): Q = point_double(Q);print(Q) if b_k[i] == 1: print (b_k[i]) #sum = point_add(sum,Q) sum1 = point_add(sum1,Q) print(sum1)#Q = point_double(Q);
return sum1
P = E(20,32);P#.random_point();P f1=P.order();f1 K =randint(1,f1);K answer1=K*P;answer1 start1 = time.time() answer = scalar_mult(K,P);answer end1 = time.time() print "\n Time Required to compute scalar multiplication: %s" % ( end1 - start1 ) answer2=E(answer);answer2