forked from itzmeanjan/ff-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff_p_test.py
More file actions
75 lines (57 loc) · 1.88 KB
/
ff_p_test.py
File metadata and controls
75 lines (57 loc) · 1.88 KB
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
#!/usr/bin/python3
import galois as gl
from ff_p import ff_p
from time import time
from random import randint, seed
MIN = 0
MAX = (1 << 64) - 1
MOD = (1 << 64) - (1 << 32) + 1
ROUNDS = 1 << 10
def main():
seed(time())
gf = gl.GF(MOD)
ff = ff_p()
start = time()
for _ in range(ROUNDS):
a = randint(MIN, MAX)
b = randint(MIN, MAX)
assert (gf(a % MOD) + gf(b % MOD)) == ff.add(a, b)
print(
f'passed {ROUNDS} randomized {"addition":>16} tests !\t{time() - start:>16.2f} s')
start = time()
for _ in range(ROUNDS):
a = randint(MIN, MAX)
b = randint(MIN, MAX)
assert (gf(a % MOD) - gf(b % MOD)) == ff.sub(a, b)
print(
f'passed {ROUNDS} randomized {"subtraction":>16} tests !\t{time() - start:>16.2f} s')
start = time()
for _ in range(ROUNDS):
a = randint(MIN, MAX)
b = randint(MIN, MAX)
assert (gf(a % MOD) * gf(b % MOD)) == ff.multiply(a, b)
print(
f'passed {ROUNDS} randomized {"multiplication":>16} tests !\t{time() - start:>16.2f} s')
start = time()
for _ in range(ROUNDS):
a = randint(MIN, MAX)
b = randint(MIN, MAX)
assert (gf(a % MOD) ** b) == ff.exponentiate(a, b)
print(
f'passed {ROUNDS} randomized {"exponentiation":>16} tests !\t{time() - start:>16.2f} s')
start = time()
one = gf(1)
for _ in range(ROUNDS):
a = randint(MIN, MAX)
assert (one / gf(a % MOD)) == ff.inverse(a)
print(
f'passed {ROUNDS} randomized {"inversion":>16} tests !\t{time() - start:>16.2f} s')
start = time()
for _ in range(ROUNDS):
a = randint(MIN, MAX)
b = randint(MIN, MAX)
assert (gf(a % MOD) / gf(b % MOD)) == ff.divide(a, b)
print(
f'passed {ROUNDS} randomized {"division":>16} tests !\t{time() - start:>16.2f} s')
if __name__ == '__main__':
main()