-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
205 lines (172 loc) · 6.47 KB
/
setup.py
File metadata and controls
205 lines (172 loc) · 6.47 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Build script for dot-ring.
Builds:
1. Cython extensions for performance-critical code
2. blst library bindings (BLS12-381 cryptography)
Requirements:
- C compiler (gcc/clang)
- SWIG (for blst bindings)
- Cython
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
from Cython.Build import cythonize
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
def get_compile_args() -> list[str]:
if sys.platform == "win32":
return ["/O2", "/arch:AVX2", "/D_CRT_SECURE_NO_WARNINGS"]
args = ["-O3", "-ffast-math", "-flto"]
if sys.platform != "darwin":
args.append("-march=native")
return args
def build_cython_extensions() -> list[Extension]:
compile_args = get_compile_args()
return [
Extension(
"dot_ring.curve.field_arithmetic",
["dot_ring/curve/field_arithmetic.pyx"],
extra_compile_args=["-O3", "-ffast-math"],
),
Extension(
"dot_ring.curve.fast_math",
["dot_ring/curve/fast_math.pyx"],
extra_compile_args=["-O3", "-ffast-math"],
),
Extension(
"dot_ring.ring_proof.polynomial.ntt",
[
"dot_ring/ring_proof/polynomial/ntt.pyx",
"dot_ring/curve/native_field/bls12_381_scalar.c",
],
include_dirs=["dot_ring/curve/native_field"],
extra_compile_args=["-O3", "-ffast-math"],
),
Extension(
"dot_ring.ring_proof.polynomial.poly_ops",
["dot_ring/ring_proof/polynomial/poly_ops.pyx"],
extra_compile_args=compile_args,
extra_link_args=[] if sys.platform == "win32" else ["-flto"],
),
Extension(
"dot_ring.curve.native_field.scalar",
[
"dot_ring/curve/native_field/scalar.pyx",
"dot_ring/curve/native_field/bls12_381_scalar.c",
],
include_dirs=["dot_ring/curve/native_field"],
extra_compile_args=compile_args,
extra_link_args=[] if sys.platform == "win32" else ["-flto"],
),
Extension(
"dot_ring.curve.native_field.vector_ops",
[
"dot_ring/curve/native_field/vector_ops.pyx",
"dot_ring/curve/native_field/bls12_381_scalar.c",
],
include_dirs=["dot_ring/curve/native_field"],
extra_compile_args=compile_args,
extra_link_args=[] if sys.platform == "win32" else ["-flto"],
),
]
cython_extensions = build_cython_extensions()
class CustomBuildExt(build_ext):
"""Custom build that also compiles blst bindings."""
def run(self) -> None:
# Build blst FIRST so it's available for packaging
self.build_blst()
super().run()
self.copy_blst_to_build()
def copy_blst_to_build(self) -> None:
"""Copy blst artifacts to the build output directory."""
root_dir = Path(__file__).parent.absolute()
src_blst = root_dir / "dot_ring" / "blst"
if not src_blst.exists():
return
# Get the build lib directory
build_lib = Path(self.build_lib) / "dot_ring" / "blst"
build_lib.mkdir(parents=True, exist_ok=True)
# Copy all blst files to build directory
for f in src_blst.iterdir():
if f.is_file():
shutil.copy(f, build_lib / f.name)
print(f"Copied {f.name} to build directory")
def build_blst(self) -> None:
"""Build blst library bindings from source."""
root_dir = Path(__file__).parent.absolute()
dest_dir = root_dir / "dot_ring" / "blst"
blst_dir = root_dir / ".blst"
print("Building blst bindings...")
# Clone blst if not present
if not blst_dir.exists():
print("Cloning blst repository...")
subprocess.check_call(
[
"git",
"clone",
"--depth",
"1",
"https://github.com/supranational/blst.git",
str(blst_dir),
]
)
# Clean previous build artifacts (may be for different platform)
self._clean_blst_artifacts(blst_dir)
# Build blst
bindings_dir = blst_dir / "bindings" / "python"
run_me = bindings_dir / "run.me"
if not run_me.exists():
raise RuntimeError("blst/bindings/python/run.me not found")
os.chmod(run_me, 0o755)
if sys.platform == "win32":
subprocess.check_call([sys.executable, str(run_me)], cwd=bindings_dir)
else:
subprocess.check_call([str(run_me)], cwd=bindings_dir)
# Copy artifacts to dot_ring/blst
dest_dir.mkdir(parents=True, exist_ok=True)
shutil.copy(bindings_dir / "blst.py", dest_dir / "__init__.py")
# Copy shared library
copied = False
for ext in ["*.so", "*.dylib", "*.dll"]:
for f in bindings_dir.glob(ext):
print(f"Copying {f.name} to {dest_dir}")
shutil.copy(f, dest_dir / f.name)
copied = True
if not copied:
raise RuntimeError("Failed to build blst shared library")
def _clean_blst_artifacts(self, blst_dir: Path) -> None:
"""Clean previous blst build artifacts."""
for pattern in ["libblst.a", "**/*.o", "**/*.so", "**/*.dylib"]:
for f in blst_dir.glob(pattern):
f.unlink()
bindings_dir = blst_dir / "bindings" / "python"
for filename in ["blst.py", "blst_wrap.cpp"]:
path = bindings_dir / filename
if path.exists():
path.unlink()
if __name__ == "__main__":
setup(
name="dot-ring",
use_scm_version=True,
packages=find_packages(exclude=["tests*", "perf*"]) + ["dot_ring.blst"],
ext_modules=cythonize(
cython_extensions,
compiler_directives={
"language_level": "3",
"boundscheck": False,
"wraparound": False,
"cdivision": True,
},
),
cmdclass={"build_ext": CustomBuildExt},
package_data={
"dot_ring": ["py.typed"],
"dot_ring.blst": ["*.so", "*.dylib", "*.dll", "*.pyd"],
"dot_ring.vrf": ["data/*.bin"],
"dot_ring.ring_proof": ["columns/*.json"],
},
include_package_data=True,
)