Skip to content

Commit

Permalink
Add if name is main to prevent dot errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jellevos committed Nov 27, 2024
1 parent f6d2c5a commit 1e606c4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 70 deletions.
14 changes: 8 additions & 6 deletions oraqle/examples/wahc2024_presentation/1_high-level.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from oraqle.compiler.circuit import Circuit
from oraqle.compiler.nodes.leafs import Input

gf = GF(101)

alex = Input("a", gf)
blake = Input("b", gf)
if __name__ == "__main__":
gf = GF(101)

output = alex < blake
circuit = Circuit(outputs=[output])
alex = Input("a", gf)
blake = Input("b", gf)

circuit.to_svg("high_level.svg")
output = alex < blake
circuit = Circuit(outputs=[output])

circuit.to_svg("high_level.svg")
63 changes: 32 additions & 31 deletions oraqle/examples/wahc2024_presentation/2_arith_step1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,48 @@
from oraqle.compiler.comparison.comparison import SemiStrictComparison
from oraqle.compiler.nodes.leafs import Constant, Input

gf = GF(101)
if __name__ == "__main__":
gf = GF(101)

alex = Input("a", gf)
blake = Input("b", gf)
alex = Input("a", gf)
blake = Input("b", gf)

output = alex < blake
output = alex < blake


p = output._gf.characteristic
p = output._gf.characteristic

if output._less_than:
left = output._left
right = output._right
else:
left = output._right
right = output._left
if output._less_than:
left = output._left
right = output._right
else:
left = output._right
right = output._left

left = left.arithmetize("best-effort")
right = right.arithmetize("best-effort")
left = left.arithmetize("best-effort")
right = right.arithmetize("best-effort")

left_is_small = SemiStrictComparison(
left, Constant(output._gf(p // 2)), less_than=True, gf=output._gf
)
right_is_small = SemiStrictComparison(
right, Constant(output._gf(p // 2)), less_than=True, gf=output._gf
)
left_is_small = SemiStrictComparison(
left, Constant(output._gf(p // 2)), less_than=True, gf=output._gf
)
right_is_small = SemiStrictComparison(
right, Constant(output._gf(p // 2)), less_than=True, gf=output._gf
)

# Test whether left and right are in the same range
same_range = (left_is_small & right_is_small) + (
Neg(left_is_small, output._gf) & Neg(right_is_small, output._gf)
)
# Test whether left and right are in the same range
same_range = (left_is_small & right_is_small) + (
Neg(left_is_small, output._gf) & Neg(right_is_small, output._gf)
)

# Performs left < right on the reduced inputs, note that if both are in the upper half the difference is still small enough for a semi-comparison
comparison = SemiStrictComparison(left, right, less_than=True, gf=output._gf)
result = same_range * comparison
# Performs left < right on the reduced inputs, note that if both are in the upper half the difference is still small enough for a semi-comparison
comparison = SemiStrictComparison(left, right, less_than=True, gf=output._gf)
result = same_range * comparison

# Performs left < right when one if small and the other is large
right_is_larger = left_is_small & Neg(right_is_small, output._gf)
result += right_is_larger
# Performs left < right when one if small and the other is large
right_is_larger = left_is_small & Neg(right_is_small, output._gf)
result += right_is_larger


circuit = Circuit(outputs=[result])
circuit = Circuit(outputs=[result])

circuit.to_svg("arith_step1.svg")
circuit.to_svg("arith_step1.svg")
21 changes: 11 additions & 10 deletions oraqle/examples/wahc2024_presentation/3_arith_step2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
from oraqle.compiler.circuit import Circuit
from oraqle.compiler.nodes.leafs import Input

gf = GF(101)
if __name__ == "__main__":
gf = GF(101)

alex = Input("a", gf)
blake = Input("b", gf)
alex = Input("a", gf)
blake = Input("b", gf)

output = alex < blake
output = alex < blake

front = output.arithmetize_depth_aware(cost_of_squaring=1.0)
print(front)
front = output.arithmetize_depth_aware(cost_of_squaring=1.0)
print(front)

_, tup = front._nodes_by_depth.popitem()
_, node = tup
circuit = Circuit(outputs=[node])
_, tup = front._nodes_by_depth.popitem()
_, node = tup
circuit = Circuit(outputs=[node])

circuit.to_svg("arith_step2.svg")
circuit.to_svg("arith_step2.svg")
17 changes: 9 additions & 8 deletions oraqle/examples/wahc2024_presentation/5_code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
from oraqle.compiler.circuit import Circuit
from oraqle.compiler.nodes.leafs import Input

gf = GF(101)
if __name__ == "__main__":
gf = GF(101)

alex = Input("a", gf)
blake = Input("b", gf)
alex = Input("a", gf)
blake = Input("b", gf)

output = alex < blake
circuit = Circuit(outputs=[output])
output = alex < blake
circuit = Circuit(outputs=[output])

front = circuit.arithmetize_depth_aware()
front = circuit.arithmetize_depth_aware()

for _, _, arithmetic_circuit in front:
program = arithmetic_circuit.generate_code("example.cpp")
for _, _, arithmetic_circuit in front:
program = arithmetic_circuit.generate_code("example.cpp")
31 changes: 16 additions & 15 deletions oraqle/examples/wahc2024_presentation/rebalancing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
from oraqle.compiler.circuit import Circuit
from oraqle.compiler.nodes.leafs import Input

gf = GF(101)
if __name__ == "__main__":
gf = GF(101)

a = Input("a", gf)
b = Input("b", gf)
c = Input("c", gf)
d = Input("d", gf)
a = Input("a", gf)
b = Input("b", gf)
c = Input("c", gf)
d = Input("d", gf)

output = a * b * c * d
circuit_good = Circuit(outputs=[output])
circuit_good = circuit_good.arithmetize_depth_aware() # FIXME: This should also work with arithmetize
circuit_good[0][2].to_svg("rebalancing_good.svg")
output = a * b * c * d
circuit_good = Circuit(outputs=[output])
circuit_good = circuit_good.arithmetize_depth_aware() # FIXME: This should also work with arithmetize
circuit_good[0][2].to_svg("rebalancing_good.svg")

ab = a.mul(b, flatten=False)
abc = ab.mul(c, flatten=False)
abcd = abc.mul(d, flatten=False)
circuit_bad = Circuit(outputs=[abcd])
circuit_bad = circuit_bad.arithmetize()
circuit_bad.to_svg("rebalancing_bad.svg")
ab = a.mul(b, flatten=False)
abc = ab.mul(c, flatten=False)
abcd = abc.mul(d, flatten=False)
circuit_bad = Circuit(outputs=[abcd])
circuit_bad = circuit_bad.arithmetize()
circuit_bad.to_svg("rebalancing_bad.svg")

0 comments on commit 1e606c4

Please sign in to comment.