-
Notifications
You must be signed in to change notification settings - Fork 1
/
fp.py
83 lines (70 loc) · 1.5 KB
/
fp.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
from parameters import *
add_counter = 0
mul_counter = 0
sqr_counter = 0
jac_counter = 0
inv_counter = 0
sqrt_counter = 0
def init_counters():
global add_counter
global mul_counter
global sqr_counter
global jac_counter
global inv_counter
global sqrt_counter
add_counter = 0
mul_counter = 0
sqr_counter = 0
jac_counter = 0
inv_counter = 0
sqrt_counter = 0
return
def print_counters():
global add_counter
global mul_counter
global sqr_counter
global jac_counter
global inv_counter
global sqrt_counter
print(str(add_counter)+"A + "+str(mul_counter)+"M + "+str(sqr_counter)+"S + "+str(jac_counter)+"J + "+str(inv_counter)+"I + "+str(sqrt_counter)+"R")
def fp_add(a, b):
global add_counter
add_counter += 1
return a+b
def fp_sub(a, b):
global add_counter
add_counter += 1
return a-b
def fp_neg(a):
global add_counter
add_counter += 1
return -a
def fp_mul(a, b):
global mul_counter
mul_counter += 1
return a*b
def fp_sqr(a):
global sqr_counter
sqr_counter += 1
return a**2
def fp_inv(a):
global inv_counter
inv_counter += 1
return 1/a
def fp_sqrt(a):
global sqrt_counter
sqrt_counter += 1
return a.nth_root(2)
def fp_jacobi(a):
global jac_counter
jac_counter += 1
return a.is_square()
def fp_cswap(c, a, b):
# Change this to constant time!
if c:
x = a
a = b
b = x
return a,b
def fp_isodd(a):
return int(a)%2 == 1