-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathECC_demo.py
158 lines (133 loc) · 3.71 KB
/
ECC_demo.py
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
import time
#px, py are my generator point on the curve
point_list = []
#append the generator to my list
point_list.append((1,3))
def point_add(a, b, px, py, qx, qy):
#if ecc_fun(a, b, px, py ) == True and ecc_fun(a, b, qx, qy) == True:
if px != qx and py != qy:
g = point_one_R(px, py, qx, qy)
round(g[0], 4)
round(g[1], 4)
return g
#print(g)
else:
g = point_tan_R(a, b, px, py)
round(g[0], 4)
round(g[1], 4)
return g
#print(g)
#else:
#print("your input is invalid.")
#brutal force doing nP, which is P+(n-1)P
def cons_p(a,b,px,py,s):
for i in range(s-1):
qx = point_list[i][0]
qy = point_list[i][1]
new_g = point_add(a,b,px,py,qx, qy)
#print("new_g", new_g)
#round_g = (round(new_g[0], 8), round(new_g[1], 8))
#print(round_g)
point_list.append(new_g)
return point_list
#this part need to develop as an recursion.
def fast_np(a,b,px,py,s):
half = int(s/2)
half_index = int((s/2)-1)
if s % 2 == 0:
cons_p(a,b,px,py,half)[half_index]
double_x = (cons_p(a,b,px,py,half)[half_index])[0]
double_y = cons_p(a,b,px,py,half)[half_index][1]
g = point_tan_R(a,b,double_x, double_y)
return g
'''
elif s % 2 == 1:
half_odd = int((s-1)/2)
half_odd_index = int((s/2)-1)
cons_p(a,b,px,py,half_odd)[half_odd_index]
double_x = (cons_p(a,b,px,py,half_odd)[half_odd_index])[0]
double_y = cons_p(a,b,px,py,half_odd)[half_odd_index][1]
g = point_tan_R(a,b,double_x, double_y)
point_one_R()
'''
'''
def cons_4p():
qx = point_list[1][0]
qy = point_list[1][1]
return point_add(-7, 10, 1,2,qx,qy)
'''
def point_one_R(px, py, qx, qy):
m = (py-qy)/(px-qx)
rx = m**2-px-qx
ry = py+m*(rx-px)
return rx, -ry
#print(rx, -ry)
def point_tan_R(a, b, px, py):
m = (3*(px**2)+a)/(2*py)
rx = m**2-px-px
ry = py+m*(rx-px)
#print(rx, -ry)
return rx, -ry
#point_tan_R(-7, 10, 1,2)
'''
def point_mult_R(s,p):
for i in s:
point
'''
def ecc_fun(a, b, x, y):
if round(y**2,4) == round(x**3+a*x+b,4):
#print("True")
return True
g1 = point_add(0, 8, 1,3,1,3)
#point_list.append(g1)
#1000P, and print out the 1000th P
#print(fast_np(0,8,1,3,1000))
print((cons_p(0,8,1,3,2)[1])[0])
#the following is to calculate the n
#print(cons_p(0,8,1,3,100000)[99997])
def break_ecc(a, b, px, py):
for i in range(100000):
if (cons_p(a,b,px,py,i)[i-1]) == (6.424718772812134, 16.134112182199456):
#print(cons_p(a,b,px,py,i)[i-1])
#print(cons_p(a,b,px,py,i)[i-1])
#print(cons_p(0,8,1,3,10)[9])
print(i)
break
#print(i)
#else:
#print(cons_p(0,8,1,3,4)[3])
#print(cons_p(a,b,px,py,i)[i-1])
#print("no")
#break_ecc(0,8,1,3)
#print(cons_p(477))
time_lapse = []
def time_inv():
for i in range(1):
#cons_p(0,8,1,3,1000)[997]
start = time.time()
break_ecc(0,8,1,3)
end = time.time()
time_lapse.append(abs(end-start))
#print(abs(end-start))
print("the average time lapse is:", sum(time_lapse)/3)
time_inv()
'''
g2 = cons_p()
point_list.append(g2)
g3 = cons_4p()
point_list.append(g3)
print(point_list)
#cons_p()
print(point_list[1][0])'''
#print(g1)
#print(g2)
#print(point_list[97])
#print(point_add(0,8,1,3,-0.678,2.767))
#print(point_list[4])
#print(point_list[3][0])
#print(point_list[3][1])
#point_R(1,2,3,4)
#ecc_fun(-7, 10, -3, 2)
#testfy(-7, 10, )
#print(ecc_fun(-7, 10, -1, -4))
#print(ecc_fun(-7, 10, -3.16, -0.752))