Vous êtes sur la page 1sur 3

# C:\Users\yasser\Desktop\courbe elliptique.

py
001| def mod(a, b):
002| return a%b
003|
004|
005| def modinv(a, b):
006| for i in range(1,b):
007| r=mod(i*a, b)
008| if r==1:
009| return i
010| return None
011|
012|
013|
014|
015| def somme(A, B, P1, P2, q):
016| if P1[0] != P2[0] :
017| a=mod((mod(P2[1]-P1[1], q)*modinv(mod(P2[0]-P1[0], q), q))**2 -P1[0]-
P2[0], q)
018| P3=[a, mod(mod(P2[1]-P1[1], q)*modinv(P2[0]-P1[0], q)*mod(P1[0]-a, q)-
mod(P1[1], q), q)]
019| elif P1[0] == P2[0] :
020| if P1[1] != P2[1] :
021| P3=[0,0]
022| elif P1[1] == P2[1] :
023| if P1[1] != 0 :
024| b=mod(3*(P1[0])**2 +A, q)*modinv(2*P1[0], q)
025| P3=[mod(b**2 -2*P1[0], q),mod(b*(P1[0]-(b**2 -2*P1[0]))-P1[1],
q)]
026| elif P1[1] == 0 :
027| P3=[0,0]
028| return P3
029|
030|
031|
032|
033|
034| def multiplication(A, B, P, q, k):
035| if k==0:
036| return [0,0]
037| elif k>0:
038| l=P
039| for i in range(k):
040| l=somme(A, B, P, l, q)
041| elif k<0:
042| P1=[P[0],mod(-P[1], q)]
043| l=P1
044| for i in range(-k):
045| l=somme(A, B, P1, l, q)
046| return l
047|
048|
049|
050|
051| def pointcourbe(A, B, q):
052| l1=[]
053| l2=[]
054| l=[]
055| a=0
056| for i in range(q):
057| l1.append([i,mod(i**2, q)])
058| l2.append([i,mod((i**3) +(A*i) +B, q)])
059|
060| for i in range(len(l1)):
061| for j in range(len(l2)):
062| if l1[i][1]==l2[j][1]:
063| l.append([l2[j][0],l1[i][0]])

1
064| a=a+1
065| return [a,l]
066|
067|
068| from random import randint
069|
070| def pointaleatoire(A, B, q):
071| a=pointcourbe(A, B, q)
072| return a[1][randint(0,len(a[1]))]
073|
074|
075|
076| def echangecle(A, B, q, n, m, P):
077| print('Le point public est :', P)
078| P1 = multiplication(A, B, P, q, n)
079|
080|
081| P2 = multiplication(A, B, P, q, m)
082|
083|
084| Q = multiplication(A, B, P1, q, m)
085| print('La clé commune est :', Q)
086| return Q
087|
088|
089|
090| def codage(A, B, q, M, n, m, P):
091| Q = echangecle(A, B, q, n, m, P)
092| P1 = somme(A, B, M, Q, q)
093| print('le message codé est : ', P1)
094| return P1
095|
096|
097| def decodage(A, B, q, P1, n, m, P):
098| Q = echangecle(A, B, q, n, m, P)
099| P2=[Q[0],mod(-Q[1], q)]
100| P3 = somme(A, B, P1, P2, q)
101| print('le message décodé est ; ', P3)
102| return P3
103|
104|
105| def bsgs(A, B, q, Q, P):
106| l=[]
107| S=[0,0]
108| R=pointcourbe(A, B, q)
109| a=R[0]+1
110| m=int(a**0.5)+10
111| for i in range(m):
112| l.append(somme(A, B, S, P, q))
113| S=somme(A, B, S, P, q)
114| for j in range(m):
115| for k in range(len(l)):
116| if somme(A, B, Q, multiplication(A, B, P, q, -j*m), q)==l[k] :
117| return mod(k+j*m, a)
118| return None
119|
120|
121|
122|
123|
124|
125|
126|
127|
128|
129|
130|

2
131|
132|
133|
134|
135|
136|
137|
138|

Vous aimerez peut-être aussi