Skip to content

Commit

Permalink
Fix ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jellevos committed Jan 29, 2025
1 parent a319847 commit a329889
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions oraqle/add_chains/addition_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions oraqle/add_chains/memoization.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 10 additions & 2 deletions oraqle/compiler/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion oraqle/compiler/comparison/in_upper_half.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions oraqle/compiler/polynomials/univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import random
import subprocess
import time
from typing import Dict

Expand All @@ -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]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}) ---")
Expand All @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@

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(),
iz21_arithmetization.multiplicative_size(),
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(),
Expand Down
1 change: 1 addition & 0 deletions oraqle/helib_template/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Template containing all the things to build an HElib program."""

0 comments on commit a329889

Please sign in to comment.