forked from itzmeanjan/ff-gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff_p.py
More file actions
100 lines (77 loc) · 3.12 KB
/
ff_p.py
File metadata and controls
100 lines (77 loc) · 3.12 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
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
#!/usr/bin/python3
'''
Make sure `make genlib` is run to generate shared object first,
which is used for invoking prime field arithmetic functions
on available default accelerator
'''
import ctypes
from genericpath import exists
from posixpath import abspath
class ff_p:
so_path: str = '../libff_p.so'
sycl_q: ctypes.c_void_p = None
so_lib: ctypes.CDLL = None
def __init__(self) -> None:
'''
Creates an instance of `ff_p` class, along with backend resource(s)
like setting up SYCL queue where compute jobs will be submitted,
getting shared library ready for forwarding future function invocations
'''
if not exists(self.so_path):
raise Exception(
f'failed to find shared library `{abspath(self.so_path)}`')
self.so_lib = ctypes.CDLL(self.so_path)
self.so_lib.make_queue.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
self.sycl_q = ctypes.c_void_p()
self.so_lib.make_queue(ctypes.byref(self.sycl_q))
if self.sycl_q.value == None:
raise Exception(f'failed to get default SYCL queue')
def add(self, a: int, b: int) -> int:
'''
Modular addition of two prime field elements
'''
self.so_lib.add.restype = ctypes.c_uint64
self.so_lib.add.argtypes = [
ctypes.c_void_p, ctypes.c_uint64, ctypes.c_uint64]
return self.so_lib.add(self.sycl_q, a, b)
def sub(self, a: int, b: int) -> int:
'''
Modular subtraction of two prime field elements
'''
self.so_lib.sub.restype = ctypes.c_uint64
self.so_lib.sub.argtypes = [
ctypes.c_void_p, ctypes.c_uint64, ctypes.c_uint64]
return self.so_lib.sub(self.sycl_q, a, b)
def multiply(self, a: int, b: int) -> int:
'''
Modular multiplication of two prime field elements
'''
self.so_lib.multiply.restype = ctypes.c_uint64
self.so_lib.multiply.argtypes = [
ctypes.c_void_p, ctypes.c_uint64, ctypes.c_uint64]
return self.so_lib.multiply(self.sycl_q, a, b)
def exponentiate(self, a: int, b: int) -> int:
'''
Modular exponentiation of one prime field element
by exponent (second operand)
'''
self.so_lib.exponentiate.restype = ctypes.c_uint64
self.so_lib.exponentiate.argtypes = [
ctypes.c_void_p, ctypes.c_uint64, ctypes.c_uint64]
return self.so_lib.exponentiate(self.sycl_q, a, b)
def inverse(self, a: int) -> int:
'''
Muliplicative identity of non-zero prime field element
'''
self.so_lib.inverse.restype = ctypes.c_uint64
self.so_lib.inverse.argtypes = [ctypes.c_void_p, ctypes.c_uint64]
return self.so_lib.inverse(self.sycl_q, a)
def divide(self, a: int, b: int) -> int:
'''
Modular division of one prime field element
by another one
'''
self.so_lib.divide.restype = ctypes.c_uint64
self.so_lib.divide.argtypes = [
ctypes.c_void_p, ctypes.c_uint64, ctypes.c_uint64]
return self.so_lib.divide(self.sycl_q, a, b)