diff --git a/oraqle/add_chains/addition_chains.py b/oraqle/add_chains/addition_chains.py index 9b0baca..da2ff65 100644 --- a/oraqle/add_chains/addition_chains.py +++ b/oraqle/add_chains/addition_chains.py @@ -24,18 +24,18 @@ def thurber_bounds(target: int, max_size: int) -> List[Tuple[int, int]]: denominator = (1 << (t + 1)) * ((1 << (max_size - t - (step + 2))) + 1) else: denominator = (1 << t) * ((1 << (max_size - t - (step + 1))) + 1) - bound = int(math.ceil(target / denominator)) + bound = math.ceil(target / denominator) bounds.append((bound, min(1 << step, target))) step = max_size - t - 2 if step > 0: denominator = (1 << t) * ((1 << (max_size - t - (step + 1))) + 1) - bound = int(math.ceil(target / denominator)) + bound = math.ceil(target / denominator) bounds.append((bound, min(1 << step, target))) if max_size - t - 1 > 0: for step in range(max_size - t - 1, max_size + 1): - bound = int(math.ceil(target / (1 << (max_size - step)))) + bound = math.ceil(target / (1 << (max_size - step))) bounds.append((bound, min(1 << step, target))) return bounds diff --git a/oraqle/add_chains/memoization.py b/oraqle/add_chains/memoization.py index ab067e1..afaedf5 100644 --- a/oraqle/add_chains/memoization.py +++ b/oraqle/add_chains/memoization.py @@ -1,9 +1,7 @@ """This module contains tools for memoizing addition chains, as these are expensive to compute.""" from hashlib import sha3_256 -import importlib.resources from importlib.resources import files import inspect -import os import shelve from typing import Set diff --git a/oraqle/compiler/circuit.py b/oraqle/compiler/circuit.py index fcdad1d..b466f9e 100644 --- a/oraqle/compiler/circuit.py +++ b/oraqle/compiler/circuit.py @@ -415,6 +415,14 @@ def run_using_helib(self, measure_time: bool = False, decrypt_outputs: bool = False, **kwargs) -> float: + """Generate a program using HElib and execute it, measuring the average run time. + + Raises: + Exception: If an error occured during the build or execution. + + Returns: + Average run time in seconds as a float + """ assert measure_time assert not decrypt_outputs @@ -440,7 +448,7 @@ def run_using_helib(self, executable_path = os.path.join(build_dir, "build", "main") program_args = [f"{keyword}={value}" for keyword, value in kwargs.items()] print(f"Build completed. Running with parameters: {', '.join(program_args)}...") - result = subprocess.run([executable_path] + program_args, check=True, text=True, capture_output=True) + result = subprocess.run([executable_path, *program_args], check=True, text=True, capture_output=True) # Check that all ciphertexts are valid lines = result.stdout.splitlines() @@ -460,7 +468,7 @@ def run_using_helib(self, print(result.stdout) except Exception: pass - raise Exception("Cannot continue since an error occured.") + raise Exception("Cannot continue since an error occured.") from e finally: os.chdir(original_directory) diff --git a/oraqle/compiler/comparison/in_upper_half.py b/oraqle/compiler/comparison/in_upper_half.py index 25f5812..899e3bf 100644 --- a/oraqle/compiler/comparison/in_upper_half.py +++ b/oraqle/compiler/comparison/in_upper_half.py @@ -8,7 +8,7 @@ from oraqle.add_chains.solving import extract_indices from oraqle.compiler.nodes.abstract import CostParetoFront, Node from oraqle.compiler.nodes.binary_arithmetic import Addition, Multiplication -from oraqle.compiler.nodes.leafs import Constant, Input +from oraqle.compiler.nodes.leafs import Input from oraqle.compiler.nodes.unary_arithmetic import ConstantMultiplication from oraqle.compiler.nodes.univariate import UnivariateNode from oraqle.compiler.polynomials.univariate import UnivariatePoly, _eval_poly diff --git a/oraqle/compiler/polynomials/univariate.py b/oraqle/compiler/polynomials/univariate.py index cd306c8..12e94e1 100644 --- a/oraqle/compiler/polynomials/univariate.py +++ b/oraqle/compiler/polynomials/univariate.py @@ -124,7 +124,7 @@ def arithmetize_custom(self, strategy: str) -> Tuple[ArithmeticNode, Dict[int, A lowest_multiplicative_size = 1_000_000_000 # TODO: Not elegant optimal_k = math.sqrt(2 * len(self._coefficients)) - bound = min(int(math.ceil(PS_METHOD_FACTOR_K * optimal_k)), len(self._coefficients)) + bound = min(math.ceil(PS_METHOD_FACTOR_K * optimal_k), len(self._coefficients)) for k in range(1, bound): ( arithmetization, @@ -178,7 +178,7 @@ def arithmetize_depth_aware_custom( for _, _, x in self._node.arithmetize_depth_aware(cost_of_squaring): optimal_k = math.sqrt(2 * len(self._coefficients)) - bound = min(int(math.ceil(PS_METHOD_FACTOR_K * optimal_k)), len(self._coefficients)) + bound = min(math.ceil(PS_METHOD_FACTOR_K * optimal_k), len(self._coefficients)) for k in range(1, bound): ( arithmetization, diff --git a/oraqle/experiments/depth_aware_arithmetization/execution/bench_cardio_circuits.py b/oraqle/experiments/depth_aware_arithmetization/execution/bench_cardio_circuits.py index 2d668a2..daed526 100644 --- a/oraqle/experiments/depth_aware_arithmetization/execution/bench_cardio_circuits.py +++ b/oraqle/experiments/depth_aware_arithmetization/execution/bench_cardio_circuits.py @@ -1,5 +1,4 @@ import random -import subprocess import time from typing import Dict @@ -9,7 +8,7 @@ construct_cardio_elevated_risk_circuit, construct_cardio_risk_circuit, ) -from oraqle.compiler.circuit import ArithmeticCircuit, Circuit +from oraqle.compiler.circuit import Circuit def gen_params() -> Dict[str, int]: diff --git a/oraqle/experiments/depth_aware_arithmetization/execution/cardio_circuits.py b/oraqle/experiments/depth_aware_arithmetization/execution/cardio_circuits.py index 31119b0..d53bc56 100644 --- a/oraqle/experiments/depth_aware_arithmetization/execution/cardio_circuits.py +++ b/oraqle/experiments/depth_aware_arithmetization/execution/cardio_circuits.py @@ -21,7 +21,6 @@ for depth, cost, arithmetic_circuit in front: print(depth, cost) - # print('params', arithmetic_circuit.generate_code("temp.cpp", 10, True, False)) arithmetic_circuit.to_graph(f"cardio_arith_d{depth}_c{cost}.dot") print(f"--- Cardio elevated risk assessment ({cost_of_squaring}) ---") @@ -33,5 +32,4 @@ for depth, cost, arithmetic_circuit in front: print(depth, cost) - # print('params', arithmetic_circuit.generate_code("temp.cpp", 10, True, False)) arithmetic_circuit.to_graph(f"cardio_elevated_arith_d{depth}_c{cost}.dot") diff --git a/oraqle/experiments/depth_aware_arithmetization/execution/comparisons.py b/oraqle/experiments/depth_aware_arithmetization/execution/comparisons.py index 49f064f..c7a685f 100644 --- a/oraqle/experiments/depth_aware_arithmetization/execution/comparisons.py +++ b/oraqle/experiments/depth_aware_arithmetization/execution/comparisons.py @@ -44,7 +44,6 @@ iz21_circuit = Circuit([IliashenkoZuccaSemiLessThan(x, y, gf)]) iz21_arithmetization = iz21_circuit.arithmetize() - #iz21_arithmetization.to_graph(f"comp_{p}_iz21.dot") print( "IZ21 circuits:", iz21_arithmetization.multiplicative_depth(), @@ -52,7 +51,6 @@ iz21_arithmetization.run_using_helib(iterations=iterations, measure_time=True, x=15, y=22) ) iz21_arithmetization.eliminate_subexpressions() - #iz21_arithmetization.to_graph(f"comp_{p}_iz21_cse.dot") print( "IZ21 circuit CSE:", iz21_arithmetization.multiplicative_depth(), diff --git a/oraqle/helib_template/__init__.py b/oraqle/helib_template/__init__.py index e69de29..3e9cfa2 100644 --- a/oraqle/helib_template/__init__.py +++ b/oraqle/helib_template/__init__.py @@ -0,0 +1 @@ +"""Template containing all the things to build an HElib program."""