Skip to content

Commit

Permalink
Add run method to arithmetic circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
jellevos committed Jan 22, 2025
1 parent a2cb549 commit 52f536a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions oraqle/compiler/circuit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""This module contains classes for representing circuits."""
import importlib.resources
import os
import shutil
import subprocess
import tempfile
from typing import Dict, List, Optional, Tuple
Expand Down Expand Up @@ -405,6 +408,57 @@ def generate_code(
file.write(helib_postamble)

return params

def run_using_helib(self,
iterations: int = 1,
measure_time: bool = False,
decrypt_outputs: bool = False,
**kwargs) -> float:
assert measure_time
assert not decrypt_outputs

try:
with tempfile.TemporaryDirectory() as temp_dir:
# Copy the template folder to the temporary directory
build_dir = os.path.join(temp_dir, "build")
with importlib.resources.path('oraqle.helib_template', '') as template_path:
shutil.copytree(template_path, build_dir)

# Generate the main.cpp file
main_cpp_path = os.path.join(build_dir, "main.cpp")
self.generate_code(main_cpp_path, iterations, measure_time, decrypt_outputs)

# Call cmake and build
os.chdir(build_dir)
subprocess.run(["cmake", "-S", ".", "-B", "build"], check=True)
subprocess.run(["cmake", "--build", "build"], check=True)

# Run the executable
executable_path = os.path.join(build_dir, "build", "main")
program_args = [f"{keyword}={value}" for keyword, value in kwargs.items()]
result = subprocess.run([executable_path] + program_args, check=True, text=True, capture_output=True)

print(result.stdout)

lines = result.stdout.splitlines()
for line in lines[:-1]:
assert line.endswith("1")

run_time = float(lines[-1]) / iterations
return run_time

except subprocess.CalledProcessError as e:
print("An error occurred during the build or execution process.")
print(e)
try:
print("stderr:")
print(result.stderr)
print()
print("stdout:")
print(result.stdout)
except Exception:
pass
raise Exception("Cannot continue since an error occured.")


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
print("Our circuits:", our_front)

our_front[0][2].to_graph(f"comp_{p}_ours.dot")
for _, _, circ in our_front:
circ.run_using_helib(measure_time=True, x=15, y=22)

t2_circuit = Circuit([T2SemiLessThan(x, y, gf)])
t2_arithmetization = t2_circuit.arithmetize()
Expand Down
Empty file.

0 comments on commit 52f536a

Please sign in to comment.